Skip to content

Commit 7669455

Browse files
committed
[X86][FastISel] Fix with.overflow eflags clobber (PR49587)
If the successor block has a phi node, then additional moves may be inserted into predecessors, which may clobber eflags. Don't try to fold the with.overflow result into the branch in that case. This is done by explicitly checking for any phis in successor blocks, not sure if there's some more principled way to address this. Other fused compare and branch patterns avoid the issue by emitting the comparison when handling the branch, so that no instructions may be inserted in between. In this case, the with.overflow call is emitted separately (and I don't think this is avoidable, as it will generally have at least two users). Fixes https://bugs.llvm.org/show_bug.cgi?id=49587. Differential Revision: https://reviews.llvm.org/D98600
1 parent bd8dd58 commit 7669455

File tree

2 files changed

+11
-2
lines changed

2 files changed

+11
-2
lines changed

llvm/lib/Target/X86/X86FastISel.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -284,6 +284,14 @@ bool X86FastISel::foldX86XALUIntrinsic(X86::CondCode &CC, const Instruction *I,
284284
return false;
285285
}
286286

287+
// Make sure no potentially eflags clobbering phi moves can be inserted in
288+
// between.
289+
auto HasPhis = [](const BasicBlock *Succ) {
290+
return !llvm::empty(Succ->phis());
291+
};
292+
if (I->isTerminator() && llvm::any_of(successors(I), HasPhis))
293+
return false;
294+
287295
CC = TmpCC;
288296
return true;
289297
}

llvm/test/CodeGen/X86/pr49587.ll

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,11 @@ define i32 @test(i64 %arg) nounwind {
55
; CHECK-LABEL: test:
66
; CHECK: # %bb.0: # %entry
77
; CHECK-NEXT: subq $1, %rdi
8-
; CHECK-NEXT: setb %al
8+
; CHECK-NEXT: setb %cl
99
; CHECK-NEXT: xorl %eax, %eax
10+
; CHECK-NEXT: testb $1, %cl
1011
; CHECK-NEXT: movl %eax, {{[-0-9]+}}(%r{{[sb]}}p) # 4-byte Spill
11-
; CHECK-NEXT: jb .LBB0_2
12+
; CHECK-NEXT: jne .LBB0_2
1213
; CHECK-NEXT: # %bb.1: # %no_overflow
1314
; CHECK-NEXT: movl $1, %eax
1415
; CHECK-NEXT: movl %eax, {{[-0-9]+}}(%r{{[sb]}}p) # 4-byte Spill

0 commit comments

Comments
 (0)