From patchwork Wed Aug 9 16:13:40 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 133329 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:c44e:0:b0:3f2:4152:657d with SMTP id w14csp2907566vqr; Wed, 9 Aug 2023 09:16:09 -0700 (PDT) X-Google-Smtp-Source: AGHT+IEQVYaHMGWteyNbWEhwt503J0/1JOrcfjc+nUIKcG+b11poeDbp0JfJNu9X5rsxfgnUsVPz X-Received: by 2002:a17:907:2c67:b0:99c:47a:8bd3 with SMTP id ib7-20020a1709072c6700b0099c047a8bd3mr2473471ejc.10.1691597769211; Wed, 09 Aug 2023 09:16:09 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1691597769; cv=none; d=google.com; s=arc-20160816; b=xa2EAHcAe23qPjpDYboC/lF3euHhjCyTkH4WRJ8FiUliPS2nTRhCwyXIzIYV6azB25 vfR4kpRvJV1HHnNLB0dY89jaGQB7UgKuBg5gqSHJ6h22gsyC+x3zaoYO0BUwG9vCPE5P s4jGkkkPKMSIDpkq2lFhV2Qo5MLrgSJAmlp0WzERFK783AYugeVrNMAKGa7nIx+szdlP kvgopf5KLKtrYl2B2d4cAf8Py0LfLnw+KU35Q1UErNGG8t2chG1iese7C2HblIccIKlg TuJfzqK2XmDDJomTlkkYvpcWKmPViZ65+rEA+cShzucgxqd6sEq8NP38GdfZHmsxjXwj fPrQ== 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=ZdyvdYs4kuK6MAqbJl7N/c6pEOMru6x0VuzeU2cGxMY=; fh=09okz+w4tVyhQmqNBttR063P0zKcIsLE1JRBioNPGC4=; b=jjzxvP81VzPUfl7B7q3qT66Y+jXu4IeYzXUdL6cJYsm5PFRQI3SNyJr+3lKNcp/Ve9 6HxEAN3tfE8dVFkkz/dqDR/IslmCBRrfcxVTtkebOvUAeHrxPMHwhfemCDkVuLmtrWYf AgCd4Aq1CNqzMUpdeRM1vkqSQo6PAYbe7rFLAW5WsnoaGFf1vjqAOSiqGdLA6V7nceS1 O2hxSKVFD07ugPKg+V7kL6uX+/rPBjbvg4srijtaGdy2aSbhoOcoBWGcDEI1ysgch2TZ 3x7u18VyiPTC4l3LYrTntsDUelpAgiqvCQim9i2JofuySPVM99JpvUXWjPIPism0X4C3 KVgw== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=Unu6OAFp; 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 bi1-20020a170906a24100b0099bd7b2663dsi3549080ejb.142.2023.08.09.09.16.08 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Wed, 09 Aug 2023 09:16:09 -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=Unu6OAFp; 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 0B3B33857806 for ; Wed, 9 Aug 2023 16:16:01 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0B3B33857806 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1691597761; bh=ZdyvdYs4kuK6MAqbJl7N/c6pEOMru6x0VuzeU2cGxMY=; h=To:CC:Subject:Date:List-Id:List-Unsubscribe:List-Archive: List-Post:List-Help:List-Subscribe:From:Reply-To:From; b=Unu6OAFpByZr1m4Y0NI8WdUxXJF/32nNagjKiVTyac5tnQLywK4NHrMrgGvoFqUQP VCX9v13YPTsJB0KyFPMVdP4gS6S9ePHO9wwzrWgkcIFBgI1Ac164DNP5SQ0xbsbj/6 vGDE+cfhUO/Nf6spAfZkvlXPjLjIo5kcBCqSgm30= 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 6F0403857C41 for ; Wed, 9 Aug 2023 16:13:59 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 6F0403857C41 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 3796626h023135 for ; Wed, 9 Aug 2023 09:13:58 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0a-0016f401.pphosted.com (PPS) with ESMTPS id 3sc57sj1yr-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Wed, 09 Aug 2023 09:13:58 -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; Wed, 9 Aug 2023 09:13:56 -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; Wed, 9 Aug 2023 09:13:56 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id 3889C3F704D; Wed, 9 Aug 2023 09:13:56 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH] VR-VALUES: Simplify comparison using range pairs Date: Wed, 9 Aug 2023 09:13:40 -0700 Message-ID: <20230809161340.2659544-1-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 MIME-Version: 1.0 X-Proofpoint-GUID: jD6lWRSK0Uouzm-ro46Gfo9SDTkJi2kz X-Proofpoint-ORIG-GUID: jD6lWRSK0Uouzm-ro46Gfo9SDTkJi2kz 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-09_13,2023-08-09_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: 1773768822381022035 X-GMAIL-MSGID: 1773768822381022035 If `A` has a range of `[0,0][100,INF]` and the comparison of `A < 50`. This should be optimized to `A <= 0` (which then will be optimized to just `A == 0`). This patch implement this via a new function which sees if the constant of a comparison is in the middle of 2 range pairs and change the constant to the either upper bound of the first pair or the lower bound of the second pair depending on the comparison. This is the first step in fixing the following PRS: PR 110131, PR 108360, and PR 108397. OK? Bootstrapped and tested on x86_64-linux-gnu with no regressions. gcc/ChangeLog: * vr-values.cc (simplify_compare_using_range_pairs): New function. (simplify_using_ranges::simplify_compare_using_ranges_1): Call it. gcc/testsuite/ChangeLog: * gcc.dg/tree-ssa/vrp124.c: New test. * gcc.dg/pr21643.c: Disable VRP. --- gcc/testsuite/gcc.dg/pr21643.c | 6 ++- gcc/testsuite/gcc.dg/tree-ssa/vrp124.c | 44 +++++++++++++++++ gcc/vr-values.cc | 65 ++++++++++++++++++++++++++ 3 files changed, 114 insertions(+), 1 deletion(-) create mode 100644 gcc/testsuite/gcc.dg/tree-ssa/vrp124.c diff --git a/gcc/testsuite/gcc.dg/pr21643.c b/gcc/testsuite/gcc.dg/pr21643.c index 4e7f93d351a..7f121d7006f 100644 --- a/gcc/testsuite/gcc.dg/pr21643.c +++ b/gcc/testsuite/gcc.dg/pr21643.c @@ -1,6 +1,10 @@ /* PR tree-optimization/21643 */ /* { dg-do compile } */ -/* { dg-options "-O2 -fdump-tree-reassoc1-details --param logical-op-non-short-circuit=1" } */ +/* Note VRP is able to transform `c >= 0x20` in f7 + to `c >= 0x21` since we want to test + reassociation and not VRP, turn it off. */ + +/* { dg-options "-O2 -fdump-tree-reassoc1-details --param logical-op-non-short-circuit=1 -fno-tree-vrp" } */ int f1 (unsigned char 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/vr-values.cc b/gcc/vr-values.cc index a4fddd62841..1262e7cf9f0 100644 --- a/gcc/vr-values.cc +++ b/gcc/vr-values.cc @@ -968,9 +968,72 @@ test_for_singularity (enum tree_code cond_code, tree op0, if (operand_equal_p (min, max, 0) && is_gimple_min_invariant (min)) return min; } + return NULL; } +/* Simplify integer comparisons such that the constant is one of the range pairs. + For an example, + A has a range of [0,0][100,INF] + and the comparison of `A < 50`. + This should be optimized to `A <= 0` + and then test_for_singularity can optimize it to `A == 0`. */ + +static bool +simplify_compare_using_range_pairs (tree_code &cond_code, tree &op0, tree &op1, + const value_range *vr) +{ + if (TREE_CODE (op1) != INTEGER_CST + || vr->num_pairs () < 2) + return false; + auto val_op1 = wi::to_wide (op1); + tree type = TREE_TYPE (op0); + auto sign = TYPE_SIGN (type); + auto p = vr->num_pairs (); + /* Find the value range pair where op1 + is in the middle of if one exist. */ + for (unsigned i = 1; i < p; i++) + { + auto lower = vr->upper_bound (i - 1); + auto upper = vr->lower_bound (i); + if (wi::lt_p (val_op1, lower, sign)) + continue; + if (wi::gt_p (val_op1, upper, sign)) + continue; + if (cond_code == LT_EXPR + && val_op1 != lower) + { + op1 = wide_int_to_tree (type, lower); + cond_code = LE_EXPR; + return true; + } + if (cond_code == LE_EXPR + && val_op1 != upper + && val_op1 != lower) + { + op1 = wide_int_to_tree (type, lower); + cond_code = LE_EXPR; + return true; + } + if (cond_code == GT_EXPR + && val_op1 != upper) + { + op1 = wide_int_to_tree (type, upper); + cond_code = GE_EXPR; + return true; + } + if (cond_code == GE_EXPR + && val_op1 != lower + && val_op1 != upper) + { + op1 = wide_int_to_tree (type, upper); + cond_code = GE_EXPR; + return true; + } + } + return false; +} + /* Return whether the value range *VR fits in an integer type specified by PRECISION and UNSIGNED_P. */ @@ -1235,6 +1298,8 @@ 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 ()) { + if (simplify_compare_using_range_pairs (cond_code, op0, op1, &vr)) + happened = true; tree new_tree = test_for_singularity (cond_code, op0, op1, &vr); if (new_tree) {