From patchwork Sat Jul 1 08:22:16 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Andrew Pinski X-Patchwork-Id: 115013 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a59:994d:0:b0:3d9:f83d:47d9 with SMTP id k13csp10903916vqr; Sat, 1 Jul 2023 01:23:26 -0700 (PDT) X-Google-Smtp-Source: APBJJlGi3OE5zl9vWnxe95RCBK2GLfFvtpCldJIrD9pHmQHRCoA675OI4Nw/4x1y/A7gl3f5uoUm X-Received: by 2002:a05:6402:387:b0:50c:2215:317e with SMTP id o7-20020a056402038700b0050c2215317emr3020056edv.15.1688199806720; Sat, 01 Jul 2023 01:23:26 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1688199806; cv=none; d=google.com; s=arc-20160816; b=TwmEe+vwUv8csMd2Yo9Rqbd2Sai4sYCulOhJPA/ZhnZ7Mz6RCI0QwVNs4G79nNOXA2 4E7heVESWGolU2x1uy0u8iYnLtupeXI+dBh8M5AQ0X26fHi0ClYAeNTTb5tgJOx1gOwR /xuuqdUV2rtgIEQWHfNThImSRImyQb69MUHUFxrC3lSAys7khgJVsInUsipuJjOyD3ei zP7En08cezJuwTlLi0UA9ng1CT/U7lbnToskLT+WfVTc3SyygOuFaDN1gPcU3Xn7yA+V c+1NDD1z9svkerETGbfWXjL8OV2yPjLnFdKVfvlJ3nWGNpgDfK4JgsEBk15DxLsxx+ZS j1DA== 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=hYd+u3HUNIrdPmAf6OtFTUpjvJl7fIfnfr0cN0ao6Xw=; fh=XbcmRug6/SzczusyoNU1I7Lu5oY0AN2u0mWDzKoxdSs=; b=SOfGHC1v+AVOmdZJdM38mPaafPyMLCUhgL9yzXZ5IVYJkqwkeoT78GU+zO3vMe8lde UWSd1J6+ZmBf0el2g+rY1bZPy+6IvtVc8WysTSLmAJKp1ne6TQWye2ktplQedXbo0Fa1 mkrzjZX8DvbDAJxbRsoWAYkhEq2DOCIceH7rOK12yBHf7xJ9Qo0LgStZHyc30dj3OCTs I5zSLK/oBEpxB7qifC+6AtUycLnFBqudhwBrb5rH+8vJonQCpDREHdGZCF6dNpMq+1BI QI/fhv0lJxbBd77R1fPEfNlFRMwflJ+9lkGJgf7P/5FbTq9zpjhDDYzR/pp0hfNX2BpX w5BA== ARC-Authentication-Results: i=1; mx.google.com; dkim=pass header.i=@gcc.gnu.org header.s=default header.b=hBDP4kQT; 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 sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id d7-20020a50fe87000000b0051a48e2d185si7797487edt.286.2023.07.01.01.23.26 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 01 Jul 2023 01:23:26 -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=hBDP4kQT; 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 815F33858433 for ; Sat, 1 Jul 2023 08:23:22 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 815F33858433 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gcc.gnu.org; s=default; t=1688199802; bh=hYd+u3HUNIrdPmAf6OtFTUpjvJl7fIfnfr0cN0ao6Xw=; 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=hBDP4kQTT9Wnu+MmLtJBhlPhNUHdfBGKyBKT7GB0lwMW6GksrnJbhuFApiQx+FtC0 dHQIu3Z1tJjVZg440g1ELwRmdTTq2wDLj/xmbUc6/5IlzwbR9EPAcamrzuHkG5ws8F VxYqxYjUPAs5STLuAfpquPxNBfMH6lAWLRZczWyM= 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 A7B5F3858D37 for ; Sat, 1 Jul 2023 08:22:29 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org A7B5F3858D37 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 3617U8cG031331 for ; Sat, 1 Jul 2023 01:22:29 -0700 Received: from dc5-exch02.marvell.com ([199.233.59.182]) by mx0b-0016f401.pphosted.com (PPS) with ESMTPS id 3rj8hg11p3-1 (version=TLSv1.2 cipher=ECDHE-RSA-AES256-SHA384 bits=256 verify=NOT) for ; Sat, 01 Jul 2023 01:22:29 -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; Sat, 1 Jul 2023 01:22:26 -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; Sat, 1 Jul 2023 01:22:26 -0700 Received: from vpnclient.wrightpinski.org.com (unknown [10.69.242.187]) by maili.marvell.com (Postfix) with ESMTP id F3F2A3F707D; Sat, 1 Jul 2023 01:22:25 -0700 (PDT) To: CC: Andrew Pinski Subject: [PATCH 2/2] PR 110487: `(a !=/== CST1 ? CST2 : CST3)` pattern for type safety Date: Sat, 1 Jul 2023 01:22:16 -0700 Message-ID: <20230701082216.299104-2-apinski@marvell.com> X-Mailer: git-send-email 2.31.1 In-Reply-To: <20230701082216.299104-1-apinski@marvell.com> References: <20230701082216.299104-1-apinski@marvell.com> MIME-Version: 1.0 X-Proofpoint-ORIG-GUID: KG6dur6agnydkpqOJqzGzfyYtiHJXgkL X-Proofpoint-GUID: KG6dur6agnydkpqOJqzGzfyYtiHJXgkL X-Proofpoint-Virus-Version: vendor=baseguard engine=ICAP:2.0.254,Aquarius:18.0.957,Hydra:6.0.591,FMLib:17.11.176.26 definitions=2023-07-01_06,2023-06-30_01,2023-05-22_02 X-Spam-Status: No, score=-14.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, PLING_QUERY, RCVD_IN_DNSWL_LOW, SPF_HELO_NONE, SPF_PASS, TXREP, T_SCC_BODY_TEXT_LINE 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: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1770205800369501187?= X-GMAIL-MSGID: =?utf-8?q?1770205800369501187?= The problem here is we might produce some values out of the type's min/max (and/or valid values, e.g. signed booleans). The fix is to use an integer type which has the same precision and signedness as the original type. Note two_value_replacement in phiopt had the same issue in previous versions; though I don't know if a problem will show up there. OK? Bootstrapped and tested on x86_64-linux-gnu. gcc/ChangeLog: PR tree-optimization/110487 * match.pd (a !=/== CST1 ? CST2 : CST3): Always build a nonstandard integer and use that. --- gcc/match.pd | 24 ++++++++---------------- 1 file changed, 8 insertions(+), 16 deletions(-) diff --git a/gcc/match.pd b/gcc/match.pd index a0d114f6a16..9748ad8466e 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -4797,24 +4797,16 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) tree type1; if ((eqne == EQ_EXPR) ^ (wi::to_wide (@1) == min)) std::swap (arg0, arg1); - if (TYPE_PRECISION (TREE_TYPE (@0)) == TYPE_PRECISION (type)) - { - /* Avoid performing the arithmetics in bool type which has different - semantics, otherwise prefer unsigned types from the two with - the same precision. */ - if (TREE_CODE (TREE_TYPE (arg0)) == BOOLEAN_TYPE - || !TYPE_UNSIGNED (type)) - type1 = TREE_TYPE (@0); - else - type1 = TREE_TYPE (arg0); - } - else if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) + if (TYPE_PRECISION (TREE_TYPE (@0)) > TYPE_PRECISION (type)) type1 = TREE_TYPE (@0); else type1 = type; - min = wide_int::from (min, TYPE_PRECISION (type1), + auto prec = TYPE_PRECISION (type1); + auto unsign = TYPE_UNSIGNED (type1); + type1 = build_nonstandard_integer_type (prec, unsign); + min = wide_int::from (min, prec, TYPE_SIGN (TREE_TYPE (@0))); - wide_int a = wide_int::from (wi::to_wide (arg0), TYPE_PRECISION (type1), + wide_int a = wide_int::from (wi::to_wide (arg0), prec, TYPE_SIGN (type)); enum tree_code code; wi::overflow_type ovf; @@ -4822,7 +4814,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) { code = PLUS_EXPR; a -= min; - if (!TYPE_UNSIGNED (type1)) + if (!unsign) { /* lhs is known to be in range [min, min+1] and we want to add a to it. Check if that operation can overflow for those 2 values @@ -4836,7 +4828,7 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) { code = MINUS_EXPR; a += min; - if (!TYPE_UNSIGNED (type1)) + if (!unsign) { /* lhs is known to be in range [min, min+1] and we want to subtract it from a. Check if that operation can overflow for those 2