DbgEntityHistoryCalculator.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157
  1. //===- llvm/CodeGen/DbgEntityHistoryCalculator.h ----------------*- 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. #ifndef LLVM_CODEGEN_DBGENTITYHISTORYCALCULATOR_H
  9. #define LLVM_CODEGEN_DBGENTITYHISTORYCALCULATOR_H
  10. #include "llvm/ADT/MapVector.h"
  11. #include "llvm/ADT/PointerIntPair.h"
  12. #include "llvm/ADT/SmallVector.h"
  13. #include "llvm/CodeGen/LexicalScopes.h"
  14. #include <utility>
  15. namespace llvm {
  16. class DILocalVariable;
  17. class DILocation;
  18. class DINode;
  19. class MachineFunction;
  20. class MachineInstr;
  21. class TargetRegisterInfo;
  22. /// Record instruction ordering so we can query their relative positions within
  23. /// a function. Meta instructions are given the same ordinal as the preceding
  24. /// non-meta instruction. Class state is invalid if MF is modified after
  25. /// calling initialize.
  26. class InstructionOrdering {
  27. public:
  28. void initialize(const MachineFunction &MF);
  29. void clear() { InstNumberMap.clear(); }
  30. /// Check if instruction \p A comes before \p B, where \p A and \p B both
  31. /// belong to the MachineFunction passed to initialize().
  32. bool isBefore(const MachineInstr *A, const MachineInstr *B) const;
  33. private:
  34. /// Each instruction is assigned an order number.
  35. DenseMap<const MachineInstr *, unsigned> InstNumberMap;
  36. };
  37. /// For each user variable, keep a list of instruction ranges where this
  38. /// variable is accessible. The variables are listed in order of appearance.
  39. class DbgValueHistoryMap {
  40. public:
  41. /// Index in the entry vector.
  42. typedef size_t EntryIndex;
  43. /// Special value to indicate that an entry is valid until the end of the
  44. /// function.
  45. static const EntryIndex NoEntry = std::numeric_limits<EntryIndex>::max();
  46. /// Specifies a change in a variable's debug value history.
  47. ///
  48. /// There exist two types of entries:
  49. ///
  50. /// * Debug value entry:
  51. ///
  52. /// A new debug value becomes live. If the entry's \p EndIndex is \p NoEntry,
  53. /// the value is valid until the end of the function. For other values, the
  54. /// index points to the entry in the entry vector that ends this debug
  55. /// value. The ending entry can either be an overlapping debug value, or
  56. /// an instruction that clobbers the value.
  57. ///
  58. /// * Clobbering entry:
  59. ///
  60. /// This entry's instruction clobbers one or more preceding
  61. /// register-described debug values that have their end index
  62. /// set to this entry's position in the entry vector.
  63. class Entry {
  64. friend DbgValueHistoryMap;
  65. public:
  66. enum EntryKind { DbgValue, Clobber };
  67. Entry(const MachineInstr *Instr, EntryKind Kind)
  68. : Instr(Instr, Kind), EndIndex(NoEntry) {}
  69. const MachineInstr *getInstr() const { return Instr.getPointer(); }
  70. EntryIndex getEndIndex() const { return EndIndex; }
  71. EntryKind getEntryKind() const { return Instr.getInt(); }
  72. bool isClobber() const { return getEntryKind() == Clobber; }
  73. bool isDbgValue() const { return getEntryKind() == DbgValue; }
  74. bool isClosed() const { return EndIndex != NoEntry; }
  75. void endEntry(EntryIndex EndIndex);
  76. private:
  77. PointerIntPair<const MachineInstr *, 1, EntryKind> Instr;
  78. EntryIndex EndIndex;
  79. };
  80. using Entries = SmallVector<Entry, 4>;
  81. using InlinedEntity = std::pair<const DINode *, const DILocation *>;
  82. using EntriesMap = MapVector<InlinedEntity, Entries>;
  83. private:
  84. EntriesMap VarEntries;
  85. public:
  86. bool startDbgValue(InlinedEntity Var, const MachineInstr &MI,
  87. EntryIndex &NewIndex);
  88. EntryIndex startClobber(InlinedEntity Var, const MachineInstr &MI);
  89. Entry &getEntry(InlinedEntity Var, EntryIndex Index) {
  90. auto &Entries = VarEntries[Var];
  91. return Entries[Index];
  92. }
  93. /// Test whether a vector of entries features any non-empty locations. It
  94. /// could have no entries, or only DBG_VALUE $noreg entries.
  95. bool hasNonEmptyLocation(const Entries &Entries) const;
  96. /// Drop location ranges which exist entirely outside each variable's scope.
  97. void trimLocationRanges(const MachineFunction &MF, LexicalScopes &LScopes,
  98. const InstructionOrdering &Ordering);
  99. bool empty() const { return VarEntries.empty(); }
  100. void clear() { VarEntries.clear(); }
  101. EntriesMap::const_iterator begin() const { return VarEntries.begin(); }
  102. EntriesMap::const_iterator end() const { return VarEntries.end(); }
  103. #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
  104. LLVM_DUMP_METHOD void dump() const;
  105. #endif
  106. };
  107. /// For each inlined instance of a source-level label, keep the corresponding
  108. /// DBG_LABEL instruction. The DBG_LABEL instruction could be used to generate
  109. /// a temporary (assembler) label before it.
  110. class DbgLabelInstrMap {
  111. public:
  112. using InlinedEntity = std::pair<const DINode *, const DILocation *>;
  113. using InstrMap = MapVector<InlinedEntity, const MachineInstr *>;
  114. private:
  115. InstrMap LabelInstr;
  116. public:
  117. void addInstr(InlinedEntity Label, const MachineInstr &MI);
  118. bool empty() const { return LabelInstr.empty(); }
  119. void clear() { LabelInstr.clear(); }
  120. InstrMap::const_iterator begin() const { return LabelInstr.begin(); }
  121. InstrMap::const_iterator end() const { return LabelInstr.end(); }
  122. };
  123. void calculateDbgEntityHistory(const MachineFunction *MF,
  124. const TargetRegisterInfo *TRI,
  125. DbgValueHistoryMap &DbgValues,
  126. DbgLabelInstrMap &DbgLabels);
  127. } // end namespace llvm
  128. #endif // LLVM_CODEGEN_DBGENTITYHISTORYCALCULATOR_H