[llvm] d2ffc11 - [CFG][InstCombine][NFC] Use block numbers when finding backedges (#186668)
via llvm-commits
llvm-commits at lists.llvm.org
Sun Mar 15 08:45:29 PDT 2026
Author: Alexis Engelke
Date: 2026-03-15T16:45:25+01:00
New Revision: d2ffc11f6b50da9aae510ff0712bd351ea3f4577
URL: https://github.com/llvm/llvm-project/commit/d2ffc11f6b50da9aae510ff0712bd351ea3f4577
DIFF: https://github.com/llvm/llvm-project/commit/d2ffc11f6b50da9aae510ff0712bd351ea3f4577.diff
LOG: [CFG][InstCombine][NFC] Use block numbers when finding backedges (#186668)
The functions traverse all basic blocks, so SmallPtrSets use a single
vector indexed by block number.
Added:
Modified:
llvm/lib/Analysis/CFG.cpp
llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
Removed:
################################################################################
diff --git a/llvm/lib/Analysis/CFG.cpp b/llvm/lib/Analysis/CFG.cpp
index 1676d6b6f592e..da74691a6e6a5 100644
--- a/llvm/lib/Analysis/CFG.cpp
+++ b/llvm/lib/Analysis/CFG.cpp
@@ -35,40 +35,49 @@ static cl::opt<unsigned> DefaultMaxBBsToExplore(
void llvm::FindFunctionBackedges(const Function &F,
SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
const BasicBlock *BB = &F.getEntryBlock();
- if (succ_empty(BB))
- return;
- SmallPtrSet<const BasicBlock*, 8> Visited;
- SmallVector<std::pair<const BasicBlock *, const_succ_iterator>, 8> VisitStack;
- SmallPtrSet<const BasicBlock*, 8> InStack;
+ // In the DFS traversal, we maintain three states: unvisited, visited in the
+ // past, and visited and currently in the DFS stack. If we have an edge to a
+ // block in the stack, we have found a backedge.
+ enum VisitState : uint8_t { Unvisited = 0, Visited = 1, InStack = 2 };
+ SmallVector<VisitState> BlockState(F.getMaxBlockNumber(), Unvisited);
+ struct StackEntry {
+ const BasicBlock *BB;
+ const_succ_iterator SuccIt;
+ const_succ_iterator SuccEnd;
+
+ StackEntry(const BasicBlock *BB)
+ : BB(BB), SuccIt(nullptr), SuccEnd(nullptr) {
+ auto Succs = successors(BB);
+ SuccIt = Succs.begin();
+ SuccEnd = Succs.end();
+ }
+ };
+ SmallVector<StackEntry, 8> VisitStack;
- Visited.insert(BB);
- VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
- InStack.insert(BB);
+ BlockState[BB->getNumber()] = InStack;
+ VisitStack.emplace_back(BB);
do {
- std::pair<const BasicBlock *, const_succ_iterator> &Top = VisitStack.back();
- const BasicBlock *ParentBB = Top.first;
- const_succ_iterator &I = Top.second;
-
+ StackEntry &Top = VisitStack.back();
bool FoundNew = false;
- while (I != succ_end(ParentBB)) {
- BB = *I++;
- if (Visited.insert(BB).second) {
+ while (Top.SuccIt != Top.SuccEnd) {
+ BB = *Top.SuccIt++;
+ if (BlockState[BB->getNumber()] == Unvisited) {
+ // Unvisited successor => go down one level.
+ BlockState[BB->getNumber()] = InStack;
+ VisitStack.emplace_back(BB);
FoundNew = true;
break;
}
- // Successor is in VisitStack, it's a back edge.
- if (InStack.count(BB))
- Result.push_back(std::make_pair(ParentBB, BB));
+ // Successor in VisitStack => backedge.
+ if (BlockState[BB->getNumber()] == InStack)
+ Result.emplace_back(Top.BB, BB);
}
- if (FoundNew) {
- // Go down one level if there is a unvisited successor.
- InStack.insert(BB);
- VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
- } else {
- // Go up one level.
- InStack.erase(VisitStack.pop_back_val().first);
+ // Go up one level.
+ if (!FoundNew) {
+ BlockState[Top.BB->getNumber()] = Visited;
+ VisitStack.pop_back();
}
} while (!VisitStack.empty());
}
diff --git a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
index a895936b5351d..e1b5819a64dfe 100644
--- a/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
+++ b/llvm/lib/Transforms/InstCombine/InstructionCombining.cpp
@@ -6145,11 +6145,11 @@ bool InstCombinerImpl::prepareWorklist(Function &F) {
void InstCombiner::computeBackEdges() {
// Collect backedges.
- SmallPtrSet<BasicBlock *, 16> Visited;
+ SmallVector<bool> Visited(F.getMaxBlockNumber());
for (BasicBlock *BB : RPOT) {
- Visited.insert(BB);
+ Visited[BB->getNumber()] = true;
for (BasicBlock *Succ : successors(BB))
- if (Visited.contains(Succ))
+ if (Visited[Succ->getNumber()])
BackEdges.insert({BB, Succ});
}
ComputedBackEdges = true;
More information about the llvm-commits
mailing list