File indexing completed on 2026-05-10 08:43:59
0001
0002
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013
0014
0015
0016
0017
0018
0019
0020
0021
0022
0023
0024
0025
0026
0027
0028
0029
0030
0031
0032
0033
0034
0035
0036
0037
0038
0039
0040
0041
0042
0043
0044
0045
0046
0047 #ifndef LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
0048 #define LLVM_IR_DEBUGPROGRAMINSTRUCTION_H
0049
0050 #include "llvm/ADT/ilist.h"
0051 #include "llvm/ADT/ilist_node.h"
0052 #include "llvm/ADT/iterator.h"
0053 #include "llvm/IR/DbgVariableFragmentInfo.h"
0054 #include "llvm/IR/DebugLoc.h"
0055 #include "llvm/IR/Instruction.h"
0056 #include "llvm/IR/SymbolTableListTraits.h"
0057 #include "llvm/Support/Casting.h"
0058
0059 namespace llvm {
0060
0061 class Instruction;
0062 class BasicBlock;
0063 class MDNode;
0064 class Module;
0065 class DbgVariableIntrinsic;
0066 class DbgInfoIntrinsic;
0067 class DbgLabelInst;
0068 class DIAssignID;
0069 class DbgMarker;
0070 class DbgVariableRecord;
0071 class raw_ostream;
0072
0073
0074
0075
0076 template <typename T> class DbgRecordParamRef {
0077 TrackingMDNodeRef Ref;
0078
0079 public:
0080 public:
0081 DbgRecordParamRef() = default;
0082
0083
0084 DbgRecordParamRef(const T *Param);
0085
0086
0087
0088
0089
0090
0091
0092 explicit DbgRecordParamRef(const MDNode *Param);
0093
0094
0095
0096
0097
0098 T *get() const;
0099 operator T *() const { return get(); }
0100 T *operator->() const { return get(); }
0101 T &operator*() const { return *get(); }
0102
0103
0104
0105
0106
0107 explicit operator bool() const { return Ref; }
0108
0109
0110 MDNode *getAsMDNode() const { return Ref; }
0111
0112 bool operator==(const DbgRecordParamRef &Other) const {
0113 return Ref == Other.Ref;
0114 }
0115 bool operator!=(const DbgRecordParamRef &Other) const {
0116 return Ref != Other.Ref;
0117 }
0118 };
0119
0120
0121
0122
0123
0124
0125
0126
0127
0128
0129
0130
0131
0132
0133
0134 class DbgRecord : public ilist_node<DbgRecord> {
0135 public:
0136
0137 DbgMarker *Marker = nullptr;
0138
0139 enum Kind : uint8_t { ValueKind, LabelKind };
0140
0141 protected:
0142 DebugLoc DbgLoc;
0143 Kind RecordKind;
0144
0145 public:
0146 DbgRecord(Kind RecordKind, DebugLoc DL)
0147 : DbgLoc(DL), RecordKind(RecordKind) {}
0148
0149
0150
0151
0152 void deleteRecord();
0153 DbgRecord *clone() const;
0154 void print(raw_ostream &O, bool IsForDebug = false) const;
0155 void print(raw_ostream &O, ModuleSlotTracker &MST, bool IsForDebug) const;
0156 bool isIdenticalToWhenDefined(const DbgRecord &R) const;
0157
0158
0159
0160 DbgInfoIntrinsic *createDebugIntrinsic(Module *M,
0161 Instruction *InsertBefore) const;
0162
0163
0164
0165 bool isEquivalentTo(const DbgRecord &R) const;
0166
0167 Kind getRecordKind() const { return RecordKind; }
0168
0169 void setMarker(DbgMarker *M) { Marker = M; }
0170
0171 DbgMarker *getMarker() { return Marker; }
0172 const DbgMarker *getMarker() const { return Marker; }
0173
0174 BasicBlock *getBlock();
0175 const BasicBlock *getBlock() const;
0176
0177 Function *getFunction();
0178 const Function *getFunction() const;
0179
0180 Module *getModule();
0181 const Module *getModule() const;
0182
0183 LLVMContext &getContext();
0184 const LLVMContext &getContext() const;
0185
0186 const Instruction *getInstruction() const;
0187 const BasicBlock *getParent() const;
0188 BasicBlock *getParent();
0189
0190 void removeFromParent();
0191 void eraseFromParent();
0192
0193 DbgRecord *getNextNode() { return &*std::next(getIterator()); }
0194 DbgRecord *getPrevNode() { return &*std::prev(getIterator()); }
0195
0196
0197
0198 void insertBefore(DbgRecord *InsertBefore);
0199 void insertAfter(DbgRecord *InsertAfter);
0200 void moveBefore(DbgRecord *MoveBefore);
0201 void moveAfter(DbgRecord *MoveAfter);
0202
0203 void insertBefore(self_iterator InsertBefore);
0204 void insertAfter(self_iterator InsertAfter);
0205 void moveBefore(self_iterator MoveBefore);
0206 void moveAfter(self_iterator MoveAfter);
0207
0208 DebugLoc getDebugLoc() const { return DbgLoc; }
0209 void setDebugLoc(DebugLoc Loc) { DbgLoc = std::move(Loc); }
0210
0211 void dump() const;
0212
0213 using self_iterator = simple_ilist<DbgRecord>::iterator;
0214 using const_self_iterator = simple_ilist<DbgRecord>::const_iterator;
0215
0216 protected:
0217
0218
0219
0220
0221 ~DbgRecord() = default;
0222 };
0223
0224 inline raw_ostream &operator<<(raw_ostream &OS, const DbgRecord &R) {
0225 R.print(OS);
0226 return OS;
0227 }
0228
0229
0230
0231 class DbgLabelRecord : public DbgRecord {
0232 DbgRecordParamRef<DILabel> Label;
0233
0234
0235
0236
0237 DbgLabelRecord(MDNode *Label, MDNode *DL);
0238
0239 public:
0240 DbgLabelRecord(DILabel *Label, DebugLoc DL);
0241
0242
0243
0244
0245
0246 static DbgLabelRecord *createUnresolvedDbgLabelRecord(MDNode *Label,
0247 MDNode *DL);
0248
0249 DbgLabelRecord *clone() const;
0250 void print(raw_ostream &O, bool IsForDebug = false) const;
0251 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
0252 DbgLabelInst *createDebugIntrinsic(Module *M,
0253 Instruction *InsertBefore) const;
0254
0255 void setLabel(DILabel *NewLabel) { Label = NewLabel; }
0256 DILabel *getLabel() const { return Label.get(); }
0257 MDNode *getRawLabel() const { return Label.getAsMDNode(); };
0258
0259
0260 static bool classof(const DbgRecord *E) {
0261 return E->getRecordKind() == LabelKind;
0262 }
0263 };
0264
0265
0266
0267
0268
0269
0270 class DbgVariableRecord : public DbgRecord, protected DebugValueUser {
0271 friend class DebugValueUser;
0272
0273 public:
0274 enum class LocationType : uint8_t {
0275 Declare,
0276 Value,
0277 Assign,
0278
0279 End,
0280 Any,
0281 };
0282
0283
0284
0285
0286 LocationType Type;
0287
0288
0289
0290
0291
0292 DbgRecordParamRef<DILocalVariable> Variable;
0293 DbgRecordParamRef<DIExpression> Expression;
0294 DbgRecordParamRef<DIExpression> AddressExpression;
0295
0296 public:
0297
0298
0299 DbgVariableRecord(const DbgVariableIntrinsic *DVI);
0300 DbgVariableRecord(const DbgVariableRecord &DVR);
0301
0302
0303 DbgVariableRecord(Metadata *Location, DILocalVariable *DV, DIExpression *Expr,
0304 const DILocation *DI,
0305 LocationType Type = LocationType::Value);
0306 DbgVariableRecord(Metadata *Value, DILocalVariable *Variable,
0307 DIExpression *Expression, DIAssignID *AssignID,
0308 Metadata *Address, DIExpression *AddressExpression,
0309 const DILocation *DI);
0310
0311 private:
0312
0313
0314
0315
0316 DbgVariableRecord(LocationType Type, Metadata *Val, MDNode *Variable,
0317 MDNode *Expression, MDNode *AssignID, Metadata *Address,
0318 MDNode *AddressExpression, MDNode *DI);
0319
0320 public:
0321
0322
0323
0324
0325
0326
0327
0328 static DbgVariableRecord *
0329 createUnresolvedDbgVariableRecord(LocationType Type, Metadata *Val,
0330 MDNode *Variable, MDNode *Expression,
0331 MDNode *AssignID, Metadata *Address,
0332 MDNode *AddressExpression, MDNode *DI);
0333
0334 static DbgVariableRecord *
0335 createDVRAssign(Value *Val, DILocalVariable *Variable,
0336 DIExpression *Expression, DIAssignID *AssignID,
0337 Value *Address, DIExpression *AddressExpression,
0338 const DILocation *DI);
0339 static DbgVariableRecord *
0340 createLinkedDVRAssign(Instruction *LinkedInstr, Value *Val,
0341 DILocalVariable *Variable, DIExpression *Expression,
0342 Value *Address, DIExpression *AddressExpression,
0343 const DILocation *DI);
0344
0345 static DbgVariableRecord *createDbgVariableRecord(Value *Location,
0346 DILocalVariable *DV,
0347 DIExpression *Expr,
0348 const DILocation *DI);
0349 static DbgVariableRecord *
0350 createDbgVariableRecord(Value *Location, DILocalVariable *DV,
0351 DIExpression *Expr, const DILocation *DI,
0352 DbgVariableRecord &InsertBefore);
0353 static DbgVariableRecord *createDVRDeclare(Value *Address,
0354 DILocalVariable *DV,
0355 DIExpression *Expr,
0356 const DILocation *DI);
0357 static DbgVariableRecord *
0358 createDVRDeclare(Value *Address, DILocalVariable *DV, DIExpression *Expr,
0359 const DILocation *DI, DbgVariableRecord &InsertBefore);
0360
0361
0362
0363
0364 class location_op_iterator
0365 : public iterator_facade_base<location_op_iterator,
0366 std::bidirectional_iterator_tag, Value *> {
0367 PointerUnion<ValueAsMetadata *, ValueAsMetadata **> I;
0368
0369 public:
0370 location_op_iterator(ValueAsMetadata *SingleIter) : I(SingleIter) {}
0371 location_op_iterator(ValueAsMetadata **MultiIter) : I(MultiIter) {}
0372
0373 location_op_iterator(const location_op_iterator &R) : I(R.I) {}
0374 location_op_iterator &operator=(const location_op_iterator &R) {
0375 I = R.I;
0376 return *this;
0377 }
0378 bool operator==(const location_op_iterator &RHS) const {
0379 return I == RHS.I;
0380 }
0381 const Value *operator*() const {
0382 ValueAsMetadata *VAM = isa<ValueAsMetadata *>(I)
0383 ? cast<ValueAsMetadata *>(I)
0384 : *cast<ValueAsMetadata **>(I);
0385 return VAM->getValue();
0386 };
0387 Value *operator*() {
0388 ValueAsMetadata *VAM = isa<ValueAsMetadata *>(I)
0389 ? cast<ValueAsMetadata *>(I)
0390 : *cast<ValueAsMetadata **>(I);
0391 return VAM->getValue();
0392 }
0393 location_op_iterator &operator++() {
0394 if (auto *VAM = dyn_cast<ValueAsMetadata *>(I))
0395 I = VAM + 1;
0396 else
0397 I = cast<ValueAsMetadata **>(I) + 1;
0398 return *this;
0399 }
0400 location_op_iterator &operator--() {
0401 if (auto *VAM = dyn_cast<ValueAsMetadata *>(I))
0402 I = VAM - 1;
0403 else
0404 I = cast<ValueAsMetadata **>(I) - 1;
0405 return *this;
0406 }
0407 };
0408
0409 bool isDbgDeclare() const { return Type == LocationType::Declare; }
0410 bool isDbgValue() const { return Type == LocationType::Value; }
0411
0412
0413
0414
0415 iterator_range<location_op_iterator> location_ops() const;
0416
0417 Value *getVariableLocationOp(unsigned OpIdx) const;
0418
0419 void replaceVariableLocationOp(Value *OldValue, Value *NewValue,
0420 bool AllowEmpty = false);
0421 void replaceVariableLocationOp(unsigned OpIdx, Value *NewValue);
0422
0423
0424
0425 void addVariableLocationOps(ArrayRef<Value *> NewValues,
0426 DIExpression *NewExpr);
0427
0428 unsigned getNumVariableLocationOps() const;
0429
0430 bool hasArgList() const { return isa<DIArgList>(getRawLocation()); }
0431
0432
0433 bool hasValidLocation() const { return getVariableLocationOp(0) != nullptr; }
0434
0435
0436
0437 bool isAddressOfVariable() const { return Type == LocationType::Declare; }
0438
0439
0440
0441 bool isValueOfVariable() const { return Type == LocationType::Value; }
0442
0443 LocationType getType() const { return Type; }
0444
0445 void setKillLocation();
0446 bool isKillLocation() const;
0447
0448 void setVariable(DILocalVariable *NewVar) { Variable = NewVar; }
0449 DILocalVariable *getVariable() const { return Variable.get(); };
0450 MDNode *getRawVariable() const { return Variable.getAsMDNode(); }
0451
0452 void setExpression(DIExpression *NewExpr) { Expression = NewExpr; }
0453 DIExpression *getExpression() const { return Expression.get(); }
0454 MDNode *getRawExpression() const { return Expression.getAsMDNode(); }
0455
0456
0457
0458
0459
0460 Metadata *getRawLocation() const { return DebugValues[0]; }
0461
0462 Value *getValue(unsigned OpIdx = 0) const {
0463 return getVariableLocationOp(OpIdx);
0464 }
0465
0466
0467
0468
0469 void setRawLocation(Metadata *NewLocation) {
0470 assert((isa<ValueAsMetadata>(NewLocation) || isa<DIArgList>(NewLocation) ||
0471 isa<MDNode>(NewLocation)) &&
0472 "Location for a DbgVariableRecord must be either ValueAsMetadata or "
0473 "DIArgList");
0474 resetDebugValue(0, NewLocation);
0475 }
0476
0477 std::optional<DbgVariableFragmentInfo> getFragment() const;
0478
0479
0480
0481 DbgVariableFragmentInfo getFragmentOrEntireVariable() const {
0482 if (auto Frag = getFragment())
0483 return *Frag;
0484 if (auto Sz = getFragmentSizeInBits())
0485 return {*Sz, 0};
0486 return {0, 0};
0487 }
0488
0489
0490 std::optional<uint64_t> getFragmentSizeInBits() const;
0491
0492 bool isEquivalentTo(const DbgVariableRecord &Other) const {
0493 return DbgLoc == Other.DbgLoc && isIdenticalToWhenDefined(Other);
0494 }
0495
0496
0497 bool isIdenticalToWhenDefined(const DbgVariableRecord &Other) const {
0498 return std::tie(Type, DebugValues, Variable, Expression,
0499 AddressExpression) ==
0500 std::tie(Other.Type, Other.DebugValues, Other.Variable,
0501 Other.Expression, Other.AddressExpression);
0502 }
0503
0504
0505
0506 bool isDbgAssign() const { return getType() == LocationType::Assign; }
0507
0508 Value *getAddress() const;
0509 Metadata *getRawAddress() const {
0510 return isDbgAssign() ? DebugValues[1] : DebugValues[0];
0511 }
0512 Metadata *getRawAssignID() const { return DebugValues[2]; }
0513 DIAssignID *getAssignID() const;
0514 DIExpression *getAddressExpression() const { return AddressExpression.get(); }
0515 MDNode *getRawAddressExpression() const {
0516 return AddressExpression.getAsMDNode();
0517 }
0518 void setAddressExpression(DIExpression *NewExpr) {
0519 AddressExpression = NewExpr;
0520 }
0521 void setAssignId(DIAssignID *New);
0522 void setAddress(Value *V) { resetDebugValue(1, ValueAsMetadata::get(V)); }
0523
0524 void setKillAddress();
0525
0526
0527
0528
0529 bool isKillAddress() const;
0530
0531
0532
0533 DbgVariableRecord *clone() const;
0534
0535
0536
0537 DbgVariableIntrinsic *createDebugIntrinsic(Module *M,
0538 Instruction *InsertBefore) const;
0539
0540
0541
0542 void handleChangedLocation(Metadata *NewLocation);
0543
0544 void print(raw_ostream &O, bool IsForDebug = false) const;
0545 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
0546
0547
0548 static bool classof(const DbgRecord *E) {
0549 return E->getRecordKind() == ValueKind;
0550 }
0551 };
0552
0553
0554 static inline auto
0555 filterDbgVars(iterator_range<simple_ilist<DbgRecord>::iterator> R) {
0556 return map_range(
0557 make_filter_range(R,
0558 [](DbgRecord &E) { return isa<DbgVariableRecord>(E); }),
0559 [](DbgRecord &E) { return std::ref(cast<DbgVariableRecord>(E)); });
0560 }
0561
0562
0563
0564
0565
0566
0567
0568
0569
0570
0571
0572
0573
0574
0575
0576
0577
0578
0579
0580
0581
0582
0583 class DbgMarker {
0584 public:
0585 DbgMarker() {}
0586
0587
0588 Instruction *MarkedInstr = nullptr;
0589
0590
0591
0592
0593
0594 simple_ilist<DbgRecord> StoredDbgRecords;
0595 bool empty() const { return StoredDbgRecords.empty(); }
0596
0597 const BasicBlock *getParent() const;
0598 BasicBlock *getParent();
0599
0600
0601
0602
0603 void removeMarker();
0604 void dump() const;
0605
0606 void removeFromParent();
0607 void eraseFromParent();
0608
0609
0610 void print(raw_ostream &O, bool IsForDebug = false) const;
0611 void print(raw_ostream &ROS, ModuleSlotTracker &MST, bool IsForDebug) const;
0612
0613
0614 iterator_range<simple_ilist<DbgRecord>::iterator> getDbgRecordRange();
0615 iterator_range<simple_ilist<DbgRecord>::const_iterator>
0616 getDbgRecordRange() const;
0617
0618
0619
0620 void absorbDebugValues(DbgMarker &Src, bool InsertAtHead);
0621
0622
0623
0624 void absorbDebugValues(iterator_range<DbgRecord::self_iterator> Range,
0625 DbgMarker &Src, bool InsertAtHead);
0626
0627
0628 void insertDbgRecord(DbgRecord *New, bool InsertAtHead);
0629
0630 void insertDbgRecord(DbgRecord *New, DbgRecord *InsertBefore);
0631
0632 void insertDbgRecordAfter(DbgRecord *New, DbgRecord *InsertAfter);
0633
0634
0635
0636
0637
0638
0639
0640
0641 iterator_range<simple_ilist<DbgRecord>::iterator>
0642 cloneDebugInfoFrom(DbgMarker *From,
0643 std::optional<simple_ilist<DbgRecord>::iterator> FromHere,
0644 bool InsertAtHead = false);
0645
0646 void dropDbgRecords();
0647
0648
0649
0650 void dropOneDbgRecord(DbgRecord *DR);
0651
0652
0653
0654
0655
0656
0657
0658
0659 static DbgMarker EmptyDbgMarker;
0660 static iterator_range<simple_ilist<DbgRecord>::iterator>
0661 getEmptyDbgRecordRange() {
0662 return make_range(EmptyDbgMarker.StoredDbgRecords.end(),
0663 EmptyDbgMarker.StoredDbgRecords.end());
0664 }
0665 };
0666
0667 inline raw_ostream &operator<<(raw_ostream &OS, const DbgMarker &Marker) {
0668 Marker.print(OS);
0669 return OS;
0670 }
0671
0672
0673
0674
0675
0676 inline iterator_range<simple_ilist<DbgRecord>::iterator>
0677 getDbgRecordRange(DbgMarker *DebugMarker) {
0678 if (!DebugMarker)
0679 return DbgMarker::getEmptyDbgRecordRange();
0680 return DebugMarker->getDbgRecordRange();
0681 }
0682
0683 DEFINE_ISA_CONVERSION_FUNCTIONS(DbgRecord, LLVMDbgRecordRef)
0684
0685
0686
0687
0688 template <typename T> class ScopedDbgInfoFormatSetter {
0689 T &Obj;
0690 bool OldState;
0691
0692 public:
0693 ScopedDbgInfoFormatSetter(T &Obj, bool NewState)
0694 : Obj(Obj), OldState(Obj.IsNewDbgInfoFormat) {
0695 Obj.setIsNewDbgInfoFormat(NewState);
0696 }
0697 ~ScopedDbgInfoFormatSetter() { Obj.setIsNewDbgInfoFormat(OldState); }
0698 };
0699
0700 template <typename T>
0701 ScopedDbgInfoFormatSetter(T &Obj,
0702 bool NewState) -> ScopedDbgInfoFormatSetter<T>;
0703
0704 }
0705
0706 #endif