clang 24.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:
30 // Canonicalization of these sets is a major performance loss here, so the
31 // non-canonicalizing form is selected at compile time.
32 template <typename T>
33 using SetTy =
34 llvm::ImmutableSet<T, llvm::ImutContainerInfo<T>, /*Canonicalize=*/false>;
35
58
59 class Observer {
60 virtual void anchor();
61 public:
62 virtual ~Observer() {}
63
64 /// A callback invoked right before invoking the
65 /// liveness transfer function on the given statement.
66 virtual void observeStmt(const Stmt *S, const CFGBlock *currentBlock,
67 const LivenessValues &V) {}
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
107private:
108 LiveVariables(void *impl);
109 void *impl;
110};
111
112class RelaxedLiveVariables : public LiveVariables {
113public:
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
#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:652
Represents a source-level, intra-procedural CFG that represents the control-flow of a Stmt.
Definition CFG.h:1271
A reference to a declared variable, function, enum, etc.
Definition Expr.h:1276
This represents one expression.
Definition Expr.h:112
LivenessValues(SetTy< const Expr * > liveExprs, SetTy< const VarDecl * > LiveDecls, SetTy< const BindingDecl * > LiveBindings)
bool operator==(const LivenessValues &V) const
SetTy< const BindingDecl * > liveBindings
bool isLive(const Expr *E) const
SetTy< const VarDecl * > liveDecls
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.
llvm::ImmutableSet< T, llvm::ImutContainerInfo< T >, false > SetTy
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:932
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',...