Skip to content

Commit e9731bd

Browse files
committed
Ensure collectTransitivePredecessors returns Pred only from the Loop.
It's possible that we encounter Irreducible control flow, due to which, we may find that a few predecessors of BB are not a part of the CurLoop. Currently we crash in the function for such cases. This patch ensures that we only return Predecessors that are a part of CurLoop and gracefully ignore other Predecessors. For example, consider Irreducible IR of this form: ``` define i64 @baz() { bb: br label %bb1 bb1: ; preds = %bb3, %bb br label %bb3 bb2: ; No predecessors! br label %bb3 bb3: ; preds = %bb2, %bb1 %load = load ptr addrspace(1), ptr addrspace(1) null, align 8 br label %bb1 } ``` This crashes when `collectTransitivePredecessors` is called on the `%bb1<Header>, %bb3<latch>` loop, because the loop body has a predecessor `%bb2` which is not a part of the loop.
1 parent 0b7e8c2 commit e9731bd

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

llvm/lib/Analysis/MustExecute.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -162,6 +162,9 @@ static bool CanProveNotTakenFirstIteration(const BasicBlock *ExitBlock,
162162
/// Collect all blocks from \p CurLoop which lie on all possible paths from
163163
/// the header of \p CurLoop (inclusive) to BB (exclusive) into the set
164164
/// \p Predecessors. If \p BB is the header, \p Predecessors will be empty.
165+
/// Note: It's possible that we encounter Irreducible control flow, due to
166+
/// which, we may find that a few predecessors of \p BB are not a part of the
167+
/// \p CurLoop. We only return Predecessors that are a part of \p CurLoop.
165168
static void collectTransitivePredecessors(
166169
const Loop *CurLoop, const BasicBlock *BB,
167170
SmallPtrSetImpl<const BasicBlock *> &Predecessors) {
@@ -171,6 +174,8 @@ static void collectTransitivePredecessors(
171174
return;
172175
SmallVector<const BasicBlock *, 4> WorkList;
173176
for (const auto *Pred : predecessors(BB)) {
177+
if (!CurLoop->contains(Pred))
178+
continue;
174179
Predecessors.insert(Pred);
175180
WorkList.push_back(Pred);
176181
}
@@ -187,7 +192,7 @@ static void collectTransitivePredecessors(
187192
// We can ignore backedge of all loops containing BB to get a sligtly more
188193
// optimistic result.
189194
for (const auto *PredPred : predecessors(Pred))
190-
if (Predecessors.insert(PredPred).second)
195+
if (CurLoop->contains(PredPred) && Predecessors.insert(PredPred).second)
191196
WorkList.push_back(PredPred);
192197
}
193198
}

0 commit comments

Comments
 (0)