bagel99 / llvm-my66000

This is a fork of the LLVM project. The code in branch my66000 supports Mitch Alsup's MY66000. The code in branch mcore supports the Motorola MCore.
http://llvm.org
Other
2 stars 2 forks source link

Multiplying integers by 3, 5 or 9 #34

Closed tkoenig1 closed 1 year ago

tkoenig1 commented 1 year ago

Everybody's favorite micro-optimization. Probably saves some cycles, but, for My66000, no instructions.

unsigned int uitimes_3 (unsigned int a)
{
  return a * 3;
}

unsigned int uitimes_5 (unsigned int a)
{
  return a * 5;
}

unsigned int uitimes_9 (unsigned int a)
{
  return a * 9;
}

gets

        mul     r1,r1,#3
        srl     r1,r1,<32:0>
        ret

etc, but could use

        la      r1,[r1, r1<<1]
        srl     r1,r1,<32:0>
        ret

instead.

bagel99 commented 1 year ago

Do you know of any LLVM target that does this?

tkoenig1 commented 1 year ago

There is x86_64 https://godbolt.org/z/c1YavEqhj , ARM v8, where they use add+shift https://godbolt.org/z/j7TeoYexP . RISC-V uses shift and add, two instructions https://godbolt.org/z/67n4GnnP9 .

bagel99 commented 1 year ago

Try again with commit af7ee91ac91db0ac489c686eb35daf15ceb1f32f

tkoenig1 commented 1 year ago

Implemented, also works for signed.

Great!