Index: llvm/include/llvm/Analysis/PostDominators.h =================================================================== --- llvm/include/llvm/Analysis/PostDominators.h +++ llvm/include/llvm/Analysis/PostDominators.h @@ -41,6 +41,14 @@ /// Return true if \p I1 dominates \p I2. This checks if \p I2 comes before /// \p I1 if they belongs to the same basic block. bool dominates(const Instruction *I1, const Instruction *I2) const; + + // Ensure base class overloads are visible. + using Base::findNearestCommonDominator; + + /// Find the nearest instruction I that post-dominates both I1 and I2, in the + /// sense that a result produced after I will be available at both I1 and I2. + Instruction *findNearestCommonDominator(Instruction *I1, + Instruction *I2) const; }; /// Analysis pass which computes a \c PostDominatorTree. Index: llvm/lib/Analysis/PostDominators.cpp =================================================================== --- llvm/lib/Analysis/PostDominators.cpp +++ llvm/lib/Analysis/PostDominators.cpp @@ -73,6 +73,23 @@ return &*I == I2; } +Instruction * +PostDominatorTree::findNearestCommonDominator(Instruction *I1, + Instruction *I2) const { + BasicBlock *BB1 = I1->getParent(); + BasicBlock *BB2 = I2->getParent(); + if (BB1 == BB2) + return I1->comesBefore(I2) ? I2 : I1; + BasicBlock *PDomBB = findNearestCommonDominator(BB1, BB2); + if (!PDomBB) + return nullptr; + if (BB1 == PDomBB) + return I2; + if (BB1 == PDomBB) + return I1; + return PDomBB->getFirstNonPHI(); +} + bool PostDominatorTreeWrapperPass::runOnFunction(Function &F) { DT.recalculate(F); return false;