From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from mail-pl1-x629.google.com (mail-pl1-x629.google.com [IPv6:2607:f8b0:4864:20::629]) by sourceware.org (Postfix) with ESMTPS id B7B5C3858D28 for ; Wed, 3 May 2023 06:36:35 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org B7B5C3858D28 Authentication-Results: sourceware.org; dmarc=pass (p=none dis=none) header.from=gmail.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=gmail.com Received: by mail-pl1-x629.google.com with SMTP id d9443c01a7336-1ab1ce53ca6so2472525ad.0 for ; Tue, 02 May 2023 23:36:35 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20221208; t=1683095795; x=1685687795; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :from:to:cc:subject:date:message-id:reply-to; bh=z3XjJMf6l0gVc6QFkIJilsjD5jUOGOtlyohIitNkmQ0=; b=m9uEF6/Zg09TsgVolmgHLkWsXnBnVQdIMwi9qvf0QjhMf4uewg++eDikBIELy8JM7b FK53jx/mdt30PVcQH2p4gmIxcmHqpS2BQ5ZPg/0CSIOQVRv9/AaDsMCoT/SfPSYyRevs A48AU06wLKbwzWByBYkvwHlaf1YlwWwB2qnTarPsrKxSbF1DthIPBY6asp1doQcnW3rp ELFiWGnNgri0wmvNsUj+6FBFuKjig9NVKTaZcE3ddAyIgKsf1h6deBE/bYuK/6PZmph1 9hMt2vnPyPddHG1u7EkHi/L5IELwBrFyxT7qSfyISrS0s2FY69WUYNXAKCrUAvLw3d02 YwLA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20221208; t=1683095795; x=1685687795; h=content-transfer-encoding:in-reply-to:from:references:cc:to :content-language:subject:user-agent:mime-version:date:message-id :x-gm-message-state:from:to:cc:subject:date:message-id:reply-to; bh=z3XjJMf6l0gVc6QFkIJilsjD5jUOGOtlyohIitNkmQ0=; b=d4+r7N/joj8kHpq3o1sd7ff3wqh2XRRiJTWAxeLd29KBrhyZPM5KCPIheAwi52tNTQ Z3LZ95mYJrL9ZAL2dOQpm73R0vt7k9iigAZdaCe2A2ztJddPJlNgPs4JJsbnkGvIZwr3 2ngD4Fj8jEgPoq/6Qx+lEcdbG51FurTWRS1SiJh0N/UOMbA9l1SKZxoJJ/A0hmJCi0b2 MvgrUFM4t8ktME6wtQBbpY0f3LSNHdaKwNJ0ZdkvaGFHlJpxKVhdeXSG6k6GnpH480oz AMi0pd6K69k2DZvTOnvgrpthtb6BufhiYvQCZ08ugcc5mulV7kGqvk3/ToQBUPHVvo6Q kFaA== X-Gm-Message-State: AC+VfDyNCTM3P3dcNy5ROlLKeAIcXcSB/KxgItJd2rS2EH2aVn1rRaYk yR7pkWCDEv5nTbsL/jIVfAA= X-Google-Smtp-Source: ACHHUZ6/s0uwdhKYIDmjtsskswcuP7x+poIbSNhUYBgYuFTfXVIo96znXTxdUjBwquEKcJs6HS94ow== X-Received: by 2002:a17:902:6946:b0:1a1:d366:b085 with SMTP id k6-20020a170902694600b001a1d366b085mr1097463plt.21.1683095794555; Tue, 02 May 2023 23:36:34 -0700 (PDT) Received: from ?IPV6:2601:681:8600:13d0::99f? ([2601:681:8600:13d0::99f]) by smtp.gmail.com with ESMTPSA id p5-20020a1709028a8500b001ab0d815dbbsm2371485plo.23.2023.05.02.23.36.33 (version=TLS1_3 cipher=TLS_AES_128_GCM_SHA256 bits=128/128); Tue, 02 May 2023 23:36:33 -0700 (PDT) Message-ID: <8fd4fbaf-b5b4-6d88-5e8b-7b65cf434417@gmail.com> Date: Wed, 3 May 2023 00:36:32 -0600 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Subject: Re: [PATCH] PHIOPT: Improve replace_phi_edge_with_variable for diamond shapped bb Content-Language: en-US To: Andrew Pinski , Richard Biener Cc: Andrew Pinski , gcc-patches@gcc.gnu.org References: <20230430211356.762030-1-apinski@marvell.com> From: Jeff Law In-Reply-To: Content-Type: text/plain; charset=UTF-8; format=flowed Content-Transfer-Encoding: 8bit X-Spam-Status: No, score=-10.8 required=5.0 tests=BAYES_00,DKIM_SIGNED,DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,FREEMAIL_FROM,GIT_PATCH_0,NICE_REPLY_A,RCVD_IN_DNSWL_NONE,SPF_HELO_NONE,SPF_PASS,TXREP,T_SCC_BODY_TEXT_LINE autolearn=ham autolearn_force=no version=3.4.6 X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server2.sourceware.org List-Id: On 5/3/23 00:27, Andrew Pinski via Gcc-patches wrote: > On Tue, May 2, 2023 at 11:14 PM Richard Biener > wrote: >> >> On Wed, May 3, 2023 at 12:04 AM Andrew Pinski wrote: >>> >>> On Tue, May 2, 2023 at 5:26 AM Richard Biener via Gcc-patches >>> wrote: >>>> >>>> On Sun, Apr 30, 2023 at 11:14 PM Andrew Pinski via Gcc-patches >>>> wrote: >>>>> >>>>> While looking at differences between what minmax_replacement >>>>> and match_simplify_replacement does. I noticed that they sometimes >>>>> chose different edges to remove. I decided we should be able to do >>>>> better and be able to remove both empty basic blocks in the >>>>> case of match_simplify_replacement as that moves the statements. >>>>> >>>>> This also updates the testcases as now match_simplify_replacement >>>>> will remove the unused MIN/MAX_EXPR and they were checking for >>>>> those. >>>>> >>>>> OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. >>>>> >>>>> gcc/ChangeLog: >>>>> >>>>> * tree-ssa-phiopt.cc (copy_phi_args): New function. >>>>> (replace_phi_edge_with_variable): Handle diamond form bb >>>>> with forwarder only empty blocks better. >>>>> >>>>> gcc/testsuite/ChangeLog: >>>>> >>>>> * gcc.dg/tree-ssa/minmax-15.c: Update test. >>>>> * gcc.dg/tree-ssa/minmax-16.c: Update test. >>>>> * gcc.dg/tree-ssa/minmax-3.c: Update test. >>>>> * gcc.dg/tree-ssa/minmax-4.c: Update test. >>>>> * gcc.dg/tree-ssa/minmax-5.c: Update test. >>>>> * gcc.dg/tree-ssa/minmax-8.c: Update test. >>>>> --- >>>>> gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c | 3 +- >>>>> gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c | 9 ++-- >>>>> gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c | 2 +- >>>>> gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c | 2 +- >>>>> gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c | 2 +- >>>>> gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c | 2 +- >>>>> gcc/tree-ssa-phiopt.cc | 51 ++++++++++++++++++++++- >>>>> 7 files changed, 59 insertions(+), 12 deletions(-) >>>>> >>>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c >>>>> index 8a39871c938..6731f91e6c3 100644 >>>>> --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c >>>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-15.c >>>>> @@ -30,5 +30,6 @@ main (void) >>>>> return 0; >>>>> } >>>>> >>>>> -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 3 "phiopt1" } } */ >>>>> +/* There should only be two MIN_EXPR left, the 3rd one was removed. */ >>>>> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ >>>>> /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */ >>>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c >>>>> index 623b12b3f74..094364e6424 100644 >>>>> --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c >>>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-16.c >>>>> @@ -25,11 +25,8 @@ main (void) >>>>> return 0; >>>>> } >>>>> >>>>> -/* After phiopt1, there really should be only 3 MIN_EXPR in the IR (including debug statements). >>>>> - But the way phiopt does not cleanup the CFG all the time, the PHI might still reference the >>>>> - alternative bb's moved statement. >>>>> - Note in the end, we do dce the statement and other debug statements to end up with only 2 MIN_EXPR. >>>>> - So check that too. */ >>>>> -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 4 "phiopt1" } } */ >>>>> +/* After phiopt1, will be only 2 MIN_EXPR in the IR (including debug statements). */ >>>>> +/* xk will only have the final result so the extra debug info does not change anything. */ >>>>> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ >>>>> /* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "optimized" } } */ >>>>> /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */ >>>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c >>>>> index 2af10776346..521afe3e4d9 100644 >>>>> --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c >>>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-3.c >>>>> @@ -25,5 +25,5 @@ main (void) >>>>> return 0; >>>>> } >>>>> >>>>> -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 3 "phiopt1" } } */ >>>>> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ >>>>> /* { dg-final { scan-tree-dump-times "MAX_EXPR" 0 "phiopt1" } } */ >>>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c >>>>> index 973f39bfed3..49e27185b5e 100644 >>>>> --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c >>>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-4.c >>>>> @@ -26,4 +26,4 @@ main (void) >>>>> } >>>>> >>>>> /* { dg-final { scan-tree-dump-times "MIN_EXPR" 0 "phiopt1" } } */ >>>>> -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 3 "phiopt1" } } */ >>>>> +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt1" } } */ >>>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c >>>>> index 34e4e720511..194c881cc98 100644 >>>>> --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c >>>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-5.c >>>>> @@ -25,5 +25,5 @@ main (void) >>>>> return 0; >>>>> } >>>>> >>>>> -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "phiopt1" } } */ >>>>> +/* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ >>>>> /* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ >>>>> diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c b/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c >>>>> index 0160e573fef..d5cb53145ea 100644 >>>>> --- a/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c >>>>> +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmax-8.c >>>>> @@ -26,4 +26,4 @@ main (void) >>>>> } >>>>> >>>>> /* { dg-final { scan-tree-dump-times "MIN_EXPR" 1 "phiopt1" } } */ >>>>> -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "phiopt1" } } */ >>>>> +/* { dg-final { scan-tree-dump-times "MAX_EXPR" 1 "phiopt1" } } */ >>>>> diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc >>>>> index 65b3deea34a..311423efeb5 100644 >>>>> --- a/gcc/tree-ssa-phiopt.cc >>>>> +++ b/gcc/tree-ssa-phiopt.cc >>>>> @@ -82,6 +82,25 @@ single_non_singleton_phi_for_edges (gimple_seq seq, edge e0, edge e1) >>>>> return phi; >>>>> } >>>>> >>>>> +/* For each PHI in BB, copy the argument associated with SRC_E to TGT_E. */ >>>>> + >>>>> +static void >>>>> +copy_phi_args (basic_block bb, edge src_e, edge tgt_e) >>>>> +{ >>>>> + gphi_iterator gsi; >>>>> + int src_indx = src_e->dest_idx; >>>>> + >>>>> + for (gsi = gsi_start_phis (bb); !gsi_end_p (gsi); gsi_next (&gsi)) >>>>> + { >>>>> + gphi *phi = gsi.phi (); >>>>> + tree def = gimple_phi_arg_def (phi, src_indx); >>>>> + location_t locus = gimple_phi_arg_location (phi, src_indx); >>>>> + >>>>> + add_phi_arg (phi, def, tgt_e, locus); >>>>> + } >>>>> +} >>>> >>>> Doesn't flush_pending_stmts (tgt_e) do this? >>> >>> No, In fact the above code is very similar to the code from >>> remove_forwarder_block in tree-cfgcleanup.cc (I copied it and changed >>> it from copy_phi_args in tree-ssa-threadupdate.cc though as I don't >>> need a mapping). >>> Let me factor out the code from remove_forwarder_block and put it in >>> some common spot and then use that; it will be the same logic even. >> >> Hmm, but it's odd - if you redirect an edge on GIMPLE then there should >> be helpers available to do all this. I think you're doing something wrong >> (without actually looking too close) > > Maybe some (crude) diagrams are needed to explain why we need to copy > the entries for the phi nodes from one edge to another. > > So the original BB structure is: > > BB > /e1 \e2 > BB1 BB2 > \e3 /e4 > BB3 > BB3 has a few phi nodes (except for one of the phi nodes, the entries > for BB1, BB2 are all the same). > When you redirect e1 (or e2) to BB3, we create new entries in the phi > nodes for that edge now as it was not there before. > So the shape is: > BB > |e1 (or e2) > BB3 > > but since it is a new entry in the PHI node, it will be a nullptr. So > we need to copy them from the e3 or e4 entries. > Does that make sense on why the new function is needed here? This is > not a normal operation done by any other pass either. Jump threading does some of this kind of stuff. Does copy_phi_arg_into_existing_phi look like something you could use? jeff