Back to home page

EIC code displayed by LXR

 
 

    


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

0001 //===- llvm/CodeGen/DbgEntityHistoryCalculator.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_DBGENTITYHISTORYCALCULATOR_H
0010 #define LLVM_CODEGEN_DBGENTITYHISTORYCALCULATOR_H
0011 
0012 #include "llvm/ADT/MapVector.h"
0013 #include "llvm/ADT/PointerIntPair.h"
0014 #include "llvm/ADT/SmallVector.h"
0015 #include "llvm/CodeGen/MachineInstr.h"
0016 #include <utility>
0017 
0018 namespace llvm {
0019 
0020 class DILocation;
0021 class LexicalScopes;
0022 class DINode;
0023 class MachineFunction;
0024 class TargetRegisterInfo;
0025 
0026 /// Record instruction ordering so we can query their relative positions within
0027 /// a function. Meta instructions are given the same ordinal as the preceding
0028 /// non-meta instruction. Class state is invalid if MF is modified after
0029 /// calling initialize.
0030 class InstructionOrdering {
0031 public:
0032   void initialize(const MachineFunction &MF);
0033   void clear() { InstNumberMap.clear(); }
0034 
0035   /// Check if instruction \p A comes before \p B, where \p A and \p B both
0036   /// belong to the MachineFunction passed to initialize().
0037   bool isBefore(const MachineInstr *A, const MachineInstr *B) const;
0038 
0039 private:
0040   /// Each instruction is assigned an order number.
0041   DenseMap<const MachineInstr *, unsigned> InstNumberMap;
0042 };
0043 
0044 /// For each user variable, keep a list of instruction ranges where this
0045 /// variable is accessible. The variables are listed in order of appearance.
0046 class DbgValueHistoryMap {
0047 public:
0048   /// Index in the entry vector.
0049   typedef size_t EntryIndex;
0050 
0051   /// Special value to indicate that an entry is valid until the end of the
0052   /// function.
0053   static const EntryIndex NoEntry = std::numeric_limits<EntryIndex>::max();
0054 
0055   /// Specifies a change in a variable's debug value history.
0056   ///
0057   /// There exist two types of entries:
0058   ///
0059   /// * Debug value entry:
0060   ///
0061   ///   A new debug value becomes live. If the entry's \p EndIndex is \p NoEntry,
0062   ///   the value is valid until the end of the function. For other values, the
0063   ///   index points to the entry in the entry vector that ends this debug
0064   ///   value. The ending entry can either be an overlapping debug value, or
0065   ///   an instruction that clobbers the value.
0066   ///
0067   /// * Clobbering entry:
0068   ///
0069   ///   This entry's instruction clobbers one or more preceding
0070   ///   register-described debug values that have their end index
0071   ///   set to this entry's position in the entry vector.
0072   class Entry {
0073     friend DbgValueHistoryMap;
0074 
0075   public:
0076     enum EntryKind { DbgValue, Clobber };
0077 
0078     Entry(const MachineInstr *Instr, EntryKind Kind)
0079         : Instr(Instr, Kind), EndIndex(NoEntry) {}
0080 
0081     const MachineInstr *getInstr() const { return Instr.getPointer(); }
0082     EntryIndex getEndIndex() const { return EndIndex; }
0083     EntryKind getEntryKind() const { return Instr.getInt(); }
0084 
0085     bool isClobber() const { return getEntryKind() == Clobber; }
0086     bool isDbgValue() const { return getEntryKind() == DbgValue; }
0087     bool isClosed() const { return EndIndex != NoEntry; }
0088 
0089     void endEntry(EntryIndex EndIndex);
0090 
0091   private:
0092     PointerIntPair<const MachineInstr *, 1, EntryKind> Instr;
0093     EntryIndex EndIndex;
0094   };
0095   using Entries = SmallVector<Entry, 4>;
0096   using InlinedEntity = std::pair<const DINode *, const DILocation *>;
0097   using EntriesMap = MapVector<InlinedEntity, Entries>;
0098 
0099 private:
0100   EntriesMap VarEntries;
0101 
0102 public:
0103   bool startDbgValue(InlinedEntity Var, const MachineInstr &MI,
0104                      EntryIndex &NewIndex);
0105   EntryIndex startClobber(InlinedEntity Var, const MachineInstr &MI);
0106 
0107   Entry &getEntry(InlinedEntity Var, EntryIndex Index) {
0108     auto &Entries = VarEntries[Var];
0109     return Entries[Index];
0110   }
0111 
0112   /// Test whether a vector of entries features any non-empty locations. It
0113   /// could have no entries, or only DBG_VALUE $noreg entries.
0114   bool hasNonEmptyLocation(const Entries &Entries) const;
0115 
0116   /// Drop location ranges which exist entirely outside each variable's scope.
0117   void trimLocationRanges(const MachineFunction &MF, LexicalScopes &LScopes,
0118                           const InstructionOrdering &Ordering);
0119   bool empty() const { return VarEntries.empty(); }
0120   void clear() { VarEntries.clear(); }
0121   EntriesMap::const_iterator begin() const { return VarEntries.begin(); }
0122   EntriesMap::const_iterator end() const { return VarEntries.end(); }
0123 
0124 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
0125   LLVM_DUMP_METHOD void dump(StringRef FuncName) const;
0126 #endif
0127 };
0128 
0129 /// For each inlined instance of a source-level label, keep the corresponding
0130 /// DBG_LABEL instruction. The DBG_LABEL instruction could be used to generate
0131 /// a temporary (assembler) label before it.
0132 class DbgLabelInstrMap {
0133 public:
0134   using InlinedEntity = std::pair<const DINode *, const DILocation *>;
0135   using InstrMap = MapVector<InlinedEntity, const MachineInstr *>;
0136 
0137 private:
0138   InstrMap LabelInstr;
0139 
0140 public:
0141   void  addInstr(InlinedEntity Label, const MachineInstr &MI);
0142 
0143   bool empty() const { return LabelInstr.empty(); }
0144   void clear() { LabelInstr.clear(); }
0145   InstrMap::const_iterator begin() const { return LabelInstr.begin(); }
0146   InstrMap::const_iterator end() const { return LabelInstr.end(); }
0147 };
0148 
0149 void calculateDbgEntityHistory(const MachineFunction *MF,
0150                                const TargetRegisterInfo *TRI,
0151                                DbgValueHistoryMap &DbgValues,
0152                                DbgLabelInstrMap &DbgLabels);
0153 
0154 } // end namespace llvm
0155 
0156 #endif // LLVM_CODEGEN_DBGENTITYHISTORYCALCULATOR_H