Skip to content

Commit 00396e6

Browse files
authored
[ConstraintElim] Support arbitrary incoming values for inductions (#68032)
Support arbitray incoming values for AddRecs by getting the loop predecessor and checking if its SCEV matches the AddRec start. This is done after the existing check, which can help to catch cases where the expression gets simplified by SCEV to either an IR constant or existing value which can be used instead.
1 parent e577e70 commit 00396e6

File tree

2 files changed

+8
-9
lines changed

2 files changed

+8
-9
lines changed

llvm/lib/Transforms/Scalar/ConstraintElimination.cpp

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -859,18 +859,18 @@ void State::addInfoForInductions(BasicBlock &BB) {
859859
return;
860860

861861
auto *AR = dyn_cast_or_null<SCEVAddRecExpr>(SE.getSCEV(PN));
862-
if (!AR)
862+
BasicBlock *LoopPred = L->getLoopPredecessor();
863+
if (!AR || !LoopPred)
863864
return;
864865

865866
const SCEV *StartSCEV = AR->getStart();
866867
Value *StartValue = nullptr;
867-
if (auto *C = dyn_cast<SCEVConstant>(StartSCEV))
868+
if (auto *C = dyn_cast<SCEVConstant>(StartSCEV)) {
868869
StartValue = C->getValue();
869-
else if (auto *U = dyn_cast<SCEVUnknown>(StartSCEV))
870-
StartValue = U->getValue();
871-
872-
if (!StartValue)
873-
return;
870+
} else {
871+
StartValue = PN->getIncomingValueForBlock(LoopPred);
872+
assert(SE.getSCEV(StartValue) == StartSCEV && "inconsistent start value");
873+
}
874874

875875
DomTreeNode *DTN = DT.getNode(InLoopSucc);
876876
auto Inc = SE.getMonotonicPredicateType(AR, CmpInst::ICMP_UGT);

llvm/test/Transforms/ConstraintElimination/monotonic-int-phis-nested-loops.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,7 @@ define void @start_value_of_inner_add_rec_is_add_rec_condition_can_be_simplified
1717
; CHECK-NEXT: [[CMP2_NOT:%.*]] = icmp eq i32 [[K_0]], [[LEN]]
1818
; CHECK-NEXT: br i1 [[CMP2_NOT]], label [[OUTER_LATCH]], label [[INNER_LATCH]]
1919
; CHECK: inner.latch:
20-
; CHECK-NEXT: [[CMP_NOT_I:%.*]] = icmp ult i32 [[K_0]], [[LEN]]
21-
; CHECK-NEXT: call void @use(i1 [[CMP_NOT_I]])
20+
; CHECK-NEXT: call void @use(i1 true)
2221
; CHECK-NEXT: [[K_INC]] = add i32 [[K_0]], 1
2322
; CHECK-NEXT: br label [[INNER_HEADER]]
2423
; CHECK: outer.latch:

0 commit comments

Comments
 (0)