blob: e3169a5c83812300a392e86e05adb7772e984eb8 [file] [log] [blame]
Chris Lattner4c9df7c2002-08-02 16:43:03 +00001//===- PostDominators.cpp - Post-Dominator Calculation --------------------===//
Misha Brukman2b37d7c2005-04-21 21:13:18 +00002//
John Criswellb576c942003-10-20 19:43:21 +00003// 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.
Misha Brukman2b37d7c2005-04-21 21:13:18 +00007//
John Criswellb576c942003-10-20 19:43:21 +00008//===----------------------------------------------------------------------===//
Chris Lattner17152292001-07-02 05:46:38 +00009//
Chris Lattner4c9df7c2002-08-02 16:43:03 +000010// This file implements the post-dominator construction algorithms.
Chris Lattner17152292001-07-02 05:46:38 +000011//
12//===----------------------------------------------------------------------===//
13
Chris Lattnera69fd902002-08-21 23:43:50 +000014#include "llvm/Analysis/PostDominators.h"
Misha Brukman47b14a42004-07-29 17:30:56 +000015#include "llvm/Instructions.h"
Chris Lattner221d6882002-02-12 21:07:25 +000016#include "llvm/Support/CFG.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000017#include "llvm/ADT/DepthFirstIterator.h"
18#include "llvm/ADT/SetOperations.h"
Chris Lattnercd7c2872003-12-07 00:35:42 +000019using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000020
Chris Lattner94108ab2001-07-06 16:58:22 +000021//===----------------------------------------------------------------------===//
Owen Anderson3dc67762007-04-15 08:47:27 +000022// PostDominatorTree Implementation
Nate Begeman442b32b2006-03-11 02:20:46 +000023//===----------------------------------------------------------------------===//
24
Devang Patel19974732007-05-03 01:11:54 +000025char PostDominatorTree::ID = 0;
26char PostDominanceFrontier::ID = 0;
Owen Anderson3dc67762007-04-15 08:47:27 +000027static RegisterPass<PostDominatorTree>
28F("postdomtree", "Post-Dominator Tree Construction", true);
Nate Begeman442b32b2006-03-11 02:20:46 +000029
Owen Anderson3dc67762007-04-15 08:47:27 +000030unsigned PostDominatorTree::DFSPass(BasicBlock *V, InfoRec &VInfo,
Chris Lattner0a5f83c2007-08-04 23:48:07 +000031 unsigned N) {
Devang Patelc8719e92006-09-07 23:22:37 +000032 std::vector<std::pair<BasicBlock *, InfoRec *> > workStack;
Devang Patel79db5b72006-09-27 17:18:05 +000033 std::set<BasicBlock *> visited;
Devang Patelc8719e92006-09-07 23:22:37 +000034 workStack.push_back(std::make_pair(V, &VInfo));
35
36 do {
37 BasicBlock *currentBB = workStack.back().first;
38 InfoRec *currentVInfo = workStack.back().second;
Devang Patelc8719e92006-09-07 23:22:37 +000039
Devang Patel79db5b72006-09-27 17:18:05 +000040 // Visit each block only once.
41 if (visited.count(currentBB) == 0) {
Devang Patelc8719e92006-09-07 23:22:37 +000042
Devang Patel79db5b72006-09-27 17:18:05 +000043 visited.insert(currentBB);
44 currentVInfo->Semi = ++N;
45 currentVInfo->Label = currentBB;
46
47 Vertex.push_back(currentBB); // Vertex[n] = current;
48 // Info[currentBB].Ancestor = 0;
49 // Ancestor[n] = 0
50 // Child[currentBB] = 0;
51 currentVInfo->Size = 1; // Size[currentBB] = 1
52 }
Devang Patelc8719e92006-09-07 23:22:37 +000053
Devang Patel79db5b72006-09-27 17:18:05 +000054 // Visit children
55 bool visitChild = false;
Devang Patelc8719e92006-09-07 23:22:37 +000056 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
Devang Patel79db5b72006-09-27 17:18:05 +000057 PI != PE && !visitChild; ++PI) {
Devang Patelc8719e92006-09-07 23:22:37 +000058 InfoRec &SuccVInfo = Info[*PI];
59 if (SuccVInfo.Semi == 0) {
Devang Patelf93f6832006-09-07 23:29:19 +000060 SuccVInfo.Parent = currentBB;
Devang Patel79db5b72006-09-27 17:18:05 +000061 if (visited.count (*PI) == 0) {
62 workStack.push_back(std::make_pair(*PI, &SuccVInfo));
63 visitChild = true;
64 }
Devang Patelc8719e92006-09-07 23:22:37 +000065 }
Nate Begeman442b32b2006-03-11 02:20:46 +000066 }
Devang Patel79db5b72006-09-27 17:18:05 +000067
68 // If all children are visited or if this block has no child then pop this
69 // block out of workStack.
70 if (!visitChild)
71 workStack.pop_back();
72
Devang Patelc8719e92006-09-07 23:22:37 +000073 } while (!workStack.empty());
Devang Patel79db5b72006-09-27 17:18:05 +000074
Nate Begeman442b32b2006-03-11 02:20:46 +000075 return N;
76}
77
Owen Anderson3dc67762007-04-15 08:47:27 +000078void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
Nate Begeman442b32b2006-03-11 02:20:46 +000079 BasicBlock *VAncestor = VInfo.Ancestor;
80 InfoRec &VAInfo = Info[VAncestor];
81 if (VAInfo.Ancestor == 0)
82 return;
83
84 Compress(VAncestor, VAInfo);
85
86 BasicBlock *VAncestorLabel = VAInfo.Label;
87 BasicBlock *VLabel = VInfo.Label;
88 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
89 VInfo.Label = VAncestorLabel;
90
91 VInfo.Ancestor = VAInfo.Ancestor;
92}
93
Owen Anderson3dc67762007-04-15 08:47:27 +000094BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
Nate Begeman442b32b2006-03-11 02:20:46 +000095 InfoRec &VInfo = Info[V];
96
97 // Higher-complexity but faster implementation
98 if (VInfo.Ancestor == 0)
99 return V;
100 Compress(V, VInfo);
101 return VInfo.Label;
102}
103
Owen Anderson3dc67762007-04-15 08:47:27 +0000104void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
Nate Begeman442b32b2006-03-11 02:20:46 +0000105 InfoRec &WInfo) {
106 // Higher-complexity but faster implementation
107 WInfo.Ancestor = V;
108}
109
Owen Anderson3dc67762007-04-15 08:47:27 +0000110void PostDominatorTree::calculate(Function &F) {
Nate Begeman442b32b2006-03-11 02:20:46 +0000111 // Step #0: Scan the function looking for the root nodes of the post-dominance
112 // relationships. These blocks, which have no successors, end with return and
113 // unwind instructions.
Chris Lattner0a5f83c2007-08-04 23:48:07 +0000114 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
115 TerminatorInst *Insn = I->getTerminator();
116 if (Insn->getNumSuccessors() == 0) {
Devang Patel62e279b2007-07-24 01:02:25 +0000117 // Unreachable block is not a root node.
118 if (!isa<UnreachableInst>(Insn))
119 Roots.push_back(I);
120 }
Chris Lattner0a5f83c2007-08-04 23:48:07 +0000121
122 // Prepopulate maps so that we don't get iterator invalidation issues later.
123 IDoms[I] = 0;
124 DomTreeNodes[I] = 0;
125 }
Nate Begeman442b32b2006-03-11 02:20:46 +0000126
127 Vertex.push_back(0);
128
129 // Step #1: Number blocks in depth-first order and initialize variables used
130 // in later stages of the algorithm.
131 unsigned N = 0;
132 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
133 N = DFSPass(Roots[i], Info[Roots[i]], N);
134
135 for (unsigned i = N; i >= 2; --i) {
136 BasicBlock *W = Vertex[i];
137 InfoRec &WInfo = Info[W];
138
139 // Step #2: Calculate the semidominators of all vertices
140 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
141 if (Info.count(*SI)) { // Only if this predecessor is reachable!
142 unsigned SemiU = Info[Eval(*SI)].Semi;
143 if (SemiU < WInfo.Semi)
144 WInfo.Semi = SemiU;
145 }
146
147 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
148
149 BasicBlock *WParent = WInfo.Parent;
150 Link(WParent, W, WInfo);
151
152 // Step #3: Implicitly define the immediate dominator of vertices
153 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
154 while (!WParentBucket.empty()) {
155 BasicBlock *V = WParentBucket.back();
156 WParentBucket.pop_back();
157 BasicBlock *U = Eval(V);
158 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
159 }
160 }
161
162 // Step #4: Explicitly define the immediate dominator of each vertex
163 for (unsigned i = 2; i <= N; ++i) {
164 BasicBlock *W = Vertex[i];
165 BasicBlock *&WIDom = IDoms[W];
166 if (WIDom != Vertex[Info[W].Semi])
167 WIDom = IDoms[WIDom];
168 }
169
Chris Lattner706e61e2003-09-10 20:37:08 +0000170 if (Roots.empty()) return;
Nate Begeman442b32b2006-03-11 02:20:46 +0000171
172 // Add a node for the root. This node might be the actual root, if there is
173 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
174 // which postdominates all real exits if there are multiple exit blocks.
Chris Lattner706e61e2003-09-10 20:37:08 +0000175 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Devang Patel4d42dea2007-06-12 00:54:38 +0000176 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
Nate Begeman442b32b2006-03-11 02:20:46 +0000177
Nate Begeman442b32b2006-03-11 02:20:46 +0000178 // Loop over all of the reachable blocks in the function...
Owen Anderson3dc67762007-04-15 08:47:27 +0000179 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
180 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
Devang Patelbec76472007-06-03 06:26:14 +0000181 DomTreeNode *&BBNode = DomTreeNodes[I];
Nate Begeman442b32b2006-03-11 02:20:46 +0000182 if (!BBNode) { // Haven't calculated this node yet?
183 // Get or calculate the node for the immediate dominator
Devang Patelbec76472007-06-03 06:26:14 +0000184 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
Nate Begeman442b32b2006-03-11 02:20:46 +0000185
186 // Add a new tree node for this BasicBlock, and link it as a child of
187 // IDomNode
Devang Patel4d42dea2007-06-12 00:54:38 +0000188 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
Devang Patel9a511572007-06-07 17:47:21 +0000189 DomTreeNodes[I] = C;
190 BBNode = IPDomNode->addChild(C);
Chris Lattner17152292001-07-02 05:46:38 +0000191 }
192 }
Owen Anderson3dc67762007-04-15 08:47:27 +0000193
194 // Free temporary memory used to construct idom's
195 IDoms.clear();
196 Info.clear();
197 std::vector<BasicBlock*>().swap(Vertex);
Devang Patel9a511572007-06-07 17:47:21 +0000198
199 int dfsnum = 0;
200 // Iterate over all nodes in depth first order...
201 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
202 for (idf_iterator<BasicBlock*> I = idf_begin(Roots[i]),
203 E = idf_end(Roots[i]); I != E; ++I) {
Devang Patel4d42dea2007-06-12 00:54:38 +0000204 if (!getNodeForBlock(*I)->getIDom())
205 getNodeForBlock(*I)->assignDFSNumber(dfsnum);
Devang Patel9a511572007-06-07 17:47:21 +0000206 }
207 DFSInfoValid = true;
Owen Anderson3dc67762007-04-15 08:47:27 +0000208}
209
210
Devang Patel26042422007-06-04 00:32:22 +0000211DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
Devang Patelbec76472007-06-03 06:26:14 +0000212 DomTreeNode *&BBNode = DomTreeNodes[BB];
Owen Anderson3dc67762007-04-15 08:47:27 +0000213 if (BBNode) return BBNode;
214
215 // Haven't calculated this node yet? Get or calculate the node for the
216 // immediate postdominator.
217 BasicBlock *IPDom = getIDom(BB);
Devang Patelbec76472007-06-03 06:26:14 +0000218 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
Owen Anderson3dc67762007-04-15 08:47:27 +0000219
220 // Add a new tree node for this BasicBlock, and link it as a child of
221 // IDomNode
Devang Patel4d42dea2007-06-12 00:54:38 +0000222 DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
Devang Patel9a511572007-06-07 17:47:21 +0000223 DomTreeNodes[BB] = C;
224 return BBNode = IPDomNode->addChild(C);
Chris Lattner17152292001-07-02 05:46:38 +0000225}
Nate Begeman442b32b2006-03-11 02:20:46 +0000226
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000227//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000228// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000229//===----------------------------------------------------------------------===//
230
Chris Lattner5d8925c2006-08-27 22:30:17 +0000231static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000232H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000233
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000234const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000235PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Devang Patel26042422007-06-04 00:32:22 +0000236 const DomTreeNode *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000237 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000238 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000239 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000240 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000241
Chris Lattner706e61e2003-09-10 20:37:08 +0000242 if (BB)
243 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel5a713cc2007-04-18 01:19:55 +0000244 SI != SE; ++SI) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000245 // Does Node immediately dominate this predecessor?
Devang Patel26042422007-06-04 00:32:22 +0000246 DomTreeNode *SINode = DT[*SI];
Devang Patel5a713cc2007-04-18 01:19:55 +0000247 if (SINode && SINode->getIDom() != Node)
Chris Lattner706e61e2003-09-10 20:37:08 +0000248 S.insert(*SI);
Devang Patel5a713cc2007-04-18 01:19:55 +0000249 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000250
251 // At this point, S is DFlocal. Now we union in DFup's of our children...
252 // Loop through and visit the nodes that Node immediately dominates (Node's
253 // children in the IDomTree)
254 //
Devang Patel26042422007-06-04 00:32:22 +0000255 for (DomTreeNode::const_iterator
Chris Lattnerce6ef112002-07-26 18:40:14 +0000256 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Devang Patel26042422007-06-04 00:32:22 +0000257 DomTreeNode *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000258 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000259
260 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
261 for (; CDFI != CDFE; ++CDFI) {
Devang Patel9a511572007-06-07 17:47:21 +0000262 if (!DT.properlyDominates(Node, DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000263 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000264 }
265 }
266
267 return S;
268}
Chris Lattnera69fd902002-08-21 23:43:50 +0000269
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000270// Ensure that this .cpp file gets linked when PostDominators.h is used.
271DEFINING_FILE_FOR(PostDominanceFrontier)