1 //===-- llvm/CodeGen/DebugLocEntry.h - Entry in debug_loc list -*- 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 9 #ifndef LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H 10 #define LLVM_LIB_CODEGEN_ASMPRINTER_DEBUGLOCENTRY_H 11 12 #include "DebugLocStream.h" 13 #include "llvm/Config/llvm-config.h" 14 #include "llvm/IR/Constants.h" 15 #include "llvm/IR/DebugInfo.h" 16 #include "llvm/MC/MCSymbol.h" 17 #include "llvm/MC/MachineLocation.h" 18 #include "llvm/Support/Debug.h" 19 20 namespace llvm { 21 class AsmPrinter; 22 23 /// This struct describes target specific location. 24 struct TargetIndexLocation { 25 int Index; 26 int Offset; 27 28 TargetIndexLocation() = default; 29 TargetIndexLocation(unsigned Idx, int64_t Offset) 30 : Index(Idx), Offset(Offset) {} 31 32 bool operator==(const TargetIndexLocation &Other) const { 33 return Index == Other.Index && Offset == Other.Offset; 34 } 35 }; 36 37 /// A single location or constant within a variable location description, with 38 /// either a single entry (with an optional DIExpression) used for a DBG_VALUE, 39 /// or a list of entries used for a DBG_VALUE_LIST. 40 class DbgValueLocEntry { 41 42 /// Type of entry that this represents. 43 enum EntryType { 44 E_Location, 45 E_Integer, 46 E_ConstantFP, 47 E_ConstantInt, 48 E_TargetIndexLocation 49 }; 50 enum EntryType EntryKind; 51 52 /// Either a constant, 53 union { 54 int64_t Int; 55 const ConstantFP *CFP; 56 const ConstantInt *CIP; 57 } Constant; 58 59 union { 60 /// Or a location in the machine frame. 61 MachineLocation Loc; 62 /// Or a location from target specific location. 63 TargetIndexLocation TIL; 64 }; 65 66 public: 67 DbgValueLocEntry(int64_t i) : EntryKind(E_Integer) { Constant.Int = i; } 68 DbgValueLocEntry(const ConstantFP *CFP) : EntryKind(E_ConstantFP) { 69 Constant.CFP = CFP; 70 } 71 DbgValueLocEntry(const ConstantInt *CIP) : EntryKind(E_ConstantInt) { 72 Constant.CIP = CIP; 73 } 74 DbgValueLocEntry(MachineLocation Loc) : EntryKind(E_Location), Loc(Loc) {} 75 DbgValueLocEntry(TargetIndexLocation Loc) 76 : EntryKind(E_TargetIndexLocation), TIL(Loc) {} 77 78 bool isLocation() const { return EntryKind == E_Location; } 79 bool isTargetIndexLocation() const { 80 return EntryKind == E_TargetIndexLocation; 81 } 82 bool isInt() const { return EntryKind == E_Integer; } 83 bool isConstantFP() const { return EntryKind == E_ConstantFP; } 84 bool isConstantInt() const { return EntryKind == E_ConstantInt; } 85 int64_t getInt() const { return Constant.Int; } 86 const ConstantFP *getConstantFP() const { return Constant.CFP; } 87 const ConstantInt *getConstantInt() const { return Constant.CIP; } 88 MachineLocation getLoc() const { return Loc; } 89 TargetIndexLocation getTargetIndexLocation() const { return TIL; } 90 friend bool operator==(const DbgValueLocEntry &, const DbgValueLocEntry &); 91 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 92 LLVM_DUMP_METHOD void dump() const { 93 if (isLocation()) { 94 llvm::dbgs() << "Loc = { reg=" << Loc.getReg() << " "; 95 if (Loc.isIndirect()) 96 llvm::dbgs() << "+0"; 97 llvm::dbgs() << "} "; 98 } else if (isConstantInt()) 99 Constant.CIP->dump(); 100 else if (isConstantFP()) 101 Constant.CFP->dump(); 102 } 103 #endif 104 }; 105 106 /// The location of a single variable, composed of an expression and 0 or more 107 /// DbgValueLocEntries. 108 class DbgValueLoc { 109 /// Any complex address location expression for this DbgValueLoc. 110 const DIExpression *Expression; 111 112 SmallVector<DbgValueLocEntry, 2> ValueLocEntries; 113 114 bool IsVariadic; 115 116 public: 117 DbgValueLoc(const DIExpression *Expr, ArrayRef<DbgValueLocEntry> Locs) 118 : Expression(Expr), ValueLocEntries(Locs.begin(), Locs.end()), 119 IsVariadic(true) { 120 #ifndef NDEBUG 121 // Currently, DBG_VALUE_VAR expressions must use stack_value. 122 assert(Expr && Expr->isValid() && 123 is_contained(Locs, dwarf::DW_OP_stack_value)); 124 for (DbgValueLocEntry &Entry : ValueLocEntries) { 125 assert(!Entry.isConstantFP() && !Entry.isConstantInt() && 126 "Constant values should only be present in non-variadic " 127 "DBG_VALUEs."); 128 } 129 #endif 130 } 131 132 DbgValueLoc(const DIExpression *Expr, ArrayRef<DbgValueLocEntry> Locs, 133 bool IsVariadic) 134 : Expression(Expr), ValueLocEntries(Locs.begin(), Locs.end()), 135 IsVariadic(IsVariadic) { 136 #ifndef NDEBUG 137 assert(cast<DIExpression>(Expr)->isValid() || 138 !any_of(Locs, [](auto LE) { return LE.isLocation(); })); 139 if (!IsVariadic) { 140 assert(ValueLocEntries.size() == 1); 141 } else { 142 // Currently, DBG_VALUE_VAR expressions must use stack_value. 143 assert(Expr && Expr->isValid() && 144 is_contained(Expr->getElements(), dwarf::DW_OP_stack_value)); 145 for (DbgValueLocEntry &Entry : ValueLocEntries) { 146 assert(!Entry.isConstantFP() && !Entry.isConstantInt() && 147 "Constant values should only be present in non-variadic " 148 "DBG_VALUEs."); 149 } 150 } 151 #endif 152 } 153 154 DbgValueLoc(const DIExpression *Expr, DbgValueLocEntry Loc) 155 : Expression(Expr), ValueLocEntries(1, Loc), IsVariadic(false) { 156 assert(((Expr && Expr->isValid()) || !Loc.isLocation()) && 157 "DBG_VALUE with a machine location must have a valid expression."); 158 } 159 160 bool isFragment() const { return getExpression()->isFragment(); } 161 bool isEntryVal() const { return getExpression()->isEntryValue(); } 162 bool isVariadic() const { return IsVariadic; } 163 const DIExpression *getExpression() const { return Expression; } 164 const ArrayRef<DbgValueLocEntry> getLocEntries() const { 165 return ValueLocEntries; 166 } 167 friend bool operator==(const DbgValueLoc &, const DbgValueLoc &); 168 friend bool operator<(const DbgValueLoc &, const DbgValueLoc &); 169 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 170 LLVM_DUMP_METHOD void dump() const { 171 for (DbgValueLocEntry DV : ValueLocEntries) 172 DV.dump(); 173 if (Expression) 174 Expression->dump(); 175 } 176 #endif 177 }; 178 179 /// This struct describes location entries emitted in the .debug_loc 180 /// section. 181 class DebugLocEntry { 182 /// Begin and end symbols for the address range that this location is valid. 183 const MCSymbol *Begin; 184 const MCSymbol *End; 185 186 /// A nonempty list of locations/constants belonging to this entry, 187 /// sorted by offset. 188 SmallVector<DbgValueLoc, 1> Values; 189 190 public: 191 /// Create a location list entry for the range [\p Begin, \p End). 192 /// 193 /// \param Vals One or more values describing (parts of) the variable. 194 DebugLocEntry(const MCSymbol *Begin, const MCSymbol *End, 195 ArrayRef<DbgValueLoc> Vals) 196 : Begin(Begin), End(End) { 197 addValues(Vals); 198 } 199 200 /// Attempt to merge this DebugLocEntry with Next and return 201 /// true if the merge was successful. Entries can be merged if they 202 /// share the same Loc/Constant and if Next immediately follows this 203 /// Entry. 204 bool MergeRanges(const DebugLocEntry &Next) { 205 // If this and Next are describing the same variable, merge them. 206 if ((End == Next.Begin && Values == Next.Values)) { 207 End = Next.End; 208 return true; 209 } 210 return false; 211 } 212 213 const MCSymbol *getBeginSym() const { return Begin; } 214 const MCSymbol *getEndSym() const { return End; } 215 ArrayRef<DbgValueLoc> getValues() const { return Values; } 216 void addValues(ArrayRef<DbgValueLoc> Vals) { 217 Values.append(Vals.begin(), Vals.end()); 218 sortUniqueValues(); 219 assert((Values.size() == 1 || all_of(Values, [](DbgValueLoc V) { 220 return V.isFragment(); 221 })) && "must either have a single value or multiple pieces"); 222 } 223 224 // Sort the pieces by offset. 225 // Remove any duplicate entries by dropping all but the first. 226 void sortUniqueValues() { 227 llvm::sort(Values); 228 Values.erase(std::unique(Values.begin(), Values.end(), 229 [](const DbgValueLoc &A, const DbgValueLoc &B) { 230 return A.getExpression() == B.getExpression(); 231 }), 232 Values.end()); 233 } 234 235 /// Lower this entry into a DWARF expression. 236 void finalize(const AsmPrinter &AP, 237 DebugLocStream::ListBuilder &List, 238 const DIBasicType *BT, 239 DwarfCompileUnit &TheCU); 240 }; 241 242 /// Compare two DbgValueLocEntries for equality. 243 inline bool operator==(const DbgValueLocEntry &A, const DbgValueLocEntry &B) { 244 if (A.EntryKind != B.EntryKind) 245 return false; 246 247 switch (A.EntryKind) { 248 case DbgValueLocEntry::E_Location: 249 return A.Loc == B.Loc; 250 case DbgValueLocEntry::E_TargetIndexLocation: 251 return A.TIL == B.TIL; 252 case DbgValueLocEntry::E_Integer: 253 return A.Constant.Int == B.Constant.Int; 254 case DbgValueLocEntry::E_ConstantFP: 255 return A.Constant.CFP == B.Constant.CFP; 256 case DbgValueLocEntry::E_ConstantInt: 257 return A.Constant.CIP == B.Constant.CIP; 258 } 259 llvm_unreachable("unhandled EntryKind"); 260 } 261 262 /// Compare two DbgValueLocs for equality. 263 inline bool operator==(const DbgValueLoc &A, const DbgValueLoc &B) { 264 return A.ValueLocEntries == B.ValueLocEntries && 265 A.Expression == B.Expression && A.IsVariadic == B.IsVariadic; 266 } 267 268 /// Compare two fragments based on their offset. 269 inline bool operator<(const DbgValueLoc &A, 270 const DbgValueLoc &B) { 271 return A.getExpression()->getFragmentInfo()->OffsetInBits < 272 B.getExpression()->getFragmentInfo()->OffsetInBits; 273 } 274 275 } 276 277 #endif 278