Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===--- DataflowValues.h - Data structure for dataflow values --*- 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 defines a skeleton data structure for encapsulating the dataflow
0010 // values for a CFG.  Typically this is subclassed to provide methods for
0011 // computing these values from a CFG.
0012 //
0013 //===----------------------------------------------------------------------===//
0014 
0015 #ifndef LLVM_CLANG_ANALYSES_DATAFLOW_VALUES
0016 #define LLVM_CLANG_ANALYSES_DATAFLOW_VALUES
0017 
0018 #include "clang/Analysis/CFG.h"
0019 #include "clang/Analysis/ProgramPoint.h"
0020 #include "llvm/ADT/DenseMap.h"
0021 
0022 namespace clang {
0023 
0024 //===----------------------------------------------------------------------===//
0025 /// Dataflow Directional Tag Classes.  These are used for tag dispatching
0026 ///  within the dataflow solver/transfer functions to determine what direction
0027 ///  a dataflow analysis flows.
0028 //===----------------------------------------------------------------------===//
0029 
0030 namespace dataflow {
0031   struct forward_analysis_tag {};
0032   struct backward_analysis_tag {};
0033 } // end namespace dataflow
0034 
0035 //===----------------------------------------------------------------------===//
0036 /// DataflowValues.  Container class to store dataflow values for a CFG.
0037 //===----------------------------------------------------------------------===//
0038 
0039 template <typename ValueTypes,
0040           typename _AnalysisDirTag = dataflow::forward_analysis_tag >
0041 class DataflowValues {
0042 
0043   //===--------------------------------------------------------------------===//
0044   // Type declarations.
0045   //===--------------------------------------------------------------------===//
0046 
0047 public:
0048   using ValTy = typename ValueTypes::ValTy;
0049   using AnalysisDataTy = typename ValueTypes::AnalysisDataTy;
0050   using AnalysisDirTag = _AnalysisDirTag;
0051   using EdgeDataMapTy = llvm::DenseMap<ProgramPoint, ValTy>;
0052   using BlockDataMapTy = llvm::DenseMap<const CFGBlock *, ValTy>;
0053   using StmtDataMapTy = llvm::DenseMap<const Stmt *, ValTy>;
0054 
0055   //===--------------------------------------------------------------------===//
0056   // Predicates.
0057   //===--------------------------------------------------------------------===//
0058 
0059 public:
0060   /// isForwardAnalysis - Returns true if the dataflow values are computed
0061   ///  from a forward analysis.
0062   bool isForwardAnalysis() { return isForwardAnalysis(AnalysisDirTag()); }
0063 
0064   /// isBackwardAnalysis - Returns true if the dataflow values are computed
0065   ///  from a backward analysis.
0066   bool isBackwardAnalysis() { return !isForwardAnalysis(); }
0067 
0068 private:
0069   bool isForwardAnalysis(dataflow::forward_analysis_tag)  { return true; }
0070   bool isForwardAnalysis(dataflow::backward_analysis_tag) { return false; }
0071 
0072   //===--------------------------------------------------------------------===//
0073   // Initialization and accessors methods.
0074   //===--------------------------------------------------------------------===//
0075 
0076 public:
0077   DataflowValues() : StmtDataMap(NULL) {}
0078   ~DataflowValues() { delete StmtDataMap; }
0079 
0080   /// InitializeValues - Invoked by the solver to initialize state needed for
0081   ///  dataflow analysis.  This method is usually specialized by subclasses.
0082   void InitializeValues(const CFG& cfg) {}
0083 
0084 
0085   /// getEdgeData - Retrieves the dataflow values associated with a
0086   ///  CFG edge.
0087   ValTy& getEdgeData(const BlockEdge &E) {
0088     typename EdgeDataMapTy::iterator I = EdgeDataMap.find(E);
0089     assert (I != EdgeDataMap.end() && "No data associated with Edge.");
0090     return I->second;
0091   }
0092 
0093   const ValTy& getEdgeData(const BlockEdge &E) const {
0094     return reinterpret_cast<DataflowValues*>(this)->getEdgeData(E);
0095   }
0096 
0097   /// getBlockData - Retrieves the dataflow values associated with a
0098   ///  specified CFGBlock.  If the dataflow analysis is a forward analysis,
0099   ///  this data is associated with the END of the block.  If the analysis
0100   ///  is a backwards analysis, it is associated with the ENTRY of the block.
0101   ValTy& getBlockData(const CFGBlock *B) {
0102     typename BlockDataMapTy::iterator I = BlockDataMap.find(B);
0103     assert (I != BlockDataMap.end() && "No data associated with block.");
0104     return I->second;
0105   }
0106 
0107   const ValTy& getBlockData(const CFGBlock *B) const {
0108     return const_cast<DataflowValues*>(this)->getBlockData(B);
0109   }
0110 
0111   /// getStmtData - Retrieves the dataflow values associated with a
0112   ///  specified Stmt.  If the dataflow analysis is a forward analysis,
0113   ///  this data corresponds to the point immediately before a Stmt.
0114   ///  If the analysis is a backwards analysis, it is associated with
0115   ///  the point after a Stmt.  This data is only computed for block-level
0116   ///  expressions, and only when requested when the analysis is executed.
0117   ValTy& getStmtData(const Stmt *S) {
0118     assert (StmtDataMap && "Dataflow values were not computed for statements.");
0119     typename StmtDataMapTy::iterator I = StmtDataMap->find(S);
0120     assert (I != StmtDataMap->end() && "No data associated with statement.");
0121     return I->second;
0122   }
0123 
0124   const ValTy& getStmtData(const Stmt *S) const {
0125     return const_cast<DataflowValues*>(this)->getStmtData(S);
0126   }
0127 
0128   /// getEdgeDataMap - Retrieves the internal map between CFG edges and
0129   ///  dataflow values.  Usually used by a dataflow solver to compute
0130   ///  values for blocks.
0131   EdgeDataMapTy& getEdgeDataMap() { return EdgeDataMap; }
0132   const EdgeDataMapTy& getEdgeDataMap() const { return EdgeDataMap; }
0133 
0134   /// getBlockDataMap - Retrieves the internal map between CFGBlocks and
0135   /// dataflow values.  If the dataflow analysis operates in the forward
0136   /// direction, the values correspond to the dataflow values at the start
0137   /// of the block.  Otherwise, for a backward analysis, the values correspond
0138   /// to the dataflow values at the end of the block.
0139   BlockDataMapTy& getBlockDataMap() { return BlockDataMap; }
0140   const BlockDataMapTy& getBlockDataMap() const { return BlockDataMap; }
0141 
0142   /// getStmtDataMap - Retrieves the internal map between Stmts and
0143   /// dataflow values.
0144   StmtDataMapTy& getStmtDataMap() {
0145     if (!StmtDataMap) StmtDataMap = new StmtDataMapTy();
0146     return *StmtDataMap;
0147   }
0148 
0149   const StmtDataMapTy& getStmtDataMap() const {
0150     return const_cast<DataflowValues*>(this)->getStmtDataMap();
0151   }
0152 
0153   /// getAnalysisData - Retrieves the meta data associated with a
0154   ///  dataflow analysis for analyzing a particular CFG.
0155   ///  This is typically consumed by transfer function code (via the solver).
0156   ///  This can also be used by subclasses to interpret the dataflow values.
0157   AnalysisDataTy& getAnalysisData() { return AnalysisData; }
0158   const AnalysisDataTy& getAnalysisData() const { return AnalysisData; }
0159 
0160   //===--------------------------------------------------------------------===//
0161   // Internal data.
0162   //===--------------------------------------------------------------------===//
0163 
0164 protected:
0165   EdgeDataMapTy      EdgeDataMap;
0166   BlockDataMapTy     BlockDataMap;
0167   StmtDataMapTy*     StmtDataMap;
0168   AnalysisDataTy     AnalysisData;
0169 };
0170 
0171 } // end namespace clang
0172 #endif // LLVM_CLANG_ANALYSES_DATAFLOW_VALUES