llvm.org GIT mirror llvm / 428039a
* Move BasicBlock and Method graph stuff to new "llvm/Support/CFG.h" file * Move Method::inst_* to new "llvm/Support/InstIterator.h" file * inst_iterator no longer permits resync'ing git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@1744 91177308-0d34-0410-b5e6-96231b3b80d8 Chris Lattner 18 years ago
2 changed file(s) with 230 addition(s) and 0 deletion(s). Raw diff Collapse all Expand all
0 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs ---*- C++ -*--=//
1 //
2 // This file defines specializations of GraphTraits that allow Methods and
3 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_CFG_H
8 #define LLVM_CFG_H
9
10 #include "Support/GraphTraits.h"
11 #include "llvm/Method.h"
12 #include "llvm/BasicBlock.h"
13
14 //===--------------------------------------------------------------------===//
15 // GraphTraits specializations for basic block graphs (CFGs)
16 //===--------------------------------------------------------------------===//
17
18 // Provide specializations of GraphTraits to be able to treat a method as a
19 // graph of basic blocks...
20
21 template <> struct GraphTraits {
22 typedef BasicBlock NodeType;
23 typedef BasicBlock::succ_iterator ChildIteratorType;
24
25 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
26 static inline ChildIteratorType child_begin(NodeType *N) {
27 return N->succ_begin();
28 }
29 static inline ChildIteratorType child_end(NodeType *N) {
30 return N->succ_end();
31 }
32 };
33
34 template <> struct GraphTraits {
35 typedef const BasicBlock NodeType;
36 typedef BasicBlock::succ_const_iterator ChildIteratorType;
37
38 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
39
40 static inline ChildIteratorType child_begin(NodeType *N) {
41 return N->succ_begin();
42 }
43 static inline ChildIteratorType child_end(NodeType *N) {
44 return N->succ_end();
45 }
46 };
47
48 // Provide specializations of GraphTraits to be able to treat a method as a
49 // graph of basic blocks... and to walk it in inverse order. Inverse order for
50 // a method is considered to be when traversing the predecessor edges of a BB
51 // instead of the successor edges.
52 //
53 template <> struct GraphTraits > {
54 typedef BasicBlock NodeType;
55 typedef BasicBlock::pred_iterator ChildIteratorType;
56 static NodeType *getEntryNode(Inverse G) { return G.Graph; }
57 static inline ChildIteratorType child_begin(NodeType *N) {
58 return N->pred_begin();
59 }
60 static inline ChildIteratorType child_end(NodeType *N) {
61 return N->pred_end();
62 }
63 };
64
65 template <> struct GraphTraits > {
66 typedef const BasicBlock NodeType;
67 typedef BasicBlock::pred_const_iterator ChildIteratorType;
68 static NodeType *getEntryNode(Inverse G) {
69 return G.Graph;
70 }
71 static inline ChildIteratorType child_begin(NodeType *N) {
72 return N->pred_begin();
73 }
74 static inline ChildIteratorType child_end(NodeType *N) {
75 return N->pred_end();
76 }
77 };
78
79
80
81 //===--------------------------------------------------------------------===//
82 // GraphTraits specializations for method basic block graphs (CFGs)
83 //===--------------------------------------------------------------------===//
84
85 // Provide specializations of GraphTraits to be able to treat a method as a
86 // graph of basic blocks... these are the same as the basic block iterators,
87 // except that the root node is implicitly the first node of the method.
88 //
89 template <> struct GraphTraits : public GraphTraits {
90 static NodeType *getEntryNode(Method *M) { return M->front(); }
91 };
92 template <> struct GraphTraits :
93 public GraphTraits {
94 static NodeType *getEntryNode(const Method *M) { return M->front(); }
95 };
96
97
98 // Provide specializations of GraphTraits to be able to treat a method as a
99 // graph of basic blocks... and to walk it in inverse order. Inverse order for
100 // a method is considered to be when traversing the predecessor edges of a BB
101 // instead of the successor edges.
102 //
103 template <> struct GraphTraits > :
104 public GraphTraits > {
105 static NodeType *getEntryNode(Inverse G) { return G.Graph->front();}
106 };
107 template <> struct GraphTraits > :
108 public GraphTraits > {
109 static NodeType *getEntryNode(Inverse G) {
110 return G.Graph->front();
111 }
112 };
113
114 #endif
0 //===-- llvm/Support/InstIterator.h - Classes for inst iteration -*- C++ -*--=//
1 //
2 // This file contains definitions of two iterators for iterating over the
3 // instructions in a method. This is effectively a wrapper around a two level
4 // iterator that can probably be genericized later.
5 //
6 // Note that this iterator gets invalidated any time that basic blocks or
7 // instructions are moved around.
8 //
9 //===----------------------------------------------------------------------===//
10
11 #ifndef LLVM_INST_ITERATOR_H
12 #define LLVM_INST_ITERATOR_H
13
14 #include "llvm/BasicBlock.h"
15 #include "llvm/Method.h"
16
17 // This class is implements inst_begin() & inst_end() for
18 // inst_iterator and const_inst_iterator's.
19 //
20 template
21 class InstIterator {
22 typedef _BB_t BBty;
23 typedef _BB_i_t BBIty;
24 typedef _BI_t BIty;
25 typedef _II_t IIty;
26 _BB_t &BBs; // BasicBlocksType
27 _BB_i_t BB; // BasicBlocksType::iterator
28 _BI_t BI; // BasicBlock::iterator
29 public:
30 typedef std::bidirectional_iterator_tag iterator_category;
31 typedef IIty value_type;
32 typedef unsigned difference_type;
33 typedef BIty pointer;
34 typedef IIty reference;
35
36 template InstIterator(M &m)
37 : BBs(m.getBasicBlocks()), BB(BBs.begin()) { // begin ctor
38 if (BB != BBs.end()) {
39 BI = (*BB)->begin();
40 advanceToNextBB();
41 }
42 }
43
44 template InstIterator(M &m, bool)
45 : BBs(m.getBasicBlocks()), BB(BBs.end()) { // end ctor
46 }
47
48 // Accessors to get at the underlying iterators...
49 inline BBIty &getBasicBlockIterator() { return BB; }
50 inline BIty &getInstructionIterator() { return BI; }
51
52 inline IIty operator*() const { return *BI; }
53 inline IIty operator->() const { return operator*(); }
54
55 inline bool operator==(const InstIterator &y) const {
56 return BB == y.BB && (BB == BBs.end() || BI == y.BI);
57 }
58 inline bool operator!=(const InstIterator& y) const {
59 return !operator==(y);
60 }
61
62 InstIterator& operator++() {
63 ++BI;
64 advanceToNextBB();
65 return *this;
66 }
67 inline InstIterator operator++(int) {
68 InstIterator tmp = *this; ++*this; return tmp;
69 }
70
71 InstIterator& operator--() {
72 while (BB == BBs.end() || BI == (*BB)->begin()) {
73 --BB;
74 BI = (*BB)->end();
75 }
76 --BI;
77 return *this;
78 }
79 inline InstIterator operator--(int) {
80 InstIterator tmp = *this; --*this; return tmp;
81 }
82
83 inline bool atEnd() const { return BB == BBs.end(); }
84
85 private:
86 inline void advanceToNextBB() {
87 // The only way that the II could be broken is if it is now pointing to
88 // the end() of the current BasicBlock and there are successor BBs.
89 while (BI == (*BB)->end()) {
90 ++BB;
91 if (BB == BBs.end()) break;
92 BI = (*BB)->begin();
93 }
94 }
95 };
96
97
98 typedef InstIterator, Method::iterator,
99 BasicBlock::iterator, Instruction*> inst_iterator;
100 typedef InstIterator,
101 Method::const_iterator,
102 BasicBlock::const_iterator,
103 const Instruction*> const_inst_iterator;
104
105 inline inst_iterator inst_begin(Method *M) { return inst_iterator(*M); }
106 inline inst_iterator inst_end(Method *M) { return inst_iterator(*M, true); }
107 inline const_inst_iterator inst_begin(const Method *M) {
108 return const_inst_iterator(*M);
109 }
110 inline const_inst_iterator inst_end(const Method *M) {
111 return const_inst_iterator(*M, true);
112 }
113
114 #endif