blob: 69e9cbe26b8a405b262da5ec5f0cbec5b035f4a7 [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
Chris Lattnere93e3112007-08-05 00:02:00 +000030unsigned PostDominatorTree::DFSPass(BasicBlock *V, unsigned N) {
31 std::vector<BasicBlock *> workStack;
Chris Lattner2f0d1ea2007-08-05 00:15:57 +000032 SmallPtrSet<BasicBlock *, 32> Visited;
Chris Lattnere93e3112007-08-05 00:02:00 +000033 workStack.push_back(V);
Devang Patelc8719e92006-09-07 23:22:37 +000034
35 do {
Chris Lattnere93e3112007-08-05 00:02:00 +000036 BasicBlock *currentBB = workStack.back();
37 InfoRec &CurVInfo = Info[currentBB];
Devang Patelc8719e92006-09-07 23:22:37 +000038
Devang Patel79db5b72006-09-27 17:18:05 +000039 // Visit each block only once.
Chris Lattner2f0d1ea2007-08-05 00:15:57 +000040 if (Visited.insert(currentBB)) {
Chris Lattnere93e3112007-08-05 00:02:00 +000041 CurVInfo.Semi = ++N;
42 CurVInfo.Label = currentBB;
Devang Patel79db5b72006-09-27 17:18:05 +000043
44 Vertex.push_back(currentBB); // Vertex[n] = current;
45 // Info[currentBB].Ancestor = 0;
46 // Ancestor[n] = 0
47 // Child[currentBB] = 0;
Chris Lattnere93e3112007-08-05 00:02:00 +000048 CurVInfo.Size = 1; // Size[currentBB] = 1
Devang Patel79db5b72006-09-27 17:18:05 +000049 }
Devang Patelc8719e92006-09-07 23:22:37 +000050
Devang Patel79db5b72006-09-27 17:18:05 +000051 // Visit children
52 bool visitChild = false;
Devang Patelc8719e92006-09-07 23:22:37 +000053 for (pred_iterator PI = pred_begin(currentBB), PE = pred_end(currentBB);
Devang Patel79db5b72006-09-27 17:18:05 +000054 PI != PE && !visitChild; ++PI) {
Devang Patelc8719e92006-09-07 23:22:37 +000055 InfoRec &SuccVInfo = Info[*PI];
56 if (SuccVInfo.Semi == 0) {
Devang Patelf93f6832006-09-07 23:29:19 +000057 SuccVInfo.Parent = currentBB;
Chris Lattner2f0d1ea2007-08-05 00:15:57 +000058 if (!Visited.count(*PI)) {
Chris Lattnere93e3112007-08-05 00:02:00 +000059 workStack.push_back(*PI);
Devang Patel79db5b72006-09-27 17:18:05 +000060 visitChild = true;
61 }
Devang Patelc8719e92006-09-07 23:22:37 +000062 }
Nate Begeman442b32b2006-03-11 02:20:46 +000063 }
Devang Patel79db5b72006-09-27 17:18:05 +000064
65 // If all children are visited or if this block has no child then pop this
66 // block out of workStack.
67 if (!visitChild)
68 workStack.pop_back();
69
Devang Patelc8719e92006-09-07 23:22:37 +000070 } while (!workStack.empty());
Devang Patel79db5b72006-09-27 17:18:05 +000071
Nate Begeman442b32b2006-03-11 02:20:46 +000072 return N;
73}
74
Owen Anderson3dc67762007-04-15 08:47:27 +000075void PostDominatorTree::Compress(BasicBlock *V, InfoRec &VInfo) {
Nate Begeman442b32b2006-03-11 02:20:46 +000076 BasicBlock *VAncestor = VInfo.Ancestor;
77 InfoRec &VAInfo = Info[VAncestor];
78 if (VAInfo.Ancestor == 0)
79 return;
80
81 Compress(VAncestor, VAInfo);
82
83 BasicBlock *VAncestorLabel = VAInfo.Label;
84 BasicBlock *VLabel = VInfo.Label;
85 if (Info[VAncestorLabel].Semi < Info[VLabel].Semi)
86 VInfo.Label = VAncestorLabel;
87
88 VInfo.Ancestor = VAInfo.Ancestor;
89}
90
Owen Anderson3dc67762007-04-15 08:47:27 +000091BasicBlock *PostDominatorTree::Eval(BasicBlock *V) {
Nate Begeman442b32b2006-03-11 02:20:46 +000092 InfoRec &VInfo = Info[V];
93
94 // Higher-complexity but faster implementation
95 if (VInfo.Ancestor == 0)
96 return V;
97 Compress(V, VInfo);
98 return VInfo.Label;
99}
100
Owen Anderson3dc67762007-04-15 08:47:27 +0000101void PostDominatorTree::Link(BasicBlock *V, BasicBlock *W,
Nate Begeman442b32b2006-03-11 02:20:46 +0000102 InfoRec &WInfo) {
103 // Higher-complexity but faster implementation
104 WInfo.Ancestor = V;
105}
106
Owen Anderson3dc67762007-04-15 08:47:27 +0000107void PostDominatorTree::calculate(Function &F) {
Nate Begeman442b32b2006-03-11 02:20:46 +0000108 // Step #0: Scan the function looking for the root nodes of the post-dominance
109 // relationships. These blocks, which have no successors, end with return and
110 // unwind instructions.
Chris Lattner0a5f83c2007-08-04 23:48:07 +0000111 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I) {
112 TerminatorInst *Insn = I->getTerminator();
113 if (Insn->getNumSuccessors() == 0) {
Devang Patel62e279b2007-07-24 01:02:25 +0000114 // Unreachable block is not a root node.
115 if (!isa<UnreachableInst>(Insn))
116 Roots.push_back(I);
117 }
Chris Lattner0a5f83c2007-08-04 23:48:07 +0000118
119 // Prepopulate maps so that we don't get iterator invalidation issues later.
120 IDoms[I] = 0;
121 DomTreeNodes[I] = 0;
122 }
Nate Begeman442b32b2006-03-11 02:20:46 +0000123
124 Vertex.push_back(0);
125
126 // Step #1: Number blocks in depth-first order and initialize variables used
127 // in later stages of the algorithm.
128 unsigned N = 0;
129 for (unsigned i = 0, e = Roots.size(); i != e; ++i)
Chris Lattnere93e3112007-08-05 00:02:00 +0000130 N = DFSPass(Roots[i], N);
Nate Begeman442b32b2006-03-11 02:20:46 +0000131
132 for (unsigned i = N; i >= 2; --i) {
133 BasicBlock *W = Vertex[i];
134 InfoRec &WInfo = Info[W];
135
136 // Step #2: Calculate the semidominators of all vertices
137 for (succ_iterator SI = succ_begin(W), SE = succ_end(W); SI != SE; ++SI)
138 if (Info.count(*SI)) { // Only if this predecessor is reachable!
139 unsigned SemiU = Info[Eval(*SI)].Semi;
140 if (SemiU < WInfo.Semi)
141 WInfo.Semi = SemiU;
142 }
143
144 Info[Vertex[WInfo.Semi]].Bucket.push_back(W);
145
146 BasicBlock *WParent = WInfo.Parent;
147 Link(WParent, W, WInfo);
148
149 // Step #3: Implicitly define the immediate dominator of vertices
150 std::vector<BasicBlock*> &WParentBucket = Info[WParent].Bucket;
151 while (!WParentBucket.empty()) {
152 BasicBlock *V = WParentBucket.back();
153 WParentBucket.pop_back();
154 BasicBlock *U = Eval(V);
155 IDoms[V] = Info[U].Semi < Info[V].Semi ? U : WParent;
156 }
157 }
158
159 // Step #4: Explicitly define the immediate dominator of each vertex
160 for (unsigned i = 2; i <= N; ++i) {
161 BasicBlock *W = Vertex[i];
162 BasicBlock *&WIDom = IDoms[W];
163 if (WIDom != Vertex[Info[W].Semi])
164 WIDom = IDoms[WIDom];
165 }
166
Chris Lattner706e61e2003-09-10 20:37:08 +0000167 if (Roots.empty()) return;
Nate Begeman442b32b2006-03-11 02:20:46 +0000168
169 // Add a node for the root. This node might be the actual root, if there is
170 // one exit block, or it may be the virtual exit (denoted by (BasicBlock *)0)
171 // which postdominates all real exits if there are multiple exit blocks.
Chris Lattner706e61e2003-09-10 20:37:08 +0000172 BasicBlock *Root = Roots.size() == 1 ? Roots[0] : 0;
Devang Patel4d42dea2007-06-12 00:54:38 +0000173 DomTreeNodes[Root] = RootNode = new DomTreeNode(Root, 0);
Nate Begeman442b32b2006-03-11 02:20:46 +0000174
Nate Begeman442b32b2006-03-11 02:20:46 +0000175 // Loop over all of the reachable blocks in the function...
Owen Anderson3dc67762007-04-15 08:47:27 +0000176 for (Function::iterator I = F.begin(), E = F.end(); I != E; ++I)
177 if (BasicBlock *ImmPostDom = getIDom(I)) { // Reachable block.
Devang Patelbec76472007-06-03 06:26:14 +0000178 DomTreeNode *&BBNode = DomTreeNodes[I];
Nate Begeman442b32b2006-03-11 02:20:46 +0000179 if (!BBNode) { // Haven't calculated this node yet?
180 // Get or calculate the node for the immediate dominator
Devang Patelbec76472007-06-03 06:26:14 +0000181 DomTreeNode *IPDomNode = getNodeForBlock(ImmPostDom);
Nate Begeman442b32b2006-03-11 02:20:46 +0000182
183 // Add a new tree node for this BasicBlock, and link it as a child of
184 // IDomNode
Devang Patel4d42dea2007-06-12 00:54:38 +0000185 DomTreeNode *C = new DomTreeNode(I, IPDomNode);
Devang Patel9a511572007-06-07 17:47:21 +0000186 DomTreeNodes[I] = C;
187 BBNode = IPDomNode->addChild(C);
Chris Lattner17152292001-07-02 05:46:38 +0000188 }
189 }
Owen Anderson3dc67762007-04-15 08:47:27 +0000190
191 // Free temporary memory used to construct idom's
192 IDoms.clear();
193 Info.clear();
194 std::vector<BasicBlock*>().swap(Vertex);
Devang Patel9a511572007-06-07 17:47:21 +0000195
Chris Lattner3e089ae2007-08-08 05:51:24 +0000196 // Start out with the DFS numbers being invalid. Let them be computed if
197 // demanded.
198 DFSInfoValid = false;
Owen Anderson3dc67762007-04-15 08:47:27 +0000199}
200
201
Devang Patel26042422007-06-04 00:32:22 +0000202DomTreeNode *PostDominatorTree::getNodeForBlock(BasicBlock *BB) {
Devang Patelbec76472007-06-03 06:26:14 +0000203 DomTreeNode *&BBNode = DomTreeNodes[BB];
Owen Anderson3dc67762007-04-15 08:47:27 +0000204 if (BBNode) return BBNode;
205
206 // Haven't calculated this node yet? Get or calculate the node for the
207 // immediate postdominator.
208 BasicBlock *IPDom = getIDom(BB);
Devang Patelbec76472007-06-03 06:26:14 +0000209 DomTreeNode *IPDomNode = getNodeForBlock(IPDom);
Owen Anderson3dc67762007-04-15 08:47:27 +0000210
211 // Add a new tree node for this BasicBlock, and link it as a child of
212 // IDomNode
Devang Patel4d42dea2007-06-12 00:54:38 +0000213 DomTreeNode *C = new DomTreeNode(BB, IPDomNode);
Chris Lattnera3196532007-08-05 00:24:30 +0000214 return DomTreeNodes[BB] = IPDomNode->addChild(C);
Chris Lattner17152292001-07-02 05:46:38 +0000215}
Nate Begeman442b32b2006-03-11 02:20:46 +0000216
Chris Lattnerccacd3c2006-01-08 08:22:18 +0000217//===----------------------------------------------------------------------===//
Chris Lattner4c9df7c2002-08-02 16:43:03 +0000218// PostDominanceFrontier Implementation
Chris Lattner17152292001-07-02 05:46:38 +0000219//===----------------------------------------------------------------------===//
220
Chris Lattner5d8925c2006-08-27 22:30:17 +0000221static RegisterPass<PostDominanceFrontier>
Chris Lattner17689df2002-07-30 16:27:52 +0000222H("postdomfrontier", "Post-Dominance Frontier Construction", true);
Chris Lattner93193f82002-01-31 00:42:27 +0000223
Chris Lattner1b7f7dc2002-04-28 16:21:30 +0000224const DominanceFrontier::DomSetType &
Misha Brukman2b37d7c2005-04-21 21:13:18 +0000225PostDominanceFrontier::calculate(const PostDominatorTree &DT,
Devang Patel26042422007-06-04 00:32:22 +0000226 const DomTreeNode *Node) {
Chris Lattner94108ab2001-07-06 16:58:22 +0000227 // Loop over CFG successors to calculate DFlocal[Node]
Chris Lattnerc444a422003-09-11 16:26:13 +0000228 BasicBlock *BB = Node->getBlock();
Chris Lattner94108ab2001-07-06 16:58:22 +0000229 DomSetType &S = Frontiers[BB]; // The new set to fill in...
Chris Lattner706e61e2003-09-10 20:37:08 +0000230 if (getRoots().empty()) return S;
Chris Lattner94108ab2001-07-06 16:58:22 +0000231
Chris Lattner706e61e2003-09-10 20:37:08 +0000232 if (BB)
233 for (pred_iterator SI = pred_begin(BB), SE = pred_end(BB);
Devang Patel5a713cc2007-04-18 01:19:55 +0000234 SI != SE; ++SI) {
Misha Brukman2f2d0652003-09-11 18:14:24 +0000235 // Does Node immediately dominate this predecessor?
Devang Patel26042422007-06-04 00:32:22 +0000236 DomTreeNode *SINode = DT[*SI];
Devang Patel5a713cc2007-04-18 01:19:55 +0000237 if (SINode && SINode->getIDom() != Node)
Chris Lattner706e61e2003-09-10 20:37:08 +0000238 S.insert(*SI);
Devang Patel5a713cc2007-04-18 01:19:55 +0000239 }
Chris Lattner94108ab2001-07-06 16:58:22 +0000240
241 // At this point, S is DFlocal. Now we union in DFup's of our children...
242 // Loop through and visit the nodes that Node immediately dominates (Node's
243 // children in the IDomTree)
244 //
Devang Patel26042422007-06-04 00:32:22 +0000245 for (DomTreeNode::const_iterator
Chris Lattnerce6ef112002-07-26 18:40:14 +0000246 NI = Node->begin(), NE = Node->end(); NI != NE; ++NI) {
Devang Patel26042422007-06-04 00:32:22 +0000247 DomTreeNode *IDominee = *NI;
Chris Lattnerce6ef112002-07-26 18:40:14 +0000248 const DomSetType &ChildDF = calculate(DT, IDominee);
Chris Lattner94108ab2001-07-06 16:58:22 +0000249
250 DomSetType::const_iterator CDFI = ChildDF.begin(), CDFE = ChildDF.end();
251 for (; CDFI != CDFE; ++CDFI) {
Devang Patel9a511572007-06-07 17:47:21 +0000252 if (!DT.properlyDominates(Node, DT[*CDFI]))
Misha Brukmandedf2bd2005-04-22 04:01:18 +0000253 S.insert(*CDFI);
Chris Lattner94108ab2001-07-06 16:58:22 +0000254 }
255 }
256
257 return S;
258}
Chris Lattnera69fd902002-08-21 23:43:50 +0000259
Reid Spencer4f1bd9e2006-06-07 22:00:26 +0000260// Ensure that this .cpp file gets linked when PostDominators.h is used.
261DEFINING_FILE_FOR(PostDominanceFrontier)