Skip to content

Commit 2230ab1

Browse files
committed
[llvm] Add NCD search on Array of basic blocks
1 parent 9c93dd2 commit 2230ab1

File tree

2 files changed

+21
-0
lines changed

2 files changed

+21
-0
lines changed

llvm/include/llvm/CodeGen/MachineDominators.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,11 @@ class MachineDominatorTree : public DomTreeBase<MachineBasicBlock> {
185185
return Base::findNearestCommonDominator(A, B);
186186
}
187187

188+
/// Returns the nearest common dominator of the given blocks.
189+
/// If that tree node is a virtual root, a nullptr will be returned.
190+
MachineBasicBlock *
191+
findNearestCommonDominator(ArrayRef<MachineBasicBlock *> Blocks) const;
192+
188193
MachineDomTreeNode *operator[](MachineBasicBlock *BB) const {
189194
applySplitCriticalEdges();
190195
return Base::getNode(BB);

llvm/lib/CodeGen/MachineDominators.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,3 +189,19 @@ void MachineDominatorTree::applySplitCriticalEdges() const {
189189
NewBBs.clear();
190190
CriticalEdgesToSplit.clear();
191191
}
192+
193+
MachineBasicBlock *MachineDominatorTree::findNearestCommonDominator(
194+
ArrayRef<MachineBasicBlock *> Blocks) const {
195+
assert(!Blocks.empty());
196+
197+
MachineBasicBlock *NCD = Blocks.front();
198+
for (MachineBasicBlock *BB : Blocks.drop_front()) {
199+
NCD = Base::findNearestCommonDominator(NCD, BB);
200+
201+
// Stop when the root is reached.
202+
if (Base::isVirtualRoot(Base::getNode(NCD)))
203+
return nullptr;
204+
}
205+
206+
return NCD;
207+
}

0 commit comments

Comments
 (0)