From patchwork Fri Nov 17 07:25:23 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajit Agarwal X-Patchwork-Id: 166001 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9910:0:b0:403:3b70:6f57 with SMTP id i16csp355363vqn; Thu, 16 Nov 2023 23:25:56 -0800 (PST) X-Google-Smtp-Source: AGHT+IFyJo6CjVtHFVf5eLtRxZ/hVN1LHKGTx6/84uRyq/Y5LmrTQanCg6kulCTvqfQFBTomfghO X-Received: by 2002:a05:620a:2402:b0:775:8040:edf6 with SMTP id d2-20020a05620a240200b007758040edf6mr12125708qkn.48.1700205956416; Thu, 16 Nov 2023 23:25:56 -0800 (PST) ARC-Seal: i=2; a=rsa-sha256; t=1700205956; cv=pass; d=google.com; s=arc-20160816; b=WTyua9QccOy0CPQGPtoj+I9qswXw5TQMuCnb/QYaNxlJSkpsuOZvSjyW7KiuQ1WMtQ 7oyDz35ZjVGrzJoVbKq2n3VdZqro3HlQiSnaEj4YT83lkd6kW2Mn4ZAgckrvGOFcEP8w xjzgyDojAPtregWlw6T1vulqalqfJ4MkrPfgNND7hQ/Z36rRN1YP2TX/y9lB0XPjsy8O pZZOUigZkDW6/eqeYAX6KDeNeFYK80bemdA9jr8a4OyvizRslysbtEVeAhVNoKuMYnma /G4KQJTVT3/jc+7EalkClVbjaclNk17ytp/jVCLYpnVtTonvFDvZGigObNmNbyUlJCZe INcA== 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:from:cc:to:content-language:user-agent:mime-version:date :message-id:dkim-signature:arc-filter:dmarc-filter:delivered-to; bh=NFv+7ZacypRVQVkWPY+O18nY0Ab3PPObZ3uI17Z2Kkk=; fh=X8j5e1dJjdlHlgTg85I9IhQsxuZPmIOnE3gNTY3uSto=; b=zR8c+lIKyktbfLzm8KKgyYc1vq83883/oEHOaJzWgJfivYYs420hP+Tg0BWw3cDDcB nTIJyyAyketfilxPJUfbyIQQUYPFVZkepGWMuc0DBHkdFmXrA/BcRyrsCC3Vd1a/p77w eug7nDfNXJrGYcnz6j9zt9k/CcGh9NyWxUiYFtAUNfSyGF4FE5UoS9onSOmmHGVny/uR a6ySLczKwuHwjA/rUQtbZVudr6gQFAfK2QjYTsKiQNzny0ysmB3yHf5J6/MBY5GXhvt8 E7faGPFCFWnQI9qGSlON62wvZYQi2kpY+QvDxUHDoBK5xw7czr0gJy47j19uOzakVpKc feQA== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=POdovFeT; 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id n21-20020a05620a295500b0076dbec5d9a0si1153778qkp.710.2023.11.16.23.25.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Thu, 16 Nov 2023 23:25:56 -0800 (PST) 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=POdovFeT; 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 EE4C3385828E for ; Fri, 17 Nov 2023 07:25:55 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-001b2d01.pphosted.com (mx0b-001b2d01.pphosted.com [148.163.158.5]) by sourceware.org (Postfix) with ESMTPS id D57533858402 for ; Fri, 17 Nov 2023 07:25:31 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org D57533858402 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 D57533858402 Authentication-Results: server2.sourceware.org; arc=none smtp.remote-ip=148.163.158.5 ARC-Seal: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700205933; cv=none; b=I5LZowF9P9TVbhNSXeZEdgaR0lHNEp7ZiDXbPneBWFyPNAv5ajgmCepopp7G67tENbVdCZi3eFDiPZviro/eLDAFKZUFq8IvBhr6McMP5dBUOh887b8DvGIiNPMRtFQSu2Ithjd04wZ5xMbZ6eYd8E2JNYhKRRBauzbZqRWnm+8= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1700205933; c=relaxed/simple; bh=AT++oNnWuV6SXt//afTbF6cN0Xj2J+K6MmR3YLLuE5Q=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=dPgBzFRwXFPUTa2w0GaGZadyLJpvKDOmiQDg5ykzLXgbZrZ7UeY+hil+Zp6gGxXxVc3ID5Z8VzsBH4+V3vReJGbZOoYFXqlTBb+khZRJF4I+0LJiCQ67lLjzn8SNbmYJdjlZ7RAIRFCzyLm4/UMDR0wKGSZ6k8PGKjU5hBgqGRk= ARC-Authentication-Results: i=1; server2.sourceware.org Received: from pps.filterd (m0356516.ppops.net [127.0.0.1]) by mx0a-001b2d01.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 3AH77ZgY000998; Fri, 17 Nov 2023 07:25:30 GMT DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=ibm.com; h=message-id : date : mime-version : to : cc : from : subject : content-type : content-transfer-encoding; s=pp1; bh=NFv+7ZacypRVQVkWPY+O18nY0Ab3PPObZ3uI17Z2Kkk=; b=POdovFeT/UPTBt8Y2Nfix7U+9aLmcTAoH0S2jmaKVXd3nPTXndtWKT8N0Bu8W18wRyib B/q+m6OuszthPBsU3x0OLv+qr3GtpBROvijJEXKBcyxYcl6082fXRJ9eIuKPbR1uMtom mJ/4YXl7ejq821PqqIYpg9hH0BJ4ngwrTSQGsDbxm5FbC5cZZId+lmRxOj5sVY7yXsFs tHKEr/DOl1/MupwNdQmV8VCEQO7kcj/BhQ25Jdx4jUdYqJ352kqLSRUIqhlos8g04utb kx1YQ7HANsVEV+zAQNBWT1gFU6/rlT7i4RIkYSKmyCArdky5/7dDHmCdIGI84Ry9Inw6 fA== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3ue3gvrpc8-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 17 Nov 2023 07:25:30 +0000 Received: from m0356516.ppops.net (m0356516.ppops.net [127.0.0.1]) by pps.reinject (8.17.1.5/8.17.1.5) with ESMTP id 3AH789Ui001790; Fri, 17 Nov 2023 07:25:29 GMT Received: from ppma13.dal12v.mail.ibm.com (dd.9e.1632.ip4.static.sl-reverse.com [50.22.158.221]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3ue3gvrpbm-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 17 Nov 2023 07:25:29 +0000 Received: from pps.filterd (ppma13.dal12v.mail.ibm.com [127.0.0.1]) by ppma13.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 3AH6IusL022448; Fri, 17 Nov 2023 07:25:28 GMT Received: from smtprelay02.wdc07v.mail.ibm.com ([172.16.1.69]) by ppma13.dal12v.mail.ibm.com (PPS) with ESMTPS id 3uap5kksd4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Fri, 17 Nov 2023 07:25:28 +0000 Received: from smtpav02.wdc07v.mail.ibm.com (smtpav02.wdc07v.mail.ibm.com [10.39.53.229]) by smtprelay02.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 3AH7PSvV58196476 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Fri, 17 Nov 2023 07:25:28 GMT Received: from smtpav02.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 063395805B; Fri, 17 Nov 2023 07:25:28 +0000 (GMT) Received: from smtpav02.wdc07v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id B5FA658059; Fri, 17 Nov 2023 07:25:25 +0000 (GMT) Received: from [9.43.10.28] (unknown [9.43.10.28]) by smtpav02.wdc07v.mail.ibm.com (Postfix) with ESMTP; Fri, 17 Nov 2023 07:25:25 +0000 (GMT) Message-ID: Date: Fri, 17 Nov 2023 12:55:23 +0530 MIME-Version: 1.0 User-Agent: Mozilla Thunderbird Content-Language: en-US To: gcc-patches , Richard Biener Cc: Jeff Law , Segher Boessenkool , Peter Bergner From: Ajit Agarwal Subject: [PATCH V12] tree-ssa-sink: Improve code sinking pass X-TM-AS-GCONF: 00 X-Proofpoint-GUID: -j4jT-CfToBlU7sKbiXCQb4d_ZomL_9U X-Proofpoint-ORIG-GUID: 5eggKvtPWn0XMxjE5_339wfacSz8z1fR X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.272,Aquarius:18.0.987,Hydra:6.0.619,FMLib:17.11.176.26 definitions=2023-11-17_05,2023-11-16_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 lowpriorityscore=0 phishscore=0 spamscore=0 mlxscore=0 clxscore=1015 impostorscore=0 adultscore=0 malwarescore=0 priorityscore=1501 suspectscore=0 bulkscore=0 mlxlogscore=991 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2311060000 definitions=main-2311170052 X-Spam-Status: No, score=-12.7 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, 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.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: 1782795161087378812 X-GMAIL-MSGID: 1782795161087378812 Hello Richard: 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-11-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 dominato for same loop nest depthr. 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 | 29 +++++++++++++++++---- 3 files changed, 58 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..4e6568df4e5 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 @@ -209,6 +212,18 @@ select_best_block (basic_block early_bb, temp_bb = get_immediate_dominator (CDI_DOMINATORS, temp_bb); } + temp_bb = best_bb; + /* If we've moved into a same loop nest, then that becomes + our best block. */ + while (best_bb == late_bb && temp_bb != early_bb + && bb_loop_depth (temp_bb) == bb_loop_depth (best_bb)) + { + best_bb = temp_bb; + /* Walk up the dominator tree, hopefully we'll find a best + block to move in same 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. */ @@ -250,7 +265,13 @@ select_best_block (basic_block early_bb, /* If result of comparsion is unknown, prefer EARLY_BB. Thus use !(...>=..) rather than (...<...) */ && !(best_bb->count * 100 >= early_bb->count * threshold)) - return best_bb; + { + /* Avoid sinking to immediate dominator if the statement to be moved + has memory operand and same loop nest. */ + if (best_bb != late_bb && gimple_vuse (stmt)) + return late_bb; + return best_bb; + } /* No better block found, so return EARLY_BB, which happens to be the statement's original block. */ @@ -430,6 +451,7 @@ statement_sink_location (gimple *stmt, basic_block frombb, continue; break; } + use = USE_STMT (one_use); if (gimple_code (use) != GIMPLE_PHI) @@ -439,10 +461,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; }