clang API Documentation
00001 //===- LiveVariables.h - Live Variable Analysis for Source CFGs -*- C++ --*-// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements Live Variables analysis for source-level CFGs. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H 00015 #define LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H 00016 00017 #include "clang/AST/Decl.h" 00018 #include "clang/Analysis/AnalysisContext.h" 00019 #include "llvm/ADT/DenseMap.h" 00020 #include "llvm/ADT/ImmutableSet.h" 00021 00022 namespace clang { 00023 00024 class CFG; 00025 class CFGBlock; 00026 class Stmt; 00027 class DeclRefExpr; 00028 class SourceManager; 00029 00030 class LiveVariables : public ManagedAnalysis { 00031 public: 00032 class LivenessValues { 00033 public: 00034 00035 llvm::ImmutableSet<const Stmt *> liveStmts; 00036 llvm::ImmutableSet<const VarDecl *> liveDecls; 00037 00038 bool equals(const LivenessValues &V) const; 00039 00040 LivenessValues() 00041 : liveStmts(nullptr), liveDecls(nullptr) {} 00042 00043 LivenessValues(llvm::ImmutableSet<const Stmt *> LiveStmts, 00044 llvm::ImmutableSet<const VarDecl *> LiveDecls) 00045 : liveStmts(LiveStmts), liveDecls(LiveDecls) {} 00046 00047 ~LivenessValues() {} 00048 00049 bool isLive(const Stmt *S) const; 00050 bool isLive(const VarDecl *D) const; 00051 00052 friend class LiveVariables; 00053 }; 00054 00055 class Observer { 00056 virtual void anchor(); 00057 public: 00058 virtual ~Observer() {} 00059 00060 /// A callback invoked right before invoking the 00061 /// liveness transfer function on the given statement. 00062 virtual void observeStmt(const Stmt *S, 00063 const CFGBlock *currentBlock, 00064 const LivenessValues& V) {} 00065 00066 /// Called when the live variables analysis registers 00067 /// that a variable is killed. 00068 virtual void observerKill(const DeclRefExpr *DR) {} 00069 }; 00070 00071 00072 virtual ~LiveVariables(); 00073 00074 /// Compute the liveness information for a given CFG. 00075 static LiveVariables *computeLiveness(AnalysisDeclContext &analysisContext, 00076 bool killAtAssign); 00077 00078 /// Return true if a variable is live at the end of a 00079 /// specified block. 00080 bool isLive(const CFGBlock *B, const VarDecl *D); 00081 00082 /// Returns true if a variable is live at the beginning of the 00083 /// the statement. This query only works if liveness information 00084 /// has been recorded at the statement level (see runOnAllBlocks), and 00085 /// only returns liveness information for block-level expressions. 00086 bool isLive(const Stmt *S, const VarDecl *D); 00087 00088 /// Returns true the block-level expression "value" is live 00089 /// before the given block-level expression (see runOnAllBlocks). 00090 bool isLive(const Stmt *Loc, const Stmt *StmtVal); 00091 00092 /// Print to stderr the liveness information associated with 00093 /// each basic block. 00094 void dumpBlockLiveness(const SourceManager& M); 00095 00096 void runOnAllBlocks(Observer &obs); 00097 00098 static LiveVariables *create(AnalysisDeclContext &analysisContext) { 00099 return computeLiveness(analysisContext, true); 00100 } 00101 00102 static const void *getTag(); 00103 00104 private: 00105 LiveVariables(void *impl); 00106 void *impl; 00107 }; 00108 00109 class RelaxedLiveVariables : public LiveVariables { 00110 public: 00111 static LiveVariables *create(AnalysisDeclContext &analysisContext) { 00112 return computeLiveness(analysisContext, false); 00113 } 00114 00115 static const void *getTag(); 00116 }; 00117 00118 } // end namespace clang 00119 00120 #endif