--- a/gcc/match.pd +++ b/gcc/match.pd @@ -529,6 +529,16 @@ along with GCC; see the file COPYING3. If not see (bitop (bit_and:c @0 @1) (bit_and @2 @1)) (bit_and (bitop @0 @2) @1))) +/* Simplify (unsigned t * 2)/2 -> unsigned t & 0x7FFFFFFF. */ +(for div (trunc_div ceil_div floor_div round_div exact_div) + (simplify + (div (mult @0 INTEGER_CST@1) INTEGER_CST@1) + (with { tree n2 = build_int_cst (TREE_TYPE (@0), + wi::exact_log2 (@1)); } + (if (TYPE_UNSIGNED (TREE_TYPE (@0))) + (bit_and @0 (rshift (lshift { build_minus_one_cst (TREE_TYPE (@0)); } + { n2; }) { n2; })))))) + /* (x | CST1) & CST2 -> (x & CST2) | (CST1 & CST2) */ (simplify (bit_and (bit_ior @0 CONSTANT_CLASS_P@1) CONSTANT_CLASS_P@2) --- /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" } } */