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

    <tr>
        <th>Summary</th>
        <td>
            MachineConstantPool::getConstantPoolIndex - CanShareConstantPoolEntry assumes undef == zero
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            llvm:codegen,
            miscompilation
      </td>
    </tr>

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

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

<pre>
    This was discovered in an internal test that I'm still trying to reduce.

CanShareConstantPoolEntry bitcasts the Constant values using ConstantFoldCastOperand to the same Type, and then checks if they have the same value.

Unfortunately this can result in cases where a Constant vector that contains zeros for specific elements can match with an existing Constant pool vector that has undefs for the same elements:

Existing: <i32 42, i32 0, i32 undef, i32 undef>
Candidate: <i32 42, i32 undef, i32 undef, i32 0>

A call to getConstantPoolIndex for the Candidate Constant will return the index for the Existing Constant.

This is fine if all we're ever going to do is write out these constant pool entries, but if use the entries for any kind of analysis, we can end up thinking an element may be undef when it actually must be treated as zero.

We either need to not share entries if an existing entry contains any undefs, or we need to adjust the existing entry to zero the undef elements where necessary (we already adjust the alignment if necessary).
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJx0VE1v4zYQ_TXUZRBDpuKvgw5uEgOLomjR3aLnMTm2uKFIgzOKV_31BanYsRfZiz7I4eObN28Gmd0xELVq8ZtaPFc4SBdT-_fvX10fQ7WPdmy_dY7hjAzWsYlvlMiCC4ABXBBKAT0IsYB0KPBF6VUPLM57kDS6cASJkMgOhmaqflb1dno-YfjaYaKnGFgwyF8x-pcgaYS9E4MsDNIRXLbhDf1ADANnyMvqLnr7hCx_nihhsPmqfIixJ_g2nkjpJyjrHQUwHZlXBnfIvyN0-EYf0QX-juA_4RCTDAGF_AiSRTAYIBEPXrIABpkYzh0lArwhSkZimtQwMQi6wPAfpchwiAn4RM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