clang 22.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
20namespace clang {
21
22class CFG;
23class CFGBlock;
24class Stmt;
25class DeclRefExpr;
26class SourceManager;
27
28class LiveVariables : public ManagedAnalysis {
29public:
31 public:
32
33 llvm::ImmutableSet<const Expr *> liveExprs;
34 llvm::ImmutableSet<const VarDecl *> liveDecls;
35 llvm::ImmutableSet<const BindingDecl *> liveBindings;
36
37 bool operator==(const LivenessValues &V) const;
38
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, const CFGBlock *currentBlock,
62 const LivenessValues &V) {}
63 };
64
65 ~LiveVariables() override;
66
67 /// Compute the liveness information for a given CFG.
68 static std::unique_ptr<LiveVariables>
69 computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign);
70
71 /// Return true if a variable is live at the end of a
72 /// specified block.
73 bool isLive(const CFGBlock *B, const VarDecl *D);
74
75 /// Returns true if a variable is live at the beginning of the
76 /// the statement. This query only works if liveness information
77 /// has been recorded at the statement level (see runOnAllBlocks), and
78 /// only returns liveness information for block-level expressions.
79 bool isLive(const Stmt *S, const VarDecl *D);
80
81 /// Returns true the block-level expression value is live
82 /// before the given block-level expression (see runOnAllBlocks).
83 bool isLive(const Stmt *Loc, const Expr *Val);
84
85 /// Print to stderr the variable liveness information associated with
86 /// each basic block.
87 void dumpBlockLiveness(const SourceManager &M);
88
89 /// Print to stderr the expression liveness information associated with
90 /// each basic block.
91 void dumpExprLiveness(const SourceManager &M);
92
93 void runOnAllBlocks(Observer &obs);
94
95 static std::unique_ptr<LiveVariables>
96 create(AnalysisDeclContext &analysisContext) {
97 return computeLiveness(analysisContext, true);
98 }
99
100 static const void *getTag();
101
102private:
103 LiveVariables(void *impl);
104 void *impl;
105};
106
107class RelaxedLiveVariables : public LiveVariables {
108public:
109 static std::unique_ptr<LiveVariables>
110 create(AnalysisDeclContext &analysisContext) {
111 return computeLiveness(analysisContext, false);
112 }
113
114 static const void *getTag();
115};
116
117} // end namespace clang
118
119#endif
#define V(N, I)
This file defines AnalysisDeclContext, a class that manages the analysis context data for context sen...
AnalysisDeclContext contains the context data for the function, method or block under analysis.
Represents a single basic block in a source-level CFG.
Definition CFG.h:605
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1222
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1272
This represents one expression.
Definition Expr.h:112
llvm::ImmutableSet< const BindingDecl * > liveBindings
LivenessValues(llvm::ImmutableSet< const Expr * > liveExprs, llvm::ImmutableSet< const VarDecl * > LiveDecls, llvm::ImmutableSet< const BindingDecl * > LiveBindings)
llvm::ImmutableSet< const Expr * > liveExprs
bool operator==(const LivenessValues &V) const
llvm::ImmutableSet< const VarDecl * > liveDecls
bool isLive(const Expr *E) const
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.
static std::unique_ptr< LiveVariables > create(AnalysisDeclContext &analysisContext)
void dumpExprLiveness(const SourceManager &M)
Print to stderr the expression liveness information associated with each basic block.
void dumpBlockLiveness(const SourceManager &M)
Print to stderr the variable liveness information associated with each basic block.
void runOnAllBlocks(Observer &obs)
static const void * getTag()
bool isLive(const CFGBlock *B, const VarDecl *D)
Return true if a variable is live at the end of a specified block.
static std::unique_ptr< LiveVariables > computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign)
Compute the liveness information for a given CFG.
static std::unique_ptr< LiveVariables > create(AnalysisDeclContext &analysisContext)
static const void * getTag()
This class handles loading and caching of source files into memory.
Stmt - This represents one statement.
Definition Stmt.h:85
Represents a variable declaration or definition.
Definition Decl.h:925
The JSON file list parser is used to communicate input to InstallAPI.
nullptr
This class represents a compute construct, representing a 'Kind' of ‘parallel’, 'serial',...