1 //===- LiveVariables.h - Live Variable Analysis for Source CFGs -*- C++ --*-//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file implements Live Variables analysis for source-level CFGs.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
14 #define LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
16 #include "clang/AST/Decl.h"
17 #include "clang/Analysis/AnalysisDeclContext.h"
18 #include "llvm/ADT/ImmutableSet.h"
28 class LiveVariables : public ManagedAnalysis {
30 class LivenessValues {
33 llvm::ImmutableSet<const Stmt *> liveStmts;
34 llvm::ImmutableSet<const VarDecl *> liveDecls;
35 llvm::ImmutableSet<const BindingDecl *> liveBindings;
37 bool equals(const LivenessValues &V) const;
40 : liveStmts(nullptr), liveDecls(nullptr), liveBindings(nullptr) {}
42 LivenessValues(llvm::ImmutableSet<const Stmt *> LiveStmts,
43 llvm::ImmutableSet<const VarDecl *> LiveDecls,
44 llvm::ImmutableSet<const BindingDecl *> LiveBindings)
45 : liveStmts(LiveStmts), liveDecls(LiveDecls),
46 liveBindings(LiveBindings) {}
48 bool isLive(const Stmt *S) const;
49 bool isLive(const VarDecl *D) const;
51 friend class LiveVariables;
55 virtual void anchor();
57 virtual ~Observer() {}
59 /// A callback invoked right before invoking the
60 /// liveness transfer function on the given statement.
61 virtual void observeStmt(const Stmt *S,
62 const CFGBlock *currentBlock,
63 const LivenessValues& V) {}
65 /// Called when the live variables analysis registers
66 /// that a variable is killed.
67 virtual void observerKill(const DeclRefExpr *DR) {}
70 ~LiveVariables() override;
72 /// Compute the liveness information for a given CFG.
73 static LiveVariables *computeLiveness(AnalysisDeclContext &analysisContext,
76 /// Return true if a variable is live at the end of a
78 bool isLive(const CFGBlock *B, const VarDecl *D);
80 /// Returns true if a variable is live at the beginning of the
81 /// the statement. This query only works if liveness information
82 /// has been recorded at the statement level (see runOnAllBlocks), and
83 /// only returns liveness information for block-level expressions.
84 bool isLive(const Stmt *S, const VarDecl *D);
86 /// Returns true the block-level expression "value" is live
87 /// before the given block-level expression (see runOnAllBlocks).
88 bool isLive(const Stmt *Loc, const Stmt *StmtVal);
90 /// Print to stderr the variable liveness information associated with
92 void dumpBlockLiveness(const SourceManager &M);
94 /// Print to stderr the statement liveness information associated with
96 void dumpStmtLiveness(const SourceManager &M);
98 void runOnAllBlocks(Observer &obs);
100 static LiveVariables *create(AnalysisDeclContext &analysisContext) {
101 return computeLiveness(analysisContext, true);
104 static const void *getTag();
107 LiveVariables(void *impl);
111 class RelaxedLiveVariables : public LiveVariables {
113 static LiveVariables *create(AnalysisDeclContext &analysisContext) {
114 return computeLiveness(analysisContext, false);
117 static const void *getTag();
120 } // end namespace clang