@@ -256,15 +256,20 @@ class MergeFunctions {
256
256
257
257
// / Fill PDIUnrelatedWL with instructions from the entry block that are
258
258
// / unrelated to parameter related debug info.
259
+ // / \param PDPVUnrelatedWL The equivalent non-intrinsic debug records.
259
260
void filterInstsUnrelatedToPDI (BasicBlock *GEntryBlock,
260
- std::vector<Instruction *> &PDIUnrelatedWL);
261
+ std::vector<Instruction *> &PDIUnrelatedWL,
262
+ std::vector<DPValue *> &PDPVUnrelatedWL);
261
263
262
264
// / Erase the rest of the CFG (i.e. barring the entry block).
263
265
void eraseTail (Function *G);
264
266
265
267
// / Erase the instructions in PDIUnrelatedWL as they are unrelated to the
266
268
// / parameter debug info, from the entry block.
267
- void eraseInstsUnrelatedToPDI (std::vector<Instruction *> &PDIUnrelatedWL);
269
+ // / \param PDPVUnrelatedWL contains the equivalent set of non-instruction
270
+ // / debug-info records.
271
+ void eraseInstsUnrelatedToPDI (std::vector<Instruction *> &PDIUnrelatedWL,
272
+ std::vector<DPValue *> &PDPVUnrelatedWL);
268
273
269
274
// / Replace G with a simple tail call to bitcast(F). Also (unless
270
275
// / MergeFunctionsPDI holds) replace direct uses of G with bitcast(F),
@@ -506,7 +511,8 @@ static Value *createCast(IRBuilder<> &Builder, Value *V, Type *DestTy) {
506
511
// Erase the instructions in PDIUnrelatedWL as they are unrelated to the
507
512
// parameter debug info, from the entry block.
508
513
void MergeFunctions::eraseInstsUnrelatedToPDI (
509
- std::vector<Instruction *> &PDIUnrelatedWL) {
514
+ std::vector<Instruction *> &PDIUnrelatedWL,
515
+ std::vector<DPValue *> &PDPVUnrelatedWL) {
510
516
LLVM_DEBUG (
511
517
dbgs () << " Erasing instructions (in reverse order of appearance in "
512
518
" entry block) unrelated to parameter debug info from entry "
@@ -519,6 +525,16 @@ void MergeFunctions::eraseInstsUnrelatedToPDI(
519
525
I->eraseFromParent ();
520
526
PDIUnrelatedWL.pop_back ();
521
527
}
528
+
529
+ while (!PDPVUnrelatedWL.empty ()) {
530
+ DPValue *DPV = PDPVUnrelatedWL.back ();
531
+ LLVM_DEBUG (dbgs () << " Deleting DPValue " );
532
+ LLVM_DEBUG (DPV->print (dbgs ()));
533
+ LLVM_DEBUG (dbgs () << " \n " );
534
+ DPV->eraseFromParent ();
535
+ PDPVUnrelatedWL.pop_back ();
536
+ }
537
+
522
538
LLVM_DEBUG (dbgs () << " } // Done erasing instructions unrelated to parameter "
523
539
" debug info from entry block. \n " );
524
540
}
@@ -547,75 +563,99 @@ void MergeFunctions::eraseTail(Function *G) {
547
563
// The rest are unrelated to debug info for the parameters; fill up
548
564
// PDIUnrelatedWL with such instructions.
549
565
void MergeFunctions::filterInstsUnrelatedToPDI (
550
- BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL) {
566
+ BasicBlock *GEntryBlock, std::vector<Instruction *> &PDIUnrelatedWL,
567
+ std::vector<DPValue *> &PDPVUnrelatedWL) {
551
568
std::set<Instruction *> PDIRelated;
552
- for (BasicBlock::iterator BI = GEntryBlock->begin (), BIE = GEntryBlock->end ();
553
- BI != BIE; ++BI) {
554
- if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) {
555
- LLVM_DEBUG (dbgs () << " Deciding: " );
556
- LLVM_DEBUG (BI->print (dbgs ()));
569
+ std::set<DPValue *> PDPVRelated;
570
+
571
+ // Work out whether a dbg.value intrinsic or an equivalent DPValue is a
572
+ // parameter to be preserved.
573
+ auto ExamineDbgValue = [](auto *DbgVal, auto &Container) {
574
+ LLVM_DEBUG (dbgs () << " Deciding: " );
575
+ LLVM_DEBUG (DbgVal->print (dbgs ()));
576
+ LLVM_DEBUG (dbgs () << " \n " );
577
+ DILocalVariable *DILocVar = DbgVal->getVariable ();
578
+ if (DILocVar->isParameter ()) {
579
+ LLVM_DEBUG (dbgs () << " Include (parameter): " );
580
+ LLVM_DEBUG (DbgVal->print (dbgs ()));
557
581
LLVM_DEBUG (dbgs () << " \n " );
558
- DILocalVariable *DILocVar = DVI->getVariable ();
559
- if (DILocVar->isParameter ()) {
560
- LLVM_DEBUG (dbgs () << " Include (parameter): " );
561
- LLVM_DEBUG (BI->print (dbgs ()));
562
- LLVM_DEBUG (dbgs () << " \n " );
563
- PDIRelated.insert (&*BI);
564
- } else {
565
- LLVM_DEBUG (dbgs () << " Delete (!parameter): " );
566
- LLVM_DEBUG (BI->print (dbgs ()));
567
- LLVM_DEBUG (dbgs () << " \n " );
568
- }
569
- } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) {
570
- LLVM_DEBUG (dbgs () << " Deciding: " );
571
- LLVM_DEBUG (BI->print (dbgs ()));
582
+ Container.insert (DbgVal);
583
+ } else {
584
+ LLVM_DEBUG (dbgs () << " Delete (!parameter): " );
585
+ LLVM_DEBUG (DbgVal->print (dbgs ()));
572
586
LLVM_DEBUG (dbgs () << " \n " );
573
- DILocalVariable *DILocVar = DDI->getVariable ();
574
- if (DILocVar->isParameter ()) {
575
- LLVM_DEBUG (dbgs () << " Parameter: " );
576
- LLVM_DEBUG (DILocVar->print (dbgs ()));
577
- AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DDI->getAddress ());
578
- if (AI) {
579
- LLVM_DEBUG (dbgs () << " Processing alloca users: " );
580
- LLVM_DEBUG (dbgs () << " \n " );
581
- for (User *U : AI->users ()) {
582
- if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
583
- if (Value *Arg = SI->getValueOperand ()) {
584
- if (isa<Argument>(Arg)) {
585
- LLVM_DEBUG (dbgs () << " Include: " );
586
- LLVM_DEBUG (AI->print (dbgs ()));
587
- LLVM_DEBUG (dbgs () << " \n " );
588
- PDIRelated.insert (AI);
589
- LLVM_DEBUG (dbgs () << " Include (parameter): " );
590
- LLVM_DEBUG (SI->print (dbgs ()));
591
- LLVM_DEBUG (dbgs () << " \n " );
592
- PDIRelated.insert (SI);
593
- LLVM_DEBUG (dbgs () << " Include: " );
594
- LLVM_DEBUG (BI->print (dbgs ()));
595
- LLVM_DEBUG (dbgs () << " \n " );
596
- PDIRelated.insert (&*BI);
597
- } else {
598
- LLVM_DEBUG (dbgs () << " Delete (!parameter): " );
599
- LLVM_DEBUG (SI->print (dbgs ()));
600
- LLVM_DEBUG (dbgs () << " \n " );
601
- }
587
+ }
588
+ };
589
+
590
+ auto ExamineDbgDeclare = [&PDIRelated](auto *DbgDecl, auto &Container) {
591
+ LLVM_DEBUG (dbgs () << " Deciding: " );
592
+ LLVM_DEBUG (DbgDecl->print (dbgs ()));
593
+ LLVM_DEBUG (dbgs () << " \n " );
594
+ DILocalVariable *DILocVar = DbgDecl->getVariable ();
595
+ if (DILocVar->isParameter ()) {
596
+ LLVM_DEBUG (dbgs () << " Parameter: " );
597
+ LLVM_DEBUG (DILocVar->print (dbgs ()));
598
+ AllocaInst *AI = dyn_cast_or_null<AllocaInst>(DbgDecl->getAddress ());
599
+ if (AI) {
600
+ LLVM_DEBUG (dbgs () << " Processing alloca users: " );
601
+ LLVM_DEBUG (dbgs () << " \n " );
602
+ for (User *U : AI->users ()) {
603
+ if (StoreInst *SI = dyn_cast<StoreInst>(U)) {
604
+ if (Value *Arg = SI->getValueOperand ()) {
605
+ if (isa<Argument>(Arg)) {
606
+ LLVM_DEBUG (dbgs () << " Include: " );
607
+ LLVM_DEBUG (AI->print (dbgs ()));
608
+ LLVM_DEBUG (dbgs () << " \n " );
609
+ PDIRelated.insert (AI);
610
+ LLVM_DEBUG (dbgs () << " Include (parameter): " );
611
+ LLVM_DEBUG (SI->print (dbgs ()));
612
+ LLVM_DEBUG (dbgs () << " \n " );
613
+ PDIRelated.insert (SI);
614
+ LLVM_DEBUG (dbgs () << " Include: " );
615
+ LLVM_DEBUG (DbgDecl->print (dbgs ()));
616
+ LLVM_DEBUG (dbgs () << " \n " );
617
+ Container.insert (DbgDecl);
618
+ } else {
619
+ LLVM_DEBUG (dbgs () << " Delete (!parameter): " );
620
+ LLVM_DEBUG (SI->print (dbgs ()));
621
+ LLVM_DEBUG (dbgs () << " \n " );
602
622
}
603
- } else {
604
- LLVM_DEBUG (dbgs () << " Defer: " );
605
- LLVM_DEBUG (U->print (dbgs ()));
606
- LLVM_DEBUG (dbgs () << " \n " );
607
623
}
624
+ } else {
625
+ LLVM_DEBUG (dbgs () << " Defer: " );
626
+ LLVM_DEBUG (U->print (dbgs ()));
627
+ LLVM_DEBUG (dbgs () << " \n " );
608
628
}
609
- } else {
610
- LLVM_DEBUG (dbgs () << " Delete (alloca NULL): " );
611
- LLVM_DEBUG (BI->print (dbgs ()));
612
- LLVM_DEBUG (dbgs () << " \n " );
613
629
}
614
630
} else {
615
- LLVM_DEBUG (dbgs () << " Delete (!parameter ): " );
616
- LLVM_DEBUG (BI ->print (dbgs ()));
631
+ LLVM_DEBUG (dbgs () << " Delete (alloca NULL ): " );
632
+ LLVM_DEBUG (DbgDecl ->print (dbgs ()));
617
633
LLVM_DEBUG (dbgs () << " \n " );
618
634
}
635
+ } else {
636
+ LLVM_DEBUG (dbgs () << " Delete (!parameter): " );
637
+ LLVM_DEBUG (DbgDecl->print (dbgs ()));
638
+ LLVM_DEBUG (dbgs () << " \n " );
639
+ }
640
+ };
641
+
642
+ for (BasicBlock::iterator BI = GEntryBlock->begin (), BIE = GEntryBlock->end ();
643
+ BI != BIE; ++BI) {
644
+ // Examine DPValues as they happen "before" the instruction. Are they
645
+ // connected to parameters?
646
+ for (DPValue &DPV : BI->getDbgValueRange ()) {
647
+ if (DPV.isDbgValue () || DPV.isDbgAssign ()) {
648
+ ExamineDbgValue (&DPV, PDPVRelated);
649
+ } else {
650
+ assert (DPV.isDbgDeclare ());
651
+ ExamineDbgDeclare (&DPV, PDPVRelated);
652
+ }
653
+ }
654
+
655
+ if (auto *DVI = dyn_cast<DbgValueInst>(&*BI)) {
656
+ ExamineDbgValue (DVI, PDIRelated);
657
+ } else if (auto *DDI = dyn_cast<DbgDeclareInst>(&*BI)) {
658
+ ExamineDbgDeclare (DDI, PDIRelated);
619
659
} else if (BI->isTerminator () && &*BI == GEntryBlock->getTerminator ()) {
620
660
LLVM_DEBUG (dbgs () << " Will Include Terminator: " );
621
661
LLVM_DEBUG (BI->print (dbgs ()));
@@ -630,17 +670,25 @@ void MergeFunctions::filterInstsUnrelatedToPDI(
630
670
LLVM_DEBUG (
631
671
dbgs ()
632
672
<< " Report parameter debug info related/related instructions: {\n " );
633
- for (Instruction &I : *GEntryBlock) {
634
- if (PDIRelated.find (&I) == PDIRelated.end ()) {
673
+
674
+ auto IsPDIRelated = [](auto *Rec, auto &Container, auto &UnrelatedCont) {
675
+ if (Container.find (Rec) == Container.end ()) {
635
676
LLVM_DEBUG (dbgs () << " !PDIRelated: " );
636
- LLVM_DEBUG (I. print (dbgs ()));
677
+ LLVM_DEBUG (Rec-> print (dbgs ()));
637
678
LLVM_DEBUG (dbgs () << " \n " );
638
- PDIUnrelatedWL .push_back (&I );
679
+ UnrelatedCont .push_back (Rec );
639
680
} else {
640
681
LLVM_DEBUG (dbgs () << " PDIRelated: " );
641
- LLVM_DEBUG (I. print (dbgs ()));
682
+ LLVM_DEBUG (Rec-> print (dbgs ()));
642
683
LLVM_DEBUG (dbgs () << " \n " );
643
684
}
685
+ };
686
+
687
+ // Collect the set of unrelated instructions and debug records.
688
+ for (Instruction &I : *GEntryBlock) {
689
+ for (auto &DPV : I.getDbgValueRange ())
690
+ IsPDIRelated (&DPV, PDPVRelated, PDPVUnrelatedWL);
691
+ IsPDIRelated (&I, PDIRelated, PDIUnrelatedWL);
644
692
}
645
693
LLVM_DEBUG (dbgs () << " }\n " );
646
694
}
@@ -680,6 +728,7 @@ static void copyMetadataIfPresent(Function *From, Function *To, StringRef Key) {
680
728
void MergeFunctions::writeThunk (Function *F, Function *G) {
681
729
BasicBlock *GEntryBlock = nullptr ;
682
730
std::vector<Instruction *> PDIUnrelatedWL;
731
+ std::vector<DPValue *> PDPVUnrelatedWL;
683
732
BasicBlock *BB = nullptr ;
684
733
Function *NewG = nullptr ;
685
734
if (MergeFunctionsPDI) {
@@ -691,7 +740,7 @@ void MergeFunctions::writeThunk(Function *F, Function *G) {
691
740
dbgs () << " writeThunk: (MergeFunctionsPDI) filter parameter related "
692
741
" debug info for "
693
742
<< G->getName () << " () {\n " );
694
- filterInstsUnrelatedToPDI (GEntryBlock, PDIUnrelatedWL);
743
+ filterInstsUnrelatedToPDI (GEntryBlock, PDIUnrelatedWL, PDPVUnrelatedWL );
695
744
GEntryBlock->getTerminator ()->eraseFromParent ();
696
745
BB = GEntryBlock;
697
746
} else {
@@ -740,7 +789,7 @@ void MergeFunctions::writeThunk(Function *F, Function *G) {
740
789
<< G->getName () << " ()\n " );
741
790
}
742
791
eraseTail (G);
743
- eraseInstsUnrelatedToPDI (PDIUnrelatedWL);
792
+ eraseInstsUnrelatedToPDI (PDIUnrelatedWL, PDPVUnrelatedWL );
744
793
LLVM_DEBUG (
745
794
dbgs () << " } // End of parameter related debug info filtering for: "
746
795
<< G->getName () << " ()\n " );
0 commit comments