[Thumb-1] Synthesize TBB/TBH instructions to make use of compressed jump tables
authorJames Molloy <james.molloy@arm.com>
Tue, 1 Nov 2016 13:37:41 +0000 (13:37 +0000)
committerJames Molloy <james.molloy@arm.com>
Tue, 1 Nov 2016 13:37:41 +0000 (13:37 +0000)
commit70a3d6df52d9d6cf7e11a95177b33eec1579dc45
tree287e2229ac6ecad4952ee4d35218610c8be15dc6
parent399a50cf356f4881cbab46a95d2c9efed18454ba
[Thumb-1] Synthesize TBB/TBH instructions to make use of compressed jump tables

[Reapplying r284580 and r285917 with fix and testing to ensure emitted jump tables for Thumb-1 have 4-byte alignment]

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: 285690
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/constant-island-crash.ll [new file with mode: 0644]
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