For signed a, (~a) >> a is the same as ~(a>>a) which is ~0 aka -1.
Bootstrapped and tested on x86_64-pc-linux-gnu
PR tree-optimization/125707
gcc/ChangeLog:
PR tree-optimization/125707
* match.pd: Add (~a) >> a to -1 for signed a.
gcc/testsuite/ChangeLog:
PR tree-optimization/125707
* gcc.dg/pr125707.c: New test.
Signed-off-by: Kael Franco <kaelfandrew@gmail.com>
&& wi::to_wide (@1) != wi::min_value (TYPE_PRECISION (type),
SIGNED))
(minus (plus @1 { build_minus_one_cst (type); }) @0))))
+
+/* (~X) >> X -> -1 for signed X. */
+(simplify
+ (rshift (bit_not @0) (convert? @0))
+ (if (INTEGRAL_TYPE_P (type)
+ && !TYPE_UNSIGNED (type))
+ { build_minus_one_cst (type); }))
#endif
/* ~(X >> Y) -> ~X >> Y if ~X can be simplified. */
--- /dev/null
+/* { dg-do compile } */
+/* { dg-options "-O2 -fdump-tree-optimized" } */
+
+int
+rshift_bit_not_int_x_x (int x)
+{
+ return (~x) >> x;
+}
+
+long
+rshift_bit_not_long_x_x (long x)
+{
+ return (~x) >> x;
+}
+
+/* { dg-final { scan-tree-dump-times "return -1;" 2 "optimized" } } */