[llvm] [SelectionDAG] Salvage debuginfo when combining load and z|s ext instrs. (PR #188544)

Orlando Cazalet-Hyams via llvm-commits llvm-commits at lists.llvm.org
Thu Apr 2 02:34:26 PDT 2026


================
@@ -14838,8 +14847,60 @@ static SDValue tryToFoldExtOfLoad(SelectionDAG &DAG, DAGCombiner &Combiner,
     return {};
 
   SDLoc DL(Load);
-  // If the load value is used only by N, replace it via CombineTo N.
-  bool NoReplaceTrunc = N0.hasOneUse();
+
+  auto SalvageDbgValue = [&](SDDbgValue *Dbg, SDValue From, SDValue To,
+                             const DIExpression *NewExpr) {
+    SmallVector<SDDbgOperand> Locs = Dbg->copyLocationOps();
+    bool Changed = false;
+
+    for (SDDbgOperand &Op : Locs) {
+      if (Op.getKind() != SDDbgOperand::SDNODE)
+        continue;
+
+      if (Op.getSDNode() == From.getNode() &&
+          Op.getResNo() == From.getResNo()) {
+        Op = SDDbgOperand::fromNode(To.getNode(), To.getResNo());
+        Changed = true;
+      }
+    }
+
+    if (!Changed)
+      return;
+
+    SDDbgValue *NewDV = DAG.getDbgValueList(
+        Dbg->getVariable(), const_cast<DIExpression *>(NewExpr), Locs,
+        Dbg->getAdditionalDependencies(), Dbg->isIndirect(), Dbg->getDebugLoc(),
+        Dbg->getOrder(), Dbg->isVariadic());
+
+    Dbg->setIsInvalidated();
+    Dbg->setIsEmitted();
+    DAG.AddDbgValue(NewDV, /*isParameter=*/false);
+  };
+
+  // Because we are replacing a load and a s|z ext with a load-s|z ext
+  // instruction, the dbg_value attached to the load will be of a smaller bit
+  // width, and we have to add a DW_OP_LLVM_convert expression to get the
+  // correct size.
+  auto SalvageToOldLoadSize = [&](SDValue From, SDValue To, bool IsSigned) {
+    SmallVector<SDDbgValue *, 4> DbgVals(
+        DAG.GetDbgValues(From.getNode()).begin(),
+        DAG.GetDbgValues(From.getNode()).end());
+
+    unsigned VarBitsFrom = From.getValueSizeInBits();
+    unsigned VarBitsTo = To.getValueSizeInBits();
+
+    for (SDDbgValue *Dbg : DbgVals) {
----------------
OCHyams wrote:

Oh right, yeah that makes sense.

https://github.com/llvm/llvm-project/pull/188544


More information about the llvm-commits mailing list