public inbox for gcc-bugs@sourceware.org
help / color / mirror / Atom feed
* [Bug target/42258]  New: redundant register move around mul instruction
@ 2009-12-03  3:27 carrot at google dot com
  2009-12-09  2:27 ` [Bug regression/42258] " carrot at google dot com
                   ` (10 more replies)
  0 siblings, 11 replies; 12+ messages in thread
From: carrot at google dot com @ 2009-12-03  3:27 UTC (permalink / raw)
  To: gcc-bugs

Compile following function with options -march=armv5te -mthumb -Os

int mulx(int x)
{
  return x*42;
}

Gcc generates:

mulx:
        mov     r2, #42
        mov     r3, r2    //A
        mul     r3, r0
        @ sp needed for prologue
        mov     r0, r3    //B
        bx      lr

There are two redundant mov instructions marked as A and B. The ideal code
should be:

    mov r2, #42
    mul r0, r2
    bx  lr

The root causes of the two movs are different.

A. 
In arm.md there is an insn pattern which can support 3-register multiplication.

(define_insn "*thumb_mulsi3"
  [(set (match_operand:SI          0 "register_operand" "=&l,&l,&l")
        (mult:SI (match_operand:SI 1 "register_operand" "%l,*h,0")
                 (match_operand:SI 2 "register_operand" "l,l,l")))]
  "TARGET_THUMB1 && !arm_arch6"
  "*
  if (which_alternative < 2)
    return \"mov\\t%0, %1\;mul\\t%0, %2\";
  else
    return \"mul\\t%0, %2\";
  "
  [(set_attr "length" "4,4,2")
   (set_attr "insn" "mul")]
)

So after expand we got the following rtx insn:

(insn 7 6 8 3 testD.i:2 (set (reg:SI 136)
        (mult:SI (reg:SI 137)
            (reg/v:SI 135 [ x ]))) -1 (nil))

For register allocator there is no difference between (set r3 mult(r0, r2)) 
and   (set r2 mult(r0, r2))   if the original r2 is dead after this
instruction. Unfortunately the first form is chosen, so an extra mov is
generated.

B.
After rtl expansion I got:

(insn 2 4 3 2 testD.i:2 (set (reg/v:SI 135 [ x ])
        (reg:SI 0 r0 [ x ])) -1 (nil))

(note 3 2 5 2 NOTE_INSN_FUNCTION_BEG)

(note 5 3 6 3 [bb 3] NOTE_INSN_BASIC_BLOCK)

(insn 6 5 7 3 testD.i:2 (set (reg:SI 137)
        (const_int 42 [0x2a])) -1 (nil))

(insn 7 6 8 3 testD.i:2 (set (reg:SI 136)
        (mult:SI (reg:SI 137)
            (reg/v:SI 135 [ x ]))) -1 (nil))

(insn 8 7 9 3 testD.i:2 (set (reg:SI 134 [ <retval> ])
        (reg:SI 136)) -1 (nil))

(jump_insn 9 8 10 3 testD.i:2 (set (pc)
        (label_ref 11)) -1 (nil)
 -> 11)

(barrier 10 9 16)

(note 16 10 13 4 [bb 4] NOTE_INSN_BASIC_BLOCK)

(insn 13 16 14 4 testD.i:4 (clobber (reg/i:SI 0 r0)) -1 (nil))

(insn 14 13 11 4 testD.i:4 (clobber (reg:SI 134 [ <retval> ])) -1 (nil))

(code_label 11 14 17 5 1 "" [1 uses])

(note 17 11 12 5 [bb 5] NOTE_INSN_BASIC_BLOCK)

(insn 12 17 15 5 testD.i:4 (set (reg/i:SI 0 r0)
        (reg:SI 134 [ <retval> ])) -1 (nil))

(insn 15 12 0 5 testD.i:4 (use (reg/i:SI 0 r0)) -1 (nil))

The extra mov comes from insn 12 and no later pass can eliminate it. Should we
propagate expression into return register?


-- 
           Summary: redundant register move around mul instruction
           Product: gcc
           Version: 4.5.0
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: target
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: carrot at google dot com
 GCC build triplet: i686-linux
  GCC host triplet: i686-linux
GCC target triplet: arm-eabi


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42258


^ permalink raw reply	[flat|nested] 12+ messages in thread

end of thread, other threads:[~2010-03-20 12:58 UTC | newest]

Thread overview: 12+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2009-12-03  3:27 [Bug target/42258] New: redundant register move around mul instruction carrot at google dot com
2009-12-09  2:27 ` [Bug regression/42258] " carrot at google dot com
2009-12-09 16:12 ` ramana at gcc dot gnu dot org
2009-12-30 23:14 ` [Bug rtl-optimization/42258] [4.5 Regression] " rguenth at gcc dot gnu dot org
2009-12-31  9:33 ` steven at gcc dot gnu dot org
2009-12-31 11:11 ` rguenth at gcc dot gnu dot org
2010-01-02 15:58 ` rguenth at gcc dot gnu dot org
2010-03-17 11:05 ` bernds at codesourcery dot com
2010-03-17 11:44 ` bernds at codesourcery dot com
2010-03-17 11:59 ` steven at gcc dot gnu dot org
2010-03-19 18:19 ` bernds at gcc dot gnu dot org
2010-03-20 12:58 ` steven at gcc dot gnu dot org

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).