-//===----------------------------------------------------------------------===//
-// DominanceFrontier Implementation
-//===----------------------------------------------------------------------===//
-
-static RegisterPass<DominanceFrontier>
-G("domfrontier", "Dominance Frontier Construction", true);
-
-namespace {
- class DFCalculateWorkObject {
- public:
- DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
- const DominatorTree::Node *N,
- const DominatorTree::Node *PN)
- : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
- BasicBlock *currentBB;
- BasicBlock *parentBB;
- const DominatorTree::Node *Node;
- const DominatorTree::Node *parentNode;
- };
-}
-
-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
-//===----------------------------------------------------------------------===//
-
-void ETNode::Split() {
- ETOccurrence *right, *left;
- ETOccurrence *rightmost = RightmostOcc;
- ETOccurrence *parent;
-
- // Update the occurrence tree first.
- RightmostOcc->Splay();
-
- // Find the leftmost occurrence in the rightmost subtree, then splay
- // around it.
- for (right = rightmost->Right; right->Left; right = right->Left);
-
- right->Splay();
-
- // Start splitting
- right->Left->Parent = NULL;
- parent = ParentOcc;
- parent->Splay();
- ParentOcc = NULL;
-
- left = parent->Left;
- parent->Right->Parent = NULL;
-
- right->setLeft(left);
-
- right->recomputeMin();
-
- rightmost->Splay();
- rightmost->Depth = 0;
- rightmost->Min = 0;
-
- delete parent;
-
- // Now update *our* tree
-
- if (Father->Son == this)
- Father->Son = Right;
-
- if (Father->Son == this)
- Father->Son = NULL;
- else {
- Left->Right = Right;
- Right->Left = Left;
- }
- Left = Right = NULL;
- Father = NULL;
-}
-
-void ETNode::setFather(ETNode *NewFather) {
- ETOccurrence *rightmost;
- ETOccurrence *leftpart;
- ETOccurrence *NewFatherOcc;
- ETOccurrence *temp;
-
- // First update the path in the splay tree
- NewFatherOcc = new ETOccurrence(NewFather);
-
- rightmost = NewFather->RightmostOcc;
- rightmost->Splay();
-
- leftpart = rightmost->Left;
-
- temp = RightmostOcc;
- temp->Splay();