Multiplying by a constant
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Multiplying by a constant



This is just an idea for an improvement in the ColdFire backend.

When multiplying by a constant, the compiler generates a series of shifts and adds (or subs) instead of using the appropriate "mul" instruction. While this is optimal when the shift and add combination is small, it is sub-optimal if more than 3 instructions are needed (multiplies take 4 clocks on a v2 Coldfire). This is a hang-over from the pre-Coldfire m68k cores which had very slow multiplies.

Test code:

extern unsigned int x, y;
void foo(void) {
  x = y * 11;
}

Generated code (-O2 -Wa,-ahlsd -m528x):

  92               	foo:
  93 008e 4E56 0000 		link.w %fp,#0
  94 0092 2079 0000 		move.l y,%a0	| y, y.6
  94      0000
  95 0098 2008      		move.l %a0,%d0	| y.6, y.6
  96 009a 2200      		move.l %d0,%d1	| y.6, tmp33
  97 009c E589      		lsl.l #2,%d1	|, tmp33
  98 009e 2001      		move.l %d1,%d0	| tmp33, tmp34
  99 00a0 E588      		lsl.l #2,%d0	|, tmp34
 100 00a2 9081      		sub.l %d1,%d0	| tmp33, tmp34
 101 00a4 9088      		sub.l %a0,%d0	| y.6, D.1159
 102 00a6 23C0 0000 		move.l %d0,x	| D.1159, x
 102      0000
 103 00ac 4E5E      		unlk %fp
 104 00ae 4E75      		rts


mvh.,

David