DbgEntityHistoryCalculator.h 5.7 KB

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