[llvm] 26c37b4 - [X86] Don't allow prologue stack probing with live EFLAGS

Josh Stone via llvm-commits llvm-commits at lists.llvm.org
Fri Sep 23 09:34:29 PDT 2022


Author: Josh Stone
Date: 2022-09-23T09:30:32-07:00
New Revision: 26c37b461a02815ccd5d0b4300823542ceeed54e

URL: https://github.com/llvm/llvm-project/commit/26c37b461a02815ccd5d0b4300823542ceeed54e
DIFF: https://github.com/llvm/llvm-project/commit/26c37b461a02815ccd5d0b4300823542ceeed54e.diff

LOG: [X86] Don't allow prologue stack probing with live EFLAGS

Fixes https://github.com/llvm/llvm-project/issues/49509

Differential Revision: https://reviews.llvm.org/D134494

Added: 
    llvm/test/CodeGen/X86/probe-stack-eflags.ll

Modified: 
    llvm/lib/Target/X86/X86FrameLowering.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Target/X86/X86FrameLowering.cpp b/llvm/lib/Target/X86/X86FrameLowering.cpp
index 0b24bf3be8c4c..fb78f9612e76f 100644
--- a/llvm/lib/Target/X86/X86FrameLowering.cpp
+++ b/llvm/lib/Target/X86/X86FrameLowering.cpp
@@ -3509,6 +3509,12 @@ bool X86FrameLowering::canUseAsPrologue(const MachineBasicBlock &MBB) const {
   if (!MBB.isLiveIn(X86::EFLAGS))
     return true;
 
+  // If stack probes have to loop inline or call, that will clobber EFLAGS.
+  const X86Subtarget &STI = MF.getSubtarget<X86Subtarget>();
+  const X86TargetLowering &TLI = *STI.getTargetLowering();
+  if (TLI.hasInlineStackProbe(MF) || TLI.hasStackProbeSymbol(MF))
+    return false;
+
   const X86MachineFunctionInfo *X86FI = MF.getInfo<X86MachineFunctionInfo>();
   return !TRI->hasStackRealignment(MF) && !X86FI->hasSwiftAsyncContext();
 }

diff  --git a/llvm/test/CodeGen/X86/probe-stack-eflags.ll b/llvm/test/CodeGen/X86/probe-stack-eflags.ll
new file mode 100644
index 0000000000000..c311c36eb9a4f
--- /dev/null
+++ b/llvm/test/CodeGen/X86/probe-stack-eflags.ll
@@ -0,0 +1,78 @@
+; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py
+; RUN: llc < %s | FileCheck %s
+
+target datalayout = "e-m:e-p270:32:32-p271:32:32-p272:64:64-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-unknown-linux-gnu"
+
+; https://github.com/llvm/llvm-project/issues/49509
+; Stack probes must not clobber live EFLAGS. Previously, the prologue was
+; inserted in %bb.2, with "subq $8, %rsp" clobbering the "sets %bl" flag.
+
+define i32 @f(i32 %a, i32 %b) #0 {
+; CHECK-LABEL: f:
+; CHECK:       # %bb.0: # %bb13.i
+; CHECK-NEXT:    pushq %rbp
+; CHECK-NEXT:    pushq %rbx
+; CHECK-NEXT:    subq $8, %rsp
+; CHECK-NEXT:    testl %edi, %edi
+; CHECK-NEXT:    je .LBB0_1
+; CHECK-NEXT:  # %bb.2: # %bb16.i
+; CHECK-NEXT:    sets %bl
+; CHECK-NEXT:    testl %esi, %esi
+; CHECK-NEXT:    sets %bpl
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:    callq *%rax
+; CHECK-NEXT:    movb $1, %al
+; CHECK-NEXT:    testb %al, %al
+; CHECK-NEXT:    xorb %bpl, %bl
+; CHECK-NEXT:    testb %al, %al
+; CHECK-NEXT:    xorb $1, %bl
+; CHECK-NEXT:    movzbl %bl, %eax
+; CHECK-NEXT:    jmp .LBB0_3
+; CHECK-NEXT:  .LBB0_1:
+; CHECK-NEXT:    xorl %eax, %eax
+; CHECK-NEXT:  .LBB0_3: # %exit2
+; CHECK-NEXT:    addq $8, %rsp
+; CHECK-NEXT:    popq %rbx
+; CHECK-NEXT:    popq %rbp
+; CHECK-NEXT:    retq
+bb13.i:
+  %0 = icmp eq i32 %a, 0
+  br i1 %0, label %exit2, label %bb16.i
+
+bb16.i:                                           ; preds = %bb13.i
+  %1 = icmp slt i32 %a, 0
+  %2 = icmp slt i32 %b, 0
+  %sign_z.i = xor i1 %1, %2
+  br label %exit1
+
+exit1: ; preds = %bb16.i
+  %3 = call { i64, i64 } null()
+  %4 = icmp eq i64 0, 0
+  br i1 %4, label %bb41.i, label %bb33.i
+
+bb33.i:                                           ; preds = %exit1
+  %rcarry.not.i = icmp ult i64 0, 0
+  br label %bb41.i
+
+bb41.i:                                           ; preds = %bb33.i, %exit1
+  br label %bb62.i
+
+bb62.i:                                           ; preds = %bb41.i
+  %_109.not.i = icmp eq i8 0, 0
+  br i1 %_109.not.i, label %bb70.i, label %bb64.i
+
+bb64.i:                                           ; preds = %bb62.i
+  %5 = icmp ne i64 0, 0
+  br label %bb70.i
+
+bb70.i:                                           ; preds = %bb64.i, %bb62.i
+  %spec.select.i.i = select i1 %sign_z.i, i32 0, i32 1
+  br label %exit2
+
+exit2: ; preds = %bb70.i, %bb13.i
+  %.1.i = phi i32 [ %spec.select.i.i, %bb70.i ], [ 0, %bb13.i ]
+  ret i32 %.1.i
+}
+
+attributes #0 = { nounwind "probe-stack"="inline-asm" }


        


More information about the llvm-commits mailing list