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