2015-02-26 Jakub Jelinek Aldy Hernandez PR rtl-optimization/65220 * config/i386/i386.md (*udivmod4_pow2): New. --- gcc/config/i386/i386.md (revision 221063) +++ gcc/config/i386/i386.md (revision 221064) @@ -7331,6 +7331,32 @@ (define_insn_and_split "*udivmod4" [(set_attr "type" "multi") (set_attr "mode" "")]) +;; Optimize division or modulo by constant power of 2, if the constant +;; materializes only after expansion. +(define_insn_and_split "*udivmod4_pow2" + [(set (match_operand:SWI48 0 "register_operand" "=r") + (udiv:SWI48 (match_operand:SWI48 2 "register_operand" "0") + (match_operand:SWI48 3 "const_int_operand" "n"))) + (set (match_operand:SWI48 1 "register_operand" "=r") + (umod:SWI48 (match_dup 2) (match_dup 3))) + (clobber (reg:CC FLAGS_REG))] + "UINTVAL (operands[3]) - 2 < * BITS_PER_UNIT + && (UINTVAL (operands[3]) & (UINTVAL (operands[3]) - 1)) == 0" + "#" + "&& reload_completed" + [(set (match_dup 1) (match_dup 2)) + (parallel [(set (match_dup 0) (lshiftrt: (match_dup 2) (match_dup 4))) + (clobber (reg:CC FLAGS_REG))]) + (parallel [(set (match_dup 1) (and: (match_dup 1) (match_dup 5))) + (clobber (reg:CC FLAGS_REG))])] +{ + int v = exact_log2 (UINTVAL (operands[3])); + operands[4] = GEN_INT (v); + operands[5] = GEN_INT ((HOST_WIDE_INT_1U << v) - 1); +} + [(set_attr "type" "multi") + (set_attr "mode" "")]) + (define_insn "*udivmod4_noext" [(set (match_operand:SWIM248 0 "register_operand" "=a") (udiv:SWIM248 (match_operand:SWIM248 2 "register_operand" "0") --- gcc/testsuite/gcc.target/i386/pr65520.c (revision 0) +++ gcc/testsuite/gcc.target/i386/pr65520.c (revision 221064) @@ -0,0 +1,20 @@ +/* PR target/65520 */ +/* { dg-do compile } */ +/* { dg-options "-O2" } */ + +int foo (void *); + +void +bar (void) +{ + unsigned s = 128; + while (1) + { + unsigned b[s]; + if (foo (b)) + break; + s *= 2; + } +} + +/* { dg-final { scan-assembler-not "div\[^\n\r]*%" } } */