--- a/gcc/match.pd +++ b/gcc/match.pd @@ -280,6 +280,20 @@ along with GCC; see the file COPYING3. If not see && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) +/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ +(simplify + (exact_div (mult @0 INTEGER_CST@1) @1) + (if (TYPE_OVERFLOW_UNDEFINED (TREE_TYPE (@0))) + @0)) + +(simplify + (trunc_div (mult @0 integer_pow2p@1) @1) + (if (TYPE_UNSIGNED (TREE_TYPE (@0))) + (with { tree n2 = build_int_cst (TREE_TYPE (@0), + wi::exact_log2 (@1)); } + (bit_and @0 (rshift (lshift { build_minus_one_cst (TREE_TYPE (@0)); } + { n2; }) { n2; }))))) + /* X % Y is smaller than Y. */ (for cmp (lt ge) (simplify new file mode 100644 --- /dev/null +++ b/gcc/testsuite/gcc.dg/pr25529.c @@ -0,0 +1,10 @@ +/* { dg-do compile } */ +/* { dg-options "-O2 -fdump-tree-optimized" } */ + +int +f1 (unsigned t) +{ + return (t * 2) / 2; +} + +/* { dg-final { scan-tree-dump "\& 2147483647" "optimized" } } */