From patchwork Mon Oct 30 12:10:18 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Ajit Agarwal X-Patchwork-Id: 159720 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:d641:0:b0:403:3b70:6f57 with SMTP id cy1csp2160837vqb; Mon, 30 Oct 2023 05:10:54 -0700 (PDT) X-Google-Smtp-Source: AGHT+IFMqcTqp1HCANL1JxmAyMhJHeyxS98+E0Hhwnzdi+X2UR05FnV6d8WhlFpzHwlT7bNRD0Yy X-Received: by 2002:a67:c015:0:b0:457:6999:968a with SMTP id v21-20020a67c015000000b004576999968amr8568708vsi.7.1698667854750; Mon, 30 Oct 2023 05:10:54 -0700 (PDT) ARC-Seal: i=2; a=rsa-sha256; t=1698667854; cv=pass; d=google.com; s=arc-20160816; b=ucZvUuViZRDrVWG/srsUSchAAlpKG+8fxfZkRTV299WWMuBs50jtjS6JZs3DjmSZfN +STs8yGbzPh5uJVK3UqXPc1gFOHLwk1uObYfA2+kMaCkr7lKjGEOGamL9dQ+xi9EU6ku pKvA6gAFh22DlKVlLjpQ6pYf1mceahGUbpVsK4LYy66GWLKCMlJmrz8NA2hIqR0T9Mn+ 3DI6o2tF8RqwsREWSQSKtFwkFJXhmBMMzgbTzyEbgnmjCrBVyRZjKqHIRU2TIRFyYuUC zIws76AKTNxRoDhwHfEjY+yrVAZfE/nbdEtFp/+m9itK/YW1jvyXirMWxkMNl0B4IpA9 m2pQ== 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=gYmY/TvpgGuBpM8BvrwWK1S250DKzh9IEHgnWflYHOU=; fh=X8j5e1dJjdlHlgTg85I9IhQsxuZPmIOnE3gNTY3uSto=; b=tHhgWwNy8rDMyAm/pIWpD/QTrL0PcGmTFhxuYUmSgTtB0qwkyuO2LA/l2gTGAyPKyb IdonKaOkz0anFvNHEkBAjco8v5W58PLLr08PK0we1uhQD8wrhPbi/ix/kIZ3IbfkZlmH TmcrLp8X8Fptx+u5fFqcXJgkA6Q8PUipoIPJTxbHA0Spv8OyVOY4IbuwPyQ7UjIu/uXi ddo5JvaaOGjWmc4OeWl/s0FT4NuMUqlAs1xXdQ7w125Nod+6EGwLCGO8fpqVp8DHcIg9 dopa1ULJo7OQf8iZMZPWZavN1aB3JkX+APczWZOdq3h2XWtIdb/Y6b3xfY5fjTMMmc+i Fgcw== ARC-Authentication-Results: i=2; mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=FMPTLg4P; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id o23-20020ab03b57000000b007aba89814f3si764558uaw.71.2023.10.30.05.10.54 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 30 Oct 2023 05:10:54 -0700 (PDT) Received-SPF: pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c as permitted sender) client-ip=2620:52:3:1:0:246e:9693:128c; Authentication-Results: mx.google.com; dkim=pass header.i=@ibm.com header.s=pp1 header.b=FMPTLg4P; arc=pass (i=1); spf=pass (google.com: domain of gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org designates 2620:52:3:1:0:246e:9693:128c 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 6F9AD3858430 for ; Mon, 30 Oct 2023 12:10:54 +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 7C7BC3858D20 for ; Mon, 30 Oct 2023 12:10:28 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 7C7BC3858D20 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 7C7BC3858D20 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=1698667830; cv=none; b=jo0CLH68HYxzgBe2I/mkWCKogoqSMVdhSLRyzYN0+y+zYx6Q44Vls6OTcRMqmOil73MiUmEOdPptVLT7o/NeU9cZN8IaXV0DTJwGDRWLVO2fbK6EelNX8K3xwXypIH/8uYpBNMCUFz3XAITZhzNhu/fXCeOvaiArDi6tkXBEJwQ= ARC-Message-Signature: i=1; a=rsa-sha256; d=sourceware.org; s=key; t=1698667830; c=relaxed/simple; bh=nbCd99/Tb2fa7z6rAksbMfVNgdM6sdo3yTe1NiZ3IAM=; h=DKIM-Signature:Message-ID:Date:MIME-Version:To:From:Subject; b=dSmHeazwlBg54sZm/IRjSsVyTx2iuk0Ha7F8seDJz9PNeXcRfJtNOAx96yXrr/Il4Ns7S4ylsfirZOUPljmKSD4n0A+izBmZrjElo72O8f/BEudI96AUvorj3eeR5PlTXXYgiSEdL5Z/6hPR3z/0VNuFUBuxrhqaYs0io0UJbbs= 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 39UC67Kq008728; Mon, 30 Oct 2023 12:10:27 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=gYmY/TvpgGuBpM8BvrwWK1S250DKzh9IEHgnWflYHOU=; b=FMPTLg4PucpkZ9LEd4J7VHkMb06W0CZwQh9ncV7NB0rd7TrHeTwvr58iLc8IW9ohcV3h l4Ee3kDhXEs5hboQ2VIGqUmBoWary93baYHAMnon6CrWmAnc12J3aek3Z1IaCJZWRY3a 7TAtLG5rKlsrzUnJzuX8b05uqM3H5/FpQA3000oy9OZHymLZ/IITXCghG/8N9QQl4HiK QZ3d8GcoWgVi/ElyDzH6aJ2aYT8C9KnGO94xGhOIJiFGiJDNaMynT40pFt5k7Gtar+yx tg8cNWt5Kf80ANfSq6ihAjva/ETT6RyepXjxdZnUTA9XRYoAHj75BE9pvQBgk/0S1VRg Bg== Received: from pps.reinject (localhost [127.0.0.1]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3u2c6u84t5-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Oct 2023 12:10:26 +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 39UC7MlW012660; Mon, 30 Oct 2023 12:10:25 GMT Received: from ppma11.dal12v.mail.ibm.com (db.9e.1632.ip4.static.sl-reverse.com [50.22.158.219]) by mx0a-001b2d01.pphosted.com (PPS) with ESMTPS id 3u2c6u84r2-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Oct 2023 12:10:25 +0000 Received: from pps.filterd (ppma11.dal12v.mail.ibm.com [127.0.0.1]) by ppma11.dal12v.mail.ibm.com (8.17.1.19/8.17.1.19) with ESMTP id 39UAfakZ031403; Mon, 30 Oct 2023 12:10:23 GMT Received: from smtprelay07.wdc07v.mail.ibm.com ([172.16.1.74]) by ppma11.dal12v.mail.ibm.com (PPS) with ESMTPS id 3u1fb1raq4-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-GCM-SHA384 bits=256 verify=NOT); Mon, 30 Oct 2023 12:10:23 +0000 Received: from smtpav01.dal12v.mail.ibm.com (smtpav01.dal12v.mail.ibm.com [10.241.53.100]) by smtprelay07.wdc07v.mail.ibm.com (8.14.9/8.14.9/NCO v10.0) with ESMTP id 39UCAMaj28312282 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-GCM-SHA384 bits=256 verify=OK); Mon, 30 Oct 2023 12:10:23 GMT Received: from smtpav01.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id C36D758059; Mon, 30 Oct 2023 12:10:22 +0000 (GMT) Received: from smtpav01.dal12v.mail.ibm.com (unknown [127.0.0.1]) by IMSVA (Postfix) with ESMTP id 999FD58057; Mon, 30 Oct 2023 12:10:20 +0000 (GMT) Received: from [9.43.55.204] (unknown [9.43.55.204]) by smtpav01.dal12v.mail.ibm.com (Postfix) with ESMTP; Mon, 30 Oct 2023 12:10:20 +0000 (GMT) Message-ID: Date: Mon, 30 Oct 2023 17:40:18 +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 V11] : tree-ssa-sink: Improve code sinking pass X-TM-AS-GCONF: 00 X-Proofpoint-GUID: 0Mkhfp4PXpLwXon5LsO9prhcxGYVhIvk X-Proofpoint-ORIG-GUID: EPwnicegtsd2UnhL4J6tK26E6lGQnm33 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-10-30_10,2023-10-27_01,2023-05-22_02 X-Proofpoint-Spam-Details: rule=outbound_notspam policy=outbound score=0 clxscore=1015 priorityscore=1501 adultscore=0 impostorscore=0 bulkscore=0 malwarescore=0 suspectscore=0 mlxscore=0 spamscore=0 phishscore=0 mlxlogscore=999 lowpriorityscore=0 classifier=spam adjust=0 reason=mlx scancount=1 engine=8.12.0-2310240000 definitions=main-2310300092 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 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: 1781182344490751166 X-GMAIL-MSGID: 1781182344490751166 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-10-30 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 | 21 ++++++++++++++------- 3 files changed, 48 insertions(+), 7 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..0b823b81309 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 @@ -201,14 +204,13 @@ select_best_block (basic_block early_bb, { /* If we've moved into a lower loop nest, then that becomes our best block. */ - if (bb_loop_depth (temp_bb) < bb_loop_depth (best_bb)) + if (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. */ @@ -250,7 +252,14 @@ 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 +439,7 @@ statement_sink_location (gimple *stmt, basic_block frombb, continue; break; } + use = USE_STMT (one_use); if (gimple_code (use) != GIMPLE_PHI) @@ -439,10 +449,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; }