Message ID | 1d4c9e6c-85e4-7eff-0833-aca7f874fbda@linux.ibm.com |
---|---|
State | New |
Headers |
Return-Path: <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> X-Original-To: patchwork@sourceware.org Delivered-To: patchwork@sourceware.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5488A3858D39 for <patchwork@sourceware.org>; Fri, 14 Apr 2023 08:42:15 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5488A3858D39 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1681461735; bh=44vZt/fgJqrXz20NyCgJmlWwNejZlCKFIavtB9VI8g0=; h=Date:To:Cc:Subject:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=yevhQuYq8ScshrNO+zhD36SiHzVjexycESg1tUmpaVaozhttdRAJM0gawphr6yEOR wBxGqkt/z9F21egYzyans+5KhBD2NDnTdunWnQcPiIomvmS3IbnsoI7H8O65Lzlkzc 0dsIUnsM9Dh7W33VV6yxG0ID3KkhhQ8fCXqh3o+0= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0a-001b2d01.pphosted.com (mx0a-001b2d01.pphosted.com [148.163.156.1]) by sourceware.org (Postfix) with ESMTPS id 34E663858D20 for <gcc-patches@gcc.gnu.org>; Fri, 14 Apr 2023 08:41:45 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 34E663858D20 Received: from pps.filterd (m0098409.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 33E8Vmgx000907; Fri, 14 Apr 2023 08:41:44 GMT Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3pxvwx4n66-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Apr 2023 08:41:43 +0000 Received: from m0098409.ppops.net (m0098409.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 33E8XRGK008533; Fri, 14 Apr 2023 08:41:43 GMT Received: from ppma04dal.us.ibm.com (7a.29.35a9.ip4.static.sl-reverse.com [169.53.41.122]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3pxvwx4n5u-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Apr 2023 08:41:43 +0000 Received: from pps.filterd (ppma04dal.us.ibm.com [127.0.0.1]) by ppma04dal.us.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 33E6Xbwp009468; Fri, 14 Apr 2023 08:41:42 GMT Received: from smtprelay05.dal12v.mail.ibm.com ([9.208.130.101]) by ppma04dal.us.ibm.com (PPS) with ESMTPS id 3pu0h07qd0-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 14 Apr 2023 08:41:42 +0000 Received: from smtpav01.dal12v.mail.ibm.com (smtpav01.dal12v.mail.ibm.com [10.241.53.100]) by smtprelay05.dal12v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 33E8fflI7733818 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 14 Apr 2023 08:41:41 GMT Received: from smtpav01.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 3DDE858058; Fri, 14 Apr 2023 08:41:41 +0000 (GMT) Received: from smtpav01.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 68B2258061; Fri, 14 Apr 2023 08:41:39 +0000 (GMT) Received: from [9.43.65.25] (unknown [9.43.65.25]) by smtpav01.dal12v.mail.ibm.com (Postfix) with ESMTP; Fri, 14 Apr 2023 08:41:39 +0000 (GMT) Message-ID: <1d4c9e6c-85e4-7eff-0833-aca7f874fbda@linux.ibm.com> Date: Fri, 14 Apr 2023 14:11:37 +0530 MIME-Version: 1.0 User-Agent: Mozilla/5.0 (Macintosh; Intel Mac OS X 10.15; rv:102.0) Gecko/20100101 Thunderbird/102.9.1 Content-Language: en-US To: gcc-patches <gcc-patches@gcc.gnu.org> Cc: Segher Boessenkool <segher@kernel.crashing.org>, Peter Bergner <bergner@linux.ibm.com>, Jeff Law <jeffreyalaw@gmail.com> Subject: PATCH] tree-ssa-sink: Add heuristics for code sinking Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 7bit X-TM-AS-GCONF: 00 X-Proofpoint-GUID: RRpanAlzSKNVaGG7Ed4aNeJL6FU1btVC X-Proofpoint-ORIG-GUID: ao3Kicy8ap8aHzotY4DWg8e9QAQZKOTg X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.942,Hydra:6.0.573,FMLib:17.11.170.22 definitions=2023-04-14_02,2023-04-13_01,2023-02-09_01 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 malwarescore=0 phishscore=0 priorityscore=1501 adultscore=0 impostorscore=0 lowpriorityscore=0 mlxscore=0 clxscore=1011 suspectscore=0 bulkscore=0 spamscore=0 mlxlogscore=999 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2303200000 definitions=main-2304140068 X-Spam-Status: No, score=-11.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, 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 X-BeenThere: gcc-patches@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-patches mailing list <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> From: Ajit Agarwal via Gcc-patches <gcc-patches@gcc.gnu.org> Reply-To: Ajit Agarwal <aagarwa1@linux.ibm.com> Errors-To: gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+patchwork=sourceware.org@gcc.gnu.org> |
Series |
PATCH] tree-ssa-sink: Add heuristics for code sinking
|
|
Commit Message
Ajit Agarwal
April 14, 2023, 8:41 a.m. UTC
Hello All: This patch add heuristics for code sinking opportunities. Bootstrapped and regtested for powerpc64-linux-gnu. Thanks & Regards Ajit tree-ssa-sink: Add heuristics for code sinking. Add following code sinking heuristics: 1. from code block dominates the call. 2. To Code block have uses inside the function call. 3. Loop headers. 4. Sinking from code block after call increases register pressure. 5. Sinking calls. 2023-04-14 Ajit Kumar Agarwal <aagarwa1@linux.ibm.com> gcc/ChangeLog: * tree-ssa-sink.cc (statement_sink_location): Add heuristics for code sinking. --- gcc/tree-ssa-sink.cc | 33 +++++++++++++++++++++++++++++++++ 1 file changed, 33 insertions(+)
Comments
On Fri, Apr 14, 2023 at 10:42 AM Ajit Agarwal via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > Hello All: > > This patch add heuristics for code sinking opportunities. > Bootstrapped and regtested for powerpc64-linux-gnu. > > Thanks & Regards > Ajit > > tree-ssa-sink: Add heuristics for code sinking. > > Add following code sinking heuristics: > > 1. from code block dominates the call. > 2. To Code block have uses inside the function call. > 3. Loop headers. > 4. Sinking from code block after call increases register > pressure. > 5. Sinking calls. > > 2023-04-14 Ajit Kumar Agarwal <aagarwa1@linux.ibm.com> > > gcc/ChangeLog: > > * tree-ssa-sink.cc (statement_sink_location): Add heuristics > for code sinking. > --- > gcc/tree-ssa-sink.cc | 33 +++++++++++++++++++++++++++++++++ > 1 file changed, 33 insertions(+) > > diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc > index 87b1d40c174..8de88b259a3 100644 > --- a/gcc/tree-ssa-sink.cc > +++ b/gcc/tree-ssa-sink.cc > @@ -465,6 +465,39 @@ statement_sink_location (gimple *stmt, basic_block frombb, > if (sinkbb == frombb) > return false; > > + auto_vec<basic_block> h; > + h = get_all_dominated_blocks (CDI_DOMINATORS, > + frombb); > + bool is_call = false; > + while (h.length ()) > + { > + basic_block bb = h.pop (); > + > + if (bb == frombb) > + continue; > + > + for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) > + { > + gimple *stmt = gsi_stmt (gsi); > + > + if (is_gimple_call (stmt)) > + { > + is_call = true; > + break; > + } > + > + if (!gsi_end_p (gsi)) > + gsi_prev (&gsi); > + } > + } > + > + if (!is_gimple_call (stmt) > + && (gimple_bb (use) != frombb) > + && !is_gimple_call (use) > + && dominated_by_p (CDI_DOMINATORS, sinkbb, frombb) > + && is_call) > + return false; > + Sorry, but this lacks a comment, it doesn't explain why the existing heuristics are not enough (select_best_block), it repeats dominance computing. More so it lacks a testcase demonstrating the effect. Richard. > if (sinkbb == gimple_bb (use)) > *togsi = gsi_for_stmt (use); > else > -- > 2.31.1 >
Hello Richard: On 14/04/23 2:29 pm, Richard Biener wrote: > On Fri, Apr 14, 2023 at 10:42 AM Ajit Agarwal via Gcc-patches > <gcc-patches@gcc.gnu.org> wrote: >> >> Hello All: >> >> This patch add heuristics for code sinking opportunities. >> Bootstrapped and regtested for powerpc64-linux-gnu. >> >> Thanks & Regards >> Ajit >> >> tree-ssa-sink: Add heuristics for code sinking. >> >> Add following code sinking heuristics: >> >> 1. from code block dominates the call. >> 2. To Code block have uses inside the function call. >> 3. Loop headers. >> 4. Sinking from code block after call increases register >> pressure. >> 5. Sinking calls. >> >> 2023-04-14 Ajit Kumar Agarwal <aagarwa1@linux.ibm.com> >> >> gcc/ChangeLog: >> >> * tree-ssa-sink.cc (statement_sink_location): Add heuristics >> for code sinking. >> --- >> gcc/tree-ssa-sink.cc | 33 +++++++++++++++++++++++++++++++++ >> 1 file changed, 33 insertions(+) >> >> diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc >> index 87b1d40c174..8de88b259a3 100644 >> --- a/gcc/tree-ssa-sink.cc >> +++ b/gcc/tree-ssa-sink.cc >> @@ -465,6 +465,39 @@ statement_sink_location (gimple *stmt, basic_block frombb, >> if (sinkbb == frombb) >> return false; >> >> + auto_vec<basic_block> h; >> + h = get_all_dominated_blocks (CDI_DOMINATORS, >> + frombb); >> + bool is_call = false; >> + while (h.length ()) >> + { >> + basic_block bb = h.pop (); >> + >> + if (bb == frombb) >> + continue; >> + >> + for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) >> + { >> + gimple *stmt = gsi_stmt (gsi); >> + >> + if (is_gimple_call (stmt)) >> + { >> + is_call = true; >> + break; >> + } >> + >> + if (!gsi_end_p (gsi)) >> + gsi_prev (&gsi); >> + } >> + } >> + >> + if (!is_gimple_call (stmt) >> + && (gimple_bb (use) != frombb) >> + && !is_gimple_call (use) >> + && dominated_by_p (CDI_DOMINATORS, sinkbb, frombb) >> + && is_call) >> + return false; >> + > > Sorry, but this lacks a comment, it doesn't explain why the existing heuristics > are not enough (select_best_block), it repeats dominance computing. > > More so it lacks a testcase demonstrating the effect. > Added testscases and comments in the code. The heuristics are added to relieve from register pressure. Thanks & Regards Ajit Here is the patch. tree-ssa-sink: Add heuristics for code sinking. Add following code sinking heuristics: 1. from code block dominates the call. 2. To Code block have uses inside the function call. 3. Loop headers. 4. Sinking from code block after call increases register pressure. 5. Sinking calls. 2023-04-14 Ajit Kumar Agarwal <aagarwa1@linux.ibm.com> gcc/ChangeLog: * tree-ssa-sink.cc (statement_sink_location): Add heuristics for code sinking. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/ssa-sink-20.c: New testcase. * gcc.dg/tree-ssa/ssa-sink-21.c: New testcase. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c | 16 ++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c | 20 ++++++++++++++++++++ gcc/tree-ssa-sink.cc | 6 ++++++ 3 files changed, 42 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c new file mode 100644 index 00000000000..ed2aefc01aa --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c @@ -0,0 +1,16 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-sink -fdump-tree-optimized" } */ + +void bar(); +int j; +void foo(int a, int b, int c, int d, int e, int f) +{ + int l; + l = a + b + c + d +e + f; + if (a != 5) + { + bar(); + j = l; + } +} +/* { dg-final { scan-tree-dump-times "Sunk statements: 1" 0 "sink1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c new file mode 100644 index 00000000000..a39724df8ec --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c @@ -0,0 +1,20 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-sink-stats" } */ + +void bar(); +int j, x; +void foo(int a, int b, int c, int d, int e, int f) +{ + int l; + l = a + b + c + d +e + f; + if (a != 5) + { + bar(); + if (b != 3) + x = 3; + else + x = 5; + j = l; + } +} +/* { dg-final { scan-tree-dump-times "Sunk statements: 1" 0 "sink1" } } */ diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc index 8de88b259a3..932fd71bec2 100644 --- a/gcc/tree-ssa-sink.cc +++ b/gcc/tree-ssa-sink.cc @@ -465,6 +465,12 @@ statement_sink_location (gimple *stmt, basic_block frombb, if (sinkbb == frombb) return false; + /* The below heuristics describes the following. + a) If the candidate to sink has call in the dominator basic + basic blocks. + b) statement to sink doesn't have use in the call. + c) candidate block dominates sink block. + In the above cases are true then don't do code sinking. */ auto_vec<basic_block> h; h = get_all_dominated_blocks (CDI_DOMINATORS, frombb);
On Fri, Apr 14, 2023 at 12:08 PM Ajit Agarwal <aagarwa1@linux.ibm.com> wrote: > > Hello Richard: > > On 14/04/23 2:29 pm, Richard Biener wrote: > > On Fri, Apr 14, 2023 at 10:42 AM Ajit Agarwal via Gcc-patches > > <gcc-patches@gcc.gnu.org> wrote: > >> > >> Hello All: > >> > >> This patch add heuristics for code sinking opportunities. > >> Bootstrapped and regtested for powerpc64-linux-gnu. > >> > >> Thanks & Regards > >> Ajit > >> > >> tree-ssa-sink: Add heuristics for code sinking. > >> > >> Add following code sinking heuristics: > >> > >> 1. from code block dominates the call. > >> 2. To Code block have uses inside the function call. > >> 3. Loop headers. > >> 4. Sinking from code block after call increases register > >> pressure. > >> 5. Sinking calls. > >> > >> 2023-04-14 Ajit Kumar Agarwal <aagarwa1@linux.ibm.com> > >> > >> gcc/ChangeLog: > >> > >> * tree-ssa-sink.cc (statement_sink_location): Add heuristics > >> for code sinking. > >> --- > >> gcc/tree-ssa-sink.cc | 33 +++++++++++++++++++++++++++++++++ > >> 1 file changed, 33 insertions(+) > >> > >> diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc > >> index 87b1d40c174..8de88b259a3 100644 > >> --- a/gcc/tree-ssa-sink.cc > >> +++ b/gcc/tree-ssa-sink.cc > >> @@ -465,6 +465,39 @@ statement_sink_location (gimple *stmt, basic_block frombb, > >> if (sinkbb == frombb) > >> return false; > >> > >> + auto_vec<basic_block> h; > >> + h = get_all_dominated_blocks (CDI_DOMINATORS, > >> + frombb); > >> + bool is_call = false; > >> + while (h.length ()) > >> + { > >> + basic_block bb = h.pop (); > >> + > >> + if (bb == frombb) > >> + continue; > >> + > >> + for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) > >> + { > >> + gimple *stmt = gsi_stmt (gsi); > >> + > >> + if (is_gimple_call (stmt)) > >> + { > >> + is_call = true; > >> + break; > >> + } > >> + > >> + if (!gsi_end_p (gsi)) > >> + gsi_prev (&gsi); > >> + } > >> + } > >> + > >> + if (!is_gimple_call (stmt) > >> + && (gimple_bb (use) != frombb) > >> + && !is_gimple_call (use) > >> + && dominated_by_p (CDI_DOMINATORS, sinkbb, frombb) > >> + && is_call) > >> + return false; > >> + > > > > Sorry, but this lacks a comment, it doesn't explain why the existing heuristics > > are not enough (select_best_block), it repeats dominance computing. > > > > More so it lacks a testcase demonstrating the effect. > > > > Added testscases and comments in the code. > The heuristics are added to relieve from register pressure. > > Thanks & Regards > Ajit > > Here is the patch. > > tree-ssa-sink: Add heuristics for code sinking. > > Add following code sinking heuristics: > > 1. from code block dominates the call. > 2. To Code block have uses inside the function call. > 3. Loop headers. > 4. Sinking from code block after call increases register > pressure. > 5. Sinking calls. > > 2023-04-14 Ajit Kumar Agarwal <aagarwa1@linux.ibm.com> > > gcc/ChangeLog: > > * tree-ssa-sink.cc (statement_sink_location): Add heuristics > for code sinking. > > gcc/testsuite/ChangeLog: > > * gcc.dg/tree-ssa/ssa-sink-20.c: New testcase. > * gcc.dg/tree-ssa/ssa-sink-21.c: New testcase. > --- > gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c | 16 ++++++++++++++++ > gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c | 20 ++++++++++++++++++++ > gcc/tree-ssa-sink.cc | 6 ++++++ > 3 files changed, 42 insertions(+) > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c > create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c > > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c > new file mode 100644 > index 00000000000..ed2aefc01aa > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-20.c > @@ -0,0 +1,16 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-sink -fdump-tree-optimized" } */ > + > +void bar(); > +int j; > +void foo(int a, int b, int c, int d, int e, int f) > +{ > + int l; > + l = a + b + c + d +e + f; > + if (a != 5) > + { why should we not sink the computes inside if (a != 5)? > + bar(); you probably want to avoid sinking after the call but since this is all in a single BB, GIMPLE doesn't really define a schedule of stmts here and so passes like sinking shouldn't really bother to look. > + j = l; > + } > +} > +/* { dg-final { scan-tree-dump-times "Sunk statements: 1" 0 "sink1" } } */ Btw, you probably want to check "Sunk statements: 0" 1 "sink1" instead. Otherwise sinking two stmts would be OK? > diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c > new file mode 100644 > index 00000000000..a39724df8ec > --- /dev/null > +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c > @@ -0,0 +1,20 @@ > +/* { dg-do compile } */ > +/* { dg-options "-O2 -fdump-tree-sink-stats" } */ > + > +void bar(); > +int j, x; > +void foo(int a, int b, int c, int d, int e, int f) > +{ > + int l; > + l = a + b + c + d +e + f; > + if (a != 5) > + { same here > + bar(); > + if (b != 3) > + x = 3; > + else > + x = 5; > + j = l; > + } > +} > +/* { dg-final { scan-tree-dump-times "Sunk statements: 1" 0 "sink1" } } */ > diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc > index 8de88b259a3..932fd71bec2 100644 > --- a/gcc/tree-ssa-sink.cc > +++ b/gcc/tree-ssa-sink.cc > @@ -465,6 +465,12 @@ statement_sink_location (gimple *stmt, basic_block frombb, > if (sinkbb == frombb) > return false; > > + /* The below heuristics describes the following. > + a) If the candidate to sink has call in the dominator basic > + basic blocks. > + b) statement to sink doesn't have use in the call. > + c) candidate block dominates sink block. > + In the above cases are true then don't do code sinking. */ but then the existing heuristic would try to find a better block. Consider +void bar(); +int j, x; +void foo(int a, int b, int c, int d, int e, int f) +{ + int l; + l = a + b + c + d +e + f; + if (a != 5) + { + if (b != 3) + x = 3; + else + x = 5; + bar(); + j = l; + } +} we do not want to completely disable sinking but might want to sink before bar() instead of not at all if the position after bar() we'd otherwise sink to is executed with the same conditions than the position before bar (). So I don't think the implementation is good at all - it wires things in the wrong place. Richard. > auto_vec<basic_block> h; > h = get_all_dominated_blocks (CDI_DOMINATORS, > frombb); > -- > 2.31.1 > > > > Richard. > > > >> if (sinkbb == gimple_bb (use)) > >> *togsi = gsi_for_stmt (use); > >> else > >> -- > >> 2.31.1 > >>
diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc index 87b1d40c174..8de88b259a3 100644 --- a/gcc/tree-ssa-sink.cc +++ b/gcc/tree-ssa-sink.cc @@ -465,6 +465,39 @@ statement_sink_location (gimple *stmt, basic_block frombb, if (sinkbb == frombb) return false; + auto_vec<basic_block> h; + h = get_all_dominated_blocks (CDI_DOMINATORS, + frombb); + bool is_call = false; + while (h.length ()) + { + basic_block bb = h.pop (); + + if (bb == frombb) + continue; + + for (gimple_stmt_iterator gsi = gsi_last_bb (bb); !gsi_end_p (gsi);) + { + gimple *stmt = gsi_stmt (gsi); + + if (is_gimple_call (stmt)) + { + is_call = true; + break; + } + + if (!gsi_end_p (gsi)) + gsi_prev (&gsi); + } + } + + if (!is_gimple_call (stmt) + && (gimple_bb (use) != frombb) + && !is_gimple_call (use) + && dominated_by_p (CDI_DOMINATORS, sinkbb, frombb) + && is_call) + return false; + if (sinkbb == gimple_bb (use)) *togsi = gsi_for_stmt (use); else