1 //===--- CFG.h - Classes for representing and building CFGs------*- 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 the CFG and CFGBuilder classes for representing and
11 // building Control-Flow Graphs (CFGs) from ASTs.
13 //===----------------------------------------------------------------------===//
15 #ifndef LLVM_CLANG_CFG_H
16 #define LLVM_CLANG_CFG_H
18 #include "llvm/ADT/GraphTraits.h"
19 #include "llvm/Support/Allocator.h"
35 /// CFGBlock - Represents a single basic block in a source-level CFG.
38 /// (1) A set of statements/expressions (which may contain subexpressions).
39 /// (2) A "terminator" statement (not in the set of statements).
40 /// (3) A list of successors and predecessors.
42 /// Terminator: The terminator represents the type of control-flow that occurs
43 /// at the end of the basic block. The terminator is a Stmt* referring to an
44 /// AST node that has control-flow: if-statements, breaks, loops, etc.
45 /// If the control-flow is conditional, the condition expression will appear
46 /// within the set of statements in the block (usually the last statement).
48 /// Predecessors: the order in the set of predecessors is arbitrary.
50 /// Successors: the order in the set of successors is NOT arbitrary. We
51 /// currently have the following orderings based on the terminator:
53 /// Terminator Successor Ordering
54 /// -----------------------------------------------------
55 /// if Then Block; Else Block
56 /// ? operator LHS expression; RHS expression
57 /// &&, || expression that uses result of && or ||, RHS
60 typedef std::vector<Stmt*> StatementListTy;
61 /// Stmts - The set of statements in the basic block.
62 StatementListTy Stmts;
64 /// Label - An (optional) label that prefixes the executable
65 /// statements in the block. When this variable is non-NULL, it is
66 /// either an instance of LabelStmt or SwitchCase.
69 /// Terminator - The terminator for a basic block that
70 /// indicates the type of control-flow that occurs between a block
71 /// and its successors.
74 /// LoopTarget - Some blocks are used to represent the "loop edge" to
75 /// the start of a loop from within the loop body. This Stmt* will be
76 /// refer to the loop statement for such blocks (and be null otherwise).
77 const Stmt *LoopTarget;
79 /// BlockID - A numerical ID assigned to a CFGBlock during construction
83 /// Predecessors/Successors - Keep track of the predecessor / successor
85 typedef std::vector<CFGBlock*> AdjacentBlocks;
90 explicit CFGBlock(unsigned blockid) : Label(NULL), Terminator(NULL),
91 LoopTarget(NULL), BlockID(blockid) {}
94 // Statement iterators
95 typedef StatementListTy::iterator iterator;
96 typedef StatementListTy::const_iterator const_iterator;
97 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
98 typedef std::reverse_iterator<iterator> reverse_iterator;
100 Stmt* front() const { return Stmts.front(); }
101 Stmt* back() const { return Stmts.back(); }
103 iterator begin() { return Stmts.begin(); }
104 iterator end() { return Stmts.end(); }
105 const_iterator begin() const { return Stmts.begin(); }
106 const_iterator end() const { return Stmts.end(); }
108 reverse_iterator rbegin() { return Stmts.rbegin(); }
109 reverse_iterator rend() { return Stmts.rend(); }
110 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
111 const_reverse_iterator rend() const { return Stmts.rend(); }
113 unsigned size() const { return Stmts.size(); }
114 bool empty() const { return Stmts.empty(); }
116 Stmt* operator[](size_t i) const { assert (i < size()); return Stmts[i]; }
119 typedef AdjacentBlocks::iterator pred_iterator;
120 typedef AdjacentBlocks::const_iterator const_pred_iterator;
121 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
122 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
124 typedef AdjacentBlocks::iterator succ_iterator;
125 typedef AdjacentBlocks::const_iterator const_succ_iterator;
126 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
127 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
129 pred_iterator pred_begin() { return Preds.begin(); }
130 pred_iterator pred_end() { return Preds.end(); }
131 const_pred_iterator pred_begin() const { return Preds.begin(); }
132 const_pred_iterator pred_end() const { return Preds.end(); }
134 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
135 pred_reverse_iterator pred_rend() { return Preds.rend(); }
136 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
137 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
139 succ_iterator succ_begin() { return Succs.begin(); }
140 succ_iterator succ_end() { return Succs.end(); }
141 const_succ_iterator succ_begin() const { return Succs.begin(); }
142 const_succ_iterator succ_end() const { return Succs.end(); }
144 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
145 succ_reverse_iterator succ_rend() { return Succs.rend(); }
146 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
147 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
149 unsigned succ_size() const { return Succs.size(); }
150 bool succ_empty() const { return Succs.empty(); }
152 unsigned pred_size() const { return Preds.size(); }
153 bool pred_empty() const { return Preds.empty(); }
155 // Manipulation of block contents
157 void appendStmt(Stmt* Statement) { Stmts.push_back(Statement); }
158 void setTerminator(Stmt* Statement) { Terminator = Statement; }
159 void setLabel(Stmt* Statement) { Label = Statement; }
160 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
162 Stmt* getTerminator() { return Terminator; }
163 const Stmt* getTerminator() const { return Terminator; }
165 Stmt* getTerminatorCondition();
167 const Stmt* getTerminatorCondition() const {
168 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
171 const Stmt *getLoopTarget() const { return LoopTarget; }
173 bool hasBinaryBranchTerminator() const;
175 Stmt* getLabel() { return Label; }
176 const Stmt* getLabel() const { return Label; }
180 void addSuccessor(CFGBlock* Block) {
182 Block->Preds.push_back(this);
183 Succs.push_back(Block);
186 unsigned getBlockID() const { return BlockID; }
188 void dump(const CFG *cfg, const LangOptions &LO) const;
189 void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
190 void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const;
194 /// CFG - Represents a source-level, intra-procedural CFG that represents the
195 /// control-flow of a Stmt. The Stmt can represent an entire function body,
196 /// or a single expression. A CFG will always contain one empty block that
197 /// represents the Exit point of the CFG. A CFG will also contain a designated
198 /// Entry block. The CFG solely represents control-flow; it consists of
199 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
200 /// was constructed from.
203 //===--------------------------------------------------------------------===//
204 // CFG Construction & Manipulation.
205 //===--------------------------------------------------------------------===//
207 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
208 /// constructed CFG belongs to the caller.
209 static CFG* buildCFG(Stmt* AST, ASTContext *C);
211 /// createBlock - Create a new block in the CFG. The CFG owns the block;
212 /// the caller should not directly free it.
213 CFGBlock* createBlock();
215 /// setEntry - Set the entry block of the CFG. This is typically used
216 /// only during CFG construction. Most CFG clients expect that the
217 /// entry block has no predecessors and contains no statements.
218 void setEntry(CFGBlock *B) { Entry = B; }
220 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
221 /// This is typically used only during CFG construction.
222 void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
224 //===--------------------------------------------------------------------===//
226 //===--------------------------------------------------------------------===//
228 typedef std::list<CFGBlock> CFGBlockListTy;
230 typedef CFGBlockListTy::iterator iterator;
231 typedef CFGBlockListTy::const_iterator const_iterator;
232 typedef std::reverse_iterator<iterator> reverse_iterator;
233 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
235 CFGBlock& front() { return Blocks.front(); }
236 CFGBlock& back() { return Blocks.back(); }
238 iterator begin() { return Blocks.begin(); }
239 iterator end() { return Blocks.end(); }
240 const_iterator begin() const { return Blocks.begin(); }
241 const_iterator end() const { return Blocks.end(); }
243 reverse_iterator rbegin() { return Blocks.rbegin(); }
244 reverse_iterator rend() { return Blocks.rend(); }
245 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
246 const_reverse_iterator rend() const { return Blocks.rend(); }
248 CFGBlock& getEntry() { return *Entry; }
249 const CFGBlock& getEntry() const { return *Entry; }
250 CFGBlock& getExit() { return *Exit; }
251 const CFGBlock& getExit() const { return *Exit; }
253 CFGBlock* getIndirectGotoBlock() { return IndirectGotoBlock; }
254 const CFGBlock* getIndirectGotoBlock() const { return IndirectGotoBlock; }
256 //===--------------------------------------------------------------------===//
257 // Member templates useful for various batch operations over CFGs.
258 //===--------------------------------------------------------------------===//
260 template <typename CALLBACK>
261 void VisitBlockStmts(CALLBACK& O) const {
262 for (const_iterator I=begin(), E=end(); I != E; ++I)
263 for (CFGBlock::const_iterator BI=I->begin(), BE=I->end(); BI != BE; ++BI)
267 //===--------------------------------------------------------------------===//
268 // CFG Introspection.
269 //===--------------------------------------------------------------------===//
271 struct BlkExprNumTy {
273 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
274 explicit BlkExprNumTy() : Idx(-1) {}
275 operator bool() const { return Idx >= 0; }
276 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
279 bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
280 BlkExprNumTy getBlkExprNum(const Stmt* S);
281 unsigned getNumBlkExprs();
283 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
285 unsigned getNumBlockIDs() const { return NumBlockIDs; }
287 //===--------------------------------------------------------------------===//
288 // CFG Debugging: Pretty-Printing and Visualization.
289 //===--------------------------------------------------------------------===//
291 void viewCFG(const LangOptions &LO) const;
292 void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
293 void dump(const LangOptions &LO) const;
295 //===--------------------------------------------------------------------===//
296 // Internal: constructors and data.
297 //===--------------------------------------------------------------------===//
299 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
304 llvm::BumpPtrAllocator& getAllocator() {
311 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
312 // for indirect gotos
313 CFGBlockListTy Blocks;
314 unsigned NumBlockIDs;
316 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
317 // It represents a map from Expr* to integers to record the set of
318 // block-level expressions and their "statement number" in the CFG.
321 /// Alloc - An internal allocator.
322 llvm::BumpPtrAllocator Alloc;
324 } // end namespace clang
326 //===----------------------------------------------------------------------===//
327 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
328 //===----------------------------------------------------------------------===//
332 // Traits for: CFGBlock
334 template <> struct GraphTraits<clang::CFGBlock* > {
335 typedef clang::CFGBlock NodeType;
336 typedef clang::CFGBlock::succ_iterator ChildIteratorType;
338 static NodeType* getEntryNode(clang::CFGBlock* BB)
341 static inline ChildIteratorType child_begin(NodeType* N)
342 { return N->succ_begin(); }
344 static inline ChildIteratorType child_end(NodeType* N)
345 { return N->succ_end(); }
348 template <> struct GraphTraits<const clang::CFGBlock* > {
349 typedef const clang::CFGBlock NodeType;
350 typedef clang::CFGBlock::const_succ_iterator ChildIteratorType;
352 static NodeType* getEntryNode(const clang::CFGBlock* BB)
355 static inline ChildIteratorType child_begin(NodeType* N)
356 { return N->succ_begin(); }
358 static inline ChildIteratorType child_end(NodeType* N)
359 { return N->succ_end(); }
362 template <> struct GraphTraits<Inverse<const clang::CFGBlock*> > {
363 typedef const clang::CFGBlock NodeType;
364 typedef clang::CFGBlock::const_pred_iterator ChildIteratorType;
366 static NodeType *getEntryNode(Inverse<const clang::CFGBlock*> G)
369 static inline ChildIteratorType child_begin(NodeType* N)
370 { return N->pred_begin(); }
372 static inline ChildIteratorType child_end(NodeType* N)
373 { return N->pred_end(); }
378 template <> struct GraphTraits<clang::CFG* >
379 : public GraphTraits<clang::CFGBlock* > {
381 typedef clang::CFG::iterator nodes_iterator;
383 static NodeType *getEntryNode(clang::CFG* F) { return &F->getEntry(); }
384 static nodes_iterator nodes_begin(clang::CFG* F) { return F->begin(); }
385 static nodes_iterator nodes_end(clang::CFG* F) { return F->end(); }
388 template <> struct GraphTraits< const clang::CFG* >
389 : public GraphTraits< const clang::CFGBlock* > {
391 typedef clang::CFG::const_iterator nodes_iterator;
393 static NodeType *getEntryNode( const clang::CFG* F) { return &F->getEntry(); }
394 static nodes_iterator nodes_begin( const clang::CFG* F) { return F->begin(); }
395 static nodes_iterator nodes_end( const clang::CFG* F) { return F->end(); }
398 template <> struct GraphTraits<Inverse<const clang::CFG*> >
399 : public GraphTraits<Inverse<const clang::CFGBlock*> > {
401 typedef clang::CFG::const_iterator nodes_iterator;
403 static NodeType *getEntryNode(const clang::CFG* F) { return &F->getExit(); }
404 static nodes_iterator nodes_begin(const clang::CFG* F) { return F->begin();}
405 static nodes_iterator nodes_end(const clang::CFG* F) { return F->end(); }
408 } // end llvm namespace