blob: cd29749c830e65c15fc6a6ee669929176264c368 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by the LLVM research group and is distributed under
6// the University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the post-dominator construction algorithms.
11//
12//===----------------------------------------------------------------------===//
13
14#include "llvm/Analysis/PostDominators.h"
15#include "llvm/Instructions.h"
16#include "llvm/Support/CFG.h"
17#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/SetOperations.h"
Owen Anderson0ce522f2007-09-23 22:21:00 +000019#include "PostDominatorCalculation.h"
Dan Gohmanf17a25c2007-07-18 16:29:46 +000020using namespace llvm;
21
22//===----------------------------------------------------------------------===//
23// PostDominatorTree Implementation
24//===----------------------------------------------------------------------===//
25
26char PostDominatorTree::ID = 0;
27char PostDominanceFrontier::ID = 0;
28static RegisterPass<PostDominatorTree>
29F("postdomtree", "Post-Dominator Tree Construction", true);
30
Dan Gohmanf17a25c2007-07-18 16:29:46 +000031//===----------------------------------------------------------------------===//
32// PostDominanceFrontier Implementation
33//===----------------------------------------------------------------------===//
34
35static RegisterPass<PostDominanceFrontier>
36H("postdomfrontier", "Post-Dominance Frontier Construction", true);
37
38const DominanceFrontier::DomSetType &
39PostDominanceFrontier::calculate(const PostDominatorTree &DT,
40 const DomTreeNode *Node) {
41 // Loop over CFG successors to calculate DFlocal[Node]
42 BasicBlock *BB = Node->getBlock();
43 DomSetType &S = Frontiers[BB]; // The new set to fill in...
44 if (getRoots().empty()) return S;
45
46 if (BB)
47 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
48 SI != SE; ++SI) {
49 // Does Node immediately dominate this predecessor?
50 DomTreeNode *SINode = DT[*SI];
51 if (SINode && SINode->getIDom() != Node)
52 S.insert(*SI);
53 }
54
55 // At this point, S is DFlocal. Now we union in DFup's of our children...
56 // Loop through and visit the nodes that Node immediately dominates (Node's
57 // children in the IDomTree)
58 //
59 for (DomTreeNode::const_iterator
60 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
61 DomTreeNode *IDominee = *NI;
62 const DomSetType &ChildDF = calculate(DT, IDominee);
63
64 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
65 for (; CDFI != CDFE; ++CDFI) {
66 if (!DT.properlyDominates(Node, DT[*CDFI]))
67 S.insert(*CDFI);
68 }
69 }
70
71 return S;
72}
73
74// Ensure that this .cpp file gets linked when PostDominators.h is used.
75DEFINING_FILE_FOR(PostDominanceFrontier)