Skip to content

Commit 15a4233

Browse files
committed
[ValueTracking] soften assert for invertible recurrence matching
There's a TODO comment in the code and discussion in D99912 about generalizing this, but I wasn't sure how to implement that, so just going with a potential minimal fix to avoid crashing. The test is a reduction beyond useful code (there's no user of %user...), but it is based on https://llvm.org/PR50191, so this is asserting on real code. Differential Revision: https://reviews.llvm.org/D101772
1 parent fd15e2b commit 15a4233

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2608,7 +2608,8 @@ static Optional<unsigned> getInvertibleOperand(const Operator *Op1,
26082608
cast<Operator>(BO2));
26092609
if (!Idx || *Idx != 0)
26102610
break;
2611-
assert(BO1->getOperand(*Idx) == PN1 && BO2->getOperand(*Idx) == PN2);
2611+
if (BO1->getOperand(*Idx) != PN1 || BO2->getOperand(*Idx) != PN2)
2612+
break;
26122613

26132614
// Phi operands might not be in the same order. TODO: generalize
26142615
// interface to return pair of operands.

llvm/test/Transforms/InstSimplify/icmp.ll

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -187,3 +187,29 @@ define i1 @shl_div_cmp_greater(i8 %x) {
187187
%cmp = icmp ule i8 %div, %x
188188
ret i1 %cmp
189189
}
190+
191+
; Don't crash matching recurrences/invertible ops.
192+
193+
define void @PR50191(i32 %x) {
194+
; CHECK-LABEL: @PR50191(
195+
; CHECK-NEXT: entry:
196+
; CHECK-NEXT: br label [[LOOP:%.*]]
197+
; CHECK: loop:
198+
; CHECK-NEXT: [[P1:%.*]] = phi i32 [ [[X:%.*]], [[ENTRY:%.*]] ], [ [[SUB1:%.*]], [[LOOP]] ]
199+
; CHECK-NEXT: [[P2:%.*]] = phi i32 [ [[X]], [[ENTRY]] ], [ [[SUB2:%.*]], [[LOOP]] ]
200+
; CHECK-NEXT: [[SUB1]] = sub i32 [[P1]], [[P2]]
201+
; CHECK-NEXT: [[SUB2]] = sub i32 42, [[P2]]
202+
; CHECK-NEXT: br label [[LOOP]]
203+
;
204+
entry:
205+
br label %loop
206+
207+
loop:
208+
%p1 = phi i32 [ %x, %entry ], [ %sub1, %loop ]
209+
%p2 = phi i32 [ %x, %entry ], [ %sub2, %loop ]
210+
%cmp = icmp eq i32 %p1, %p2
211+
%user = zext i1 %cmp to i32
212+
%sub1 = sub i32 %p1, %p2
213+
%sub2 = sub i32 42, %p2
214+
br label %loop
215+
}

0 commit comments

Comments
 (0)