[Thumb-1] Synthesize TBB/TBH instructions to make use of compressed jump tables
authorJames Molloy <james.molloy@arm.com>
Wed, 19 Oct 2016 12:06:49 +0000 (12:06 +0000)
committerJames Molloy <james.molloy@arm.com>
Wed, 19 Oct 2016 12:06:49 +0000 (12:06 +0000)
commitfbfd173447b64d9d73c44cf47bafeb68e8139e5a
tree8fd1aa19098e83b1170e6b7d245cde554dcbcc3d
parent9941ca8af6b4c39fd0b9e47dc7e593d884b55710
[Thumb-1] Synthesize TBB/TBH instructions to make use of compressed jump tables

The TBB and TBH instructions in Thumb-2 allow jump tables to be compressed into sequences of bytes or shorts respectively. These instructions do not exist in Thumb-1, however it is possible to synthesize them out of a sequence of other instructions.

It turns out this sequence is so short that it's almost never a lose for performance and is ALWAYS a significant win for code size.

TBB example:
Before: lsls r0, r0, #2    After: add  r0, pc
        adr  r1, .LJTI0_0         ldrb r0, [r0, #6]
        ldr  r0, [r0, r1]         lsls r0, r0, #1
        mov  pc, r0               add  pc, r0
  => No change in prologue code size or dynamic instruction count. Jump table shrunk by a factor of 4.

The only case that can increase dynamic instruction count is the TBH case:

Before: lsls r0, r4, #2    After: lsls r4, r4, #1
        adr  r1, .LJTI0_0         add  r4, pc
        ldr  r0, [r0, r1]         ldrh r4, [r4, #6]
        mov  pc, r0               lsls r4, r4, #1
                                  add  pc, r4
  => 1 more instruction in prologue. Jump table shrunk by a factor of 2.

So there is an argument that this should be disabled when optimizing for performance (and a TBH needs to be generated). I'm not so sure about that in practice, because on small cores with Thumb-1 performance is often tied to code size. But I'm willing to turn it off when optimizing for performance if people want (also note that TBHs are fairly rare in practice!)

llvm-svn: 284580
llvm/lib/Target/ARM/ARMAsmPrinter.cpp
llvm/lib/Target/ARM/ARMConstantIslandPass.cpp
llvm/lib/Target/ARM/ARMInstrThumb.td
llvm/test/CodeGen/ARM/arm-position-independence-jump-table.ll
llvm/test/CodeGen/ARM/jump-table-tbh.ll [new file with mode: 0644]
llvm/test/CodeGen/Thumb2/thumb2-jtb.ll
llvm/test/CodeGen/Thumb2/thumb2-tbb.ll
llvm/test/CodeGen/Thumb2/thumb2-tbh.ll