<table border="1" cellspacing="0" cellpadding="8">
    <tr>
        <th>Issue</th>
        <td>
            <a href=https://github.com/llvm/llvm-project/issues/135736>135736</a>
        </td>
    </tr>

    <tr>
        <th>Summary</th>
        <td>
            [DebugInfo][ConstraintElimination] Potential debug value loss in replacing comparisons with the speculated constants
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            debuginfo,
            llvm:transforms
      </td>
    </tr>

    <tr>
      <th>Assignees</th>
      <td>
      </td>
    </tr>

    <tr>
      <th>Reporter</th>
      <td>
          Apochens
      </td>
    </tr>
</table>

<pre>
    In ConstraintElimination, the pass first collects instructions that could be replaced with a speculated constant in function [`checkAndReplaceCondition()`](https://github.com/llvm/llvm-project/blob/fe54d1afcca055f464840654dd2ec3fd83aea688/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp#L1431), and then it removes the collected instruction in [ConstraintElimination-L1964](https://github.com/llvm/llvm-project/blob/fe54d1afcca055f464840654dd2ec3fd83aea688/llvm/lib/Transforms/Scalar/ConstraintElimination.cpp#L1964).
```c++
1963 for (Instruction *I : ToRemove)
1964 I->eraseFromParent();
```

However, I found that this process leads to _**poison debug values**_ in the optimzied IR. [Here](https://godbolt.org/z/78aMxx6En) is an example where the ConstraintElimination is performed on an LLVM IR function in the regression test (and.ll) of the pass. The debug information is obtained by `opt -passes=debugify`. Poison debug values are produced in the basic block `bb1`:
```llvm ir
; Before the optimization
bb1: ; preds = %entry
  %t.1 = icmp ule i4 %x, %z, !dbg !31
 #dbg_value(i1 %t.1, !13, !DIExpression(), !31)
  %t.2 = icmp ule i4 %x, %y, !dbg !32
    #dbg_value(i1 %t.2, !14, !DIExpression(), !32)
  %r.1 = xor i1 %t.1, %t.2, !dbg !33
```

```llvm ir
; After the optimization
bb1:                                              ; preds = %entry
    #dbg_value(i1 poison, !13, !DIExpression(), !31)
 #dbg_value(i1 poison, !14, !DIExpression(), !32)
  %r.1 = xor i1 true, true, !dbg !33
```

I think these poison debug values, which corresponds to the erased `icmp` instructions, **_could be prevented_** according to [rules-for-updating-debug-values](https://llvm.org/docs/HowToUpdateDebugInfo.html#rules-for-updating-debug-values). Specifically, we can use either `salvageDebugInfo()` (i.e., using the DIExpression) or `replaceAllDbgUsesWith()` (i.e., refering the debug value to the speculated constrant) to save the debug value information for the erased `imp` instructions.

Moreover, could this issue be caused by the missed debug information salvage in `Value::replaceUsesWithIf()` used in `checkAndReplaceCondition()`? (`Value::replaceAllUsesWith` handles the debug value information for the replaced instruction.)
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJzUls1u27oSx5-G3gwiSKSkWAsvlLhGDbRA0dNz7rKgyJHFW5oUSCofffqLkZXEbdwGxV0dw4BskTMc_v7D4cgYzcEhblh1w6rtSk5p8GHTjl4N6OKq8_pxs3dw611MQRqX3llzNE4m4x3jt5AGhFHGCL0JMYHy1qJKEQzNnxRNi5AGSUOT1dAhBBytVKjh3qQBJMQR1WRlQg2KlpEugXHQT242BwqtztWA6lvr9OeT9a132pyCWDPesDpn1Zbx9ZDSGJloGd8xvjuYNExdpvyR8Z21d0-PqzH4_6JKjO866zvGdz1WpS5kr5TMq6ov63Jd5nVVas1RiV6vhURZr9dnfgzZfQnSxd6HY2R895eSVgbGdxdxZWocGRcfilIUFDK_Bek0EXRgEgQ8-juMM9EFI-pzjgSFVTcXfV99KJq6_PcgoGB5k7G8JeXmr2L8hr55WzS1gN4HYHy9P9s_4-0emGjhi_88wyKK8_QS9ldMvMMgI-6CP36SAV1aUkPcnC9Dv_P2vb_HOwykwR56P806yARpMBHG4BXGCBaljpA8fGW8ZbwdvYnegcZuOsCdtBPG08BXkoZ082Myx-8GNew_ZyTWewx4URWvO29T5sOB8d13xnfXa_nx4aF-5xhvwESQDvBBHkeLcD9gwNn_Rag0e8RACqAG78j0w4d_PsL-88shWgIMeAgYI71JGBMRlk5n1tKqvn8-zhl8GXDZqXHk-nkp3yVpHGroHoHVuR8TXJEJRia2s4XpH1mdZ_DpNS-QAYmvntSc3POCnYxGQWe9-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