@@ -410,6 +410,10 @@ static bool replaceFoldableUses(Instruction *Cond, Value *ToVal,
410410 if (Cond->getParent () == KnownAtEndOfBB)
411411 Changed |= replaceNonLocalUsesWith (Cond, ToVal);
412412 for (Instruction &I : reverse (*KnownAtEndOfBB)) {
413+ // Replace any debug-info record users of Cond with ToVal.
414+ for (DPValue &DPV : I.getDbgValueRange ())
415+ DPV.replaceVariableLocationOp (Cond, ToVal, true );
416+
413417 // Reached the Cond whose uses we are trying to replace, so there are no
414418 // more uses.
415419 if (&I == Cond)
@@ -1961,6 +1965,7 @@ void JumpThreadingPass::updateSSA(
19611965 SSAUpdater SSAUpdate;
19621966 SmallVector<Use *, 16 > UsesToRename;
19631967 SmallVector<DbgValueInst *, 4 > DbgValues;
1968+ SmallVector<DPValue *, 4 > DPValues;
19641969
19651970 for (Instruction &I : *BB) {
19661971 // Scan all uses of this instruction to see if it is used outside of its
@@ -1977,10 +1982,13 @@ void JumpThreadingPass::updateSSA(
19771982 }
19781983
19791984 // Find debug values outside of the block
1980- findDbgValues (DbgValues, &I);
1985+ findDbgValues (DbgValues, &I, &DPValues );
19811986 llvm::erase_if (DbgValues, [&](const DbgValueInst *DbgVal) {
19821987 return DbgVal->getParent () == BB;
19831988 });
1989+ llvm::erase_if (DPValues, [&](const DPValue *DPVal) {
1990+ return DPVal->getParent () == BB;
1991+ });
19841992
19851993 // If there are no uses outside the block, we're done with this instruction.
19861994 if (UsesToRename.empty () && DbgValues.empty ())
@@ -1996,9 +2004,11 @@ void JumpThreadingPass::updateSSA(
19962004
19972005 while (!UsesToRename.empty ())
19982006 SSAUpdate.RewriteUse (*UsesToRename.pop_back_val ());
1999- if (!DbgValues.empty ()) {
2007+ if (!DbgValues.empty () || !DPValues. empty () ) {
20002008 SSAUpdate.UpdateDebugValues (&I, DbgValues);
2009+ SSAUpdate.UpdateDebugValues (&I, DPValues);
20012010 DbgValues.clear ();
2011+ DPValues.clear ();
20022012 }
20032013
20042014 LLVM_DEBUG (dbgs () << " \n " );
@@ -2041,6 +2051,26 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
20412051 return true ;
20422052 };
20432053
2054+ // Duplicate implementation of the above dbg.value code, using DPValues
2055+ // instead.
2056+ auto RetargetDPValueIfPossible = [&](DPValue *DPV) {
2057+ SmallSet<std::pair<Value *, Value *>, 16 > OperandsToRemap;
2058+ for (auto *Op : DPV->location_ops ()) {
2059+ Instruction *OpInst = dyn_cast<Instruction>(Op);
2060+ if (!OpInst)
2061+ continue ;
2062+
2063+ auto I = ValueMapping.find (OpInst);
2064+ if (I != ValueMapping.end ())
2065+ OperandsToRemap.insert ({OpInst, I->second });
2066+ }
2067+
2068+ for (auto &[OldOp, MappedOp] : OperandsToRemap)
2069+ DPV->replaceVariableLocationOp (OldOp, MappedOp);
2070+ };
2071+
2072+ BasicBlock *RangeBB = BI->getParent ();
2073+
20442074 // Clone the phi nodes of the source basic block into NewBB. The resulting
20452075 // phi nodes are trivial since NewBB only has one predecessor, but SSAUpdater
20462076 // might need to rewrite the operand of the cloned phi.
@@ -2059,6 +2089,12 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
20592089 identifyNoAliasScopesToClone (BI, BE, NoAliasScopes);
20602090 cloneNoAliasScopes (NoAliasScopes, ClonedScopes, " thread" , Context);
20612091
2092+ auto CloneAndRemapDbgInfo = [&](Instruction *NewInst, Instruction *From) {
2093+ auto DPVRange = NewInst->cloneDebugInfoFrom (From);
2094+ for (DPValue &DPV : DPVRange)
2095+ RetargetDPValueIfPossible (&DPV);
2096+ };
2097+
20622098 // Clone the non-phi instructions of the source basic block into NewBB,
20632099 // keeping track of the mapping and using it to remap operands in the cloned
20642100 // instructions.
@@ -2069,6 +2105,8 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
20692105 ValueMapping[&*BI] = New;
20702106 adaptNoAliasScopes (New, ClonedScopes, Context);
20712107
2108+ CloneAndRemapDbgInfo (New, &*BI);
2109+
20722110 if (RetargetDbgValueIfPossible (New))
20732111 continue ;
20742112
@@ -2081,6 +2119,17 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
20812119 }
20822120 }
20832121
2122+ // There may be DPValues on the terminator, clone directly from marker
2123+ // to marker as there isn't an instruction there.
2124+ if (BE != RangeBB->end () && BE->hasDbgValues ()) {
2125+ // Dump them at the end.
2126+ DPMarker *Marker = RangeBB->getMarker (BE);
2127+ DPMarker *EndMarker = NewBB->createMarker (NewBB->end ());
2128+ auto DPVRange = EndMarker->cloneDebugInfoFrom (Marker, std::nullopt );
2129+ for (DPValue &DPV : DPVRange)
2130+ RetargetDPValueIfPossible (&DPV);
2131+ }
2132+
20842133 return ValueMapping;
20852134}
20862135
@@ -2666,13 +2715,18 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
26662715 if (!New->mayHaveSideEffects ()) {
26672716 New->eraseFromParent ();
26682717 New = nullptr ;
2718+ // Clone debug-info on the elided instruction to the destination
2719+ // position.
2720+ OldPredBranch->cloneDebugInfoFrom (&*BI, std::nullopt , true );
26692721 }
26702722 } else {
26712723 ValueMapping[&*BI] = New;
26722724 }
26732725 if (New) {
26742726 // Otherwise, insert the new instruction into the block.
26752727 New->setName (BI->getName ());
2728+ // Clone across any debug-info attached to the old instruction.
2729+ New->cloneDebugInfoFrom (&*BI);
26762730 // Update Dominance from simplified New instruction operands.
26772731 for (unsigned i = 0 , e = New->getNumOperands (); i != e; ++i)
26782732 if (BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand (i)))
0 commit comments