CFG.h revision 2a4cd498ff9b2b126de3e370b768ab307c221ab8
1//===--- CFG.h - Classes for representing and building CFGs------*- C++ -*-===//
2//
3//                     The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10//  This file defines the CFG and CFGBuilder classes for representing and
11//  building Control-Flow Graphs (CFGs) from ASTs.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_CLANG_CFG_H
16#define LLVM_CLANG_CFG_H
17
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"
24#include <cassert>
25
26namespace llvm {
27  class raw_ostream;
28}
29
30namespace clang {
31  class Decl;
32  class Stmt;
33  class Expr;
34  class CFG;
35  class PrinterHelper;
36  class LangOptions;
37  class ASTContext;
38
39/// CFGElement - Represents a top-level expression in a basic block.
40class CFGElement {
41  llvm::PointerIntPair<Stmt *, 2> Data;
42public:
43  enum Type { StartScope, EndScope };
44  explicit CFGElement() {}
45  CFGElement(Stmt *S, bool lvalue) : Data(S, lvalue ? 1 : 0) {}
46  CFGElement(Stmt *S, Type t) : Data(S, t == StartScope ? 2 : 3) {}
47  Stmt *getStmt() const { return Data.getPointer(); }
48  bool asLValue() const { return Data.getInt() == 1; }
49  bool asStartScope() const { return Data.getInt() == 2; }
50  bool asEndScope() const { return Data.getInt() == 3; }
51  bool asDtor() const { return Data.getInt() == 4; }
52  operator Stmt*() const { return getStmt(); }
53  operator bool() const { return getStmt() != 0; }
54};
55
56/// CFGBlock - Represents a single basic block in a source-level CFG.
57///  It consists of:
58///
59///  (1) A set of statements/expressions (which may contain subexpressions).
60///  (2) A "terminator" statement (not in the set of statements).
61///  (3) A list of successors and predecessors.
62///
63/// Terminator: The terminator represents the type of control-flow that occurs
64/// at the end of the basic block.  The terminator is a Stmt* referring to an
65/// AST node that has control-flow: if-statements, breaks, loops, etc.
66/// If the control-flow is conditional, the condition expression will appear
67/// within the set of statements in the block (usually the last statement).
68///
69/// Predecessors: the order in the set of predecessors is arbitrary.
70///
71/// Successors: the order in the set of successors is NOT arbitrary.  We
72///  currently have the following orderings based on the terminator:
73///
74///     Terminator       Successor Ordering
75///  -----------------------------------------------------
76///       if            Then Block;  Else Block
77///     ? operator      LHS expression;  RHS expression
78///     &&, ||          expression that uses result of && or ||, RHS
79///
80class CFGBlock {
81  class StatementList {
82    typedef BumpVector<CFGElement> ImplTy;
83    ImplTy Impl;
84  public:
85    StatementList(BumpVectorContext &C) : Impl(C, 4) {}
86
87    typedef std::reverse_iterator<ImplTy::iterator>       iterator;
88    typedef std::reverse_iterator<ImplTy::const_iterator> const_iterator;
89    typedef ImplTy::iterator                              reverse_iterator;
90    typedef ImplTy::const_iterator                        const_reverse_iterator;
91
92    void push_back(CFGElement e, BumpVectorContext &C) { Impl.push_back(e, C); }
93    CFGElement front() const { return Impl.back(); }
94    CFGElement back() const { return Impl.front(); }
95
96    iterator begin() { return Impl.rbegin(); }
97    iterator end() { return Impl.rend(); }
98    const_iterator begin() const { return Impl.rbegin(); }
99    const_iterator end() const { return Impl.rend(); }
100    reverse_iterator rbegin() { return Impl.begin(); }
101    reverse_iterator rend() { return Impl.end(); }
102    const_reverse_iterator rbegin() const { return Impl.begin(); }
103    const_reverse_iterator rend() const { return Impl.end(); }
104
105   CFGElement operator[](size_t i) const  {
106     assert(i < Impl.size());
107     return Impl[Impl.size() - 1 - i];
108   }
109
110    size_t size() const { return Impl.size(); }
111    bool empty() const { return Impl.empty(); }
112  };
113
114  /// Stmts - The set of statements in the basic block.
115  StatementList Stmts;
116
117  /// Label - An (optional) label that prefixes the executable
118  ///  statements in the block.  When this variable is non-NULL, it is
119  ///  either an instance of LabelStmt, SwitchCase or CXXCatchStmt.
120  Stmt *Label;
121
122  /// Terminator - The terminator for a basic block that
123  ///  indicates the type of control-flow that occurs between a block
124  ///  and its successors.
125  Stmt *Terminator;
126
127  /// LoopTarget - Some blocks are used to represent the "loop edge" to
128  ///  the start of a loop from within the loop body.  This Stmt* will be
129  ///  refer to the loop statement for such blocks (and be null otherwise).
130  const Stmt *LoopTarget;
131
132  /// BlockID - A numerical ID assigned to a CFGBlock during construction
133  ///   of the CFG.
134  unsigned BlockID;
135
136  /// Predecessors/Successors - Keep track of the predecessor / successor
137  /// CFG blocks.
138  typedef BumpVector<CFGBlock*> AdjacentBlocks;
139  AdjacentBlocks Preds;
140  AdjacentBlocks Succs;
141
142public:
143  explicit CFGBlock(unsigned blockid, BumpVectorContext &C)
144    : Stmts(C), Label(NULL), Terminator(NULL), LoopTarget(NULL),
145      BlockID(blockid), Preds(C, 1), Succs(C, 1) {}
146  ~CFGBlock() {}
147
148  // Statement iterators
149  typedef StatementList::iterator                      iterator;
150  typedef StatementList::const_iterator                const_iterator;
151  typedef StatementList::reverse_iterator              reverse_iterator;
152  typedef StatementList::const_reverse_iterator        const_reverse_iterator;
153
154  CFGElement                   front()       const { return Stmts.front();   }
155  CFGElement                   back()        const { return Stmts.back();    }
156
157  iterator                     begin()             { return Stmts.begin();   }
158  iterator                     end()               { return Stmts.end();     }
159  const_iterator               begin()       const { return Stmts.begin();   }
160  const_iterator               end()         const { return Stmts.end();     }
161
162  reverse_iterator             rbegin()            { return Stmts.rbegin();  }
163  reverse_iterator             rend()              { return Stmts.rend();    }
164  const_reverse_iterator       rbegin()      const { return Stmts.rbegin();  }
165  const_reverse_iterator       rend()        const { return Stmts.rend();    }
166
167  unsigned                     size()        const { return Stmts.size();    }
168  bool                         empty()       const { return Stmts.empty();   }
169
170  CFGElement operator[](size_t i) const  { return Stmts[i]; }
171
172  // CFG iterators
173  typedef AdjacentBlocks::iterator                              pred_iterator;
174  typedef AdjacentBlocks::const_iterator                  const_pred_iterator;
175  typedef AdjacentBlocks::reverse_iterator              pred_reverse_iterator;
176  typedef AdjacentBlocks::const_reverse_iterator  const_pred_reverse_iterator;
177
178  typedef AdjacentBlocks::iterator                              succ_iterator;
179  typedef AdjacentBlocks::const_iterator                  const_succ_iterator;
180  typedef AdjacentBlocks::reverse_iterator              succ_reverse_iterator;
181  typedef AdjacentBlocks::const_reverse_iterator  const_succ_reverse_iterator;
182
183  pred_iterator                pred_begin()        { return Preds.begin();   }
184  pred_iterator                pred_end()          { return Preds.end();     }
185  const_pred_iterator          pred_begin()  const { return Preds.begin();   }
186  const_pred_iterator          pred_end()    const { return Preds.end();     }
187
188  pred_reverse_iterator        pred_rbegin()       { return Preds.rbegin();  }
189  pred_reverse_iterator        pred_rend()         { return Preds.rend();    }
190  const_pred_reverse_iterator  pred_rbegin() const { return Preds.rbegin();  }
191  const_pred_reverse_iterator  pred_rend()   const { return Preds.rend();    }
192
193  succ_iterator                succ_begin()        { return Succs.begin();   }
194  succ_iterator                succ_end()          { return Succs.end();     }
195  const_succ_iterator          succ_begin()  const { return Succs.begin();   }
196  const_succ_iterator          succ_end()    const { return Succs.end();     }
197
198  succ_reverse_iterator        succ_rbegin()       { return Succs.rbegin();  }
199  succ_reverse_iterator        succ_rend()         { return Succs.rend();    }
200  const_succ_reverse_iterator  succ_rbegin() const { return Succs.rbegin();  }
201  const_succ_reverse_iterator  succ_rend()   const { return Succs.rend();    }
202
203  unsigned                     succ_size()   const { return Succs.size();    }
204  bool                         succ_empty()  const { return Succs.empty();   }
205
206  unsigned                     pred_size()   const { return Preds.size();    }
207  bool                         pred_empty()  const { return Preds.empty();   }
208
209
210  class FilterOptions {
211  public:
212    FilterOptions() {
213      IgnoreDefaultsWithCoveredEnums = 0;
214    }
215
216    unsigned IgnoreDefaultsWithCoveredEnums : 1;
217  };
218
219  static bool FilterEdge(const FilterOptions &F, const CFGBlock *Src,
220       const CFGBlock *Dst);
221
222  template <typename IMPL, bool IsPred>
223  class FilteredCFGBlockIterator {
224  private:
225    IMPL I, E;
226    const FilterOptions F;
227    const CFGBlock *From;
228   public:
229    explicit FilteredCFGBlockIterator(const IMPL &i, const IMPL &e,
230              const CFGBlock *from,
231              const FilterOptions &f)
232      : I(i), E(e), F(f), From(from) {}
233
234    bool hasMore() const { return I != E; }
235
236    FilteredCFGBlockIterator &operator++() {
237      do { ++I; } while (hasMore() && Filter(*I));
238      return *this;
239    }
240
241    const CFGBlock *operator*() const { return *I; }
242  private:
243    bool Filter(const CFGBlock *To) {
244      return IsPred ? FilterEdge(F, To, From) : FilterEdge(F, From, To);
245    }
246  };
247
248  typedef FilteredCFGBlockIterator<const_pred_iterator, true>
249          filtered_pred_iterator;
250
251  typedef FilteredCFGBlockIterator<const_succ_iterator, false>
252          filtered_succ_iterator;
253
254  filtered_pred_iterator filtered_pred_start_end(const FilterOptions &f) const {
255    return filtered_pred_iterator(pred_begin(), pred_end(), this, f);
256  }
257
258  filtered_succ_iterator filtered_succ_start_end(const FilterOptions &f) const {
259    return filtered_succ_iterator(succ_begin(), succ_end(), this, f);
260  }
261
262  // Manipulation of block contents
263
264  void setTerminator(Stmt* Statement) { Terminator = Statement; }
265  void setLabel(Stmt* Statement) { Label = Statement; }
266  void setLoopTarget(const Stmt *loopTarget) { LoopTarget = loopTarget; }
267
268  Stmt* getTerminator() { return Terminator; }
269  const Stmt* getTerminator() const { return Terminator; }
270
271  Stmt* getTerminatorCondition();
272
273  const Stmt* getTerminatorCondition() const {
274    return const_cast<CFGBlock*>(this)->getTerminatorCondition();
275  }
276
277  const Stmt *getLoopTarget() const { return LoopTarget; }
278
279  bool hasBinaryBranchTerminator() const;
280
281  Stmt* getLabel() { return Label; }
282  const Stmt* getLabel() const { return Label; }
283
284  unsigned getBlockID() const { return BlockID; }
285
286  void dump(const CFG *cfg, const LangOptions &LO) const;
287  void print(llvm::raw_ostream &OS, const CFG* cfg, const LangOptions &LO) const;
288  void printTerminator(llvm::raw_ostream &OS, const LangOptions &LO) const;
289
290  void addSuccessor(CFGBlock* Block, BumpVectorContext &C) {
291    if (Block)
292      Block->Preds.push_back(this, C);
293    Succs.push_back(Block, C);
294  }
295
296  void appendStmt(Stmt* Statement, BumpVectorContext &C, bool asLValue) {
297      Stmts.push_back(CFGElement(Statement, asLValue), C);
298  }
299};
300
301
302/// CFG - Represents a source-level, intra-procedural CFG that represents the
303///  control-flow of a Stmt.  The Stmt can represent an entire function body,
304///  or a single expression.  A CFG will always contain one empty block that
305///  represents the Exit point of the CFG.  A CFG will also contain a designated
306///  Entry block.  The CFG solely represents control-flow; it consists of
307///  CFGBlocks which are simply containers of Stmt*'s in the AST the CFG
308///  was constructed from.
309class CFG {
310public:
311  //===--------------------------------------------------------------------===//
312  // CFG Construction & Manipulation.
313  //===--------------------------------------------------------------------===//
314
315  class BuildOptions {
316  public:
317    bool PruneTriviallyFalseEdges:1;
318    bool AddEHEdges:1;
319    bool AddInitializers:1;
320    bool AddImplicitDtors:1;
321
322    BuildOptions()
323        : PruneTriviallyFalseEdges(true)
324        , AddEHEdges(false)
325        , AddInitializers(false)
326        , AddImplicitDtors(false) {}
327  };
328
329  /// buildCFG - Builds a CFG from an AST.  The responsibility to free the
330  ///   constructed CFG belongs to the caller.
331  static CFG* buildCFG(const Decl *D, Stmt* AST, ASTContext *C,
332      BuildOptions BO = BuildOptions());
333
334  /// createBlock - Create a new block in the CFG.  The CFG owns the block;
335  ///  the caller should not directly free it.
336  CFGBlock* createBlock();
337
338  /// setEntry - Set the entry block of the CFG.  This is typically used
339  ///  only during CFG construction.  Most CFG clients expect that the
340  ///  entry block has no predecessors and contains no statements.
341  void setEntry(CFGBlock *B) { Entry = B; }
342
343  /// setIndirectGotoBlock - Set the block used for indirect goto jumps.
344  ///  This is typically used only during CFG construction.
345  void setIndirectGotoBlock(CFGBlock* B) { IndirectGotoBlock = B; }
346
347  //===--------------------------------------------------------------------===//
348  // Block Iterators
349  //===--------------------------------------------------------------------===//
350
351  typedef BumpVector<CFGBlock*>                    CFGBlockListTy;
352  typedef CFGBlockListTy::iterator                 iterator;
353  typedef CFGBlockListTy::const_iterator           const_iterator;
354  typedef std::reverse_iterator<iterator>          reverse_iterator;
355  typedef std::reverse_iterator<const_iterator>    const_reverse_iterator;
356
357  CFGBlock&                 front()                { return *Blocks.front(); }
358  CFGBlock&                 back()                 { return *Blocks.back(); }
359
360  iterator                  begin()                { return Blocks.begin(); }
361  iterator                  end()                  { return Blocks.end(); }
362  const_iterator            begin()       const    { return Blocks.begin(); }
363  const_iterator            end()         const    { return Blocks.end(); }
364
365  reverse_iterator          rbegin()               { return Blocks.rbegin(); }
366  reverse_iterator          rend()                 { return Blocks.rend(); }
367  const_reverse_iterator    rbegin()      const    { return Blocks.rbegin(); }
368  const_reverse_iterator    rend()        const    { return Blocks.rend(); }
369
370  CFGBlock&                 getEntry()             { return *Entry; }
371  const CFGBlock&           getEntry()    const    { return *Entry; }
372  CFGBlock&                 getExit()              { return *Exit; }
373  const CFGBlock&           getExit()     const    { return *Exit; }
374
375  CFGBlock*        getIndirectGotoBlock() { return IndirectGotoBlock; }
376  const CFGBlock*  getIndirectGotoBlock() const { return IndirectGotoBlock; }
377
378  //===--------------------------------------------------------------------===//
379  // Member templates useful for various batch operations over CFGs.
380  //===--------------------------------------------------------------------===//
381
382  template <typename CALLBACK>
383  void VisitBlockStmts(CALLBACK& O) const {
384    for (const_iterator I=begin(), E=end(); I != E; ++I)
385      for (CFGBlock::const_iterator BI=(*I)->begin(), BE=(*I)->end();
386           BI != BE; ++BI)
387        O(*BI);
388  }
389
390  //===--------------------------------------------------------------------===//
391  // CFG Introspection.
392  //===--------------------------------------------------------------------===//
393
394  struct   BlkExprNumTy {
395    const signed Idx;
396    explicit BlkExprNumTy(signed idx) : Idx(idx) {}
397    explicit BlkExprNumTy() : Idx(-1) {}
398    operator bool() const { return Idx >= 0; }
399    operator unsigned() const { assert(Idx >=0); return (unsigned) Idx; }
400  };
401
402  bool          isBlkExpr(const Stmt* S) { return getBlkExprNum(S); }
403  BlkExprNumTy  getBlkExprNum(const Stmt* S);
404  unsigned      getNumBlkExprs();
405
406  /// getNumBlockIDs - Returns the total number of BlockIDs allocated (which
407  /// start at 0).
408  unsigned getNumBlockIDs() const { return NumBlockIDs; }
409
410  //===--------------------------------------------------------------------===//
411  // CFG Debugging: Pretty-Printing and Visualization.
412  //===--------------------------------------------------------------------===//
413
414  void viewCFG(const LangOptions &LO) const;
415  void print(llvm::raw_ostream& OS, const LangOptions &LO) const;
416  void dump(const LangOptions &LO) const;
417
418  //===--------------------------------------------------------------------===//
419  // Internal: constructors and data.
420  //===--------------------------------------------------------------------===//
421
422  CFG() : Entry(NULL), Exit(NULL), IndirectGotoBlock(NULL), NumBlockIDs(0),
423          BlkExprMap(NULL), Blocks(BlkBVC, 10) {}
424
425  ~CFG();
426
427  llvm::BumpPtrAllocator& getAllocator() {
428    return BlkBVC.getAllocator();
429  }
430
431  BumpVectorContext &getBumpVectorContext() {
432    return BlkBVC;
433  }
434
435private:
436  CFGBlock* Entry;
437  CFGBlock* Exit;
438  CFGBlock* IndirectGotoBlock;  // Special block to contain collective dispatch
439                                // for indirect gotos
440  unsigned  NumBlockIDs;
441
442  // BlkExprMap - An opaque pointer to prevent inclusion of DenseMap.h.
443  //  It represents a map from Expr* to integers to record the set of
444  //  block-level expressions and their "statement number" in the CFG.
445  void*     BlkExprMap;
446
447  BumpVectorContext BlkBVC;
448
449  CFGBlockListTy Blocks;
450
451};
452} // end namespace clang
453
454//===----------------------------------------------------------------------===//
455// GraphTraits specializations for CFG basic block graphs (source-level CFGs)
456//===----------------------------------------------------------------------===//
457
458namespace llvm {
459
460/// Implement simplify_type for CFGElement, so that we can dyn_cast from
461/// CFGElement to a specific Stmt class.
462template <> struct simplify_type<const ::clang::CFGElement> {
463  typedef ::clang::Stmt* SimpleType;
464  static SimpleType getSimplifiedValue(const ::clang::CFGElement &Val) {
465    return Val.getStmt();
466  }
467};
468
469template <> struct simplify_type< ::clang::CFGElement>
470  : public simplify_type<const ::clang::CFGElement> {};
471
472// Traits for: CFGBlock
473
474template <> struct GraphTraits< ::clang::CFGBlock* > {
475  typedef ::clang::CFGBlock NodeType;
476  typedef ::clang::CFGBlock::succ_iterator ChildIteratorType;
477
478  static NodeType* getEntryNode(::clang::CFGBlock* BB)
479  { return BB; }
480
481  static inline ChildIteratorType child_begin(NodeType* N)
482  { return N->succ_begin(); }
483
484  static inline ChildIteratorType child_end(NodeType* N)
485  { return N->succ_end(); }
486};
487
488template <> struct GraphTraits< const ::clang::CFGBlock* > {
489  typedef const ::clang::CFGBlock NodeType;
490  typedef ::clang::CFGBlock::const_succ_iterator ChildIteratorType;
491
492  static NodeType* getEntryNode(const clang::CFGBlock* BB)
493  { return BB; }
494
495  static inline ChildIteratorType child_begin(NodeType* N)
496  { return N->succ_begin(); }
497
498  static inline ChildIteratorType child_end(NodeType* N)
499  { return N->succ_end(); }
500};
501
502template <> struct GraphTraits<Inverse<const ::clang::CFGBlock*> > {
503  typedef const ::clang::CFGBlock NodeType;
504  typedef ::clang::CFGBlock::const_pred_iterator ChildIteratorType;
505
506  static NodeType *getEntryNode(Inverse<const ::clang::CFGBlock*> G)
507  { return G.Graph; }
508
509  static inline ChildIteratorType child_begin(NodeType* N)
510  { return N->pred_begin(); }
511
512  static inline ChildIteratorType child_end(NodeType* N)
513  { return N->pred_end(); }
514};
515
516// Traits for: CFG
517
518template <> struct GraphTraits< ::clang::CFG* >
519    : public GraphTraits< ::clang::CFGBlock* >  {
520
521  typedef ::clang::CFG::iterator nodes_iterator;
522
523  static NodeType *getEntryNode(::clang::CFG* F) { return &F->getEntry(); }
524  static nodes_iterator nodes_begin(::clang::CFG* F) { return F->begin(); }
525  static nodes_iterator nodes_end(::clang::CFG* F) { return F->end(); }
526};
527
528template <> struct GraphTraits<const ::clang::CFG* >
529    : public GraphTraits<const ::clang::CFGBlock* >  {
530
531  typedef ::clang::CFG::const_iterator nodes_iterator;
532
533  static NodeType *getEntryNode( const ::clang::CFG* F) {
534    return &F->getEntry();
535  }
536  static nodes_iterator nodes_begin( const ::clang::CFG* F) {
537    return F->begin();
538  }
539  static nodes_iterator nodes_end( const ::clang::CFG* F) {
540    return F->end();
541  }
542};
543
544template <> struct GraphTraits<Inverse<const ::clang::CFG*> >
545  : public GraphTraits<Inverse<const ::clang::CFGBlock*> > {
546
547  typedef ::clang::CFG::const_iterator nodes_iterator;
548
549  static NodeType *getEntryNode(const ::clang::CFG* F) { return &F->getExit(); }
550  static nodes_iterator nodes_begin(const ::clang::CFG* F) { return F->begin();}
551  static nodes_iterator nodes_end(const ::clang::CFG* F) { return F->end(); }
552};
553} // end llvm namespace
554#endif
555