Hi,

This patch wants to fix PR66552 on gimple and optimizes (x shift (n mod C))
to (x shift (n bit_and (C - 1))) when C is a constant and power of two as
discussed in PR66552.

The regression testing for the patch was done on GCC mainline on

    powerpc64le-unknown-linux-gnu (Power 9 LE)

with no regressions.  Is it OK for GCC11 ?

Thanks,
Lijia He

gcc/ChangeLog
2020-02-17  Li Jia He  <heli...@linux.ibm.com>

        PR tree-optimization/66552
        * match.pd (x shift (n mod pow2(c))): Optimizes to
        (x shift (n bit_and (pow2(c) - 1)).

testsuite/ChangeLog
2019-02-17  Li Jia He  <heli...@linux.ibm.com>

        PR tree-optimization/66552
        * testsuite/gcc.dg/pr66552.c: New testcase.
---
 gcc/match.pd                   |  6 ++++++
 gcc/testsuite/gcc.dg/pr66552.c | 14 ++++++++++++++
 2 files changed, 20 insertions(+)
 create mode 100644 gcc/testsuite/gcc.dg/pr66552.c

diff --git a/gcc/match.pd b/gcc/match.pd
index 73834c25593..1d74f7dba7f 100644
--- a/gcc/match.pd
+++ b/gcc/match.pd
@@ -546,6 +546,12 @@ DEFINE_INT_AND_FLOAT_ROUND_FN (RINT)
  (simplify
   (mod (mod@2 @0 @1) @1)
   @2)
+ /* Optimize (x shift (n mod C)) to (x shift (n bit_and (C - 1))) when C is a
+    constant and power of two.  */
+ (for shift (lshift rshift)
+  (simplify
+   (shift @0 (mod @1 integer_pow2p@2))
+   (shift @0 (bit_and @1 (minus @2 { build_int_cst (TREE_TYPE (@2), 1); })))))
  /* From extract_muldiv_1: (X * C1) % C2 is zero if C1 is a multiple of C2.  */
  (simplify
   (mod (mult @0 INTEGER_CST@1) INTEGER_CST@2)
diff --git a/gcc/testsuite/gcc.dg/pr66552.c b/gcc/testsuite/gcc.dg/pr66552.c
new file mode 100644
index 00000000000..7583c9ad25a
--- /dev/null
+++ b/gcc/testsuite/gcc.dg/pr66552.c
@@ -0,0 +1,14 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-lower" } */
+
+unsigned a(unsigned x, int n)
+{
+  return x >> (n % 32);
+}
+
+unsigned b(unsigned x, int n)
+{
+  return x << (n % 32);
+}
+
+/* { dg-final { scan-tree-dump-not " % " "lower" } } */
-- 
2.17.1

Reply via email to