1 //===- LiveVariables.h - Live Variable Analysis for Source 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 implements Live Variables analysis for source-level CFGs.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
15 #define LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
17 #include "clang/AST/Decl.h"
18 #include "clang/Analysis/AnalysisContext.h"
19 #include "llvm/ADT/ImmutableSet.h"
29 class LiveVariables : public ManagedAnalysis {
31 class LivenessValues {
34 llvm::ImmutableSet<const Stmt *> liveStmts;
35 llvm::ImmutableSet<const VarDecl *> liveDecls;
37 bool equals(const LivenessValues &V) const;
40 : liveStmts(nullptr), liveDecls(nullptr) {}
42 LivenessValues(llvm::ImmutableSet<const Stmt *> LiveStmts,
43 llvm::ImmutableSet<const VarDecl *> LiveDecls)
44 : liveStmts(LiveStmts), liveDecls(LiveDecls) {}
46 bool isLive(const Stmt *S) const;
47 bool isLive(const VarDecl *D) const;
49 friend class LiveVariables;
53 virtual void anchor();
55 virtual ~Observer() {}
57 /// A callback invoked right before invoking the
58 /// liveness transfer function on the given statement.
59 virtual void observeStmt(const Stmt *S,
60 const CFGBlock *currentBlock,
61 const LivenessValues& V) {}
63 /// Called when the live variables analysis registers
64 /// that a variable is killed.
65 virtual void observerKill(const DeclRefExpr *DR) {}
68 ~LiveVariables() override;
70 /// Compute the liveness information for a given CFG.
71 static LiveVariables *computeLiveness(AnalysisDeclContext &analysisContext,
74 /// Return true if a variable is live at the end of a
76 bool isLive(const CFGBlock *B, const VarDecl *D);
78 /// Returns true if a variable is live at the beginning of the
79 /// the statement. This query only works if liveness information
80 /// has been recorded at the statement level (see runOnAllBlocks), and
81 /// only returns liveness information for block-level expressions.
82 bool isLive(const Stmt *S, const VarDecl *D);
84 /// Returns true the block-level expression "value" is live
85 /// before the given block-level expression (see runOnAllBlocks).
86 bool isLive(const Stmt *Loc, const Stmt *StmtVal);
88 /// Print to stderr the liveness information associated with
90 void dumpBlockLiveness(const SourceManager& M);
92 void runOnAllBlocks(Observer &obs);
94 static LiveVariables *create(AnalysisDeclContext &analysisContext) {
95 return computeLiveness(analysisContext, true);
98 static const void *getTag();
101 LiveVariables(void *impl);
105 class RelaxedLiveVariables : public LiveVariables {
107 static LiveVariables *create(AnalysisDeclContext &analysisContext) {
108 return computeLiveness(analysisContext, false);
111 static const void *getTag();
114 } // end namespace clang