From patchwork Fri Aug 27 19:30:56 2021 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 8bit X-Patchwork-Submitter: Jeff Law X-Patchwork-Id: 1521780 Return-Path: X-Original-To: incoming@patchwork.ozlabs.org Delivered-To: patchwork-incoming@bilbo.ozlabs.org Authentication-Results: ozlabs.org; spf=pass (sender SPF authorized) smtp.mailfrom=gcc.gnu.org (client-ip=8.43.85.97; helo=sourceware.org; envelope-from=gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org; receiver=) Authentication-Results: ozlabs.org; dkim=pass (1024-bit key; unprotected) header.d=gcc.gnu.org header.i=@gcc.gnu.org header.a=rsa-sha256 header.s=default header.b=lF+7V3Oi; dkim-atps=neutral Received: from sourceware.org (server2.sourceware.org [8.43.85.97]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (No client certificate requested) by ozlabs.org (Postfix) with ESMTPS id 4Gx8wF0Pbfz9sXM for ; Sat, 28 Aug 2021 05:31:24 +1000 (AEST) Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A210D3858006 for ; Fri, 27 Aug 2021 19:31:21 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A210D3858006 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1630092681; bh=cNATNM1U999xa93RKKYhuiELVgHmeGIA6pMIvBEceSI=; h=Subject:To:Date:List-Id:List-Unsubscribe:List-Archive:List-Post: List-Help:List-Subscribe:From:Reply-To:From; b=lF+7V3OicBpc0fM4luHnsNcEk2C6v1uGGSOpsficMedG7ZSOd7b8+KE2Ls2UsO26p ItdNtQjRvu3EuVZkMvPeuOok4PTJ/nDJ6GZ8rDboLpL0UNHWYNi1s9XE3JsknvX9b3 RrahkevjGmQIU9vonkfIRYCAEwS2ANbR30ZVawJA= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mail-pj1-x1034.google.com (mail-pj1-x1034.google.com [IPv6:2607:f8b0:4864:20::1034]) by sourceware.org (Postfix) with ESMTPS id 7E14D3858D34 for ; Fri, 27 Aug 2021 19:30:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7E14D3858D34 Received: by mail-pj1-x1034.google.com with SMTP id n13-20020a17090a4e0d00b0017946980d8dso9534975pjh.5 for ; Fri, 27 Aug 2021 12:30:59 -0700 (PDT) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:subject:to:message-id:date:user-agent :mime-version:content-language; bh=cNATNM1U999xa93RKKYhuiELVgHmeGIA6pMIvBEceSI=; b=PEMJmZJlNn79LD5sn3odvKaAXmBI4Yd7/9MUw/8T7g3pgmB7U9NsJhBxAyfmCrZNFp QA7ZBlcsMnKrzF6f6fBGi6BjavMLV89LRZ6veTpuxh/Rs1pLF+SlR7+2VKmVj+vgQ11T m7stbPhQUNYw7zG1VxnWXBwNztKXSWBL7M4a2xVoaNepd1txKJNMgvTpm6MAjfEkTc0G frlWBJGkuV7M5TUjmbMCKhZRmPsSRi5jfZn4QYGQMNLTgbJ3JS/S2gbfZCiYvWTkvMrw rzbnr95duxQnpvZ2IGL5inLLPprF7Mc/3fy/p8ONZz5ny4mrjL/nX5NVWc0z0OWjBwLr XtdA== X-Gm-Message-State: AOAM530hl43CdO4j3opEiSJu+8s6BJZCZM5KtynfMl70oPs0dVZ0pFj+ dAj5CSxWray/N5RHxyPrujpcZhTXY2uNTg== X-Google-Smtp-Source: ABdhPJya0gz19LfAkyNn5U9bOEgkJyD5w5kFS8u4xTF0yejSBF6ACdvuo5sm2Twl0fud/U5+6gBC/w== X-Received: by 2002:a17:902:ea0d:b0:137:9eb6:c9c5 with SMTP id s13-20020a170902ea0d00b001379eb6c9c5mr9931781plg.7.1630092658084; Fri, 27 Aug 2021 12:30:58 -0700 (PDT) Received: from [172.31.0.175] (c-98-202-48-222.hsd1.ut.comcast.net. [98.202.48.222]) by smtp.gmail.com with ESMTPSA id t15sm7450952pgk.13.2021.08.27.12.30.57 for (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Fri, 27 Aug 2021 12:30:57 -0700 (PDT) Subject: [committed] Reduce vector comparison of uniform vectors to a scalar comparison To: GCC Patches Message-ID: <7a698dd9-fe80-ed74-bed1-ab9b8141e3b6@gmail.com> Date: Fri, 27 Aug 2021 13:30:56 -0600 User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:78.0) Gecko/20100101 Thunderbird/78.13.0 MIME-Version: 1.0 Content-Language: en-US X-Spam-Status: No, score=-6.9 required=5.0 tests=BAYES_00, BODY_8BITS, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, RCVD_IN_DNSWL_NONE, SPF_HELO_NONE, SPF_PASS, TXREP autolearn=ham autolearn_force=no version=3.4.4 X-Spam-Checker-Version: SpamAssassin 3.4.4 (2020-01-24) on server2.sourceware.org X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-Patchwork-Original-From: Jeff Law via Gcc-patches From: Jeff Law Reply-To: Jeff Law Errors-To: gcc-patches-bounces+incoming=patchwork.ozlabs.org@gcc.gnu.org Sender: "Gcc-patches" I was working some aspects of our port's vector support and stumbled across a bit of silly code.  We were comparing two vectors that were both uniform. We can simplify a comparison of uniform vectors to a comparison of a representative element from each.  That in turn may expose const/copy propagation opportunities or even jump threading opportunities. And that's precisely what this patch does inside DOM.  At the start of statement processing we see if we can reduce a vector comparison to a scalar comparison. You can see this in pr95308.C on x86.   As we enter dom3 we have: ;;   basic block 2, loop depth 0 ;;    pred:       ENTRY   e.1_1 = e;   _27 = f_26(D) != 0;   _163 = e.1_1 != 0;   _164 = _27 & _163;   _196 = _164 ? -1 : 0;   vect_cst__194 = {_196, _196, _196, _196, _196, _196, _196, _196}; [ ... ] ;;   basic block 8, loop depth 1 ;;    pred:       7 ;;                6   if (vect_cst__194 == { 0, 0, 0, 0, 0, 0, 0, 0 })     goto ; [100.00%]   else     goto ; [20.00%] ;;    succ:       10 ;;                9 There's another similar comparison later.  We transform the comparison into: ;;   basic block 8, loop depth 1 ;;    pred:       7 ;;                6   if (_196 == 0)     goto ; [100.00%]   else     goto ; [20.00%] ;;    succ:       13 ;;                9 There's nice secondary effects that show up after the transformation as well. Bootstrapped and regression tested on x86_64.   It'll go through the rest of the public targets as well as our internal port over the next 24-48 hrs. Committed to the trunk, Jeff commit ac6d5c9112bb78e47398e040c553ad216edf3ebb Author: Jeff Law Date: Fri Aug 27 15:27:38 2021 -0400 Reduce vector comparison of uniform vectors to a scalar comparison gcc/ * tree-ssa-dom.c (reduce_vector_comparison_to_scalar_comparison): New function. (dom_opt_dom_walker::optimize_stmt): Use it. diff --git a/gcc/tree-ssa-dom.c b/gcc/tree-ssa-dom.c index a0df492df10..a5245b33de6 100644 --- a/gcc/tree-ssa-dom.c +++ b/gcc/tree-ssa-dom.c @@ -1891,6 +1891,66 @@ dom_opt_dom_walker::test_for_singularity (gimple *stmt, } } +/* If STMT is a comparison of two uniform vectors reduce it to a comparison + of scalar objects, otherwise leave STMT unchanged. */ + +static void +reduce_vector_comparison_to_scalar_comparison (gimple *stmt) +{ + if (gimple_code (stmt) == GIMPLE_COND) + { + tree lhs = gimple_cond_lhs (stmt); + tree rhs = gimple_cond_rhs (stmt); + + /* We may have a vector comparison where both arms are uniform + vectors. If so, we can simplify the vector comparison down + to a scalar comparison. */ + if (TREE_CODE (TREE_TYPE (lhs)) == VECTOR_TYPE + && TREE_CODE (TREE_TYPE (rhs)) == VECTOR_TYPE) + { + /* If either operand is an SSA_NAME, then look back to its + defining statement to try and get at a suitable source. */ + if (TREE_CODE (rhs) == SSA_NAME) + { + gimple *def_stmt = SSA_NAME_DEF_STMT (rhs); + if (gimple_assign_single_p (def_stmt)) + rhs = gimple_assign_rhs1 (def_stmt); + } + + if (TREE_CODE (lhs) == SSA_NAME) + { + gimple *def_stmt = SSA_NAME_DEF_STMT (lhs); + if (gimple_assign_single_p (def_stmt)) + lhs = gimple_assign_rhs1 (def_stmt); + } + + /* Now see if they are both uniform vectors and if so replace + the vector comparison with a scalar comparison. */ + tree rhs_elem = rhs ? uniform_vector_p (rhs) : NULL_TREE; + tree lhs_elem = lhs ? uniform_vector_p (lhs) : NULL_TREE; + if (rhs_elem && lhs_elem) + { + if (dump_file && dump_flags & TDF_DETAILS) + { + fprintf (dump_file, "Reducing vector comparison: "); + print_gimple_stmt (dump_file, stmt, 0); + } + + gimple_cond_set_rhs (as_a (stmt), rhs_elem); + gimple_cond_set_lhs (as_a (stmt), lhs_elem); + gimple_set_modified (stmt, true); + + if (dump_file && dump_flags & TDF_DETAILS) + { + fprintf (dump_file, "To scalar equivalent: "); + print_gimple_stmt (dump_file, stmt, 0); + fprintf (dump_file, "\n"); + } + } + } + } +} + /* Optimize the statement in block BB pointed to by iterator SI. We try to perform some simplistic global redundancy elimination and @@ -1933,6 +1993,11 @@ dom_opt_dom_walker::optimize_stmt (basic_block bb, gimple_stmt_iterator *si, update_stmt_if_modified (stmt); opt_stats.num_stmts++; + /* STMT may be a comparison of uniform vectors that we can simplify + down to a comparison of scalars. Do that transformation first + so that all the scalar optimizations from here onward apply. */ + reduce_vector_comparison_to_scalar_comparison (stmt); + /* Const/copy propagate into USES, VUSES and the RHS of VDEFs. */ cprop_into_stmt (stmt, m_evrp_range_analyzer);