Bootstrapped and tested on x86_64-unknown-linux-gnu, applied. Richard.
2015-06-09 Richard Biener <rguent...@suse.de> PR middle-end/66423 * match.pd: Handle A % (unsigned)(1 << B). * gcc.dg/fold-modpow2.c: New testcase. Index: gcc/match.pd =================================================================== *** gcc/match.pd (revision 224271) --- gcc/match.pd (working copy) *************** (define_operator_list swapped_tcc_compar *** 248,258 **** (lshift INTEGER_CST@1 @2)) (for mod (trunc_mod floor_mod) (simplify ! (mod @0 (power_of_two_cand@1 @2)) (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) ! (bit_and @0 (minus @1 { build_int_cst (TREE_TYPE (@1), 1); }))))) /* X % Y is smaller than Y. */ (for cmp (lt ge) --- 248,259 ---- (lshift INTEGER_CST@1 @2)) (for mod (trunc_mod floor_mod) (simplify ! (mod @0 (convert?@3 (power_of_two_cand@1 @2))) (if ((TYPE_UNSIGNED (type) || tree_expr_nonnegative_p (@0)) + && tree_nop_conversion_p (type, TREE_TYPE (@3)) && integer_pow2p (@2) && tree_int_cst_sgn (@2) > 0) ! (bit_and @0 (convert (minus @1 { build_int_cst (TREE_TYPE (@1), 1); })))))) /* X % Y is smaller than Y. */ (for cmp (lt ge) Index: gcc/testsuite/gcc.dg/fold-modpow2.c =================================================================== *** gcc/testsuite/gcc.dg/fold-modpow2.c (revision 0) --- gcc/testsuite/gcc.dg/fold-modpow2.c (working copy) *************** *** 0 **** --- 1,11 ---- + /* { dg-do compile } */ + /* { dg-options "-fdump-tree-original" } */ + + unsigned int + my_mod (unsigned int a, unsigned int b) + { + return a % (1 << b); + } + + /* The above should be simplified to (unsigned int) ((1 << b) + -1) & a */ + /* { dg-final { scan-tree-dump "& a;" "original" } } */