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"
35 class CXXBaseOrMemberInitializer;
41 /// CFGElement - Represents a top-level expression in a basic block.
55 DTOR_BEGIN = AutomaticObjectDtor
59 // The int bits are used to mark the main kind.
60 llvm::PointerIntPair<void *, 2> Data1;
61 // The int bits are used to mark the dtor kind.
62 llvm::PointerIntPair<void *, 2> Data2;
64 CFGElement(void *Ptr, unsigned Int) : Data1(Ptr, Int) {}
65 CFGElement(void *Ptr1, unsigned Int1, void *Ptr2, unsigned Int2)
66 : Data1(Ptr1, Int1), Data2(Ptr2, Int2) {}
71 Kind getKind() const { return static_cast<Kind>(Data1.getInt()); }
73 Kind getDtorKind() const {
74 assert(getKind() == Dtor);
75 return static_cast<Kind>(Data2.getInt() + DTOR_BEGIN);
78 bool isValid() const { return Data1.getPointer(); }
80 operator bool() const { return isValid(); }
82 template<class ElemTy> ElemTy getAs() const {
83 if (llvm::isa<ElemTy>(this))
84 return *static_cast<const ElemTy*>(this);
88 static bool classof(const CFGElement *E) { return true; }
91 class CFGStmt : public CFGElement {
94 CFGStmt(Stmt *S, bool asLValue) : CFGElement(S, asLValue) {}
96 Stmt *getStmt() const { return static_cast<Stmt *>(Data1.getPointer()); }
98 operator Stmt*() const { return getStmt(); }
100 bool asLValue() const {
101 return static_cast<Kind>(Data1.getInt()) == StatementAsLValue;
104 static bool classof(const CFGElement *E) {
105 return E->getKind() == Statement || E->getKind() == StatementAsLValue;
109 /// CFGInitializer - Represents C++ base or member initializer from
110 /// constructor's initialization list.
111 class CFGInitializer : public CFGElement {
114 CFGInitializer(CXXBaseOrMemberInitializer* I)
115 : CFGElement(I, Initializer) {}
117 CXXBaseOrMemberInitializer* getInitializer() const {
118 return static_cast<CXXBaseOrMemberInitializer*>(Data1.getPointer());
120 operator CXXBaseOrMemberInitializer*() const { return getInitializer(); }
122 static bool classof(const CFGElement *E) {
123 return E->getKind() == Initializer;
127 /// CFGImplicitDtor - Represents C++ object destructor imlicitly generated
128 /// by compiler on various occasions.
129 class CFGImplicitDtor : public CFGElement {
131 CFGImplicitDtor(unsigned K, void* P, void* S)
132 : CFGElement(P, Dtor, S, K - DTOR_BEGIN) {}
137 static bool classof(const CFGElement *E) {
138 return E->getKind() == Dtor;
142 /// CFGAutomaticObjDtor - Represents C++ object destructor implicit generated
143 /// for automatic object or temporary bound to const reference at the point
144 /// of leaving its local scope.
145 class CFGAutomaticObjDtor: public CFGImplicitDtor {
147 CFGAutomaticObjDtor() {}
148 CFGAutomaticObjDtor(VarDecl* VD, Stmt* S)
149 : CFGImplicitDtor(AutomaticObjectDtor, VD, S) {}
151 VarDecl* getVarDecl() const {
152 return static_cast<VarDecl*>(Data1.getPointer());
155 // Get statement end of which triggered the destructor call.
156 Stmt* getTriggerStmt() const {
157 return static_cast<Stmt*>(Data2.getPointer());
160 static bool classof(const CFGElement *E) {
161 return E->getKind() == Dtor && E->getDtorKind() == AutomaticObjectDtor;
165 class CFGBaseDtor : public CFGImplicitDtor {
167 static bool classof(const CFGElement *E) {
168 return E->getKind() == Dtor && E->getDtorKind() == BaseDtor;
172 class CFGMemberDtor : public CFGImplicitDtor {
174 static bool classof(const CFGElement *E) {
175 return E->getKind() == Dtor && E->getDtorKind() == MemberDtor;
180 class CFGTemporaryDtor : public CFGImplicitDtor {
182 static bool classof(const CFGElement *E) {
183 return E->getKind() == Dtor && E->getDtorKind() == TemporaryDtor;
187 /// CFGBlock - Represents a single basic block in a source-level CFG.
190 /// (1) A set of statements/expressions (which may contain subexpressions).
191 /// (2) A "terminator" statement (not in the set of statements).
192 /// (3) A list of successors and predecessors.
194 /// Terminator: The terminator represents the type of control-flow that occurs
195 /// at the end of the basic block. The terminator is a Stmt* referring to an
196 /// AST node that has control-flow: if-statements, breaks, loops, etc.
197 /// If the control-flow is conditional, the condition expression will appear
198 /// within the set of statements in the block (usually the last statement).
200 /// Predecessors: the order in the set of predecessors is arbitrary.
202 /// Successors: the order in the set of successors is NOT arbitrary. We
203 /// currently have the following orderings based on the terminator:
205 /// Terminator Successor Ordering
206 /// -----------------------------------------------------
207 /// if Then Block; Else Block
208 /// ? operator LHS expression; RHS expression
209 /// &&, || expression that uses result of && or ||, RHS
213 typedef BumpVector<CFGElement> ImplTy;
216 ElementList(BumpVectorContext &C) : Impl(C, 4) {}
218 typedef std::reverse_iterator<ImplTy::iterator> iterator;
219 typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
220 typedef ImplTy::iterator reverse_iterator;
221 typedef ImplTy::const_iterator const_reverse_iterator;
223 void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
224 CFGElement front() const { return Impl.back(); }
225 CFGElement back() const { return Impl.front(); }
227 iterator begin() { return Impl.rbegin(); }
228 iterator end() { return Impl.rend(); }
229 const_iterator begin() const { return Impl.rbegin(); }
230 const_iterator end() const { return Impl.rend(); }
231 reverse_iterator rbegin() { return Impl.begin(); }
232 reverse_iterator rend() { return Impl.end(); }
233 const_reverse_iterator rbegin() const { return Impl.begin(); }
234 const_reverse_iterator rend() const { return Impl.end(); }
236 CFGElement operator[](size_t i) const {
237 assert(i < Impl.size());
238 return Impl[Impl.size() - 1 - i];
241 size_t size() const { return Impl.size(); }
242 bool empty() const { return Impl.empty(); }
245 /// Stmts - The set of statements in the basic block.
246 ElementList Elements;
248 /// Label - An (optional) label that prefixes the executable
249 /// statements in the block. When this variable is non-NULL, it is
250 /// either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
253 /// Terminator - The terminator for a basic block that
254 /// indicates the type of control-flow that occurs between a block
255 /// and its successors.
258 /// LoopTarget - Some blocks are used to represent the "loop edge" to
259 /// the start of a loop from within the loop body. This Stmt* will be
260 /// refer to the loop statement for such blocks (and be null otherwise).
261 const Stmt *LoopTarget;
263 /// BlockID - A numerical ID assigned to a CFGBlock during construction
267 /// Predecessors/Successors - Keep track of the predecessor / successor
269 typedef BumpVector<CFGBlock*> AdjacentBlocks;
270 AdjacentBlocks Preds;
271 AdjacentBlocks Succs;
274 explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
275 : Elements(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
276 BlockID(blockid), Preds(C, 1), Succs(C, 1) {}
279 // Statement iterators
280 typedef ElementList::iterator iterator;
281 typedef ElementList::const_iterator const_iterator;
282 typedef ElementList::reverse_iterator reverse_iterator;
283 typedef ElementList::const_reverse_iterator const_reverse_iterator;
285 CFGElement front() const { return Elements.front(); }
286 CFGElement back() const { return Elements.back(); }
288 iterator begin() { return Elements.begin(); }
289 iterator end() { return Elements.end(); }
290 const_iterator begin() const { return Elements.begin(); }
291 const_iterator end() const { return Elements.end(); }
293 reverse_iterator rbegin() { return Elements.rbegin(); }
294 reverse_iterator rend() { return Elements.rend(); }
295 const_reverse_iterator rbegin() const { return Elements.rbegin(); }
296 const_reverse_iterator rend() const { return Elements.rend(); }
298 unsigned size() const { return Elements.size(); }
299 bool empty() const { return Elements.empty(); }
301 CFGElement operator[](size_t i) const { return Elements[i]; }
304 typedef AdjacentBlocks::iterator pred_iterator;
305 typedef AdjacentBlocks::const_iterator const_pred_iterator;
306 typedef AdjacentBlocks::reverse_iterator pred_reverse_iterator;
307 typedef AdjacentBlocks::const_reverse_iterator const_pred_reverse_iterator;
309 typedef AdjacentBlocks::iterator succ_iterator;
310 typedef AdjacentBlocks::const_iterator const_succ_iterator;
311 typedef AdjacentBlocks::reverse_iterator succ_reverse_iterator;
312 typedef AdjacentBlocks::const_reverse_iterator const_succ_reverse_iterator;
314 pred_iterator pred_begin() { return Preds.begin(); }
315 pred_iterator pred_end() { return Preds.end(); }
316 const_pred_iterator pred_begin() const { return Preds.begin(); }
317 const_pred_iterator pred_end() const { return Preds.end(); }
319 pred_reverse_iterator pred_rbegin() { return Preds.rbegin(); }
320 pred_reverse_iterator pred_rend() { return Preds.rend(); }
321 const_pred_reverse_iterator pred_rbegin() const { return Preds.rbegin(); }
322 const_pred_reverse_iterator pred_rend() const { return Preds.rend(); }
324 succ_iterator succ_begin() { return Succs.begin(); }
325 succ_iterator succ_end() { return Succs.end(); }
326 const_succ_iterator succ_begin() const { return Succs.begin(); }
327 const_succ_iterator succ_end() const { return Succs.end(); }
329 succ_reverse_iterator succ_rbegin() { return Succs.rbegin(); }
330 succ_reverse_iterator succ_rend() { return Succs.rend(); }
331 const_succ_reverse_iterator succ_rbegin() const { return Succs.rbegin(); }
332 const_succ_reverse_iterator succ_rend() const { return Succs.rend(); }
334 unsigned succ_size() const { return Succs.size(); }
335 bool succ_empty() const { return Succs.empty(); }
337 unsigned pred_size() const { return Preds.size(); }
338 bool pred_empty() const { return Preds.empty(); }
341 class FilterOptions {
344 IgnoreDefaultsWithCoveredEnums = 0;
347 unsigned IgnoreDefaultsWithCoveredEnums : 1;
350 static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
351 const CFGBlock *Dst);
353 template <typename IMPL, bool IsPred>
354 class FilteredCFGBlockIterator {
357 const FilterOptions F;
358 const CFGBlock *From;
360 explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
361 const CFGBlock *from,
362 const FilterOptions &f)
363 : I(i), E(e), F(f), From(from) {}
365 bool hasMore() const { return I != E; }
367 FilteredCFGBlockIterator &operator++() {
368 do { ++I; } while (hasMore() && Filter(*I));
372 const CFGBlock *operator*() const { return *I; }
374 bool Filter(const CFGBlock *To) {
375 return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
379 typedef FilteredCFGBlockIterator<const_pred_iterator, true>
380 filtered_pred_iterator;
382 typedef FilteredCFGBlockIterator<const_succ_iterator, false>
383 filtered_succ_iterator;
385 filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
386 return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
389 filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
390 return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
393 // Manipulation of block contents
395 void setTerminator(Stmt* Statement) { Terminator = Statement; }
396 void setLabel(Stmt* Statement) { Label = Statement; }
397 void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
399 Stmt* getTerminator() { return Terminator; }
400 const Stmt* getTerminator() const { return Terminator; }
402 Stmt* getTerminatorCondition();
404 const Stmt* getTerminatorCondition() const {
405 return const_cast<CFGBlock*>(this)->getTerminatorCondition();
408 const Stmt *getLoopTarget() const { return LoopTarget; }
410 bool hasBinaryBranchTerminator() const;
412 Stmt* getLabel() { return Label; }
413 const Stmt* getLabel() const { return Label; }
415 unsigned getBlockID() const { return BlockID; }
417 void dump(const CFG *cfg, const LangOptions &LO) const;
418 void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
419 void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const;
421 void addSuccessor(CFGBlock* Block, BumpVectorContext &C) {
423 Block->Preds.push_back(this, C);
424 Succs.push_back(Block, C);
427 void appendStmt(Stmt* Statement, BumpVectorContext &C, bool asLValue) {
428 Elements.push_back(CFGStmt(Statement, asLValue), C);
432 /// CFG - Represents a source-level, intra-procedural CFG that represents the
433 /// control-flow of a Stmt. The Stmt can represent an entire function body,
434 /// or a single expression. A CFG will always contain one empty block that
435 /// represents the Exit point of the CFG. A CFG will also contain a designated
436 /// Entry block. The CFG solely represents control-flow; it consists of
437 /// CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
438 /// was constructed from.
441 //===--------------------------------------------------------------------===//
442 // CFG Construction & Manipulation.
443 //===--------------------------------------------------------------------===//
447 bool PruneTriviallyFalseEdges:1;
449 bool AddInitializers:1;
450 bool AddImplicitDtors:1;
453 : PruneTriviallyFalseEdges(true)
455 , AddInitializers(false)
456 , AddImplicitDtors(false) {}
459 /// buildCFG - Builds a CFG from an AST. The responsibility to free the
460 /// constructed CFG belongs to the caller.
461 static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
462 BuildOptions BO = BuildOptions());
464 /// createBlock - Create a new block in the CFG. The CFG owns the block;
465 /// the caller should not directly free it.
466 CFGBlock* createBlock();
468 /// setEntry - Set the entry block of the CFG. This is typically used
469 /// only during CFG construction. Most CFG clients expect that the
470 /// entry block has no predecessors and contains no statements.
471 void setEntry(CFGBlock *B) { Entry = B; }
473 /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
474 /// This is typically used only during CFG construction.
475 void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
477 //===--------------------------------------------------------------------===//
479 //===--------------------------------------------------------------------===//
481 typedef BumpVector<CFGBlock*> CFGBlockListTy;
482 typedef CFGBlockListTy::iterator iterator;
483 typedef CFGBlockListTy::const_iterator const_iterator;
484 typedef std::reverse_iterator<iterator> reverse_iterator;
485 typedef std::reverse_iterator<const_iterator> const_reverse_iterator;
487 CFGBlock& front() { return *Blocks.front(); }
488 CFGBlock& back() { return *Blocks.back(); }
490 iterator begin() { return Blocks.begin(); }
491 iterator end() { return Blocks.end(); }
492 const_iterator begin() const { return Blocks.begin(); }
493 const_iterator end() const { return Blocks.end(); }
495 reverse_iterator rbegin() { return Blocks.rbegin(); }
496 reverse_iterator rend() { return Blocks.rend(); }
497 const_reverse_iterator rbegin() const { return Blocks.rbegin(); }
498 const_reverse_iterator rend() const { return Blocks.rend(); }
500 CFGBlock& getEntry() { return *Entry; }
501 const CFGBlock& getEntry() const { return *Entry; }
502 CFGBlock& getExit() { return *Exit; }
503 const CFGBlock& getExit() const { return *Exit; }
505 CFGBlock* getIndirectGotoBlock() { return IndirectGotoBlock; }
506 const CFGBlock* getIndirectGotoBlock() const { return IndirectGotoBlock; }
508 //===--------------------------------------------------------------------===//
509 // Member templates useful for various batch operations over CFGs.
510 //===--------------------------------------------------------------------===//
512 template <typename CALLBACK>
513 void VisitBlockStmts(CALLBACK& O) const {
514 for (const_iterator I=begin(), E=end(); I != E; ++I)
515 for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
517 if (CFGStmt S = BI->getAs<CFGStmt>())
522 //===--------------------------------------------------------------------===//
523 // CFG Introspection.
524 //===--------------------------------------------------------------------===//
526 struct BlkExprNumTy {
528 explicit BlkExprNumTy(signed idx) : Idx(idx) {}
529 explicit BlkExprNumTy() : Idx(-1) {}
530 operator bool() const { return Idx >= 0; }
531 operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
534 bool isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
535 BlkExprNumTy getBlkExprNum(const Stmt* S);
536 unsigned getNumBlkExprs();
538 /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
540 unsigned getNumBlockIDs() const { return NumBlockIDs; }
542 //===--------------------------------------------------------------------===//
543 // CFG Debugging: Pretty-Printing and Visualization.
544 //===--------------------------------------------------------------------===//
546 void viewCFG(const LangOptions &LO) const;
547 void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
548 void dump(const LangOptions &LO) const;
550 //===--------------------------------------------------------------------===//
551 // Internal: constructors and data.
552 //===--------------------------------------------------------------------===//
554 CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
555 BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
559 llvm::BumpPtrAllocator& getAllocator() {
560 return BlkBVC.getAllocator();
563 BumpVectorContext &getBumpVectorContext() {
570 CFGBlock* IndirectGotoBlock; // Special block to contain collective dispatch
571 // for indirect gotos
572 unsigned NumBlockIDs;
574 // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
575 // It represents a map from Expr* to integers to record the set of
576 // block-level expressions and their "statement number" in the CFG.
579 BumpVectorContext BlkBVC;
581 CFGBlockListTy Blocks;
584 } // end namespace clang
586 //===----------------------------------------------------------------------===//
587 // GraphTraits specializations for CFG basic block graphs (source-level CFGs)
588 //===----------------------------------------------------------------------===//
592 // Traits for: CFGBlock
594 template <> struct GraphTraits< ::clang::CFGBlock* > {
595 typedef ::clang::CFGBlock NodeType;
596 typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
598 static NodeType* getEntryNode(::clang::CFGBlock* BB)
601 static inline ChildIteratorType child_begin(NodeType* N)
602 { return N->succ_begin(); }
604 static inline ChildIteratorType child_end(NodeType* N)
605 { return N->succ_end(); }
608 template <> struct GraphTraits< const ::clang::CFGBlock* > {
609 typedef const ::clang::CFGBlock NodeType;
610 typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
612 static NodeType* getEntryNode(const clang::CFGBlock* BB)
615 static inline ChildIteratorType child_begin(NodeType* N)
616 { return N->succ_begin(); }
618 static inline ChildIteratorType child_end(NodeType* N)
619 { return N->succ_end(); }
622 template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
623 typedef const ::clang::CFGBlock NodeType;
624 typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
626 static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
629 static inline ChildIteratorType child_begin(NodeType* N)
630 { return N->pred_begin(); }
632 static inline ChildIteratorType child_end(NodeType* N)
633 { return N->pred_end(); }
638 template <> struct GraphTraits< ::clang::CFG* >
639 : public GraphTraits< ::clang::CFGBlock* > {
641 typedef ::clang::CFG::iterator nodes_iterator;
643 static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
644 static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
645 static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
648 template <> struct GraphTraits<const ::clang::CFG* >
649 : public GraphTraits<const ::clang::CFGBlock* > {
651 typedef ::clang::CFG::const_iterator nodes_iterator;
653 static NodeType *getEntryNode( const ::clang::CFG* F) {
654 return &F->getEntry();
656 static nodes_iterator nodes_begin( const ::clang::CFG* F) {
659 static nodes_iterator nodes_end( const ::clang::CFG* F) {
664 template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
665 : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
667 typedef ::clang::CFG::const_iterator nodes_iterator;
669 static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
670 static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
671 static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
673 } // end llvm namespace