llvm-project icon indicating copy to clipboard operation
llvm-project copied to clipboard

Save code space on ARM Thumb with PC-relative LDR

Open llvmbot opened this issue 15 years ago • 19 comments

Bugzilla Link 11087
Resolution FIXED
Resolved on Oct 26, 2011 02:38
Version 2.9
OS Windows NT
Reporter LLVM Bugzilla Contributor
CC @asl,@jmolloy

Extended Description

It would be nice if LLVM was able to save space on ARM Thumb/Thumb2 by using PC-relative LDR, at least when set to optimize for size. The Keil compiler does this.

For example, instead of 8-byte MOVW/MOVT, what the Keil compiler will do is intersperse the 8-byte relocation with the code, and use a PC-relative LDR. This takes 6 bytes instead of 8. Trivial example from a boot loader (objdump output):

00001000 <__main-0x8>: 1000: 4800 ldr r0, [pc, #​0] (1004 <BuildAttributes$$gibberish+0x1004>) 1002: 4700 bx r0 1004: 0000100d .word 0x0000100d

The other nice effect of this is that it seems to search through your already-existing constants before introducing new 4-byte addresses. So often the 8-byte MOVW/MOVT becomes 2 bytes. Once, for example, I had a table of function pointers, and it just pulled the address from there as it was only a few hundred bytes away.

llvmbot avatar Oct 08 '11 02:10 llvmbot

...and 72MHz to run it on, I should add. ;)

I've reverted the patch for the moment while we argue about it on LLVM-commits.

jmolloy avatar Oct 26 '11 09:10 jmolloy

...and 72MHz to run it on, I should add. ;)

llvmbot avatar Oct 26 '11 09:10 llvmbot

Guys, please check with me before making changes to Darwin. I'll comment on the patch in llvm-commits.

Isn't "without sacrificing speed" just -O3? I don't understand, isn't that the entire point of specifying -Os, that you'd like to trade some speed? If not, wouldn't the optimization be in -O3?

Will this change stay for ELF, at least? This kind of thing is very important for embedded developers. Me, I've only got 4KB to work with in some cases.

llvmbot avatar Oct 26 '11 09:10 llvmbot

Guys, please check with me before making changes to Darwin. I'll comment on the patch in llvm-commits.

llvmbot avatar Oct 26 '11 03:10 llvmbot

Addendum, in Thumb mode (cortex-a8), there's a 4.9% code size improvement.

934640      14370     125588      15764      69808        128   thumb ToT / -O3
842002      14066     125810      15764      69776        128   thumb ToT / -Os
802688      50592     125804      15764      69776        128   thumb Patched / -Os

Again with a massive increase in literal pool size to boot. I may have a look at optimising literal pool usage.

jmolloy avatar Oct 19 '11 15:10 jmolloy

Yes. however, everything depends on how eh-heavy our code is. Maybe fixme for some checking is desired there.

Btw, your patch is ok provided that there will be size comparison of 403.gcc in the commit message.

Thanks for working on this!

Yep, makes sense. Here is the lowdown for gcc:

  Code (inc. data)   RO Data    RW Data    ZI Data      Debug   Object Name

1168400 67056 125804 15772 69776 128 Litpool Patch / -Os 1191488 30032 125804 15772 69776 128 ToT / -Os

1329000 31228 125588 15772 69808 128 Both / -O3

Everything is in bytes. This patch decreases codesize by around 1.9%, despite over doubling literal pool usage. I suspect that with some litpool optimisations (not duplicating literal pool entries as much) that could be reduced too.

Overall, even though it's only 2% that's 2% more than 0% so is worthwhile IMHO.

jmolloy avatar Oct 19 '11 15:10 jmolloy

Yes. however, everything depends on how eh-heavy our code is. Maybe fixme for some checking is desired there.

Btw, your patch is ok provided that there will be size comparison of 403.gcc in the commit message.

Thanks for working on this!

asl avatar Oct 19 '11 14:10 asl

Probably you should also take care about Bill's code introduced in r142485...

Hi Anton,

I'm not sure r142485 is relevant. Yes, it changes between using a constant pool and movw/movt, but just for landingpads so isn't on the critical path or emitted that often.

jmolloy avatar Oct 19 '11 14:10 jmolloy

Probably you should also take care about Bill's code introduced in r142485...

asl avatar Oct 19 '11 13:10 asl

Thanks!

asl avatar Oct 18 '11 21:10 asl

Yes, that's why I was curious. Do you have access to SPEC? If yes - will you please try 403.gcc with -Os vs -O2 / -O3 ?

Yeah, I'll give it a shot tomorrow.

jmolloy avatar Oct 18 '11 21:10 jmolloy

Yes, that's why I was curious. Do you have access to SPEC? If yes - will you please try 403.gcc with -Os vs -O2 / -O3 ?

asl avatar Oct 18 '11 20:10 asl

James, just curious - how much is the impact of this patch on e.g. SPEC (or just gcc benchmark from SPEC)?

Hi Anton,

I haven't run it. I'd expect only differences on -Os/-Oz, which noone really tests at the moment.

Cheers,

James

jmolloy avatar Oct 18 '11 20:10 jmolloy

James, just curious - how much is the impact of this patch on e.g. SPEC (or just gcc benchmark from SPEC)?

asl avatar Oct 18 '11 19:10 asl

Proposed patch mk.II - with testcase Hi Anton,

Thanks - nice catch w.r.t. the Darwin case.

Patch attached has a testcase and the Darwin fix. I can commit it myself if you approve.

Cheers,

James

jmolloy avatar Oct 18 '11 16:10 jmolloy

Hi James,

The patch is generally ok, but:

  1. Please handle darwin as well (pretty similar case)
  2. Please provide the testcase

I'll commit the stuff after 1/2 will be done.

asl avatar Oct 18 '11 09:10 asl

Proposed patch The use of MOVT/MOVW is an optimisation that is enabled all the time for V6T +. This patch disables the optimisation if the function is being compiled for size, resulting in a constant pool load.

jmolloy avatar Oct 17 '11 17:10 jmolloy

if LLVM were able, rather

llvmbot avatar Oct 08 '11 02:10 llvmbot

assigned to @jmolloy

llvmbot avatar Oct 08 '11 02:10 llvmbot