#include "llvm/IR/Dominators.h"
#include "llvm/ADT/StringRef.h"
#include "llvm/Config/llvm-config.h"
#include "llvm/IR/CFG.h"
#include "llvm/IR/Function.h"
#include "llvm/IR/Instruction.h"
#include "llvm/IR/Instructions.h"
#include "llvm/IR/PassManager.h"
#include "llvm/InitializePasses.h"
#include "llvm/PassRegistry.h"
#include "llvm/Support/Casting.h"
#include "llvm/Support/CommandLine.h"
#include "llvm/Support/raw_ostream.h"
#include <cassert>
namespace llvm {
class Argument;
class Constant;
class Value;
} using namespace llvm;
bool llvm::VerifyDomInfo = false;
static cl::opt<bool, true>
VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo), cl::Hidden,
cl::desc("Verify dominator info (time consuming)"));
#ifdef EXPENSIVE_CHECKS
static constexpr bool ExpensiveChecksEnabled = true;
#else
static constexpr bool ExpensiveChecksEnabled = false;
#endif
bool BasicBlockEdge::isSingleEdge() const {
const Instruction *TI = Start->getTerminator();
unsigned NumEdgesToEnd = 0;
for (unsigned int i = 0, n = TI->getNumSuccessors(); i < n; ++i) {
if (TI->getSuccessor(i) == End)
++NumEdgesToEnd;
if (NumEdgesToEnd >= 2)
return false;
}
assert(NumEdgesToEnd == 1);
return true;
}
template class llvm::DomTreeNodeBase<BasicBlock>;
template class llvm::DominatorTreeBase<BasicBlock, false>; template class llvm::DominatorTreeBase<BasicBlock, true>;
template class llvm::cfg::Update<BasicBlock *>;
template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBDomTree>(
DomTreeBuilder::BBDomTree &DT);
template void
llvm::DomTreeBuilder::CalculateWithUpdates<DomTreeBuilder::BBDomTree>(
DomTreeBuilder::BBDomTree &DT, BBUpdates U);
template void llvm::DomTreeBuilder::Calculate<DomTreeBuilder::BBPostDomTree>(
DomTreeBuilder::BBPostDomTree &DT);
template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBDomTree>(
DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To);
template void llvm::DomTreeBuilder::InsertEdge<DomTreeBuilder::BBPostDomTree>(
DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To);
template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBDomTree>(
DomTreeBuilder::BBDomTree &DT, BasicBlock *From, BasicBlock *To);
template void llvm::DomTreeBuilder::DeleteEdge<DomTreeBuilder::BBPostDomTree>(
DomTreeBuilder::BBPostDomTree &DT, BasicBlock *From, BasicBlock *To);
template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBDomTree>(
DomTreeBuilder::BBDomTree &DT, DomTreeBuilder::BBDomTreeGraphDiff &,
DomTreeBuilder::BBDomTreeGraphDiff *);
template void llvm::DomTreeBuilder::ApplyUpdates<DomTreeBuilder::BBPostDomTree>(
DomTreeBuilder::BBPostDomTree &DT, DomTreeBuilder::BBPostDomTreeGraphDiff &,
DomTreeBuilder::BBPostDomTreeGraphDiff *);
template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBDomTree>(
const DomTreeBuilder::BBDomTree &DT,
DomTreeBuilder::BBDomTree::VerificationLevel VL);
template bool llvm::DomTreeBuilder::Verify<DomTreeBuilder::BBPostDomTree>(
const DomTreeBuilder::BBPostDomTree &DT,
DomTreeBuilder::BBPostDomTree::VerificationLevel VL);
bool DominatorTree::invalidate(Function &F, const PreservedAnalyses &PA,
FunctionAnalysisManager::Invalidator &) {
auto PAC = PA.getChecker<DominatorTreeAnalysis>();
return !(PAC.preserved() || PAC.preservedSet<AllAnalysesOn<Function>>() ||
PAC.preservedSet<CFGAnalyses>());
}
bool DominatorTree::dominates(const BasicBlock *BB, const Use &U) const {
Instruction *UserInst = cast<Instruction>(U.getUser());
if (auto *PN = dyn_cast<PHINode>(UserInst))
return dominates(BB, PN->getIncomingBlock(U));
else
return properlyDominates(BB, UserInst->getParent());
}
bool DominatorTree::dominates(const Value *DefV,
const Instruction *User) const {
const Instruction *Def = dyn_cast<Instruction>(DefV);
if (!Def) {
assert((isa<Argument>(DefV) || isa<Constant>(DefV)) &&
"Should be called with an instruction, argument or constant");
return true; }
const BasicBlock *UseBB = User->getParent();
const BasicBlock *DefBB = Def->getParent();
if (!isReachableFromEntry(UseBB))
return true;
if (!isReachableFromEntry(DefBB))
return false;
if (Def == User)
return false;
if (isa<InvokeInst>(Def) || isa<CallBrInst>(Def) || isa<PHINode>(User))
return dominates(Def, UseBB);
if (DefBB != UseBB)
return dominates(DefBB, UseBB);
return Def->comesBefore(User);
}
bool DominatorTree::dominates(const Instruction *Def,
const BasicBlock *UseBB) const {
const BasicBlock *DefBB = Def->getParent();
if (!isReachableFromEntry(UseBB))
return true;
if (!isReachableFromEntry(DefBB))
return false;
if (DefBB == UseBB)
return false;
if (const auto *II = dyn_cast<InvokeInst>(Def)) {
BasicBlock *NormalDest = II->getNormalDest();
BasicBlockEdge E(DefBB, NormalDest);
return dominates(E, UseBB);
}
if (const auto *CBI = dyn_cast<CallBrInst>(Def)) {
BasicBlock *NormalDest = CBI->getDefaultDest();
BasicBlockEdge E(DefBB, NormalDest);
return dominates(E, UseBB);
}
return dominates(DefBB, UseBB);
}
bool DominatorTree::dominates(const BasicBlockEdge &BBE,
const BasicBlock *UseBB) const {
const BasicBlock *Start = BBE.getStart();
const BasicBlock *End = BBE.getEnd();
if (!dominates(End, UseBB))
return false;
if (End->getSinglePredecessor())
return true;
int IsDuplicateEdge = 0;
for (const BasicBlock *BB : predecessors(End)) {
if (BB == Start) {
if (IsDuplicateEdge++)
return false;
continue;
}
if (!dominates(End, BB))
return false;
}
return true;
}
bool DominatorTree::dominates(const BasicBlockEdge &BBE, const Use &U) const {
Instruction *UserInst = cast<Instruction>(U.getUser());
PHINode *PN = dyn_cast<PHINode>(UserInst);
if (PN && PN->getParent() == BBE.getEnd() &&
PN->getIncomingBlock(U) == BBE.getStart())
return true;
const BasicBlock *UseBB;
if (PN)
UseBB = PN->getIncomingBlock(U);
else
UseBB = UserInst->getParent();
return dominates(BBE, UseBB);
}
bool DominatorTree::dominates(const Value *DefV, const Use &U) const {
const Instruction *Def = dyn_cast<Instruction>(DefV);
if (!Def) {
assert((isa<Argument>(DefV) || isa<Constant>(DefV)) &&
"Should be called with an instruction, argument or constant");
return true; }
Instruction *UserInst = cast<Instruction>(U.getUser());
const BasicBlock *DefBB = Def->getParent();
const BasicBlock *UseBB;
if (PHINode *PN = dyn_cast<PHINode>(UserInst))
UseBB = PN->getIncomingBlock(U);
else
UseBB = UserInst->getParent();
if (!isReachableFromEntry(UseBB))
return true;
if (!isReachableFromEntry(DefBB))
return false;
if (const InvokeInst *II = dyn_cast<InvokeInst>(Def)) {
BasicBlock *NormalDest = II->getNormalDest();
BasicBlockEdge E(DefBB, NormalDest);
return dominates(E, U);
}
if (const auto *CBI = dyn_cast<CallBrInst>(Def)) {
BasicBlock *NormalDest = CBI->getDefaultDest();
BasicBlockEdge E(DefBB, NormalDest);
return dominates(E, U);
}
if (DefBB != UseBB)
return dominates(DefBB, UseBB);
if (isa<PHINode>(UserInst))
return true;
return Def->comesBefore(UserInst);
}
bool DominatorTree::isReachableFromEntry(const Use &U) const {
Instruction *I = dyn_cast<Instruction>(U.getUser());
if (!I) return true;
if (PHINode *PN = dyn_cast<PHINode>(I))
return isReachableFromEntry(PN->getIncomingBlock(U));
return isReachableFromEntry(I->getParent());
}
bool DominatorTree::dominates(const BasicBlockEdge &BBE1,
const BasicBlockEdge &BBE2) const {
if (BBE1.getStart() == BBE2.getStart() && BBE1.getEnd() == BBE2.getEnd())
return true;
return dominates(BBE1, BBE2.getStart());
}
DominatorTree DominatorTreeAnalysis::run(Function &F,
FunctionAnalysisManager &) {
DominatorTree DT;
DT.recalculate(F);
return DT;
}
AnalysisKey DominatorTreeAnalysis::Key;
DominatorTreePrinterPass::DominatorTreePrinterPass(raw_ostream &OS) : OS(OS) {}
PreservedAnalyses DominatorTreePrinterPass::run(Function &F,
FunctionAnalysisManager &AM) {
OS << "DominatorTree for function: " << F.getName() << "\n";
AM.getResult<DominatorTreeAnalysis>(F).print(OS);
return PreservedAnalyses::all();
}
PreservedAnalyses DominatorTreeVerifierPass::run(Function &F,
FunctionAnalysisManager &AM) {
auto &DT = AM.getResult<DominatorTreeAnalysis>(F);
assert(DT.verify());
(void)DT;
return PreservedAnalyses::all();
}
char DominatorTreeWrapperPass::ID = 0;
DominatorTreeWrapperPass::DominatorTreeWrapperPass() : FunctionPass(ID) {
initializeDominatorTreeWrapperPassPass(*PassRegistry::getPassRegistry());
}
INITIALIZE_PASS(DominatorTreeWrapperPass, "domtree",
"Dominator Tree Construction", true, true)
bool DominatorTreeWrapperPass::runOnFunction(Function &F) {
DT.recalculate(F);
return false;
}
void DominatorTreeWrapperPass::verifyAnalysis() const {
if (VerifyDomInfo)
assert(DT.verify(DominatorTree::VerificationLevel::Full));
else if (ExpensiveChecksEnabled)
assert(DT.verify(DominatorTree::VerificationLevel::Basic));
}
void DominatorTreeWrapperPass::print(raw_ostream &OS, const Module *) const {
DT.print(OS);
}