1 //=====-- DebugProgramInstruction.cpp - Implement DbgRecords/DPMarkers --=====// 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 #include "llvm/IR/DebugInfoMetadata.h" 10 #include "llvm/IR/DebugProgramInstruction.h" 11 #include "llvm/IR/DIBuilder.h" 12 #include "llvm/IR/IntrinsicInst.h" 13 14 namespace llvm { 15 16 template <typename T> 17 DbgRecordParamRef<T>::DbgRecordParamRef(const T *Param) 18 : Ref(const_cast<T *>(Param)) {} 19 template <typename T> 20 DbgRecordParamRef<T>::DbgRecordParamRef(const MDNode *Param) 21 : Ref(const_cast<MDNode *>(Param)) {} 22 23 template <typename T> T *DbgRecordParamRef<T>::get() const { 24 return cast<T>(Ref); 25 } 26 27 template class DbgRecordParamRef<DIExpression>; 28 template class DbgRecordParamRef<DILabel>; 29 template class DbgRecordParamRef<DILocalVariable>; 30 31 DPValue::DPValue(const DbgVariableIntrinsic *DVI) 32 : DbgRecord(ValueKind, DVI->getDebugLoc()), 33 DebugValueUser({DVI->getRawLocation(), nullptr, nullptr}), 34 Variable(DVI->getVariable()), Expression(DVI->getExpression()), 35 AddressExpression() { 36 switch (DVI->getIntrinsicID()) { 37 case Intrinsic::dbg_value: 38 Type = LocationType::Value; 39 break; 40 case Intrinsic::dbg_declare: 41 Type = LocationType::Declare; 42 break; 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()); 50 break; 51 } 52 default: 53 llvm_unreachable( 54 "Trying to create a DPValue with an invalid intrinsic type!"); 55 } 56 } 57 58 DPValue::DPValue(const DPValue &DPV) 59 : DbgRecord(ValueKind, DPV.getDebugLoc()), DebugValueUser(DPV.DebugValues), 60 Type(DPV.getType()), Variable(DPV.getVariable()), 61 Expression(DPV.getExpression()), 62 AddressExpression(DPV.AddressExpression) {} 63 64 DPValue::DPValue(Metadata *Location, DILocalVariable *DV, DIExpression *Expr, 65 const DILocation *DI, LocationType Type) 66 : DbgRecord(ValueKind, DI), DebugValueUser({Location, nullptr, nullptr}), 67 Type(Type), Variable(DV), Expression(Expr) {} 68 69 DPValue::DPValue(Metadata *Value, DILocalVariable *Variable, 70 DIExpression *Expression, DIAssignID *AssignID, 71 Metadata *Address, DIExpression *AddressExpression, 72 const DILocation *DI) 73 : DbgRecord(ValueKind, DI), DebugValueUser({Value, Address, AssignID}), 74 Type(LocationType::Assign), Variable(Variable), Expression(Expression), 75 AddressExpression(AddressExpression) {} 76 77 void DbgRecord::deleteRecord() { 78 switch (RecordKind) { 79 case ValueKind: 80 delete cast<DPValue>(this); 81 return; 82 case LabelKind: 83 delete cast<DPLabel>(this); 84 return; 85 } 86 llvm_unreachable("unsupported DbgRecord kind"); 87 } 88 89 void DbgRecord::print(raw_ostream &O, bool IsForDebug) const { 90 switch (RecordKind) { 91 case ValueKind: 92 cast<DPValue>(this)->print(O, IsForDebug); 93 return; 94 case LabelKind: 95 cast<DPLabel>(this)->print(O, IsForDebug); 96 return; 97 }; 98 llvm_unreachable("unsupported DbgRecord kind"); 99 } 100 101 void DbgRecord::print(raw_ostream &O, ModuleSlotTracker &MST, 102 bool IsForDebug) const { 103 switch (RecordKind) { 104 case ValueKind: 105 cast<DPValue>(this)->print(O, MST, IsForDebug); 106 return; 107 case LabelKind: 108 cast<DPLabel>(this)->print(O, MST, IsForDebug); 109 return; 110 }; 111 llvm_unreachable("unsupported DbgRecord kind"); 112 } 113 114 bool DbgRecord::isIdenticalToWhenDefined(const DbgRecord &R) const { 115 if (RecordKind != R.RecordKind) 116 return false; 117 switch (RecordKind) { 118 case ValueKind: 119 return cast<DPValue>(this)->isIdenticalToWhenDefined(*cast<DPValue>(&R)); 120 case LabelKind: 121 return cast<DPLabel>(this)->getLabel() == cast<DPLabel>(R).getLabel(); 122 }; 123 llvm_unreachable("unsupported DbgRecord kind"); 124 } 125 126 bool DbgRecord::isEquivalentTo(const DbgRecord &R) const { 127 return getDebugLoc() == R.getDebugLoc() && isIdenticalToWhenDefined(R); 128 } 129 130 DbgInfoIntrinsic * 131 DbgRecord::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const { 132 switch (RecordKind) { 133 case ValueKind: 134 return cast<DPValue>(this)->createDebugIntrinsic(M, InsertBefore); 135 case LabelKind: 136 return cast<DPLabel>(this)->createDebugIntrinsic(M, InsertBefore); 137 }; 138 llvm_unreachable("unsupported DbgRecord kind"); 139 } 140 141 DPLabel::DPLabel(MDNode *Label, MDNode *DL) 142 : DbgRecord(LabelKind, DebugLoc(DL)), Label(Label) { 143 assert(Label && "Unexpected nullptr"); 144 assert((isa<DILabel>(Label) || Label->isTemporary()) && 145 "Label type must be or resolve to a DILabel"); 146 } 147 DPLabel::DPLabel(DILabel *Label, DebugLoc DL) 148 : DbgRecord(LabelKind, DL), Label(Label) { 149 assert(Label && "Unexpected nullptr"); 150 } 151 152 DPLabel *DPLabel::createUnresolvedDPLabel(MDNode *Label, MDNode *DL) { 153 return new DPLabel(Label, DL); 154 } 155 156 DPValue::DPValue(DPValue::LocationType Type, Metadata *Val, MDNode *Variable, 157 MDNode *Expression, MDNode *AssignID, Metadata *Address, 158 MDNode *AddressExpression, MDNode *DI) 159 : DbgRecord(ValueKind, DebugLoc(DI)), 160 DebugValueUser({Val, Address, AssignID}), Type(Type), Variable(Variable), 161 Expression(Expression), AddressExpression(AddressExpression) {} 162 163 DPValue *DPValue::createUnresolvedDPValue(DPValue::LocationType Type, 164 Metadata *Val, MDNode *Variable, 165 MDNode *Expression, MDNode *AssignID, 166 Metadata *Address, 167 MDNode *AddressExpression, 168 MDNode *DI) { 169 return new DPValue(Type, Val, Variable, Expression, AssignID, Address, 170 AddressExpression, DI); 171 } 172 173 DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV, 174 DIExpression *Expr, const DILocation *DI) { 175 return new DPValue(ValueAsMetadata::get(Location), DV, Expr, DI, 176 LocationType::Value); 177 } 178 179 DPValue *DPValue::createDPValue(Value *Location, DILocalVariable *DV, 180 DIExpression *Expr, const DILocation *DI, 181 DPValue &InsertBefore) { 182 auto *NewDPValue = createDPValue(Location, DV, Expr, DI); 183 NewDPValue->insertBefore(&InsertBefore); 184 return NewDPValue; 185 } 186 187 DPValue *DPValue::createDPVDeclare(Value *Address, DILocalVariable *DV, 188 DIExpression *Expr, const DILocation *DI) { 189 return new DPValue(ValueAsMetadata::get(Address), DV, Expr, DI, 190 LocationType::Declare); 191 } 192 193 DPValue *DPValue::createDPVDeclare(Value *Address, DILocalVariable *DV, 194 DIExpression *Expr, const DILocation *DI, 195 DPValue &InsertBefore) { 196 auto *NewDPVDeclare = createDPVDeclare(Address, DV, Expr, DI); 197 NewDPVDeclare->insertBefore(&InsertBefore); 198 return NewDPVDeclare; 199 } 200 201 DPValue *DPValue::createDPVAssign(Value *Val, DILocalVariable *Variable, 202 DIExpression *Expression, 203 DIAssignID *AssignID, Value *Address, 204 DIExpression *AddressExpression, 205 const DILocation *DI) { 206 return new DPValue(ValueAsMetadata::get(Val), Variable, Expression, AssignID, 207 ValueAsMetadata::get(Address), AddressExpression, DI); 208 } 209 210 DPValue *DPValue::createLinkedDPVAssign(Instruction *LinkedInstr, Value *Val, 211 DILocalVariable *Variable, 212 DIExpression *Expression, 213 Value *Address, 214 DIExpression *AddressExpression, 215 const DILocation *DI) { 216 auto *Link = LinkedInstr->getMetadata(LLVMContext::MD_DIAssignID); 217 assert(Link && "Linked instruction must have DIAssign metadata attached"); 218 auto *NewDPVAssign = DPValue::createDPVAssign(Val, Variable, Expression, 219 cast<DIAssignID>(Link), Address, 220 AddressExpression, DI); 221 LinkedInstr->getParent()->insertDbgRecordAfter(NewDPVAssign, LinkedInstr); 222 return NewDPVAssign; 223 } 224 225 iterator_range<DPValue::location_op_iterator> DPValue::location_ops() const { 226 auto *MD = getRawLocation(); 227 // If a Value has been deleted, the "location" for this DPValue will be 228 // replaced by nullptr. Return an empty range. 229 if (!MD) 230 return {location_op_iterator(static_cast<ValueAsMetadata *>(nullptr)), 231 location_op_iterator(static_cast<ValueAsMetadata *>(nullptr))}; 232 233 // If operand is ValueAsMetadata, return a range over just that operand. 234 if (auto *VAM = dyn_cast<ValueAsMetadata>(MD)) 235 return {location_op_iterator(VAM), location_op_iterator(VAM + 1)}; 236 237 // If operand is DIArgList, return a range over its args. 238 if (auto *AL = dyn_cast<DIArgList>(MD)) 239 return {location_op_iterator(AL->args_begin()), 240 location_op_iterator(AL->args_end())}; 241 242 // Operand is an empty metadata tuple, so return empty iterator. 243 assert(cast<MDNode>(MD)->getNumOperands() == 0); 244 return {location_op_iterator(static_cast<ValueAsMetadata *>(nullptr)), 245 location_op_iterator(static_cast<ValueAsMetadata *>(nullptr))}; 246 } 247 248 unsigned DPValue::getNumVariableLocationOps() const { 249 if (hasArgList()) 250 return cast<DIArgList>(getRawLocation())->getArgs().size(); 251 return 1; 252 } 253 254 Value *DPValue::getVariableLocationOp(unsigned OpIdx) const { 255 auto *MD = getRawLocation(); 256 if (!MD) 257 return nullptr; 258 259 if (auto *AL = dyn_cast<DIArgList>(MD)) 260 return AL->getArgs()[OpIdx]->getValue(); 261 if (isa<MDNode>(MD)) 262 return nullptr; 263 assert(isa<ValueAsMetadata>(MD) && 264 "Attempted to get location operand from DPValue with none."); 265 auto *V = cast<ValueAsMetadata>(MD); 266 assert(OpIdx == 0 && "Operand Index must be 0 for a debug intrinsic with a " 267 "single location operand."); 268 return V->getValue(); 269 } 270 271 static ValueAsMetadata *getAsMetadata(Value *V) { 272 return isa<MetadataAsValue>(V) ? dyn_cast<ValueAsMetadata>( 273 cast<MetadataAsValue>(V)->getMetadata()) 274 : ValueAsMetadata::get(V); 275 } 276 277 void DPValue::replaceVariableLocationOp(Value *OldValue, Value *NewValue, 278 bool AllowEmpty) { 279 assert(NewValue && "Values must be non-null"); 280 281 bool DbgAssignAddrReplaced = isDbgAssign() && OldValue == getAddress(); 282 if (DbgAssignAddrReplaced) 283 setAddress(NewValue); 284 285 auto Locations = location_ops(); 286 auto OldIt = find(Locations, OldValue); 287 if (OldIt == Locations.end()) { 288 if (AllowEmpty || DbgAssignAddrReplaced) 289 return; 290 llvm_unreachable("OldValue must be a current location"); 291 } 292 293 if (!hasArgList()) { 294 // Set our location to be the MAV wrapping the new Value. 295 setRawLocation(isa<MetadataAsValue>(NewValue) 296 ? cast<MetadataAsValue>(NewValue)->getMetadata() 297 : ValueAsMetadata::get(NewValue)); 298 return; 299 } 300 301 // We must be referring to a DIArgList, produce a new operands vector with the 302 // old value replaced, generate a new DIArgList and set it as our location. 303 SmallVector<ValueAsMetadata *, 4> MDs; 304 ValueAsMetadata *NewOperand = getAsMetadata(NewValue); 305 for (auto *VMD : Locations) 306 MDs.push_back(VMD == *OldIt ? NewOperand : getAsMetadata(VMD)); 307 setRawLocation(DIArgList::get(getVariableLocationOp(0)->getContext(), MDs)); 308 } 309 310 void DPValue::replaceVariableLocationOp(unsigned OpIdx, Value *NewValue) { 311 assert(OpIdx < getNumVariableLocationOps() && "Invalid Operand Index"); 312 313 if (!hasArgList()) { 314 setRawLocation(isa<MetadataAsValue>(NewValue) 315 ? cast<MetadataAsValue>(NewValue)->getMetadata() 316 : ValueAsMetadata::get(NewValue)); 317 return; 318 } 319 320 SmallVector<ValueAsMetadata *, 4> MDs; 321 ValueAsMetadata *NewOperand = getAsMetadata(NewValue); 322 for (unsigned Idx = 0; Idx < getNumVariableLocationOps(); ++Idx) 323 MDs.push_back(Idx == OpIdx ? NewOperand 324 : getAsMetadata(getVariableLocationOp(Idx))); 325 326 setRawLocation(DIArgList::get(getVariableLocationOp(0)->getContext(), MDs)); 327 } 328 329 void DPValue::addVariableLocationOps(ArrayRef<Value *> NewValues, 330 DIExpression *NewExpr) { 331 assert(NewExpr->hasAllLocationOps(getNumVariableLocationOps() + 332 NewValues.size()) && 333 "NewExpr for debug variable intrinsic does not reference every " 334 "location operand."); 335 assert(!is_contained(NewValues, nullptr) && "New values must be non-null"); 336 setExpression(NewExpr); 337 SmallVector<ValueAsMetadata *, 4> MDs; 338 for (auto *VMD : location_ops()) 339 MDs.push_back(getAsMetadata(VMD)); 340 for (auto *VMD : NewValues) 341 MDs.push_back(getAsMetadata(VMD)); 342 setRawLocation(DIArgList::get(getVariableLocationOp(0)->getContext(), MDs)); 343 } 344 345 void DPValue::setKillLocation() { 346 // TODO: When/if we remove duplicate values from DIArgLists, we don't need 347 // this set anymore. 348 SmallPtrSet<Value *, 4> RemovedValues; 349 for (Value *OldValue : location_ops()) { 350 if (!RemovedValues.insert(OldValue).second) 351 continue; 352 Value *Poison = PoisonValue::get(OldValue->getType()); 353 replaceVariableLocationOp(OldValue, Poison); 354 } 355 } 356 357 bool DPValue::isKillLocation() const { 358 return (getNumVariableLocationOps() == 0 && 359 !getExpression()->isComplex()) || 360 any_of(location_ops(), [](Value *V) { return isa<UndefValue>(V); }); 361 } 362 363 std::optional<uint64_t> DPValue::getFragmentSizeInBits() const { 364 if (auto Fragment = getExpression()->getFragmentInfo()) 365 return Fragment->SizeInBits; 366 return getVariable()->getSizeInBits(); 367 } 368 369 DbgRecord *DbgRecord::clone() const { 370 switch (RecordKind) { 371 case ValueKind: 372 return cast<DPValue>(this)->clone(); 373 case LabelKind: 374 return cast<DPLabel>(this)->clone(); 375 }; 376 llvm_unreachable("unsupported DbgRecord kind"); 377 } 378 379 DPValue *DPValue::clone() const { return new DPValue(*this); } 380 381 DPLabel *DPLabel::clone() const { 382 return new DPLabel(getLabel(), getDebugLoc()); 383 } 384 385 DbgVariableIntrinsic * 386 DPValue::createDebugIntrinsic(Module *M, Instruction *InsertBefore) const { 387 [[maybe_unused]] DICompileUnit *Unit = 388 getDebugLoc().get()->getScope()->getSubprogram()->getUnit(); 389 assert(M && Unit && 390 "Cannot clone from BasicBlock that is not part of a Module or " 391 "DICompileUnit!"); 392 LLVMContext &Context = getDebugLoc()->getContext(); 393 Function *IntrinsicFn; 394 395 // Work out what sort of intrinsic we're going to produce. 396 switch (getType()) { 397 case DPValue::LocationType::Declare: 398 IntrinsicFn = Intrinsic::getDeclaration(M, Intrinsic::dbg_declare); 399 break; 400 case DPValue::LocationType::Value: 401 IntrinsicFn = Intrinsic::getDeclaration(M, Intrinsic::dbg_value); 402 break; 403 case DPValue::LocationType::Assign: 404 IntrinsicFn = Intrinsic::getDeclaration(M, Intrinsic::dbg_assign); 405 break; 406 case DPValue::LocationType::End: 407 case DPValue::LocationType::Any: 408 llvm_unreachable("Invalid LocationType"); 409 } 410 411 // Create the intrinsic from this DPValue's information, optionally insert 412 // into the target location. 413 DbgVariableIntrinsic *DVI; 414 assert(getRawLocation() && "DPValue's RawLocation should be non-null."); 415 if (isDbgAssign()) { 416 Value *AssignArgs[] = { 417 MetadataAsValue::get(Context, getRawLocation()), 418 MetadataAsValue::get(Context, getVariable()), 419 MetadataAsValue::get(Context, getExpression()), 420 MetadataAsValue::get(Context, getAssignID()), 421 MetadataAsValue::get(Context, getRawAddress()), 422 MetadataAsValue::get(Context, getAddressExpression())}; 423 DVI = cast<DbgVariableIntrinsic>(CallInst::Create( 424 IntrinsicFn->getFunctionType(), IntrinsicFn, AssignArgs)); 425 } else { 426 Value *Args[] = {MetadataAsValue::get(Context, getRawLocation()), 427 MetadataAsValue::get(Context, getVariable()), 428 MetadataAsValue::get(Context, getExpression())}; 429 DVI = cast<DbgVariableIntrinsic>( 430 CallInst::Create(IntrinsicFn->getFunctionType(), IntrinsicFn, Args)); 431 } 432 DVI->setTailCall(); 433 DVI->setDebugLoc(getDebugLoc()); 434 if (InsertBefore) 435 DVI->insertBefore(InsertBefore); 436 437 return DVI; 438 } 439 440 DbgLabelInst *DPLabel::createDebugIntrinsic(Module *M, 441 Instruction *InsertBefore) const { 442 auto *LabelFn = Intrinsic::getDeclaration(M, Intrinsic::dbg_label); 443 Value *Args[] = { 444 MetadataAsValue::get(getDebugLoc()->getContext(), getLabel())}; 445 DbgLabelInst *DbgLabel = cast<DbgLabelInst>( 446 CallInst::Create(LabelFn->getFunctionType(), LabelFn, Args)); 447 DbgLabel->setTailCall(); 448 DbgLabel->setDebugLoc(getDebugLoc()); 449 if (InsertBefore) 450 DbgLabel->insertBefore(InsertBefore); 451 return DbgLabel; 452 } 453 454 Value *DPValue::getAddress() const { 455 auto *MD = getRawAddress(); 456 if (auto *V = dyn_cast<ValueAsMetadata>(MD)) 457 return V->getValue(); 458 459 // When the value goes to null, it gets replaced by an empty MDNode. 460 assert(!cast<MDNode>(MD)->getNumOperands() && "Expected an empty MDNode"); 461 return nullptr; 462 } 463 464 DIAssignID *DPValue::getAssignID() const { 465 return cast<DIAssignID>(DebugValues[2]); 466 } 467 468 void DPValue::setAssignId(DIAssignID *New) { resetDebugValue(2, New); } 469 470 void DPValue::setKillAddress() { 471 resetDebugValue( 472 1, ValueAsMetadata::get(UndefValue::get(getAddress()->getType()))); 473 } 474 475 bool DPValue::isKillAddress() const { 476 Value *Addr = getAddress(); 477 return !Addr || isa<UndefValue>(Addr); 478 } 479 480 const Instruction *DbgRecord::getInstruction() const { 481 return Marker->MarkedInstr; 482 } 483 484 const BasicBlock *DbgRecord::getParent() const { 485 return Marker->MarkedInstr->getParent(); 486 } 487 488 BasicBlock *DbgRecord::getParent() { return Marker->MarkedInstr->getParent(); } 489 490 BasicBlock *DbgRecord::getBlock() { return Marker->getParent(); } 491 492 const BasicBlock *DbgRecord::getBlock() const { return Marker->getParent(); } 493 494 Function *DbgRecord::getFunction() { return getBlock()->getParent(); } 495 496 const Function *DbgRecord::getFunction() const { 497 return getBlock()->getParent(); 498 } 499 500 Module *DbgRecord::getModule() { return getFunction()->getParent(); } 501 502 const Module *DbgRecord::getModule() const { 503 return getFunction()->getParent(); 504 } 505 506 LLVMContext &DbgRecord::getContext() { return getBlock()->getContext(); } 507 508 const LLVMContext &DbgRecord::getContext() const { 509 return getBlock()->getContext(); 510 } 511 512 void DbgRecord::insertBefore(DbgRecord *InsertBefore) { 513 assert(!getMarker() && 514 "Cannot insert a DbgRecord that is already has a DPMarker!"); 515 assert(InsertBefore->getMarker() && 516 "Cannot insert a DbgRecord before a DbgRecord that does not have a " 517 "DPMarker!"); 518 InsertBefore->getMarker()->insertDbgRecord(this, InsertBefore); 519 } 520 void DbgRecord::insertAfter(DbgRecord *InsertAfter) { 521 assert(!getMarker() && 522 "Cannot insert a DbgRecord that is already has a DPMarker!"); 523 assert(InsertAfter->getMarker() && 524 "Cannot insert a DbgRecord after a DbgRecord that does not have a " 525 "DPMarker!"); 526 InsertAfter->getMarker()->insertDbgRecordAfter(this, InsertAfter); 527 } 528 void DbgRecord::moveBefore(DbgRecord *MoveBefore) { 529 assert(getMarker() && 530 "Canot move a DbgRecord that does not currently have a DPMarker!"); 531 removeFromParent(); 532 insertBefore(MoveBefore); 533 } 534 void DbgRecord::moveAfter(DbgRecord *MoveAfter) { 535 assert(getMarker() && 536 "Canot move a DbgRecord that does not currently have a DPMarker!"); 537 removeFromParent(); 538 insertAfter(MoveAfter); 539 } 540 541 /////////////////////////////////////////////////////////////////////////////// 542 543 // An empty, global, DPMarker for the purpose of describing empty ranges of 544 // DbgRecords. 545 DPMarker DPMarker::EmptyDPMarker; 546 547 void DPMarker::dropDbgRecords() { 548 while (!StoredDbgRecords.empty()) { 549 auto It = StoredDbgRecords.begin(); 550 DbgRecord *DR = &*It; 551 StoredDbgRecords.erase(It); 552 DR->deleteRecord(); 553 } 554 } 555 556 void DPMarker::dropOneDbgRecord(DbgRecord *DR) { 557 assert(DR->getMarker() == this); 558 StoredDbgRecords.erase(DR->getIterator()); 559 DR->deleteRecord(); 560 } 561 562 const BasicBlock *DPMarker::getParent() const { 563 return MarkedInstr->getParent(); 564 } 565 566 BasicBlock *DPMarker::getParent() { return MarkedInstr->getParent(); } 567 568 void DPMarker::removeMarker() { 569 // Are there any DbgRecords in this DPMarker? If not, nothing to preserve. 570 Instruction *Owner = MarkedInstr; 571 if (StoredDbgRecords.empty()) { 572 eraseFromParent(); 573 Owner->DbgMarker = nullptr; 574 return; 575 } 576 577 // The attached DbgRecords need to be preserved; attach them to the next 578 // instruction. If there isn't a next instruction, put them on the 579 // "trailing" list. 580 DPMarker *NextMarker = Owner->getParent()->getNextMarker(Owner); 581 if (NextMarker) { 582 NextMarker->absorbDebugValues(*this, true); 583 eraseFromParent(); 584 } else { 585 // We can avoid a deallocation -- just store this marker onto the next 586 // instruction. Unless we're at the end of the block, in which case this 587 // marker becomes the trailing marker of a degenerate block. 588 BasicBlock::iterator NextIt = std::next(Owner->getIterator()); 589 if (NextIt == getParent()->end()) { 590 getParent()->setTrailingDbgRecords(this); 591 MarkedInstr = nullptr; 592 } else { 593 NextIt->DbgMarker = this; 594 MarkedInstr = &*NextIt; 595 } 596 } 597 Owner->DbgMarker = nullptr; 598 } 599 600 void DPMarker::removeFromParent() { 601 MarkedInstr->DbgMarker = nullptr; 602 MarkedInstr = nullptr; 603 } 604 605 void DPMarker::eraseFromParent() { 606 if (MarkedInstr) 607 removeFromParent(); 608 dropDbgRecords(); 609 delete this; 610 } 611 612 iterator_range<DbgRecord::self_iterator> DPMarker::getDbgRecordRange() { 613 return make_range(StoredDbgRecords.begin(), StoredDbgRecords.end()); 614 } 615 iterator_range<DbgRecord::const_self_iterator> 616 DPMarker::getDbgRecordRange() const { 617 return make_range(StoredDbgRecords.begin(), StoredDbgRecords.end()); 618 } 619 620 void DbgRecord::removeFromParent() { 621 getMarker()->StoredDbgRecords.erase(getIterator()); 622 Marker = nullptr; 623 } 624 625 void DbgRecord::eraseFromParent() { 626 removeFromParent(); 627 deleteRecord(); 628 } 629 630 void DPMarker::insertDbgRecord(DbgRecord *New, bool InsertAtHead) { 631 auto It = InsertAtHead ? StoredDbgRecords.begin() : StoredDbgRecords.end(); 632 StoredDbgRecords.insert(It, *New); 633 New->setMarker(this); 634 } 635 void DPMarker::insertDbgRecord(DbgRecord *New, DbgRecord *InsertBefore) { 636 assert(InsertBefore->getMarker() == this && 637 "DbgRecord 'InsertBefore' must be contained in this DPMarker!"); 638 StoredDbgRecords.insert(InsertBefore->getIterator(), *New); 639 New->setMarker(this); 640 } 641 void DPMarker::insertDbgRecordAfter(DbgRecord *New, DbgRecord *InsertAfter) { 642 assert(InsertAfter->getMarker() == this && 643 "DbgRecord 'InsertAfter' must be contained in this DPMarker!"); 644 StoredDbgRecords.insert(++(InsertAfter->getIterator()), *New); 645 New->setMarker(this); 646 } 647 648 void DPMarker::absorbDebugValues(DPMarker &Src, bool InsertAtHead) { 649 auto It = InsertAtHead ? StoredDbgRecords.begin() : StoredDbgRecords.end(); 650 for (DbgRecord &DPV : Src.StoredDbgRecords) 651 DPV.setMarker(this); 652 653 StoredDbgRecords.splice(It, Src.StoredDbgRecords); 654 } 655 656 void DPMarker::absorbDebugValues(iterator_range<DbgRecord::self_iterator> Range, 657 DPMarker &Src, bool InsertAtHead) { 658 for (DbgRecord &DR : Range) 659 DR.setMarker(this); 660 661 auto InsertPos = 662 (InsertAtHead) ? StoredDbgRecords.begin() : StoredDbgRecords.end(); 663 664 StoredDbgRecords.splice(InsertPos, Src.StoredDbgRecords, Range.begin(), 665 Range.end()); 666 } 667 668 iterator_range<simple_ilist<DbgRecord>::iterator> DPMarker::cloneDebugInfoFrom( 669 DPMarker *From, std::optional<simple_ilist<DbgRecord>::iterator> from_here, 670 bool InsertAtHead) { 671 DbgRecord *First = nullptr; 672 // Work out what range of DbgRecords to clone: normally all the contents of 673 // the "From" marker, optionally we can start from the from_here position down 674 // to end(). 675 auto Range = 676 make_range(From->StoredDbgRecords.begin(), From->StoredDbgRecords.end()); 677 if (from_here.has_value()) 678 Range = make_range(*from_here, From->StoredDbgRecords.end()); 679 680 // Clone each DPValue and insert into StoreDPValues; optionally place them at 681 // the start or the end of the list. 682 auto Pos = (InsertAtHead) ? StoredDbgRecords.begin() : StoredDbgRecords.end(); 683 for (DbgRecord &DR : Range) { 684 DbgRecord *New = DR.clone(); 685 New->setMarker(this); 686 StoredDbgRecords.insert(Pos, *New); 687 if (!First) 688 First = New; 689 } 690 691 if (!First) 692 return {StoredDbgRecords.end(), StoredDbgRecords.end()}; 693 694 if (InsertAtHead) 695 // If InsertAtHead is set, we cloned a range onto the front of of the 696 // StoredDbgRecords collection, return that range. 697 return {StoredDbgRecords.begin(), Pos}; 698 else 699 // We inserted a block at the end, return that range. 700 return {First->getIterator(), StoredDbgRecords.end()}; 701 } 702 703 } // end namespace llvm 704