From patchwork Tue Sep 12 15:25:41 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 138256 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:9ecd:0:b0:3f2:4152:657d with SMTP id t13csp489389vqx; Tue, 12 Sep 2023 08:32:05 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHLmNGUhpj4YOuw6sN9Jzg+gpyf3GGgEzq9mAO9nauBAJ9Eq3PBKHzeVTbFhf2pzc0k+EL2 X-Received: by 2002:aa7:c999:0:b0:529:fa63:ef80 with SMTP id c25-20020aa7c999000000b00529fa63ef80mr10131534edt.28.1694532725035; Tue, 12 Sep 2023 08:32:05 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1694532725; cv=none; d=google.com; s=arc-20160816; b=Ha1obkCSZC1ZqVR0liLjNZsRJYbgIdvQhfrJWzte4pqR5ZxymMaLvhh8gi+vsVuawp 9xLg8owpTSjfoaCoqNrCiXE77iiRTIuANem79mjsb0FILpC8hhyaMgNotqm7JS4TB9Dx yPEYG2HIIx8D453oBY3NFsO+83A9gJn1fcXNQ6Em9mggGBjPoHw0qMGubHeu4VQcND4Q RqidUUqgCLaURH1eloZbOG0cMvRHuyYCn5c2Y/MOjLvUKAMwiqceUdhsKjvyMKp8XuVI cC0CJzvKlozc9gwtnLcDyBhQW/mZ3UAWdVAz9QK/jE2x1JgEQCOLLaD0S9hEcOoPKKEw f92w== 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=DPlGTXj2ZkFRKTpiwJ93UTMU2UQRictCijx8HnRSC6Q=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=K/nkbFKD6qOF6KyOKttrXuFxpxW/cTp7wXx5nrsqksUhD4BrnXPBCPZvmoRcLwuN8M zTKeANrSgNoRjImRhp83ZJedMtxS+l/J1BZm6wUxLT4h3T8/r4FYbq3JH9bRUDPgt5Gk Al7NuGLdagI+tllkqVEdpHf06Zv6nso3MSsago7ajVNoIxLBRSXmIV8VxY8p/vF3+esC SLkAD5P+WQSJGZkafhpMYQuf4UwszUdBRx5H9avv3tvyQ+/fQkxxnxy9ObQjQwx56XGu MXH28FSfE2w1qW5iLA+V7I4q7ixZ+GslbAxYvCU4FHZX+HpW6UfDJxweHCcRrb1FuYki 1r1Q== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Bn+w+AkA; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id j19-20020aa7ca53000000b00523b8072776si8686656edt.121.2023.09.12.08.32.04 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 12 Sep 2023 08:32:05 -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=@gcc.gnu.org header.s=default header.b=Bn+w+AkA; 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=NONE sp=NONE dis=NONE) header.from=gnu.org Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id A221B38983AA for ; Tue, 12 Sep 2023 15:28:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org A221B38983AA DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1694532485; bh=DPlGTXj2ZkFRKTpiwJ93UTMU2UQRictCijx8HnRSC6Q=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Bn+w+AkArRjibNUAHuiS4nyyAycZbPiQIrExkSsLSiJPOu7+KApZo01z2EqPs+qLj yXUCR0BlCK84gi1CTrYXfhyowNmdjLjwltOMQsGkTxIiK+Ao7QtbmdKGTJJarzWB46 jZlQpT19HuXRZfi0+pzdAA8PJjZgWty/3FQncnyg= 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 AB2A538515CC for ; Tue, 12 Sep 2023 15:25:58 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org AB2A538515CC 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 38C7K8ER031999 for ; Tue, 12 Sep 2023 08:25:57 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3t2kgesv4t-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Tue, 12 Sep 2023 08:25:57 -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; Tue, 12 Sep 2023 08:25:55 -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; Tue, 12 Sep 2023 08:25:55 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 13B753F70E5; Tue, 12 Sep 2023 08:25:54 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 1/2] MATCH: [PR111364] Add some more minmax cmp operand simplifications Date: Tue, 12 Sep 2023 08:25:41 -0700 Message-ID: <20230912152542.1440800-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: DiMU54tc1FK-ap_nXLN7aPGNiQJokgKI X-Proofpoint-ORIG-GUID: DiMU54tc1FK-ap_nXLN7aPGNiQJokgKI X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.601,FMLib:17.11.176.26 definitions=2023-09-12_14,2023-09-05_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: 1776846346684810169 X-GMAIL-MSGID: 1776846346684810169 This adds a few more minmax cmp operand simplifications which were missed before. `MIN(a,b) < a` -> `a > b` `MIN(a,b) >= a` -> `a <= b` `MAX(a,b) > a` -> `a < b` `MAX(a,b) <= a` -> `a >= b` OK? Bootstrapped and tested on x86_64-linux-gnu. Note gcc.dg/pr96708-negative.c needed to updated to remove the check for MIN/MAX as they have been optimized (correctly) away. PR tree-optimization/111364 gcc/ChangeLog: * match.pd (`MIN (X, Y) == X`): Extend to min/lt, min/ge, max/gt, max/le. gcc/testsuite/ChangeLog: * gcc.c-torture/execute/minmaxcmp-1.c: New test. * gcc.dg/tree-ssa/minmaxcmp-2.c: New test. * gcc.dg/pr96708-negative.c: Update testcase. * gcc.dg/pr96708-positive.c: Add comment about `return 0`. --- gcc/match.pd | 8 +-- .../gcc.c-torture/execute/minmaxcmp-1.c | 51 +++++++++++++++++++ gcc/testsuite/gcc.dg/pr96708-negative.c | 4 +- gcc/testsuite/gcc.dg/pr96708-positive.c | 1 + gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c | 30 +++++++++++ 5 files changed, 89 insertions(+), 5 deletions(-) create mode 100644 gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c diff --git a/gcc/match.pd b/gcc/match.pd index 51985c1bad4..36e3da4841b 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -3902,9 +3902,11 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (maxmin @0 (bit_not @1)))) /* MIN (X, Y) == X -> X <= Y */ -(for minmax (min min max max) - cmp (eq ne eq ne ) - out (le gt ge lt ) +/* MIN (X, Y) < X -> X > Y */ +/* MIN (X, Y) >= X -> X <= Y */ +(for minmax (min min min min max max max max) + cmp (eq ne lt ge eq ne gt le ) + out (le gt gt le ge lt lt ge ) (simplify (cmp:c (minmax:c @0 @1) @0) (if (ANY_INTEGRAL_TYPE_P (TREE_TYPE (@0))) diff --git a/gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c b/gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c new file mode 100644 index 00000000000..6705a053768 --- /dev/null +++ b/gcc/testsuite/gcc.c-torture/execute/minmaxcmp-1.c @@ -0,0 +1,51 @@ +#define func(vol, op1, op2) \ +_Bool op1##_##op2##_##vol (int a, int b) \ +{ \ + vol int x = op_##op1(a, b); \ + return op_##op2(x, a); \ +} + +#define op_lt(a, b) ((a) < (b)) +#define op_le(a, b) ((a) <= (b)) +#define op_eq(a, b) ((a) == (b)) +#define op_ne(a, b) ((a) != (b)) +#define op_gt(a, b) ((a) > (b)) +#define op_ge(a, b) ((a) >= (b)) +#define op_min(a, b) ((a) < (b) ? (a) : (b)) +#define op_max(a, b) ((a) > (b) ? (a) : (b)) + + +#define funcs(a) \ + a(min,lt) \ + a(max,lt) \ + a(min,gt) \ + a(max,gt) \ + a(min,le) \ + a(max,le) \ + a(min,ge) \ + a(max,ge) \ + a(min,ne) \ + a(max,ne) \ + a(min,eq) \ + a(max,eq) + +#define funcs1(a,b) \ +func(,a,b) \ +func(volatile,a,b) + +funcs(funcs1) + +#define test(op1,op2) \ +do { \ + if (op1##_##op2##_(x,y) != op1##_##op2##_volatile(x,y)) \ + __builtin_abort(); \ +} while(0); + +int main() +{ + for(int x = -10; x < 10; x++) + for(int y = -10; y < 10; y++) + { + funcs(test) + } +} diff --git a/gcc/testsuite/gcc.dg/pr96708-negative.c b/gcc/testsuite/gcc.dg/pr96708-negative.c index 91964d3b971..c9c1aa85558 100644 --- a/gcc/testsuite/gcc.dg/pr96708-negative.c +++ b/gcc/testsuite/gcc.dg/pr96708-negative.c @@ -42,7 +42,7 @@ int main() return 0; } -/* { dg-final { scan-tree-dump-times "MAX_EXPR" 2 "optimized" } } */ -/* { dg-final { scan-tree-dump-times "MIN_EXPR" 2 "optimized" } } */ +/* Even though test[1-4] originally has MIN/MAX, those can be optimized away + into just comparing a and b arguments. */ /* { dg-final { scan-tree-dump-times "return 0;" 1 "optimized" } } */ /* { dg-final { scan-tree-dump-not { "return 1;" } "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/pr96708-positive.c b/gcc/testsuite/gcc.dg/pr96708-positive.c index 65af85344b6..12c5fedfd30 100644 --- a/gcc/testsuite/gcc.dg/pr96708-positive.c +++ b/gcc/testsuite/gcc.dg/pr96708-positive.c @@ -42,6 +42,7 @@ int main() return 0; } +/* Note main has one `return 0`. */ /* { dg-final { scan-tree-dump-times "return 0;" 3 "optimized" } } */ /* { dg-final { scan-tree-dump-times "return 1;" 2 "optimized" } } */ /* { dg-final { scan-tree-dump-not { "MAX_EXPR" } "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c b/gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c new file mode 100644 index 00000000000..f64a9253cfb --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/minmaxcmp-2.c @@ -0,0 +1,30 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-original" } */ +/* PR tree-optimization/111364 */ + +#define min1(a, b) ((a) < (b) ? (a) : (b)) +#define max1(a, b) ((a) > (b) ? (a) : (b)) + +int minlt(int a, int b) +{ + return min1(a, b) < a; // b < a or a > b +} +/* { dg-final { scan-tree-dump "return a > b;|return b < a;" "original" } } */ + +int minge(int c, int d) +{ + return min1(c, d) >= c; // d >= c or c <= d +} +/* { dg-final { scan-tree-dump "return c <= d;|return d <= c;" "original" } } */ + +int maxgt(int e, int f) +{ + return max1(e, f) > e; // e > f or f < e +} +/* { dg-final { scan-tree-dump "return e < f;|return f > e;" "original" } } */ + +int maxle(int x, int y) +{ + return max1(x, y) <= x; // y <= x or x >= y +} +/* { dg-final { scan-tree-dump "return x >= y;|return y <= x;" "original" } } */