1 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines specializations of GraphTraits that allow Function and
11 // BasicBlock graphs to be treated as proper graphs for generic algorithms.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_SUPPORT_CFG_H
16 #define LLVM_SUPPORT_CFG_H
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/Function.h"
20 #include "llvm/InstrTypes.h"
21 #include "llvm/ADT/iterator"
25 //===--------------------------------------------------------------------===//
26 // BasicBlock pred_iterator definition
27 //===--------------------------------------------------------------------===//
29 template <class _Ptr, class _USE_iterator> // Predecessor Iterator
30 class PredIterator : public forward_iterator<_Ptr, ptrdiff_t> {
31 typedef forward_iterator<_Ptr, ptrdiff_t> super;
34 typedef PredIterator<_Ptr,_USE_iterator> _Self;
35 typedef typename super::pointer pointer;
37 inline void advancePastNonPreds() {
38 // Loop to ignore non predecessor uses (for example PHI nodes)...
40 if (isa<TerminatorInst>(*It) || isa<BasicBlock>(*It))
46 inline PredIterator(_Ptr *bb) : It(bb->use_begin()) {
47 advancePastNonPreds();
49 inline PredIterator(_Ptr *bb, bool) : It(bb->use_end()) {}
51 inline bool operator==(const _Self& x) const { return It == x.It; }
52 inline bool operator!=(const _Self& x) const { return !operator==(x); }
54 inline pointer operator*() const {
55 assert(!It.atEnd() && "pred_iterator out of range!");
56 return cast<TerminatorInst>(*It)->getParent();
58 inline pointer *operator->() const { return &(operator*()); }
60 inline _Self& operator++() { // Preincrement
61 assert(!It.atEnd() && "pred_iterator out of range!");
62 ++It; advancePastNonTerminators();
66 inline _Self operator++(int) { // Postincrement
67 _Self tmp = *this; ++*this; return tmp;
71 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
72 typedef PredIterator<const BasicBlock,
73 Value::use_const_iterator> pred_const_iterator;
75 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
76 inline pred_const_iterator pred_begin(const BasicBlock *BB) {
77 return pred_const_iterator(BB);
79 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
80 inline pred_const_iterator pred_end(const BasicBlock *BB) {
81 return pred_const_iterator(BB, true);
86 //===--------------------------------------------------------------------===//
87 // BasicBlock succ_iterator definition
88 //===--------------------------------------------------------------------===//
90 template <class Term_, class BB_> // Successor Iterator
91 class SuccIterator : public bidirectional_iterator<BB_, ptrdiff_t> {
94 typedef bidirectional_iterator<BB_, ptrdiff_t> super;
96 typedef SuccIterator<Term_, BB_> _Self;
97 typedef typename super::pointer pointer;
98 // TODO: This can be random access iterator, need operator+ and stuff tho
100 inline SuccIterator(Term_ T) : Term(T), idx(0) { // begin iterator
101 assert(T && "getTerminator returned null!");
103 inline SuccIterator(Term_ T, bool) // end iterator
104 : Term(T), idx(Term->getNumSuccessors()) {
105 assert(T && "getTerminator returned null!");
108 inline const _Self &operator=(const _Self &I) {
109 assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
114 /// getSuccessorIndex - This is used to interface between code that wants to
115 /// operate on terminator instructions directly.
116 unsigned getSuccessorIndex() const { return idx; }
118 inline bool operator==(const _Self& x) const { return idx == x.idx; }
119 inline bool operator!=(const _Self& x) const { return !operator==(x); }
121 inline pointer operator*() const { return Term->getSuccessor(idx); }
122 inline pointer operator->() const { return operator*(); }
124 inline _Self& operator++() { ++idx; return *this; } // Preincrement
125 inline _Self operator++(int) { // Postincrement
126 _Self tmp = *this; ++*this; return tmp;
129 inline _Self& operator--() { --idx; return *this; } // Predecrement
130 inline _Self operator--(int) { // Postdecrement
131 _Self tmp = *this; --*this; return tmp;
135 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
136 typedef SuccIterator<const TerminatorInst*,
137 const BasicBlock> succ_const_iterator;
139 inline succ_iterator succ_begin(BasicBlock *BB) {
140 return succ_iterator(BB->getTerminator());
142 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
143 return succ_const_iterator(BB->getTerminator());
145 inline succ_iterator succ_end(BasicBlock *BB) {
146 return succ_iterator(BB->getTerminator(), true);
148 inline succ_const_iterator succ_end(const BasicBlock *BB) {
149 return succ_const_iterator(BB->getTerminator(), true);
154 //===--------------------------------------------------------------------===//
155 // GraphTraits specializations for basic block graphs (CFGs)
156 //===--------------------------------------------------------------------===//
158 // Provide specializations of GraphTraits to be able to treat a function as a
159 // graph of basic blocks...
161 template <> struct GraphTraits<BasicBlock*> {
162 typedef BasicBlock NodeType;
163 typedef succ_iterator ChildIteratorType;
165 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
166 static inline ChildIteratorType child_begin(NodeType *N) {
167 return succ_begin(N);
169 static inline ChildIteratorType child_end(NodeType *N) {
174 template <> struct GraphTraits<const BasicBlock*> {
175 typedef const BasicBlock NodeType;
176 typedef succ_const_iterator ChildIteratorType;
178 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
180 static inline ChildIteratorType child_begin(NodeType *N) {
181 return succ_begin(N);
183 static inline ChildIteratorType child_end(NodeType *N) {
188 // Provide specializations of GraphTraits to be able to treat a function as a
189 // graph of basic blocks... and to walk it in inverse order. Inverse order for
190 // a function is considered to be when traversing the predecessor edges of a BB
191 // instead of the successor edges.
193 template <> struct GraphTraits<Inverse<BasicBlock*> > {
194 typedef BasicBlock NodeType;
195 typedef pred_iterator ChildIteratorType;
196 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
197 static inline ChildIteratorType child_begin(NodeType *N) {
198 return pred_begin(N);
200 static inline ChildIteratorType child_end(NodeType *N) {
205 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
206 typedef const BasicBlock NodeType;
207 typedef pred_const_iterator ChildIteratorType;
208 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
211 static inline ChildIteratorType child_begin(NodeType *N) {
212 return pred_begin(N);
214 static inline ChildIteratorType child_end(NodeType *N) {
221 //===--------------------------------------------------------------------===//
222 // GraphTraits specializations for function basic block graphs (CFGs)
223 //===--------------------------------------------------------------------===//
225 // Provide specializations of GraphTraits to be able to treat a function as a
226 // graph of basic blocks... these are the same as the basic block iterators,
227 // except that the root node is implicitly the first node of the function.
229 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
230 static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
232 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
233 typedef Function::iterator nodes_iterator;
234 static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
235 static nodes_iterator nodes_end (Function *F) { return F->end(); }
237 template <> struct GraphTraits<const Function*> :
238 public GraphTraits<const BasicBlock*> {
239 static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
241 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
242 typedef Function::const_iterator nodes_iterator;
243 static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
244 static nodes_iterator nodes_end (const Function *F) { return F->end(); }
248 // Provide specializations of GraphTraits to be able to treat a function as a
249 // graph of basic blocks... and to walk it in inverse order. Inverse order for
250 // a function is considered to be when traversing the predecessor edges of a BB
251 // instead of the successor edges.
253 template <> struct GraphTraits<Inverse<Function*> > :
254 public GraphTraits<Inverse<BasicBlock*> > {
255 static NodeType *getEntryNode(Inverse<Function*> G) {
256 return &G.Graph->getEntryBlock();
259 template <> struct GraphTraits<Inverse<const Function*> > :
260 public GraphTraits<Inverse<const BasicBlock*> > {
261 static NodeType *getEntryNode(Inverse<const Function *> G) {
262 return &G.Graph->getEntryBlock();
266 } // End llvm namespace