It turns out most of the thumb2 instructions are not allowed to touch SP. The semanti...
[llvm/avr.git] / lib / VMCore / Dominators.cpp
blob735a70c509275cb33ae8a4b6b1494919d9852bec
1 //===- Dominators.cpp - Dominator Calculation -----------------------------===//
2 //
3 // The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements simple dominator construction algorithms for finding
11 // forward dominators. Postdominators are available in libanalysis, but are not
12 // included in libvmcore, because it's not needed. Forward dominators are
13 // needed to support the Verifier pass.
15 //===----------------------------------------------------------------------===//
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/ADT/DepthFirstIterator.h"
21 #include "llvm/ADT/SetOperations.h"
22 #include "llvm/ADT/SmallPtrSet.h"
23 #include "llvm/ADT/SmallVector.h"
24 #include "llvm/Analysis/DominatorInternals.h"
25 #include "llvm/Instructions.h"
26 #include "llvm/Support/Streams.h"
27 #include <algorithm>
28 using namespace llvm;
30 namespace llvm {
31 static std::ostream &operator<<(std::ostream &o,
32 const std::set<BasicBlock*> &BBs) {
33 for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
34 I != E; ++I)
35 if (*I)
36 WriteAsOperand(o, *I, false);
37 else
38 o << " <<exit node>>";
39 return o;
43 //===----------------------------------------------------------------------===//
44 // DominatorTree Implementation
45 //===----------------------------------------------------------------------===//
47 // Provide public access to DominatorTree information. Implementation details
48 // can be found in DominatorCalculation.h.
50 //===----------------------------------------------------------------------===//
52 TEMPLATE_INSTANTIATION(class DomTreeNodeBase<BasicBlock>);
53 TEMPLATE_INSTANTIATION(class DominatorTreeBase<BasicBlock>);
55 char DominatorTree::ID = 0;
56 static RegisterPass<DominatorTree>
57 E("domtree", "Dominator Tree Construction", true, true);
59 bool DominatorTree::runOnFunction(Function &F) {
60 DT->recalculate(F);
61 return false;
64 //===----------------------------------------------------------------------===//
65 // DominanceFrontier Implementation
66 //===----------------------------------------------------------------------===//
68 char DominanceFrontier::ID = 0;
69 static RegisterPass<DominanceFrontier>
70 G("domfrontier", "Dominance Frontier Construction", true, true);
72 // NewBB is split and now it has one successor. Update dominace frontier to
73 // reflect this change.
74 void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
75 assert(NewBB->getTerminator()->getNumSuccessors() == 1
76 && "NewBB should have a single successor!");
77 BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
79 std::vector<BasicBlock*> PredBlocks;
80 for (pred_iterator PI = pred_begin(NewBB), PE = pred_end(NewBB);
81 PI != PE; ++PI)
82 PredBlocks.push_back(*PI);
84 if (PredBlocks.empty())
85 // If NewBB does not have any predecessors then it is a entry block.
86 // In this case, NewBB and its successor NewBBSucc dominates all
87 // other blocks.
88 return;
90 // NewBBSucc inherits original NewBB frontier.
91 DominanceFrontier::iterator NewBBI = find(NewBB);
92 if (NewBBI != end()) {
93 DominanceFrontier::DomSetType NewBBSet = NewBBI->second;
94 DominanceFrontier::DomSetType NewBBSuccSet;
95 NewBBSuccSet.insert(NewBBSet.begin(), NewBBSet.end());
96 addBasicBlock(NewBBSucc, NewBBSuccSet);
99 // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
100 // DF(PredBlocks[0]) without the stuff that the new block does not dominate
101 // a predecessor of.
102 DominatorTree &DT = getAnalysis<DominatorTree>();
103 if (DT.dominates(NewBB, NewBBSucc)) {
104 DominanceFrontier::iterator DFI = find(PredBlocks[0]);
105 if (DFI != end()) {
106 DominanceFrontier::DomSetType Set = DFI->second;
107 // Filter out stuff in Set that we do not dominate a predecessor of.
108 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
109 E = Set.end(); SetI != E;) {
110 bool DominatesPred = false;
111 for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
112 PI != E; ++PI)
113 if (DT.dominates(NewBB, *PI))
114 DominatesPred = true;
115 if (!DominatesPred)
116 Set.erase(SetI++);
117 else
118 ++SetI;
121 if (NewBBI != end()) {
122 for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
123 E = Set.end(); SetI != E; ++SetI) {
124 BasicBlock *SB = *SetI;
125 addToFrontier(NewBBI, SB);
127 } else
128 addBasicBlock(NewBB, Set);
131 } else {
132 // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
133 // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
134 // NewBBSucc)). NewBBSucc is the single successor of NewBB.
135 DominanceFrontier::DomSetType NewDFSet;
136 NewDFSet.insert(NewBBSucc);
137 addBasicBlock(NewBB, NewDFSet);
140 // Now we must loop over all of the dominance frontiers in the function,
141 // replacing occurrences of NewBBSucc with NewBB in some cases. All
142 // blocks that dominate a block in PredBlocks and contained NewBBSucc in
143 // their dominance frontier must be updated to contain NewBB instead.
145 for (Function::iterator FI = NewBB->getParent()->begin(),
146 FE = NewBB->getParent()->end(); FI != FE; ++FI) {
147 DominanceFrontier::iterator DFI = find(FI);
148 if (DFI == end()) continue; // unreachable block.
150 // Only consider nodes that have NewBBSucc in their dominator frontier.
151 if (!DFI->second.count(NewBBSucc)) continue;
153 // Verify whether this block dominates a block in predblocks. If not, do
154 // not update it.
155 bool BlockDominatesAny = false;
156 for (std::vector<BasicBlock*>::const_iterator BI = PredBlocks.begin(),
157 BE = PredBlocks.end(); BI != BE; ++BI) {
158 if (DT.dominates(FI, *BI)) {
159 BlockDominatesAny = true;
160 break;
164 // If NewBBSucc should not stay in our dominator frontier, remove it.
165 // We remove it unless there is a predecessor of NewBBSucc that we
166 // dominate, but we don't strictly dominate NewBBSucc.
167 bool ShouldRemove = true;
168 if ((BasicBlock*)FI == NewBBSucc || !DT.dominates(FI, NewBBSucc)) {
169 // Okay, we know that PredDom does not strictly dominate NewBBSucc.
170 // Check to see if it dominates any predecessors of NewBBSucc.
171 for (pred_iterator PI = pred_begin(NewBBSucc),
172 E = pred_end(NewBBSucc); PI != E; ++PI)
173 if (DT.dominates(FI, *PI)) {
174 ShouldRemove = false;
175 break;
179 if (ShouldRemove)
180 removeFromFrontier(DFI, NewBBSucc);
181 if (BlockDominatesAny && (&*FI == NewBB || !DT.dominates(FI, NewBB)))
182 addToFrontier(DFI, NewBB);
186 namespace {
187 class DFCalculateWorkObject {
188 public:
189 DFCalculateWorkObject(BasicBlock *B, BasicBlock *P,
190 const DomTreeNode *N,
191 const DomTreeNode *PN)
192 : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
193 BasicBlock *currentBB;
194 BasicBlock *parentBB;
195 const DomTreeNode *Node;
196 const DomTreeNode *parentNode;
200 const DominanceFrontier::DomSetType &
201 DominanceFrontier::calculate(const DominatorTree &DT,
202 const DomTreeNode *Node) {
203 BasicBlock *BB = Node->getBlock();
204 DomSetType *Result = NULL;
206 std::vector<DFCalculateWorkObject> workList;
207 SmallPtrSet<BasicBlock *, 32> visited;
209 workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
210 do {
211 DFCalculateWorkObject *currentW = &workList.back();
212 assert (currentW && "Missing work object.");
214 BasicBlock *currentBB = currentW->currentBB;
215 BasicBlock *parentBB = currentW->parentBB;
216 const DomTreeNode *currentNode = currentW->Node;
217 const DomTreeNode *parentNode = currentW->parentNode;
218 assert (currentBB && "Invalid work object. Missing current Basic Block");
219 assert (currentNode && "Invalid work object. Missing current Node");
220 DomSetType &S = Frontiers[currentBB];
222 // Visit each block only once.
223 if (visited.count(currentBB) == 0) {
224 visited.insert(currentBB);
226 // Loop over CFG successors to calculate DFlocal[currentNode]
227 for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
228 SI != SE; ++SI) {
229 // Does Node immediately dominate this successor?
230 if (DT[*SI]->getIDom() != currentNode)
231 S.insert(*SI);
235 // At this point, S is DFlocal. Now we union in DFup's of our children...
236 // Loop through and visit the nodes that Node immediately dominates (Node's
237 // children in the IDomTree)
238 bool visitChild = false;
239 for (DomTreeNode::const_iterator NI = currentNode->begin(),
240 NE = currentNode->end(); NI != NE; ++NI) {
241 DomTreeNode *IDominee = *NI;
242 BasicBlock *childBB = IDominee->getBlock();
243 if (visited.count(childBB) == 0) {
244 workList.push_back(DFCalculateWorkObject(childBB, currentBB,
245 IDominee, currentNode));
246 visitChild = true;
250 // If all children are visited or there is any child then pop this block
251 // from the workList.
252 if (!visitChild) {
254 if (!parentBB) {
255 Result = &S;
256 break;
259 DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
260 DomSetType &parentSet = Frontiers[parentBB];
261 for (; CDFI != CDFE; ++CDFI) {
262 if (!DT.properlyDominates(parentNode, DT[*CDFI]))
263 parentSet.insert(*CDFI);
265 workList.pop_back();
268 } while (!workList.empty());
270 return *Result;
273 void DominanceFrontierBase::print(std::ostream &o, const Module* ) const {
274 for (const_iterator I = begin(), E = end(); I != E; ++I) {
275 o << " DomFrontier for BB";
276 if (I->first)
277 WriteAsOperand(o, I->first, false);
278 else
279 o << " <<exit node>>";
280 o << " is:\t" << I->second << "\n";
284 void DominanceFrontierBase::dump() {
285 print (llvm::cerr);