From patchwork Fri Sep 1 17:30:59 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 137406 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c792:0:b0:3f2:4152:657d with SMTP id b18csp1039114vqu; Fri, 1 Sep 2023 10:32:17 -0700 (PDT) X-Google-Smtp-Source: AGHT+IHYbbddibHZxyiWWK5aEMaNkLT8grzn5xc7XU6bj5ZCW2r6j0vgmvO06b6GNt/3ffhtK+lk X-Received: by 2002:a17:906:ef8b:b0:9a1:bd82:de1c with SMTP id ze11-20020a170906ef8b00b009a1bd82de1cmr2341476ejb.41.1693589537012; Fri, 01 Sep 2023 10:32:17 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1693589537; cv=none; d=google.com; s=arc-20160816; b=mIsRkoD86K6aQLiBUoz3ZD11NPoOAZu1E/M+fa0wE9LG8gu/DBvr+Y+g5EA6A36XNO s4SYskkHs+dX5LkW98/mSXP/9qHmj68jtP4wgguHSbAVwZeeGELm7UQ/vb7HlLNvRzzZ qPCTVPc5m+53qc6nIZHms1x1HCL4/ulYT5uRZRCOgV8Iz2lIcMcw15KRsLyrRDiE/bVC b7lP0sPWGw1/5cSW0VsZaD9JVFqcizA62THKWKgjdjtdtou3rD4IQtCzhigE4MSp4ihd 4E8WkDCrxLuldQxxDErErcZDr0VywZUpEaA18+GgTOieK2bQQH9FiqqVRrubCsxz3JS8 xndw== 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=TQl05vka3S3d+FcgFGJnXqju/F8IwrqoCK6toEH5eoc=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=gBkoLKg7pVJJagjrVWZhrtdtQR1ny/6HZiWfyx+CKa6fT44Um2536BUJQukAq/Rw6P XCEDnFVmVK8vzI5Rqcy2suQY0wy6/22fK9vTyYtEkABRxFthuRSiKXIAe09IaJME1mqZ IruBgxNdseHjyE7WUxnWLHIxOOR+tlX4zxqJVrxIYFRe92IlE3Cv3Tio57SQwAbbQyfC Svb0atfOYuoBubGBUvXHEj/XztU4PrVmeY4pdoIAn8q6uFvF3b9rufpy9OZm0rwWKpN5 JOgzEssqyQAFauBQl7VmPo+B9CH0YKwO8wxhretDnEYUhgM9NPQfRfMaDrdbZc9+ASpq fhMA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=kwd5PpBu; 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 o21-20020a1709062e9500b0099dff3bc40dsi2856968eji.726.2023.09.01.10.32.16 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 01 Sep 2023 10:32:16 -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=kwd5PpBu; 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 3AEC33857700 for ; Fri, 1 Sep 2023 17:32:05 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 3AEC33857700 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1693589525; bh=TQl05vka3S3d+FcgFGJnXqju/F8IwrqoCK6toEH5eoc=; 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=kwd5PpBuITITU4VHv77wSs+qmSAOVniVyBjt88uBNpg3sPO20z8pBDSWPweyJs1Js JR4+thDpt/H0Z2qYU6+GwBc0DHGRKlmaMF5Tb2WtLbnOosCrZZz6OpzQJ8YDrE5CmX wj104iO08Z0dktpvZxzOk6Mj/+hUwreymUUeHsKY= 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 528423858D37 for ; Fri, 1 Sep 2023 17:31:11 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 528423858D37 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 3818ZhD2021766 for ; Fri, 1 Sep 2023 10:31:11 -0700 Received: from dc5-exch01.marvell.com ([199.233.59.181]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3sucjvsmbx-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Fri, 01 Sep 2023 10:31:10 -0700 Received: from DC5-EXCH01.marvell.com (10.69.176.38) by DC5-EXCH01.marvell.com (10.69.176.38) with Microsoft SMTP Server (TLS) id 15.0.1497.48; Fri, 1 Sep 2023 10:31:08 -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, 1 Sep 2023 10:31:08 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 4BF1A3F7084; Fri, 1 Sep 2023 10:31:08 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 2/2] VR-VALUES: Rewrite test_for_singularity using range_op_handler Date: Fri, 1 Sep 2023 10:30:59 -0700 Message-ID: <20230901173059.791894-2-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230901173059.791894-1-apinski@marvell.com> References: <20230901173059.791894-1-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-GUID: CrNlvQU8K1DITJuMTHD6yu7eWCb-lvbY X-Proofpoint-ORIG-GUID: CrNlvQU8K1DITJuMTHD6yu7eWCb-lvbY 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-01_14,2023-08-31_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: 1775857342160010303 X-GMAIL-MSGID: 1775857342160010303 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 | 99 ++++++++------------------ 3 files changed, 117 insertions(+), 70 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 52ab4fe6109..2474e57ee90 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -904,69 +904,33 @@ simplify_using_ranges::simplify_bit_ops_using_ranges } /* We are comparing trees OP1 and OP2 using COND_CODE. OP1 has - a known value range VR. + a known value range OP1_RANGE. 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 op1, - tree op2, const value_range *vr) + tree op2, const int_range_max &op1_range, 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); + wide_int w = wi::to_wide (op2); + int_range<1> op2_range (TREE_TYPE (op2), w, w); + int_range_max vr; + if (range_op.op1_range (vr, TREE_TYPE (op1), + edge ? range_true () : range_false (), + op2_range)) { - min = TYPE_MIN_VALUE (TREE_TYPE (op1)); - - max = op2; - if (cond_code == LT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op1), 1); - max = fold_build2 (MINUS_EXPR, TREE_TYPE (op1), 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 (op1)); - - min = op2; - if (cond_code == GT_EXPR) - { - tree one = build_int_cst (TREE_TYPE (op1), 1); - min = fold_build2 (PLUS_EXPR, TREE_TYPE (op1), 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 op1. */ - if (min && max) - { - tree type = TREE_TYPE (op1); - 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; + int_range_max new_range = op1_range; + new_range.intersect (vr); + tree newop2; + /* If the updated range is just a singleton, then we can just do a comparison */ + if (new_range.singleton_p (&newop2)) + return newop2; } return NULL; } @@ -1224,31 +1188,26 @@ 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; - - if (!query->range_of_expr (vr, op0, stmt)) - vr.set_undefined (); + int_range_max vr; /* If we have range information for OP0, then we might be able to simplify this conditional. */ - if (!vr.undefined_p () && !vr.varying_p ()) + if (query->range_of_expr (vr, op0, stmt) + && !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;