1 //===-- llvm/Support/CFG.h - Process LLVM structures as graphs --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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 "Support/GraphTraits.h"
19 #include "llvm/Function.h"
20 #include "llvm/InstrTypes.h"
21 #include "Support/iterator"
23 //===--------------------------------------------------------------------===//
24 // BasicBlock pred_iterator definition
25 //===--------------------------------------------------------------------===//
27 template <class _Ptr, class _USE_iterator> // Predecessor Iterator
28 class PredIterator : public bidirectional_iterator<_Ptr, ptrdiff_t> {
29 typedef bidirectional_iterator<_Ptr, ptrdiff_t> super;
33 typedef PredIterator<_Ptr,_USE_iterator> _Self;
34 typedef typename super::pointer pointer;
36 inline void advancePastConstants() {
37 // Loop to ignore non terminator uses (for example PHI nodes)...
38 while (It != BB->use_end() && !isa<TerminatorInst>(*It))
42 inline PredIterator(_Ptr *bb) : BB(bb), It(bb->use_begin()) {
43 advancePastConstants();
45 inline PredIterator(_Ptr *bb, bool) : BB(bb), It(bb->use_end()) {}
47 inline bool operator==(const _Self& x) const { return It == x.It; }
48 inline bool operator!=(const _Self& x) const { return !operator==(x); }
50 inline pointer operator*() const {
51 assert(It != BB->use_end() && "pred_iterator out of range!");
52 return cast<TerminatorInst>(*It)->getParent();
54 inline pointer *operator->() const { return &(operator*()); }
56 inline _Self& operator++() { // Preincrement
57 assert(It != BB->use_end() && "pred_iterator out of range!");
58 ++It; advancePastConstants();
62 inline _Self operator++(int) { // Postincrement
63 _Self tmp = *this; ++*this; return tmp;
66 inline _Self& operator--() { --It; return *this; } // Predecrement
67 inline _Self operator--(int) { // Postdecrement
68 _Self tmp = *this; --*this; return tmp;
72 typedef PredIterator<BasicBlock, Value::use_iterator> pred_iterator;
73 typedef PredIterator<const BasicBlock,
74 Value::use_const_iterator> pred_const_iterator;
76 inline pred_iterator pred_begin(BasicBlock *BB) { return pred_iterator(BB); }
77 inline pred_const_iterator pred_begin(const BasicBlock *BB) {
78 return pred_const_iterator(BB);
80 inline pred_iterator pred_end(BasicBlock *BB) { return pred_iterator(BB, true);}
81 inline pred_const_iterator pred_end(const BasicBlock *BB) {
82 return pred_const_iterator(BB, true);
87 //===--------------------------------------------------------------------===//
88 // BasicBlock succ_iterator definition
89 //===--------------------------------------------------------------------===//
91 template <class _Term, class _BB> // Successor Iterator
92 class SuccIterator : public bidirectional_iterator<_BB, ptrdiff_t> {
95 typedef bidirectional_iterator<_BB, ptrdiff_t> super;
97 typedef SuccIterator<_Term, _BB> _Self;
98 typedef typename super::pointer pointer;
99 // TODO: This can be random access iterator, need operator+ and stuff tho
101 inline SuccIterator(_Term T) : Term(T), idx(0) { // begin iterator
102 assert(T && "getTerminator returned null!");
104 inline SuccIterator(_Term T, bool) // end iterator
105 : Term(T), idx(Term->getNumSuccessors()) {
106 assert(T && "getTerminator returned null!");
109 inline const _Self &operator=(const _Self &I) {
110 assert(Term == I.Term &&"Cannot assign iterators to two different blocks!");
115 inline bool operator==(const _Self& x) const { return idx == x.idx; }
116 inline bool operator!=(const _Self& x) const { return !operator==(x); }
118 inline pointer operator*() const { return Term->getSuccessor(idx); }
119 inline pointer operator->() const { return operator*(); }
121 inline _Self& operator++() { ++idx; return *this; } // Preincrement
122 inline _Self operator++(int) { // Postincrement
123 _Self tmp = *this; ++*this; return tmp;
126 inline _Self& operator--() { --idx; return *this; } // Predecrement
127 inline _Self operator--(int) { // Postdecrement
128 _Self tmp = *this; --*this; return tmp;
132 typedef SuccIterator<TerminatorInst*, BasicBlock> succ_iterator;
133 typedef SuccIterator<const TerminatorInst*,
134 const BasicBlock> succ_const_iterator;
136 inline succ_iterator succ_begin(BasicBlock *BB) {
137 return succ_iterator(BB->getTerminator());
139 inline succ_const_iterator succ_begin(const BasicBlock *BB) {
140 return succ_const_iterator(BB->getTerminator());
142 inline succ_iterator succ_end(BasicBlock *BB) {
143 return succ_iterator(BB->getTerminator(), true);
145 inline succ_const_iterator succ_end(const BasicBlock *BB) {
146 return succ_const_iterator(BB->getTerminator(), true);
151 //===--------------------------------------------------------------------===//
152 // GraphTraits specializations for basic block graphs (CFGs)
153 //===--------------------------------------------------------------------===//
155 // Provide specializations of GraphTraits to be able to treat a function as a
156 // graph of basic blocks...
158 template <> struct GraphTraits<BasicBlock*> {
159 typedef BasicBlock NodeType;
160 typedef succ_iterator ChildIteratorType;
162 static NodeType *getEntryNode(BasicBlock *BB) { return BB; }
163 static inline ChildIteratorType child_begin(NodeType *N) {
164 return succ_begin(N);
166 static inline ChildIteratorType child_end(NodeType *N) {
171 template <> struct GraphTraits<const BasicBlock*> {
172 typedef const BasicBlock NodeType;
173 typedef succ_const_iterator ChildIteratorType;
175 static NodeType *getEntryNode(const BasicBlock *BB) { return BB; }
177 static inline ChildIteratorType child_begin(NodeType *N) {
178 return succ_begin(N);
180 static inline ChildIteratorType child_end(NodeType *N) {
185 // Provide specializations of GraphTraits to be able to treat a function as a
186 // graph of basic blocks... and to walk it in inverse order. Inverse order for
187 // a function is considered to be when traversing the predecessor edges of a BB
188 // instead of the successor edges.
190 template <> struct GraphTraits<Inverse<BasicBlock*> > {
191 typedef BasicBlock NodeType;
192 typedef pred_iterator ChildIteratorType;
193 static NodeType *getEntryNode(Inverse<BasicBlock *> G) { return G.Graph; }
194 static inline ChildIteratorType child_begin(NodeType *N) {
195 return pred_begin(N);
197 static inline ChildIteratorType child_end(NodeType *N) {
202 template <> struct GraphTraits<Inverse<const BasicBlock*> > {
203 typedef const BasicBlock NodeType;
204 typedef pred_const_iterator ChildIteratorType;
205 static NodeType *getEntryNode(Inverse<const BasicBlock*> G) {
208 static inline ChildIteratorType child_begin(NodeType *N) {
209 return pred_begin(N);
211 static inline ChildIteratorType child_end(NodeType *N) {
218 //===--------------------------------------------------------------------===//
219 // GraphTraits specializations for function basic block graphs (CFGs)
220 //===--------------------------------------------------------------------===//
222 // Provide specializations of GraphTraits to be able to treat a function as a
223 // graph of basic blocks... these are the same as the basic block iterators,
224 // except that the root node is implicitly the first node of the function.
226 template <> struct GraphTraits<Function*> : public GraphTraits<BasicBlock*> {
227 static NodeType *getEntryNode(Function *F) { return &F->getEntryBlock(); }
229 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
230 typedef Function::iterator nodes_iterator;
231 static nodes_iterator nodes_begin(Function *F) { return F->begin(); }
232 static nodes_iterator nodes_end (Function *F) { return F->end(); }
234 template <> struct GraphTraits<const Function*> :
235 public GraphTraits<const BasicBlock*> {
236 static NodeType *getEntryNode(const Function *F) {return &F->getEntryBlock();}
238 // nodes_iterator/begin/end - Allow iteration over all nodes in the graph
239 typedef Function::const_iterator nodes_iterator;
240 static nodes_iterator nodes_begin(const Function *F) { return F->begin(); }
241 static nodes_iterator nodes_end (const Function *F) { return F->end(); }
245 // Provide specializations of GraphTraits to be able to treat a function as a
246 // graph of basic blocks... and to walk it in inverse order. Inverse order for
247 // a function is considered to be when traversing the predecessor edges of a BB
248 // instead of the successor edges.
250 template <> struct GraphTraits<Inverse<Function*> > :
251 public GraphTraits<Inverse<BasicBlock*> > {
252 static NodeType *getEntryNode(Inverse<Function*> G) {
253 return &G.Graph->getEntryBlock();
256 template <> struct GraphTraits<Inverse<const Function*> > :
257 public GraphTraits<Inverse<const BasicBlock*> > {
258 static NodeType *getEntryNode(Inverse<const Function *> G) {
259 return &G.Graph->getEntryBlock();