Index: include/llvm/CodeGen/MachineFunction.h =================================================================== --- include/llvm/CodeGen/MachineFunction.h +++ include/llvm/CodeGen/MachineFunction.h @@ -970,6 +970,10 @@ return CallSitesInfo; } + /// Update call sites info by deleting entry for \p Old call instruction. + /// If \p New is present then transfer \p Old call info to it. + void updateCallSiteInfo(const MachineInstr *Old, + const MachineInstr *New = nullptr); }; //===--------------------------------------------------------------------===// Index: lib/CodeGen/InlineSpiller.cpp =================================================================== --- lib/CodeGen/InlineSpiller.cpp +++ lib/CodeGen/InlineSpiller.cpp @@ -868,6 +868,8 @@ HSpiller.rmFromMergeableSpills(*MI, FI)) --NumSpills; LIS.ReplaceMachineInstrInMaps(*MI, *FoldMI); + if (MI->isCall()) + MI->getMF()->updateCallSiteInfo(MI, FoldMI); MI->eraseFromParent(); // Insert any new instructions other than FoldMI into the LIS maps. Index: lib/CodeGen/MachineFunction.cpp =================================================================== --- lib/CodeGen/MachineFunction.cpp +++ lib/CodeGen/MachineFunction.cpp @@ -363,6 +363,11 @@ /// ~MachineInstr() destructor must be empty. void MachineFunction::DeleteMachineInstr(MachineInstr *MI) { +#ifndef NDEBUG + assert((!MI->isCall(MachineInstr::IgnoreBundle) || + CallSitesInfo.find(MI) == CallSitesInfo.end()) && + "Call site info is not updated!"); +#endif // Strip it for parts. The operand array and the MI object itself are // independently recyclable. if (MI->Operands) @@ -817,6 +822,23 @@ CodeViewHeapAllocSites.push_back(std::make_tuple(BeginLabel, EndLabel, DI)); } +void MachineFunction::updateCallSiteInfo(const MachineInstr *Old, + const MachineInstr *New) { + if (!Target.Options.EnableDebugEntryValues) + return; + + assert(Old->isCall() && (!New || New->isCall())); + CallSiteInfoMap::iterator CSIt = CallSitesInfo.find(Old); + if (CSIt == CallSitesInfo.end()) + return; + CallSiteInfo CSInfo; + if (New) + CSInfo = std::move(CSIt->second); + CallSitesInfo.erase(CSIt); + if (New) + CallSitesInfo[New] = CSInfo; +} + /// \} //===----------------------------------------------------------------------===// Index: lib/CodeGen/MachineOutliner.cpp =================================================================== --- lib/CodeGen/MachineOutliner.cpp +++ lib/CodeGen/MachineOutliner.cpp @@ -1245,8 +1245,9 @@ if (MBB.getParent()->getProperties().hasProperty( MachineFunctionProperties::Property::TracksLiveness)) { // Helper lambda for adding implicit def operands to the call - // instruction. - auto CopyDefs = [&CallInst](MachineInstr &MI) { + // instruction. It also updates call site information for moved + // code. + auto CopyDefsAndUpdateCalls = [&CallInst](MachineInstr &MI) { for (MachineOperand &MOP : MI.operands()) { // Skip over anything that isn't a register. if (!MOP.isReg()) @@ -1258,13 +1259,16 @@ MOP.getReg(), true, /* isDef = true */ true /* isImp = true */)); } + if (MI.isCall()) + MI.getMF()->updateCallSiteInfo(&MI); }; // Copy over the defs in the outlined range. // First inst in outlined range <-- Anything that's defined in this // ... .. range has to be added as an // implicit Last inst in outlined range <-- def to the call - // instruction. - std::for_each(CallInst, std::next(EndIt), CopyDefs); + // instruction. Also remove call site information for outlined block + // of code. + std::for_each(CallInst, std::next(EndIt), CopyDefsAndUpdateCalls); } // Erase from the point after where the call was inserted up to, and Index: lib/CodeGen/PeepholeOptimizer.cpp =================================================================== --- lib/CodeGen/PeepholeOptimizer.cpp +++ lib/CodeGen/PeepholeOptimizer.cpp @@ -1777,6 +1777,8 @@ LocalMIs.erase(MI); LocalMIs.erase(DefMI); LocalMIs.insert(FoldMI); + if (MI->isCall()) + MI->getMF()->updateCallSiteInfo(MI, FoldMI); MI->eraseFromParent(); DefMI->eraseFromParent(); MRI->markUsesInDebugValueAsUndef(FoldedReg); Index: lib/CodeGen/TargetInstrInfo.cpp =================================================================== --- lib/CodeGen/TargetInstrInfo.cpp +++ lib/CodeGen/TargetInstrInfo.cpp @@ -137,8 +137,14 @@ // Save off the debug loc before erasing the instruction. DebugLoc DL = Tail->getDebugLoc(); - // Remove all the dead instructions from the end of MBB. - MBB->erase(Tail, MBB->end()); + // Update call site info and remove all the dead instructions + // from the end of MBB. + while (Tail != MBB->end()) { + auto MI = Tail++; + if (MI->isCall()) + MBB->getParent()->updateCallSiteInfo(&*MI); + MBB->erase(MI); + } // If MBB isn't immediately before MBB, insert a branch to it. if (++MachineFunction::iterator(MBB) != MachineFunction::iterator(NewDest)) Index: lib/CodeGen/XRayInstrumentation.cpp =================================================================== --- lib/CodeGen/XRayInstrumentation.cpp +++ lib/CodeGen/XRayInstrumentation.cpp @@ -110,6 +110,8 @@ for (auto &MO : T.operands()) MIB.add(MO); Terminators.push_back(&T); + if (T.isCall()) + MF.updateCallSiteInfo(&T); } } } Index: lib/Target/X86/X86ExpandPseudo.cpp =================================================================== --- lib/Target/X86/X86ExpandPseudo.cpp +++ lib/Target/X86/X86ExpandPseudo.cpp @@ -270,6 +270,7 @@ MachineInstr &NewMI = *std::prev(MBBI); NewMI.copyImplicitOps(*MBBI->getParent()->getParent(), *MBBI); + MBB.getParent()->updateCallSiteInfo(&*MBBI, &NewMI); // Delete the pseudo instruction TCRETURN. MBB.erase(MBBI);