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 "DbgRecord" design, each instruction /may/ have a
19 // connection with a DbgMarker, which identifies a position immediately before
20 // the instruction, and each DbgMarker /may/ then have connections to DbgRecords
21 // which record the variable assignment information. To illustrate:
22 //
23 // %foo = add i32 1, %0
24 // ; foo->DebugMarker == nullptr
25 // ;; There are no variable assignments / debug records "in front" of
26 // ;; the instruction for %foo, therefore it has no DebugMarker.
27 // %bar = void call @ext(%foo)
28 // ; bar->DebugMarker = {
29 // ; StoredDbgRecords = {
30 // ; DbgVariableRecord(metadata i32 %foo, ...)
31 // ; }
32 // ; }
33 // ;; There is a debug-info record in front of the %bar instruction,
34 // ;; thus it points at a DbgMarker object. That DbgMarker contains a
35 // ;; DbgVariableRecord in its ilist, storing the equivalent information
36 // ;; to the 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 #include "llvm/Support/Casting.h"
57
58 namespace llvm {
59
60 class Instruction;
61 class BasicBlock;
62 class MDNode;
63 class Module;
64 class DbgVariableIntrinsic;
65 class DbgInfoIntrinsic;
66 class DbgLabelInst;
67 class DIAssignID;
68 class DbgMarker;
69 class DbgVariableRecord;
70 class raw_ostream;
71
72 /// A typed tracking MDNode reference that does not require a definition for its
73 /// parameter type. Necessary to avoid including DebugInfoMetadata.h, which has
74 /// a significant impact on compile times if included in this file.
75 template <typename T> class DbgRecordParamRef {
76 TrackingMDNodeRef Ref;
77
78 public:
79 public:
80 DbgRecordParamRef() = default;
81
82 /// Construct from the templated type.
83 DbgRecordParamRef(const T *Param);
84
85 /// Construct from an \a MDNode.
86 ///
87 /// Note: if \c Param does not have the template type, a verifier check will
88 /// fail, and accessors will crash. However, construction from other nodes
89 /// is supported in order to handle forward references when reading textual
90 /// IR.
91 explicit DbgRecordParamRef(const MDNode *Param);
92
93 /// Get the underlying type.
94 ///
95 /// \pre !*this or \c isa<T>(getAsMDNode()).
96 /// @{
97 T *get() const;
98 operator T *() const { return get(); }
99 T *operator->() const { return get(); }
100 T &operator*() const { return *get(); }
101 /// @}
102
103 /// Check for null.
104 ///
105 /// Check for null in a way that is safe with broken debug info.
106 explicit operator bool() const { return Ref; }
107
108 /// Return \c this as a \a MDNode.
getAsMDNode()109 MDNode *getAsMDNode() const { return Ref; }
110
111 bool operator==(const DbgRecordParamRef &Other) const {
112 return Ref == Other.Ref;
113 }
114 bool operator!=(const DbgRecordParamRef &Other) const {
115 return Ref != Other.Ref;
116 }
117 };
118
119 /// Base class for non-instruction debug metadata records that have positions
120 /// within IR. Features various methods copied across from the Instruction
121 /// class to aid ease-of-use. DbgRecords should always be linked into a
122 /// DbgMarker's StoredDbgRecords list. The marker connects a DbgRecord back to
123 /// its position in the BasicBlock.
124 ///
125 /// We need a discriminator for dyn/isa casts. In order to avoid paying for a
126 /// vtable for "virtual" functions too, subclasses must add a new discriminator
127 /// value (RecordKind) and cases to a few functions in the base class:
128 /// deleteRecord
129 /// clone
130 /// isIdenticalToWhenDefined
131 /// both print methods
132 /// createDebugIntrinsic
133 class DbgRecord : public ilist_node<DbgRecord> {
134 public:
135 /// Marker that this DbgRecord is linked into.
136 DbgMarker *Marker = nullptr;
137 /// Subclass discriminator.
138 enum Kind : uint8_t { ValueKind, LabelKind };
139
140 protected:
141 DebugLoc DbgLoc;
142 Kind RecordKind; ///< Subclass discriminator.
143
144 public:
DbgRecord(Kind RecordKind,DebugLoc DL)145 DbgRecord(Kind RecordKind, DebugLoc DL)
146 : DbgLoc(DL), RecordKind(RecordKind) {}
147
148 /// Methods that dispatch to subclass implementations. These need to be
149 /// manually updated when a new subclass is added.
150 ///@{
151 void deleteRecord();
152 DbgRecord *clone() const;
153 void print(raw_ostream &O, bool IsForDebug = false) const;
154 void print(raw_ostream &O, ModuleSlotTracker &MST, bool IsForDebug) const;
155 bool isIdenticalToWhenDefined(const DbgRecord &R) const;
156 /// Convert this DbgRecord back into an appropriate llvm.dbg.* intrinsic.
157 /// \p InsertBefore Optional position to insert this intrinsic.
158 /// \returns A new llvm.dbg.* intrinsic representiung this DbgRecord.
159 DbgInfoIntrinsic *createDebugIntrinsic(Module *M,
160 Instruction *InsertBefore) const;
161 ///@}
162
163 /// Same as isIdenticalToWhenDefined but checks DebugLoc too.
164 bool isEquivalentTo(const DbgRecord &R) const;
165
getRecordKind()166 Kind getRecordKind() const { return RecordKind; }
167
setMarker(DbgMarker * M)168 void setMarker(DbgMarker *M) { Marker = M; }
169
getMarker()170 DbgMarker *getMarker() { return Marker; }
getMarker()171 const DbgMarker *getMarker() const { return Marker; }
172
173 BasicBlock *getBlock();
174 const BasicBlock *getBlock() const;
175
176 Function *getFunction();
177 const Function *getFunction() const;
178
179 Module *getModule();
180 const Module *getModule() const;
181
182 LLVMContext &getContext();
183 const LLVMContext &getContext() const;
184
185 const Instruction *getInstruction() const;
186 const BasicBlock *getParent() const;
187 BasicBlock *getParent();
188
189 void removeFromParent();
190 void eraseFromParent();
191
getNextNode()192 DbgRecord *getNextNode() { return &*std::next(getIterator()); }
getPrevNode()193 DbgRecord *getPrevNode() { return &*std::prev(getIterator()); }
194 void insertBefore(DbgRecord *InsertBefore);
195 void insertAfter(DbgRecord *InsertAfter);
196 void moveBefore(DbgRecord *MoveBefore);
197 void moveAfter(DbgRecord *MoveAfter);
198
getDebugLoc()199 DebugLoc getDebugLoc() const { return DbgLoc; }
setDebugLoc(DebugLoc Loc)200 void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
201
202 void dump() const;
203
204 using self_iterator = simple_ilist<DbgRecord>::iterator;
205 using const_self_iterator = simple_ilist<DbgRecord>::const_iterator;
206
207 protected:
208 /// Similarly to Value, we avoid paying the cost of a vtable
209 /// by protecting the dtor and having deleteRecord dispatch
210 /// cleanup.
211 /// Use deleteRecord to delete a generic record.
212 ~DbgRecord() = default;
213 };
214
215 inline raw_ostream &operator<<(raw_ostream &OS, const DbgRecord &R) {
216 R.print(OS);
217 return OS;
218 }
219
220 /// Records a position in IR for a source label (DILabel). Corresponds to the
221 /// llvm.dbg.label intrinsic.
222 class DbgLabelRecord : public DbgRecord {
223 DbgRecordParamRef<DILabel> Label;
224
225 /// This constructor intentionally left private, so that it is only called via
226 /// "createUnresolvedDbgLabelRecord", which clearly expresses that it is for
227 /// parsing only.
228 DbgLabelRecord(MDNode *Label, MDNode *DL);
229
230 public:
231 DbgLabelRecord(DILabel *Label, DebugLoc DL);
232
233 /// For use during parsing; creates a DbgLabelRecord from as-of-yet unresolved
234 /// MDNodes. Trying to access the resulting DbgLabelRecord's fields before
235 /// they are resolved, or if they resolve to the wrong type, will result in a
236 /// crash.
237 static DbgLabelRecord *createUnresolvedDbgLabelRecord(MDNode *Label,
238 MDNode *DL);
239
240 DbgLabelRecord *clone() const;
241 void print(raw_ostream &O, bool IsForDebug = false) const;
242 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
243 DbgLabelInst *createDebugIntrinsic(Module *M,
244 Instruction *InsertBefore) const;
245
setLabel(DILabel * NewLabel)246 void setLabel(DILabel *NewLabel) { Label = NewLabel; }
getLabel()247 DILabel *getLabel() const { return Label.get(); }
getRawLabel()248 MDNode *getRawLabel() const { return Label.getAsMDNode(); };
249
250 /// Support type inquiry through isa, cast, and dyn_cast.
classof(const DbgRecord * E)251 static bool classof(const DbgRecord *E) {
252 return E->getRecordKind() == LabelKind;
253 }
254 };
255
256 /// Record of a variable value-assignment, aka a non instruction representation
257 /// of the dbg.value intrinsic.
258 ///
259 /// This class inherits from DebugValueUser to allow LLVM's metadata facilities
260 /// to update our references to metadata beneath our feet.
261 class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
262 friend class DebugValueUser;
263
264 public:
265 enum class LocationType : uint8_t {
266 Declare,
267 Value,
268 Assign,
269
270 End, ///< Marks the end of the concrete types.
271 Any, ///< To indicate all LocationTypes in searches.
272 };
273 /// Classification of the debug-info record that this DbgVariableRecord
274 /// represents. Essentially, "does this correspond to a dbg.value,
275 /// dbg.declare, or dbg.assign?".
276 /// FIXME: We could use spare padding bits from DbgRecord for this.
277 LocationType Type;
278
279 // NB: there is no explicit "Value" field in this class, it's effectively the
280 // DebugValueUser superclass instead. The referred to Value can either be a
281 // ValueAsMetadata or a DIArgList.
282
283 DbgRecordParamRef<DILocalVariable> Variable;
284 DbgRecordParamRef<DIExpression> Expression;
285 DbgRecordParamRef<DIExpression> AddressExpression;
286
287 public:
288 /// Create a new DbgVariableRecord representing the intrinsic \p DVI, for
289 /// example the assignment represented by a dbg.value.
290 DbgVariableRecord(const DbgVariableIntrinsic *DVI);
291 DbgVariableRecord(const DbgVariableRecord &DVR);
292 /// Directly construct a new DbgVariableRecord representing a dbg.value
293 /// intrinsic assigning \p Location to the DV / Expr / DI variable.
294 DbgVariableRecord(Metadata *Location, DILocalVariable *DV, DIExpression *Expr,
295 const DILocation *DI,
296 LocationType Type = LocationType::Value);
297 DbgVariableRecord(Metadata *Value, DILocalVariable *Variable,
298 DIExpression *Expression, DIAssignID *AssignID,
299 Metadata *Address, DIExpression *AddressExpression,
300 const DILocation *DI);
301
302 private:
303 /// Private constructor for creating new instances during parsing only. Only
304 /// called through `createUnresolvedDbgVariableRecord` below, which makes
305 /// clear that this is used for parsing only, and will later return a subclass
306 /// depending on which Type is passed.
307 DbgVariableRecord(LocationType Type, Metadata *Val, MDNode *Variable,
308 MDNode *Expression, MDNode *AssignID, Metadata *Address,
309 MDNode *AddressExpression, MDNode *DI);
310
311 public:
312 /// Used to create DbgVariableRecords during parsing, where some metadata
313 /// references may still be unresolved. Although for some fields a generic
314 /// `Metadata*` argument is accepted for forward type-references, the verifier
315 /// and accessors will reject incorrect types later on. The function is used
316 /// for all types of DbgVariableRecords for simplicity while parsing, but
317 /// asserts if any necessary fields are empty or unused fields are not empty,
318 /// i.e. if the #dbg_assign fields are used for a non-dbg-assign type.
319 static DbgVariableRecord *
320 createUnresolvedDbgVariableRecord(LocationType Type, Metadata *Val,
321 MDNode *Variable, MDNode *Expression,
322 MDNode *AssignID, Metadata *Address,
323 MDNode *AddressExpression, MDNode *DI);
324
325 static DbgVariableRecord *
326 createDVRAssign(Value *Val, DILocalVariable *Variable,
327 DIExpression *Expression, DIAssignID *AssignID,
328 Value *Address, DIExpression *AddressExpression,
329 const DILocation *DI);
330 static DbgVariableRecord *
331 createLinkedDVRAssign(Instruction *LinkedInstr, Value *Val,
332 DILocalVariable *Variable, DIExpression *Expression,
333 Value *Address, DIExpression *AddressExpression,
334 const DILocation *DI);
335
336 static DbgVariableRecord *createDbgVariableRecord(Value *Location,
337 DILocalVariable *DV,
338 DIExpression *Expr,
339 const DILocation *DI);
340 static DbgVariableRecord *
341 createDbgVariableRecord(Value *Location, DILocalVariable *DV,
342 DIExpression *Expr, const DILocation *DI,
343 DbgVariableRecord &InsertBefore);
344 static DbgVariableRecord *createDVRDeclare(Value *Address,
345 DILocalVariable *DV,
346 DIExpression *Expr,
347 const DILocation *DI);
348 static DbgVariableRecord *
349 createDVRDeclare(Value *Address, DILocalVariable *DV, DIExpression *Expr,
350 const DILocation *DI, DbgVariableRecord &InsertBefore);
351
352 /// Iterator for ValueAsMetadata that internally uses direct pointer iteration
353 /// over either a ValueAsMetadata* or a ValueAsMetadata**, dereferencing to the
354 /// ValueAsMetadata .
355 class location_op_iterator
356 : public iterator_facade_base<location_op_iterator,
357 std::bidirectional_iterator_tag, Value *> {
358 PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I;
359
360 public:
location_op_iterator(ValueAsMetadata * SingleIter)361 location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {}
location_op_iterator(ValueAsMetadata ** MultiIter)362 location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {}
363
location_op_iterator(const location_op_iterator & R)364 location_op_iterator(const location_op_iterator &R) : I(R.I) {}
365 location_op_iterator &operator=(const location_op_iterator &R) {
366 I = R.I;
367 return *this;
368 }
369 bool operator==(const location_op_iterator &RHS) const {
370 return I == RHS.I;
371 }
372 const Value *operator*() const {
373 ValueAsMetadata *VAM = I.is<ValueAsMetadata *>()
374 ? I.get<ValueAsMetadata *>()
375 : *I.get<ValueAsMetadata **>();
376 return VAM->getValue();
377 };
378 Value *operator*() {
379 ValueAsMetadata *VAM = I.is<ValueAsMetadata *>()
380 ? I.get<ValueAsMetadata *>()
381 : *I.get<ValueAsMetadata **>();
382 return VAM->getValue();
383 }
384 location_op_iterator &operator++() {
385 if (I.is<ValueAsMetadata *>())
386 I = I.get<ValueAsMetadata *>() + 1;
387 else
388 I = I.get<ValueAsMetadata **>() + 1;
389 return *this;
390 }
391 location_op_iterator &operator--() {
392 if (I.is<ValueAsMetadata *>())
393 I = I.get<ValueAsMetadata *>() - 1;
394 else
395 I = I.get<ValueAsMetadata **>() - 1;
396 return *this;
397 }
398 };
399
isDbgDeclare()400 bool isDbgDeclare() { return Type == LocationType::Declare; }
isDbgValue()401 bool isDbgValue() { return Type == LocationType::Value; }
402
403 /// Get the locations corresponding to the variable referenced by the debug
404 /// info intrinsic. Depending on the intrinsic, this could be the
405 /// variable's value or its address.
406 iterator_range<location_op_iterator> location_ops() const;
407
408 Value *getVariableLocationOp(unsigned OpIdx) const;
409
410 void replaceVariableLocationOp(Value *OldValue, Value *NewValue,
411 bool AllowEmpty = false);
412 void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
413 /// Adding a new location operand will always result in this intrinsic using
414 /// an ArgList, and must always be accompanied by a new expression that uses
415 /// the new operand.
416 void addVariableLocationOps(ArrayRef<Value *> NewValues,
417 DIExpression *NewExpr);
418
419 unsigned getNumVariableLocationOps() const;
420
hasArgList()421 bool hasArgList() const { return isa<DIArgList>(getRawLocation()); }
422 /// Returns true if this DbgVariableRecord has no empty MDNodes in its
423 /// location list.
hasValidLocation()424 bool hasValidLocation() const { return getVariableLocationOp(0) != nullptr; }
425
426 /// Does this describe the address of a local variable. True for dbg.addr
427 /// and dbg.declare, but not dbg.value, which describes its value.
isAddressOfVariable()428 bool isAddressOfVariable() const { return Type == LocationType::Declare; }
getType()429 LocationType getType() const { return Type; }
430
431 void setKillLocation();
432 bool isKillLocation() const;
433
setVariable(DILocalVariable * NewVar)434 void setVariable(DILocalVariable *NewVar) { Variable = NewVar; }
getVariable()435 DILocalVariable *getVariable() const { return Variable.get(); };
getRawVariable()436 MDNode *getRawVariable() const { return Variable.getAsMDNode(); }
437
setExpression(DIExpression * NewExpr)438 void setExpression(DIExpression *NewExpr) { Expression = NewExpr; }
getExpression()439 DIExpression *getExpression() const { return Expression.get(); }
getRawExpression()440 MDNode *getRawExpression() const { return Expression.getAsMDNode(); }
441
442 /// Returns the metadata operand for the first location description. i.e.,
443 /// dbg intrinsic dbg.value,declare operand and dbg.assign 1st location
444 /// operand (the "value componenet"). Note the operand (singular) may be
445 /// a DIArgList which is a list of values.
getRawLocation()446 Metadata *getRawLocation() const { return DebugValues[0]; }
447
448 Value *getValue(unsigned OpIdx = 0) const {
449 return getVariableLocationOp(OpIdx);
450 }
451
452 /// Use of this should generally be avoided; instead,
453 /// replaceVariableLocationOp and addVariableLocationOps should be used where
454 /// possible to avoid creating invalid state.
setRawLocation(Metadata * NewLocation)455 void setRawLocation(Metadata *NewLocation) {
456 assert((isa<ValueAsMetadata>(NewLocation) || isa<DIArgList>(NewLocation) ||
457 isa<MDNode>(NewLocation)) &&
458 "Location for a DbgVariableRecord must be either ValueAsMetadata or "
459 "DIArgList");
460 resetDebugValue(0, NewLocation);
461 }
462
463 /// Get the size (in bits) of the variable, or fragment of the variable that
464 /// is described.
465 std::optional<uint64_t> getFragmentSizeInBits() const;
466
isEquivalentTo(const DbgVariableRecord & Other)467 bool isEquivalentTo(const DbgVariableRecord &Other) const {
468 return DbgLoc == Other.DbgLoc && isIdenticalToWhenDefined(Other);
469 }
470 // Matches the definition of the Instruction version, equivalent to above but
471 // without checking DbgLoc.
isIdenticalToWhenDefined(const DbgVariableRecord & Other)472 bool isIdenticalToWhenDefined(const DbgVariableRecord &Other) const {
473 return std::tie(Type, DebugValues, Variable, Expression,
474 AddressExpression) ==
475 std::tie(Other.Type, Other.DebugValues, Other.Variable,
476 Other.Expression, Other.AddressExpression);
477 }
478
479 /// @name DbgAssign Methods
480 /// @{
isDbgAssign()481 bool isDbgAssign() const { return getType() == LocationType::Assign; }
482
483 Value *getAddress() const;
getRawAddress()484 Metadata *getRawAddress() const {
485 return isDbgAssign() ? DebugValues[1] : DebugValues[0];
486 }
getRawAssignID()487 Metadata *getRawAssignID() const { return DebugValues[2]; }
488 DIAssignID *getAssignID() const;
getAddressExpression()489 DIExpression *getAddressExpression() const { return AddressExpression.get(); }
getRawAddressExpression()490 MDNode *getRawAddressExpression() const {
491 return AddressExpression.getAsMDNode();
492 }
setAddressExpression(DIExpression * NewExpr)493 void setAddressExpression(DIExpression *NewExpr) {
494 AddressExpression = NewExpr;
495 }
496 void setAssignId(DIAssignID *New);
setAddress(Value * V)497 void setAddress(Value *V) { resetDebugValue(1, ValueAsMetadata::get(V)); }
498 /// Kill the address component.
499 void setKillAddress();
500 /// Check whether this kills the address component. This doesn't take into
501 /// account the position of the intrinsic, therefore a returned value of false
502 /// does not guarentee the address is a valid location for the variable at the
503 /// intrinsic's position in IR.
504 bool isKillAddress() const;
505
506 /// @}
507
508 DbgVariableRecord *clone() const;
509 /// Convert this DbgVariableRecord back into a dbg.value intrinsic.
510 /// \p InsertBefore Optional position to insert this intrinsic.
511 /// \returns A new dbg.value intrinsic representiung this DbgVariableRecord.
512 DbgVariableIntrinsic *createDebugIntrinsic(Module *M,
513 Instruction *InsertBefore) const;
514
515 /// Handle changes to the location of the Value(s) that we refer to happening
516 /// "under our feet".
517 void handleChangedLocation(Metadata *NewLocation);
518
519 void print(raw_ostream &O, bool IsForDebug = false) const;
520 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
521
522 /// Support type inquiry through isa, cast, and dyn_cast.
classof(const DbgRecord * E)523 static bool classof(const DbgRecord *E) {
524 return E->getRecordKind() == ValueKind;
525 }
526 };
527
528 /// Filter the DbgRecord range to DbgVariableRecord types only and downcast.
529 static inline auto
filterDbgVars(iterator_range<simple_ilist<DbgRecord>::iterator> R)530 filterDbgVars(iterator_range<simple_ilist<DbgRecord>::iterator> R) {
531 return map_range(
532 make_filter_range(R,
533 [](DbgRecord &E) { return isa<DbgVariableRecord>(E); }),
534 [](DbgRecord &E) { return std::ref(cast<DbgVariableRecord>(E)); });
535 }
536
537 /// Per-instruction record of debug-info. If an Instruction is the position of
538 /// some debugging information, it points at a DbgMarker storing that info. Each
539 /// marker points back at the instruction that owns it. Various utilities are
540 /// provided for manipulating the DbgRecords contained within this marker.
541 ///
542 /// This class has a rough surface area, because it's needed to preserve the
543 /// one arefact that we can't yet eliminate from the intrinsic / dbg.value
544 /// debug-info design: the order of records is significant, and duplicates can
545 /// exist. Thus, if one has a run of debug-info records such as:
546 /// dbg.value(...
547 /// %foo = barinst
548 /// dbg.value(...
549 /// and remove barinst, then the dbg.values must be preserved in the correct
550 /// order. Hence, the use of iterators to select positions to insert things
551 /// into, or the occasional InsertAtHead parameter indicating that new records
552 /// should go at the start of the list.
553 ///
554 /// There are only five or six places in LLVM that truly rely on this ordering,
555 /// which we can improve in the future. Additionally, many improvements in the
556 /// way that debug-info is stored can be achieved in this class, at a future
557 /// date.
558 class DbgMarker {
559 public:
DbgMarker()560 DbgMarker() {}
561 /// Link back to the Instruction that owns this marker. Can be null during
562 /// operations that move a marker from one instruction to another.
563 Instruction *MarkedInstr = nullptr;
564
565 /// List of DbgRecords, the non-instruction equivalent of llvm.dbg.*
566 /// intrinsics. There is a one-to-one relationship between each debug
567 /// intrinsic in a block and each DbgRecord once the representation has been
568 /// converted, and the ordering is meaningful in the same way.
569 simple_ilist<DbgRecord> StoredDbgRecords;
empty()570 bool empty() const { return StoredDbgRecords.empty(); }
571
572 const BasicBlock *getParent() const;
573 BasicBlock *getParent();
574
575 /// Handle the removal of a marker: the position of debug-info has gone away,
576 /// but the stored debug records should not. Drop them onto the next
577 /// instruction, or otherwise work out what to do with them.
578 void removeMarker();
579 void dump() const;
580
581 void removeFromParent();
582 void eraseFromParent();
583
584 /// Implement operator<< on DbgMarker.
585 void print(raw_ostream &O, bool IsForDebug = false) const;
586 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
587
588 /// Produce a range over all the DbgRecords in this Marker.
589 iterator_range<simple_ilist<DbgRecord>::iterator> getDbgRecordRange();
590 iterator_range<simple_ilist<DbgRecord>::const_iterator>
591 getDbgRecordRange() const;
592 /// Transfer any DbgRecords from \p Src into this DbgMarker. If \p
593 /// InsertAtHead is true, place them before existing DbgRecords, otherwise
594 /// afterwards.
595 void absorbDebugValues(DbgMarker &Src, bool InsertAtHead);
596 /// Transfer the DbgRecords in \p Range from \p Src into this DbgMarker. If
597 /// \p InsertAtHead is true, place them before existing DbgRecords, otherwise
598 // afterwards.
599 void absorbDebugValues(iterator_range<DbgRecord::self_iterator> Range,
600 DbgMarker &Src, bool InsertAtHead);
601 /// Insert a DbgRecord into this DbgMarker, at the end of the list. If
602 /// \p InsertAtHead is true, at the start.
603 void insertDbgRecord(DbgRecord *New, bool InsertAtHead);
604 /// Insert a DbgRecord prior to a DbgRecord contained within this marker.
605 void insertDbgRecord(DbgRecord *New, DbgRecord *InsertBefore);
606 /// Insert a DbgRecord after a DbgRecord contained within this marker.
607 void insertDbgRecordAfter(DbgRecord *New, DbgRecord *InsertAfter);
608 /// Clone all DbgMarkers from \p From into this marker. There are numerous
609 /// options to customise the source/destination, due to gnarliness, see class
610 /// comment.
611 /// \p FromHere If non-null, copy from FromHere to the end of From's
612 /// DbgRecords
613 /// \p InsertAtHead Place the cloned DbgRecords at the start of
614 /// StoredDbgRecords
615 /// \returns Range over all the newly cloned DbgRecords
616 iterator_range<simple_ilist<DbgRecord>::iterator>
617 cloneDebugInfoFrom(DbgMarker *From,
618 std::optional<simple_ilist<DbgRecord>::iterator> FromHere,
619 bool InsertAtHead = false);
620 /// Erase all DbgRecords in this DbgMarker.
621 void dropDbgRecords();
622 /// Erase a single DbgRecord from this marker. In an ideal future, we would
623 /// never erase an assignment in this way, but it's the equivalent to
624 /// erasing a debug intrinsic from a block.
625 void dropOneDbgRecord(DbgRecord *DR);
626
627 /// We generally act like all llvm Instructions have a range of DbgRecords
628 /// attached to them, but in reality sometimes we don't allocate the DbgMarker
629 /// to save time and memory, but still have to return ranges of DbgRecords.
630 /// When we need to describe such an unallocated DbgRecord range, use this
631 /// static markers range instead. This will bite us if someone tries to insert
632 /// a DbgRecord in that range, but they should be using the Official (TM) API
633 /// for that.
634 static DbgMarker EmptyDbgMarker;
635 static iterator_range<simple_ilist<DbgRecord>::iterator>
getEmptyDbgRecordRange()636 getEmptyDbgRecordRange() {
637 return make_range(EmptyDbgMarker.StoredDbgRecords.end(),
638 EmptyDbgMarker.StoredDbgRecords.end());
639 }
640 };
641
642 inline raw_ostream &operator<<(raw_ostream &OS, const DbgMarker &Marker) {
643 Marker.print(OS);
644 return OS;
645 }
646
647 /// Inline helper to return a range of DbgRecords attached to a marker. It needs
648 /// to be inlined as it's frequently called, but also come after the declaration
649 /// of DbgMarker. Thus: it's pre-declared by users like Instruction, then an
650 /// inlineable body defined here.
651 inline iterator_range<simple_ilist<DbgRecord>::iterator>
getDbgRecordRange(DbgMarker * DebugMarker)652 getDbgRecordRange(DbgMarker *DebugMarker) {
653 if (!DebugMarker)
654 return DbgMarker::getEmptyDbgRecordRange();
655 return DebugMarker->getDbgRecordRange();
656 }
657
DEFINE_ISA_CONVERSION_FUNCTIONS(DbgRecord,LLVMDbgRecordRef)658 DEFINE_ISA_CONVERSION_FUNCTIONS(DbgRecord, LLVMDbgRecordRef)
659
660 /// Used to temporarily set the debug info format of a function, module, or
661 /// basic block for the duration of this object's lifetime, after which the
662 /// prior state will be restored.
663 template <typename T> class ScopedDbgInfoFormatSetter {
664 T &Obj;
665 bool OldState;
666
667 public:
668 ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
669 : Obj(Obj), OldState(Obj.IsNewDbgInfoFormat) {
670 Obj.setIsNewDbgInfoFormat(NewState);
671 }
672 ~ScopedDbgInfoFormatSetter() { Obj.setIsNewDbgInfoFormat(OldState); }
673 };
674
675 template <typename T>
676 ScopedDbgInfoFormatSetter(T &Obj,
677 bool NewState) -> ScopedDbgInfoFormatSetter<T>;
678
679 } // namespace llvm
680
681 #endif // LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
682