DebugLocEntry.h 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180
  1. //===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- C++ -*--===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
  10. #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H
  11. #include "llvm/ADT/SmallString.h"
  12. #include "llvm/IR/Constants.h"
  13. #include "llvm/IR/DebugInfo.h"
  14. #include "llvm/MC/MCSymbol.h"
  15. #include "llvm/MC/MachineLocation.h"
  16. namespace llvm {
  17. class AsmPrinter;
  18. class DebugLocStream;
  19. /// \brief This struct describes location entries emitted in the .debug_loc
  20. /// section.
  21. class DebugLocEntry {
  22. /// Begin and end symbols for the address range that this location is valid.
  23. const MCSymbol *Begin;
  24. const MCSymbol *End;
  25. public:
  26. /// \brief A single location or constant.
  27. struct Value {
  28. Value(const DIExpression *Expr, int64_t i)
  29. : Expression(Expr), EntryKind(E_Integer) {
  30. Constant.Int = i;
  31. }
  32. Value(const DIExpression *Expr, const ConstantFP *CFP)
  33. : Expression(Expr), EntryKind(E_ConstantFP) {
  34. Constant.CFP = CFP;
  35. }
  36. Value(const DIExpression *Expr, const ConstantInt *CIP)
  37. : Expression(Expr), EntryKind(E_ConstantInt) {
  38. Constant.CIP = CIP;
  39. }
  40. Value(const DIExpression *Expr, MachineLocation Loc)
  41. : Expression(Expr), EntryKind(E_Location), Loc(Loc) {
  42. assert(cast<DIExpression>(Expr)->isValid());
  43. }
  44. /// Any complex address location expression for this Value.
  45. const DIExpression *Expression;
  46. /// Type of entry that this represents.
  47. enum EntryType { E_Location, E_Integer, E_ConstantFP, E_ConstantInt };
  48. enum EntryType EntryKind;
  49. /// Either a constant,
  50. union {
  51. int64_t Int;
  52. const ConstantFP *CFP;
  53. const ConstantInt *CIP;
  54. } Constant;
  55. // Or a location in the machine frame.
  56. MachineLocation Loc;
  57. bool isLocation() const { return EntryKind == E_Location; }
  58. bool isInt() const { return EntryKind == E_Integer; }
  59. bool isConstantFP() const { return EntryKind == E_ConstantFP; }
  60. bool isConstantInt() const { return EntryKind == E_ConstantInt; }
  61. int64_t getInt() const { return Constant.Int; }
  62. const ConstantFP *getConstantFP() const { return Constant.CFP; }
  63. const ConstantInt *getConstantInt() const { return Constant.CIP; }
  64. MachineLocation getLoc() const { return Loc; }
  65. bool isBitPiece() const { return getExpression()->isBitPiece(); }
  66. const DIExpression *getExpression() const { return Expression; }
  67. friend bool operator==(const Value &, const Value &);
  68. friend bool operator<(const Value &, const Value &);
  69. };
  70. private:
  71. /// A nonempty list of locations/constants belonging to this entry,
  72. /// sorted by offset.
  73. SmallVector<Value, 1> Values;
  74. public:
  75. DebugLocEntry(const MCSymbol *B, const MCSymbol *E, Value Val)
  76. : Begin(B), End(E) {
  77. Values.push_back(std::move(Val));
  78. }
  79. /// \brief If this and Next are describing different pieces of the same
  80. /// variable, merge them by appending Next's values to the current
  81. /// list of values.
  82. /// Return true if the merge was successful.
  83. bool MergeValues(const DebugLocEntry &Next) {
  84. if (Begin == Next.Begin) {
  85. auto *Expr = cast_or_null<DIExpression>(Values[0].Expression);
  86. auto *NextExpr = cast_or_null<DIExpression>(Next.Values[0].Expression);
  87. if (Expr->isBitPiece() && NextExpr->isBitPiece()) {
  88. addValues(Next.Values);
  89. End = Next.End;
  90. return true;
  91. }
  92. }
  93. return false;
  94. }
  95. /// \brief Attempt to merge this DebugLocEntry with Next and return
  96. /// true if the merge was successful. Entries can be merged if they
  97. /// share the same Loc/Constant and if Next immediately follows this
  98. /// Entry.
  99. bool MergeRanges(const DebugLocEntry &Next) {
  100. // If this and Next are describing the same variable, merge them.
  101. if ((End == Next.Begin && Values == Next.Values)) {
  102. End = Next.End;
  103. return true;
  104. }
  105. return false;
  106. }
  107. const MCSymbol *getBeginSym() const { return Begin; }
  108. const MCSymbol *getEndSym() const { return End; }
  109. ArrayRef<Value> getValues() const { return Values; }
  110. void addValues(ArrayRef<DebugLocEntry::Value> Vals) {
  111. Values.append(Vals.begin(), Vals.end());
  112. sortUniqueValues();
  113. assert(std::all_of(Values.begin(), Values.end(), [](DebugLocEntry::Value V){
  114. return V.isBitPiece();
  115. }) && "value must be a piece");
  116. }
  117. // \brief Sort the pieces by offset.
  118. // Remove any duplicate entries by dropping all but the first.
  119. void sortUniqueValues() {
  120. std::sort(Values.begin(), Values.end());
  121. Values.erase(
  122. std::unique(
  123. Values.begin(), Values.end(), [](const Value &A, const Value &B) {
  124. return A.getExpression() == B.getExpression();
  125. }),
  126. Values.end());
  127. }
  128. /// \brief Lower this entry into a DWARF expression.
  129. void finalize(const AsmPrinter &AP, DebugLocStream::ListBuilder &List,
  130. const DIBasicType *BT);
  131. };
  132. /// \brief Compare two Values for equality.
  133. inline bool operator==(const DebugLocEntry::Value &A,
  134. const DebugLocEntry::Value &B) {
  135. if (A.EntryKind != B.EntryKind)
  136. return false;
  137. if (A.Expression != B.Expression)
  138. return false;
  139. switch (A.EntryKind) {
  140. case DebugLocEntry::Value::E_Location:
  141. return A.Loc == B.Loc;
  142. case DebugLocEntry::Value::E_Integer:
  143. return A.Constant.Int == B.Constant.Int;
  144. case DebugLocEntry::Value::E_ConstantFP:
  145. return A.Constant.CFP == B.Constant.CFP;
  146. case DebugLocEntry::Value::E_ConstantInt:
  147. return A.Constant.CIP == B.Constant.CIP;
  148. }
  149. llvm_unreachable("unhandled EntryKind");
  150. }
  151. /// \brief Compare two pieces based on their offset.
  152. inline bool operator<(const DebugLocEntry::Value &A,
  153. const DebugLocEntry::Value &B) {
  154. return A.getExpression()->getBitPieceOffset() <
  155. B.getExpression()->getBitPieceOffset();
  156. }
  157. }
  158. #endif