Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:36:23

0001 //===- LiveVariables.h - Live Variable Analysis for Source CFGs -*- C++ --*-//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 //
0009 // This file implements Live Variables analysis for source-level CFGs.
0010 //
0011 //===----------------------------------------------------------------------===//
0012 
0013 #ifndef LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
0014 #define LLVM_CLANG_ANALYSIS_ANALYSES_LIVEVARIABLES_H
0015 
0016 #include "clang/AST/Decl.h"
0017 #include "clang/Analysis/AnalysisDeclContext.h"
0018 #include "llvm/ADT/ImmutableSet.h"
0019 
0020 namespace clang {
0021 
0022 class CFG;
0023 class CFGBlock;
0024 class Stmt;
0025 class DeclRefExpr;
0026 class SourceManager;
0027 
0028 class LiveVariables : public ManagedAnalysis {
0029 public:
0030   class LivenessValues {
0031   public:
0032 
0033     llvm::ImmutableSet<const Expr *> liveExprs;
0034     llvm::ImmutableSet<const VarDecl *> liveDecls;
0035     llvm::ImmutableSet<const BindingDecl *> liveBindings;
0036 
0037     bool equals(const LivenessValues &V) const;
0038 
0039     LivenessValues()
0040       : liveExprs(nullptr), liveDecls(nullptr), liveBindings(nullptr) {}
0041 
0042     LivenessValues(llvm::ImmutableSet<const Expr *> liveExprs,
0043                    llvm::ImmutableSet<const VarDecl *> LiveDecls,
0044                    llvm::ImmutableSet<const BindingDecl *> LiveBindings)
0045         : liveExprs(liveExprs), liveDecls(LiveDecls),
0046           liveBindings(LiveBindings) {}
0047 
0048     bool isLive(const Expr *E) const;
0049     bool isLive(const VarDecl *D) const;
0050 
0051     friend class LiveVariables;
0052   };
0053 
0054   class Observer {
0055     virtual void anchor();
0056   public:
0057     virtual ~Observer() {}
0058 
0059     /// A callback invoked right before invoking the
0060     ///  liveness transfer function on the given statement.
0061     virtual void observeStmt(const Stmt *S,
0062                              const CFGBlock *currentBlock,
0063                              const LivenessValues& V) {}
0064 
0065     /// Called when the live variables analysis registers
0066     /// that a variable is killed.
0067     virtual void observerKill(const DeclRefExpr *DR) {}
0068   };
0069 
0070   ~LiveVariables() override;
0071 
0072   /// Compute the liveness information for a given CFG.
0073   static std::unique_ptr<LiveVariables>
0074   computeLiveness(AnalysisDeclContext &analysisContext, bool killAtAssign);
0075 
0076   /// Return true if a variable is live at the end of a
0077   /// specified block.
0078   bool isLive(const CFGBlock *B, const VarDecl *D);
0079 
0080   /// Returns true if a variable is live at the beginning of the
0081   ///  the statement.  This query only works if liveness information
0082   ///  has been recorded at the statement level (see runOnAllBlocks), and
0083   ///  only returns liveness information for block-level expressions.
0084   bool isLive(const Stmt *S, const VarDecl *D);
0085 
0086   /// Returns true the block-level expression value is live
0087   ///  before the given block-level expression (see runOnAllBlocks).
0088   bool isLive(const Stmt *Loc, const Expr *Val);
0089 
0090   /// Print to stderr the variable liveness information associated with
0091   /// each basic block.
0092   void dumpBlockLiveness(const SourceManager &M);
0093 
0094   /// Print to stderr the expression liveness information associated with
0095   /// each basic block.
0096   void dumpExprLiveness(const SourceManager &M);
0097 
0098   void runOnAllBlocks(Observer &obs);
0099 
0100   static std::unique_ptr<LiveVariables>
0101   create(AnalysisDeclContext &analysisContext) {
0102     return computeLiveness(analysisContext, true);
0103   }
0104 
0105   static const void *getTag();
0106 
0107 private:
0108   LiveVariables(void *impl);
0109   void *impl;
0110 };
0111 
0112 class RelaxedLiveVariables : public LiveVariables {
0113 public:
0114   static std::unique_ptr<LiveVariables>
0115   create(AnalysisDeclContext &analysisContext) {
0116     return computeLiveness(analysisContext, false);
0117   }
0118 
0119   static const void *getTag();
0120 };
0121 
0122 } // end namespace clang
0123 
0124 #endif