Skip to content

Commit 26ab444

Browse files
committed
[ConstraintElim] Make sure add-rec is for the current loop.
Update addInfoForInductions to also check if the add-rec is for the current loop. Otherwise we might add incorrect facts or crash. Fixes a miscompile & crash introduced by 00396e6.
1 parent 067632e commit 26ab444

File tree

2 files changed

+41
-2
lines changed

2 files changed

+41
-2
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -870,7 +870,7 @@ void State::addInfoForInductions(BasicBlock &BB) {
870870

871871
auto *AR = dyn_cast_or_null<SCEVAddRecExpr>(SE.getSCEV(PN));
872872
BasicBlock *LoopPred = L->getLoopPredecessor();
873-
if (!AR || !LoopPred)
873+
if (!AR || AR->getLoop() != L || !LoopPred)
874874
return;
875875

876876
const SCEV *StartSCEV = AR->getStart();

llvm/test/Transforms/ConstraintElimination/monotonic-int-phis-adjacent-loops.ll renamed to llvm/test/Transforms/ConstraintElimination/monotonic-phis-adjacent-loops.ll

Lines changed: 40 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ define void @test_loop_add_rec_used_in_adjacent_loop(i8 %len.n, i16 %a) {
2323
; CHECK-NEXT: [[C:%.*]] = icmp eq i16 [[IV_2]], [[LEN]]
2424
; CHECK-NEXT: br i1 [[C]], label [[EXIT]], label [[FOR_BODY:%.*]]
2525
; CHECK: for.body:
26-
; CHECK-NEXT: [[AND:%.*]] = and i1 true, true
26+
; CHECK-NEXT: [[T_2:%.*]] = icmp ult i16 [[IV_2]], [[A]]
27+
; CHECK-NEXT: [[AND:%.*]] = and i1 true, [[T_2]]
2728
; CHECK-NEXT: br i1 [[AND]], label [[LOOP_2_LATCH]], label [[EXIT]]
2829
; CHECK: loop.2.latch:
2930
; CHECK-NEXT: call void @use(i16 [[IV_2]])
@@ -63,3 +64,41 @@ loop.2.latch:
6364
exit:
6465
ret void
6566
}
67+
68+
define void @test_adjacen_loops_pointer_iv_crash() {
69+
; CHECK-LABEL: @test_adjacen_loops_pointer_iv_crash(
70+
; CHECK-NEXT: entry:
71+
; CHECK-NEXT: br label [[LOOP_1:%.*]]
72+
; CHECK: loop.1:
73+
; CHECK-NEXT: [[IV_1:%.*]] = phi ptr [ null, [[ENTRY:%.*]] ], [ [[IV_1_NEXT:%.*]], [[LOOP_1]] ]
74+
; CHECK-NEXT: [[IV_1_NEXT]] = getelementptr ptr, ptr [[IV_1]], i32 1
75+
; CHECK-NEXT: br i1 false, label [[LOOP_1]], label [[LOOP_2:%.*]]
76+
; CHECK: loop.2:
77+
; CHECK-NEXT: [[__FIRST_ADDR_1_LCSSA:%.*]] = phi ptr [ [[IV_1]], [[LOOP_1]] ], [ [[IV_1]], [[LOOP_2_LATCH:%.*]] ]
78+
; CHECK-NEXT: [[CMP7:%.*]] = icmp eq ptr [[__FIRST_ADDR_1_LCSSA]], null
79+
; CHECK-NEXT: br i1 [[CMP7]], label [[IF_THEN8:%.*]], label [[LOOP_2_LATCH]]
80+
; CHECK: if.then8:
81+
; CHECK-NEXT: ret void
82+
; CHECK: loop.2.latch:
83+
; CHECK-NEXT: br label [[LOOP_2]]
84+
;
85+
entry:
86+
br label %loop.1
87+
88+
loop.1:
89+
%iv.1 = phi ptr [ null, %entry ], [ %iv.1.next, %loop.1 ]
90+
%iv.1.next = getelementptr ptr, ptr %iv.1, i32 1
91+
br i1 false, label %loop.1, label %loop.2
92+
93+
loop.2:
94+
%__first.addr.1.lcssa = phi ptr [ %iv.1, %loop.1 ], [ %iv.1, %loop.2.latch ]
95+
%cmp7 = icmp eq ptr %__first.addr.1.lcssa, null
96+
br i1 %cmp7, label %if.then8, label %loop.2.latch
97+
98+
if.then8: ; preds = %do.body
99+
ret void
100+
101+
loop.2.latch:
102+
br label %loop.2
103+
}
104+

0 commit comments

Comments
 (0)