From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: Received: by sourceware.org (Postfix, from userid 7892) id 0E08B3858407; Thu, 4 Aug 2022 13:37:52 +0000 (GMT) DKIM-Filter: OpenDKIM Filter v2.11.0 sourceware.org 0E08B3858407 MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Type: text/plain; charset="utf-8" From: Sam Feifer To: gcc-cvs@gcc.gnu.org Subject: [gcc r13-1962] match.pd: Add bitwise and pattern [PR106243] X-Act-Checkin: gcc X-Git-Author: Sam Feifer X-Git-Refname: refs/heads/trunk X-Git-Oldrev: d8552eaddc40b72461158e56b5db8709f2eb21ed X-Git-Newrev: 39579ba8de9d91eafcc4943259b154a9e66538f1 Message-Id: <20220804133752.0E08B3858407@sourceware.org> Date: Thu, 4 Aug 2022 13:37:52 +0000 (GMT) X-BeenThere: gcc-cvs@gcc.gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: Gcc-cvs mailing list List-Unsubscribe: , List-Archive: List-Help: List-Subscribe: , X-List-Received-Date: Thu, 04 Aug 2022 13:37:52 -0000 https://gcc.gnu.org/g:39579ba8de9d91eafcc4943259b154a9e66538f1 commit r13-1962-g39579ba8de9d91eafcc4943259b154a9e66538f1 Author: Sam Feifer Date: Wed Aug 3 10:31:03 2022 -0400 match.pd: Add bitwise and pattern [PR106243] This patch adds a new optimization to match.pd. The pattern, -x & 1, now gets simplified to x & 1, reducing the number of instructions produced. This patch also adds tests for the optimization rule. Bootstrapped/regtested on x86_64-pc-linux-gnu. PR tree-optimization/106243 gcc/ChangeLog: * match.pd (-x & 1): New simplification. gcc/testsuite/ChangeLog: * gcc.dg/pr106243-1.c: New test. * gcc.dg/pr106243.c: New test. Diff: --- gcc/match.pd | 5 +++++ gcc/testsuite/gcc.dg/pr106243-1.c | 18 ++++++++++++++++ gcc/testsuite/gcc.dg/pr106243.c | 43 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+) diff --git a/gcc/match.pd b/gcc/match.pd index 562138a8034..d3d73e3f55c 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -8061,3 +8061,8 @@ and, (if (TYPE_UNSIGNED (TREE_TYPE (@0))) (bit_and @0 @1) (cond (le @0 @1) @0 (bit_and @0 @1)))))) + +/* -x & 1 -> x & 1. */ +(simplify + (bit_and (negate @0) integer_onep@1) + (bit_and @0 @1)) diff --git a/gcc/testsuite/gcc.dg/pr106243-1.c b/gcc/testsuite/gcc.dg/pr106243-1.c new file mode 100644 index 00000000000..b1dbe5cbe44 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr106243-1.c @@ -0,0 +1,18 @@ +/* PR tree-optimization/106243 */ +/* { dg-do run } */ +/* { dg-options "-O2" } */ + +#include "pr106243.c" + +int main () { + + if (foo(3) != 1 + || bar(-6) != 0 + || baz(17) != 1 + || qux(-128) != 0 + || foo(127) != 1) { + __builtin_abort(); + } + + return 0; +} diff --git a/gcc/testsuite/gcc.dg/pr106243.c b/gcc/testsuite/gcc.dg/pr106243.c new file mode 100644 index 00000000000..ee2706f2bf9 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr106243.c @@ -0,0 +1,43 @@ +/* PR tree-optimization/106243 */ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +#define vector __attribute__((vector_size(4*sizeof(int)))) + +/* Test from PR. */ +__attribute__((noipa)) int foo (int x) { + return -x & 1; +} + +/* Other test from PR. */ +__attribute__((noipa)) int bar (int x) { + return (0 - x) & 1; +} + +/* Forward propogation. */ +__attribute__((noipa)) int baz (int x) { + x = -x; + return x & 1; +} + +/* Commutative property. */ +__attribute__((noipa)) int qux (int x) { + return 1 & -x; +} + +/* Vector test case. */ +__attribute__((noipa)) vector int waldo (vector int x) { + return -x & 1; +} + +/* Should not simplify. */ +__attribute__((noipa)) int thud (int x) { + return -x & 2; +} + +/* Should not simplify. */ +__attribute__((noipa)) int corge (int x) { + return -x & -1; +} + +/* { dg-final {scan-tree-dump-times "-" 2 "optimized" } } */