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/DenseMap.h"
20 #include "llvm/ADT/ImmutableSet.h"
30 class LiveVariables : public ManagedAnalysis {
32 class LivenessValues {
35 llvm::ImmutableSet<const Stmt *> liveStmts;
36 llvm::ImmutableSet<const VarDecl *> liveDecls;
38 bool equals(const LivenessValues &V) const;
41 : liveStmts(nullptr), liveDecls(nullptr) {}
43 LivenessValues(llvm::ImmutableSet<const Stmt *> LiveStmts,
44 llvm::ImmutableSet<const VarDecl *> LiveDecls)
45 : liveStmts(LiveStmts), liveDecls(LiveDecls) {}
47 bool isLive(const Stmt *S) const;
48 bool isLive(const VarDecl *D) const;
50 friend class LiveVariables;
54 virtual void anchor();
56 virtual ~Observer() {}
58 /// A callback invoked right before invoking the
59 /// liveness transfer function on the given statement.
60 virtual void observeStmt(const Stmt *S,
61 const CFGBlock *currentBlock,
62 const LivenessValues& V) {}
64 /// Called when the live variables analysis registers
65 /// that a variable is killed.
66 virtual void observerKill(const DeclRefExpr *DR) {}
69 ~LiveVariables() override;
71 /// Compute the liveness information for a given CFG.
72 static LiveVariables *computeLiveness(AnalysisDeclContext &analysisContext,
75 /// Return true if a variable is live at the end of a
77 bool isLive(const CFGBlock *B, const VarDecl *D);
79 /// Returns true if a variable is live at the beginning of the
80 /// the statement. This query only works if liveness information
81 /// has been recorded at the statement level (see runOnAllBlocks), and
82 /// only returns liveness information for block-level expressions.
83 bool isLive(const Stmt *S, const VarDecl *D);
85 /// Returns true the block-level expression "value" is live
86 /// before the given block-level expression (see runOnAllBlocks).
87 bool isLive(const Stmt *Loc, const Stmt *StmtVal);
89 /// Print to stderr the liveness information associated with
91 void dumpBlockLiveness(const SourceManager& M);
93 void runOnAllBlocks(Observer &obs);
95 static LiveVariables *create(AnalysisDeclContext &analysisContext) {
96 return computeLiveness(analysisContext, true);
99 static const void *getTag();
102 LiveVariables(void *impl);
106 class RelaxedLiveVariables : public LiveVariables {
108 static LiveVariables *create(AnalysisDeclContext &analysisContext) {
109 return computeLiveness(analysisContext, false);
112 static const void *getTag();
115 } // end namespace clang