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

    <tr>
        <th>Summary</th>
        <td>
            [AArch64] improve codegen for shifted mask op
        </td>
    </tr>

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

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

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

<pre>
    During type legalization, we can end up with a mess of shifts and masks like this:
```
define i64 @src(i64 %x) {
  %s1 = lshr i64 %x, 31
  %a = and i64 %s1, 2147483647
  %s2 = shl i64 %a, 31
  ret i64 %s2
}
```

That's equivalent to a shifted mask op (and we do this transform in IR in InstCombine):
```
define i64 @tgt(i64 %x) {
  %a = and i64 %x, 4611686016279904256 ; 0x3fffffff80000000
  ret i64 %a
}
```

We also do that in DAGCombiner, but the general pattern is guarded by a TLI hook:
isDesirableToCommuteWithShift()
...and that is set to false for this case by AArch64.

But this example seems like it would be better with the transform because we can represent the mask constant as an immediate value:
        and     x0, x0, #0x3fffffff80000000

vs. current codegen:
        ubfx    x8, x0, #31, #31
        lsl     x0, x8, #31


Note: I noticed this while investigating a similar pattern for a different target's test diff in https://reviews.llvm.org/D77804
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyNlN9vozgQx_8aeBktAkP48cBD2qirSqd7uKu0zwYP4K3BWdsk6f31NzZp0-7unRohB9vDzHc-M3anxUt7WI1cRnAvRwSFI1fyH-6kXiJ2D2eEni-Ai4D1CGfpJuAwo7WgB7CTHJwFTpszt88WlHxGcJO0Ub6P0kOU7qMyvT5hKnCQC4IsC4iK1Jo-YnWYsN0lYg1E1d1mCH7JZhDlB1B2MnCzuoc8e2fEg43XcDWxmbdhWVEVdV4W1XuHLBjbSb0a84_-DLo3N-yaQXX4bSrb-DRxF7HKAv5Y5YkrXBw4TYwCG9zAgD6Sx9prJJ5CB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