Skip to content

Commit cdab6ff

Browse files
authored
[X86] Don't save/restore fp/bp around terminator (#106462)
In function spillFPBP we already try to skip terminator, but there is a logic error, so when there is only terminator instruction in the MBB, it still tries to save/restore fp/bp around it if the terminator clobbers fp/bp, for example a tail call with ghc calling convention. Now this patch really skips terminator even if it is the only instruction in the MBB.
1 parent 319e8cd commit cdab6ff

File tree

2 files changed

+46
-2
lines changed

2 files changed

+46
-2
lines changed

llvm/lib/Target/X86/X86FrameLowering.cpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4509,8 +4509,9 @@ void X86FrameLowering::spillFPBP(MachineFunction &MF) const {
45094509
bool InsideEHLabels = false;
45104510
auto MI = MBB.rbegin(), ME = MBB.rend();
45114511
auto TermMI = MBB.getFirstTerminator();
4512-
if (TermMI != MBB.begin())
4513-
MI = *(std::prev(TermMI));
4512+
if (TermMI == MBB.begin())
4513+
continue;
4514+
MI = *(std::prev(TermMI));
45144515

45154516
while (MI != ME) {
45164517
// Skip frame setup/destroy instructions.

llvm/test/CodeGen/X86/clobber_frame_ptr.ll

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,3 +144,46 @@ entry:
144144
call void @llvm.eh.sjlj.longjmp(ptr @buf)
145145
unreachable
146146
}
147+
148+
declare ghccc void @tail()
149+
150+
; We should not save/restore fp/bp around terminator.
151+
define ghccc void @test5() {
152+
; CHECK-LABEL: test5:
153+
; CHECK: # %bb.0: # %entry
154+
; CHECK-NEXT: pushq %rbp
155+
; CHECK-NEXT: .cfi_def_cfa_offset 16
156+
; CHECK-NEXT: .cfi_offset %rbp, -16
157+
; CHECK-NEXT: movq %rsp, %rbp
158+
; CHECK-NEXT: .cfi_def_cfa_register %rbp
159+
; CHECK-NEXT: andq $-8, %rsp
160+
; CHECK-NEXT: xorl %eax, %eax
161+
; CHECK-NEXT: testb %al, %al
162+
; CHECK-NEXT: jne .LBB3_2
163+
; CHECK-NEXT: # %bb.1: # %then
164+
; CHECK-NEXT: movq $0, (%rax)
165+
; CHECK-NEXT: movq %rbp, %rsp
166+
; CHECK-NEXT: popq %rbp
167+
; CHECK-NEXT: .cfi_def_cfa %rsp, 8
168+
; CHECK-NEXT: retq
169+
; CHECK-NEXT: .LBB3_2: # %else
170+
; CHECK-NEXT: .cfi_def_cfa %rbp, 16
171+
; CHECK-NEXT: movq %rbp, %rsp
172+
; CHECK-NEXT: popq %rbp
173+
; CHECK-NEXT: .cfi_def_cfa %rsp, 8
174+
; CHECK-NEXT: jmp tail@PLT # TAILCALL
175+
entry:
176+
br i1 undef, label %then, label %else
177+
178+
then:
179+
store i64 0, ptr undef
180+
br label %exit
181+
182+
else:
183+
musttail call ghccc void @tail()
184+
ret void
185+
186+
exit:
187+
ret void
188+
}
189+

0 commit comments

Comments
 (0)