From patchwork Fri Aug 11 09:15: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: 134377 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp957734vqi; Fri, 11 Aug 2023 02:17:12 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEUBlT2iYyLyWek2w5cVZ3n7IKcwIOLt00PPRsbpyqljWjavkfiBHZduLkBn0gTPgQtSM1D X-Received: by 2002:a05:6512:3682:b0:4f3:93d6:f969 with SMTP id d2-20020a056512368200b004f393d6f969mr799062lfs.59.1691745432671; Fri, 11 Aug 2023 02:17:12 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691745432; cv=none; d=google.com; s=arc-20160816; b=k/BGGAI5001lkD83YH3wXhjZYIiCtXWWzpB8urOKrILq9YA7x8bS0UKBcKmGH7w2oD +qgPMJ7RTf/sNczHl+/7YJAEJun77Vnj1eDalCBP9BsPRRGgz4jkrrsidXHJzrqOMSvB NlO4pj9qwoblRf9kUAbLV2sAtbiF+Pf61xzL5mbtnF8NECGvGeiDOfsM6pt201glO0wN PoZVtapkuNP63RTt5rCu5KEszDtrYw9VMBWBpo4IIiAcPu5z5LS2vDfG+0Es1gwT7ArV qLADq7iRWmvmQL7mohAAAZmBiH9tRElr6ugr8JhJxuyJRGba0wxUrO3C5rqxBDFEyPtG Wx0w== 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=WnMsyqEgXbimWEXkO/mo0Nb9OsHApshnYJ/xtoGQmJM=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=l6VXfuEeRF9WF4RoYZVGHVYdWJBZBCQU8oRmw04WO+QPrhE07yTam8lHmmky/kURTS mC2BhCi/cWW7bBC5xGEkOvJFmVKDF39iPg0AAakJ2w+hU40U8Gd5rRDbd88SL/PtapBA kGhPKc+RDFHbJRO67WGMdXgBmhjdAgZP2juY7GJqoO8SERt6apuiqovRLkdp1mvn8DYY PCYbZQyHyvzFi1N+B1HDcmhbpSl5nT7LV0SE90rOeYO5LiXSuA01dJDprWeMN0mMk1Dx wySeNHZ9HdXUhnbcKpwFE7JmmzfQv5JA2ZkoZtoGymvkgJZmt17d0FiKKMjcdHzK6HTs e1Gg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=TuFvVZGQ; 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 c21-20020a056402121500b005236a77eb1dsi3067174edw.104.2023.08.11.02.17.12 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 02:17:12 -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=TuFvVZGQ; 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 0EAB0385701E for ; Fri, 11 Aug 2023 09:16:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0EAB0385701E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691745417; bh=WnMsyqEgXbimWEXkO/mo0Nb9OsHApshnYJ/xtoGQmJM=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=TuFvVZGQrkSzZOZq97niL2j6hiKd7Isv3oBQzR3ZCJjegFwUfK6VSA+v6AaUyi2nL XGki7P0NOvrHx82Wr7qr/dPnj2iYNtmpsusxYsBWIv8IYG1W6BJjsYeraq9l2OZBY1 bODO5qppv7M6kXJkL9xdlsmrno896hP+zUMME8P8= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id A2C6F3858D20 for ; Fri, 11 Aug 2023 09:16:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A2C6F3858D20 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37AN2YaE011353 for ; Fri, 11 Aug 2023 02:16:02 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3sd8ya1j8y-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 11 Aug 2023 02:16:01 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 11 Aug 2023 02:15:59 -0700 Received: from maili.marvell.com (10.69.176.80) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server id 15.0.1497.48 via Frontend Transport; Fri, 11 Aug 2023 02:15:59 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 6D3343F706B; Fri, 11 Aug 2023 02:15:59 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 1/2] PHI-OPT [PR 110984]: Add support for NE_EXPR/EQ_EXPR with casts to spaceship_replacement Date: Fri, 11 Aug 2023 02:15:50 -0700 Message-ID: <20230811091551.2758227-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: PtuWAVLhbQ93PtBTupAlPnnr4X769qBW X-Proofpoint-GUID: PtuWAVLhbQ93PtBTupAlPnnr4X769qBW X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-10_20,2023-08-10_01,2023-05-22_02 X-Spam-Status: No, score=-13.6 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_DNSWL_LOW, SCC_10_SHORT_WORD_LINES, SCC_5_SHORT_WORD_LINES, 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.29 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: 1773923659077748547 X-GMAIL-MSGID: 1773923659077748547 So with my next VRP patch, VRP causes: ``` # c$_M_value_18 = PHI <-1(3), 0(2), 1(4)> _11 = (unsigned int) c$_M_value_18; _16 = _11 <= 1; ``` To be changed to: ``` # c$_M_value_18 = PHI <-1(3), 0(2), 1(4)> _11 = (unsigned int) c$_M_value_18; _16 = _11 != 4294967295; ``` So let's add support for the above. A few changes was needed, first to change the range check of the rhs of the comparison to possibly integer_all_onesp also. The next is to add support for the cast and EQ/NE case. Note on the testcases pr110984-1.c is basically pr94589-2.c but with what the C++ code is doing with the signed char type; pr110984-2.c is pr110984-1.c with the cast added to give an explicit testcase to test against. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. Thanks, Andrew Pinski PR tree-optimization/110984 gcc/ChangeLog: * tree-ssa-phiopt.cc (spaceship_replacement): Add support for NE/EQ for the cast case. gcc/testsuite/ChangeLog: * gcc.dg/pr110984-1.c: New test. * gcc.dg/pr110984-2.c: New test. --- gcc/testsuite/gcc.dg/pr110984-1.c | 37 +++++++++++++++++++++++++++++++ gcc/testsuite/gcc.dg/pr110984-2.c | 21 ++++++++++++++++++ gcc/tree-ssa-phiopt.cc | 19 +++++++++++++--- 3 files changed, 74 insertions(+), 3 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/pr110984-1.c create mode 100644 gcc/testsuite/gcc.dg/pr110984-2.c diff --git a/gcc/testsuite/gcc.dg/pr110984-1.c b/gcc/testsuite/gcc.dg/pr110984-1.c new file mode 100644 index 00000000000..85b19eb8279 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr110984-1.c @@ -0,0 +1,37 @@ +/* PR tree-optimization/110984 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -g0 -ffast-math -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "\[ij]_\[0-9]+\\(D\\) (?:<|<=|==|!=|>|>=) \[ij]_\[0-9]+\\(D\\)" 14 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "i_\[0-9]+\\(D\\) (?:<|<=|==|!=|>|>=) 5\\.0" 14 "optimized" } } */ + +/* This is similar to pr94589-2.c except use signed char as the type for the [-1,2] case */ + +#define A __attribute__((noipa)) +A int f1 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c == 0; } +A int f2 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c != 0; } +A int f3 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c > 0; } +A int f4 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c < 0; } +A int f5 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c >= 0; } +A int f6 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c <= 0; } +A int f7 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c == -1; } +A int f8 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c != -1; } +A int f9 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c > -1; } +A int f10 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c <= -1; } +A int f11 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c == 1; } +A int f12 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c != 1; } +A int f13 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c < 1; } +A int f14 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; return c >= 1; } +A int f15 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c == 0; } +A int f16 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c != 0; } +A int f17 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c > 0; } +A int f18 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c < 0; } +A int f19 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c >= 0; } +A int f20 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c <= 0; } +A int f21 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c == -1; } +A int f22 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c != -1; } +A int f23 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c > -1; } +A int f24 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c <= -1; } +A int f25 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c == 1; } +A int f26 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c != 1; } +A int f27 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c < 1; } +A int f28 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; return c >= 1; } diff --git a/gcc/testsuite/gcc.dg/pr110984-2.c b/gcc/testsuite/gcc.dg/pr110984-2.c new file mode 100644 index 00000000000..cddce045745 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr110984-2.c @@ -0,0 +1,21 @@ +/* PR tree-optimization/110984 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -g0 -ffast-math -fdump-tree-optimized" } */ +/* { dg-final { scan-tree-dump-times "\[ij]_\[0-9]+\\(D\\) (?:<|<=|==|!=|>|>=) \[ij]_\[0-9]+\\(D\\)" 6 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "i_\[0-9]+\\(D\\) (?:<|<=|==|!=|>|>=) 5\\.0" 6 "optimized" } } */ + +/* This is similar to pr94589-2.c except use signed char and use unsigned types to check against the variable, which means removing the non !=/== comparisons. */ + +#define A __attribute__((noipa)) +A int f1 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; unsigned t = c; return t == 0; } +A int f2 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; unsigned t = c; return t != 0; } +A int f7 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; unsigned t = c; return t == -1; } +A int f8 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; unsigned t = c; return t != -1; } +A int f11 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; unsigned t = c; return t == 1; } +A int f12 (double i, double j) { signed char c; if (i == j) c = 0; else if (i < j) c = -1; else if (i > j) c = 1; else c = 2; unsigned t = c; return t != 1; } +A int f15 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; unsigned t = c; return t == 0; } +A int f16 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; unsigned t = c; return t != 0; } +A int f21 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; unsigned t = c; return t == -1; } +A int f22 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; unsigned t = c; return t != -1; } +A int f25 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; unsigned t = c; return t == 1; } +A int f26 (double i) { signed char c; if (i == 5.0) c = 0; else if (i < 5.0) c = -1; else if (i > 5.0) c = 1; else c = 2; unsigned t = c; return t != 1; } diff --git a/gcc/tree-ssa-phiopt.cc b/gcc/tree-ssa-phiopt.cc index 485662dfcc7..5f24d62c49a 100644 --- a/gcc/tree-ssa-phiopt.cc +++ b/gcc/tree-ssa-phiopt.cc @@ -2346,7 +2346,8 @@ spaceship_replacement (basic_block cond_bb, basic_block middle_bb, } if (lhs != (orig_use_lhs ? orig_use_lhs : phires) || !tree_fits_shwi_p (rhs) - || !IN_RANGE (tree_to_shwi (rhs), -1, 1)) + || !(IN_RANGE (tree_to_shwi (rhs), 0, 1) + || integer_all_onesp (rhs))) return false; if (is_cast) @@ -2356,9 +2357,20 @@ spaceship_replacement (basic_block cond_bb, basic_block middle_bb, /* As for -ffast-math we assume the 2 return to be impossible, canonicalize (unsigned) res <= 1U or (unsigned) res < 2U into res >= 0 and (unsigned) res > 1U - or (unsigned) res >= 2U as res < 0. */ + or (unsigned) res >= 2U as res < 0. + Sometimes we get (unsigned)res != N. Support those cases too. */ switch (cmp) { + case NE_EXPR: + case EQ_EXPR: + { + tree newrhs = fold_convert (TREE_TYPE (phires), rhs); + tree tmp = fold_convert (TREE_TYPE (rhs), newrhs); + if (!tree_int_cst_equal (rhs, tmp)) + return false; + rhs = newrhs; + break; + } case LE_EXPR: if (!integer_onep (rhs)) return false; @@ -2382,7 +2394,8 @@ spaceship_replacement (basic_block cond_bb, basic_block middle_bb, default: return false; } - rhs = build_zero_cst (TREE_TYPE (phires)); + if (cmp != EQ_EXPR && cmp != NE_EXPR) + rhs = build_zero_cst (TREE_TYPE (phires)); } else if (orig_use_lhs) { From patchwork Fri Aug 11 09:15:51 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 134378 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:b824:0:b0:3f2:4152:657d with SMTP id z4csp957741vqi; Fri, 11 Aug 2023 02:17:13 -0700 (PDT) X-Google-Smtp-Source: AGHT+IGSyTnqidwSY10FI2iRWbYdflTsKGbXc1x9GBHb8iIuBdURGhg6DeW2U7OmTvLdxqbMWXNv X-Received: by 2002:a17:906:8a7c:b0:992:a85d:278b with SMTP id hy28-20020a1709068a7c00b00992a85d278bmr1221714ejc.59.1691745433668; Fri, 11 Aug 2023 02:17:13 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691745433; cv=none; d=google.com; s=arc-20160816; b=QQ5UAqvoS9C9dzY6VSQ9vNkoStMK6O+l0sXdxlG2UIpT8JV2//05XQje2/lufGNavD zJRA3N58merWZLKzsSTSG/Jo/S7F6uMvxSRevIX5CckO4cIj1Gejb/f9BTM2vdAL6p02 vlWdrvXlo4dIoDyTSNdliwttNivtluo08MioH1tDjX9EFLEru2B2dhDTtiwn10dv7pRY 1rD81PwcSaYS3JgVDe3c7ws3+JvhjFpI6OVGApZvF63/LNltWlFOU3v6NJbg2s/SLAS5 z13F2HKywewZybemOEk8YbE6HE20zH3sib1ZWgb9icISok4uWh7xkHu0XxQRvo3dj4Ry t/lQ== 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:references:in-reply-to :message-id:date:subject:cc:to:dmarc-filter:delivered-to :dkim-signature:dkim-filter; bh=oKyVP68ezK5sLwtR+Ou9QxBCMAC0SLINA6Ex8FacSm4=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=trUMJzX9nd3Jt4elf5JYMg+cxeRZNc0JOkn2z4umKb4dokVg91jhg4loAgeOJxaRl/ aEhFMtMTUYcsCUwiYsxjVRfhryrI6Q8bs+5c5WNqUasS5/QlvfzQH+V7EeQxsBlzI6XK sVuGnQ0/3R9PefgxH3u+QIG+T0WDEi2uJsiLJo6wv0l0w/AvvJ7UWVZXsqot+WVj7+vd tUqa7+gQFofYsmfF6+WpQl1zx92YlIwt3dt6rSdQHut6UM2DgdvIkwPuDseHE/TZKcLV avuK4evKxT0wHGtegFZ7ReWvtxN7C5es7JDlJeXT39XM0hQ1YaG2NONXizmtC9CZYdue Xlmg== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=uPHUBMab; 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 dc22-20020a170906c7d600b00997e71d037dsi2950162ejb.624.2023.08.11.02.17.13 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 11 Aug 2023 02:17:13 -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=uPHUBMab; 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 9BBF5385694E for ; Fri, 11 Aug 2023 09:16:57 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 9BBF5385694E DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691745417; bh=oKyVP68ezK5sLwtR+Ou9QxBCMAC0SLINA6Ex8FacSm4=; h=To:CC:Subject:Date:In-Reply-To:References:List-Id: List-Unsubscribe:List-Archive:List-Post:List-Help:List-Subscribe: From:Reply-To:From; b=uPHUBMabkyYpMqPRlv5FiiIkhZ448WX/JZw9qNDU9KU/Nqk7txj9mfsSoUuxzAil6 xq21d7TRbO6tS8e9UbLHS8yahkyVDGAD73uk943V0D6y2zru0fDCSkNhgCZbICP9Gt kgSeKKMXO+1CpBoZHcbkhP0uBl6YjIQotl3KOxUE= X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from mx0b-0016f401.pphosted.com (mx0b-0016f401.pphosted.com [67.231.156.173]) by sourceware.org (Postfix) with ESMTPS id 00B4B3858D28 for ; Fri, 11 Aug 2023 09:16:02 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 00B4B3858D28 Received: from pps.filterd (m0045851.ppops.net [127.0.0.1]) by mx0b-0016f401.pphosted.com (8.17.1.19/8.17.1.19) with ESMTP id 37AN2YaF011353 for ; Fri, 11 Aug 2023 02:16:02 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3sd8ya1j8y-2 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 11 Aug 2023 02:16:02 -0700 Received: from DC5-EXCH02.marvell.com (10.69.176.39) by DC5-EXCH02.marvell.com (10.69.176.39) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 11 Aug 2023 02:16:00 -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; Fri, 11 Aug 2023 02:16:00 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id D361A3F706A; Fri, 11 Aug 2023 02:15:59 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler Date: Fri, 11 Aug 2023 02:15:51 -0700 Message-ID: <20230811091551.2758227-2-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230811091551.2758227-1-apinski@marvell.com> References: <20230811091551.2758227-1-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: ZjuOf1qoiLvBEnqeLjIt-29CCFZ-VelQ X-Proofpoint-GUID: ZjuOf1qoiLvBEnqeLjIt-29CCFZ-VelQ X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.267,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-08-10_20,2023-08-10_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.29 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: 1773923660171329577 X-GMAIL-MSGID: 1773923660171329577 So it turns out there was a simplier way of starting to improve VRP to start to fix PR 110131, PR 108360, and PR 108397. That was rewrite test_for_singularity to use range_op_handler and Value_Range. This patch implements that and OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * vr-values.cc (test_for_singularity): Add edge argument and rewrite using range_op_handler. (simplify_compare_using_range_pairs): Use Value_Range instead of value_range and update test_for_singularity call. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/tree-ssa/vrp125.c: New test. --- gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 +++++++++++++ gcc/testsuite/gcc.dg/tree-ssa/vrp125.c | 44 +++++++++++++ gcc/vr-values.cc | 91 ++++++++------------------ 3 files changed, 114 insertions(+), 65 deletions(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp125.c diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c new file mode 100644 index 00000000000..6ccbda35d1b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp124.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a >= 0) + ; + else + return 0; + return a < 0; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a > 100) + ; + else + return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a > 100) + ; + else + return 0; + return a < 100; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 100) + ; + else + return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c new file mode 100644 index 00000000000..f6c2f8e35f1 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/vrp125.c @@ -0,0 +1,44 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +/* Should be optimized to a == -100 */ +int g(int a) +{ + if (a == -100 || a == -50 || a >= 0) + ; + else + return 0; + return a < -50; +} + +/* Should optimize to a == 0 */ +int f(int a) +{ + if (a == 0 || a == 50 || a > 100) + ; + else + return 0; + return a < 50; +} + +/* Should be optimized to a == 0. */ +int f2(int a) +{ + if (a == 0 || a == 50 || a > 100) + ; + else + return 0; + return a < 25; +} + +/* Should optimize to a == 100 */ +int f1(int a) +{ + if (a < 0 || a == 50 || a == 100) + ; + else + return 0; + return a > 50; +} + +/* { dg-final { scan-tree-dump-not "goto " "optimized" } } */ diff --git a/gcc/vr-values.cc b/gcc/vr-values.cc index a4fddd62841..7004b0224bd 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -907,66 +907,30 @@ simplify_using_ranges::simplify_bit_ops_using_ranges a known value range VR. If there is one and only one value which will satisfy the - conditional, then return that value. Else return NULL. - - If signed overflow must be undefined for the value to satisfy - the conditional, then set *STRICT_OVERFLOW_P to true. */ + conditional on the EDGE, then return that value. + Else return NULL. */ static tree test_for_singularity (enum tree_code cond_code, tree op0, - tree op1, const value_range *vr) + tree op1, Value_Range vr, bool edge) { - tree min = NULL; - tree max = NULL; - - /* Extract minimum/maximum values which satisfy the conditional as it was - written. */ - if (cond_code == LE_EXPR || cond_code == LT_EXPR) + /* This is already a singularity. */ + if (cond_code == NE_EXPR || cond_code == EQ_EXPR) + return NULL; + auto range_op = range_op_handler (cond_code); + int_range<2> op1_range (TREE_TYPE (op0)); + wide_int w = wi::to_wide (op1); + op1_range.set (TREE_TYPE (op1), w, w); + Value_Range vr1(TREE_TYPE (op0)); + if (range_op.op1_range (vr1, TREE_TYPE (op0), + edge ? range_true () : range_false (), + op1_range)) { - min = TYPE_MIN_VALUE (TREE_TYPE (op0)); - - max = op1; - if (cond_code == LT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op0), 1); - max = fold_build2 (MINUS_EXPR, TREE_TYPE (op0), max, one); - /* Signal to compare_values_warnv this expr doesn't overflow. */ - if (EXPR_P (max)) - suppress_warning (max, OPT_Woverflow); - } - } - else if (cond_code == GE_EXPR || cond_code == GT_EXPR) - { - max = TYPE_MAX_VALUE (TREE_TYPE (op0)); - - min = op1; - if (cond_code == GT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op0), 1); - min = fold_build2 (PLUS_EXPR, TREE_TYPE (op0), min, one); - /* Signal to compare_values_warnv this expr doesn't overflow. */ - if (EXPR_P (min)) - suppress_warning (min, OPT_Woverflow); - } - } - - /* Now refine the minimum and maximum values using any - value range information we have for op0. */ - if (min && max) - { - tree type = TREE_TYPE (op0); - tree tmin = wide_int_to_tree (type, vr->lower_bound ()); - tree tmax = wide_int_to_tree (type, vr->upper_bound ()); - if (compare_values (tmin, min) == 1) - min = tmin; - if (compare_values (tmax, max) == -1) - max = tmax; - - /* If the new min/max values have converged to a single value, - then there is only one value which can satisfy the condition, - return that value. */ - if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min)) - return min; + vr.intersect (vr1); + tree newop1; + /* If the updated range is just a singleton, then we can just do a comparison */ + if (vr.singleton_p (&newop1)) + return newop1; } return NULL; } @@ -1224,9 +1188,9 @@ simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tr && cond_code != EQ_EXPR && TREE_CODE (op0) == SSA_NAME && INTEGRAL_TYPE_P (TREE_TYPE (op0)) - && is_gimple_min_invariant (op1)) + && TREE_CODE (op1) == INTEGER_CST) { - value_range vr; + Value_Range vr (TREE_TYPE (op0)); if (!query->range_of_expr (vr, op0, stmt)) vr.set_undefined (); @@ -1235,20 +1199,17 @@ simplify_using_ranges::simplify_compare_using_ranges_1 (tree_code &cond_code, tr able to simplify this conditional. */ if (!vr.undefined_p () && !vr.varying_p ()) { - tree new_tree = test_for_singularity (cond_code, op0, op1, &vr); + tree new_tree = test_for_singularity (cond_code, op0, op1, vr, + true); if (new_tree) { cond_code = EQ_EXPR; op1 = new_tree; happened = true; } - - /* Try again after inverting the condition. We only deal - with integral types here, so no need to worry about - issues with inverting FP comparisons. */ - new_tree = test_for_singularity - (invert_tree_comparison (cond_code, false), - op0, op1, &vr); + /* Try again after inverting the condition. */ + new_tree = test_for_singularity (cond_code, op0, op1, vr, + false); if (new_tree) { cond_code = NE_EXPR;