-void DominatorTreeBase::reset() {
- for (NodeMapType::iterator I = Nodes.begin(), E = Nodes.end(); I != E; ++I)
- delete I->second;
- Nodes.clear();
- RootNode = 0;
-}
-
-void DominatorTreeBase::Node::setIDom(Node *NewIDom) {
- assert(IDom && "No immediate dominator?");
- if (IDom != NewIDom) {
- std::vector<Node*>::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);
- }
-}
-
-DominatorTreeBase::Node *DominatorTree::getNodeForBlock(BasicBlock *BB) {
- Node *&BBNode = Nodes[BB];
- if (BBNode) return BBNode;
-
- // Haven't calculated this node yet? Get or calculate the node for the
- // immediate dominator.
- BasicBlock *IDom = getAnalysis<ImmediateDominators>()[BB];
- Node *IDomNode = getNodeForBlock(IDom);
-
- // Add a new tree node for this BasicBlock, and link it as a child of
- // IDomNode
- return BBNode = IDomNode->addChild(new Node(BB, IDomNode));
-}
-
-void DominatorTree::calculate(const ImmediateDominators &ID) {
- assert(Roots.size() == 1 && "DominatorTree should have 1 root block!");
- BasicBlock *Root = Roots[0];
- Nodes[Root] = RootNode = new Node(Root, 0); // Add a node for the root...
-
- Function *F = Root->getParent();
- // Loop over all of the reachable blocks in the function...
- for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
- if (BasicBlock *ImmDom = ID.get(I)) { // Reachable block.
- Node *&BBNode = Nodes[I];
- if (!BBNode) { // Haven't calculated this node yet?
- // Get or calculate the node for the immediate dominator
- Node *IDomNode = getNodeForBlock(ImmDom);
-
- // Add a new tree node for this BasicBlock, and link it as a child of
- // IDomNode
- BBNode = IDomNode->addChild(new Node(I, IDomNode));
- }
- }
-}
-
-static std::ostream &operator<<(std::ostream &o,
- const DominatorTreeBase::Node *Node) {
- if (Node->getBlock())
- WriteAsOperand(o, Node->getBlock(), false);
- else
- o << " <<exit node>>";
- return o << "\n";
-}
-
-static void PrintDomTree(const DominatorTreeBase::Node *N, std::ostream &o,
- unsigned Lev) {
- o << std::string(2*Lev, ' ') << "[" << Lev << "] " << N;
- for (DominatorTreeBase::Node::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);
-}
-
-
-//===----------------------------------------------------------------------===//
-// DominanceFrontier Implementation
-//===----------------------------------------------------------------------===//
-
-static RegisterPass<DominanceFrontier>
-G("domfrontier", "Dominance Frontier Construction", true);
-
-const DominanceFrontier::DomSetType &
-DominanceFrontier::calculate(const DominatorTree &DT,
- const DominatorTree::Node *Node) {
- BasicBlock *BB = Node->getBlock();
- DomSetType *Result = NULL;
-
- std::vector<DFCalculateWorkObject> workList;
- SmallPtrSet<BasicBlock *, 32> visited;
-
- workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
- do {
- DFCalculateWorkObject *currentW = &workList.back();
- assert (currentW && "Missing work object.");
-
- BasicBlock *currentBB = currentW->currentBB;
- BasicBlock *parentBB = currentW->parentBB;
- const DominatorTree::Node *currentNode = currentW->Node;
- const DominatorTree::Node *parentNode = currentW->parentNode;
- assert (currentBB && "Invalid work object. Missing current Basic Block");
- assert (currentNode && "Invalid work object. Missing current Node");
- DomSetType &S = Frontiers[currentBB];
-
- // Visit each block only once.
- if (visited.count(currentBB) == 0) {
- visited.insert(currentBB);
-
- // Loop over CFG successors to calculate DFlocal[currentNode]
- for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
- SI != SE; ++SI) {
- // Does Node immediately dominate this successor?
- if (DT[*SI]->getIDom() != currentNode)
- S.insert(*SI);
- }
- }
-
- // At this point, S is DFlocal. Now we union in DFup's of our children...
- // Loop through and visit the nodes that Node immediately dominates (Node's
- // children in the IDomTree)
- bool visitChild = false;
- for (DominatorTree::Node::const_iterator NI = currentNode->begin(),
- NE = currentNode->end(); NI != NE; ++NI) {
- DominatorTree::Node *IDominee = *NI;
- BasicBlock *childBB = IDominee->getBlock();
- if (visited.count(childBB) == 0) {
- workList.push_back(DFCalculateWorkObject(childBB, currentBB, IDominee, currentNode));
- visitChild = true;
- }
- }
-
- // If all children are visited or there is any child then pop this block
- // from the workList.
- if (!visitChild) {
-
- if (!parentBB) {
- Result = &S;
- break;
- }
-
- DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
- DomSetType &parentSet = Frontiers[parentBB];
- for (; CDFI != CDFE; ++CDFI) {
- if (!parentNode->properlyDominates(DT[*CDFI]))
- parentSet.insert(*CDFI);
- }
- workList.pop_back();
- }
-
- } while (!workList.empty());
-
- return *Result;
-}
-
-void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
- for (const_iterator I = begin(), E = end(); I != E; ++I) {
- o << " DomFrontier for BB";
- if (I->first)
- WriteAsOperand(o, I->first, false);
- else
- o << " <<exit node>>";
- o << " is:\t" << I->second << "\n";
- }
-}
-
-//===----------------------------------------------------------------------===//
-// ETOccurrence Implementation
-//===----------------------------------------------------------------------===//
-
-void ETOccurrence::Splay() {
- ETOccurrence *father;
- ETOccurrence *grandfather;
- int occdepth;
- int fatherdepth;
-
- while (Parent) {
- occdepth = Depth;
-
- father = Parent;
- fatherdepth = Parent->Depth;
- grandfather = father->Parent;
-
- // If we have no grandparent, a single zig or zag will do.
- if (!grandfather) {
- setDepthAdd(fatherdepth);
- MinOccurrence = father->MinOccurrence;
- Min = father->Min;
-
- // See what we have to rotate
- if (father->Left == this) {
- // Zig
- father->setLeft(Right);
- setRight(father);
- if (father->Left)
- father->Left->setDepthAdd(occdepth);
- } else {
- // Zag
- father->setRight(Left);
- setLeft(father);
- if (father->Right)
- father->Right->setDepthAdd(occdepth);
- }
- father->setDepth(-occdepth);
- Parent = NULL;
-
- father->recomputeMin();
- return;
- }
-
- // If we have a grandfather, we need to do some
- // combination of zig and zag.
- int grandfatherdepth = grandfather->Depth;
-
- setDepthAdd(fatherdepth + grandfatherdepth);
- MinOccurrence = grandfather->MinOccurrence;
- Min = grandfather->Min;
-
- ETOccurrence *greatgrandfather = grandfather->Parent;
-
- if (grandfather->Left == father) {
- if (father->Left == this) {
- // Zig zig
- grandfather->setLeft(father->Right);
- father->setLeft(Right);
- setRight(father);
- father->setRight(grandfather);
-
- father->setDepth(-occdepth);
-
- if (father->Left)
- father->Left->setDepthAdd(occdepth);
-
- grandfather->setDepth(-fatherdepth);
- if (grandfather->Left)
- grandfather->Left->setDepthAdd(fatherdepth);
- } else {
- // Zag zig
- grandfather->setLeft(Right);
- father->setRight(Left);
- setLeft(father);
- setRight(grandfather);
-
- father->setDepth(-occdepth);
- if (father->Right)
- father->Right->setDepthAdd(occdepth);
- grandfather->setDepth(-occdepth - fatherdepth);
- if (grandfather->Left)
- grandfather->Left->setDepthAdd(occdepth + fatherdepth);
- }
- } else {
- if (father->Left == this) {
- // Zig zag
- grandfather->setRight(Left);
- father->setLeft(Right);
- setLeft(grandfather);
- setRight(father);
-
- father->setDepth(-occdepth);
- if (father->Left)
- father->Left->setDepthAdd(occdepth);
- grandfather->setDepth(-occdepth - fatherdepth);
- if (grandfather->Right)
- grandfather->Right->setDepthAdd(occdepth + fatherdepth);
- } else { // Zag Zag
- grandfather->setRight(father->Left);
- father->setRight(Left);
- setLeft(father);
- father->setLeft(grandfather);
-
- father->setDepth(-occdepth);
- if (father->Right)
- father->Right->setDepthAdd(occdepth);
- grandfather->setDepth(-fatherdepth);
- if (grandfather->Right)
- grandfather->Right->setDepthAdd(fatherdepth);
- }
- }
-
- // Might need one more rotate depending on greatgrandfather.
- setParent(greatgrandfather);
- if (greatgrandfather) {
- if (greatgrandfather->Left == grandfather)
- greatgrandfather->Left = this;
- else
- greatgrandfather->Right = this;
-
- }
- grandfather->recomputeMin();
- father->recomputeMin();
- }
-}
-
-//===----------------------------------------------------------------------===//
-// ETNode implementation