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

    <tr>
        <th>Summary</th>
        <td>
            [RISC-V] MachineLICM incorrectly hoists fsrmi out of loop
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:RISC-V
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          skachkov-sc
      </td>
    </tr>
</table>

<pre>
    **Godbolt example:** https://godbolt.org/z/h9rc5xrMj

Here we have two fsrmi instructions setting different dynamic rounding modes that are used in subsequent fadd operations. However, MachineLICM hoists them out of loop, leading to an incorrect result. After some investigation, looks like it's caused by implicit-def $frm in WriteFRMImm marked as dead (despite of its use in next FADD_D). so MachineLICM treats WriteFRMImm as a valid candidate. This IR inconsistency happens due to the fact that `RISCVTargetLowering::AdjustInstrPostInstrSelection` hook (which adds implicit frm uses) is called after instruction emission, so EmitNode correctly sets $frm def as dead (because at this stage there are no $frm uses), but after AdjustInstrPostInstrSelection "dead" flag is not updated.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJx8lE1v4zYQhn8NfRnYkCl_6aCDGlddA5uiyC62x4IiRxJjilQ5Izvury-oOGnSw55sSZyP95l5qYhs5xFLsf1FbI8LNXEfYklnpftzuCxJL5pgbqWQlZDVb8E0wTHgixpGhyKvXt9DzzzS_FgLWXevx1YhdkLW_whZ90XU25f4-CyySmTVF4wIV4ReXRD4GqClOFiwnjhOmm3wBITM1ndgbNtiRM9gbl4NVkMMkzfp0xAMEnCvGFREmAgNWA80NYR_TymkVcZAGDGqOekKvoQrXjAK-QCPSvfW49fTwyP0wRKnVDhAmBhCCy6EMR1zqOZiHEB5sF6HGFEzRKTJ8QqqljEChQHB-gsS224uNseGcCZw9oxgWcg9gVZzl80N7DA6qy0vDbYg5KaNQ2r-z2gZ66fH0zDAoOIZDSgCg8qAkAeDNFrG1J9lSopTjMcXhro6Hv86ClmsgMIncRxRMX3KrAgUXJSzBrTyxhrFuILvvSU4Pc0iPVli9PoGvRpH9ARmwgSBe4RWaX7lLnbZ0-nbw4_vKnbIX8MVo_Vd2oS8qszzRHxKQ_0j3P98Q4fzgMUugz6Ec1J17a3uQRlD71Qg4ZgIScgCbOLmXEIxw_6wJ4CDJbrjpgC_DpZ_DwbhPiZ3S4tEb4AT6w84G5znASqpsQTEqsOkMOK8UT68Bd5bSVWaie99_FQgCClTHSEltE51SYUPDNOYYJvVwpS5KfJCLbBc7zcbWWTbXbHoy3WemU1z2LWtzHY61_vCaI0ZthvVHjaFWthSZnKbbdbZOs8KeVht82bfZOt81-qDOexasclwUNatnLsMyYULSzRhuc63671cONWgo9nxUjZKn9EbkVdpjssfQsp0D8QyxS6bqSOxyVxyx3_Z2LKbb4x7yPb4ad_ePeJub8Z69fcHZy2m6Mr_3RqW-6lZ6TAIWada95_lGMMzahaynmWQkPVdyaWU_wYAAP__AfWfIw">