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

    <tr>
        <th>Summary</th>
        <td>
            [DAG] SelectionDAG::FoldConstantArithmetic - failure to constant fold ISD::FSHL/FSHR nodes
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            good first issue,
            llvm:SelectionDAG
      </td>
    </tr>

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

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

<pre>
    https://zig.godbolt.org/z/36WWT1Kes

```ll
define <4 x i32> @doit() {
  %res = call <4 x i32> @llvm.fshl.v4i32(<4 x i32> <i32 0, i32 1, i32 2, i32 3>, <4 x i32> <i32 4, i32 5, i32 6, i32 7>, <4 x i32> <i32 8, i32 9, i32 10, i32 11>)
  ret <4 x i32> %res
}
```
llc -matr=+avx2
```asm
  vmovaps .LCPI0_0(%rip), %xmm0 # xmm0 = [0,512,2048,6144]
  retq
```
llc -mattr=+avx512vl,+avx512vbmi2
```asm
doit: # @doit
  vpmovsxbd .LCPI0_3(%rip), %xmm1 # xmm1 = [4,5,6,7]
  vpmovsxbd .LCPI0_4(%rip), %xmm0 # xmm0 = [0,1,2,3]
  vpshldvd .LCPI0_2(%rip), %xmm1, %xmm0
 retq
```

AVX2 manages to constant fold fshl/fshr only because we expand to regular shifts, AVX512VBMI2 targets have legal funnel shift instructions and have no opportunity to constant fold.

FoldConstantArithmetic only attempts to constant fold unary/binary scalars, but it shouldn't be difficult to handle trinary scalar integers as well for FSHL/R.
</pre>
<img width="1" height="1" alt="" src="http://email.email.llvm.org/o/eJyUlFFvpDYQxz-NeRkF4THsLg88kOXoRXeVqqTK3VtlwIArY29twyX99JXZcNkmm0qVkDIx8x__5j_scOfkoIUoSHZLsirisx-NLe6_PMjJ6Kgx3XMxen9yhJUEa4L133KIB9M1RvnY2CGcEKzZ7tu33-kX4Uh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