Message ID | 002d01d8c79f$dc5fe830$951fb890$@nextmovesoftware.com |
---|---|
State | Accepted, archived |
Headers |
Return-Path: <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> Delivered-To: ouuuleilei@gmail.com Received: by 2002:a5d:5044:0:0:0:0:0 with SMTP id h4csp2479878wrt; Tue, 13 Sep 2022 11:37:53 -0700 (PDT) X-Google-Smtp-Source: AA6agR5S6uRxUfI5ktNhW0IcdoymDNJHrgP8e8FQURvRZP8ONMZLbCEBlOtKdCqqGmg2XfSegbk2 X-Received: by 2002:aa7:ca50:0:b0:44e:973b:461e with SMTP id j16-20020aa7ca50000000b0044e973b461emr27123782edt.414.1663094273459; Tue, 13 Sep 2022 11:37:53 -0700 (PDT) ARC-Seal: i=1; a=rsa-sha256; t=1663094273; cv=none; d=google.com; s=arc-20160816; b=ZUKdXZYGI6vIWoArSyUZEn0+1N/JMveTrXbpntcyhc9vALyaqo/7fuuC+C3CfXI8v/ b9JKTneAyHF8m/1ZZbCRr+1kHb4vRZ2ttWB8iRrEDGjpOdOfzFGtGNGbVzlKmYt5Aevx awwEbWKvrTdOHACa8Iq9yd1Wfnvxf+TU/S0cJme0vdYmU1WmLaXIh1+Kp0l2YdVZhRwL b4vroivkfebNGNlneB/T6muk7Xu+3SZiXFidUkR5sXOrOwxZZ7eTEMfP2ah2wbE8Epv7 DbqExHVU5GWn7CH7Jrys/gB55AFDhN97Z36B776Clb3u2+tzxnTNOT8n9EFf/sUnVWiT Dc5A== 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=+hXwPZVh/oYEV2uXpN0YJOazRK8Xt1Fh8rbu5T0PSBA=; b=0pKkn83gpmvGVcJk4ph0OEIs1P5ipj+X5adqngcKosIKL9h3OVBcm5gHCoORC5gzX9 +KkEN/JOI9JogkoIDKxtUxd3eQVO45FRMQZZH/v+pAjzPIYYJ+XpnUK39k7yy7NEappi z5shZ7wtB6UqbiaMjgttxOtPyAoUDYkY2CrqkZeg8JbuVOA81SHINttyPwZAJw4X2+Nr pTPC3Qol9085wkMdZNe0KJCMNev1vq1TiifahJ44c/chty11WqHEO3+2hw98malAnhsm UB1LLj8xMMQT0IUCU004ZcJE/1OVY8keHL7/bzT3cD4FBaUlyTxvMRXEiZPWdPuh2QFg v6CQ== ARC-Authentication-Results: i=1; mx.google.com; dkim=fail header.i=@nextmovesoftware.com header.s=default header.b="La/KX8gX"; 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 (server2.sourceware.org. [8.43.85.97]) by mx.google.com with ESMTPS id p8-20020a170906a00800b0076ed46e4440si8866117ejy.636.2022.09.13.11.37.53 for <ouuuleilei@gmail.com> (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 13 Sep 2022 11:37:53 -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="La/KX8gX"; 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 82D153858407 for <ouuuleilei@gmail.com>; Tue, 13 Sep 2022 18:37:46 +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 AA39D3858D28 for <gcc-patches@gcc.gnu.org>; Tue, 13 Sep 2022 18:37:22 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org AA39D3858D28 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=+hXwPZVh/oYEV2uXpN0YJOazRK8Xt1Fh8rbu5T0PSBA=; b=La/KX8gXX0hgoDKo2i/4VH+m4X 2rb5Ng18briZIyJ5Om0/vTHf2xVK/FIeoUHx/ZUh7BBDYww+bqsYYj4LWgiMQni33DHjloKDNJnC2 Lk9EmGoIw2hQWdJTEup8gKH0WlVZfYY8sXKC74Se2uB4OqI4rQPsh8HS+E7sBIFdjtCcsDP0E6bAQ efVsdU9Cxbr1umFCLJTnaQCCmBrwwA1suvp6FnA7hy8udNNFOKjmvlbcVrFw1KB83k94sNhrG/nzR kb8a+aCmyAhjrYTCrsr/31iypvS0m15ayr0IK4rl+BOqQZSCFXMo7lzPP6OJSzDEpVIWT3myiSgqY nMeWKYAw==; Received: from [185.62.158.67] (port=65526 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.95) (envelope-from <roger@nextmovesoftware.com>) id 1oYAmf-0006ua-Ii for gcc-patches@gcc.gnu.org; Tue, 13 Sep 2022 14:37:21 -0400 From: "Roger Sayle" <roger@nextmovesoftware.com> To: "'GCC Patches'" <gcc-patches@gcc.gnu.org> Subject: [PATCH] Optimize (X<<C)+(Y<<C) as (X+Y)<<C for signed addition. Date: Tue, 13 Sep 2022 19:37:20 +0100 Message-ID: <002d01d8c79f$dc5fe830$951fb890$@nextmovesoftware.com> MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_002E_01D8C7A8.3E26E840" X-Mailer: Microsoft Outlook 16.0 Thread-Index: AdjHnskNwiLuCwEkRK2BcXF+Q6VAOw== 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.5 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, 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 <gcc-patches.gcc.gnu.org> List-Unsubscribe: <https://gcc.gnu.org/mailman/options/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=unsubscribe> List-Archive: <https://gcc.gnu.org/pipermail/gcc-patches/> List-Post: <mailto:gcc-patches@gcc.gnu.org> List-Help: <mailto:gcc-patches-request@gcc.gnu.org?subject=help> List-Subscribe: <https://gcc.gnu.org/mailman/listinfo/gcc-patches>, <mailto:gcc-patches-request@gcc.gnu.org?subject=subscribe> Errors-To: gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org Sender: "Gcc-patches" <gcc-patches-bounces+ouuuleilei=gmail.com@gcc.gnu.org> X-getmail-retrieved-from-mailbox: =?utf-8?q?INBOX?= X-GMAIL-THRID: =?utf-8?q?1743880740558220973?= X-GMAIL-MSGID: =?utf-8?q?1743880740558220973?= |
Series |
Optimize (X<<C)+(Y<<C) as (X+Y)<<C for signed addition.
|
|
Checks
Context | Check | Description |
---|---|---|
snail/gcc-patches-check | success | Github commit url |
Commit Message
Roger Sayle
Sept. 13, 2022, 6:37 p.m. UTC
This patch tweaks the match.pd transformation previously added to fold (X<<C)+(Y<<C) as (X+Y)<<C that was previously restricted to unsigned (wrapping) types, to also allow signed integer types provided that they don't trap and the overflow needn't be preserved for sanitization. i.e. this should now apply (by default) for "int x,y;", but is disabled with -ftrapv. This unsigned transformation has baked on mainline without problems, so it's time to turn up the heat... LLVM, icc and MSVC perform this optimization. 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. Ok for mainline? 2022-09-13 Roger Sayle <roger@nextmovesoftware.com> gcc/ChangeLog * match.pd (op (lshift @0 @1) (lshift @2 @1)): Optimize the expression (X<<C) + (Y<<C) to (X+Y)<<C for signed types, that may have undefined overflow, provided that they don't trap. gcc/testsuite/ChangeLog * gcc.dg/pr71343-3.c: New test case. Thanks in advance, Roger --
Comments
On Tue, Sep 13, 2022 at 8:37 PM Roger Sayle <roger@nextmovesoftware.com> wrote: > > > This patch tweaks the match.pd transformation previously added to fold > (X<<C)+(Y<<C) as (X+Y)<<C that was previously restricted to unsigned > (wrapping) types, to also allow signed integer types provided that they > don't trap and the overflow needn't be preserved for sanitization. > i.e. this should now apply (by default) for "int x,y;", but is disabled > with -ftrapv. > > This unsigned transformation has baked on mainline without problems, so it's > time to turn up the heat... LLVM, icc and MSVC perform this optimization. > > > 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. Ok for mainline? OK. Thanks, Richard. > > > 2022-09-13 Roger Sayle <roger@nextmovesoftware.com> > > gcc/ChangeLog > * match.pd (op (lshift @0 @1) (lshift @2 @1)): Optimize the > expression (X<<C) + (Y<<C) to (X+Y)<<C for signed types, that > may have undefined overflow, provided that they don't trap. > > gcc/testsuite/ChangeLog > * gcc.dg/pr71343-3.c: New test case. > > > Thanks in advance, > Roger > -- >
On Tue, 13 Sep 2022, Roger Sayle wrote: > This patch tweaks the match.pd transformation previously added to fold > (X<<C)+(Y<<C) as (X+Y)<<C that was previously restricted to unsigned > (wrapping) types, to also allow signed integer types provided that they > don't trap and the overflow needn't be preserved for sanitization. > i.e. this should now apply (by default) for "int x,y;", but is disabled > with -ftrapv. In https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html , I read: "Bitwise operators act on the representation of the value including both the sign and value bits, where the sign bit is considered immediately above the highest-value value bit. Signed ‘>>’ acts on negative numbers by sign extension. As an extension to the C language, GCC does not use the latitude given in C99 and C11 only to treat certain aspects of signed ‘<<’ as undefined." To me, this means that for instance INT_MIN<<1 is well defined and evaluates to 0. But with this patch we turn (INT_MIN<<1)+(INT_MIN<<1) into (INT_MIN+INT_MIN)<<1, which is UB. If we decide not to support this extension anymore, I think we need to change the documentation first.
On Wed, Sep 14, 2022 at 11:42 PM Marc Glisse via Gcc-patches <gcc-patches@gcc.gnu.org> wrote: > > On Tue, 13 Sep 2022, Roger Sayle wrote: > > > This patch tweaks the match.pd transformation previously added to fold > > (X<<C)+(Y<<C) as (X+Y)<<C that was previously restricted to unsigned > > (wrapping) types, to also allow signed integer types provided that they > > don't trap and the overflow needn't be preserved for sanitization. > > i.e. this should now apply (by default) for "int x,y;", but is disabled > > with -ftrapv. > > In https://gcc.gnu.org/onlinedocs/gcc/Integers-implementation.html , I > read: > > "Bitwise operators act on the representation of the value including both > the sign and value bits, where the sign bit is considered immediately > above the highest-value value bit. Signed ‘>>’ acts on negative numbers by > sign extension. > > As an extension to the C language, GCC does not use the latitude given in > C99 and C11 only to treat certain aspects of signed ‘<<’ as undefined." Oh, I wasn't aware of that. > To me, this means that for instance INT_MIN<<1 is well defined and > evaluates to 0. But with this patch we turn (INT_MIN<<1)+(INT_MIN<<1) into > (INT_MIN+INT_MIN)<<1, which is UB. > > If we decide not to support this extension anymore, I think we need to > change the documentation first. Or perform the (X+Y) operation in an unsigned type. Richard. > -- > Marc Glisse
diff --git a/gcc/match.pd b/gcc/match.pd index 17318f52..e1e6af9 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -988,7 +988,8 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (simplify (op (lshift:s @0 @1) (lshift:s @2 @1)) (if (INTEGRAL_TYPE_P (type) - && TYPE_OVERFLOW_WRAPS (type) + && !TYPE_OVERFLOW_SANITIZED (type) + && !TYPE_OVERFLOW_TRAPS (type) && !TYPE_SATURATING (type)) (lshift (op @0 @2) @1)))) diff --git a/gcc/testsuite/gcc.dg/pr71343-3.c b/gcc/testsuite/gcc.dg/pr71343-3.c new file mode 100644 index 0000000..d0bdbae --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr71343-3.c @@ -0,0 +1,15 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int foo(int a, int b) +{ + return (a << 2) + (b << 2); +} + +int bar(int a, int b) +{ + return (a << 2) + (b << 2) == (a + b) << 2; +} + +/* { dg-final { scan-tree-dump-times " << " 1 "optimized" } } */ +/* { dg-final { scan-tree-dump-times "return 1" 1 "optimized" } } */