Skip to content

Commit 3d63ce2

Browse files
serge-sans-pailletstellar
serge-sans-paille
authored andcommitted
[stack-clash] Fix probing of dynamic alloca
- Perform the probing in the correct direction. Related to rust-lang/rust#77885 (comment) - The first touch on a dynamic alloca cannot use a mov because it clobbers existing space. Use a xor 0 instead Differential Revision: https://reviews.llvm.org/D90216 (cherry picked from commit 0f60bcc)
1 parent fcd757d commit 3d63ce2

File tree

3 files changed

+13
-13
lines changed

3 files changed

+13
-13
lines changed

llvm/lib/Target/X86/X86ISelLowering.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -31876,7 +31876,7 @@ X86TargetLowering::EmitLoweredProbedAlloca(MachineInstr &MI,
3187631876

3187731877
BuildMI(testMBB, DL, TII->get(X86::JCC_1))
3187831878
.addMBB(tailMBB)
31879-
.addImm(X86::COND_LE);
31879+
.addImm(X86::COND_GE);
3188031880
testMBB->addSuccessor(blockMBB);
3188131881
testMBB->addSuccessor(tailMBB);
3188231882

@@ -31892,9 +31892,9 @@ X86TargetLowering::EmitLoweredProbedAlloca(MachineInstr &MI,
3189231892
//
3189331893
// The property we want to enforce is to never have more than [page alloc] between two probes.
3189431894

31895-
const unsigned MovMIOpc =
31896-
TFI.Uses64BitFramePtr ? X86::MOV64mi32 : X86::MOV32mi;
31897-
addRegOffset(BuildMI(blockMBB, DL, TII->get(MovMIOpc)), physSPReg, false, 0)
31895+
const unsigned XORMIOpc =
31896+
TFI.Uses64BitFramePtr ? X86::XOR64mi8 : X86::XOR32mi8;
31897+
addRegOffset(BuildMI(blockMBB, DL, TII->get(XORMIOpc)), physSPReg, false, 0)
3189831898
.addImm(0);
3189931899

3190031900
BuildMI(blockMBB, DL,

llvm/test/CodeGen/X86/stack-clash-dynamic-alloca.ll

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@ attributes #0 = {"probe-stack"="inline-asm"}
2424
; CHECK-X86-64-NEXT: andq $-16, %rcx
2525
; CHECK-X86-64-NEXT: subq %rcx, %rax
2626
; CHECK-X86-64-NEXT: cmpq %rsp, %rax
27-
; CHECK-X86-64-NEXT: jle .LBB0_3
27+
; CHECK-X86-64-NEXT: jge .LBB0_3
2828
; CHECK-X86-64-NEXT: .LBB0_2: # =>This Inner Loop Header: Depth=1
29-
; CHECK-X86-64-NEXT: movq $0, (%rsp)
29+
; CHECK-X86-64-NEXT: xorq $0, (%rsp)
3030
; CHECK-X86-64-NEXT: subq $4096, %rsp # imm = 0x1000
3131
; CHECK-X86-64-NEXT: cmpq %rsp, %rax
32-
; CHECK-X86-64-NEXT: jg .LBB0_2
32+
; CHECK-X86-64-NEXT: jl .LBB0_2
3333
; CHECK-X86-64-NEXT: .LBB0_3:
3434
; CHECK-X86-64-NEXT: movq %rax, %rsp
3535
; CHECK-X86-64-NEXT: movl $1, 4792(%rax)
@@ -54,12 +54,12 @@ attributes #0 = {"probe-stack"="inline-asm"}
5454
; CHECK-X86-32-NEXT: andl $-16, %ecx
5555
; CHECK-X86-32-NEXT: subl %ecx, %eax
5656
; CHECK-X86-32-NEXT: cmpl %esp, %eax
57-
; CHECK-X86-32-NEXT: jle .LBB0_3
57+
; CHECK-X86-32-NEXT: jge .LBB0_3
5858
; CHECK-X86-32-NEXT: .LBB0_2: # =>This Inner Loop Header: Depth=1
59-
; CHECK-X86-32-NEXT: movl $0, (%esp)
59+
; CHECK-X86-32-NEXT: xorl $0, (%esp)
6060
; CHECK-X86-32-NEXT: subl $4096, %esp # imm = 0x1000
6161
; CHECK-X86-32-NEXT: cmpl %esp, %eax
62-
; CHECK-X86-32-NEXT: jg .LBB0_2
62+
; CHECK-X86-32-NEXT: jl .LBB0_2
6363
; CHECK-X86-32-NEXT: .LBB0_3:
6464
; CHECK-X86-32-NEXT: movl %eax, %esp
6565
; CHECK-X86-32-NEXT: movl $1, 4792(%eax)

llvm/test/CodeGen/X86/stack-clash-small-alloc-medium-align.ll

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -106,12 +106,12 @@ define i32 @foo4(i64 %i) local_unnamed_addr #0 {
106106
; CHECK-NEXT: andq $-16, %rcx
107107
; CHECK-NEXT: subq %rcx, %rax
108108
; CHECK-NEXT: cmpq %rsp, %rax
109-
; CHECK-NEXT: jle .LBB3_3
109+
; CHECK-NEXT: jge .LBB3_3
110110
; CHECK-NEXT:.LBB3_2: # =>This Inner Loop Header: Depth=1
111-
; CHECK-NEXT: movq $0, (%rsp)
111+
; CHECK-NEXT: xorq $0, (%rsp)
112112
; CHECK-NEXT: subq $4096, %rsp # imm = 0x1000
113113
; CHECK-NEXT: cmpq %rsp, %rax
114-
; CHECK-NEXT: jg .LBB3_2
114+
; CHECK-NEXT: jl .LBB3_2
115115
; CHECK-NEXT:.LBB3_3:
116116
; CHECK-NEXT: andq $-64, %rax
117117
; CHECK-NEXT: movq %rax, %rsp

0 commit comments

Comments
 (0)