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

    <tr>
        <th>Summary</th>
        <td>
            Handling tied operands in RISCVVLOptimizer
        </td>
    </tr>

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

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

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

<pre>
    TL;DR: we can't express tail undefined with ternary pseudos

I've been exploring using RISCVVLOptimizer to reduce VL toggles with EVL tail folding. In some of the benchmarks we fail to reduce the AVL of a ternary instruction, causing a vl toggle e.g.:

```asm
        vsetvli a4, zero, e32, m2, ta, ma
 vsext.vf2       v14, v10
        vsetvli zero, a1, e32, m2, ta, ma
 vmadd.vv        v14, v12, v8
```

Although we're able to reduce the AVL of the vmadd.vv itself, we can't reduce the AVL of the vsext.vf2 because its user, vmadd.vv, is a tied operand:

```
Trying to reduce VL for early-clobber %43:vrm2 = PseudoVSEXT_VF2_M2 $noreg(tied-
def 0), killed %42:vr, -1, 5, 3

  Checking user: %49:vrm2 = nsw PseudoVMADD_VV_M2 %43:vrm2(tied-def 0), killed %
47:vrm2, %48:vrm2, %40:gprnox0, 5, 1

 Abort because user used as tied operand
```

We don't allow users that are passthrus because the tail elements past `vl` will depend on them.

```c++
std::optional<MachineOperand> RISCVVLOptimizer::checkUsers(MachineInstr &MI) {
  for (auto &UserOp : MRI->use_operands(MI.getOperand(0).getReg())) {
    ...
    // Tied operands might pass through.
    if (UserOp.isTied()) {
 LLVM_DEBUG(dbgs() << "    Abort because user used as tied operand\n");
 return std::nullopt;
    }
```

At first glance we might think that we can relax this restriction if the user's policy is ta/ma. However I don't think this is correct because tail agnostic/mask agnostic != tail undefined/mask undefined, so we still need to preserve the passthru to get "previous or -1" for each element.

I think the problem comes down to how for binary pseudos we represent tail undefined/mask undefined by using NoRegister for the passthru.

For ternary pseudos the third operand is simultaneously also the passthru, so it's not possible to represent a tail undefined/mask undefined pseudo with a non-undefined third operand.

This means that we lose the information that the inactive elements are undefined during instruction selection. One possible solution could be to add new _INACTIVE_UNDEF pseudos with a TSFlag that marks this fact, and use these for regular SDNode patterns. Then RISCVVLOptimizer (and RISCVInsertVSETVLI) could pick up the TSFlag and allow it as a user.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyMVstu4zoS_Rp6U4ggU_Fr4YVix3MNJN2DxO2ZXUCLZYkTihRISu7crx8UJT_S6Zl7g8CWqarDU69DCu9VaRCXbPLAJuuRaENl3VK376gXs9HByo_l7ollD-sXluVwQiiEYXwWAH82Dr2HIJSG1kg8KoMSTipUENAZ4T6g8dhK61maszTfMj7rEA6Ihpy1dcqU0Hr6fNm-rvb7p-9NULX6Ex0ECw5lWyDsnyDYstToe-xHWqA9j1ZLZcoEtga8rRHsEUJFG5iiqoV790T3SKZXNDLI909kKy40lfHBtUVQ1jC-gkL0pAR0etgbMCkTluV9JGya9v_C1yzNYfjrPIZOKxD3hPInOkvfmHH6quNnEPFZkFfn8WdIuiM_u4-jXzdOf4N5RhPj_49ZCymTroNfMaNlN78l38eS61DZtqzghIzPHII4aPx9wujxsoEKHvWRUG964n_4XAI9IOUWyRlajy6SGhDpWXmqikIJtkEnjPyacpbmO_dB5fnUIkfrAIXTH3eFtocDOmB8cp-xLO9czYFla_hnbMb96-O_d2_7DX975sD4vbEOS8bntOsdS3OJR0gZXxCdd6U1yojEIxIt3sUKTOgj67kBrCos3vtuRkdzQi6L282NP50JPOfr9dt-3-9_5Xjm8HsCLM3vZxfLVfSc__I7ZVleNs7Yn-mF4nigmB-sC5f8E036kCD853x_6Y9_IUjbV1dobU_R10OoRADhEBrhfahc6y_gVPI4oKixRhM82QRg07TTbJrCSWkNEhs0Eqwh8zr5pcgF4w_0n-Y-xB7IctvQeArNstWzKCpl8Pu5RR6_yEfvUlBVfhBdxueD05ZGHRifPm8ZXwCb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