diff --git a/llvm/include/llvm/Support/GenericDomTree.h b/llvm/include/llvm/Support/GenericDomTree.h index a4a680c97a079..d3f789dd00269 100644 --- a/llvm/include/llvm/Support/GenericDomTree.h +++ b/llvm/include/llvm/Support/GenericDomTree.h @@ -558,6 +558,22 @@ class DominatorTreeBase { return isPostDominator() && !A->getBlock(); } + template + NodeT *findNearestCommonDominator(iterator_range Nodes) const { + assert(!Nodes.empty() && "Nodes list is empty!"); + + NodeT *NCD = *Nodes.begin(); + for (NodeT *Node : llvm::drop_begin(Nodes)) { + NCD = findNearestCommonDominator(NCD, Node); + + // Stop when the root is reached. + if (isVirtualRoot(getNode(NCD))) + return nullptr; + } + + return NCD; + } + //===--------------------------------------------------------------------===// // API to update (Post)DominatorTree information based on modifications to // the CFG... diff --git a/llvm/lib/CodeGen/ShrinkWrap.cpp b/llvm/lib/CodeGen/ShrinkWrap.cpp index 5029f45def226..fa57eb30fac43 100644 --- a/llvm/lib/CodeGen/ShrinkWrap.cpp +++ b/llvm/lib/CodeGen/ShrinkWrap.cpp @@ -375,12 +375,7 @@ bool ShrinkWrap::useOrDefCSROrFI(const MachineInstr &MI, RegScavenger *RS, template static MachineBasicBlock *FindIDom(MachineBasicBlock &Block, ListOfBBs BBs, DominanceAnalysis &Dom, bool Strict = true) { - MachineBasicBlock *IDom = &Block; - for (MachineBasicBlock *BB : BBs) { - IDom = Dom.findNearestCommonDominator(IDom, BB); - if (!IDom) - break; - } + MachineBasicBlock *IDom = Dom.findNearestCommonDominator(iterator_range(BBs)); if (Strict && IDom == &Block) return nullptr; return IDom;