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

    <tr>
        <th>Summary</th>
        <td>
            [CodeGen][X86] Improving robustness of PATCHABLE_OP wrapper instruction
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            backend:X86,
            llvm:codegen
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          sylvain-audi
      </td>
    </tr>
</table>

<pre>
    When activating `-fms-hotpatch` in clang, the `"patchable-function"` pass replaces the first machine instruction with a wrapper `PATCHABLE_OP`.

This wrapping loses some information about the wrapped instruction: when a `PATCHABLE_OP` instruction is handled by `X86AsmPrinter::emitInstruction`, the wrapped instruction simply gets lowered without going through `X86AsmPrinter::emitInstruction` itself.

Here is an example in C/C++, where a tail call doesn't get lowered properly: https://godbolt.org/z/1Pjcbx87n

The source of the issue seems to be the loss of information in `PatchableFunction::runOnMachineFunction` when replacing a `MachineInstr` with the `PATCHABLE_OP` one:  It only keeps the OpCode and operands of the wrapped instruction, and `X86AsmPrinter::emitInstruction` can't be called from it.

We are looking for a clean way to achieve this. Any suggestions?

</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNVE1vozAQ_TVwsRIRCCE5cEjTzbbSrtpDpe5tZcwAbo2NbJM0--t3xjTdtOpKlfgyHr-Zee_ZlalP5WMHmnHh5YF7qVsWrZJZ07tZZ_zAvehwzKRmQnHdRumO-Q4oJkrTMM0rBbNm1AhgNP6k8IE7xywMigtwYUEjrfOs56KTGhDOeTuGFewofcc4O1o-DGAJ-X77sLvZXv349vvuHofzKLmOku30fOikm2KpVGUc4jvTE2RjbM8DJK_M6EPaCbW-TBhlW3YMLX-S611lmKnjula4vjpR8K_1auv6eyu1B4s4eEEv_e0FOPGy-19q5mQ_qBNrwTus_QgW56l_Krc11JHvrBnb7svZmPQOVPOOoxvEpeK5ZvDCMSOxw3ZRusf7Klw7ogCjOPNcKia4Uqw24FDAwlN9b-UN1qAs6kS0dd4PjipJ93i1pq6M8nNj0Rb7P3gv7p9E9bIu9HvFABUarQBmmsCMdG7EfwA9esOwCsJf1NJRxKWQWDZpdLbZ_uyyQIYd9Z3-OTnqbQYZCeJO5iNGg8yvYYG7EEOmezXyRwsYDdQsu_X4iWo9AwyTie-GnamRNF0zIgXf7tzTZ0ZDlin0y1IKPtGPhJAgiNZY06PC79R9xAIs0WWeqT0kC1sUClDtIz8RodQrHIhV6eZsq0_MjW0LjvKgfPsJJ4ZysVqtV2lRJElcl1m9yTY89tIrKKP8ilr9DjrKr3GADeAHu-3RDgdKa001Oq9h0uySwredfEFFPFpVfnAPKjBWc2F6HCh1OL9mmOEJhMdh8InDj3yTZJu4K-t1USwX6aqqlnkqoF6uxDrJikakmyYpMh4rXoFyVD0eRBUXz6BrzEjV48GEeuAr5Mq2AttrIRxY-XUsyzRJ08VisVogC3k-F-s64ZnI82KZpTxbR8sEetwqc1pOlo9tGcqtxtbhpJLOu3-TeP7JVkMgkvD5iJvclu6kDlzqGR9rGYf2ytDbX4621Mg">