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

    <tr>
        <th>Summary</th>
        <td>
            [MacroFusion][RISCV] Make commutable instructions fusible as possible
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          wangpc-pp
      </td>
    </tr>
</table>

<pre>
    In https://github.com/llvm/llvm-project/issues/82738, the user raised a issue about commutable instuctions.
I have created a PR (https://github.com/llvm/llvm-project/pull/82751) to fix it, but it comes another problem: what if the μ-arch can fuse the instruction pair only if `rs1` matches?
For example, for `slli+add` fusion (see https://godbolt.org/z/xo6xEoPKe):
```
slli r0, r0, 1
add r0, r0, r1 // fusible

slli r0, r0, 1
add r0, r1, r0 // fusible
```

The μ-arch may not check `rs2`, but `add` is commutable, we can't guarantee that `r0` will always be in `rs1`.
In InstCombine, we have `InstCombinerImpl::SimplifyAssociativeOrCommutative`:
```
/// This performs a few simplifications for operators that are associative or
/// commutative:
///
///  Commutative operators:
///
///  1. Order operands such that they are listed from right (least complex) to
///     left (most complex).  This puts constants before unary operators before
///     binary operators.
```

But do we need or have a way in backend to change the operands order of second instruction when it's commutable? 
cc @dtcxzyw @topperc
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVcFu2zgQ_Rr6MoghUbItH3RInAoIFkWLttj7iBpZ3FCkQFKR3a9fkHSa2NtddFHAkCVyOPP45vERnZNHTVSzzQPbPK5w9oOx9YL6OIm7aVq1pjvXTxoG7yfHinvGG8abo_TD3K6FGRlvlHp5_bubrPmLhGe8kc7N5BhvKr4rKsYP4AeC2ZEFi9JRBwgxBrA1swdhxnH22CoCqZ2fhZdGuzXLHll2_wQDvhAIS-jjys9fgPHqf4OaZqUSpE3O-B68gV6eQPqAr509yAiEHKA2fiALkzWtopEV97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