Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:26

0001 //===-- llvm/CodeGen/AssignmentTrackingAnalysis.h --------------*- 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 #ifndef LLVM_CODEGEN_ASSIGNMENTTRACKINGANALYSIS_H
0010 #define LLVM_CODEGEN_ASSIGNMENTTRACKINGANALYSIS_H
0011 
0012 #include "llvm/IR/DebugInfoMetadata.h"
0013 #include "llvm/IR/DebugLoc.h"
0014 #include "llvm/IR/IntrinsicInst.h"
0015 #include "llvm/IR/PassManager.h"
0016 #include "llvm/Pass.h"
0017 
0018 namespace llvm {
0019 class Instruction;
0020 class raw_ostream;
0021 } // namespace llvm
0022 class FunctionVarLocsBuilder;
0023 
0024 namespace llvm {
0025 /// Type wrapper for integer ID for Variables. 0 is reserved.
0026 enum class VariableID : unsigned { Reserved = 0 };
0027 /// Variable location definition used by FunctionVarLocs.
0028 struct VarLocInfo {
0029   llvm::VariableID VariableID;
0030   DIExpression *Expr = nullptr;
0031   DebugLoc DL;
0032   RawLocationWrapper Values = RawLocationWrapper();
0033 };
0034 
0035 /// Data structure describing the variable locations in a function. Used as the
0036 /// result of the AssignmentTrackingAnalysis pass. Essentially read-only
0037 /// outside of AssignmentTrackingAnalysis where it is built.
0038 class FunctionVarLocs {
0039   /// Maps VarLocInfo.VariableID to a DebugVariable for VarLocRecords.
0040   SmallVector<DebugVariable> Variables;
0041   /// List of variable location changes grouped by the instruction the
0042   /// change occurs before (see VarLocsBeforeInst). The elements from
0043   /// zero to SingleVarLocEnd represent variables with a single location.
0044   SmallVector<VarLocInfo> VarLocRecords;
0045   /// End of range of VarLocRecords that represent variables with a single
0046   /// location that is valid for the entire scope. Range starts at 0.
0047   unsigned SingleVarLocEnd = 0;
0048   /// Maps an instruction to a range of VarLocs that start just before it.
0049   DenseMap<const Instruction *, std::pair<unsigned, unsigned>>
0050       VarLocsBeforeInst;
0051 
0052 public:
0053   /// Return the DILocalVariable for the location definition represented by \p
0054   /// ID.
0055   DILocalVariable *getDILocalVariable(const VarLocInfo *Loc) const {
0056     VariableID VarID = Loc->VariableID;
0057     return getDILocalVariable(VarID);
0058   }
0059   /// Return the DILocalVariable of the variable represented by \p ID.
0060   DILocalVariable *getDILocalVariable(VariableID ID) const {
0061     return const_cast<DILocalVariable *>(getVariable(ID).getVariable());
0062   }
0063   /// Return the DebugVariable represented by \p ID.
0064   const DebugVariable &getVariable(VariableID ID) const {
0065     return Variables[static_cast<unsigned>(ID)];
0066   }
0067 
0068   ///@name iterators
0069   ///@{
0070   /// First single-location variable location definition.
0071   const VarLocInfo *single_locs_begin() const { return VarLocRecords.begin(); }
0072   /// One past the last single-location variable location definition.
0073   const VarLocInfo *single_locs_end() const {
0074     const auto *It = VarLocRecords.begin();
0075     std::advance(It, SingleVarLocEnd);
0076     return It;
0077   }
0078   /// First variable location definition that comes before \p Before.
0079   const VarLocInfo *locs_begin(const Instruction *Before) const {
0080     auto Span = VarLocsBeforeInst.lookup(Before);
0081     const auto *It = VarLocRecords.begin();
0082     std::advance(It, Span.first);
0083     return It;
0084   }
0085   /// One past the last variable location definition that comes before \p
0086   /// Before.
0087   const VarLocInfo *locs_end(const Instruction *Before) const {
0088     auto Span = VarLocsBeforeInst.lookup(Before);
0089     const auto *It = VarLocRecords.begin();
0090     std::advance(It, Span.second);
0091     return It;
0092   }
0093   ///@}
0094 
0095   void print(raw_ostream &OS, const Function &Fn) const;
0096 
0097   ///@{
0098   /// Non-const methods used by AssignmentTrackingAnalysis (which invalidate
0099   /// analysis results if called incorrectly).
0100   void init(FunctionVarLocsBuilder &Builder);
0101   void clear();
0102   ///@}
0103 };
0104 
0105 class DebugAssignmentTrackingAnalysis
0106     : public AnalysisInfoMixin<DebugAssignmentTrackingAnalysis> {
0107   friend AnalysisInfoMixin<DebugAssignmentTrackingAnalysis>;
0108   static AnalysisKey Key;
0109 
0110 public:
0111   using Result = FunctionVarLocs;
0112   Result run(Function &F, FunctionAnalysisManager &FAM);
0113 };
0114 
0115 class DebugAssignmentTrackingPrinterPass
0116     : public PassInfoMixin<DebugAssignmentTrackingPrinterPass> {
0117   raw_ostream &OS;
0118 
0119 public:
0120   DebugAssignmentTrackingPrinterPass(raw_ostream &OS) : OS(OS) {}
0121   PreservedAnalyses run(Function &F, FunctionAnalysisManager &FAM);
0122 };
0123 
0124 class AssignmentTrackingAnalysis : public FunctionPass {
0125   std::unique_ptr<FunctionVarLocs> Results;
0126 
0127 public:
0128   static char ID;
0129 
0130   AssignmentTrackingAnalysis();
0131 
0132   bool runOnFunction(Function &F) override;
0133 
0134   static bool isRequired() { return true; }
0135 
0136   void getAnalysisUsage(AnalysisUsage &AU) const override {
0137     AU.setPreservesAll();
0138   }
0139 
0140   const FunctionVarLocs *getResults() { return Results.get(); }
0141 };
0142 
0143 } // end namespace llvm
0144 #endif // LLVM_CODEGEN_ASSIGNMENTTRACKINGANALYSIS_H