From patchwork Sun Mar 12 15:04:20 2023 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Roger Sayle X-Patchwork-Id: 68361 Return-Path: Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5915:0:0:0:0:0 with SMTP id v21csp753241wrd; Sun, 12 Mar 2023 08:04:57 -0700 (PDT) X-Google-Smtp-Source: AK7set8QTOoj9GtbKU1DylyuOnZHVI7VJ8FSf1g0ixYrvPIjq5Kcq0lyCHPyOr4KOGcPR73JtLvf X-Received: by 2002:a17:907:6eab:b0:86f:64bb:47eb with SMTP id sh43-20020a1709076eab00b0086f64bb47ebmr36178903ejc.3.1678633496835; Sun, 12 Mar 2023 08:04:56 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1678633496; cv=none; d=google.com; s=arc-20160816; b=arJnx6GzBADlCJVPRSPqhT4TXaCFpWMTiemk9ChCl6icEpCeSKGNDwegPVgovqGLEc ua5AmTlcpOx1HZsbGQ7jCqLEpsQKxmLPhxdT4Twk9DcCvVk53m/SzJtEGdGOVPtPPEdp 76SLCo06BHAg1JlabiiZi/0iMOCWuZFrMPCnHDOqGt2wdEbZ/l93G98esScyi2f4KxRs Khu496wnF/c0R0OUt2O7MTz7A+1Oc6Cvik1GIs6MbEEiJNsA4IZcBMiyVstugI9hdNSL azN6yPfLOuK8wNE1yj+hzzphbSuzVIL0PH1yjvFBzDq/Xu5WSxmmEb4BN/uimJuHU9id LO3g== ARC-Message-Signature: i=1; a=rsa-sha256; c=relaxed/relaxed; d=google.com; s=arc-20160816; h=sender:errors-to:list-subscribe:list-help:list-post:list-archive :list-unsubscribe:list-id:precedence:content-language:thread-index :mime-version:message-id:date:subject:to:from:dkim-signature :dmarc-filter:delivered-to; bh=hwPHJ2f2S2zMf7jZ5fqhh6Lw4ITSEhnTvewMR1NjJ5M=; b=0VNVkqu1RHxViWdsoWEkwOLbkk11uo+4feCyHE/hB5e/JiJs2zX4OoUnSvfiZlgHcd tdvwWFjCIYL8ySPWgciisCKSbvfcLrmdLZGVVpMq/4oSmOj9AYeSJhPit+tC+aV/u5pm k1P3xSo/iQ/JgWXHGgiUKfavKAe1NqmNapjdIxBVhunAf5w4Fmjjcm23ArpKZcHwrzEy f6gtLQ4WZL8JQpSm9QpMm66B1jDDIJull4Bk4aCmEsU5WhjeRBnf3hH7tDl0la1Ir+jJ q0I9jsqeu58r+Df07RZPMh7yZHqE6Q3VeMWMZwmAg3S/Qn+Q2Y0XvMoaL3bU0/p4ar45 X3Yw== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b="pWBV8z/6"; 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" Received: from sourceware.org (ip-8-43-85-97.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id w14-20020a50fa8e000000b004ad15704e7csi5693150edr.376.2023.03.12.08.04.56 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sun, 12 Mar 2023 08:04:56 -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=fail header.i=@nextmovesoftware.com header.s=default header.b="pWBV8z/6"; 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" Received: from server2.sourceware.org (localhost [IPv6:::1]) by sourceware.org (Postfix) with ESMTP id C2A153858035 for ; Sun, 12 Mar 2023 15:04:51 +0000 (GMT) X-Original-To: gcc-patches@gcc.gnu.org Delivered-To: gcc-patches@gcc.gnu.org Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id 73DB13858D32 for ; Sun, 12 Mar 2023 15:04:25 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.2 sourceware.org 73DB13858D32 Authentication-Results: sourceware.org; dmarc=none (p=none dis=none) header.from=nextmovesoftware.com Authentication-Results: sourceware.org; spf=pass smtp.mailfrom=nextmovesoftware.com DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=nextmovesoftware.com; s=default; h=Content-Type:MIME-Version:Message-ID: Date:Subject:To:From:Sender:Reply-To:Cc:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=hwPHJ2f2S2zMf7jZ5fqhh6Lw4ITSEhnTvewMR1NjJ5M=; b=pWBV8z/6rtW7LNd0YNaZabFzkf WYd7mf+d94XIx28DJC5vJ8J6sBs9S3g4wOc46XF4TIjcEzScTOb5SGAEtviDsjgACWPnZEl6qdYz2 GRKFslnzqltnhZxq//LgI9Y+FAs8FURUSh6djplbn89uO5nMVxguiRzcmXTj7tF5dGfLjHp78TQxo 8l/IqBB7Y9twATh8k19DBm12A3Ntr9cebtSYAbKyNHyxFrtFlO4IPRIXse6ymr3R++ux/xEn0hEGL fesriRi8jAyntl3a2wtEXxy76gyx8dpHIVaR0fw10fENppt75SBUaPoMz/d+fZtU2P86VtpKjLif9 8DLGBs+w==; Received: from host86-163-35-54.range86-163.btcentralplus.com ([86.163.35.54]:54917 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from ) id 1pbNFG-0000KB-6h for gcc-patches@gcc.gnu.org; Sun, 12 Mar 2023 11:04:22 -0400 From: "Roger Sayle" To: "'GCC Patches'" Subject: [PATCH] PR middle-end/109031: Fix final value replacement from narrower IVs. Date: Sun, 12 Mar 2023 15:04:20 -0000 Message-ID: <000601d954f3$ee202350$ca6069f0$@nextmovesoftware.com> MIME-Version: 1.0 X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdlU8RpkQ7byle/gSd2ouut+jhAHAg== Content-Language: en-gb X-AntiAbuse: This header was added to track abuse, please include it with any abuse report X-AntiAbuse: Primary Hostname - server.nextmovesoftware.com X-AntiAbuse: Original Domain - gcc.gnu.org X-AntiAbuse: Originator/Caller UID/GID - [47 12] / [47 12] X-AntiAbuse: Sender Address Domain - nextmovesoftware.com X-Get-Message-Sender-Via: server.nextmovesoftware.com: authenticated_id: roger@nextmovesoftware.com X-Authenticated-Sender: server.nextmovesoftware.com: roger@nextmovesoftware.com X-Source: X-Source-Args: X-Source-Dir: X-Spam-Status: No, score=-12.8 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, KAM_SHORT, 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: , 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?1760174797324099638?= X-GMAIL-MSGID: =?utf-8?q?1760174797324099638?= This patch fixes a P1 regression, a problem with my February 2022 patch to improve folding for final value replacement: https://gcc.gnu.org/pipermail/gcc-patches/2022-February/590618.html The motivation for the original patch is that because we know the number of loop iterations can't be negative, final value expressions such as (int) ((unsigned int) x + 4294967295) + 1 can be simplified to x, as this is effectively ((x - 1) + 1) without overflow. The bug/oversight is that using integer_all_onesp to check for the implicit tree constant -1 it didn't consider that the inner (unsigned) type might be narrower than outer result type. For the case in the PR, (int)((unsigned char)x + 255) + 1 gets simplified to (int)x, but when x is originally zero, the correct result should be 256. The fix is to check that the inner type's precision (the width of the subtraction) is at least as wide as the result type (that of the addition). This patch has been tested on x86_64-pc-linux-gnu with make bootstrap and make -k check, both with and without --target_board=unix{-m32}, with no new failures. I've also added a test for signed types, but without -fwrapv this invokes undefined behaviour, and with -fwrapv it doesn't exhibit the problem in the PR. Ok for mainline? 2023-03-12 Roger Sayle gcc/ChangeLog PR middle-end/109031 * tree-chrec.cc (chrec_apply): When folding "{a, +, a} (x-1)", ensure that the type of x is as wide or wider than the type of a. gcc/testsuite/ChangeLog PR middle-end/109031 * gcc.dg/tree-ssa/pr109031-1.c: New test case. * gcc.dg/tree-ssa/pr109031-2.c: Likewise. Thanks in advance, and my apologies to the breakage/inconvenience. Roger diff --git a/gcc/tree-chrec.cc b/gcc/tree-chrec.cc index f93d8dc..2f67581 100644 --- a/gcc/tree-chrec.cc +++ b/gcc/tree-chrec.cc @@ -623,7 +623,9 @@ chrec_apply (unsigned var, else if (operand_equal_p (CHREC_LEFT (chrec), chrecr) && TREE_CODE (x) == PLUS_EXPR && integer_all_onesp (TREE_OPERAND (x, 1)) - && !POINTER_TYPE_P (type)) + && !POINTER_TYPE_P (type) + && TYPE_PRECISION (TREE_TYPE (x)) + >= TYPE_PRECISION (type)) { /* We know the number of iterations can't be negative. So {a, +, a} (x-1) -> "a*x". */ diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c new file mode 100644 index 0000000..84e1a08 --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-1.c @@ -0,0 +1,39 @@ +/* { dg-do run } */ +/* { dg-options "-O2" } */ +unsigned char uc; +unsigned short us; + +void testuc() { + unsigned int g = 0; + unsigned int *p1 = &g; + unsigned char *p2 = &uc; + + do { + (*p1)++; + (*p2)--; + } while (uc); + + if (g != 256) + __builtin_abort(); +} + +void testus() { + unsigned int g = 0; + unsigned int *p1 = &g; + unsigned short *p2 = &us; + + do { + (*p1)++; + (*p2)--; + } while (us); + + if (g != 65536) + __builtin_abort(); +} + +int main() { + testuc(); + testus(); + return 0; +} + diff --git a/gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c new file mode 100644 index 0000000..6f28b3b --- /dev/null +++ b/gcc/testsuite/gcc.dg/tree-ssa/pr109031-2.c @@ -0,0 +1,39 @@ +/* { dg-do run } */ +/* { dg-options "-O2 -fwrapv" } */ +signed char sc; +signed short ss; + +void testsc() { + unsigned int g = 0; + unsigned int *p1 = &g; + signed char *p2 = ≻ + + do { + (*p1)++; + (*p2)--; + } while (sc); + + if (g != 256) + __builtin_abort(); +} + +void testss() { + unsigned int g = 0; + unsigned int *p1 = &g; + signed short *p2 = &ss; + + do { + (*p1)++; + (*p2)--; + } while (ss); + + if (g != 65536) + __builtin_abort(); +} + +int main() { + testsc(); + testss(); + return 0; +} +