From patchwork Tue Oct 17 13:21:56 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajit Agarwal X-Patchwork-Id: 154294 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:2908:b0:403:3b70:6f57 with SMTP id ib8csp4131251vqb; Tue, 17 Oct 2023 06:23:30 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGWhnsyoDImtduItwOJeO11x6DjS2CTQa7uwRngoEkXBKucrEylcjaidjL5vZ7feWcTVZhQ X-Received: by 2002:a05:620a:bd6:b0:76c:ba25:a8f1 with SMTP id s22-20020a05620a0bd600b0076cba25a8f1mr2444183qki.42.1697549010479; Tue, 17 Oct 2023 06:23:30 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1697549010; cv=pass; d=google.com; s=arc-20160816; b=OtHz4zTW6vrUttkPKWtFkKVMfUGlkg5izG3u91ARdIkniDTa1qg9SBQL/9yPWEFrn0 /QvIWG8U/0VQ5mNCzFD4Pp/sd4LfrnBGj3bt2UdO2It5XX59Xn30kYfE3QfTDKmig4pO ltD7upqg4lDu5N21VWIv/8evf4E55cRWS5SRK3QricoVYW5V6MK7mhcPBc/cVMb03ApY 3Rf/W4aKDcwmca2XeDB8ZYam8lIkfpl9B8Q+bFR3IRfQ/tjY0e2jxwtCYGGLekZ9x8PS 1q46OEKQvVQSoqwWsEeTLdDKlAtfR1I7EqmOuJZ95jlSB4L+0s8D0Z3h7p3AsO9VQ4Sz MTjQ== ARC-Message-Signature: i=2; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-transfer-encoding :subject:cc:to:from:content-language:user-agent:mime-version:date :message-id:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=gsM2oACGSvo9zk1RO9u8APIIbXwmwF9Ae4KFOCnJC6k=; fh=X8j5e1dJjdlHlgTg85I9IhQsxuZPmIOnE3gNTY3uSto=; b=fW9J9jeNhsJJIUPCPT+o7YIxc0kTnZ55I7lqwP3YHDxo4M/xj4doBHRgPexQuCBrTO tEohvfPLhPnB3lca4EQgxlx0ncETJtV7EAz1mZGwymARGqIvJSJRqmlDeVcEcc7dvVUR M6lCx6c1kYx1kH7XMOHir8QXzLjHczbue97Y4YRvA3z4ozpaqCUa64NLwiZdnmY+cn1B 5pDbwP1IHiYVZga3ySgV6GQ3qMLiMgSYfsZcqIs+oMy+VUl4ptokXlsuscnbvTjetKCY Pe76rpYEyXTtfMfUayGECZgfrnEJRDt1XA1iZN2EVcr/uMr3EyPVcWXDZeI90YsrFhy5 wCIg== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=PEszhtb9; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id pe20-20020a05620a851400b00765a70926fbsi987794qkn.543.2023.10.17.06.23.30 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 17 Oct 2023 06:23:30 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) client-ip=8.43.85.97; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=PEszhtb9; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 8.43.85.97 as permitted sender) smtp.mailfrom="gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org"; dmarc=pass (p=REJECT sp=NONE dis=NONE) header.from=ibm.com Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 3E808385802F for ; Tue, 17 Oct 2023 13:23:30 +0000 (GMT) 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 AB7C23858C52 for ; Tue, 17 Oct 2023 13:23:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB7C23858C52 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=linux.ibm.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=linux.ibm.com ARC-Filter: OpenARC Filter v1.0.0 sourceware.org AB7C23858C52 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.156.1 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697548984; cv=none; b=QqqfBDGDwr5T5iMKOPS7MY+S+AMXU045xrxuwkIYQhsOpLttu6eKwfO++iPryVhZn8KnUIU9ls9q6Xx4UYCZ0c+tzCEnflNk1CHjOh714LRTa1IA5EhdREh4muv7lzwOky/IEaAO/b6jpuXqjL5LCs2Bntb+pG7qvZ0oEWWscO8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1697548984; c=relaxed/simple; bh=MClfMSXS1KsBwTPlnzVq5JIsPOnSaZUl53wwxUVHM0A=; h=DKIM-Signature:Message-ID:Date:MIME-Version:From:To:Subject; b=I8U74xYRyULJzfhGXj24Ca8XeiOzskAGsCdJv9of99Rvrmj+yuRZvWyn4iKhKhz00SVibTbBE5TET+ZZIY9NOLkoqLwTUNYqen9rBgsmhUGRofGAWGkWHe144XJMXEiOcwuM5jkC/ZsCz7gDOD3jqdLkiMjIv3Po12Ddlll9MPg= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0356517.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 39HDLeM7021953; Tue, 17 Oct 2023 13:23:01 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : from : to : cc : subject : content-type : content-transfer-encoding; s=pp1; bh=gsM2oACGSvo9zk1RO9u8APIIbXwmwF9Ae4KFOCnJC6k=; b=PEszhtb9hgh9VEoeq2fLWIeYSVEU5f0Ytx9s7rqZ/dvzk9lV9XjVdBueiftULT+GBxyq rsLtBaCs5TZSDUvIRf7UipgbVBJUtVOs56taF+IvgtFBA5iFKdul04dlSeW7zmCqUNEI vbNZCW8URDNyDYzh884cIPdhnzYg6GGCYaYegGH9CneIYn1fdc7Movn+HdxB60eSEHd9 HS82hzGzZ8E5w8ERamHzE1ZQOTLsdrCiduaoCUrNE62/t4dgiotcMnA3BEMlt6zC2IOB Vy6ofHk5gjvOEebxZ5n5omOXwf5zRNE6FtfEfb4Ji79dpy42fzQgXZTFQjfzGnJkXDfN QA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3tstt38p3w-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 17 Oct 2023 13:23:00 +0000 Received: from m0356517.ppops.net (m0356517.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 39HDLtLA023216; Tue, 17 Oct 2023 13:22:40 GMT Received: from ppma23.wdc07v.mail.ibm.com (5d.69.3da9.ip4.static.sl-reverse.com [169.61.105.93]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3tstt38m87-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 17 Oct 2023 13:22:39 +0000 Received: from pps.filterd (ppma23.wdc07v.mail.ibm.com [127.0.0.1]) by ppma23.wdc07v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 39HC8IHg027154; Tue, 17 Oct 2023 13:22:01 GMT Received: from smtprelay01.wdc07v.mail.ibm.com ([172.16.1.68]) by ppma23.wdc07v.mail.ibm.com (PPS) with ESMTPS id 3tr6tk8xdr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Tue, 17 Oct 2023 13:22:01 +0000 Received: from smtpav04.wdc07v.mail.ibm.com (smtpav04.wdc07v.mail.ibm.com [10.39.53.231]) by smtprelay01.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 39HDM1XX54460820 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Tue, 17 Oct 2023 13:22:01 GMT Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 202D958052; Tue, 17 Oct 2023 13:22:01 +0000 (GMT) Received: from smtpav04.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id AAA9D58050; Tue, 17 Oct 2023 13:21:58 +0000 (GMT) Received: from [9.43.39.223] (unknown [9.43.39.223]) by smtpav04.wdc07v.mail.ibm.com (Postfix) with ESMTP; Tue, 17 Oct 2023 13:21:58 +0000 (GMT) Message-ID: <319f0053-b18e-4e36-96cd-8713c6de31a7@linux.ibm.com> Date: Tue, 17 Oct 2023 18:51:56 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US From: Ajit Agarwal To: gcc-patches , Richard Biener Cc: Jeff Law , Segher Boessenkool , Peter Bergner Subject: [PATCH v10] tree-ssa-sink: Improve code sinking pass X-TM-AS-GCONF: 00 X-Proofpoint-ORIG-GUID: TI68iWCRdB2w679KEZIKfgn80ZVQ4COh X-Proofpoint-GUID: LN6bpxPrp9oi0S-Aq6BNTrOasPxMVoP1 X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.980,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-10-17_02,2023-10-17_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 mlxscore=0 adultscore=0 suspectscore=0 phishscore=0 malwarescore=0 mlxlogscore=999 impostorscore=0 spamscore=0 bulkscore=0 lowpriorityscore=0 clxscore=1015 priorityscore=1501 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2309180000 definitions=main-2310170113 X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_MSPIKE_H4, RCVD_IN_MSPIKE_WL, SPF_HELO_NONE, SPF_PASS, TXREP 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.30 Precedence: list List-Id: Gcc-patches mailing list List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1780009151351636964 X-GMAIL-MSGID: 1780009151351636964 Currently, code sinking will sink code at the use points with loop having same nesting depth. The following patch improves code sinking by placing the sunk code in immediate dominator with same loop nest depth. Review comments are incorporated. For example : 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; } } Code Sinking does the following: void bar(); int j; void foo(int a, int b, int c, int d, int e, int f) { int l; if (a != 5) { l = a + b + c + d +e + f; bar(); j = l; } } Bootstrapped regtested on powerpc64-linux-gnu. Thanks & Regards Ajit tree-ssa-sink: Improve code sinking pass Currently, code sinking will sink code at the use points with loop having same nesting depth. The following patch improves code sinking by placing the sunk code in immediate dominator with same loop nest depth. 2023-10-17 Ajit Kumar Agarwal gcc/ChangeLog: PR tree-optimization/81953 * tree-ssa-sink.cc (statement_sink_location): Move statements with same loop nest depth. (select_best_block): Add heuristics to select the best blocks in the immediate dominator. gcc/testsuite/ChangeLog: PR tree-optimization/81953 * gcc.dg/tree-ssa/ssa-sink-21.c: New test. * gcc.dg/tree-ssa/ssa-sink-22.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c | 15 +++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c | 19 +++++++++++++++++++ gcc/tree-ssa-sink.cc | 16 +++++++++++----- 3 files changed, 45 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c 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..d3b79ca5803 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-21.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-sink-stats" } */ +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 {l_12\s+=\s+_4\s+\+\s+f_11\(D\);\n\s+bar\s+\(\)} sink1 } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c new file mode 100644 index 00000000000..84e7938c54f --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/ssa-sink-22.c @@ -0,0 +1,19 @@ +/* { 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 {l_13\s+=\s+_4\s+\+\s+f_12\(D\);\n\s+bar\s+\(\)} sink1 } } */ diff --git a/gcc/tree-ssa-sink.cc b/gcc/tree-ssa-sink.cc index a360c5cdd6e..d96df0d81e9 100644 --- a/gcc/tree-ssa-sink.cc +++ b/gcc/tree-ssa-sink.cc @@ -176,6 +176,9 @@ nearest_common_dominator_of_uses (def_operand_p def_p, bool *debug_stmts) tree, return the best basic block between them (inclusive) to place statements. + The best basic block should be an immediate dominator of + best basic block if we've moved to same loop nest. + We want the most control dependent block in the shallowest loop nest. If the resulting block is in a shallower loop nest, then use it. Else @@ -204,11 +207,16 @@ select_best_block (basic_block early_bb, if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb)) best_bb = temp_bb; + /* If we've moved into same loop nest, then that becomes + our best block. */ + if (!gimple_vuse (stmt) + && bb_loop_depth (temp_bb) == bb_loop_depth (best_bb)) + best_bb = temp_bb; + /* Walk up the dominator tree, hopefully we'll find a shallower loop nest. */ temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb); } - /* Placing a statement before a setjmp-like function would be invalid (it cannot be reevaluated when execution follows an abnormal edge). If we selected a block with abnormal predecessors, just punt. */ @@ -430,6 +438,7 @@ statement_sink_location (gimple *stmt, basic_block frombb, continue; break; } + use = USE_STMT (one_use); if (gimple_code (use) != GIMPLE_PHI) @@ -439,10 +448,7 @@ statement_sink_location (gimple *stmt, basic_block frombb, if (sinkbb == frombb) return false; - if (sinkbb == gimple_bb (use)) - *togsi = gsi_for_stmt (use); - else - *togsi = gsi_after_labels (sinkbb); + *togsi = gsi_after_labels (sinkbb); return true; }