[llvm] [WebAssembly] Replace Reachability with SCCs in Irreducible CFG Fixer (PR #179722)
Kamil Jakubus via llvm-commits
llvm-commits at lists.llvm.org
Sat Feb 14 06:56:39 PST 2026
================
@@ -120,65 +113,138 @@ class ReachabilityGraph {
assert(I != LoopEnterers.end());
return I->second;
}
+ unsigned getSCCId(MachineBasicBlock *MBB) const {
+ return SccId[getIndex(MBB)];
+ }
private:
MachineBasicBlock *Entry;
const BlockSet &Blocks;
+ DenseMap<MachineBasicBlock *, unsigned> BlockIndex;
BlockSet Loopers, LoopEntries;
DenseMap<MachineBasicBlock *, BlockSet> LoopEnterers;
bool inRegion(MachineBasicBlock *MBB) const { return Blocks.count(MBB); }
- // Maps a block to all the other blocks it can reach.
- DenseMap<MachineBasicBlock *, BlockSet> Reachable;
+ // Per-node adjacency in the region (excluding edges to Entry).
+ SmallVector<SmallVector<unsigned, 4>, 0> Succs;
+ SmallVector<bool, 0> SelfLoop;
+
+ // SCC = Strongly-connected component
+ // Map of an MBB's index (provided by `BlockIndex`) to it's assigned SCC
+ SmallVector<unsigned, 0> SccId;
+ // The number of elements in each SCC
+ SmallVector<unsigned, 0> SccSize;
+
+ unsigned getIndex(MachineBasicBlock *MBB) const {
+ auto It = BlockIndex.find(MBB);
+ assert(It != BlockIndex.end());
+ return It->second;
+ }
void calculate() {
- // Reachability computation work list. Contains pairs of recent additions
- // (A, B) where we just added a link A => B.
- using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
- SmallVector<BlockPair, 4> WorkList;
+ auto NumBlocks = Blocks.size();
+ Succs.assign(NumBlocks, {});
+ SelfLoop.assign(NumBlocks, false);
+
+ unsigned MMBIdx = 0;
+ BlockIndex.clear();
+ BlockIndex.reserve(NumBlocks);
+ for (auto *MBB : Blocks)
+ BlockIndex[MBB] = MMBIdx++;
// Add all relevant direct branches.
+ MMBIdx = 0;
for (auto *MBB : Blocks) {
for (auto *Succ : MBB->successors()) {
if (Succ != Entry && inRegion(Succ)) {
- Reachable[MBB].insert(Succ);
- WorkList.emplace_back(MBB, Succ);
+ if (Succ == MBB)
+ SelfLoop[MMBIdx] = true;
+ Succs[MMBIdx].push_back(getIndex(Succ));
}
}
+
+ ++MMBIdx;
}
- while (!WorkList.empty()) {
- MachineBasicBlock *MBB, *Succ;
- std::tie(MBB, Succ) = WorkList.pop_back_val();
- assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
- if (MBB != Entry) {
- // We recently added MBB => Succ, and that means we may have enabled
- // Pred => MBB => Succ.
- for (auto *Pred : MBB->predecessors()) {
- if (Reachable[Pred].insert(Succ).second) {
- WorkList.emplace_back(Pred, Succ);
+ // Tarjan SCC (iterative) on the region graph.
----------------
jkbz64 wrote:
Before doing documentation work in this section I think it should be decided if we opt to use SCCIterator as @QuantumSegfault mentioned, if we do then I think we don't need much of the comment here as the documentation would fall down to SCCIterator abstraction.
https://github.com/llvm/llvm-project/pull/179722
More information about the llvm-commits
mailing list