From patchwork Thu Sep 14 05:33:50 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 139327 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a05:612c:172:b0:3f2:4152:657d with SMTP id h50csp127652vqi; Wed, 13 Sep 2023 22:34:48 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGI2YzOR3xOJ6beOzUhNuaAq/56w73BWLKvdC0mP6tk3nv+cGVEqdIK6s8cckbpihnoumtZ X-Received: by 2002:a2e:81d2:0:b0:2bc:b75e:b8b with SMTP id s18-20020a2e81d2000000b002bcb75e0b8bmr4414876ljg.38.1694669688681; Wed, 13 Sep 2023 22:34:48 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694669688; cv=none; d=google.com; s=arc-20160816; b=P5reufZC24cwMeNd3Ivx5Olej/00cDNcsnB6QgSn7X37q3c3WNwwG5XfgNqf95w2gQ Kcgpp17Sd/C6W4eeSjnPc3n8TyjWKo/ycZJJU+iIZ5ekb9p1+SAfVU6tjIBYWedn0jIu /3WtvLmi1ynwLrBptqNpeWSlf1GCIqlXl5nIWEXApsWIE7CEI8Xh8/lPLkAX/8k8QJ1G dHnEz6WKQnqu2aCXoSfll1nzbdNo6OyTeb3VCOyMLjnWM6TElguKeeGZ+2btsXcYqrTp XAryyNF1DsgVMB4TYjKuO1K61rHHJyX5WjKkLkx1+CPWrKOjmL6h4XzIY9peSr5yLaf8 rvLg== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:reply-to:from:list-subscribe:list-help:list-post :list-archive:list-unsubscribe:list-id:precedence :content-transfer-encoding:mime-version:message-id:date:subject:cc :to:dmarc-filter:delivered-to:dkim-signature:dkim-filter; bh=3fpRw1VtjhvZnrFzFakBnVfOayJlrQwmojqtwXavBJ0=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=HAR2thVkOotuxu0jm8ARvsEzLA6hN0S0pcW3TEIzBYUd1XFA9KWhjfr1ML08b1J3U1 np4TIdN0xl34xeEgwgq/AP9Icoc/Y2AVPN8M5BlGQBQtCBKXIrCCG5pOCdbD09mEc98U RdmB/0lmgSx1GXjI47fNsX5lx+DcsnSiwdVYlHwcVcnAc58/lz5tt7wFrrFIxMErjIIM i/+kIAoWnHz+Lwb8WMq5JQ7AIgcxHj6DMgXeN8FTtZBavanmmzyjJ+i1MVZHy4R0eqDH ZsB31zcGkes2i39ShLp0FtCq+B/d4BEUKbYTRCTH3lKy80TFC0b29svwodlgxXD7Sufi 53Rw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=VOO6ZDtp; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (server2.sourceware.org. [2620:52:3:1:0:246e:9693:128c]) by mx.google.com with ESMTPS id pw8-20020a17090720a800b00991cb7517bdsi602787ejb.948.2023.09.13.22.34.48 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 13 Sep 2023 22:34:48 -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=@gcc.gnu.org header.s=default header.b=VOO6ZDtp; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id 5A51A3858024 for ; Thu, 14 Sep 2023 05:34:47 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 5A51A3858024 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694669687; bh=3fpRw1VtjhvZnrFzFakBnVfOayJlrQwmojqtwXavBJ0=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=VOO6ZDtp30EiHydTRFPlBWYi8v4kqo19+zpg0oWYLPjveznrza5DAaQT5ljBC0L6l Pcclai7K9nn2RLPnnBXNID0bZfXnnjXF2RnWFPNhOjM9OYI5vx3pPLdkeMyUejKe6P RGenPEeYBtTs+iXPhpA2BHwk7O7LiPqdpVLm5sR8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0a-0016f401.pphosted.com [67.231.148.174]) by sourceware.org (Postfix) with ESMTPS id E0BC83858D20 for ; Thu, 14 Sep 2023 05:34:03 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org E0BC83858D20 Received: from pps.filterd (m0045849.ppops.net [127.0.0.1]) by mx0a-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 38DK0C6h014638 for ; Wed, 13 Sep 2023 22:34:01 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3t3kqrhkgx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 13 Sep 2023 22:34:01 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Wed, 13 Sep 2023 22:33:59 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Wed, 13 Sep 2023 22:33:59 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 4FD923F7053; Wed, 13 Sep 2023 22:33:59 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] MATCH: Support `(a != (CST+1)) & (a > CST)` optimizations Date: Wed, 13 Sep 2023 22:33:50 -0700 Message-ID: <20230914053350.1533941-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: Pwss6qHq4OYtHprh6PqChVhTEFRdyKie X-Proofpoint-GUID: Pwss6qHq4OYtHprh6PqChVhTEFRdyKie X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.980,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-14_03,2023-09-13_01,2023-05-22_02 X-Spam-Status: No, score=-14.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, 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: , X-Patchwork-Original-From: Andrew Pinski via Gcc-patches From: Andrew Pinski Reply-To: Andrew Pinski Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" X-getmail-retrieved-from-mailbox: INBOX X-GMAIL-THRID: 1776989963373451647 X-GMAIL-MSGID: 1776989963373451647 Even though this is done via reassocation, match can support these with a simple change to detect that the difference is just one. This allows to optimize these earlier and even during phiopt for an example. This patch adds the following cases: (a != (CST+1)) & (a > CST) -> a > (CST+1) (a != (CST-1)) & (a < CST) -> a < (CST-1) (a == (CST-1)) | (a >= CST) -> a >= (CST-1) (a == (CST+1)) | (a <= CST) -> a <= (CST+1) Canonicalizations of comparisons causes this case to show up more. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. PR tree-optimization/106164 gcc/ChangeLog: * match.pd (`(X CMP1 CST1) AND/IOR (X CMP2 CST2)`): Expand to support constants that are off by one. gcc/testsuite/ChangeLog: * gcc.dg/pr21643.c: Update test now that match does the combing of the comparisons. * gcc.dg/tree-ssa/cmpbit-5.c: New test. * gcc.dg/tree-ssa/phi-opt-35.c: New test. --- gcc/match.pd | 44 ++++++++++++++++++- gcc/testsuite/gcc.dg/pr21643.c | 6 ++- gcc/testsuite/gcc.dg/tree-ssa/cmpbit-5.c | 51 ++++++++++++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/phi-opt-35.c | 13 ++++++ 4 files changed, 111 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/cmpbit-5.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/phi-opt-35.c diff --git a/gcc/match.pd b/gcc/match.pd index 7ecf5568599..07ffd831132 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -2970,10 +2970,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && operand_equal_p (@1, @2))) (with { + bool one_before = false; + bool one_after = false; int cmp = 0; if (TREE_CODE (@1) == INTEGER_CST && TREE_CODE (@2) == INTEGER_CST) - cmp = tree_int_cst_compare (@1, @2); + { + cmp = tree_int_cst_compare (@1, @2); + if (cmp < 0 + && wi::to_wide (@1) == wi::to_wide (@2) - 1) + one_before = true; + if (cmp > 0 + && wi::to_wide (@1) == wi::to_wide (@2) + 1) + one_after = true; + } bool val; switch (code2) { @@ -2998,6 +3008,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && code2 == LE_EXPR && cmp == 0) (lt @0 @1)) + /* (a != (b+1)) & (a > b) -> a > (b+1) */ + (if (code1 == NE_EXPR + && code2 == GT_EXPR + && one_after) + (gt @0 @1)) + /* (a != (b-1)) & (a < b) -> a < (b-1) */ + (if (code1 == NE_EXPR + && code2 == LT_EXPR + && one_before) + (lt @0 @1)) ) ) ) @@ -3069,10 +3089,20 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && operand_equal_p (@1, @2))) (with { + bool one_before = false; + bool one_after = false; int cmp = 0; if (TREE_CODE (@1) == INTEGER_CST && TREE_CODE (@2) == INTEGER_CST) - cmp = tree_int_cst_compare (@1, @2); + { + cmp = tree_int_cst_compare (@1, @2); + if (cmp < 0 + && wi::to_wide (@1) == wi::to_wide (@2) - 1) + one_before = true; + if (cmp > 0 + && wi::to_wide (@1) == wi::to_wide (@2) + 1) + one_after = true; + } bool val; switch (code2) { @@ -3097,6 +3127,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) && code2 == LT_EXPR && cmp == 0) (le @0 @1)) + /* (a == (b-1)) | (a >= b) -> a >= (b-1) */ + (if (code1 == EQ_EXPR + && code2 == GE_EXPR + && one_before) + (ge @0 @1)) + /* (a == (b+1)) | (a <= b) -> a <= (b-1) */ + (if (code1 == EQ_EXPR + && code2 == LE_EXPR + && one_after) + (le @0 @1)) ) ) ) diff --git a/gcc/testsuite/gcc.dg/pr21643.c b/gcc/testsuite/gcc.dg/pr21643.c index 4e7f93d351a..42517b5af1e 100644 --- a/gcc/testsuite/gcc.dg/pr21643.c +++ b/gcc/testsuite/gcc.dg/pr21643.c @@ -86,4 +86,8 @@ f9 (unsigned char c) return 1; } -/* { dg-final { scan-tree-dump-times "Optimizing range tests c_\[0-9\]*.D. -.0, 31. and -.32, 32.\[\n\r\]* into" 6 "reassoc1" } } */ +/* Note with match being able to simplify this, optimizing range tests is no longer needed here. */ +/* Equivalence: _7 | _2 -> c_5(D) <= 32 */ +/* old test: dg-final scan-tree-dump-times "Optimizing range tests c_\[0-9\]*.D. -.0, 31. and -.32, 32.\[\n\r\]* into" 6 "reassoc1" */ +/* { dg-final { scan-tree-dump-times "Equivalence: _\[0-9\]+ \\\| _\[0-9\]+ -> c_\[0-9\]+.D. <= 32" 5 "reassoc1" } } */ +/* { dg-final { scan-tree-dump-times "Equivalence: _\[0-9\]+ \& _\[0-9\]+ -> c_\[0-9\]+.D. > 32" 1 "reassoc1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-5.c b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-5.c new file mode 100644 index 00000000000..d81a129825b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/cmpbit-5.c @@ -0,0 +1,51 @@ +/* PR tree-optimization/106164 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-forwprop1-details" } */ + + +int f(int a) +{ + int c = a != 2; + int d = a >= 2; + return c & d; +} +int g(int b) +{ + int c = b != -1; + int d = b <= -1; + return c & d; +} + + +int g_(int e) +{ + int c = e != -2; + int d = e <= -2; + return c & d; +} + +int f1(int x) +{ + int c = x == 2; + int d = x <= 1; + return c | d; +} +int g1(int y) +{ + int c = y == -1; + int d = y > -1; + return c | d; +} +int g1_(int z) +{ + int c = z == -2; + int d = z >= -1; + return c | d; +} + +/* { dg-final { scan-tree-dump "gimple_simplified to _\[0-9\]+ = a_\[0-9\]+.D. > 2" "forwprop1" } } */ +/* { dg-final { scan-tree-dump "gimple_simplified to _\[0-9\]+ = b_\[0-9\]+.D. < -1" "forwprop1" } } */ +/* { dg-final { scan-tree-dump "gimple_simplified to _\[0-9\]+ = e_\[0-9\]+.D. < -2" "forwprop1" } } */ +/* { dg-final { scan-tree-dump "gimple_simplified to _\[0-9\]+ = x_\[0-9\]+.D. <= 2" "forwprop1" } } */ +/* { dg-final { scan-tree-dump "gimple_simplified to _\[0-9\]+ = y_\[0-9\]+.D. >= -1" "forwprop1" } } */ +/* { dg-final { scan-tree-dump "gimple_simplified to _\[0-9\]+ = z_\[0-9\]+.D. >= -2" "forwprop1" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-35.c b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-35.c new file mode 100644 index 00000000000..c52c64c5524 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/phi-opt-35.c @@ -0,0 +1,13 @@ +/* PR tree-optimization/106164 */ +/* { dg-do compile } */ +/* { dg-options "-O1 -fdump-tree-phiopt2" } */ + +_Bool f2(int a) +{ + if (a != 2) + return a > 1; + return 0; +} +/* phiopt2 should be able to optimize this to `a > 2` via match and simplify */ +/* { dg-final { scan-tree-dump "_\[0-9\]+ = a_\[0-9\]+.D. > 2" "phiopt2" } } */ +/* { dg-final { scan-tree-dump-not "a_\[0-9\]+.D. != 2" "phiopt2" } } */