clang  14.0.0git
LiveVariables.h
Go to the documentation of this file.
1 //===- LiveVariables.h - Live Variable Analysis for Source CFGs -*- C++ --*-//
2 //
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
6 //
7 //===----------------------------------------------------------------------===//
8 //
9 // This file implements Live Variables analysis for source-level CFGs.
10 //
11 //===----------------------------------------------------------------------===//
12 
13 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
14 #define LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
15 
16 #include "clang/AST/Decl.h"
18 #include "llvm/ADT/ImmutableSet.h"
19 
20 namespace clang {
21 
22 class CFG;
23 class CFGBlock;
24 class Stmt;
25 class DeclRefExpr;
26 class SourceManager;
27 
29 public:
31  public:
32 
33  llvm::ImmutableSet<const Expr *> liveExprs;
34  llvm::ImmutableSet<const VarDecl *> liveDecls;
35  llvm::ImmutableSet<const BindingDecl *> liveBindings;
36 
37  bool equals(const LivenessValues &V) const;
38 
40  : liveExprs(nullptr), liveDecls(nullptr), liveBindings(nullptr) {}
41 
42  LivenessValues(llvm::ImmutableSet<const Expr *> liveExprs,
43  llvm::ImmutableSet<const VarDecl *> LiveDecls,
44  llvm::ImmutableSet<const BindingDecl *> LiveBindings)
45  : liveExprs(liveExprs), liveDecls(LiveDecls),
46  liveBindings(LiveBindings) {}
47 
48  bool isLive(const Expr *E) const;
49  bool isLive(const VarDecl *D) const;
50 
51  friend class LiveVariables;
52  };
53 
54  class Observer {
55  virtual void anchor();
56  public:
57  virtual ~Observer() {}
58 
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) {}
64 
65  /// Called when the live variables analysis registers
66  /// that a variable is killed.
67  virtual void observerKill(const DeclRefExpr *DR) {}
68  };
69 
70  ~LiveVariables() override;
71 
72  /// Compute the liveness information for a given CFG.
73  static std::unique_ptr<LiveVariables>
74  computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign);
75 
76  /// Return true if a variable is live at the end of a
77  /// specified block.
78  bool isLive(const CFGBlock *B, const VarDecl *D);
79 
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);
85 
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 Expr *Val);
89 
90  /// Print to stderr the variable liveness information associated with
91  /// each basic block.
92  void dumpBlockLiveness(const SourceManager &M);
93 
94  /// Print to stderr the expression liveness information associated with
95  /// each basic block.
96  void dumpExprLiveness(const SourceManager &M);
97 
98  void runOnAllBlocks(Observer &obs);
99 
100  static std::unique_ptr<LiveVariables>
101  create(AnalysisDeclContext &analysisContext) {
102  return computeLiveness(analysisContext, true);
103  }
104 
105  static const void *getTag();
106 
107 private:
108  LiveVariables(void *impl);
109  void *impl;
110 };
111 
113 public:
114  static std::unique_ptr<LiveVariables>
115  create(AnalysisDeclContext &analysisContext) {
116  return computeLiveness(analysisContext, false);
117  }
118 
119  static const void *getTag();
120 };
121 
122 } // end namespace clang
123 
124 #endif
clang::LiveVariables::LivenessValues::isLive
bool isLive(const Expr *E) const
Definition: LiveVariables.cpp:66
clang::LiveVariables::LivenessValues::equals
bool equals(const LivenessValues &V) const
Definition: LiveVariables.cpp:123
AnalysisDeclContext.h
clang::LiveVariables::LivenessValues
Definition: LiveVariables.h:30
clang::LiveVariables::create
static std::unique_ptr< LiveVariables > create(AnalysisDeclContext &analysisContext)
Definition: LiveVariables.h:101
clang::AnalysisDeclContext
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Definition: AnalysisDeclContext.h:72
clang::RelaxedLiveVariables::create
static std::unique_ptr< LiveVariables > create(AnalysisDeclContext &analysisContext)
Definition: LiveVariables.h:115
clang::RelaxedLiveVariables::getTag
static const void * getTag()
Definition: LiveVariables.cpp:621
clang::LiveVariables::Observer::observeStmt
virtual void observeStmt(const Stmt *S, const CFGBlock *currentBlock, const LivenessValues &V)
A callback invoked right before invoking the liveness transfer function on the given statement.
Definition: LiveVariables.h:61
clang::SourceManager
This class handles loading and caching of source files into memory.
Definition: SourceManager.h:626
clang::LiveVariables::isLive
bool isLive(const CFGBlock *B, const VarDecl *D)
Return true if a variable is live at the end of a specified block.
Definition: LiveVariables.cpp:135
Decl.h
clang::CFGBlock
Represents a single basic block in a source-level CFG.
Definition: CFG.h:576
clang::LiveVariables::LivenessValues::liveBindings
llvm::ImmutableSet< const BindingDecl * > liveBindings
Definition: LiveVariables.h:35
clang::LiveVariables::~LiveVariables
~LiveVariables() override
Definition: LiveVariables.cpp:496
V
#define V(N, I)
Definition: ASTContext.h:3121
clang::LiveVariables::runOnAllBlocks
void runOnAllBlocks(Observer &obs)
Definition: LiveVariables.cpp:488
clang::LiveVariables::computeLiveness
static std::unique_ptr< LiveVariables > computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign)
Compute the liveness information for a given CFG.
Definition: LiveVariables.cpp:501
clang::LiveVariables::LivenessValues::LivenessValues
LivenessValues()
Definition: LiveVariables.h:39
clang::LiveVariables::LivenessValues::LivenessValues
LivenessValues(llvm::ImmutableSet< const Expr * > liveExprs, llvm::ImmutableSet< const VarDecl * > LiveDecls, llvm::ImmutableSet< const BindingDecl * > LiveBindings)
Definition: LiveVariables.h:42
clang::LiveVariables::dumpBlockLiveness
void dumpBlockLiveness(const SourceManager &M)
Print to stderr the variable liveness information associated with each basic block.
Definition: LiveVariables.cpp:556
clang::VarDecl
Represents a variable declaration or definition.
Definition: Decl.h:876
clang::LiveVariables::dumpExprLiveness
void dumpExprLiveness(const SourceManager &M)
Print to stderr the expression liveness information associated with each basic block.
Definition: LiveVariables.cpp:602
clang::LiveVariables::getTag
static const void * getTag()
Definition: LiveVariables.cpp:620
clang::LiveVariables
Definition: LiveVariables.h:28
clang::ManagedAnalysis
The base class of a hierarchy of objects representing analyses tied to AnalysisDeclContext.
Definition: AnalysisDeclContext.h:51
clang::LiveVariables::Observer
Definition: LiveVariables.h:54
clang::LiveVariables::LivenessValues::liveDecls
llvm::ImmutableSet< const VarDecl * > liveDecls
Definition: LiveVariables.h:34
clang::LiveVariables::Observer::~Observer
virtual ~Observer()
Definition: LiveVariables.h:57
clang
Definition: CalledOnceCheck.h:17
clang::Stmt
Stmt - This represents one statement.
Definition: Stmt.h:69
clang::LiveVariables::LivenessValues::liveExprs
llvm::ImmutableSet< const Expr * > liveExprs
Definition: LiveVariables.h:33
clang::RelaxedLiveVariables
Definition: LiveVariables.h:112
clang::Expr
This represents one expression.
Definition: Expr.h:109
clang::DeclRefExpr
A reference to a declared variable, function, enum, etc.
Definition: Expr.h:1217
clang::LiveVariables::Observer::observerKill
virtual void observerKill(const DeclRefExpr *DR)
Called when the live variables analysis registers that a variable is killed.
Definition: LiveVariables.h:67