Save code space on ARM Thumb with PC-relative LDR
| 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.
...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.
...and 72MHz to run it on, I should add. ;)
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.
Guys, please check with me before making changes to Darwin. I'll comment on the patch in llvm-commits.
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.
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.
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!
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.
Probably you should also take care about Bill's code introduced in r142485...
Thanks!
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.
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 ?
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
James, just curious - how much is the impact of this patch on e.g. SPEC (or just gcc benchmark from SPEC)?
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
Hi James,
The patch is generally ok, but:
- Please handle darwin as well (pretty similar case)
- Please provide the testcase
I'll commit the stuff after 1/2 will be done.
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.
if LLVM were able, rather
assigned to @jmolloy