- if(!IsPostDominators) {
- // A dominates B if it is found first in the basic block.
- return &*I == A;
- } else {
- // A post-dominates B if B is found first in the basic block.
- return &*I == B;
- }
-}
-
-// DominatorTreeBase::reset - Free all of the tree node memory.
-//
-void DominatorTreeBase::reset() {
- for (DomTreeNodeMapType::iterator I = DomTreeNodes.begin(),
- E = DomTreeNodes.end(); I != E; ++I)
- delete I->second;
- DomTreeNodes.clear();
- IDoms.clear();
- Roots.clear();
- Vertex.clear();
- RootNode = 0;
-}
-
-/// findNearestCommonDominator - Find nearest common dominator basic block
-/// for basic block A and B. If there is no such block then return NULL.
-BasicBlock *DominatorTreeBase::findNearestCommonDominator(BasicBlock *A,
- BasicBlock *B) {
-
- assert (!isPostDominator()
- && "This is not implemented for post dominators");
- assert (A->getParent() == B->getParent()
- && "Two blocks are not in same function");
-
- // If either A or B is a entry block then it is nearest common dominator.
- BasicBlock &Entry = A->getParent()->getEntryBlock();
- if (A == &Entry || B == &Entry)
- return &Entry;
-
- // If B dominates A then B is nearest common dominator.
- if (dominates(B,A))
- return B;
-
- // If A dominates B then A is nearest common dominator.
- if (dominates(A,B))
- return A;
-
- DomTreeNode *NodeA = getNode(A);
- DomTreeNode *NodeB = getNode(B);
-
- // Collect NodeA dominators set.
- SmallPtrSet<DomTreeNode*, 16> NodeADoms;
- NodeADoms.insert(NodeA);
- DomTreeNode *IDomA = NodeA->getIDom();
- while(IDomA) {
- NodeADoms.insert(IDomA);
- IDomA = IDomA->getIDom();
- }
-
- // Walk NodeB immediate dominators chain and find common dominator node.
- DomTreeNode *IDomB = NodeB->getIDom();
- while(IDomB) {
- if (NodeADoms.count(IDomB) != 0)
- return IDomB->getBlock();
-
- IDomB = IDomB->getIDom();
- }
-
- return NULL;
-}
-
-/// assignDFSNumber - Assign In and Out numbers while walking dominator tree
-/// in dfs order.
-void DomTreeNode::assignDFSNumber(int num) {
- std::vector<DomTreeNode *> workStack;
- std::set<DomTreeNode *> visitedNodes;
-
- workStack.push_back(this);
- visitedNodes.insert(this);
- this->DFSNumIn = num++;
-
- while (!workStack.empty()) {
- DomTreeNode *Node = workStack.back();
-
- bool visitChild = false;
- for (std::vector<DomTreeNode*>::iterator DI = Node->begin(),
- E = Node->end(); DI != E && !visitChild; ++DI) {
- DomTreeNode *Child = *DI;
- if (visitedNodes.count(Child) == 0) {
- visitChild = true;
- Child->DFSNumIn = num++;
- workStack.push_back(Child);
- visitedNodes.insert(Child);
- }
- }
- if (!visitChild) {
- // If we reach here means all children are visited
- Node->DFSNumOut = num++;
- workStack.pop_back();
- }
- }
-}
-
-void DomTreeNode::setIDom(DomTreeNode *NewIDom) {
- assert(IDom && "No immediate dominator?");
- if (IDom != NewIDom) {
- std::vector<DomTreeNode*>::iterator I =
- std::find(IDom->Children.begin(), IDom->Children.end(), this);
- assert(I != IDom->Children.end() &&
- "Not in immediate dominator children set!");
- // I am no longer your child...
- IDom->Children.erase(I);
-
- // Switch to new dominator
- IDom = NewIDom;
- IDom->Children.push_back(this);
- }
-}
-
-DomTreeNode *DominatorTree::getNodeForBlock(BasicBlock *BB) {
- DomTreeNode *&BBNode = DomTreeNodes[BB];
- if (BBNode) return BBNode;
-
- // Haven't calculated this node yet? Get or calculate the node for the
- // immediate dominator.
- BasicBlock *IDom = getIDom(BB);
- DomTreeNode *IDomNode = getNodeForBlock(IDom);
-
- // Add a new tree node for this BasicBlock, and link it as a child of
- // IDomNode
- DomTreeNode *C = new DomTreeNode(BB, IDomNode);
- DomTreeNodes[BB] = C;
- return BBNode = IDomNode->addChild(C);
-}
-
-static std::ostream &operator<<(std::ostream &o,
- const DomTreeNode *Node) {
- if (Node->getBlock())
- WriteAsOperand(o, Node->getBlock(), false);
- else
- o << " <<exit node>>";
- return o << "\n";
-}
-
-static void PrintDomTree(const DomTreeNode *N, std::ostream &o,
- unsigned Lev) {
- o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
- for (DomTreeNode::const_iterator I = N->begin(), E = N->end();
- I != E; ++I)
- PrintDomTree(*I, o, Lev+1);
-}
-
-void DominatorTreeBase::print(std::ostream &o, const Module* ) const {
- o << "=============================--------------------------------\n"
- << "Inorder Dominator Tree:\n";
- PrintDomTree(getRootNode(), o, 1);
-}
-
-void DominatorTreeBase::dump() {
- print (llvm::cerr);
-}
-
-bool DominatorTree::runOnFunction(Function &F) {
- reset(); // Reset from the last time we were run...
- Roots.push_back(&F.getEntryBlock());
- calculate(F);
- return false;
-}
-
-//===----------------------------------------------------------------------===//
-// DominanceFrontier Implementation
-//===----------------------------------------------------------------------===//
-
-char DominanceFrontier::ID = 0;
-static RegisterPass<DominanceFrontier>
-G("domfrontier", "Dominance Frontier Construction", true);
-
-// NewBB is split and now it has one successor. Update dominace frontier to
-// reflect this change.
-void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
-
- assert(NewBB->getTerminator()->getNumSuccessors() == 1
- && "NewBB should have a single successor!");
- BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
-
- std::vector<BasicBlock*> PredBlocks;
- for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
- PI != PE; ++PI)
- PredBlocks.push_back(*PI);
-
- if (PredBlocks.empty())
- // If NewBB does not have any predecessors then it is a entry block.
- // In this case, NewBB and its successor NewBBSucc dominates all
- // other blocks.
- return;
-
- DominatorTree &DT = getAnalysis<DominatorTree>();
- bool NewBBDominatesNewBBSucc = true;
- if (!DT.dominates(NewBB, NewBBSucc))
- NewBBDominatesNewBBSucc = false;
-
- // NewBBSucc inherites original NewBB frontier.
- DominanceFrontier::iterator NewBBI = find(NewBB);
- if (NewBBI != end()) {
- DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
- DominanceFrontier::DomSetType NewBBSuccSet;
- NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end());
- addBasicBlock(NewBBSucc, NewBBSuccSet);
- }
-
- // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
- // DF(PredBlocks[0]) without the stuff that the new block does not dominate
- // a predecessor of.
- if (NewBBDominatesNewBBSucc) {
- DominanceFrontier::iterator DFI = find(PredBlocks[0]);
- if (DFI != end()) {
- DominanceFrontier::DomSetType Set = DFI->second;
- // Filter out stuff in Set that we do not dominate a predecessor of.
- for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
- E = Set.end(); SetI != E;) {
- bool DominatesPred = false;
- for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
- PI != E; ++PI)
- if (DT.dominates(NewBB, *PI))
- DominatesPred = true;
- if (!DominatesPred)
- Set.erase(SetI++);
- else
- ++SetI;
- }
-
- if (NewBBI != end()) {
- DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
- NewBBSet.insert(Set.begin(), Set.end());
- } else
- addBasicBlock(NewBB, Set);
- }
-
- } else {
- // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
- // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
- // NewBBSucc)). NewBBSucc is the single successor of NewBB.
- DominanceFrontier::DomSetType NewDFSet;
- NewDFSet.insert(NewBBSucc);
- addBasicBlock(NewBB, NewDFSet);
- }