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/PointerIntPair.h"
19 #include "llvm/ADT/GraphTraits.h"
20 #include "llvm/Support/Allocator.h"
21 #include "llvm/Support/Casting.h"
22 #include "clang/Analysis/Support/BumpVector.h"
23 #include "clang/Basic/SourceLocation.h"
39 // An element of the CFG for implicit descructor calls implied by the language
42 // Statement that introduces the variable.
44 // A token which ends the scope, return, goto, throw, }.
47 Dtor(Stmt *s, SourceLocation l) : S(s), Loc(l) {
49 SourceLocation getLoc() { return Loc; }
50 Stmt *getStmt() { return S; }
54 /// CFGElement - Represents a top-level expression in a basic block.
56 llvm::PointerIntPair<Stmt *, 2> Data;
58 enum Type { StartScope, EndScope };
59 explicit CFGElement() {}
60 CFGElement(Stmt *S, bool lvalue) : Data(S, lvalue ? 1 : 0) {}
61 CFGElement(Stmt *S, Type t) : Data(S, t == StartScope ? 2 : 3) {}
62 // CFGElement(Dtor *S, Type t) : Data(reinterpret_cast<Stmt*>(S), 4) {}
63 Stmt *getStmt() const { return Data.getPointer(); }
64 bool asLValue() const { return Data.getInt() == 1; }
65 bool asStartScope() const { return Data.getInt() == 2; }
66 bool asEndScope() const { return Data.getInt() == 3; }
67 bool asDtor() const { return Data.getInt() == 4; }
68 operator Stmt*() const { return getStmt(); }
69 operator bool() const { return getStmt() != 0; }
70 operator Dtor*() const { return reinterpret_cast<Dtor*>(getStmt()); }
73 /// CFGBlock - Represents a single basic block in a source-level CFG.
76 /// (1) A set of statements/expressions (which may contain subexpressions).
77 /// (2) A "terminator" statement (not in the set of statements).
78 /// (3) A list of successors and predecessors.
80 /// Terminator: The terminator represents the type of control-flow that occurs
81 /// at the end of the basic block. The terminator is a Stmt* referring to an
82 /// AST node that has control-flow: if-statements, breaks, loops, etc.
83 /// If the control-flow is conditional, the condition expression will appear
84 /// within the set of statements in the block (usually the last statement).
86 /// Predecessors: the order in the set of predecessors is arbitrary.
88 /// Successors: the order in the set of successors is NOT arbitrary. We
89 /// currently have the following orderings based on the terminator:
91 /// Terminator Successor Ordering
92 /// -----------------------------------------------------
93 /// if Then Block; Else Block
94 /// ? operator LHS expression; RHS expression
95 /// &&, || expression that uses result of && or ||, RHS
99 typedef BumpVector<CFGElement> ImplTy;
102 StatementList(BumpVectorContext &C) : Impl(C, 4) {}
104 typedef std::reverse_iterator<ImplTy::iterator> iterator;
105 typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
106 typedef ImplTy::iterator reverse_iterator;
107 typedef ImplTy::const_iterator const_reverse_iterator;
109 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
110 CFGElement front() const { return Impl.back(); }
111 CFGElement back() const { return Impl.front(); }
113 iterator begin() { return Impl.rbegin(); }
114 iterator end() { return Impl.rend(); }
115 const_iterator begin() const { return Impl.rbegin(); }
116 const_iterator end() const { return Impl.rend(); }
117 reverse_iterator rbegin() { return Impl.begin(); }
118 reverse_iterator rend() { return Impl.end(); }
119 const_reverse_iterator rbegin() const { return Impl.begin(); }
120 const_reverse_iterator rend() const { return Impl.end(); }
122 CFGElement operator[](size_t i) const {
123 assert(i < Impl.size());
124 return Impl[Impl.size() - 1 - i];
127 size_t size() const { return Impl.size(); }
128 bool empty() const { return Impl.empty(); }
131 /// Stmts - The set of statements in the basic block.
134 /// Label - An (optional) label that prefixes the executable
135 /// statements in the block. When this variable is non-NULL, it is
136 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
139 /// Terminator - The terminator for a basic block that
140 /// indicates the type of control-flow that occurs between a block
141 /// and its successors.
144 /// LoopTarget - Some blocks are used to represent the "loop edge" to
145 /// the start of a loop from within the loop body. This Stmt* will be
146 /// refer to the loop statement for such blocks (and be null otherwise).
147 const Stmt *LoopTarget;
149 /// BlockID - A numerical ID assigned to a CFGBlock during construction
153 /// Predecessors/Successors - Keep track of the predecessor / successor
155 typedef BumpVector<CFGBlock*> AdjacentBlocks;
156 AdjacentBlocks Preds;
157 AdjacentBlocks Succs;
160 explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
161 : Stmts(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
162 BlockID(blockid), Preds(C, 1), Succs(C, 1) {}
165 // Statement iterators
166 typedef StatementList::iterator iterator;
167 typedef StatementList::const_iterator const_iterator;
168 typedef StatementList::reverse_iterator reverse_iterator;
169 typedef StatementList::const_reverse_iterator const_reverse_iterator;
171 CFGElement front() const { return Stmts.front(); }
172 CFGElement back() const { return Stmts.back(); }
174 iterator begin() { return Stmts.begin(); }
175 iterator end() { return Stmts.end(); }
176 const_iterator begin() const { return Stmts.begin(); }
177 const_iterator end() const { return Stmts.end(); }
179 reverse_iterator rbegin() { return Stmts.rbegin(); }
180 reverse_iterator rend() { return Stmts.rend(); }
181 const_reverse_iterator rbegin() const { return Stmts.rbegin(); }
182 const_reverse_iterator rend() const { return Stmts.rend(); }
184 unsigned size() const { return Stmts.size(); }
185 bool empty() const { return Stmts.empty(); }
187 CFGElement operator[](size_t i) const { return Stmts[i]; }
190 typedef AdjacentBlocks::iterator pred_iterator;
191 typedef AdjacentBlocks::const_iterator const_pred_iterator;
192 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
193 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
195 typedef AdjacentBlocks::iterator succ_iterator;
196 typedef AdjacentBlocks::const_iterator const_succ_iterator;
197 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
198 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
200 pred_iterator pred_begin() { return Preds.begin(); }
201 pred_iterator pred_end() { return Preds.end(); }
202 const_pred_iterator pred_begin() const { return Preds.begin(); }
203 const_pred_iterator pred_end() const { return Preds.end(); }
205 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
206 pred_reverse_iterator pred_rend() { return Preds.rend(); }
207 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
208 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
210 succ_iterator succ_begin() { return Succs.begin(); }
211 succ_iterator succ_end() { return Succs.end(); }
212 const_succ_iterator succ_begin() const { return Succs.begin(); }
213 const_succ_iterator succ_end() const { return Succs.end(); }
215 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
216 succ_reverse_iterator succ_rend() { return Succs.rend(); }
217 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
218 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
220 unsigned succ_size() const { return Succs.size(); }
221 bool succ_empty() const { return Succs.empty(); }
223 unsigned pred_size() const { return Preds.size(); }
224 bool pred_empty() const { return Preds.empty(); }
226 // Manipulation of block contents
228 void setTerminator(Stmt* Statement) { Terminator = Statement; }
229 void setLabel(Stmt* Statement) { Label = Statement; }
230 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
232 Stmt* getTerminator() { return Terminator; }
233 const Stmt* getTerminator() const { return Terminator; }
235 Stmt* getTerminatorCondition();
237 const Stmt* getTerminatorCondition() const {
238 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
241 const Stmt *getLoopTarget() const { return LoopTarget; }
243 bool hasBinaryBranchTerminator() const;
245 Stmt* getLabel() { return Label; }
246 const Stmt* getLabel() const { return Label; }
248 unsigned getBlockID() const { return BlockID; }
250 void dump(const CFG *cfg, const LangOptions &LO) const;
251 void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
252 void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const;
254 void addSuccessor(CFGBlock* Block, BumpVectorContext &C) {
256 Block->Preds.push_back(this, C);
257 Succs.push_back(Block, C);
260 void appendStmt(Stmt* Statement, BumpVectorContext &C, bool asLValue) {
261 Stmts.push_back(CFGElement(Statement, asLValue), C);
263 void StartScope(Stmt* S, BumpVectorContext &C) {
264 Stmts.push_back(CFGElement(S, CFGElement::StartScope), C);
266 void EndScope(Stmt* S, BumpVectorContext &C) {
267 Stmts.push_back(CFGElement(S, CFGElement::EndScope), C);
272 /// CFG - Represents a source-level, intra-procedural CFG that represents the
273 /// control-flow of a Stmt. The Stmt can represent an entire function body,
274 /// or a single expression. A CFG will always contain one empty block that
275 /// represents the Exit point of the CFG. A CFG will also contain a designated
276 /// Entry block. The CFG solely represents control-flow; it consists of
277 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
278 /// was constructed from.
281 //===--------------------------------------------------------------------===//
282 // CFG Construction & Manipulation.
283 //===--------------------------------------------------------------------===//
285 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
286 /// constructed CFG belongs to the caller.
287 static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
288 bool AddEHEdges = false,
289 bool AddScopes = false);
291 /// createBlock - Create a new block in the CFG. The CFG owns the block;
292 /// the caller should not directly free it.
293 CFGBlock* createBlock();
295 /// setEntry - Set the entry block of the CFG. This is typically used
296 /// only during CFG construction. Most CFG clients expect that the
297 /// entry block has no predecessors and contains no statements.
298 void setEntry(CFGBlock *B) { Entry = B; }
300 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
301 /// This is typically used only during CFG construction.
302 void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
304 //===--------------------------------------------------------------------===//
306 //===--------------------------------------------------------------------===//
308 typedef BumpVector<CFGBlock*> CFGBlockListTy;
309 typedef CFGBlockListTy::iterator iterator;
310 typedef CFGBlockListTy::const_iterator const_iterator;
311 typedef std::reverse_iterator<iterator> reverse_iterator;
312 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
314 CFGBlock& front() { return *Blocks.front(); }
315 CFGBlock& back() { return *Blocks.back(); }
317 iterator begin() { return Blocks.begin(); }
318 iterator end() { return Blocks.end(); }
319 const_iterator begin() const { return Blocks.begin(); }
320 const_iterator end() const { return Blocks.end(); }
322 reverse_iterator rbegin() { return Blocks.rbegin(); }
323 reverse_iterator rend() { return Blocks.rend(); }
324 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
325 const_reverse_iterator rend() const { return Blocks.rend(); }
327 CFGBlock& getEntry() { return *Entry; }
328 const CFGBlock& getEntry() const { return *Entry; }
329 CFGBlock& getExit() { return *Exit; }
330 const CFGBlock& getExit() const { return *Exit; }
332 CFGBlock* getIndirectGotoBlock() { return IndirectGotoBlock; }
333 const CFGBlock* getIndirectGotoBlock() const { return IndirectGotoBlock; }
335 //===--------------------------------------------------------------------===//
336 // Member templates useful for various batch operations over CFGs.
337 //===--------------------------------------------------------------------===//
339 template <typename CALLBACK>
340 void VisitBlockStmts(CALLBACK& O) const {
341 for (const_iterator I=begin(), E=end(); I != E; ++I)
342 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
347 //===--------------------------------------------------------------------===//
348 // CFG Introspection.
349 //===--------------------------------------------------------------------===//
351 struct BlkExprNumTy {
353 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
354 explicit BlkExprNumTy() : Idx(-1) {}
355 operator bool() const { return Idx >= 0; }
356 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
359 bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
360 BlkExprNumTy getBlkExprNum(const Stmt* S);
361 unsigned getNumBlkExprs();
363 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
365 unsigned getNumBlockIDs() const { return NumBlockIDs; }
367 //===--------------------------------------------------------------------===//
368 // CFG Debugging: Pretty-Printing and Visualization.
369 //===--------------------------------------------------------------------===//
371 void viewCFG(const LangOptions &LO) const;
372 void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
373 void dump(const LangOptions &LO) const;
375 //===--------------------------------------------------------------------===//
376 // Internal: constructors and data.
377 //===--------------------------------------------------------------------===//
379 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
380 BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
384 llvm::BumpPtrAllocator& getAllocator() {
385 return BlkBVC.getAllocator();
388 BumpVectorContext &getBumpVectorContext() {
395 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
396 // for indirect gotos
397 unsigned NumBlockIDs;
399 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
400 // It represents a map from Expr* to integers to record the set of
401 // block-level expressions and their "statement number" in the CFG.
404 BumpVectorContext BlkBVC;
406 CFGBlockListTy Blocks;
409 } // end namespace clang
411 //===----------------------------------------------------------------------===//
412 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
413 //===----------------------------------------------------------------------===//
417 /// Implement simplify_type for CFGElement, so that we can dyn_cast from
418 /// CFGElement to a specific Stmt class.
419 template <> struct simplify_type<const ::clang::CFGElement> {
420 typedef ::clang::Stmt* SimpleType;
421 static SimpleType getSimplifiedValue(const ::clang::CFGElement &Val) {
422 return Val.getStmt();
426 template <> struct simplify_type< ::clang::CFGElement>
427 : public simplify_type<const ::clang::CFGElement> {};
429 // Traits for: CFGBlock
431 template <> struct GraphTraits< ::clang::CFGBlock* > {
432 typedef ::clang::CFGBlock NodeType;
433 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
435 static NodeType* getEntryNode(::clang::CFGBlock* BB)
438 static inline ChildIteratorType child_begin(NodeType* N)
439 { return N->succ_begin(); }
441 static inline ChildIteratorType child_end(NodeType* N)
442 { return N->succ_end(); }
445 template <> struct GraphTraits< const ::clang::CFGBlock* > {
446 typedef const ::clang::CFGBlock NodeType;
447 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
449 static NodeType* getEntryNode(const clang::CFGBlock* BB)
452 static inline ChildIteratorType child_begin(NodeType* N)
453 { return N->succ_begin(); }
455 static inline ChildIteratorType child_end(NodeType* N)
456 { return N->succ_end(); }
459 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
460 typedef const ::clang::CFGBlock NodeType;
461 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
463 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
466 static inline ChildIteratorType child_begin(NodeType* N)
467 { return N->pred_begin(); }
469 static inline ChildIteratorType child_end(NodeType* N)
470 { return N->pred_end(); }
475 template <> struct GraphTraits< ::clang::CFG* >
476 : public GraphTraits< ::clang::CFGBlock* > {
478 typedef ::clang::CFG::iterator nodes_iterator;
480 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
481 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
482 static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
485 template <> struct GraphTraits<const ::clang::CFG* >
486 : public GraphTraits<const ::clang::CFGBlock* > {
488 typedef ::clang::CFG::const_iterator nodes_iterator;
490 static NodeType *getEntryNode( const ::clang::CFG* F) {
491 return &F->getEntry();
493 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
496 static nodes_iterator nodes_end( const ::clang::CFG* F) {
501 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
502 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
504 typedef ::clang::CFG::const_iterator nodes_iterator;
506 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
507 static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
508 static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
510 } // end llvm namespace