1 //=====-- DebugProgramInstruction.cpp - Implement DbgRecords/DbgMarkers --====//
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
7 //===----------------------------------------------------------------------===//
9 #include "llvm/IR/DebugInfoMetadata.h"
10 #include "llvm/IR/DebugProgramInstruction.h"
11 #include "llvm/IR/DIBuilder.h"
12 #include "llvm/IR/IntrinsicInst.h"
17 DbgRecordParamRef
<T
>::DbgRecordParamRef(const T
*Param
)
18 : Ref(const_cast<T
*>(Param
)) {}
20 DbgRecordParamRef
<T
>::DbgRecordParamRef(const MDNode
*Param
)
21 : Ref(const_cast<MDNode
*>(Param
)) {}
23 template <typename T
> T
*DbgRecordParamRef
<T
>::get() const {
27 template class DbgRecordParamRef
<DIExpression
>;
28 template class DbgRecordParamRef
<DILabel
>;
29 template class DbgRecordParamRef
<DILocalVariable
>;
31 DbgVariableRecord::DbgVariableRecord(const DbgVariableIntrinsic
*DVI
)
32 : DbgRecord(ValueKind
, DVI
->getDebugLoc()),
33 DebugValueUser({DVI
->getRawLocation(), nullptr, nullptr}),
34 Variable(DVI
->getVariable()), Expression(DVI
->getExpression()),
36 switch (DVI
->getIntrinsicID()) {
37 case Intrinsic::dbg_value
:
38 Type
= LocationType::Value
;
40 case Intrinsic::dbg_declare
:
41 Type
= LocationType::Declare
;
43 case Intrinsic::dbg_assign
: {
44 Type
= LocationType::Assign
;
45 const DbgAssignIntrinsic
*Assign
=
46 static_cast<const DbgAssignIntrinsic
*>(DVI
);
47 resetDebugValue(1, Assign
->getRawAddress());
48 AddressExpression
= Assign
->getAddressExpression();
49 setAssignId(Assign
->getAssignID());
54 "Trying to create a DbgVariableRecord with an invalid intrinsic type!");
58 DbgVariableRecord::DbgVariableRecord(const DbgVariableRecord
&DVR
)
59 : DbgRecord(ValueKind
, DVR
.getDebugLoc()), DebugValueUser(DVR
.DebugValues
),
60 Type(DVR
.getType()), Variable(DVR
.getVariable()),
61 Expression(DVR
.getExpression()),
62 AddressExpression(DVR
.AddressExpression
) {}
64 DbgVariableRecord::DbgVariableRecord(Metadata
*Location
, DILocalVariable
*DV
,
65 DIExpression
*Expr
, const DILocation
*DI
,
67 : DbgRecord(ValueKind
, DI
), DebugValueUser({Location
, nullptr, nullptr}),
68 Type(Type
), Variable(DV
), Expression(Expr
) {}
70 DbgVariableRecord::DbgVariableRecord(Metadata
*Value
, DILocalVariable
*Variable
,
71 DIExpression
*Expression
,
72 DIAssignID
*AssignID
, Metadata
*Address
,
73 DIExpression
*AddressExpression
,
75 : DbgRecord(ValueKind
, DI
), DebugValueUser({Value
, Address
, AssignID
}),
76 Type(LocationType::Assign
), Variable(Variable
), Expression(Expression
),
77 AddressExpression(AddressExpression
) {}
79 void DbgRecord::deleteRecord() {
82 delete cast
<DbgVariableRecord
>(this);
85 delete cast
<DbgLabelRecord
>(this);
88 llvm_unreachable("unsupported DbgRecord kind");
91 void DbgRecord::print(raw_ostream
&O
, bool IsForDebug
) const {
94 cast
<DbgVariableRecord
>(this)->print(O
, IsForDebug
);
97 cast
<DbgLabelRecord
>(this)->print(O
, IsForDebug
);
100 llvm_unreachable("unsupported DbgRecord kind");
103 void DbgRecord::print(raw_ostream
&O
, ModuleSlotTracker
&MST
,
104 bool IsForDebug
) const {
105 switch (RecordKind
) {
107 cast
<DbgVariableRecord
>(this)->print(O
, MST
, IsForDebug
);
110 cast
<DbgLabelRecord
>(this)->print(O
, MST
, IsForDebug
);
113 llvm_unreachable("unsupported DbgRecord kind");
116 bool DbgRecord::isIdenticalToWhenDefined(const DbgRecord
&R
) const {
117 if (RecordKind
!= R
.RecordKind
)
119 switch (RecordKind
) {
121 return cast
<DbgVariableRecord
>(this)->isIdenticalToWhenDefined(
122 *cast
<DbgVariableRecord
>(&R
));
124 return cast
<DbgLabelRecord
>(this)->getLabel() ==
125 cast
<DbgLabelRecord
>(R
).getLabel();
127 llvm_unreachable("unsupported DbgRecord kind");
130 bool DbgRecord::isEquivalentTo(const DbgRecord
&R
) const {
131 return getDebugLoc() == R
.getDebugLoc() && isIdenticalToWhenDefined(R
);
135 DbgRecord::createDebugIntrinsic(Module
*M
, Instruction
*InsertBefore
) const {
136 switch (RecordKind
) {
138 return cast
<DbgVariableRecord
>(this)->createDebugIntrinsic(M
, InsertBefore
);
140 return cast
<DbgLabelRecord
>(this)->createDebugIntrinsic(M
, InsertBefore
);
142 llvm_unreachable("unsupported DbgRecord kind");
145 DbgLabelRecord::DbgLabelRecord(MDNode
*Label
, MDNode
*DL
)
146 : DbgRecord(LabelKind
, DebugLoc(DL
)), Label(Label
) {
147 assert(Label
&& "Unexpected nullptr");
148 assert((isa
<DILabel
>(Label
) || Label
->isTemporary()) &&
149 "Label type must be or resolve to a DILabel");
151 DbgLabelRecord::DbgLabelRecord(DILabel
*Label
, DebugLoc DL
)
152 : DbgRecord(LabelKind
, DL
), Label(Label
) {
153 assert(Label
&& "Unexpected nullptr");
156 DbgLabelRecord
*DbgLabelRecord::createUnresolvedDbgLabelRecord(MDNode
*Label
,
158 return new DbgLabelRecord(Label
, DL
);
161 DbgVariableRecord::DbgVariableRecord(DbgVariableRecord::LocationType Type
,
162 Metadata
*Val
, MDNode
*Variable
,
163 MDNode
*Expression
, MDNode
*AssignID
,
165 MDNode
*AddressExpression
, MDNode
*DI
)
166 : DbgRecord(ValueKind
, DebugLoc(DI
)),
167 DebugValueUser({Val
, Address
, AssignID
}), Type(Type
), Variable(Variable
),
168 Expression(Expression
), AddressExpression(AddressExpression
) {}
170 DbgVariableRecord
*DbgVariableRecord::createUnresolvedDbgVariableRecord(
171 DbgVariableRecord::LocationType Type
, Metadata
*Val
, MDNode
*Variable
,
172 MDNode
*Expression
, MDNode
*AssignID
, Metadata
*Address
,
173 MDNode
*AddressExpression
, MDNode
*DI
) {
174 return new DbgVariableRecord(Type
, Val
, Variable
, Expression
, AssignID
,
175 Address
, AddressExpression
, DI
);
179 DbgVariableRecord::createDbgVariableRecord(Value
*Location
, DILocalVariable
*DV
,
181 const DILocation
*DI
) {
182 return new DbgVariableRecord(ValueAsMetadata::get(Location
), DV
, Expr
, DI
,
183 LocationType::Value
);
186 DbgVariableRecord
*DbgVariableRecord::createDbgVariableRecord(
187 Value
*Location
, DILocalVariable
*DV
, DIExpression
*Expr
,
188 const DILocation
*DI
, DbgVariableRecord
&InsertBefore
) {
189 auto *NewDbgVariableRecord
= createDbgVariableRecord(Location
, DV
, Expr
, DI
);
190 NewDbgVariableRecord
->insertBefore(&InsertBefore
);
191 return NewDbgVariableRecord
;
194 DbgVariableRecord
*DbgVariableRecord::createDVRDeclare(Value
*Address
,
197 const DILocation
*DI
) {
198 return new DbgVariableRecord(ValueAsMetadata::get(Address
), DV
, Expr
, DI
,
199 LocationType::Declare
);
203 DbgVariableRecord::createDVRDeclare(Value
*Address
, DILocalVariable
*DV
,
204 DIExpression
*Expr
, const DILocation
*DI
,
205 DbgVariableRecord
&InsertBefore
) {
206 auto *NewDVRDeclare
= createDVRDeclare(Address
, DV
, Expr
, DI
);
207 NewDVRDeclare
->insertBefore(&InsertBefore
);
208 return NewDVRDeclare
;
211 DbgVariableRecord
*DbgVariableRecord::createDVRAssign(
212 Value
*Val
, DILocalVariable
*Variable
, DIExpression
*Expression
,
213 DIAssignID
*AssignID
, Value
*Address
, DIExpression
*AddressExpression
,
214 const DILocation
*DI
) {
215 return new DbgVariableRecord(ValueAsMetadata::get(Val
), Variable
, Expression
,
216 AssignID
, ValueAsMetadata::get(Address
),
217 AddressExpression
, DI
);
220 DbgVariableRecord
*DbgVariableRecord::createLinkedDVRAssign(
221 Instruction
*LinkedInstr
, Value
*Val
, DILocalVariable
*Variable
,
222 DIExpression
*Expression
, Value
*Address
, DIExpression
*AddressExpression
,
223 const DILocation
*DI
) {
224 auto *Link
= LinkedInstr
->getMetadata(LLVMContext::MD_DIAssignID
);
225 assert(Link
&& "Linked instruction must have DIAssign metadata attached");
226 auto *NewDVRAssign
= DbgVariableRecord::createDVRAssign(
227 Val
, Variable
, Expression
, cast
<DIAssignID
>(Link
), Address
,
228 AddressExpression
, DI
);
229 LinkedInstr
->getParent()->insertDbgRecordAfter(NewDVRAssign
, LinkedInstr
);
233 iterator_range
<DbgVariableRecord::location_op_iterator
>
234 DbgVariableRecord::location_ops() const {
235 auto *MD
= getRawLocation();
236 // If a Value has been deleted, the "location" for this DbgVariableRecord will
237 // be replaced by nullptr. Return an empty range.
239 return {location_op_iterator(static_cast<ValueAsMetadata
*>(nullptr)),
240 location_op_iterator(static_cast<ValueAsMetadata
*>(nullptr))};
242 // If operand is ValueAsMetadata, return a range over just that operand.
243 if (auto *VAM
= dyn_cast
<ValueAsMetadata
>(MD
))
244 return {location_op_iterator(VAM
), location_op_iterator(VAM
+ 1)};
246 // If operand is DIArgList, return a range over its args.
247 if (auto *AL
= dyn_cast
<DIArgList
>(MD
))
248 return {location_op_iterator(AL
->args_begin()),
249 location_op_iterator(AL
->args_end())};
251 // Operand is an empty metadata tuple, so return empty iterator.
252 assert(cast
<MDNode
>(MD
)->getNumOperands() == 0);
253 return {location_op_iterator(static_cast<ValueAsMetadata
*>(nullptr)),
254 location_op_iterator(static_cast<ValueAsMetadata
*>(nullptr))};
257 unsigned DbgVariableRecord::getNumVariableLocationOps() const {
259 return cast
<DIArgList
>(getRawLocation())->getArgs().size();
263 Value
*DbgVariableRecord::getVariableLocationOp(unsigned OpIdx
) const {
264 auto *MD
= getRawLocation();
268 if (auto *AL
= dyn_cast
<DIArgList
>(MD
))
269 return AL
->getArgs()[OpIdx
]->getValue();
272 assert(isa
<ValueAsMetadata
>(MD
) &&
273 "Attempted to get location operand from DbgVariableRecord with none.");
274 auto *V
= cast
<ValueAsMetadata
>(MD
);
275 assert(OpIdx
== 0 && "Operand Index must be 0 for a debug intrinsic with a "
276 "single location operand.");
277 return V
->getValue();
280 static ValueAsMetadata
*getAsMetadata(Value
*V
) {
281 return isa
<MetadataAsValue
>(V
) ? dyn_cast
<ValueAsMetadata
>(
282 cast
<MetadataAsValue
>(V
)->getMetadata())
283 : ValueAsMetadata::get(V
);
286 void DbgVariableRecord::replaceVariableLocationOp(Value
*OldValue
,
289 assert(NewValue
&& "Values must be non-null");
291 bool DbgAssignAddrReplaced
= isDbgAssign() && OldValue
== getAddress();
292 if (DbgAssignAddrReplaced
)
293 setAddress(NewValue
);
295 auto Locations
= location_ops();
296 auto OldIt
= find(Locations
, OldValue
);
297 if (OldIt
== Locations
.end()) {
298 if (AllowEmpty
|| DbgAssignAddrReplaced
)
300 llvm_unreachable("OldValue must be a current location");
304 // Set our location to be the MAV wrapping the new Value.
305 setRawLocation(isa
<MetadataAsValue
>(NewValue
)
306 ? cast
<MetadataAsValue
>(NewValue
)->getMetadata()
307 : ValueAsMetadata::get(NewValue
));
311 // We must be referring to a DIArgList, produce a new operands vector with the
312 // old value replaced, generate a new DIArgList and set it as our location.
313 SmallVector
<ValueAsMetadata
*, 4> MDs
;
314 ValueAsMetadata
*NewOperand
= getAsMetadata(NewValue
);
315 for (auto *VMD
: Locations
)
316 MDs
.push_back(VMD
== *OldIt
? NewOperand
: getAsMetadata(VMD
));
317 setRawLocation(DIArgList::get(getVariableLocationOp(0)->getContext(), MDs
));
320 void DbgVariableRecord::replaceVariableLocationOp(unsigned OpIdx
,
322 assert(OpIdx
< getNumVariableLocationOps() && "Invalid Operand Index");
325 setRawLocation(isa
<MetadataAsValue
>(NewValue
)
326 ? cast
<MetadataAsValue
>(NewValue
)->getMetadata()
327 : ValueAsMetadata::get(NewValue
));
331 SmallVector
<ValueAsMetadata
*, 4> MDs
;
332 ValueAsMetadata
*NewOperand
= getAsMetadata(NewValue
);
333 for (unsigned Idx
= 0; Idx
< getNumVariableLocationOps(); ++Idx
)
334 MDs
.push_back(Idx
== OpIdx
? NewOperand
335 : getAsMetadata(getVariableLocationOp(Idx
)));
337 setRawLocation(DIArgList::get(getVariableLocationOp(0)->getContext(), MDs
));
340 void DbgVariableRecord::addVariableLocationOps(ArrayRef
<Value
*> NewValues
,
341 DIExpression
*NewExpr
) {
342 assert(NewExpr
->hasAllLocationOps(getNumVariableLocationOps() +
344 "NewExpr for debug variable intrinsic does not reference every "
345 "location operand.");
346 assert(!is_contained(NewValues
, nullptr) && "New values must be non-null");
347 setExpression(NewExpr
);
348 SmallVector
<ValueAsMetadata
*, 4> MDs
;
349 for (auto *VMD
: location_ops())
350 MDs
.push_back(getAsMetadata(VMD
));
351 for (auto *VMD
: NewValues
)
352 MDs
.push_back(getAsMetadata(VMD
));
353 setRawLocation(DIArgList::get(getVariableLocationOp(0)->getContext(), MDs
));
356 void DbgVariableRecord::setKillLocation() {
357 // TODO: When/if we remove duplicate values from DIArgLists, we don't need
359 SmallPtrSet
<Value
*, 4> RemovedValues
;
360 for (Value
*OldValue
: location_ops()) {
361 if (!RemovedValues
.insert(OldValue
).second
)
363 Value
*Poison
= PoisonValue::get(OldValue
->getType());
364 replaceVariableLocationOp(OldValue
, Poison
);
368 bool DbgVariableRecord::isKillLocation() const {
369 return (!hasArgList() && isa
<MDNode
>(getRawLocation())) ||
370 (getNumVariableLocationOps() == 0 && !getExpression()->isComplex()) ||
371 any_of(location_ops(), [](Value
*V
) { return isa
<UndefValue
>(V
); });
374 std::optional
<DbgVariableFragmentInfo
> DbgVariableRecord::getFragment() const {
375 return getExpression()->getFragmentInfo();
378 std::optional
<uint64_t> DbgVariableRecord::getFragmentSizeInBits() const {
379 if (auto Fragment
= getExpression()->getFragmentInfo())
380 return Fragment
->SizeInBits
;
381 return getVariable()->getSizeInBits();
384 DbgRecord
*DbgRecord::clone() const {
385 switch (RecordKind
) {
387 return cast
<DbgVariableRecord
>(this)->clone();
389 return cast
<DbgLabelRecord
>(this)->clone();
391 llvm_unreachable("unsupported DbgRecord kind");
394 DbgVariableRecord
*DbgVariableRecord::clone() const {
395 return new DbgVariableRecord(*this);
398 DbgLabelRecord
*DbgLabelRecord::clone() const {
399 return new DbgLabelRecord(getLabel(), getDebugLoc());
402 DbgVariableIntrinsic
*
403 DbgVariableRecord::createDebugIntrinsic(Module
*M
,
404 Instruction
*InsertBefore
) const {
405 [[maybe_unused
]] DICompileUnit
*Unit
=
406 getDebugLoc()->getScope()->getSubprogram()->getUnit();
408 "Cannot clone from BasicBlock that is not part of a Module or "
410 LLVMContext
&Context
= getDebugLoc()->getContext();
411 Function
*IntrinsicFn
;
413 // Work out what sort of intrinsic we're going to produce.
415 case DbgVariableRecord::LocationType::Declare
:
416 IntrinsicFn
= Intrinsic::getOrInsertDeclaration(M
, Intrinsic::dbg_declare
);
418 case DbgVariableRecord::LocationType::Value
:
419 IntrinsicFn
= Intrinsic::getOrInsertDeclaration(M
, Intrinsic::dbg_value
);
421 case DbgVariableRecord::LocationType::Assign
:
422 IntrinsicFn
= Intrinsic::getOrInsertDeclaration(M
, Intrinsic::dbg_assign
);
424 case DbgVariableRecord::LocationType::End
:
425 case DbgVariableRecord::LocationType::Any
:
426 llvm_unreachable("Invalid LocationType");
429 // Create the intrinsic from this DbgVariableRecord's information, optionally
430 // insert into the target location.
431 DbgVariableIntrinsic
*DVI
;
432 assert(getRawLocation() &&
433 "DbgVariableRecord's RawLocation should be non-null.");
435 Value
*AssignArgs
[] = {
436 MetadataAsValue::get(Context
, getRawLocation()),
437 MetadataAsValue::get(Context
, getVariable()),
438 MetadataAsValue::get(Context
, getExpression()),
439 MetadataAsValue::get(Context
, getAssignID()),
440 MetadataAsValue::get(Context
, getRawAddress()),
441 MetadataAsValue::get(Context
, getAddressExpression())};
442 DVI
= cast
<DbgVariableIntrinsic
>(CallInst::Create(
443 IntrinsicFn
->getFunctionType(), IntrinsicFn
, AssignArgs
));
445 Value
*Args
[] = {MetadataAsValue::get(Context
, getRawLocation()),
446 MetadataAsValue::get(Context
, getVariable()),
447 MetadataAsValue::get(Context
, getExpression())};
448 DVI
= cast
<DbgVariableIntrinsic
>(
449 CallInst::Create(IntrinsicFn
->getFunctionType(), IntrinsicFn
, Args
));
452 DVI
->setDebugLoc(getDebugLoc());
454 DVI
->insertBefore(InsertBefore
);
460 DbgLabelRecord::createDebugIntrinsic(Module
*M
,
461 Instruction
*InsertBefore
) const {
462 auto *LabelFn
= Intrinsic::getOrInsertDeclaration(M
, Intrinsic::dbg_label
);
464 MetadataAsValue::get(getDebugLoc()->getContext(), getLabel())};
465 DbgLabelInst
*DbgLabel
= cast
<DbgLabelInst
>(
466 CallInst::Create(LabelFn
->getFunctionType(), LabelFn
, Args
));
467 DbgLabel
->setTailCall();
468 DbgLabel
->setDebugLoc(getDebugLoc());
470 DbgLabel
->insertBefore(InsertBefore
);
474 Value
*DbgVariableRecord::getAddress() const {
475 auto *MD
= getRawAddress();
476 if (auto *V
= dyn_cast_or_null
<ValueAsMetadata
>(MD
))
477 return V
->getValue();
479 // When the value goes to null, it gets replaced by an empty MDNode.
480 assert((!MD
|| !cast
<MDNode
>(MD
)->getNumOperands()) &&
481 "Expected an empty MDNode");
485 DIAssignID
*DbgVariableRecord::getAssignID() const {
486 return cast
<DIAssignID
>(DebugValues
[2]);
489 void DbgVariableRecord::setAssignId(DIAssignID
*New
) {
490 resetDebugValue(2, New
);
493 void DbgVariableRecord::setKillAddress() {
495 1, ValueAsMetadata::get(PoisonValue::get(getAddress()->getType())));
498 bool DbgVariableRecord::isKillAddress() const {
499 Value
*Addr
= getAddress();
500 return !Addr
|| isa
<UndefValue
>(Addr
);
503 const Instruction
*DbgRecord::getInstruction() const {
504 return Marker
->MarkedInstr
;
507 const BasicBlock
*DbgRecord::getParent() const {
508 return Marker
->MarkedInstr
->getParent();
511 BasicBlock
*DbgRecord::getParent() { return Marker
->MarkedInstr
->getParent(); }
513 BasicBlock
*DbgRecord::getBlock() { return Marker
->getParent(); }
515 const BasicBlock
*DbgRecord::getBlock() const { return Marker
->getParent(); }
517 Function
*DbgRecord::getFunction() { return getBlock()->getParent(); }
519 const Function
*DbgRecord::getFunction() const {
520 return getBlock()->getParent();
523 Module
*DbgRecord::getModule() { return getFunction()->getParent(); }
525 const Module
*DbgRecord::getModule() const {
526 return getFunction()->getParent();
529 LLVMContext
&DbgRecord::getContext() { return getBlock()->getContext(); }
531 const LLVMContext
&DbgRecord::getContext() const {
532 return getBlock()->getContext();
535 void DbgRecord::insertBefore(DbgRecord
*InsertBefore
) {
536 assert(!getMarker() &&
537 "Cannot insert a DbgRecord that is already has a DbgMarker!");
538 assert(InsertBefore
->getMarker() &&
539 "Cannot insert a DbgRecord before a DbgRecord that does not have a "
541 InsertBefore
->getMarker()->insertDbgRecord(this, InsertBefore
);
543 void DbgRecord::insertAfter(DbgRecord
*InsertAfter
) {
544 assert(!getMarker() &&
545 "Cannot insert a DbgRecord that is already has a DbgMarker!");
546 assert(InsertAfter
->getMarker() &&
547 "Cannot insert a DbgRecord after a DbgRecord that does not have a "
549 InsertAfter
->getMarker()->insertDbgRecordAfter(this, InsertAfter
);
551 void DbgRecord::moveBefore(DbgRecord
*MoveBefore
) {
552 assert(getMarker() &&
553 "Canot move a DbgRecord that does not currently have a DbgMarker!");
555 insertBefore(MoveBefore
);
557 void DbgRecord::moveAfter(DbgRecord
*MoveAfter
) {
558 assert(getMarker() &&
559 "Canot move a DbgRecord that does not currently have a DbgMarker!");
561 insertAfter(MoveAfter
);
564 ///////////////////////////////////////////////////////////////////////////////
566 // An empty, global, DbgMarker for the purpose of describing empty ranges of
568 DbgMarker
DbgMarker::EmptyDbgMarker
;
570 void DbgMarker::dropDbgRecords() {
571 while (!StoredDbgRecords
.empty()) {
572 auto It
= StoredDbgRecords
.begin();
573 DbgRecord
*DR
= &*It
;
574 StoredDbgRecords
.erase(It
);
579 void DbgMarker::dropOneDbgRecord(DbgRecord
*DR
) {
580 assert(DR
->getMarker() == this);
581 StoredDbgRecords
.erase(DR
->getIterator());
585 const BasicBlock
*DbgMarker::getParent() const {
586 return MarkedInstr
->getParent();
589 BasicBlock
*DbgMarker::getParent() { return MarkedInstr
->getParent(); }
591 void DbgMarker::removeMarker() {
592 // Are there any DbgRecords in this DbgMarker? If not, nothing to preserve.
593 Instruction
*Owner
= MarkedInstr
;
594 if (StoredDbgRecords
.empty()) {
596 Owner
->DebugMarker
= nullptr;
600 // The attached DbgRecords need to be preserved; attach them to the next
601 // instruction. If there isn't a next instruction, put them on the
603 DbgMarker
*NextMarker
= Owner
->getParent()->getNextMarker(Owner
);
605 NextMarker
->absorbDebugValues(*this, true);
608 // We can avoid a deallocation -- just store this marker onto the next
609 // instruction. Unless we're at the end of the block, in which case this
610 // marker becomes the trailing marker of a degenerate block.
611 BasicBlock::iterator NextIt
= std::next(Owner
->getIterator());
612 if (NextIt
== getParent()->end()) {
613 getParent()->setTrailingDbgRecords(this);
614 MarkedInstr
= nullptr;
616 NextIt
->DebugMarker
= this;
617 MarkedInstr
= &*NextIt
;
620 Owner
->DebugMarker
= nullptr;
623 void DbgMarker::removeFromParent() {
624 MarkedInstr
->DebugMarker
= nullptr;
625 MarkedInstr
= nullptr;
628 void DbgMarker::eraseFromParent() {
635 iterator_range
<DbgRecord::self_iterator
> DbgMarker::getDbgRecordRange() {
636 return make_range(StoredDbgRecords
.begin(), StoredDbgRecords
.end());
638 iterator_range
<DbgRecord::const_self_iterator
>
639 DbgMarker::getDbgRecordRange() const {
640 return make_range(StoredDbgRecords
.begin(), StoredDbgRecords
.end());
643 void DbgRecord::removeFromParent() {
644 getMarker()->StoredDbgRecords
.erase(getIterator());
648 void DbgRecord::eraseFromParent() {
653 void DbgMarker::insertDbgRecord(DbgRecord
*New
, bool InsertAtHead
) {
654 auto It
= InsertAtHead
? StoredDbgRecords
.begin() : StoredDbgRecords
.end();
655 StoredDbgRecords
.insert(It
, *New
);
656 New
->setMarker(this);
658 void DbgMarker::insertDbgRecord(DbgRecord
*New
, DbgRecord
*InsertBefore
) {
659 assert(InsertBefore
->getMarker() == this &&
660 "DbgRecord 'InsertBefore' must be contained in this DbgMarker!");
661 StoredDbgRecords
.insert(InsertBefore
->getIterator(), *New
);
662 New
->setMarker(this);
664 void DbgMarker::insertDbgRecordAfter(DbgRecord
*New
, DbgRecord
*InsertAfter
) {
665 assert(InsertAfter
->getMarker() == this &&
666 "DbgRecord 'InsertAfter' must be contained in this DbgMarker!");
667 StoredDbgRecords
.insert(++(InsertAfter
->getIterator()), *New
);
668 New
->setMarker(this);
671 void DbgMarker::absorbDebugValues(DbgMarker
&Src
, bool InsertAtHead
) {
672 auto It
= InsertAtHead
? StoredDbgRecords
.begin() : StoredDbgRecords
.end();
673 for (DbgRecord
&DVR
: Src
.StoredDbgRecords
)
676 StoredDbgRecords
.splice(It
, Src
.StoredDbgRecords
);
679 void DbgMarker::absorbDebugValues(
680 iterator_range
<DbgRecord::self_iterator
> Range
, DbgMarker
&Src
,
682 for (DbgRecord
&DR
: Range
)
686 (InsertAtHead
) ? StoredDbgRecords
.begin() : StoredDbgRecords
.end();
688 StoredDbgRecords
.splice(InsertPos
, Src
.StoredDbgRecords
, Range
.begin(),
692 iterator_range
<simple_ilist
<DbgRecord
>::iterator
> DbgMarker::cloneDebugInfoFrom(
693 DbgMarker
*From
, std::optional
<simple_ilist
<DbgRecord
>::iterator
> from_here
,
695 DbgRecord
*First
= nullptr;
696 // Work out what range of DbgRecords to clone: normally all the contents of
697 // the "From" marker, optionally we can start from the from_here position down
700 make_range(From
->StoredDbgRecords
.begin(), From
->StoredDbgRecords
.end());
701 if (from_here
.has_value())
702 Range
= make_range(*from_here
, From
->StoredDbgRecords
.end());
704 // Clone each DbgVariableRecord and insert into StoreDbgVariableRecords;
705 // optionally place them at the start or the end of the list.
706 auto Pos
= (InsertAtHead
) ? StoredDbgRecords
.begin() : StoredDbgRecords
.end();
707 for (DbgRecord
&DR
: Range
) {
708 DbgRecord
*New
= DR
.clone();
709 New
->setMarker(this);
710 StoredDbgRecords
.insert(Pos
, *New
);
716 return {StoredDbgRecords
.end(), StoredDbgRecords
.end()};
719 // If InsertAtHead is set, we cloned a range onto the front of of the
720 // StoredDbgRecords collection, return that range.
721 return {StoredDbgRecords
.begin(), Pos
};
723 // We inserted a block at the end, return that range.
724 return {First
->getIterator(), StoredDbgRecords
.end()};
727 } // end namespace llvm