]> gcc.gnu.org Git - gcc.git/commitdiff
re PR tree-optimization/67351 (Missed optimisation on 64-bit field compared to 32...
authorNaveen H.S <Naveen.Hurugalawadi@caviumnetworks.com>
Thu, 3 Sep 2015 10:20:03 +0000 (10:20 +0000)
committerNaveen H.S <naveenh@gcc.gnu.org>
Thu, 3 Sep 2015 10:20:03 +0000 (10:20 +0000)
2015-09-03  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>

PR middle-end/67351

gcc/ChangeLog:
* fold-const.c (fold_binary_loc) : Move
Transform (x >> c) << c into x & (-1<<c) or
transform (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
types using simplify and match.
* match.pd (lshift (rshift @0 INTEGER_CST@1) @1) : New simplifier.
(rshift (lshift @0 INTEGER_CST@1) @1) : New Simplifier.

gcc/testsuite/ChangeLog:
* g++.dg/pr66752-2.C: New test.

From-SVN: r227432

gcc/ChangeLog
gcc/fold-const.c
gcc/match.pd
gcc/testsuite/ChangeLog
gcc/testsuite/g++.dg/pr67351.C [new file with mode: 0644]

index f20e551f5ed1a66ca30c71a23245aebdd4a3c874..57234e7c1e06b615ed3391a0db05b6cba412493f 100644 (file)
@@ -1,3 +1,13 @@
+2015-09-03  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>
+
+       PR middle-end/67351
+       * fold-const.c (fold_binary_loc) : Move 
+       Transform (x >> c) << c into x & (-1<<c) or
+       transform (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
+       types using simplify and match.
+       * match.pd (lshift (rshift @0 INTEGER_CST@1) @1) : New simplifier.
+       (rshift (lshift @0 INTEGER_CST@1) @1) : New Simplifier
+
 2015-09-03  Richard Biener  <rguenther@suse.de>
 
        PR ipa/66705
index d478c4dc1c234a7223922d2bf5d987514a7134a1..a79bfa7b0600eb3f9f3239939d5e8aa982256698 100644 (file)
@@ -10412,32 +10412,6 @@ fold_binary_loc (location_t loc,
 
       prec = element_precision (type);
 
-      /* Transform (x >> c) << c into x & (-1<<c), or transform (x << c) >> c
-         into x & ((unsigned)-1 >> c) for unsigned types.  */
-      if (((code == LSHIFT_EXPR && TREE_CODE (arg0) == RSHIFT_EXPR)
-           || (TYPE_UNSIGNED (type)
-              && code == RSHIFT_EXPR && TREE_CODE (arg0) == LSHIFT_EXPR))
-         && tree_fits_uhwi_p (arg1)
-         && tree_to_uhwi (arg1) < prec
-         && tree_fits_uhwi_p (TREE_OPERAND (arg0, 1))
-         && tree_to_uhwi (TREE_OPERAND (arg0, 1)) < prec)
-       {
-         HOST_WIDE_INT low0 = tree_to_uhwi (TREE_OPERAND (arg0, 1));
-         HOST_WIDE_INT low1 = tree_to_uhwi (arg1);
-         tree lshift;
-         tree arg00;
-
-         if (low0 == low1)
-           {
-             arg00 = fold_convert_loc (loc, type, TREE_OPERAND (arg0, 0));
-
-             lshift = build_minus_one_cst (type);
-             lshift = const_binop (code, lshift, arg1);
-
-             return fold_build2_loc (loc, BIT_AND_EXPR, type, arg00, lshift);
-           }
-       }
-
       /* If we have a rotate of a bit operation with the rotate count and
         the second operand of the bit operation both constant,
         permute the two operations.  */
index fb4b342d31d26a03bc756c538f6635f2acf6ddb2..bd5c267f1f86093be10eab8c40cfbf94b71c6545 100644 (file)
@@ -931,6 +931,20 @@ along with GCC; see the file COPYING3.  If not see
       && tree_expr_nonnegative_p (@1))
   @0))
 
+/* Optimize (x >> c) << c into x & (-1<<c).  */
+(simplify
+ (lshift (rshift @0 INTEGER_CST@1) @1)
+ (if (wi::ltu_p (@1, element_precision (type)))
+  (bit_and @0 (lshift { build_minus_one_cst (type); } @1))))
+
+/* Optimize (x << c) >> c into x & ((unsigned)-1 >> c) for unsigned
+   types.  */
+(simplify
+ (rshift (lshift @0 INTEGER_CST@1) @1)
+ (if (TYPE_UNSIGNED (type)
+      && (wi::ltu_p (@1, element_precision (type))))
+  (bit_and @0 (rshift { build_minus_one_cst (type); } @1))))
+
 (for shiftrotate (lrotate rrotate lshift rshift)
  (simplify
   (shiftrotate @0 integer_zerop)
index 97bdd7a10ea4eacf5eb61bf19feec93fc6f5a243..c107835c41dd00e35d24bc9cc76dfc4e7b5af996 100644 (file)
@@ -1,3 +1,8 @@
+2015-09-03  Naveen H.S  <Naveen.Hurugalawadi@caviumnetworks.com>
+
+       PR middle-end/67351
+       * g++.dg/pr66752-2.C: New test.
+
 2015-09-03  Richard Biener  <rguenther@suse.de>
 
        PR ipa/66705
diff --git a/gcc/testsuite/g++.dg/pr67351.C b/gcc/testsuite/g++.dg/pr67351.C
new file mode 100644 (file)
index 0000000..f5bdda6
--- /dev/null
@@ -0,0 +1,106 @@
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+typedef unsigned char uchar;
+typedef unsigned short ushort;
+typedef unsigned int uint;
+typedef unsigned long long uint64;
+
+class MyRgba
+{
+  uint rgba;
+
+public:
+    explicit MyRgba (uint c):rgba (c)
+  {
+  };
+
+  static MyRgba fromRgba (uchar r, uchar g, uchar b, uchar a)
+  {
+    return MyRgba (uint (r) << 24
+                  | uint (g) << 16 | uint (b) << 8 | uint (a));
+  }
+
+  uchar r ()
+  {
+    return rgba >> 24;
+  }
+  uchar g ()
+  {
+    return rgba >> 16;
+  }
+  uchar b ()
+  {
+    return rgba >> 8;
+  }
+  uchar a ()
+  {
+    return rgba;
+  }
+
+  void setG (uchar _g)
+  {
+    *this = fromRgba (r (), _g, b (), a ());
+  }
+};
+
+extern MyRgba giveMe ();
+
+MyRgba
+test ()
+{
+  MyRgba a = giveMe ();
+  a.setG (0xf0);
+  return a;
+}
+
+class MyRgba64
+{
+  uint64 rgba;
+
+public:
+    explicit MyRgba64 (uint64 c):rgba (c)
+  {
+  };
+
+  static MyRgba64 fromRgba64 (ushort r, ushort g, ushort b, ushort a)
+  {
+    return MyRgba64 (uint64 (r) << 48
+                    | uint64 (g) << 32 | uint64 (b) << 16 | uint64 (a));
+  }
+
+  ushort r ()
+  {
+    return rgba >> 48;
+  }
+  ushort g ()
+  {
+    return rgba >> 32;
+  }
+  ushort b ()
+  {
+    return rgba >> 16;
+  }
+  ushort a ()
+  {
+    return rgba;
+  }
+
+  void setG (ushort _g)
+  {
+    *this = fromRgba64 (r (), _g, b (), a ());
+  }
+};
+
+extern MyRgba64 giveMe64 ();
+
+MyRgba64
+test64 ()
+{
+  MyRgba64 a = giveMe64 ();
+  a.setG (0xf0f0);
+  return a;
+}
+
+/* { dg-final { scan-tree-dump-not "<<" "optimized" } } */
+/* { dg-final { scan-tree-dump-not ">>" "optimized" } } */
This page took 0.141447 seconds and 5 git commands to generate.