@@ -410,6 +410,10 @@ static bool replaceFoldableUses(Instruction *Cond, Value *ToVal,
410
410
if (Cond->getParent () == KnownAtEndOfBB)
411
411
Changed |= replaceNonLocalUsesWith (Cond, ToVal);
412
412
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
+
413
417
// Reached the Cond whose uses we are trying to replace, so there are no
414
418
// more uses.
415
419
if (&I == Cond)
@@ -1961,6 +1965,7 @@ void JumpThreadingPass::updateSSA(
1961
1965
SSAUpdater SSAUpdate;
1962
1966
SmallVector<Use *, 16 > UsesToRename;
1963
1967
SmallVector<DbgValueInst *, 4 > DbgValues;
1968
+ SmallVector<DPValue *, 4 > DPValues;
1964
1969
1965
1970
for (Instruction &I : *BB) {
1966
1971
// Scan all uses of this instruction to see if it is used outside of its
@@ -1977,10 +1982,13 @@ void JumpThreadingPass::updateSSA(
1977
1982
}
1978
1983
1979
1984
// Find debug values outside of the block
1980
- findDbgValues (DbgValues, &I);
1985
+ findDbgValues (DbgValues, &I, &DPValues );
1981
1986
llvm::erase_if (DbgValues, [&](const DbgValueInst *DbgVal) {
1982
1987
return DbgVal->getParent () == BB;
1983
1988
});
1989
+ llvm::erase_if (DPValues, [&](const DPValue *DPVal) {
1990
+ return DPVal->getParent () == BB;
1991
+ });
1984
1992
1985
1993
// If there are no uses outside the block, we're done with this instruction.
1986
1994
if (UsesToRename.empty () && DbgValues.empty ())
@@ -1996,9 +2004,11 @@ void JumpThreadingPass::updateSSA(
1996
2004
1997
2005
while (!UsesToRename.empty ())
1998
2006
SSAUpdate.RewriteUse (*UsesToRename.pop_back_val ());
1999
- if (!DbgValues.empty ()) {
2007
+ if (!DbgValues.empty () || !DPValues. empty () ) {
2000
2008
SSAUpdate.UpdateDebugValues (&I, DbgValues);
2009
+ SSAUpdate.UpdateDebugValues (&I, DPValues);
2001
2010
DbgValues.clear ();
2011
+ DPValues.clear ();
2002
2012
}
2003
2013
2004
2014
LLVM_DEBUG (dbgs () << " \n " );
@@ -2041,6 +2051,26 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
2041
2051
return true ;
2042
2052
};
2043
2053
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
+
2044
2074
// Clone the phi nodes of the source basic block into NewBB. The resulting
2045
2075
// phi nodes are trivial since NewBB only has one predecessor, but SSAUpdater
2046
2076
// might need to rewrite the operand of the cloned phi.
@@ -2059,6 +2089,12 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
2059
2089
identifyNoAliasScopesToClone (BI, BE, NoAliasScopes);
2060
2090
cloneNoAliasScopes (NoAliasScopes, ClonedScopes, " thread" , Context);
2061
2091
2092
+ auto CloneAndRemapDbgInfo = [&](Instruction *NewInst, Instruction *From) {
2093
+ auto DPVRange = NewInst->cloneDebugInfoFrom (From);
2094
+ for (DPValue &DPV : DPVRange)
2095
+ RetargetDPValueIfPossible (&DPV);
2096
+ };
2097
+
2062
2098
// Clone the non-phi instructions of the source basic block into NewBB,
2063
2099
// keeping track of the mapping and using it to remap operands in the cloned
2064
2100
// instructions.
@@ -2069,6 +2105,8 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
2069
2105
ValueMapping[&*BI] = New;
2070
2106
adaptNoAliasScopes (New, ClonedScopes, Context);
2071
2107
2108
+ CloneAndRemapDbgInfo (New, &*BI);
2109
+
2072
2110
if (RetargetDbgValueIfPossible (New))
2073
2111
continue ;
2074
2112
@@ -2081,6 +2119,17 @@ JumpThreadingPass::cloneInstructions(BasicBlock::iterator BI,
2081
2119
}
2082
2120
}
2083
2121
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
+
2084
2133
return ValueMapping;
2085
2134
}
2086
2135
@@ -2666,13 +2715,18 @@ bool JumpThreadingPass::duplicateCondBranchOnPHIIntoPred(
2666
2715
if (!New->mayHaveSideEffects ()) {
2667
2716
New->eraseFromParent ();
2668
2717
New = nullptr ;
2718
+ // Clone debug-info on the elided instruction to the destination
2719
+ // position.
2720
+ OldPredBranch->cloneDebugInfoFrom (&*BI, std::nullopt, true );
2669
2721
}
2670
2722
} else {
2671
2723
ValueMapping[&*BI] = New;
2672
2724
}
2673
2725
if (New) {
2674
2726
// Otherwise, insert the new instruction into the block.
2675
2727
New->setName (BI->getName ());
2728
+ // Clone across any debug-info attached to the old instruction.
2729
+ New->cloneDebugInfoFrom (&*BI);
2676
2730
// Update Dominance from simplified New instruction operands.
2677
2731
for (unsigned i = 0 , e = New->getNumOperands (); i != e; ++i)
2678
2732
if (BasicBlock *SuccBB = dyn_cast<BasicBlock>(New->getOperand (i)))
0 commit comments