Don't generate carry bit when loading immediate values on the Microblaze.
[oota-llvm.git] / lib / VMCore / Dominators.cpp
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.
14 //
15 //===----------------------------------------------------------------------===//
16
17 #include "llvm/Analysis/Dominators.h"
18 #include "llvm/Support/CFG.h"
19 #include "llvm/Support/Compiler.h"
20 #include "llvm/Support/Debug.h"
21 #include "llvm/ADT/DepthFirstIterator.h"
22 #include "llvm/ADT/SetOperations.h"
23 #include "llvm/ADT/SmallPtrSet.h"
24 #include "llvm/ADT/SmallVector.h"
25 #include "llvm/Analysis/DominatorInternals.h"
26 #include "llvm/Instructions.h"
27 #include "llvm/Support/raw_ostream.h"
28 #include "llvm/Support/CommandLine.h"
29 #include <algorithm>
30 using namespace llvm;
31
32 // Always verify dominfo if expensive checking is enabled.
33 #ifdef XDEBUG
34 static bool VerifyDomInfo = true;
35 #else
36 static bool VerifyDomInfo = false;
37 #endif
38 static cl::opt<bool,true>
39 VerifyDomInfoX("verify-dom-info", cl::location(VerifyDomInfo),
40                cl::desc("Verify dominator info (time consuming)"));
41
42 //===----------------------------------------------------------------------===//
43 //  DominatorTree Implementation
44 //===----------------------------------------------------------------------===//
45 //
46 // Provide public access to DominatorTree information.  Implementation details
47 // can be found in DominatorCalculation.h.
48 //
49 //===----------------------------------------------------------------------===//
50
51 TEMPLATE_INSTANTIATION(class llvm::DomTreeNodeBase<BasicBlock>);
52 TEMPLATE_INSTANTIATION(class llvm::DominatorTreeBase<BasicBlock>);
53
54 char DominatorTree::ID = 0;
55 INITIALIZE_PASS(DominatorTree, "domtree",
56                 "Dominator Tree Construction", true, true)
57
58 bool DominatorTree::runOnFunction(Function &F) {
59   DT->recalculate(F);
60   return false;
61 }
62
63 void DominatorTree::verifyAnalysis() const {
64   if (!VerifyDomInfo) return;
65
66   Function &F = *getRoot()->getParent();
67
68   DominatorTree OtherDT;
69   OtherDT.getBase().recalculate(F);
70   assert(!compare(OtherDT) && "Invalid DominatorTree info!");
71 }
72
73 void DominatorTree::print(raw_ostream &OS, const Module *) const {
74   DT->print(OS);
75 }
76
77 // dominates - Return true if A dominates a use in B. This performs the
78 // special checks necessary if A and B are in the same basic block.
79 bool DominatorTree::dominates(const Instruction *A, const Instruction *B) const{
80   const BasicBlock *BBA = A->getParent(), *BBB = B->getParent();
81   
82   // If A is an invoke instruction, its value is only available in this normal
83   // successor block.
84   if (const InvokeInst *II = dyn_cast<InvokeInst>(A))
85     BBA = II->getNormalDest();
86   
87   if (BBA != BBB) return dominates(BBA, BBB);
88   
89   // It is not possible to determine dominance between two PHI nodes 
90   // based on their ordering.
91   if (isa<PHINode>(A) && isa<PHINode>(B)) 
92     return false;
93   
94   // Loop through the basic block until we find A or B.
95   BasicBlock::const_iterator I = BBA->begin();
96   for (; &*I != A && &*I != B; ++I)
97     /*empty*/;
98   
99   return &*I == A;
100 }
101
102
103
104 //===----------------------------------------------------------------------===//
105 //  DominanceFrontier Implementation
106 //===----------------------------------------------------------------------===//
107
108 char DominanceFrontier::ID = 0;
109 INITIALIZE_PASS_BEGIN(DominanceFrontier, "domfrontier",
110                 "Dominance Frontier Construction", true, true)
111 INITIALIZE_PASS_DEPENDENCY(DominatorTree)
112 INITIALIZE_PASS_END(DominanceFrontier, "domfrontier",
113                 "Dominance Frontier Construction", true, true)
114
115 void DominanceFrontier::verifyAnalysis() const {
116   if (!VerifyDomInfo) return;
117
118   DominatorTree &DT = getAnalysis<DominatorTree>();
119
120   DominanceFrontier OtherDF;
121   const std::vector<BasicBlock*> &DTRoots = DT.getRoots();
122   OtherDF.calculate(DT, DT.getNode(DTRoots[0]));
123   assert(!compare(OtherDF) && "Invalid DominanceFrontier info!");
124 }
125
126 // NewBB is split and now it has one successor. Update dominance frontier to
127 // reflect this change.
128 void DominanceFrontier::splitBlock(BasicBlock *NewBB) {
129   assert(NewBB->getTerminator()->getNumSuccessors() == 1 &&
130          "NewBB should have a single successor!");
131   BasicBlock *NewBBSucc = NewBB->getTerminator()->getSuccessor(0);
132
133   // NewBBSucc inherits original NewBB frontier.
134   DominanceFrontier::iterator NewBBI = find(NewBB);
135   if (NewBBI != end())
136     addBasicBlock(NewBBSucc, NewBBI->second);
137
138   // If NewBB dominates NewBBSucc, then DF(NewBB) is now going to be the
139   // DF(NewBBSucc) without the stuff that the new block does not dominate
140   // a predecessor of.
141   DominatorTree &DT = getAnalysis<DominatorTree>();
142   DomTreeNode *NewBBNode = DT.getNode(NewBB);
143   DomTreeNode *NewBBSuccNode = DT.getNode(NewBBSucc);
144   if (DT.dominates(NewBBNode, NewBBSuccNode)) {
145     DominanceFrontier::iterator DFI = find(NewBBSucc);
146     if (DFI != end()) {
147       DominanceFrontier::DomSetType Set = DFI->second;
148       // Filter out stuff in Set that we do not dominate a predecessor of.
149       for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
150              E = Set.end(); SetI != E;) {
151         bool DominatesPred = false;
152         for (pred_iterator PI = pred_begin(*SetI), E = pred_end(*SetI);
153              PI != E; ++PI)
154           if (DT.dominates(NewBBNode, DT.getNode(*PI))) {
155             DominatesPred = true;
156             break;
157           }
158         if (!DominatesPred)
159           Set.erase(SetI++);
160         else
161           ++SetI;
162       }
163
164       if (NewBBI != end()) {
165         for (DominanceFrontier::DomSetType::iterator SetI = Set.begin(),
166                E = Set.end(); SetI != E; ++SetI) {
167           BasicBlock *SB = *SetI;
168           addToFrontier(NewBBI, SB);
169         }
170       } else 
171         addBasicBlock(NewBB, Set);
172     }
173     
174   } else {
175     // DF(NewBB) is {NewBBSucc} because NewBB does not strictly dominate
176     // NewBBSucc, but it does dominate itself (and there is an edge (NewBB ->
177     // NewBBSucc)).  NewBBSucc is the single successor of NewBB.
178     DominanceFrontier::DomSetType NewDFSet;
179     NewDFSet.insert(NewBBSucc);
180     addBasicBlock(NewBB, NewDFSet);
181   }
182
183   // Now update dominance frontiers which either used to contain NewBBSucc
184   // or which now need to include NewBB.
185
186   // Collect the set of blocks which dominate a predecessor of NewBB or
187   // NewSuccBB and which don't dominate both. This is an initial
188   // approximation of the blocks whose dominance frontiers will need updates.
189   SmallVector<DomTreeNode *, 16> AllPredDoms;
190
191   // Compute the block which dominates both NewBBSucc and NewBB. This is
192   // the immediate dominator of NewBBSucc unless NewBB dominates NewBBSucc.
193   // The code below which climbs dominator trees will stop at this point,
194   // because from this point up, dominance frontiers are unaffected.
195   DomTreeNode *DominatesBoth = 0;
196   if (NewBBSuccNode) {
197     DominatesBoth = NewBBSuccNode->getIDom();
198     if (DominatesBoth == NewBBNode)
199       DominatesBoth = NewBBNode->getIDom();
200   }
201
202   // Collect the set of all blocks which dominate a predecessor of NewBB.
203   SmallPtrSet<DomTreeNode *, 8> NewBBPredDoms;
204   for (pred_iterator PI = pred_begin(NewBB), E = pred_end(NewBB); PI != E; ++PI)
205     for (DomTreeNode *DTN = DT.getNode(*PI); DTN; DTN = DTN->getIDom()) {
206       if (DTN == DominatesBoth)
207         break;
208       if (!NewBBPredDoms.insert(DTN))
209         break;
210       AllPredDoms.push_back(DTN);
211     }
212
213   // Collect the set of all blocks which dominate a predecessor of NewSuccBB.
214   SmallPtrSet<DomTreeNode *, 8> NewBBSuccPredDoms;
215   for (pred_iterator PI = pred_begin(NewBBSucc),
216        E = pred_end(NewBBSucc); PI != E; ++PI)
217     for (DomTreeNode *DTN = DT.getNode(*PI); DTN; DTN = DTN->getIDom()) {
218       if (DTN == DominatesBoth)
219         break;
220       if (!NewBBSuccPredDoms.insert(DTN))
221         break;
222       if (!NewBBPredDoms.count(DTN))
223         AllPredDoms.push_back(DTN);
224     }
225
226   // Visit all relevant dominance frontiers and make any needed updates.
227   for (SmallVectorImpl<DomTreeNode *>::const_iterator I = AllPredDoms.begin(),
228        E = AllPredDoms.end(); I != E; ++I) {
229     DomTreeNode *DTN = *I;
230     iterator DFI = find((*I)->getBlock());
231
232     // Only consider nodes that have NewBBSucc in their dominator frontier.
233     if (DFI == end() || !DFI->second.count(NewBBSucc)) continue;
234
235     // If the block dominates a predecessor of NewBB but does not properly
236     // dominate NewBB itself, add NewBB to its dominance frontier.
237     if (NewBBPredDoms.count(DTN) &&
238         !DT.properlyDominates(DTN, NewBBNode))
239       addToFrontier(DFI, NewBB);
240
241     // If the block does not dominate a predecessor of NewBBSucc or
242     // properly dominates NewBBSucc itself, remove NewBBSucc from its
243     // dominance frontier.
244     if (!NewBBSuccPredDoms.count(DTN) ||
245         DT.properlyDominates(DTN, NewBBSuccNode))
246       removeFromFrontier(DFI, NewBBSucc);
247   }
248 }
249
250 namespace {
251   class DFCalculateWorkObject {
252   public:
253     DFCalculateWorkObject(BasicBlock *B, BasicBlock *P, 
254                           const DomTreeNode *N,
255                           const DomTreeNode *PN)
256     : currentBB(B), parentBB(P), Node(N), parentNode(PN) {}
257     BasicBlock *currentBB;
258     BasicBlock *parentBB;
259     const DomTreeNode *Node;
260     const DomTreeNode *parentNode;
261   };
262 }
263
264 const DominanceFrontier::DomSetType &
265 DominanceFrontier::calculate(const DominatorTree &DT,
266                              const DomTreeNode *Node) {
267   BasicBlock *BB = Node->getBlock();
268   DomSetType *Result = NULL;
269
270   std::vector<DFCalculateWorkObject> workList;
271   SmallPtrSet<BasicBlock *, 32> visited;
272
273   workList.push_back(DFCalculateWorkObject(BB, NULL, Node, NULL));
274   do {
275     DFCalculateWorkObject *currentW = &workList.back();
276     assert (currentW && "Missing work object.");
277
278     BasicBlock *currentBB = currentW->currentBB;
279     BasicBlock *parentBB = currentW->parentBB;
280     const DomTreeNode *currentNode = currentW->Node;
281     const DomTreeNode *parentNode = currentW->parentNode;
282     assert (currentBB && "Invalid work object. Missing current Basic Block");
283     assert (currentNode && "Invalid work object. Missing current Node");
284     DomSetType &S = Frontiers[currentBB];
285
286     // Visit each block only once.
287     if (visited.count(currentBB) == 0) {
288       visited.insert(currentBB);
289
290       // Loop over CFG successors to calculate DFlocal[currentNode]
291       for (succ_iterator SI = succ_begin(currentBB), SE = succ_end(currentBB);
292            SI != SE; ++SI) {
293         // Does Node immediately dominate this successor?
294         if (DT[*SI]->getIDom() != currentNode)
295           S.insert(*SI);
296       }
297     }
298
299     // At this point, S is DFlocal.  Now we union in DFup's of our children...
300     // Loop through and visit the nodes that Node immediately dominates (Node's
301     // children in the IDomTree)
302     bool visitChild = false;
303     for (DomTreeNode::const_iterator NI = currentNode->begin(), 
304            NE = currentNode->end(); NI != NE; ++NI) {
305       DomTreeNode *IDominee = *NI;
306       BasicBlock *childBB = IDominee->getBlock();
307       if (visited.count(childBB) == 0) {
308         workList.push_back(DFCalculateWorkObject(childBB, currentBB,
309                                                  IDominee, currentNode));
310         visitChild = true;
311       }
312     }
313
314     // If all children are visited or there is any child then pop this block
315     // from the workList.
316     if (!visitChild) {
317
318       if (!parentBB) {
319         Result = &S;
320         break;
321       }
322
323       DomSetType::const_iterator CDFI = S.begin(), CDFE = S.end();
324       DomSetType &parentSet = Frontiers[parentBB];
325       for (; CDFI != CDFE; ++CDFI) {
326         if (!DT.properlyDominates(parentNode, DT[*CDFI]))
327           parentSet.insert(*CDFI);
328       }
329       workList.pop_back();
330     }
331
332   } while (!workList.empty());
333
334   return *Result;
335 }
336
337 void DominanceFrontierBase::print(raw_ostream &OS, const Module* ) const {
338   for (const_iterator I = begin(), E = end(); I != E; ++I) {
339     OS << "  DomFrontier for BB ";
340     if (I->first)
341       WriteAsOperand(OS, I->first, false);
342     else
343       OS << " <<exit node>>";
344     OS << " is:\t";
345     
346     const std::set<BasicBlock*> &BBs = I->second;
347     
348     for (std::set<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
349          I != E; ++I) {
350       OS << ' ';
351       if (*I)
352         WriteAsOperand(OS, *I, false);
353       else
354         OS << "<<exit node>>";
355     }
356     OS << "\n";
357   }
358 }
359
360 void DominanceFrontierBase::dump() const {
361   print(dbgs());
362 }
363