From dc1a030aecf718a5b8b7811507e0f15f7e225e4f Mon Sep 17 00:00:00 2001 From: Peter Collingbourne Date: Fri, 1 May 2015 18:05:59 +0000 Subject: [PATCH] ARM: Align functions containing Thumb-2 jump tables to 4 bytes. Functions with jump tables need an alignment of 4 because they use the ADR instruction, which aligns the PC to 4 bytes before adding an offset. Differential Revision: http://reviews.llvm.org/D9424 git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@236327 91177308-0d34-0410-b5e6-96231b3b80d8 --- lib/Target/ARM/ARMConstantIslandPass.cpp | 4 +++ test/CodeGen/ARM/thumb-alignment.ll | 39 ++++++++++++++++++++++++ 2 files changed, 43 insertions(+) diff --git a/lib/Target/ARM/ARMConstantIslandPass.cpp b/lib/Target/ARM/ARMConstantIslandPass.cpp index e6d4b14a262..0a47400c490 100644 --- a/lib/Target/ARM/ARMConstantIslandPass.cpp +++ b/lib/Target/ARM/ARMConstantIslandPass.cpp @@ -423,6 +423,10 @@ bool ARMConstantIslands::runOnMachineFunction(MachineFunction &mf) { CPEMIs.clear(); DEBUG(dumpBBs()); + // Functions with jump tables need an alignment of 4 because they use the ADR + // instruction, which aligns the PC to 4 bytes before adding an offset. + if (!T2JumpTables.empty()) + MF->ensureAlignment(2); /// Remove dead constant pool entries. MadeChange |= removeUnusedCPEntries(); diff --git a/test/CodeGen/ARM/thumb-alignment.ll b/test/CodeGen/ARM/thumb-alignment.ll index d2565098798..c11d4b6da3c 100644 --- a/test/CodeGen/ARM/thumb-alignment.ll +++ b/test/CodeGen/ARM/thumb-alignment.ll @@ -13,3 +13,42 @@ define i32* @foo() { define i32* @bar() { ret i32* zeroinitializer } + +@a = external global i32 +@b = external global i32 +@c = external global i32 +@d = external global i32 + +; Create a Thumb-2 jump table, which should force alignment to 4 bytes. + +; CHECK: .globl baz +; CHECK-NEXT: .align 2 +; CHECK: adr.w +define i32 @baz() { + %1 = load i32, i32* @c, align 4 + switch i32 %1, label %7 [ + i32 1, label %2 + i32 4, label %5 + i32 9, label %5 + i32 3, label %8 + ] + +;