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

    <tr>
        <th>Summary</th>
        <td>
            AMDGPU should form s_addk_i32 and s_mulk_i32 earlier
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:AMDGPU
      </td>
    </tr>

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

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

<pre>
    Currently AMDGPU forms these in SIShrinkInstructions, which runs twice. The first run sets a register hint that the input and the result should be the same register. The second post-regalloc runs performs the s_add_i32 -> s_addk_i32 / s_mul_i32 -> s_mulk_i32 transform if the source and destination registers are the same.

This kind of works, but could be better. When touching the allocator and copy related patches, I regularly see regressions where the formation of the K forms is broken. Using tied operands to begin with is a much stronger hint to the allocator to prefer the tied form. This is how we handle FMA vs. fmac cases.

This can be done in 3 parts:

1. Handle s_addk_i32 -> s_add_i32 and s_mulk_i32 -> s_mul_i32 in convertToThreeAddress
2. Teach FoldImmediate to form the K forms from the non-K forms
3. Modify constant selection patterns to directly emit the K forms for valid immediate
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJxcVM2O4zYMfhrnQozh2E4yOfiQ7jbtoFigwGbR40CW6EiNLBkknSBvX0jOzqR7SUDa_vj9SFTM7hwQu2LzW7H5ulKz2EidIsYwrvpo7t2XmQiD-Dscvn394-8fMEQaGcQiI7gA39--W3Lh8hZYaNbiYuCi_gI367QFmgOD3JzGEk4WYXDEkrrAKAwKCM-OBQmsCwJiVfpJwNMsoILJFSHPXoBtnL2BHnOT1Ygfny_ojDoGA1NkeSE8K--jXihMSB-8gd-VMe-uqeGlaH5fykuui_oI_D7O_vnpOPvlqZAKnGDADQtQnEljpmmQxQWV5H-QYlD0ybUsqkNRHU7WMVxcMBAHuEW6ZLf6WUD_VNejZEn_WAwgcdbWhXPGyYqURMozdZzuQOiVoIFJibaYwd4Sg9kr8ndgzCYRMqdk4GbxwSkJWfjGRc1fj2gdQ0_xgqGEH5wnOzQQJyQVDINE6PHsAtyc2PSygnHWFlgohvNHkvEXwhJhIhyQcj9DpnEpOJdn2niDG4JVwXiE47cDXLmEYVQatGLkZ_-0CskoE0M-gw1MioSL5rC8sy7hzwXmKdvPrHOZDHzK9jPrXLoAOoYrkpziyRLiwZjkYVEd6hJOqLSFY_TmbRzROCWY9OWj8ezkQHFphBheHs2iOjQlfIvGDfc0g0UFAUaP-e6kHAUpZJ-NI9Tp6uHo5P_IkeCqvDPgfjJYma4x-2avVtitd-26bqr2dbuyXb9Zv6oBVYtbs93htlnrQe2x3enNHtu1WrmurupN1VTbqqleq225q1q9W2O1Xbem1bu-aCsclfOl99exjHReOeYZu3W93-9eV1716DnvkLrulb5gMEVzWPZFUddps1CXvn3p5zMXbeUdC3-iiROP3WO_PG55NvMpvl_yQkXeIa1m8p0VmXL49bGoj2cndu5LHceiPqYRj7-XieK_qKWoj5k9F_XxIeDa1f8FAAD__4aHu-U">