llvm.org GIT mirror llvm / 3ff4387
Pull iterators out of CFG.h and CFGdecls and put them in Support directory git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@664 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 19 years ago
24 changed file(s) with 340 addition(s) and 514 deletion(s). Raw diff Collapse all Expand all
2323
2424 #include "llvm/Value.h" // Get the definition of Value
2525 #include "llvm/ValueHolder.h"
26 #include "llvm/CFGdecls.h"
26 #include "llvm/Support/GraphTraits.h"
27
28 #include "llvm/CFGdecls.h" // TODO FIXME: remove
2729
2830 class Instruction;
2931 class Method;
139141 BasicBlock *splitBasicBlock(iterator I);
140142 };
141143
144 #include "llvm/CFG.h" // TODO FIXME when succ iterators are in BB.h
145
146 // Provide specializations of GraphTraits to be able to treat a method as a
147 // graph of basic blocks...
148
149 template <> struct GraphTraits {
150 typedef BasicBlock NodeType;
151 typedef BasicBlock::succ_iterator ChildIteratorType;
152
153 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
154 static inline ChildIteratorType child_begin(NodeType *N) {
155 return cfg::succ_begin(N);
156 }
157 static inline ChildIteratorType child_end(NodeType *N) {
158 return cfg::succ_end(N);
159 }
160 };
161
162 template <> struct GraphTraits {
163 typedef const BasicBlock NodeType;
164 typedef BasicBlock::succ_const_iterator ChildIteratorType;
165
166 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
167
168 static inline ChildIteratorType child_begin(NodeType *N) {
169 return cfg::succ_begin(N);
170 }
171 static inline ChildIteratorType child_end(NodeType *N) {
172 return cfg::succ_end(N);
173 }
174 };
175
176 // Provide specializations of GraphTraits to be able to treat a method as a
177 // graph of basic blocks... and to walk it in inverse order. Inverse order for
178 // a method is considered to be when traversing the predecessor edges of a BB
179 // instead of the successor edges.
180 //
181 template <> struct GraphTraits > {
182 typedef BasicBlock NodeType;
183 typedef BasicBlock::pred_iterator ChildIteratorType;
184 static NodeType *getEntryNode(Inverse G) { return G.Graph; }
185 static inline ChildIteratorType child_begin(NodeType *N) {
186 return cfg::pred_begin(N);
187 }
188 static inline ChildIteratorType child_end(NodeType *N) {
189 return cfg::pred_end(N);
190 }
191 };
192
193 template <> struct GraphTraits > {
194 typedef const BasicBlock NodeType;
195 typedef BasicBlock::pred_const_iterator ChildIteratorType;
196 static NodeType *getEntryNode(Inverse G) {
197 return G.Graph;
198 }
199 static inline ChildIteratorType child_begin(NodeType *N) {
200 return cfg::pred_begin(N);
201 }
202 static inline ChildIteratorType child_end(NodeType *N) {
203 return cfg::pred_end(N);
204 }
205 };
206
207
142208 #endif
2020 #define LLVM_CFG_H
2121
2222 #include "llvm/CFGdecls.h" // See this file for concise interface info
23 #include "llvm/Method.h"
2423 #include "llvm/BasicBlock.h"
2524 #include "llvm/InstrTypes.h"
2625 #include "llvm/Type.h"
2726 #include
28 #include
29 #include
3027
3128 namespace cfg {
3229
146143 return succ_const_iterator(BB->getTerminator(),true);
147144 }
148145
149
150 //===----------------------------------------------------------------------===//
151 // Graph Type Declarations
152 //
153 // BasicBlockGraph - Represent a standard traversal of a CFG
154 // ConstBasicBlockGraph - Represent a standard traversal of a const CFG
155 // InverseBasicBlockGraph - Represent a inverse traversal of a CFG
156 // ConstInverseBasicBlockGraph - Represent a inverse traversal of a const CFG
157 //
158 // An Inverse traversal of a graph is where we chase predecessors, instead of
159 // successors.
160 //
161 struct BasicBlockGraph {
162 typedef BasicBlock NodeType;
163 typedef succ_iterator ChildIteratorType;
164 static inline ChildIteratorType child_begin(NodeType *N) {
165 return succ_begin(N);
166 }
167 static inline ChildIteratorType child_end(NodeType *N) {
168 return succ_end(N);
169 }
170 };
171
172 struct ConstBasicBlockGraph {
173 typedef const BasicBlock NodeType;
174 typedef succ_const_iterator ChildIteratorType;
175 static inline ChildIteratorType child_begin(NodeType *N) {
176 return succ_begin(N);
177 }
178 static inline ChildIteratorType child_end(NodeType *N) {
179 return succ_end(N);
180 }
181 };
182
183 struct InverseBasicBlockGraph {
184 typedef BasicBlock NodeType;
185 typedef pred_iterator ChildIteratorType;
186 static inline ChildIteratorType child_begin(NodeType *N) {
187 return pred_begin(N);
188 }
189 static inline ChildIteratorType child_end(NodeType *N) {
190 return pred_end(N);
191 }
192 };
193
194 struct ConstInverseBasicBlockGraph {
195 typedef const BasicBlock NodeType;
196 typedef pred_const_iterator ChildIteratorType;
197 static inline ChildIteratorType child_begin(NodeType *N) {
198 return pred_begin(N);
199 }
200 static inline ChildIteratorType child_end(NodeType *N) {
201 return pred_end(N);
202 }
203 };
204
205 struct TypeGraph {
206 typedef const ::Type NodeType;
207 typedef ::Type::subtype_iterator ChildIteratorType;
208
209 static inline ChildIteratorType child_begin(NodeType *N) {
210 return N->subtype_begin();
211 }
212 static inline ChildIteratorType child_end(NodeType *N) {
213 return N->subtype_end();
214 }
215 };
216
217
218 //===----------------------------------------------------------------------===//
219 // Depth First Iterator
220 //
221
222 // Generic Depth First Iterator
223 template
224 class DFIterator : public std::forward_iterator
225 ptrdiff_t> {
226 typedef typename GI::NodeType NodeType;
227 typedef typename GI::ChildIteratorType ChildItTy;
228
229 set Visited; // All of the blocks visited so far...
230 // VisitStack - Used to maintain the ordering. Top = current block
231 // First element is node pointer, second is the 'next child' to visit
232 stack > VisitStack;
233 const bool Reverse; // Iterate over children before self?
234 private:
235 void reverseEnterNode() {
236 pair &Top = VisitStack.top();
237 NodeType *Node = Top.first;
238 ChildItTy &It = Top.second;
239 for (; It != GI::child_end(Node); ++It) {
240 NodeType *Child = *It;
241 if (!Visited.count(Child)) {
242 Visited.insert(Child);
243 VisitStack.push(make_pair(Child, GI::child_begin(Child)));
244 reverseEnterNode();
245 return;
246 }
247 }
248 }
249 public:
250 typedef DFIterator _Self;
251
252 inline DFIterator(NodeType *Node, bool reverse) : Reverse(reverse) {
253 Visited.insert(Node);
254 VisitStack.push(make_pair(Node, GI::child_begin(Node)));
255 if (Reverse) reverseEnterNode();
256 }
257 inline DFIterator() { /* End is when stack is empty */ }
258
259 inline bool operator==(const _Self& x) const {
260 return VisitStack == x.VisitStack;
261 }
262 inline bool operator!=(const _Self& x) const { return !operator==(x); }
263
264 inline pointer operator*() const {
265 return VisitStack.top().first;
266 }
267
268 // This is a nonstandard operator-> that dereferences the pointer an extra
269 // time... so that you can actually call methods ON the Node, because
270 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
271 //
272 inline NodeType *operator->() const { return operator*(); }
273
274 inline _Self& operator++() { // Preincrement
275 if (Reverse) { // Reverse Depth First Iterator
276 if (VisitStack.top().second == GI::child_end(VisitStack.top().first))
277 VisitStack.pop();
278 if (!VisitStack.empty())
279 reverseEnterNode();
280 } else { // Normal Depth First Iterator
281 do {
282 pair &Top = VisitStack.top();
283 NodeType *Node = Top.first;
284 ChildItTy &It = Top.second;
285
286 while (It != GI::child_end(Node)) {
287 NodeType *Next = *It++;
288 if (!Visited.count(Next)) { // Has our next sibling been visited?
289 // No, do it now.
290 Visited.insert(Next);
291 VisitStack.push(make_pair(Next, GI::child_begin(Next)));
292 return *this;
293 }
294 }
295
296 // Oops, ran out of successors... go up a level on the stack.
297 VisitStack.pop();
298 } while (!VisitStack.empty());
299 }
300 return *this;
301 }
302
303 inline _Self operator++(int) { // Postincrement
304 _Self tmp = *this; ++*this; return tmp;
305 }
306
307 // nodeVisited - return true if this iterator has already visited the
308 // specified node. This is public, and will probably be used to iterate over
309 // nodes that a depth first iteration did not find: ie unreachable nodes.
310 //
311 inline bool nodeVisited(NodeType *Node) const {
312 return Visited.count(Node) != 0;
313 }
314 };
315
316 inline df_iterator df_begin(Method *M, bool Reverse = false) {
317 return df_iterator(M->front(), Reverse);
318 }
319
320 inline df_const_iterator df_begin(const Method *M, bool Reverse = false) {
321 return df_const_iterator(M->front(), Reverse);
322 }
323 inline df_iterator df_end(Method*) {
324 return df_iterator();
325 }
326 inline df_const_iterator df_end(const Method*) {
327 return df_const_iterator();
328 }
329
330 inline df_iterator df_begin(BasicBlock *BB, bool Reverse = false) {
331 return df_iterator(BB, Reverse);
332 }
333 inline df_const_iterator df_begin(const BasicBlock *BB, bool Reverse = false) {
334 return df_const_iterator(BB, Reverse);
335 }
336
337 inline df_iterator df_end(BasicBlock*) {
338 return df_iterator();
339 }
340 inline df_const_iterator df_end(const BasicBlock*) {
341 return df_const_iterator();
342 }
343
344
345
346 inline idf_iterator idf_begin(BasicBlock *BB, bool Reverse = false) {
347 return idf_iterator(BB, Reverse);
348 }
349 inline idf_const_iterator idf_begin(const BasicBlock *BB, bool Reverse = false) {
350 return idf_const_iterator(BB, Reverse);
351 }
352
353 inline idf_iterator idf_end(BasicBlock*) {
354 return idf_iterator();
355 }
356 inline idf_const_iterator idf_end(const BasicBlock*) {
357 return idf_const_iterator();
358 }
359
360
361
362
363 inline tdf_iterator tdf_begin(const Type *T, bool Reverse = false) {
364 return tdf_iterator(T, Reverse);
365 }
366 inline tdf_iterator tdf_end (const Type *T) {
367 return tdf_iterator();
368 }
369
370
371
372
373 //===----------------------------------------------------------------------===//
374 // Post Order CFG iterator code
375 //
376
377 template
378 class POIterator : public std::forward_iterator {
379 set Visited; // All of the blocks visited so far...
380 // VisitStack - Used to maintain the ordering. Top = current block
381 // First element is basic block pointer, second is the 'next child' to visit
382 stack > VisitStack;
383
384 void traverseChild() {
385 while (VisitStack.top().second != succ_end(VisitStack.top().first)) {
386 BBType *BB = *VisitStack.top().second++;
387 if (!Visited.count(BB)) { // If the block is not visited...
388 Visited.insert(BB);
389 VisitStack.push(make_pair(BB, succ_begin(BB)));
390 }
391 }
392 }
393
394 public:
395 typedef POIterator _Self;
396
397 inline POIterator(BBType *BB) {
398 Visited.insert(BB);
399 VisitStack.push(make_pair(BB, succ_begin(BB)));
400 traverseChild();
401 }
402 inline POIterator() { /* End is when stack is empty */ }
403
404 inline bool operator==(const _Self& x) const {
405 return VisitStack == x.VisitStack;
406 }
407 inline bool operator!=(const _Self& x) const { return !operator==(x); }
408
409 inline pointer operator*() const {
410 return VisitStack.top().first;
411 }
412
413 // This is a nonstandard operator-> that dereferences the pointer an extra
414 // time... so that you can actually call methods ON the BasicBlock, because
415 // the contained type is a pointer. This allows BBIt->getTerminator() f.e.
416 //
417 inline BBType *operator->() const { return operator*(); }
418
419 inline _Self& operator++() { // Preincrement
420 VisitStack.pop();
421 if (!VisitStack.empty())
422 traverseChild();
423 return *this;
424 }
425
426 inline _Self operator++(int) { // Postincrement
427 _Self tmp = *this; ++*this; return tmp;
428 }
429
430 // Provide default begin and end methods when nothing special is needed.
431 static inline _Self begin (BBType *BB) { return _Self(BB); }
432 static inline _Self end (BBType *BB) { return _Self(); }
433 };
434
435 inline po_iterator po_begin( Method *M) {
436 return po_iterator(M->front());
437 }
438 inline po_const_iterator po_begin(const Method *M) {
439 return po_const_iterator(M->front());
440 }
441 inline po_iterator po_end ( Method *M) {
442 return po_iterator();
443 }
444 inline po_const_iterator po_end (const Method *M) {
445 return po_const_iterator();
446 }
447
448 inline po_iterator po_begin( BasicBlock *BB) {
449 return po_iterator(BB);
450 }
451 inline po_const_iterator po_begin(const BasicBlock *BB) {
452 return po_const_iterator(BB);
453 }
454 inline po_iterator po_end ( BasicBlock *BB) {
455 return po_iterator();
456 }
457 inline po_const_iterator po_end (const BasicBlock *BB) {
458 return po_const_iterator();
459 }
460
461
462 //===--------------------------------------------------------------------===//
463 // Reverse Post Order CFG iterator code
464 //===--------------------------------------------------------------------===//
465 //
466 // This is used to visit basic blocks in a method in reverse post order. This
467 // class is awkward to use because I don't know a good incremental algorithm to
468 // computer RPO from a graph. Because of this, the construction of the
469 // ReversePostOrderTraversal object is expensive (it must walk the entire graph
470 // with a postorder iterator to build the data structures). The moral of this
471 // story is: Don't create more ReversePostOrderTraversal classes than neccesary.
472 //
473 // This class should be used like this:
474 // {
475 // cfg::ReversePostOrderTraversal RPOT(MethodPtr); // Expensive to create
476 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
477 // ...
478 // }
479 // for (cfg::rpo_iterator I = RPOT.begin(); I != RPOT.end(); ++I) {
480 // ...
481 // }
482 // }
483 //
484
485 typedef reverse_iterator::iterator> rpo_iterator;
486
487 class ReversePostOrderTraversal {
488 vector Blocks; // Block list in normal PO order
489 inline void Initialize(BasicBlock *BB) {
490 copy(po_begin(BB), po_end(BB), back_inserter(Blocks));
491 }
492 public:
493 inline ReversePostOrderTraversal(Method *M) {
494 Initialize(M->front());
495 }
496 inline ReversePostOrderTraversal(BasicBlock *BB) {
497 Initialize(BB);
498 }
499
500 // Because we want a reverse post order, use reverse iterators from the vector
501 inline rpo_iterator begin() { return Blocks.rbegin(); }
502 inline rpo_iterator end() { return Blocks.rend(); }
503 };
504
505146 } // End namespace cfg
506147
507148 #endif
5959 inline succ_iterator succ_end ( BasicBlock *BB);
6060 inline succ_const_iterator succ_end (const BasicBlock *BB);
6161
62
62 #if 0
6363 //===--------------------------------------------------------------------===//
6464 // Depth First CFG iterator code
6565 //===--------------------------------------------------------------------===//
136136 inline po_const_iterator po_begin(const BasicBlock *BB);
137137 inline po_iterator po_end ( BasicBlock *BB);
138138 inline po_const_iterator po_end (const BasicBlock *BB);
139 #endif
139140
140141 } // End namespace cfg
141142
1212
1313 #include "llvm/SymTabValue.h"
1414 #include "llvm/BasicBlock.h"
15 #include
1615
1716 class Instruction;
1817 class BasicBlock;
200199 inline inst_const_iterator inst_end() const { return inst_const_iterator(*this, true); }
201200 };
202201
202 // Provide specializations of GraphTraits to be able to treat a method as a
203 // graph of basic blocks... these are the same as the basic block iterators,
204 // except that the root node is implicitly the first node of the method.
205 //
206 template <> struct GraphTraits : public GraphTraits {
207 static NodeType *getEntryNode(Method *M) { return M->front(); }
208 };
209 template <> struct GraphTraits :
210 public GraphTraits {
211 static NodeType *getEntryNode(const Method *M) { return M->front(); }
212 };
213
214 // Provide specializations of GraphTraits to be able to treat a method as a
215 // graph of basic blocks... and to walk it in inverse order. Inverse order for
216 // a method is considered to be when traversing the predecessor edges of a BB
217 // instead of the successor edges.
218 //
219 template <> struct GraphTraits > :
220 public GraphTraits > {
221 static NodeType *getEntryNode(Inverse G) { return G.Graph->front();}
222 };
223 template <> struct GraphTraits > :
224 public GraphTraits > {
225 static NodeType *getEntryNode(Inverse G) {
226 return G.Graph->front();
227 }
228 };
229
203230 #endif
2626 #define LLVM_TYPE_H
2727
2828 #include "llvm/Value.h"
29 #include "llvm/Support/GraphTraits.h"
2930
3031 class DerivedType;
3132 class MethodType;
263264 return TypeIterator(this, getNumContainedTypes());
264265 }
265266
267
268 // Provide specializations of GraphTraits to be able to treat a type as a
269 // graph of sub types...
270
271 template <> struct GraphTraits {
272 typedef Type NodeType;
273 typedef Type::subtype_iterator ChildIteratorType;
274
275 static inline NodeType *getEntryNode(Type *T) { return T; }
276 static inline ChildIteratorType child_begin(NodeType *N) {
277 return N->subtype_begin();
278 }
279 static inline ChildIteratorType child_end(NodeType *N) {
280 return N->subtype_end();
281 }
282 };
283
284 template <> struct GraphTraits {
285 typedef const Type NodeType;
286 typedef Type::subtype_iterator ChildIteratorType;
287
288 static inline NodeType *getEntryNode(const Type *T) { return T; }
289 static inline ChildIteratorType child_begin(NodeType *N) {
290 return N->subtype_begin();
291 }
292 static inline ChildIteratorType child_end(NodeType *N) {
293 return N->subtype_end();
294 }
295 };
296
266297 #endif
66
77 #include "llvm/Analysis/Interval.h"
88 #include "llvm/BasicBlock.h"
9 #include "llvm/CFG.h"
109
1110 using namespace cfg;
1211
1010
1111 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
1212 #include "llvm/CodeGen/MachineInstr.h"
13
13 #include "llvm/Support/PostOrderIterator.h"
1414
1515
1616 /************************** Constructor/Destructor ***************************/
4646 {
4747 unsigned int POId = 0; // Reverse Depth-first Order ID
4848
49 cfg::po_const_iterator BBI = cfg::po_begin(Meth);
50
51 for( ; BBI != cfg::po_end(Meth) ; ++BBI, ++POId)
49 po_iterator BBI = po_begin(Meth);
50
51 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
5252 {
5353
5454 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl ;
7676 if(DEBUG_LV)
7777 cout << endl << " After Backward Pass ..." << endl;
7878
79 cfg::po_const_iterator BBI = cfg::po_begin(Meth);
80
81 for( ; BBI != cfg::po_end(Meth) ; ++BBI)
79 po_iterator BBI = po_begin(Meth);
80
81 for( ; BBI != po_end(Meth) ; ++BBI)
8282 {
8383
8484 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
55
66 #include "llvm/Analysis/Dominators.h"
77 #include "llvm/Analysis/SimplifyCFG.h" // To get cfg::UnifyAllExitNodes
8 #include "llvm/CFG.h"
8 #include "llvm/Support/DepthFirstIterator.h"
99 #include "llvm/Support/STLExtras.h"
10 #include "llvm/Method.h"
1011 #include
1112
1213 //===----------------------------------------------------------------------===//
5859 Changed = false;
5960
6061 DomSetType WorkingSet;
61 df_const_iterator It = df_begin(M), End = df_end(M);
62 df_iterator It = df_begin(M), End = df_end(M);
6263 for ( ; It != End; ++It) {
6364 const BasicBlock *BB = *It;
6465 pred_const_iterator PI = pred_begin(BB), PEnd = pred_end(BB);
109110
110111 set Visited;
111112 DomSetType WorkingSet;
112 idf_const_iterator It = idf_begin(Root), End = idf_end(Root);
113 idf_iterator It = idf_begin(Root), End = idf_end(Root);
113114 for ( ; It != End; ++It) {
114115 const BasicBlock *BB = *It;
115116 succ_const_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
200201 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
201202
202203 // Iterate over all nodes in depth first order...
203 for (df_const_iterator I = df_begin(M), E = df_end(M); I != E; ++I) {
204 for (df_iterator I = df_begin(M), E = df_end(M); I != E; ++I) {
204205 const BasicBlock *BB = *I, *IDom = IDoms[*I];
205206
206207 if (IDom != 0) { // Ignore the root node and other nasty nodes
222223
223224 if (!isPostDominator()) {
224225 // Iterate over all nodes in depth first order...
225 for (df_const_iterator I = df_begin(Root), E = df_end(Root); I != E; ++I) {
226 for (df_iterator I = df_begin(Root), E = df_end(Root);
227 I != E; ++I) {
226228 const BasicBlock *BB = *I;
227229 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
228230 unsigned DomSetSize = Dominators.size();
229231 if (DomSetSize == 1) continue; // Root node... IDom = null
230232
231 // Loop over all dominators of this node. This corresponds to looping over
233 // Loop over all dominators of this node. This corresponds to looping over
232234 // nodes in the dominator chain, looking for a node whose dominator set is
233235 // equal to the current nodes, except that the current node does not exist
234 // in it. This means that it is one level higher in the dom chain than the
236 // in it. This means that it is one level higher in the dom chain than the
235237 // current node, and it is our idom! We know that we have already added
236238 // a DominatorTree node for our idom, because the idom must be a
237239 // predecessor in the depth first order that we are iterating through the
238240 // method.
241 //
242 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
243 DominatorSet::DomSetType::const_iterator End = Dominators.end();
244 for (; I != End; ++I) { // Iterate over dominators...
245 // All of our dominators should form a chain, where the number of
246 // elements in the dominator set indicates what level the node is at in
247 // the chain. We want the node immediately above us, so it will have
248 // an identical dominator set, except that BB will not dominate it...
249 // therefore it's dominator set size will be one less than BB's...
250 //
251 if (DS.getDominators(*I).size() == DomSetSize - 1) {
252 // We know that the immediate dominator should already have a node,
253 // because we are traversing the CFG in depth first order!
254 //
255 Node *IDomNode = Nodes[*I];
256 assert(IDomNode && "No node for IDOM?");
257
258 // Add a new tree node for this BasicBlock, and link it as a child of
259 // IDomNode
260 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
261 break;
262 }
263 }
264 }
265 } else if (Root) {
266 // Iterate over all nodes in depth first order...
267 for (idf_iterator I = idf_begin(Root), E = idf_end(Root);
268 I != E; ++I) {
269 const BasicBlock *BB = *I;
270 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
271 unsigned DomSetSize = Dominators.size();
272 if (DomSetSize == 1) continue; // Root node... IDom = null
273
274 // Loop over all dominators of this node. This corresponds to looping
275 // over nodes in the dominator chain, looking for a node whose dominator
276 // set is equal to the current nodes, except that the current node does
277 // not exist in it. This means that it is one level higher in the dom
278 // chain than the current node, and it is our idom! We know that we have
279 // already added a DominatorTree node for our idom, because the idom must
280 // be a predecessor in the depth first order that we are iterating through
281 // the method.
239282 //
240283 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
241284 DominatorSet::DomSetType::const_iterator End = Dominators.end();
260303 }
261304 }
262305 }
263 } else if (Root) {
264 // Iterate over all nodes in depth first order...
265 for (idf_const_iterator I = idf_begin(Root), E = idf_end(Root); I != E; ++I) {
266 const BasicBlock *BB = *I;
267 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
268 unsigned DomSetSize = Dominators.size();
269 if (DomSetSize == 1) continue; // Root node... IDom = null
270
271 // Loop over all dominators of this node. This corresponds to looping over
272 // nodes in the dominator chain, looking for a node whose dominator set is
273 // equal to the current nodes, except that the current node does not exist
274 // in it. This means that it is one level higher in the dom chain than the
275 // current node, and it is our idom! We know that we have already added
276 // a DominatorTree node for our idom, because the idom must be a
277 // predecessor in the depth first order that we are iterating through the
278 // method.
279 //
280 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
281 DominatorSet::DomSetType::const_iterator End = Dominators.end();
282 for (; I != End; ++I) { // Iterate over dominators...
283 // All of our dominators should form a chain, where the number of elements
284 // in the dominator set indicates what level the node is at in the chain.
285 // We want the node immediately above us, so it will have an identical
286 // dominator set, except that BB will not dominate it... therefore it's
287 // dominator set size will be one less than BB's...
288 //
289 if (DS.getDominators(*I).size() == DomSetSize - 1) {
290 // We know that the immediate dominator should already have a node,
291 // because we are traversing the CFG in depth first order!
292 //
293 Node *IDomNode = Nodes[*I];
294 assert(IDomNode && "No node for IDOM?");
295
296 // Add a new tree node for this BasicBlock, and link it as a child of
297 // IDomNode
298 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
299 break;
300 }
301 }
302 }
303306 }
304307 }
305308
2020 #include "llvm/DerivedTypes.h"
2121 #include "llvm/iTerminators.h"
2222 #include "llvm/iMemory.h"
23 #include "llvm/CFG.h" // TODO: Change this when we have a DF.h
2423 #include "llvm/Support/STLExtras.h"
24 #include "llvm/Support/DepthFirstIterator.h"
2525 #include
2626 #include // Get definition of pair class
2727 #include
416416 // TypeContains - Returns true if Ty contains E in it.
417417 //
418418 static bool TypeContains(const Type *Ty, const Type *E) {
419 return find(cfg::tdf_begin(Ty), cfg::tdf_end(Ty), E) != cfg::tdf_end(Ty);
419 return find(df_begin(Ty), df_end(Ty), E) != df_end(Ty);
420420 }
421421
422422
1212 #include "llvm/Instruction.h"
1313 #include
1414 #include
15 #include
1516
1617 // Enable to trace to figure out what the heck is going on when parsing fails
1718 #define TRACE_LEVEL 0
1919 #include "llvm/DerivedTypes.h"
2020 #include "llvm/SymbolTable.h"
2121 #include "llvm/Support/STLExtras.h"
22 #include "llvm/CFG.h"
22 #include "llvm/Support/DepthFirstIterator.h"
2323 #include
2424
2525 #if 0
263263 // the type itself is. This also assures us that we will not hit infinite
264264 // recursion on recursive types...
265265 //
266 for (cfg::tdf_iterator I = cfg::tdf_begin(TheTy, true),
267 E = cfg::tdf_end(TheTy); I != E; ++I)
266 for (df_iterator I = df_begin(TheTy, true),
267 E = df_end(TheTy); I != E; ++I)
268268 if (*I != TheTy) {
269269 // If we haven't seen this sub type before, add it to our type table!
270270 const Type *SubTy = *I;
1818 #ifndef LLVM_CODEGEN_SCHEDGRAPH_H
1919 #define LLVM_CODEGEN_SCHEDGRAPH_H
2020
21 #include "llvm/CFGdecls.h" // just for graph iterators
2221 #include "llvm/Support/NonCopyable.h"
2322 #include "llvm/Support/HashExtras.h"
23 #include "llvm/Support/GraphTraits.h"
2424 #include
2525
2626 class Value;
2727 class Instruction;
28 class TerminatorInst;
2829 class BasicBlock;
2930 class Method;
3031 class TargetMachine;
479480 return sg_succ_const_iterator(N->endOutEdges());
480481 }
481482
482 //
483 // po_iterator
484 // po_const_iterator
483 // Provide specializations of GraphTraits to be able to use graph iterators on
484 // the scheduling graph!
485485 //
486 typedef cfg::POIterator sg_po_iterator;
487 typedef cfg::POIterator
488 sg_succ_const_iterator> sg_po_const_iterator;
486 template <> struct GraphTraits {
487 typedef SchedGraphNode NodeType;
488 typedef sg_succ_iterator ChildIteratorType;
489
490 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
491 static inline ChildIteratorType child_begin(NodeType *N) {
492 return succ_begin(N);
493 }
494 static inline ChildIteratorType child_end(NodeType *N) {
495 return succ_end(N);
496 }
497 };
498
499 template <> struct GraphTraits {
500 typedef const SchedGraphNode NodeType;
501 typedef sg_succ_const_iterator ChildIteratorType;
502
503 static inline NodeType *getEntryNode(const SchedGraph *SG) {
504 return SG->getRoot();
505 }
506 static inline ChildIteratorType child_begin(NodeType *N) {
507 return succ_begin(N);
508 }
509 static inline ChildIteratorType child_end(NodeType *N) {
510 return succ_end(N);
511 }
512 };
489513
490514
491515 //************************ External Functions *****************************/
1818 //**************************************************************************/
1919
2020 #include "SchedPriorities.h"
21 #include "llvm/Support/PostOrderIterator.h"
2122
2223
2324 SchedPriorities::SchedPriorities(const Method* method,
4950 void
5051 SchedPriorities::computeDelays(const SchedGraph* graph)
5152 {
52 sg_po_const_iterator poIter = sg_po_const_iterator::begin(graph->getRoot());
53 sg_po_const_iterator poEnd = sg_po_const_iterator::end( graph->getRoot());
53 po_iterator poIter = po_begin(graph), poEnd =po_end(graph);
5454 for ( ; poIter != poEnd; ++poIter)
5555 {
5656 const SchedGraphNode* node = *poIter;
2424 #include "llvm/CodeGen/InstrScheduling.h"
2525 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
2626 #include "llvm/Target/MachineSchedInfo.h"
27 #include
2728
2829 class Method;
2930 class MachineInstr;
1818 #ifndef LLVM_CODEGEN_SCHEDGRAPH_H
1919 #define LLVM_CODEGEN_SCHEDGRAPH_H
2020
21 #include "llvm/CFGdecls.h" // just for graph iterators
2221 #include "llvm/Support/NonCopyable.h"
2322 #include "llvm/Support/HashExtras.h"
23 #include "llvm/Support/GraphTraits.h"
2424 #include
2525
2626 class Value;
2727 class Instruction;
28 class TerminatorInst;
2829 class BasicBlock;
2930 class Method;
3031 class TargetMachine;
479480 return sg_succ_const_iterator(N->endOutEdges());
480481 }
481482
482 //
483 // po_iterator
484 // po_const_iterator
483 // Provide specializations of GraphTraits to be able to use graph iterators on
484 // the scheduling graph!
485485 //
486 typedef cfg::POIterator sg_po_iterator;
487 typedef cfg::POIterator
488 sg_succ_const_iterator> sg_po_const_iterator;
486 template <> struct GraphTraits {
487 typedef SchedGraphNode NodeType;
488 typedef sg_succ_iterator ChildIteratorType;
489
490 static inline NodeType *getEntryNode(SchedGraph *SG) { return SG->getRoot(); }
491 static inline ChildIteratorType child_begin(NodeType *N) {
492 return succ_begin(N);
493 }
494 static inline ChildIteratorType child_end(NodeType *N) {
495 return succ_end(N);
496 }
497 };
498
499 template <> struct GraphTraits {
500 typedef const SchedGraphNode NodeType;
501 typedef sg_succ_const_iterator ChildIteratorType;
502
503 static inline NodeType *getEntryNode(const SchedGraph *SG) {
504 return SG->getRoot();
505 }
506 static inline ChildIteratorType child_begin(NodeType *N) {
507 return succ_begin(N);
508 }
509 static inline ChildIteratorType child_end(NodeType *N) {
510 return succ_end(N);
511 }
512 };
489513
490514
491515 //************************ External Functions *****************************/
1818 //**************************************************************************/
1919
2020 #include "SchedPriorities.h"
21 #include "llvm/Support/PostOrderIterator.h"
2122
2223
2324 SchedPriorities::SchedPriorities(const Method* method,
4950 void
5051 SchedPriorities::computeDelays(const SchedGraph* graph)
5152 {
52 sg_po_const_iterator poIter = sg_po_const_iterator::begin(graph->getRoot());
53 sg_po_const_iterator poEnd = sg_po_const_iterator::end( graph->getRoot());
53 po_iterator poIter = po_begin(graph), poEnd =po_end(graph);
5454 for ( ; poIter != poEnd; ++poIter)
5555 {
5656 const SchedGraphNode* node = *poIter;
2424 #include "llvm/CodeGen/InstrScheduling.h"
2525 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
2626 #include "llvm/Target/MachineSchedInfo.h"
27 #include
2728
2829 class Method;
2930 class MachineInstr;
1010
1111 #include "llvm/Analysis/LiveVar/MethodLiveVarInfo.h"
1212 #include "llvm/CodeGen/MachineInstr.h"
13
13 #include "llvm/Support/PostOrderIterator.h"
1414
1515
1616 /************************** Constructor/Destructor ***************************/
4646 {
4747 unsigned int POId = 0; // Reverse Depth-first Order ID
4848
49 cfg::po_const_iterator BBI = cfg::po_begin(Meth);
50
51 for( ; BBI != cfg::po_end(Meth) ; ++BBI, ++POId)
49 po_iterator BBI = po_begin(Meth);
50
51 for( ; BBI != po_end(Meth) ; ++BBI, ++POId)
5252 {
5353
5454 if(DEBUG_LV) cout << " For BB " << (*BBI)->getName() << ":" << endl ;
7676 if(DEBUG_LV)
7777 cout << endl << " After Backward Pass ..." << endl;
7878
79 cfg::po_const_iterator BBI = cfg::po_begin(Meth);
80
81 for( ; BBI != cfg::po_end(Meth) ; ++BBI)
79 po_iterator BBI = po_begin(Meth);
80
81 for( ; BBI != po_end(Meth) ; ++BBI)
8282 {
8383
8484 BBLiveVar* LVBB = BB2BBLVMap[*BBI];
1010 #include "llvm/Type.h"
1111 #include "llvm/Analysis/Dominators.h"
1212 #include "llvm/Support/STLExtras.h"
13 #include "llvm/Support/DepthFirstIterator.h"
1314 #include "llvm/Analysis/Writer.h"
14 #include "llvm/CFG.h"
1515 #include "llvm/iTerminators.h"
1616 #include
1717 #include
8989 // instructions live in basic blocks that are unreachable. These blocks will
9090 // be eliminated later, along with the instructions inside.
9191 //
92 for (cfg::df_iterator BBI = cfg::df_begin(M), BBE = cfg::df_end(M);
92 for (df_iterator BBI = df_begin(M),
93 BBE = df_end(M);
9394 BBI != BBE; ++BBI) {
9495 BasicBlock *BB = *BBI;
9596 for (BasicBlock::iterator II = BB->begin(), EI = BB->end(); II != EI; ) {
55
66 #include "llvm/Analysis/Dominators.h"
77 #include "llvm/Analysis/SimplifyCFG.h" // To get cfg::UnifyAllExitNodes
8 #include "llvm/CFG.h"
8 #include "llvm/Support/DepthFirstIterator.h"
99 #include "llvm/Support/STLExtras.h"
10 #include "llvm/Method.h"
1011 #include
1112
1213 //===----------------------------------------------------------------------===//
5859 Changed = false;
5960
6061 DomSetType WorkingSet;
61 df_const_iterator It = df_begin(M), End = df_end(M);
62 df_iterator It = df_begin(M), End = df_end(M);
6263 for ( ; It != End; ++It) {
6364 const BasicBlock *BB = *It;
6465 pred_const_iterator PI = pred_begin(BB), PEnd = pred_end(BB);
109110
110111 set Visited;
111112 DomSetType WorkingSet;
112 idf_const_iterator It = idf_begin(Root), End = idf_end(Root);
113 idf_iterator It = idf_begin(Root), End = idf_end(Root);
113114 for ( ; It != End; ++It) {
114115 const BasicBlock *BB = *It;
115116 succ_const_iterator PI = succ_begin(BB), PEnd = succ_end(BB);
200201 Nodes[Root] = new Node(Root, 0); // Add a node for the root...
201202
202203 // Iterate over all nodes in depth first order...
203 for (df_const_iterator I = df_begin(M), E = df_end(M); I != E; ++I) {
204 for (df_iterator I = df_begin(M), E = df_end(M); I != E; ++I) {
204205 const BasicBlock *BB = *I, *IDom = IDoms[*I];
205206
206207 if (IDom != 0) { // Ignore the root node and other nasty nodes
222223
223224 if (!isPostDominator()) {
224225 // Iterate over all nodes in depth first order...
225 for (df_const_iterator I = df_begin(Root), E = df_end(Root); I != E; ++I) {
226 for (df_iterator I = df_begin(Root), E = df_end(Root);
227 I != E; ++I) {
226228 const BasicBlock *BB = *I;
227229 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
228230 unsigned DomSetSize = Dominators.size();
229231 if (DomSetSize == 1) continue; // Root node... IDom = null
230232
231 // Loop over all dominators of this node. This corresponds to looping over
233 // Loop over all dominators of this node. This corresponds to looping over
232234 // nodes in the dominator chain, looking for a node whose dominator set is
233235 // equal to the current nodes, except that the current node does not exist
234 // in it. This means that it is one level higher in the dom chain than the
236 // in it. This means that it is one level higher in the dom chain than the
235237 // current node, and it is our idom! We know that we have already added
236238 // a DominatorTree node for our idom, because the idom must be a
237239 // predecessor in the depth first order that we are iterating through the
238240 // method.
241 //
242 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
243 DominatorSet::DomSetType::const_iterator End = Dominators.end();
244 for (; I != End; ++I) { // Iterate over dominators...
245 // All of our dominators should form a chain, where the number of
246 // elements in the dominator set indicates what level the node is at in
247 // the chain. We want the node immediately above us, so it will have
248 // an identical dominator set, except that BB will not dominate it...
249 // therefore it's dominator set size will be one less than BB's...
250 //
251 if (DS.getDominators(*I).size() == DomSetSize - 1) {
252 // We know that the immediate dominator should already have a node,
253 // because we are traversing the CFG in depth first order!
254 //
255 Node *IDomNode = Nodes[*I];
256 assert(IDomNode && "No node for IDOM?");
257
258 // Add a new tree node for this BasicBlock, and link it as a child of
259 // IDomNode
260 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
261 break;
262 }
263 }
264 }
265 } else if (Root) {
266 // Iterate over all nodes in depth first order...
267 for (idf_iterator I = idf_begin(Root), E = idf_end(Root);
268 I != E; ++I) {
269 const BasicBlock *BB = *I;
270 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
271 unsigned DomSetSize = Dominators.size();
272 if (DomSetSize == 1) continue; // Root node... IDom = null
273
274 // Loop over all dominators of this node. This corresponds to looping
275 // over nodes in the dominator chain, looking for a node whose dominator
276 // set is equal to the current nodes, except that the current node does
277 // not exist in it. This means that it is one level higher in the dom
278 // chain than the current node, and it is our idom! We know that we have
279 // already added a DominatorTree node for our idom, because the idom must
280 // be a predecessor in the depth first order that we are iterating through
281 // the method.
239282 //
240283 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
241284 DominatorSet::DomSetType::const_iterator End = Dominators.end();
260303 }
261304 }
262305 }
263 } else if (Root) {
264 // Iterate over all nodes in depth first order...
265 for (idf_const_iterator I = idf_begin(Root), E = idf_end(Root); I != E; ++I) {
266 const BasicBlock *BB = *I;
267 const DominatorSet::DomSetType &Dominators = DS.getDominators(BB);
268 unsigned DomSetSize = Dominators.size();
269 if (DomSetSize == 1) continue; // Root node... IDom = null
270
271 // Loop over all dominators of this node. This corresponds to looping over
272 // nodes in the dominator chain, looking for a node whose dominator set is
273 // equal to the current nodes, except that the current node does not exist
274 // in it. This means that it is one level higher in the dom chain than the
275 // current node, and it is our idom! We know that we have already added
276 // a DominatorTree node for our idom, because the idom must be a
277 // predecessor in the depth first order that we are iterating through the
278 // method.
279 //
280 DominatorSet::DomSetType::const_iterator I = Dominators.begin();
281 DominatorSet::DomSetType::const_iterator End = Dominators.end();
282 for (; I != End; ++I) { // Iterate over dominators...
283 // All of our dominators should form a chain, where the number of elements
284 // in the dominator set indicates what level the node is at in the chain.
285 // We want the node immediately above us, so it will have an identical
286 // dominator set, except that BB will not dominate it... therefore it's
287 // dominator set size will be one less than BB's...
288 //
289 if (DS.getDominators(*I).size() == DomSetSize - 1) {
290 // We know that the immediate dominator should already have a node,
291 // because we are traversing the CFG in depth first order!
292 //
293 Node *IDomNode = Nodes[*I];
294 assert(IDomNode && "No node for IDOM?");
295
296 // Add a new tree node for this BasicBlock, and link it as a child of
297 // IDomNode
298 Nodes[BB] = IDomNode->addChild(new Node(BB, IDomNode));
299 break;
300 }
301 }
302 }
303306 }
304307 }
305308
1919 #include "llvm/DerivedTypes.h"
2020 #include "llvm/SymbolTable.h"
2121 #include "llvm/Support/STLExtras.h"
22 #include "llvm/CFG.h"
22 #include "llvm/Support/DepthFirstIterator.h"
2323 #include
2424
2525 #if 0
263263 // the type itself is. This also assures us that we will not hit infinite
264264 // recursion on recursive types...
265265 //
266 for (cfg::tdf_iterator I = cfg::tdf_begin(TheTy, true),
267 E = cfg::tdf_end(TheTy); I != E; ++I)
266 for (df_iterator I = df_begin(TheTy, true),
267 E = df_end(TheTy); I != E; ++I)
268268 if (*I != TheTy) {
269269 // If we haven't seen this sub type before, add it to our type table!
270270 const Type *SubTy = *I;
2222 #include "llvm/Bytecode/Reader.h"
2323 #include "llvm/Support/CommandLine.h"
2424 #include "llvm/Method.h"
25 #include "llvm/CFG.h"
25 #include "llvm/Support/DepthFirstIterator.h"
26 #include "llvm/Support/PostOrderIterator.h"
2627
2728 // OutputMode - The different orderings to print basic blocks in...
2829 enum OutputMode {
9899
99100 switch (WriteMode) {
100101 case dfo: // Depth First ordering
101 copy(cfg::df_begin(M), cfg::df_end(M),
102 copy(df_begin(M), df_end(M),
102103 ostream_iterator(*Out, "\n"));
103104 break;
104105 case rdfo: // Reverse Depth First ordering
105 copy(cfg::df_begin(M, true), cfg::df_end(M),
106 copy(df_begin(M, true), df_end(M),
106107 ostream_iterator(*Out, "\n"));
107108 break;
108109 case po: // Post Order
109 copy(cfg::po_begin(M), cfg::po_end(M),
110 copy(po_begin(M), po_end(M),
110111 ostream_iterator(*Out, "\n"));
111112 break;
112113 case rpo: { // Reverse Post Order
113 cfg::ReversePostOrderTraversal RPOT(M);
114 ReversePostOrderTraversal RPOT(M);
114115 copy(RPOT.begin(), RPOT.end(),
115116 ostream_iterator(*Out, "\n"));
116117 break;
2222 #include "llvm/Bytecode/Reader.h"
2323 #include "llvm/Support/CommandLine.h"
2424 #include "llvm/Method.h"
25 #include "llvm/CFG.h"
25 #include "llvm/Support/DepthFirstIterator.h"
26 #include "llvm/Support/PostOrderIterator.h"
2627
2728 // OutputMode - The different orderings to print basic blocks in...
2829 enum OutputMode {
9899
99100 switch (WriteMode) {
100101 case dfo: // Depth First ordering
101 copy(cfg::df_begin(M), cfg::df_end(M),
102 copy(df_begin(M), df_end(M),
102103 ostream_iterator(*Out, "\n"));
103104 break;
104105 case rdfo: // Reverse Depth First ordering
105 copy(cfg::df_begin(M, true), cfg::df_end(M),
106 copy(df_begin(M, true), df_end(M),
106107 ostream_iterator(*Out, "\n"));
107108 break;
108109 case po: // Post Order
109 copy(cfg::po_begin(M), cfg::po_end(M),
110 copy(po_begin(M), po_end(M),
110111 ostream_iterator(*Out, "\n"));
111112 break;
112113 case rpo: { // Reverse Post Order
113 cfg::ReversePostOrderTraversal RPOT(M);
114 ReversePostOrderTraversal RPOT(M);
114115 copy(RPOT.begin(), RPOT.end(),
115116 ostream_iterator(*Out, "\n"));
116117 break;
2222 #include "llvm/Bytecode/Reader.h"
2323 #include "llvm/Support/CommandLine.h"
2424 #include "llvm/Method.h"
25 #include "llvm/CFG.h"
25 #include "llvm/Support/DepthFirstIterator.h"
26 #include "llvm/Support/PostOrderIterator.h"
2627
2728 // OutputMode - The different orderings to print basic blocks in...
2829 enum OutputMode {
9899
99100 switch (WriteMode) {
100101 case dfo: // Depth First ordering
101 copy(cfg::df_begin(M), cfg::df_end(M),
102 copy(df_begin(M), df_end(M),
102103 ostream_iterator(*Out, "\n"));
103104 break;
104105 case rdfo: // Reverse Depth First ordering
105 copy(cfg::df_begin(M, true), cfg::df_end(M),
106 copy(df_begin(M, true), df_end(M),
106107 ostream_iterator(*Out, "\n"));
107108 break;
108109 case po: // Post Order
109 copy(cfg::po_begin(M), cfg::po_end(M),
110 copy(po_begin(M), po_end(M),
110111 ostream_iterator(*Out, "\n"));
111112 break;
112113 case rpo: { // Reverse Post Order
113 cfg::ReversePostOrderTraversal RPOT(M);
114 ReversePostOrderTraversal RPOT(M);
114115 copy(RPOT.begin(), RPOT.end(),
115116 ostream_iterator(*Out, "\n"));
116117 break;