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

    <tr>
        <th>Summary</th>
        <td>
            SelectionDAG::getNode may fold UNDEF to POISON
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            new issue
      </td>
    </tr>

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

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

<pre>
    When for example doing 
```
  U = DAG.getUNDEF(...);
  V = DAG.getNode(ISD::INSERT_VECTOR_ELT, DL, VT, N1, N2, N3)
```
SelectionDAG::getNode will just return N1 if `N2.isUndef.()`.

The code in SelectionDAG::getNode looks like this
```
 case ISD::INSERT_VECTOR_ELT: {
   ...
    // If the inserted element is an UNDEF, just use the input vector.
    if (N2.isUndef())
      return N1;
```

But the source element in N1 may be POISON, and we can't just fold UNDEF into POISON (otherwise SelectionDAG::getUNDEF could just return ISD::POISON as well).

I think that piece of code need to be more careful checking that the overwritten element from the source vector N1 is guaranteed not to be poison when doing that fold.


Same kind of problem seem to exist for ISD::INSERT_SUBVECTOR:
```
    // If this is an insert of an extracted vector into an undef vector, we
    // can just use the input to the extract.
    if (N1.isUndef() && N2.getOpcode() == ISD::EXTRACT_SUBVECTOR &&
        N2.getOperand(1) == N3 && N2.getOperand(0).getValueType() == VT)
      return N2.getOperand(0);
```

The above feels like a footgun to me.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8VdFuozoQ_RrnZbTImJCGBx5o06wqrdKrNu29bysDQ3BjbGSbpP37Kxs2TbLdrSpCYHzmzJkzE26t2CnEnKS3JF3N-OBabfLyTfc4K3X9kf_booJGG8B33vUSodZC7YDQgizo9E8LgBcgyQpWxfdoh-5ls7pfE7aMooiwjCS3IeT1PGSjayRs-fC8IklBkuJh83z_tP35en-3fXz6ef9jS9gdrH7462u438ThysI18bCXFJ5RYuWEVqvi-w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