Skip to content

Commit 09bdbfd

Browse files
authored
[CodeGenPrepare] Bail out of usubo creation if sub's parent is not the same as the comparison (#160358)
We match uadd's behavior here. Codegen comparison: https://godbolt.org/z/x8j4EhGno
1 parent 373a2f1 commit 09bdbfd

File tree

2 files changed

+11
-5
lines changed

2 files changed

+11
-5
lines changed

llvm/lib/CodeGen/CodeGenPrepare.cpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1749,6 +1749,12 @@ bool CodeGenPrepare::combineToUSubWithOverflow(CmpInst *Cmp,
17491749
Sub->hasNUsesOrMore(1)))
17501750
return false;
17511751

1752+
// We don't want to move around uses of condition values this late, so we
1753+
// check if it is legal to create the call to the intrinsic in the basic
1754+
// block containing the icmp.
1755+
if (Sub->getParent() != Cmp->getParent() && !Sub->hasOneUse())
1756+
return false;
1757+
17521758
if (!replaceMathCmpWithIntrinsic(Sub, Sub->getOperand(0), Sub->getOperand(1),
17531759
Cmp, Intrinsic::usub_with_overflow))
17541760
return false;

llvm/test/CodeGen/X86/usub_inc_iv.ll

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -303,14 +303,14 @@ define i32 @test_06(ptr %p, i64 %len, i32 %x) {
303303
; CHECK-NEXT: br label [[LOOP:%.*]]
304304
; CHECK: loop:
305305
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[MATH:%.*]], [[BACKEDGE:%.*]] ], [ [[LEN:%.*]], [[ENTRY:%.*]] ]
306-
; CHECK-NEXT: [[TMP0:%.*]] = call { i64, i1 } @llvm.usub.with.overflow.i64(i64 [[IV]], i64 1)
307-
; CHECK-NEXT: [[MATH]] = extractvalue { i64, i1 } [[TMP0]], 0
308-
; CHECK-NEXT: [[OV:%.*]] = extractvalue { i64, i1 } [[TMP0]], 1
306+
; CHECK-NEXT: [[OV:%.*]] = icmp eq i64 [[IV]], 0
309307
; CHECK-NEXT: br i1 [[OV]], label [[EXIT:%.*]], label [[BACKEDGE]]
310308
; CHECK: backedge:
311-
; CHECK-NEXT: [[SUNKADDR:%.*]] = mul i64 [[MATH]], 4
309+
; CHECK-NEXT: [[SUNKADDR:%.*]] = mul i64 [[IV]], 4
312310
; CHECK-NEXT: [[SUNKADDR1:%.*]] = getelementptr i8, ptr [[P:%.*]], i64 [[SUNKADDR]]
313-
; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, ptr [[SUNKADDR1]] unordered, align 4
311+
; CHECK-NEXT: [[SUNKADDR2:%.*]] = getelementptr i8, ptr [[SUNKADDR1]], i64 -4
312+
; CHECK-NEXT: [[LOADED:%.*]] = load atomic i32, ptr [[SUNKADDR2]] unordered, align 4
313+
; CHECK-NEXT: [[MATH]] = add i64 [[IV]], -1
314314
; CHECK-NEXT: [[COND_2:%.*]] = icmp eq i32 [[LOADED]], [[X:%.*]]
315315
; CHECK-NEXT: br i1 [[COND_2]], label [[FAILURE:%.*]], label [[LOOP]]
316316
; CHECK: exit:

0 commit comments

Comments
 (0)