From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: from server.nextmovesoftware.com (server.nextmovesoftware.com [162.254.253.69]) by sourceware.org (Postfix) with ESMTPS id BE0213858287 for ; Mon, 8 Aug 2022 08:07:20 +0000 (GMT) DMARC-Filter: OpenDMARC Filter v1.4.1 sourceware.org BE0213858287 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=P0IUAOr51V1XS3n5I+uvppmTCbjFxQHmyDQKLmQqgg0=; b=pnmkKzJbOm9ulsgSckT6z7a3Wd eImjZZn/vtof8InKwqZTeZwGjcGIfr+xl3JOhWcEPCR/wUep2KkOcgsiw9ehviC+wkspT2hAM7MSS idOz8tqtykd3oxyP0vr63a8AsWnq8Oxt0kfBbjFU9JQ9c5vzo50cGgKrA9hBHChMKzpsqQ1j1894m DJbqaU9TZP81a5FOXiqWyZoaIu9ry6WYEQRDGZxTAEUGBov4RLI2FPEaCwtMmUlMs/UMF5ODFMcvB hhzQmQvN42qqge5dGyz8BjLjrYRDL7hWUGVSaBeTtkvDGsmfODlndI2AT97axiNLfKV3DcpWnMf74 DD60c0Iw==; Received: from host86-169-41-119.range86-169.btcentralplus.com ([86.169.41.119]:57016 helo=Dell) by server.nextmovesoftware.com with esmtpsa (TLS1.2) tls TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384 (Exim 4.94.2) (envelope-from ) id 1oKxnD-0005MN-Lw for gcc-patches@gcc.gnu.org; Mon, 08 Aug 2022 04:07:19 -0400 From: "Roger Sayle" To: Subject: [PATCH] PR tree-optimization/71343: Optimize (X< MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="----=_NextPart_000_00A6_01D8AB06.42D10E20" X-Mailer: Microsoft Outlook 16.0 Thread-Index: Adiq/VoMU7jUXs6WSfS3cL8QJP0Pwg== 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=-10.9 required=5.0 tests=BAYES_00, DKIM_SIGNED, DKIM_VALID, DKIM_VALID_AU, DKIM_VALID_EF, GIT_PATCH_0, RCVD_IN_BARRACUDACENTRAL, 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-List-Received-Date: Mon, 08 Aug 2022 08:07:24 -0000 This is a multipart message in MIME format. ------=_NextPart_000_00A6_01D8AB06.42D10E20 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit This patch resolves PR tree-optimization/71343, a missed-optimization enhancement request where GCC fails to see that (a<<2)+(b<<2) == a*4+b*4. This requires two related (sets of) optimizations to be added to match.pd. The first is that (X< 4*X" will always evaluate to false. 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-08-08 Roger Sayle gcc/ChangeLog PR tree-optimization/71343 * match.pd (op (lshift @0 @1) (lshift @2 @1)): Optimize the expression (X<>C)^(Y>>C) to (X^Y)>>C for binary logical operators, AND, IOR and XOR. (cmp:c (lshift @0) (mult @1)): Optimize comparisons between shifts by integer constants and multiplications by powers of 2. gcc/testsuite/ChangeLog PR tree-optimization/71343 * gcc.dg/pr71343-1.c: New test case. * gcc.dg/pr71343-2.c: Likewise. Thanks in advance, Roger -- ------=_NextPart_000_00A6_01D8AB06.42D10E20 Content-Type: text/plain; name="patchfd.txt" Content-Transfer-Encoding: quoted-printable Content-Disposition: attachment; filename="patchfd.txt" diff --git a/gcc/match.pd b/gcc/match.pd=0A= index f82f94a..d6b9cfb 100644=0A= --- a/gcc/match.pd=0A= +++ b/gcc/match.pd=0A= @@ -982,6 +982,35 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= && tree_nop_conversion_p (type, TREE_TYPE (@1)))=0A= (lshift @0 @2)))=0A= =0A= +/* Shifts by constants distribute over several binary operations,=0A= + hence (X << C) + (Y << C) can be simplified to (X + Y) << C. */=0A= +(for op (plus minus)=0A= + (simplify=0A= + (op (lshift:s @0 INTEGER_CST@1) (lshift:s @2 INTEGER_CST@1))=0A= + (if (INTEGRAL_TYPE_P (type)=0A= + && TYPE_OVERFLOW_WRAPS (type)=0A= + && !TYPE_SATURATING (type)=0A= + && tree_fits_shwi_p (@1)=0A= + && tree_to_shwi (@1) > 0=0A= + && tree_to_shwi (@1) < TYPE_PRECISION (type))=0A= + (lshift (op @0 @2) @1))))=0A= +=0A= +(for op (bit_and bit_ior bit_xor)=0A= + (simplify=0A= + (op (lshift:s @0 INTEGER_CST@1) (lshift:s @2 INTEGER_CST@1))=0A= + (if (INTEGRAL_TYPE_P (type)=0A= + && tree_fits_shwi_p (@1)=0A= + && tree_to_shwi (@1) > 0=0A= + && tree_to_shwi (@1) < TYPE_PRECISION (type))=0A= + (lshift (op @0 @2) @1)))=0A= + (simplify=0A= + (op (rshift:s @0 INTEGER_CST@1) (rshift:s @2 INTEGER_CST@1))=0A= + (if (INTEGRAL_TYPE_P (type)=0A= + && tree_fits_shwi_p (@1)=0A= + && tree_to_shwi (@1) > 0=0A= + && tree_to_shwi (@1) < TYPE_PRECISION (type))=0A= + (rshift (op @0 @2) @1))))=0A= +=0A= /* Fold (1 << (C - x)) where C =3D precision(type) - 1=0A= into ((1 << C) >> x). */=0A= (simplify=0A= @@ -2241,6 +2270,28 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)=0A= (if (TREE_INT_CST_LOW (@1) & 1)=0A= { constant_boolean_node (cmp =3D=3D NE_EXPR, type); })))=0A= =0A= +/* Some tree expressions are intentionally non-canonical.=0A= + We handle the comparison of the equivalent forms here. */=0A= +(for cmp (eq le ge)=0A= + (simplify=0A= + (cmp:c (lshift @0 INTEGER_CST@1) (mult @0 integer_pow2p@2))=0A= + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))=0A= + && tree_fits_shwi_p (@1)=0A= + && tree_to_shwi (@1) > 0=0A= + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))=0A= + && wi::to_wide (@1) =3D=3D wi::exact_log2 (wi::to_wide (@2)))=0A= + { constant_boolean_node (true, type); })))=0A= +=0A= +(for cmp (ne lt gt)=0A= + (simplify=0A= + (cmp:c (lshift @0 INTEGER_CST@1) (mult @0 integer_pow2p@2))=0A= + (if (INTEGRAL_TYPE_P (TREE_TYPE (@0))=0A= + && tree_fits_shwi_p (@1)=0A= + && tree_to_shwi (@1) > 0=0A= + && tree_to_shwi (@1) < TYPE_PRECISION (TREE_TYPE (@0))=0A= + && wi::to_wide (@1) =3D=3D wi::exact_log2 (wi::to_wide (@2)))=0A= + { constant_boolean_node (false, type); })))=0A= +=0A= /* Arguments on which one can call get_nonzero_bits to get the bits=0A= possibly set. */=0A= (match with_possible_nonzero_bits=0A= diff --git a/gcc/testsuite/gcc.dg/pr71343-1.c = b/gcc/testsuite/gcc.dg/pr71343-1.c=0A= new file mode 100644=0A= index 0000000..146f5fc=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.dg/pr71343-1.c=0A= @@ -0,0 +1,56 @@=0A= +/* { dg-do compile } */=0A= +/* { dg-options "-O2 -fdump-tree-optimized" } */=0A= +=0A= +unsigned int foo_plus(unsigned int a, unsigned int b)=0A= +{=0A= + return (a << 2) + (b << 2);=0A= +}=0A= +=0A= +unsigned int foo_and(unsigned int a, unsigned int b)=0A= +{=0A= + return (a << 2) & (b << 2);=0A= +}=0A= +=0A= +unsigned int foo_ior(unsigned int a, unsigned int b)=0A= +{=0A= + return (a << 2) | (b << 2);=0A= +}=0A= +=0A= +unsigned int foo_xor(unsigned int a, unsigned int b)=0A= +{=0A= + return (a << 2) ^ (b << 2);=0A= +}=0A= +=0A= +unsigned int bar_and(unsigned int a, unsigned int b)=0A= +{=0A= + return (a >> 2) & (b >> 2);=0A= +}=0A= +=0A= +unsigned int bar_ior(unsigned int a, unsigned int b)=0A= +{=0A= + return (a >> 2) | (b >> 2);=0A= +}=0A= +=0A= +unsigned int bar_xor(unsigned int a, unsigned int b)=0A= +{=0A= + return (a >> 2) ^ (b >> 2);=0A= +}=0A= +=0A= +int baz_and(int a, int b)=0A= +{=0A= + return (a >> 2) & (b >> 2);=0A= +}=0A= +=0A= +int baz_ior(int a, int b)=0A= +{=0A= + return (a >> 2) | (b >> 2);=0A= +}=0A= +=0A= +int baz_xor(int a, int b)=0A= +{=0A= + return (a >> 2) ^ (b >> 2);=0A= +}=0A= +=0A= +/* { dg-final { scan-tree-dump-times " << " 4 "optimized" } } */=0A= +/* { dg-final { scan-tree-dump-times " >> " 6 "optimized" } } */=0A= +=0A= diff --git a/gcc/testsuite/gcc.dg/pr71343-2.c = b/gcc/testsuite/gcc.dg/pr71343-2.c=0A= new file mode 100644=0A= index 0000000..11800a9=0A= --- /dev/null=0A= +++ b/gcc/testsuite/gcc.dg/pr71343-2.c=0A= @@ -0,0 +1,34 @@=0A= +/* { dg-do compile } */=0A= +/* { dg-options "-O2 -fdump-tree-optimized" } */=0A= +=0A= +unsigned int test1(unsigned int a , unsigned int b)=0A= +{=0A= + return (a << 2) + (b << 2) =3D=3D a * 4 + b * 4;=0A= +}=0A= +=0A= +unsigned int test2(unsigned int a , unsigned int b)=0A= +{=0A= + return (a << 2) + (b << 2) =3D=3D (a + b) << 2;=0A= +}=0A= +=0A= +unsigned int test3(unsigned int a , unsigned int b)=0A= +{=0A= + return a * 4 + b * 4 =3D=3D (a + b) * 4;=0A= +}=0A= +=0A= +unsigned int test4(unsigned int a , unsigned int b)=0A= +{=0A= + return (a + b) << 2 =3D=3D (a + b) * 4;=0A= +}=0A= +=0A= +unsigned int test5(unsigned int a , unsigned int b)=0A= +{=0A= + return (a << 2) + (b << 2) =3D=3D (a + b) * 4;=0A= +}=0A= +=0A= +unsigned int test6(unsigned int a , unsigned int b)=0A= +{=0A= + return (a + b) << 2 =3D=3D a * 4 + b * 4;=0A= +}=0A= +=0A= +/* { dg-final { scan-tree-dump-times "return 1" 6 "optimized" } } */=0A= ------=_NextPart_000_00A6_01D8AB06.42D10E20--