[llvm] 2e47b93 - [ARM] Honour -mno-movt in stack protector handling (#109022)

via llvm-commits llvm-commits at lists.llvm.org
Wed Oct 9 09:34:24 PDT 2024


Author: Ard Biesheuvel
Date: 2024-10-09T09:34:17-07:00
New Revision: 2e47b93fd29ad6ef13a4134f3b0be3c42e91180c

URL: https://github.com/llvm/llvm-project/commit/2e47b93fd29ad6ef13a4134f3b0be3c42e91180c
DIFF: https://github.com/llvm/llvm-project/commit/2e47b93fd29ad6ef13a4134f3b0be3c42e91180c.diff

LOG: [ARM] Honour -mno-movt in stack protector handling (#109022)

When -mno-movt is passed to Clang, the ARM codegen correctly avoids
movt/movw pairs to take the address of __stack_chk_guard in the stack
protector code emitted into the function pro- and epilogues. However,
the Thumb2 codegen fails to do so, and happily emits movw/movt pairs
unless it is generating an ELF binary and the symbol might be in a
different DSO. Let's incorporate a check for useMovt() in the logic
here, so movt/movw are never emitted when -mno-movt is specified.

Suggestions welcome for how/where to add a test case for this.

Signed-off-by: Ard Biesheuvel <ardb at kernel.org>

Added: 
    llvm/test/CodeGen/ARM/stack-guard-nomovt.ll

Modified: 
    llvm/lib/Target/ARM/Thumb2InstrInfo.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp b/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
index d1e07b6703a5e6..27f86389a3856a 100644
--- a/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
+++ b/llvm/lib/Target/ARM/Thumb2InstrInfo.cpp
@@ -264,8 +264,11 @@ void Thumb2InstrInfo::expandLoadStackGuard(
   }
 
   const auto *GV = cast<GlobalValue>((*MI->memoperands_begin())->getValue());
-  if (MF.getSubtarget<ARMSubtarget>().isTargetELF() && !GV->isDSOLocal())
+  const ARMSubtarget &Subtarget = MF.getSubtarget<ARMSubtarget>();
+  if (Subtarget.isTargetELF() && !GV->isDSOLocal())
     expandLoadStackGuardBase(MI, ARM::t2LDRLIT_ga_pcrel, ARM::t2LDRi12);
+  else if (!Subtarget.useMovt())
+    expandLoadStackGuardBase(MI, ARM::tLDRLIT_ga_abs, ARM::t2LDRi12);
   else if (MF.getTarget().isPositionIndependent())
     expandLoadStackGuardBase(MI, ARM::t2MOV_ga_pcrel, ARM::t2LDRi12);
   else

diff  --git a/llvm/test/CodeGen/ARM/stack-guard-nomovt.ll b/llvm/test/CodeGen/ARM/stack-guard-nomovt.ll
new file mode 100644
index 00000000000000..6802dabfda87a6
--- /dev/null
+++ b/llvm/test/CodeGen/ARM/stack-guard-nomovt.ll
@@ -0,0 +1,32 @@
+; RUN: llc -relocation-model=static -mattr=+no-movt < %s | FileCheck %s
+
+target triple = "thumbv7a-linux-gnueabi"
+
+define i32 @test1() #0 {
+; CHECK-LABEL: test1:
+; CHECK:       @ %bb.0:
+; CHECK-NEXT:    push	{r7, lr}
+; CHECK-NEXT:    sub.w	sp, sp, #1032
+; CHECK-NEXT:    ldr	r0, .LCPI0_0
+; CHECK-NEXT:    ldr	r0, [r0]
+; CHECK-NEXT:    str.w	r0, [sp, #1028]
+; CHECK-NEXT:    add	r0, sp, #4
+; CHECK-NEXT:    bl	foo
+; CHECK-NEXT:    ldr.w	r0, [sp, #1028]
+; CHECK-NEXT:    ldr	r1, .LCPI0_0
+; CHECK-NEXT:    ldr	r1, [r1]
+; CHECK-NEXT:    cmp	r1, r0
+; CHECK-NEXT:    ittt	eq
+; CHECK-NEXT:    moveq	r0, #0
+; CHECK-NEXT:    addeq.w	sp, sp, #1032
+; CHECK-NEXT:    popeq	{r7, pc}
+; CHECK-NEXT:  .LBB0_1:
+; CHECK-NEXT:    bl __stack_chk_fail
+  %a1 = alloca [256 x i32], align 4
+  call void @foo(ptr %a1) #3
+  ret i32 0
+}
+
+declare void @foo(ptr)
+
+attributes #0 = { nounwind sspstrong }


        


More information about the llvm-commits mailing list