LiveVariables.h revision e0dbda136444b2f3550a421f4436d438230c732f
1//===- LiveVariables.h - Live Variable Analysis for Source 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 implements Live Variables analysis for source-level CFGs. 11// 12//===----------------------------------------------------------------------===// 13 14#ifndef LLVM_CLANG_LIVEVARIABLES_H 15#define LLVM_CLANG_LIVEVARIABLES_H 16 17#include "clang/AST/Decl.h" 18#include "clang/Analysis/Support/BlkExprDeclBitVector.h" 19#include "clang/Analysis/FlowSensitive/DataflowValues.h" 20 21namespace clang { 22 23class Stmt; 24class DeclRefExpr; 25class SourceManager; 26 27struct LiveVariables_ValueTypes { 28 29 struct ObserverTy; 30 31 // We keep dataflow state for declarations and block-level expressions; 32 typedef StmtDeclBitVector_Types::ValTy ValTy; 33 34 // We need to keep track of both declarations and CFGBlock-level expressions, 35 // (so that we don't explore such expressions twice). We also want 36 // to compute liveness information for block-level expressions, since these 37 // act as "temporary" values. 38 39 struct AnalysisDataTy : public StmtDeclBitVector_Types::AnalysisDataTy { 40 ObserverTy* Observer; 41 ValTy AlwaysLive; 42 43 AnalysisDataTy() : Observer(NULL) {} 44 }; 45 46 //===-----------------------------------------------------===// 47 // ObserverTy - Observer for uninitialized values queries. 48 //===-----------------------------------------------------===// 49 50 struct ObserverTy { 51 virtual ~ObserverTy() {} 52 53 /// ObserveStmt - A callback invoked right before invoking the 54 /// liveness transfer function on the given statement. 55 virtual void ObserveStmt(Stmt* S, const AnalysisDataTy& AD, 56 const ValTy& V) {} 57 58 virtual void ObserverKill(DeclRefExpr* DR) {} 59 }; 60}; 61 62class LiveVariables : public DataflowValues<LiveVariables_ValueTypes, 63 dataflow::backward_analysis_tag> { 64 65 66public: 67 typedef LiveVariables_ValueTypes::ObserverTy ObserverTy; 68 69 LiveVariables(ASTContext& Ctx, CFG& cfg); 70 71 /// IsLive - Return true if a variable is live at beginning of a 72 /// specified block. 73 bool isLive(const CFGBlock* B, const VarDecl* D) const; 74 75 /// IsLive - Returns true if a variable is live at the beginning of the 76 /// the statement. This query only works if liveness information 77 /// has been recorded at the statement level (see runOnAllBlocks), and 78 /// only returns liveness information for block-level expressions. 79 bool isLive(const Stmt* S, const VarDecl* D) const; 80 81 /// IsLive - Returns true the block-level expression "value" is live 82 /// before the given block-level expression (see runOnAllBlocks). 83 bool isLive(const Stmt* Loc, const Stmt* StmtVal) const; 84 85 /// IsLive - Return true if a variable is live according to the 86 /// provided livness bitvector. 87 bool isLive(const ValTy& V, const VarDecl* D) const; 88 89 /// dumpLiveness - Print to stderr the liveness information encoded 90 /// by a specified bitvector. 91 void dumpLiveness(const ValTy& V, SourceManager& M) const; 92 93 /// dumpBlockLiveness - Print to stderr the liveness information 94 /// associated with each basic block. 95 void dumpBlockLiveness(SourceManager& M) const; 96 97 /// getNumDecls - Return the number of variables (declarations) that 98 /// whose liveness status is being tracked by the dataflow 99 /// analysis. 100 unsigned getNumDecls() const { return getAnalysisData().getNumDecls(); } 101 102 /// IntializeValues - This routine can perform extra initialization, but 103 /// for LiveVariables this does nothing since all that logic is in 104 /// the constructor. 105 void InitializeValues(const CFG& cfg) {} 106 107 void runOnCFG(CFG& cfg); 108 109 /// runOnAllBlocks - Propagate the dataflow values once for each block, 110 /// starting from the current dataflow values. 'recordStmtValues' indicates 111 /// whether the method should store dataflow values per each individual 112 /// block-level expression. 113 void runOnAllBlocks(const CFG& cfg, ObserverTy* Obs, 114 bool recordStmtValues=false); 115}; 116 117} // end namespace clang 118 119#endif 120