From 89b80c429a4e09fa8f3ccc1dfc2c2ec17f663d88 Mon Sep 17 00:00:00 2001 From: Prathamesh Kulkarni Date: Mon, 17 Oct 2016 08:48:43 +0000 Subject: [PATCH] re PR tree-optimization/71636 (Missed optimization in variable alignment test) 2016-10-17 Prathamesh Kulkarni PR tree-optimization/71636 * match.pd (x & ((1 << b) - 1) -> x & ~(~0 << b)): New pattern. testsuite/ * gcc.dg/pr71636-1.c: New test-case. * gcc.dg/pr71636-2.c: Likewise. From-SVN: r241229 --- gcc/ChangeLog | 5 +++++ gcc/match.pd | 6 ++++++ gcc/testsuite/ChangeLog | 5 +++++ gcc/testsuite/gcc.dg/pr71636-1.c | 9 +++++++++ gcc/testsuite/gcc.dg/pr71636-2.c | 12 ++++++++++++ 5 files changed, 37 insertions(+) create mode 100644 gcc/testsuite/gcc.dg/pr71636-1.c create mode 100644 gcc/testsuite/gcc.dg/pr71636-2.c diff --git a/gcc/ChangeLog b/gcc/ChangeLog index a9b1e3fdacd..9f4ff42fc29 100644 --- a/gcc/ChangeLog +++ b/gcc/ChangeLog @@ -1,3 +1,8 @@ +2016-10-17 Prathamesh Kulkarni + + PR tree-optimization/71636 + * match.pd (x & ((1 << b) - 1) -> x & ~(~0 << b)): New pattern. + 2016-10-17 Richard Biener * gimplify.c (gimplify_function_tree): Do not move the outer diff --git a/gcc/match.pd b/gcc/match.pd index e4ff0e70588..b782a1ed2f4 100644 --- a/gcc/match.pd +++ b/gcc/match.pd @@ -513,6 +513,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT) (bit_and:c (convert? @0) (convert? (bit_not @0))) { build_zero_cst (type); }) +/* PR71636: Transform x & ((1U << b) - 1) -> x & ~(~0U << b); */ +(simplify + (bit_and:c @0 (plus:s (lshift:s integer_onep @1) integer_minus_onep)) + (if (TYPE_UNSIGNED (type)) + (bit_and @0 (bit_not (lshift { build_all_ones_cst (type); } @1))))) + /* Fold (A & ~B) - (A & B) into (A ^ B) - B. */ (simplify (minus (bit_and:cs @0 (bit_not @1)) (bit_and:cs @0 @1)) diff --git a/gcc/testsuite/ChangeLog b/gcc/testsuite/ChangeLog index 92a0a3ad676..b99ce7b6326 100644 --- a/gcc/testsuite/ChangeLog +++ b/gcc/testsuite/ChangeLog @@ -1,3 +1,8 @@ +2016-10-17 Prathamesh Kulkarni + + * gcc.dg/pr71636-1.c: New test-case. + * gcc.dg/pr71636-2.c: Likewise. + 2016-10-16 Eric Botcazou * gnat.dg/trampoline3.adb: New test. diff --git a/gcc/testsuite/gcc.dg/pr71636-1.c b/gcc/testsuite/gcc.dg/pr71636-1.c new file mode 100644 index 00000000000..2df5f96f467 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr71636-1.c @@ -0,0 +1,9 @@ +/* { dg-do compile } */ +/* { dg-options "-fdump-tree-gimple" } */ + +unsigned f(unsigned x, unsigned b) +{ + return x & ((1U << b) - 1); +} + +/* { dg-final { scan-tree-dump-not "1 <<" "gimple" } } */ diff --git a/gcc/testsuite/gcc.dg/pr71636-2.c b/gcc/testsuite/gcc.dg/pr71636-2.c new file mode 100644 index 00000000000..9e9297d4a7c --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr71636-2.c @@ -0,0 +1,12 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-forwprop-details" } */ + +unsigned f(unsigned x, unsigned b) +{ + unsigned t1 = 1U << b; + unsigned t2 = t1 - 1; + unsigned t3 = x & t2; + return t3; +} + +/* { dg-final { scan-tree-dump "_\[0-9\] = ~_\[0-9\]" "forwprop1" } } */ -- 2.30.2