From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-ed1-x533.google.com (mail-ed1-x533.google.com [IPv6:2a00:1450:4864:20::533]) by sourceware.org (Postfix) with ESMTPS id 7EBAC3858D39 for ; Mon, 15 Nov 2021 14:11:32 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org 7EBAC3858D39 Received: by mail-ed1-x533.google.com with SMTP id x15so72579162edv.1 for ; Mon, 15 Nov 2021 06:11:32 -0800 (PST) X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; h=x-gm-message-state:mime-version:references:in-reply-to:from:date :message-id:subject:to:content-transfer-encoding; bh=nAWLqgU0ekoijCe8xBPoTCQFpzcJHKIU16qXF/2nuHw=; b=HohBYpVAmkQaUxtpMbnRBxbhVg4cIrSxbCUlXmVh0J5lXyfiaHls7f7fAwDWCLT8uI mH2Eo8xhzehIyE+PFiWpDxoTkgbtRLkiL0qV8Mor47jU4+XITJWZrcSRG1SzG3Sf/0wm Ff+5ZTMPAhB/KyToPK3bdOoUJtyxKTTCwDfe6Kcg0sCneYTt33wjJgm7sK78rmPLhp+W SlVXc5sRdjwqVPfk73vcaXAgeUmukbiheWc1/HCl15Dnm68/9I/QyEScqpltOfZcrrCW RMN+tS9tNbZqigovXubEDxIjmmxzU7FyfJP5kIvdPR0lTABXSefAq+n3LpCHDTPD59cG k2gA== X-Gm-Message-State: AOAM5315Wn9m9T3IZWgQ4KhHzBhGIi1Tg8GMDO6hxSVcwWUsUH3soLkY qO+AbS9Ur8r6YS6KlFG6xl8bH7ieHr3OgrdI4OE+K9Kc X-Google-Smtp-Source: ABdhPJydiK2DEkvT9LpYoOTPjsSLWocTMSkw+Y7Xsphf4bG8YHZyoYZuO9LDfxm7i+xuGsV3WWEUGH+icEPJxloGa7o= X-Received: by 2002:a05:6402:84f:: with SMTP id b15mr54419238edz.307.1636985491196; Mon, 15 Nov 2021 06:11:31 -0800 (PST) MIME-Version: 1.0 References: In-Reply-To: From: Richard Biener Date: Mon, 15 Nov 2021 15:11:20 +0100 Message-ID: Subject: Re: [PATCH 4/5] if-conv: Apply VN to hoisted conversions To: Richard Biener via Gcc-patches , Richard Biener , Richard Sandiford Content-Type: text/plain; charset="UTF-8" Content-Transfer-Encoding: quoted-printable X-Spam-Status: No, score=-8.4 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, FREEMAIL_FROM, GIT_PATCH_0, KAM_SHORT, 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-List-Received-Date: Mon, 15 Nov 2021 14:11:35 -0000 On Mon, Nov 15, 2021 at 3:00 PM Richard Sandiford wrote: > > Richard Biener via Gcc-patches writes: > > On Fri, Nov 12, 2021 at 7:05 PM Richard Sandiford via Gcc-patches > > wrote: > >> > >> This patch is a prerequisite for a later one. At the moment, > >> if-conversion converts predicated POINTER_PLUS_EXPRs into > >> non-wrapping forms, which for: > >> > >> =E2=80=A6 =3D base + offset > >> > >> becomes: > >> > >> tmp =3D (unsigned long) base > >> =E2=80=A6 =3D tmp + offset > >> > >> It then hoists these conversions out of the loop where possible. > >> > >> However, because =E2=80=9Cbase=E2=80=9D is a valid gimple operand, the= re can be > >> multiple POINTER_PLUS_EXPRs with the same base, which can in turn > >> lead to multiple instances of the same conversion. The later VN pass > >> is (and I think needs to be) restricted to the new if-converted code, > >> whereas here we're deliberately inserting the conversions before the > >> .LOOP_VECTORIZED condition: > >> > >> /* If we versioned loop then make sure to insert invariant > >> stmts before the .LOOP_VECTORIZED check since the vectorize= r > >> will re-use that for things like runtime alias versioning > >> whose condition can end up using those invariants. */ > >> > >> We can therefore enter the vectoriser with redundant conversions. > >> > >> The easiest fix seemed to be to defer the hoisting until after VN. > >> This catches other hoisting opportunities too. > >> > >> Hoisting the code from the (artificial) loop in pr99102.c means > >> that it's no longer worth vectorising. The patch forces vectorisation > >> instead of relying on the cost model. > >> > >> The patch also reverts pr87007-4.c and pr87007-5.c back to their > >> original forms, undoing changes in 783dc66f9ccb0019c3dad. > >> The code at the time the tests were added was: > >> > >> testl %edi, %edi > >> je .L10 > >> vxorps %xmm1, %xmm1, %xmm1 > >> vsqrtsd d3(%rip), %xmm1, %xmm0 > >> vsqrtsd d2(%rip), %xmm1, %xmm1 > >> ... > >> .L10: > >> ret > >> > >> with the operations being hoisted, and the vxorps was specifically > >> wanted (compared to the previous code). This patch restores the code > >> to that form, with the hoisted operations and the vxorps. > >> > >> Regstrapped on aarch64-linux-gnu and x86_64-linux-gnu. OK to install? > >> > >> Richard > >> > >> > >> gcc/ > >> * tree-if-conv.c: Include tree-eh.h. > >> (predicate_statements): Remove pe argument. Don't hoist > >> statements here. > >> (combine_blocks): Remove pe argument. > >> (ifcvt_can_hoist, ifcvt_can_hoist_further): New functions. > >> (ifcvt_hoist_invariants): Likewise. > >> (tree_if_conversion): Update call to combine_blocks. Call > >> ifcvt_hoist_invariants after VN. > >> > >> gcc/testsuite/ > >> * gcc.dg/vect/pr99102.c: Add -fno-vect-cost-model. > >> > >> Revert: > >> > >> 2020-09-09 Richard Biener > >> > >> * gcc.target/i386/pr87007-4.c: Adjust. > >> * gcc.target/i386/pr87007-5.c: Likewise. > >> --- > >> gcc/testsuite/gcc.dg/vect/pr99102.c | 2 +- > >> gcc/testsuite/gcc.target/i386/pr87007-4.c | 2 +- > >> gcc/testsuite/gcc.target/i386/pr87007-5.c | 2 +- > >> gcc/tree-if-conv.c | 122 ++++++++++++++++++++-= - > >> 4 files changed, 114 insertions(+), 14 deletions(-) > >> > >> diff --git a/gcc/testsuite/gcc.dg/vect/pr99102.c b/gcc/testsuite/gcc.d= g/vect/pr99102.c > >> index 6c1a13f0783..0d030d15c86 100644 > >> --- a/gcc/testsuite/gcc.dg/vect/pr99102.c > >> +++ b/gcc/testsuite/gcc.dg/vect/pr99102.c > >> @@ -1,4 +1,4 @@ > >> -/* { dg-options "-O2 -ftree-vectorize -fdump-tree-vect-details" } */ > >> +/* { dg-options "-O2 -ftree-vectorize -fno-vect-cost-model -fdump-tre= e-vect-details" } */ > >> /* { dg-additional-options "-msve-vector-bits=3D256" { target aarch64= _sve256_hw } } */ > >> long a[44]; > >> short d, e =3D -7; > >> diff --git a/gcc/testsuite/gcc.target/i386/pr87007-4.c b/gcc/testsuite= /gcc.target/i386/pr87007-4.c > >> index 9c4b8005af3..e91bdcbac44 100644 > >> --- a/gcc/testsuite/gcc.target/i386/pr87007-4.c > >> +++ b/gcc/testsuite/gcc.target/i386/pr87007-4.c > >> @@ -15,4 +15,4 @@ foo (int n, int k) > >> d1 =3D ceil (d3); > >> } > >> > >> -/* { dg-final { scan-assembler-times "vxorps\[^\n\r\]*xmm\[0-9\]" 0 }= } */ > >> +/* { dg-final { scan-assembler-times "vxorps\[^\n\r\]*xmm\[0-9\]" 1 }= } */ > >> diff --git a/gcc/testsuite/gcc.target/i386/pr87007-5.c b/gcc/testsuite= /gcc.target/i386/pr87007-5.c > >> index e4d956a5d7f..20d13cf650b 100644 > >> --- a/gcc/testsuite/gcc.target/i386/pr87007-5.c > >> +++ b/gcc/testsuite/gcc.target/i386/pr87007-5.c > >> @@ -15,4 +15,4 @@ foo (int n, int k) > >> d1 =3D sqrt (d3); > >> } > >> > >> -/* { dg-final { scan-assembler-times "vxorps\[^\n\r\]*xmm\[0-9\]" 0 }= } */ > >> +/* { dg-final { scan-assembler-times "vxorps\[^\n\r\]*xmm\[0-9\]" 1 }= } */ > >> diff --git a/gcc/tree-if-conv.c b/gcc/tree-if-conv.c > >> index e88ddc9f788..0ad557a2f4d 100644 > >> --- a/gcc/tree-if-conv.c > >> +++ b/gcc/tree-if-conv.c > >> @@ -121,6 +121,7 @@ along with GCC; see the file COPYING3. If not see > >> #include "tree-cfgcleanup.h" > >> #include "tree-ssa-dse.h" > >> #include "tree-vectorizer.h" > >> +#include "tree-eh.h" > >> > >> /* Only handle PHIs with no more arguments unless we are asked to by > >> simd pragma. */ > >> @@ -2496,7 +2497,7 @@ predicate_rhs_code (gassign *stmt, tree mask, tr= ee cond, > >> */ > >> > >> static void > >> -predicate_statements (loop_p loop, edge pe) > >> +predicate_statements (loop_p loop) > >> { > >> unsigned int i, orig_loop_num_nodes =3D loop->num_nodes; > >> auto_vec vect_sizes; > >> @@ -2597,13 +2598,7 @@ predicate_statements (loop_p loop, edge pe) > >> { > >> gassign *stmt2 =3D as_a (gsi_stmt (gsi2)= ); > >> gsi_remove (&gsi2, false); > >> - /* Make sure to move invariant conversions out of th= e > >> - loop. */ > >> - if (CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (stm= t2)) > >> - && expr_invariant_in_loop_p (loop, > >> - gimple_assign_rhs1 = (stmt2))) > >> - gsi_insert_on_edge_immediate (pe, stmt2); > >> - else if (first) > >> + if (first) > >> { > >> gsi_insert_before (&gsi, stmt2, GSI_NEW_STMT); > >> first =3D false; > >> @@ -2684,7 +2679,7 @@ remove_conditions_and_labels (loop_p loop) > >> blocks. Replace PHI nodes with conditional modify expressions. *= / > >> > >> static void > >> -combine_blocks (class loop *loop, edge pe) > >> +combine_blocks (class loop *loop) > >> { > >> basic_block bb, exit_bb, merge_target_bb; > >> unsigned int orig_loop_num_nodes =3D loop->num_nodes; > >> @@ -2697,7 +2692,7 @@ combine_blocks (class loop *loop, edge pe) > >> predicate_all_scalar_phis (loop); > >> > >> if (need_to_predicate || need_to_rewrite_undefined) > >> - predicate_statements (loop, pe); > >> + predicate_statements (loop); > >> > >> /* Merge basic blocks. */ > >> exit_bb =3D NULL; > >> @@ -3181,6 +3176,109 @@ ifcvt_local_dce (class loop *loop) > >> } > >> } > >> > >> +/* Return true if STMT can be hoisted from if-converted loop LOOP. *= / > >> + > >> +static bool > >> +ifcvt_can_hoist (class loop *loop, gimple *stmt) > >> +{ > >> + if (auto *call =3D dyn_cast (stmt)) > >> + { > >> + if (gimple_call_internal_p (call) > >> + && internal_fn_mask_index (gimple_call_internal_fn (call)) >= =3D 0) > >> + return false; > >> + } > >> + else if (auto *assign =3D dyn_cast (stmt)) > >> + { > >> + if (gimple_assign_rhs_code (assign) =3D=3D COND_EXPR) > >> + return false; > >> + } > >> + else > >> + return false; > >> + > >> + if (gimple_has_side_effects (stmt) > >> + || gimple_could_trap_p (stmt) > >> + || stmt_could_throw_p (cfun, stmt) > >> + || gimple_vdef (stmt) > >> + || gimple_vuse (stmt)) > >> + return false; > >> + > >> + int num_args =3D gimple_num_args (stmt); > >> + for (int i =3D 0; i < num_args; ++i) > >> + if (!expr_invariant_in_loop_p (loop, gimple_arg (stmt, i))) > >> + return false; > >> + > >> + return true; > >> +} > >> + > >> +/* PE is the preferred hoisting edge selected by tree_if_conversion, = which > >> + s known to be different from (and to dominate) the preheader edge = of the > >> + if-converted loop. We already know that STMT can be inserted on t= he loop > >> + preheader edge. Return true if we prefer to insert it on PE inste= ad. */ > >> + > >> +static bool > >> +ifcvt_can_hoist_further (edge pe, gimple *stmt) > >> +{ > >> + /* As explained in tree_if_conversion, we want to hoist invariant > >> + conversions further so that they can be reused by alias analysis= . */ > >> + auto *assign =3D dyn_cast (stmt); > >> + if (assign > >> + && CONVERT_EXPR_CODE_P (gimple_assign_rhs_code (assign))) > >> + { > >> + tree rhs =3D gimple_assign_rhs1 (assign); > >> + if (is_gimple_min_invariant (rhs)) > >> + return true; > >> + > >> + if (TREE_CODE (rhs) =3D=3D SSA_NAME) > >> + { > >> + basic_block def_bb =3D gimple_bb (SSA_NAME_DEF_STMT (rhs)); > >> + if (!def_bb || dominated_by_p (CDI_DOMINATORS, pe->dest, def= _bb)) > >> + return true; > >> + } > >> + } > >> + return false; > >> +} > >> + > >> +/* Hoist invariant statements from LOOP. PE is the preferred edge fo= r > >> + hoisting conversions, as selected by tree_if_conversion; see there > >> + for details. */ > >> + > >> +static void > >> +ifcvt_hoist_invariants (class loop *loop, edge pe) > >> +{ > >> + gimple_stmt_iterator hoist_gsi =3D {}; > >> + gimple_stmt_iterator hoist_gsi_pe =3D {}; > >> + unsigned int num_blocks =3D loop->num_nodes; > >> + basic_block *body =3D get_loop_body (loop); > >> + for (unsigned int i =3D 0; i < num_blocks; ++i) > >> + for (gimple_stmt_iterator gsi =3D gsi_start_bb (body[i]); !gsi_en= d_p (gsi);) > >> + { > >> + gimple *stmt =3D gsi_stmt (gsi); > >> + if (ifcvt_can_hoist (loop, stmt)) > >> + { > >> + /* Once we've hoisted one statement, insert other statemen= ts > >> + after it. */ > >> + edge e =3D loop_preheader_edge (loop); > >> + gimple_stmt_iterator *hoist_gsi_ptr =3D &hoist_gsi; > >> + if (e !=3D pe && ifcvt_can_hoist_further (pe, stmt)) > > > > One issue with hoisting to loop_preheader_edge instead of 'pe' > > is that eventually the loop versioning code will pick up the defs > > through the versioning condition and that will break because the > > versioning condition will be inserted in place of the > > IFN_VECTORIZED_LOOP, which is _before_ the preheader. > > The code basically expects the preheader to be empty. > > Do you mean that the versioning-for-aliases code assumes that all > loop-invariant definitions are available in the versioning condition? Yes. Or well, it assumes it can use the .LOOP_VECTORIZED condition as versioning condition and there simply use insert the condition without further checks. And the checks are generated utlimatively from SCEV which just expands things up to the loops preheader. > I hadn't realised that's what the comment was saying. It sounded more > like an optimisation. The hoisting itself is, but that we don't just hoist to the loops preheader is for correctness, not an optimization ... it's indeed a bit messy though. Richard. > > Thanks, > Richard > > > I don't see how ifcvt_can_hoist_further avoids this for > > non-CONVERT_EXPRs. > > > > So I don't see how this is actually safe? It's probably moderately > > easy to trigger issues when you disable both PRE and > > loop invariant motion before if-conversion/vectorization. > > > > Maybe doing something similar as cse_and_gimplify_to_preheader > > done in the vectorizer helps for the original problem. Or alternativel= y > > do the hoisting during loop versioning ... > > > > Richard. > > > >> + { > >> + e =3D pe; > >> + hoist_gsi_ptr =3D &hoist_gsi_pe; > >> + } > >> + gsi_remove (&gsi, false); > >> + if (hoist_gsi_ptr->ptr) > >> + gsi_insert_after (hoist_gsi_ptr, stmt, GSI_NEW_STMT); > >> + else > >> + { > >> + gsi_insert_on_edge_immediate (e, stmt); > >> + *hoist_gsi_ptr =3D gsi_for_stmt (stmt); > >> + } > >> + continue; > >> + } > >> + gsi_next (&gsi); > >> + } > >> + free (body); > >> +} > >> + > >> /* If-convert LOOP when it is legal. For the moment this pass has no > >> profitability analysis. Returns non-zero todo flags when somethin= g > >> changed. */ > >> @@ -3275,7 +3373,7 @@ tree_if_conversion (class loop *loop, vec *preds) > >> /* Now all statements are if-convertible. Combine all the basic > >> blocks into one huge basic block doing the if-conversion > >> on-the-fly. */ > >> - combine_blocks (loop, pe); > >> + combine_blocks (loop); > >> > >> /* Perform local CSE, this esp. helps the vectorizer analysis if lo= ads > >> and stores are involved. CSE only the loop body, not the entry > >> @@ -3297,6 +3395,8 @@ tree_if_conversion (class loop *loop, vec *preds) > >> ifcvt_local_dce (loop); > >> BITMAP_FREE (exit_bbs); > >> > >> + ifcvt_hoist_invariants (loop, pe); > >> + > >> todo |=3D TODO_cleanup_cfg; > >> > >> cleanup: > >> -- > >> 2.25.1 > >>