//===- Dominators.cpp - Dominator Calculation -----------------------------===//
//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
//
//===----------------------------------------------------------------------===//
//
// This file implements simple dominator construction algorithms for finding
// forward dominators. Postdominators are available in libanalysis, but are not
// included in libvmcore, because it's not needed. Forward dominators are
// needed to support the Verifier pass.
//
//===----------------------------------------------------------------------===//
#include "llvm/Analysis/Dominators.h"
#include "llvm/Support/CFG.h"
#include "llvm/Assembly/Writer.h"
#include "llvm/ADT/DepthFirstIterator.h"
#include "llvm/ADT/SetOperations.h"
#include "llvm/ADT/SmallPtrSet.h"
#include "llvm/Instructions.h"
#include "llvm/Support/Streams.h"
#include <algorithm>
using namespace llvm;
namespace llvm {
static std::ostream &operator<<(std::ostream &o,
const std::set<BasicBlock*> &BBs) {
for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
I != E; ++I)
if (*I)
WriteAsOperand(o, *I, false);
else
o << " <<exit node>>";
return o;
}
}
//===----------------------------------------------------------------------===//
// DominatorTree Implementation
//===----------------------------------------------------------------------===//
//
// DominatorTree construction - This pass constructs immediate dominator
// information for a flow-graph based on the algorithm described in this
// document:
//
// A Fast Algorithm for Finding Dominators in a Flowgraph
// T. Lengauer & R. Tarjan, ACM TOPLAS July 1979, pgs 121-141.
//
// This implements both the O(n*ack(n)) and the O(n*log(n)) versions of EVAL and
// LINK, but it turns out that the theoretically slower O(n*log(n))
// implementation is actually faster than the "efficient" algorithm (even for
// large CFGs) because the constant overheads are substantially smaller. The
// lower-complexity version can be enabled with the following #define:
//
#define BALANCE_IDOM_TREE 0
//
//===----------------------------------------------------------------------===//
char DominatorTree::ID = 0;
static RegisterPass<DominatorTree>
E("domtree", "Dominator Tree Construction", true);
unsigned DominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
unsigned N) {
// This is more understandable as a recursive algorithm, but we can't use the
// recursive algorithm due to stack depth issues. Keep it here for
// documentation purposes.
#if 0
VInfo.Semi = ++N;
VInfo.Label = V;
Vertex.push_back(V); // Vertex[n] = V;
//Info[V].Ancestor = 0; // Ancestor[n] = 0
//Info[V].Child = 0; // Child[v] = 0
VInfo.Size = 1; // Size[v] = 1
for (succ_iterator SI = succ_begin(V), E = succ_end(V); SI != E; ++SI) {
InfoRec &SuccVInfo = Info[*SI];
if (SuccVInfo.Semi == 0) {
SuccVInfo.Parent = V;
N = DFSPass(*SI, SuccVInfo, N);
}
}
#else
std::vector<std::pair<BasicBlock*, unsigned> > Worklist;
Worklist.push_back(std::make_pair(V, 0U));
while (!Worklist.empty()) {
BasicBlock *BB = Worklist.back().first;
unsigned NextSucc = Worklist.back().second;
// First time we visited this BB?
if (NextSucc == 0) {
InfoRec &BBInfo = Info[BB];
BBInfo.Semi = ++N;
BBInfo.Label = BB;
Vertex.push_back(BB); // Vertex[n] = V;
//BBInfo[V].Ancestor = 0; // Ancestor[n] = 0
//BBInfo[V].Child = 0; // Child[v] = 0
BBInfo.Size = 1; // Size[v] = 1
}
// If we are done with this block, remove it from the worklist.
if (NextSucc == BB->getTerminator()->getNumSuccessors()) {
Worklist.pop_back();
continue;
}
// Otherwise, increment the successor number for the next time we get to it.
++Worklist.back().second;
// Visit the successor next, if it isn't already visited.
BasicBlock *Succ = BB->getTerminator()->getSuccessor(NextSucc);
InfoRec &SuccVInfo = Info[Succ];
if (SuccVInfo.Semi == 0) {
SuccVInfo.Parent = BB;
Worklist.push_back(std::make_pair(Succ, 0U));
}
}
#endif
return N;
}
void DominatorTree::Compress(BasicBlock *VIn) {
std::vector<BasicBlock *> Work;
std::set<BasicBlock *> Visited;
InfoRec &VInInfo = Info[VIn];
BasicBlock *VInAncestor = VInInfo.Ancestor;
InfoRec &VInVAInfo = Info[VInAncestor];
if (VInVAInfo.Ancestor != 0)
Work.push_back(VIn);
while (!Work.empty()) {
BasicBlock *V = Work.back();
InfoRec &VInfo = Info[V];
BasicBlock *VAncestor =