1 //===-- llvm/DebugProgramInstruction.h - Stream of debug info -------*- 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 // Data structures for storing variable assignment information in LLVM. In the 10 // dbg.value design, a dbg.value intrinsic specifies the position in a block 11 // a source variable take on an LLVM Value: 12 // 13 // %foo = add i32 1, %0 14 // dbg.value(metadata i32 %foo, ...) 15 // %bar = void call @ext(%foo); 16 // 17 // and all information is stored in the Value / Metadata hierachy defined 18 // elsewhere in LLVM. In the "DPValue" design, each instruction /may/ have a 19 // connection with a DPMarker, which identifies a position immediately before the 20 // instruction, and each DPMarker /may/ then have connections to DPValues which 21 // record the variable assignment information. To illustrate: 22 // 23 // %foo = add i32 1, %0 24 // ; foo->DbgMarker == nullptr 25 // ;; There are no variable assignments / debug records "in front" of 26 // ;; the instruction for %foo, therefore it has no DbgMarker. 27 // %bar = void call @ext(%foo) 28 // ; bar->DbgMarker = { 29 // ; StoredDPValues = { 30 // ; DPValue(metadata i32 %foo, ...) 31 // ; } 32 // ; } 33 // ;; There is a debug-info record in front of the %bar instruction, 34 // ;; thus it points at a DPMarker object. That DPMarker contains a 35 // ;; DPValue in it's ilist, storing the equivalent information to the 36 // ;; dbg.value above: the Value, DILocalVariable, etc. 37 // 38 // This structure separates the two concerns of the position of the debug-info 39 // in the function, and the Value that it refers to. It also creates a new 40 // "place" in-between the Value / Metadata hierachy where we can customise 41 // storage and allocation techniques to better suite debug-info workloads. 42 // NB: as of the initial prototype, none of that has actually been attempted 43 // yet. 44 // 45 //===----------------------------------------------------------------------===// 46 47 #ifndef LLVM_IR_DEBUGPROGRAMINSTRUCTION_H 48 #define LLVM_IR_DEBUGPROGRAMINSTRUCTION_H 49 50 #include "llvm/ADT/ilist.h" 51 #include "llvm/ADT/ilist_node.h" 52 #include "llvm/ADT/iterator.h" 53 #include "llvm/IR/DebugLoc.h" 54 #include "llvm/IR/Instruction.h" 55 #include "llvm/IR/SymbolTableListTraits.h" 56 57 namespace llvm { 58 59 class Instruction; 60 class BasicBlock; 61 class MDNode; 62 class Module; 63 class DbgVariableIntrinsic; 64 class DIAssignID; 65 class DPMarker; 66 class DPValue; 67 class raw_ostream; 68 69 /// Record of a variable value-assignment, aka a non instruction representation 70 /// of the dbg.value intrinsic. Features various methods copied across from the 71 /// Instruction class to aid ease-of-use. DPValue objects should always be 72 /// linked into a DPMarker's StoredDPValues list. The marker connects a DPValue 73 /// back to it's position in the BasicBlock. 74 /// 75 /// This class inherits from DebugValueUser to allow LLVM's metadata facilities 76 /// to update our references to metadata beneath our feet. 77 class DPValue : public ilist_node<DPValue>, private DebugValueUser { 78 friend class DebugValueUser; 79 80 // NB: there is no explicit "Value" field in this class, it's effectively the 81 // DebugValueUser superclass instead. The referred to Value can either be a 82 // ValueAsMetadata or a DIArgList. 83 84 DILocalVariable *Variable; 85 DIExpression *Expression; 86 DebugLoc DbgLoc; 87 DIExpression *AddressExpression; 88 89 public: 90 void deleteInstr(); 91 92 const BasicBlock *getParent() const; 93 BasicBlock *getParent(); 94 void dump() const; 95 void removeFromParent(); 96 void eraseFromParent(); 97 98 DPValue *getNextNode() { return &*std::next(getIterator()); } 99 DPValue *getPrevNode() { return &*std::prev(getIterator()); } 100 101 using self_iterator = simple_ilist<DPValue>::iterator; 102 using const_self_iterator = simple_ilist<DPValue>::const_iterator; 103 104 enum class LocationType { 105 Declare, 106 Value, 107 Assign, 108 109 End, ///< Marks the end of the concrete types. 110 Any, ///< To indicate all LocationTypes in searches. 111 }; 112 /// Classification of the debug-info record that this DPValue represents. 113 /// Essentially, "is this a dbg.value or dbg.declare?". dbg.declares are not 114 /// currently supported, but it would be trivial to do so. 115 LocationType Type; 116 117 /// Marker that this DPValue is linked into. 118 DPMarker *Marker = nullptr; 119 120 /// Create a new DPValue representing the intrinsic \p DVI, for example the 121 /// assignment represented by a dbg.value. 122 DPValue(const DbgVariableIntrinsic *DVI); 123 DPValue(const DPValue &DPV); 124 /// Directly construct a new DPValue representing a dbg.value intrinsic 125 /// assigning \p Location to the DV / Expr / DI variable. 126 DPValue(Metadata *Location, DILocalVariable *DV, DIExpression *Expr, 127 const DILocation *DI, LocationType Type = LocationType::Value); 128 DPValue(Metadata *Value, DILocalVariable *Variable, DIExpression *Expression, 129 DIAssignID *AssignID, Metadata *Address, 130 DIExpression *AddressExpression, const DILocation *DI); 131 132 static DPValue *createDPVAssign(Value *Val, DILocalVariable *Variable, 133 DIExpression *Expression, 134 DIAssignID *AssignID, Value *Address, 135 DIExpression *AddressExpression, 136 const DILocation *DI); 137 static DPValue *createLinkedDPVAssign(Instruction *LinkedInstr, Value *Val, 138 DILocalVariable *Variable, 139 DIExpression *Expression, 140 Value *Address, 141 DIExpression *AddressExpression, 142 const DILocation *DI); 143 144 static DPValue *createDPValue(Value *Location, DILocalVariable *DV, 145 DIExpression *Expr, const DILocation *DI); 146 static DPValue *createDPValue(Value *Location, DILocalVariable *DV, 147 DIExpression *Expr, const DILocation *DI, 148 DPValue &InsertBefore); 149 static DPValue *createDPVDeclare(Value *Address, DILocalVariable *DV, 150 DIExpression *Expr, const DILocation *DI); 151 static DPValue *createDPVDeclare(Value *Address, DILocalVariable *DV, 152 DIExpression *Expr, const DILocation *DI, 153 DPValue &InsertBefore); 154 155 /// Iterator for ValueAsMetadata that internally uses direct pointer iteration 156 /// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the 157 /// ValueAsMetadata . 158 class location_op_iterator 159 : public iterator_facade_base<location_op_iterator, 160 std::bidirectional_iterator_tag, Value *> { 161 PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I; 162 163 public: 164 location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {} 165 location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {} 166 167 location_op_iterator(const location_op_iterator &R) : I(R.I) {} 168 location_op_iterator &operator=(const location_op_iterator &R) { 169 I = R.I; 170 return *this; 171 } 172 bool operator==(const location_op_iterator &RHS) const { 173 return I == RHS.I; 174 } 175 const Value *operator*() const { 176 ValueAsMetadata *VAM = I.is<ValueAsMetadata *>() 177 ? I.get<ValueAsMetadata *>() 178 : *I.get<ValueAsMetadata **>(); 179 return VAM->getValue(); 180 }; 181 Value *operator*() { 182 ValueAsMetadata *VAM = I.is<ValueAsMetadata *>() 183 ? I.get<ValueAsMetadata *>() 184 : *I.get<ValueAsMetadata **>(); 185 return VAM->getValue(); 186 } 187 location_op_iterator &operator++() { 188 if (I.is<ValueAsMetadata *>()) 189 I = I.get<ValueAsMetadata *>() + 1; 190 else 191 I = I.get<ValueAsMetadata **>() + 1; 192 return *this; 193 } 194 location_op_iterator &operator--() { 195 if (I.is<ValueAsMetadata *>()) 196 I = I.get<ValueAsMetadata *>() - 1; 197 else 198 I = I.get<ValueAsMetadata **>() - 1; 199 return *this; 200 } 201 }; 202 203 bool isDbgDeclare() { return Type == LocationType::Declare; } 204 bool isDbgValue() { return Type == LocationType::Value; } 205 206 /// Get the locations corresponding to the variable referenced by the debug 207 /// info intrinsic. Depending on the intrinsic, this could be the 208 /// variable's value or its address. 209 iterator_range<location_op_iterator> location_ops() const; 210 211 Value *getVariableLocationOp(unsigned OpIdx) const; 212 213 void replaceVariableLocationOp(Value *OldValue, Value *NewValue, 214 bool AllowEmpty = false); 215 void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue); 216 /// Adding a new location operand will always result in this intrinsic using 217 /// an ArgList, and must always be accompanied by a new expression that uses 218 /// the new operand. 219 void addVariableLocationOps(ArrayRef<Value *> NewValues, 220 DIExpression *NewExpr); 221 222 void setVariable(DILocalVariable *NewVar) { Variable = NewVar; } 223 224 void setExpression(DIExpression *NewExpr) { Expression = NewExpr; } 225 226 unsigned getNumVariableLocationOps() const; 227 228 bool hasArgList() const { return isa<DIArgList>(getRawLocation()); } 229 /// Returns true if this DPValue has no empty MDNodes in its location list. 230 bool hasValidLocation() const { return getVariableLocationOp(0) != nullptr; } 231 232 /// Does this describe the address of a local variable. True for dbg.addr 233 /// and dbg.declare, but not dbg.value, which describes its value. 234 bool isAddressOfVariable() const { return Type == LocationType::Declare; } 235 LocationType getType() const { return Type; } 236 237 DebugLoc getDebugLoc() const { return DbgLoc; } 238 void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); } 239 240 void setKillLocation(); 241 bool isKillLocation() const; 242 243 DILocalVariable *getVariable() const { return Variable; } 244 245 DIExpression *getExpression() const { return Expression; } 246 247 /// Returns the metadata operand for the first location description. i.e., 248 /// dbg intrinsic dbg.value,declare operand and dbg.assign 1st location 249 /// operand (the "value componenet"). Note the operand (singular) may be 250 /// a DIArgList which is a list of values. 251 Metadata *getRawLocation() const { return DebugValues[0]; } 252 253 Value *getValue(unsigned OpIdx = 0) const { 254 return getVariableLocationOp(OpIdx); 255 } 256 257 /// Use of this should generally be avoided; instead, 258 /// replaceVariableLocationOp and addVariableLocationOps should be used where 259 /// possible to avoid creating invalid state. 260 void setRawLocation(Metadata *NewLocation) { 261 assert( 262 (isa<ValueAsMetadata>(NewLocation) || isa<DIArgList>(NewLocation) || 263 isa<MDNode>(NewLocation)) && 264 "Location for a DPValue must be either ValueAsMetadata or DIArgList"); 265 resetDebugValue(0, NewLocation); 266 } 267 268 /// Get the size (in bits) of the variable, or fragment of the variable that 269 /// is described. 270 std::optional<uint64_t> getFragmentSizeInBits() const; 271 272 bool isEquivalentTo(const DPValue &Other) { 273 return std::tie(Type, DebugValues, Variable, Expression, DbgLoc) == 274 std::tie(Other.Type, Other.DebugValues, Other.Variable, 275 Other.Expression, Other.DbgLoc); 276 } 277 // Matches the definition of the Instruction version, equivalent to above but 278 // without checking DbgLoc. 279 bool isIdenticalToWhenDefined(const DPValue &Other) { 280 return std::tie(Type, DebugValues, Variable, Expression) == 281 std::tie(Other.Type, Other.DebugValues, Other.Variable, 282 Other.Expression); 283 } 284 285 /// @name DbgAssign Methods 286 /// @{ 287 bool isDbgAssign() const { return getType() == LocationType::Assign; } 288 289 Value *getAddress() const; 290 Metadata *getRawAddress() const { 291 return isDbgAssign() ? DebugValues[1] : DebugValues[0]; 292 } 293 Metadata *getRawAssignID() const { return DebugValues[2]; } 294 DIAssignID *getAssignID() const; 295 DIExpression *getAddressExpression() const { return AddressExpression; } 296 void setAddressExpression(DIExpression *NewExpr) { 297 AddressExpression = NewExpr; 298 } 299 void setAssignId(DIAssignID *New); 300 void setAddress(Value *V) { resetDebugValue(1, ValueAsMetadata::get(V)); } 301 /// Kill the address component. 302 void setKillAddress(); 303 /// Check whether this kills the address component. This doesn't take into 304 /// account the position of the intrinsic, therefore a returned value of false 305 /// does not guarentee the address is a valid location for the variable at the 306 /// intrinsic's position in IR. 307 bool isKillAddress() const; 308 309 /// @} 310 311 DPValue *clone() const; 312 /// Convert this DPValue back into a dbg.value intrinsic. 313 /// \p InsertBefore Optional position to insert this intrinsic. 314 /// \returns A new dbg.value intrinsic representiung this DPValue. 315 DbgVariableIntrinsic *createDebugIntrinsic(Module *M, 316 Instruction *InsertBefore) const; 317 void setMarker(DPMarker *M) { Marker = M; } 318 319 DPMarker *getMarker() { return Marker; } 320 const DPMarker *getMarker() const { return Marker; } 321 322 BasicBlock *getBlock(); 323 const BasicBlock *getBlock() const; 324 325 Function *getFunction(); 326 const Function *getFunction() const; 327 328 Module *getModule(); 329 const Module *getModule() const; 330 331 LLVMContext &getContext(); 332 const LLVMContext &getContext() const; 333 334 /// Insert this DPValue prior to \p InsertBefore. Must not be called if this 335 /// is already contained in a DPMarker. 336 void insertBefore(DPValue *InsertBefore); 337 void insertAfter(DPValue *InsertAfter); 338 void moveBefore(DPValue *MoveBefore); 339 void moveAfter(DPValue *MoveAfter); 340 341 void print(raw_ostream &O, bool IsForDebug = false) const; 342 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const; 343 }; 344 345 /// Per-instruction record of debug-info. If an Instruction is the position of 346 /// some debugging information, it points at a DPMarker storing that info. Each 347 /// marker points back at the instruction that owns it. Various utilities are 348 /// provided for manipulating the DPValues contained within this marker. 349 /// 350 /// This class has a rough surface area, because it's needed to preserve the one 351 /// arefact that we can't yet eliminate from the intrinsic / dbg.value 352 /// debug-info design: the order of DPValues/records is significant, and 353 /// duplicates can exist. Thus, if one has a run of debug-info records such as: 354 /// dbg.value(... 355 /// %foo = barinst 356 /// dbg.value(... 357 /// and remove barinst, then the dbg.values must be preserved in the correct 358 /// order. Hence, the use of iterators to select positions to insert things 359 /// into, or the occasional InsertAtHead parameter indicating that new records 360 /// should go at the start of the list. 361 /// 362 /// There are only five or six places in LLVM that truly rely on this ordering, 363 /// which we can improve in the future. Additionally, many improvements in the 364 /// way that debug-info is stored can be achieved in this class, at a future 365 /// date. 366 class DPMarker { 367 public: 368 DPMarker() {} 369 /// Link back to the Instruction that owns this marker. Can be null during 370 /// operations that move a marker from one instruction to another. 371 Instruction *MarkedInstr = nullptr; 372 373 /// List of DPValues, each recording a single variable assignment, the 374 /// equivalent of a dbg.value intrinsic. There is a one-to-one relationship 375 /// between each dbg.value in a block and each DPValue once the 376 /// representation has been converted, and the ordering of DPValues is 377 /// meaningful in the same was a dbg.values. 378 simple_ilist<DPValue> StoredDPValues; 379 bool empty() const { return StoredDPValues.empty(); } 380 381 const BasicBlock *getParent() const; 382 BasicBlock *getParent(); 383 384 /// Handle the removal of a marker: the position of debug-info has gone away, 385 /// but the stored debug records should not. Drop them onto the next 386 /// instruction, or otherwise work out what to do with them. 387 void removeMarker(); 388 void dump() const; 389 390 void removeFromParent(); 391 void eraseFromParent(); 392 393 /// Implement operator<< on DPMarker. 394 void print(raw_ostream &O, bool IsForDebug = false) const; 395 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const; 396 397 /// Produce a range over all the DPValues in this Marker. 398 iterator_range<simple_ilist<DPValue>::iterator> getDbgValueRange(); 399 iterator_range<simple_ilist<DPValue>::const_iterator> 400 getDbgValueRange() const; 401 /// Transfer any DPValues from \p Src into this DPMarker. If \p InsertAtHead 402 /// is true, place them before existing DPValues, otherwise afterwards. 403 void absorbDebugValues(DPMarker &Src, bool InsertAtHead); 404 /// Transfer the DPValues in \p Range from \p Src into this DPMarker. If 405 /// \p InsertAtHead is true, place them before existing DPValues, otherwise 406 // afterwards. 407 void absorbDebugValues(iterator_range<DPValue::self_iterator> Range, 408 DPMarker &Src, bool InsertAtHead); 409 /// Insert a DPValue into this DPMarker, at the end of the list. If 410 /// \p InsertAtHead is true, at the start. 411 void insertDPValue(DPValue *New, bool InsertAtHead); 412 /// Insert a DPValue prior to a DPValue contained within this marker. 413 void insertDPValue(DPValue *New, DPValue *InsertBefore); 414 /// Insert a DPValue after a DPValue contained within this marker. 415 void insertDPValueAfter(DPValue *New, DPValue *InsertAfter); 416 /// Clone all DPMarkers from \p From into this marker. There are numerous 417 /// options to customise the source/destination, due to gnarliness, see class 418 /// comment. 419 /// \p FromHere If non-null, copy from FromHere to the end of From's DPValues 420 /// \p InsertAtHead Place the cloned DPValues at the start of StoredDPValues 421 /// \returns Range over all the newly cloned DPValues 422 iterator_range<simple_ilist<DPValue>::iterator> 423 cloneDebugInfoFrom(DPMarker *From, 424 std::optional<simple_ilist<DPValue>::iterator> FromHere, 425 bool InsertAtHead = false); 426 /// Erase all DPValues in this DPMarker. 427 void dropDPValues(); 428 /// Erase a single DPValue from this marker. In an ideal future, we would 429 /// never erase an assignment in this way, but it's the equivalent to 430 /// erasing a dbg.value from a block. 431 void dropOneDPValue(DPValue *DPV); 432 433 /// We generally act like all llvm Instructions have a range of DPValues 434 /// attached to them, but in reality sometimes we don't allocate the DPMarker 435 /// to save time and memory, but still have to return ranges of DPValues. When 436 /// we need to describe such an unallocated DPValue range, use this static 437 /// markers range instead. This will bite us if someone tries to insert a 438 /// DPValue in that range, but they should be using the Official (TM) API for 439 /// that. 440 static DPMarker EmptyDPMarker; 441 static iterator_range<simple_ilist<DPValue>::iterator> getEmptyDPValueRange(){ 442 return make_range(EmptyDPMarker.StoredDPValues.end(), EmptyDPMarker.StoredDPValues.end()); 443 } 444 }; 445 446 inline raw_ostream &operator<<(raw_ostream &OS, const DPMarker &Marker) { 447 Marker.print(OS); 448 return OS; 449 } 450 451 inline raw_ostream &operator<<(raw_ostream &OS, const DPValue &Value) { 452 Value.print(OS); 453 return OS; 454 } 455 456 } // namespace llvm 457 458 #endif // LLVM_IR_DEBUGPROGRAMINSTRUCTION_H 459