#include "llvm/Analysis/CFG.h"
#include "llvm/Analysis/LoopInfo.h"
#include "llvm/IR/Dominators.h"
#include "llvm/Support/CommandLine.h"
using namespace llvm;
static cl::opt<unsigned> DefaultMaxBBsToExplore(
"dom-tree-reachability-max-bbs-to-explore", cl::Hidden,
cl::desc("Max number of BBs to explore for reachability analysis"),
cl::init(32));
void llvm::FindFunctionBackedges(const Function &F,
SmallVectorImpl<std::pair<const BasicBlock*,const BasicBlock*> > &Result) {
const BasicBlock *BB = &F.getEntryBlock();
if (succ_empty(BB))
return;
SmallPtrSet<const BasicBlock*, 8> Visited;
SmallVector<std::pair<const BasicBlock *, const_succ_iterator>, 8> VisitStack;
SmallPtrSet<const BasicBlock*, 8> InStack;
Visited.insert(BB);
VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
InStack.insert(BB);
do {
std::pair<const BasicBlock *, const_succ_iterator> &Top = VisitStack.back();
const BasicBlock *ParentBB = Top.first;
const_succ_iterator &I = Top.second;
bool FoundNew = false;
while (I != succ_end(ParentBB)) {
BB = *I++;
if (Visited.insert(BB).second) {
FoundNew = true;
break;
}
if (InStack.count(BB))
Result.push_back(std::make_pair(ParentBB, BB));
}
if (FoundNew) {
InStack.insert(BB);
VisitStack.push_back(std::make_pair(BB, succ_begin(BB)));
} else {
InStack.erase(VisitStack.pop_back_val().first);
}
} while (!VisitStack.empty());
}
unsigned llvm::GetSuccessorNumber(const BasicBlock *BB,
const BasicBlock *Succ) {
const Instruction *Term = BB->getTerminator();
#ifndef NDEBUG
unsigned e = Term->getNumSuccessors();
#endif
for (unsigned i = 0; ; ++i) {
assert(i != e && "Didn't find edge?");
if (Term->getSuccessor(i) == Succ)
return i;
}
}
bool llvm::isCriticalEdge(const Instruction *TI, unsigned SuccNum,
bool AllowIdenticalEdges) {
assert(SuccNum < TI->getNumSuccessors() && "Illegal edge specification!");
return isCriticalEdge(TI, TI->getSuccessor(SuccNum), AllowIdenticalEdges);
}
bool llvm::isCriticalEdge(const Instruction *TI, const BasicBlock *Dest,
bool AllowIdenticalEdges) {
assert(TI->isTerminator() && "Must be a terminator to have successors!");
if (TI->getNumSuccessors() == 1) return false;
assert(is_contained(predecessors(Dest), TI->getParent()) &&
"No edge between TI's block and Dest.");
const_pred_iterator I = pred_begin(Dest), E = pred_end(Dest);
assert(I != E && "No preds, but we have an edge to the block?");
const BasicBlock *FirstPred = *I;
++I; if (!AllowIdenticalEdges)
return I != E;
for (; I != E; ++I)
if (*I != FirstPred)
return true;
return false;
}
static const Loop *getOutermostLoop(const LoopInfo *LI, const BasicBlock *BB) {
const Loop *L = LI->getLoopFor(BB);
return L ? L->getOutermostLoop() : nullptr;
}
bool llvm::isPotentiallyReachableFromMany(
SmallVectorImpl<BasicBlock *> &Worklist, BasicBlock *StopBB,
const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
const LoopInfo *LI) {
if (DT && !DT->isReachableFromEntry(StopBB))
DT = nullptr;
if (ExclusionSet && !ExclusionSet->empty())
DT = nullptr;
SmallPtrSet<const Loop *, 8> LoopsWithHoles;
if (LI && ExclusionSet) {
for (auto *BB : *ExclusionSet) {
if (const Loop *L = getOutermostLoop(LI, BB))
LoopsWithHoles.insert(L);
}
}
const Loop *StopLoop = LI ? getOutermostLoop(LI, StopBB) : nullptr;
unsigned Limit = DefaultMaxBBsToExplore;
SmallPtrSet<const BasicBlock*, 32> Visited;
do {
BasicBlock *BB = Worklist.pop_back_val();
if (!Visited.insert(BB).second)
continue;
if (BB == StopBB)
return true;
if (ExclusionSet && ExclusionSet->count(BB))
continue;
if (DT && DT->dominates(BB, StopBB))
return true;
const Loop *Outer = nullptr;
if (LI) {
Outer = getOutermostLoop(LI, BB);
if (LoopsWithHoles.count(Outer))
Outer = nullptr;
if (StopLoop && Outer == StopLoop)
return true;
}
if (!--Limit) {
return true;
}
if (Outer) {
Outer->getExitBlocks(Worklist);
} else {
Worklist.append(succ_begin(BB), succ_end(BB));
}
} while (!Worklist.empty());
return false;
}
bool llvm::isPotentiallyReachable(
const BasicBlock *A, const BasicBlock *B,
const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
const LoopInfo *LI) {
assert(A->getParent() == B->getParent() &&
"This analysis is function-local!");
if (DT) {
if (DT->isReachableFromEntry(A) && !DT->isReachableFromEntry(B))
return false;
if (!ExclusionSet || ExclusionSet->empty()) {
if (A->isEntryBlock() && DT->isReachableFromEntry(B))
return true;
if (B->isEntryBlock() && DT->isReachableFromEntry(A))
return false;
}
}
SmallVector<BasicBlock*, 32> Worklist;
Worklist.push_back(const_cast<BasicBlock*>(A));
return isPotentiallyReachableFromMany(Worklist, const_cast<BasicBlock *>(B),
ExclusionSet, DT, LI);
}
bool llvm::isPotentiallyReachable(
const Instruction *A, const Instruction *B,
const SmallPtrSetImpl<BasicBlock *> *ExclusionSet, const DominatorTree *DT,
const LoopInfo *LI) {
assert(A->getParent()->getParent() == B->getParent()->getParent() &&
"This analysis is function-local!");
if (A->getParent() == B->getParent()) {
BasicBlock *BB = const_cast<BasicBlock *>(A->getParent());
if (LI && LI->getLoopFor(BB) != nullptr)
return true;
if (A == B || A->comesBefore(B))
return true;
if (BB->isEntryBlock())
return false;
SmallVector<BasicBlock*, 32> Worklist;
Worklist.append(succ_begin(BB), succ_end(BB));
if (Worklist.empty()) {
return false;
}
return isPotentiallyReachableFromMany(
Worklist, const_cast<BasicBlock *>(B->getParent()), ExclusionSet,
DT, LI);
}
return isPotentiallyReachable(
A->getParent(), B->getParent(), ExclusionSet, DT, LI);
}