@@ -94,6 +94,7 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
9494 const char *NameSuffix, ClonedCodeInfo *CodeInfo,
9595 ValueMapTypeRemapper *TypeMapper,
9696 ValueMaterializer *Materializer) {
97+ NewFunc->setIsNewDbgInfoFormat (OldFunc->IsNewDbgInfoFormat );
9798 assert (NameSuffix && " NameSuffix cannot be null!" );
9899
99100#ifndef NDEBUG
@@ -271,9 +272,13 @@ void llvm::CloneFunctionInto(Function *NewFunc, const Function *OldFunc,
271272 BB = cast<BasicBlock>(VMap[&OldFunc->front ()])->getIterator (),
272273 BE = NewFunc->end ();
273274 BB != BE; ++BB)
274- // Loop over all instructions, fixing each one as we find it...
275- for (Instruction &II : *BB)
275+ // Loop over all instructions, fixing each one as we find it, and any
276+ // attached debug-info records.
277+ for (Instruction &II : *BB) {
276278 RemapInstruction (&II, VMap, RemapFlag, TypeMapper, Materializer);
279+ RemapDPValueRange (II.getModule (), II.getDbgValueRange (), VMap, RemapFlag,
280+ TypeMapper, Materializer);
281+ }
277282
278283 // Only update !llvm.dbg.cu for DifferentModule (not CloneModule). In the
279284 // same module, the compile unit will already be listed (or not). When
@@ -331,6 +336,7 @@ Function *llvm::CloneFunction(Function *F, ValueToValueMapTy &VMap,
331336 // Create the new function...
332337 Function *NewF = Function::Create (FTy, F->getLinkage (), F->getAddressSpace (),
333338 F->getName (), F->getParent ());
339+ NewF->setIsNewDbgInfoFormat (F->IsNewDbgInfoFormat );
334340
335341 // Loop over the arguments, copying the names of the mapped arguments over...
336342 Function::arg_iterator DestI = NewF->arg_begin ();
@@ -496,6 +502,22 @@ void PruningFunctionCloner::CloneBlock(
496502 bool hasCalls = false , hasDynamicAllocas = false , hasStaticAllocas = false ;
497503 bool hasMemProfMetadata = false ;
498504
505+ // Keep a cursor pointing at the last place we cloned debug-info records from.
506+ BasicBlock::const_iterator DbgCursor = StartingInst;
507+ auto CloneDbgRecordsToHere =
508+ [NewBB, &DbgCursor](Instruction *NewInst, BasicBlock::const_iterator II) {
509+ if (!NewBB->IsNewDbgInfoFormat )
510+ return ;
511+
512+ // Clone debug-info records onto this instruction. Iterate through any
513+ // source-instructions we've cloned and then subsequently optimised
514+ // away, so that their debug-info doesn't go missing.
515+ for (; DbgCursor != II; ++DbgCursor)
516+ NewInst->cloneDebugInfoFrom (&*DbgCursor, std::nullopt , false );
517+ NewInst->cloneDebugInfoFrom (&*II);
518+ DbgCursor = std::next (II);
519+ };
520+
499521 // Loop over all instructions, and copy them over, DCE'ing as we go. This
500522 // loop doesn't include the terminator.
501523 for (BasicBlock::const_iterator II = StartingInst, IE = --BB->end (); II != IE;
@@ -545,6 +567,8 @@ void PruningFunctionCloner::CloneBlock(
545567 hasMemProfMetadata |= II->hasMetadata (LLVMContext::MD_memprof);
546568 }
547569
570+ CloneDbgRecordsToHere (NewInst, II);
571+
548572 if (CodeInfo) {
549573 CodeInfo->OrigVMap [&*II] = NewInst;
550574 if (auto *CB = dyn_cast<CallBase>(&*II))
@@ -602,6 +626,9 @@ void PruningFunctionCloner::CloneBlock(
602626 if (OldTI->hasName ())
603627 NewInst->setName (OldTI->getName () + NameSuffix);
604628 NewInst->insertInto (NewBB, NewBB->end ());
629+
630+ CloneDbgRecordsToHere (NewInst, OldTI->getIterator ());
631+
605632 VMap[OldTI] = NewInst; // Add instruction map to value.
606633
607634 if (CodeInfo) {
@@ -613,6 +640,13 @@ void PruningFunctionCloner::CloneBlock(
613640
614641 // Recursively clone any reachable successor blocks.
615642 append_range (ToClone, successors (BB->getTerminator ()));
643+ } else {
644+ // If we didn't create a new terminator, clone DPValues from the old
645+ // terminator onto the new terminator.
646+ Instruction *NewInst = NewBB->getTerminator ();
647+ assert (NewInst);
648+
649+ CloneDbgRecordsToHere (NewInst, OldTI->getIterator ());
616650 }
617651
618652 if (CodeInfo) {
@@ -850,12 +884,22 @@ void llvm::CloneAndPruneIntoFromInst(Function *NewFunc, const Function *OldFunc,
850884 TypeMapper, Materializer);
851885 }
852886
887+ // Do the same for DPValues, touching all the instructions in the cloned
888+ // range of blocks.
889+ Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator ();
890+ for (BasicBlock &BB : make_range (Begin, NewFunc->end ())) {
891+ for (Instruction &I : BB) {
892+ RemapDPValueRange (I.getModule (), I.getDbgValueRange (), VMap,
893+ ModuleLevelChanges ? RF_None : RF_NoModuleLevelChanges,
894+ TypeMapper, Materializer);
895+ }
896+ }
897+
853898 // Simplify conditional branches and switches with a constant operand. We try
854899 // to prune these out when cloning, but if the simplification required
855900 // looking through PHI nodes, those are only available after forming the full
856901 // basic block. That may leave some here, and we still want to prune the dead
857902 // code as early as possible.
858- Function::iterator Begin = cast<BasicBlock>(VMap[StartingBB])->getIterator ();
859903 for (BasicBlock &BB : make_range (Begin, NewFunc->end ()))
860904 ConstantFoldTerminator (&BB);
861905
@@ -944,10 +988,15 @@ void llvm::CloneAndPruneFunctionInto(
944988void llvm::remapInstructionsInBlocks (ArrayRef<BasicBlock *> Blocks,
945989 ValueToValueMapTy &VMap) {
946990 // Rewrite the code to refer to itself.
947- for (auto *BB : Blocks)
948- for (auto &Inst : *BB)
991+ for (auto *BB : Blocks) {
992+ Module *M = BB->getModule ();
993+ for (auto &Inst : *BB) {
994+ RemapDPValueRange (Inst.getModule (), Inst.getDbgValueRange (), VMap,
995+ RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
949996 RemapInstruction (&Inst, VMap,
950997 RF_NoModuleLevelChanges | RF_IgnoreMissingLocals);
998+ }
999+ }
9511000}
9521001
9531002// / Clones a loop \p OrigLoop. Returns the loop and the blocks in \p
@@ -1071,6 +1120,7 @@ BasicBlock *llvm::DuplicateInstructionsInSplitBetween(
10711120 Instruction *New = BI->clone ();
10721121 New->setName (BI->getName ());
10731122 New->insertBefore (NewTerm);
1123+ New->cloneDebugInfoFrom (&*BI);
10741124 ValueMapping[&*BI] = New;
10751125
10761126 // Remap operands to patch up intra-block references.
0 commit comments