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

    <tr>
        <th>Summary</th>
        <td>
            [AArch64] GlobalIsel unable to legalize vectorized binaryop(G_ADD, G_SUB, ...)
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
      </td>
    </tr>

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

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

<pre>
    Our aflplusplus fuzzing shows that AArch64 can't compile the following code can't compile using `llc -mtriple=aarch64 -global-isel add.ll`, It could complaint: `unable to legalize instruction: %10:_(<32 x s16>) = G_ADD %0:_, %1:_ (in function: add_32xi16)`

Godbolt: https://godbolt.org/z/7rz7WdbEx

```
define <32 x i16> @add_32xi16(<32 x i16> %0, <32 x i16> %1) {
    %3 = add <32 x i16> %0, %1
    ret <32 x i16> %3
}

define <64 x i8> @add_64xi8(<64 x i8> %0, <64 x i8> %1) {
    %3 = add <64 x i8> %0, %1
    ret <64 x i8> %3
}
```

A further study shows  that in `AArch64LegalizeInfo.cpp:116`

```cpp
  getActionDefinitionsBuilder({G_ADD, G_SUB, G_MUL, G_AND, G_OR, G_XOR})
      .legalFor({s32, s64, v2s32, v4s32, v4s16, v8s16, v16s8, v8s8})
      .scalarizeIf(
          [=](const LegalityQuery &Query) {
            return Query.Opcode == G_MUL && Query.Types[0] == v2s64;
          },
          0)
      .legalFor({v2s64})
      .widenScalarToNextPow2(0)
      .clampScalar(0, s32, s64)
      .clampNumElements(0, v2s32, v4s32)
      .clampNumElements(0, v2s64, v2s64)
      .moreElementsToNextPow2(0);
```

Many vectorized operations are not legal for all five operations.
It seems to us that we should be using the following diff:

```diff
@@ -121,8 +121,8 @@ AArch64LegalizerInfo::AArch64LegalizerInfo(const AArch64Subtarget &ST)
           },
           0)
       .legalFor({v2s64})
-      .widenScalarToNextPow2(0)
-      .clampScalar(0, s32, s64)
+      .widenScalarOrEltToNextPow2(0)
+      .clampScalarOrElt(0, s32, s64)
       .clampNumElements(0, v2s32, v4s32)
       .clampNumElements(0, v2s64, v2s64)
       .moreElementsToNextPow2(0);
```

However, many tests failed after we make this change.  It seems many other places need to switch from `clampScalar` to `clampScalarOrElt` to include vector types.

Is this a feature, where we don't want certain vector types to get compiled; or this is a bug.
If it is a feature, can anyone elaborate why do we design it like this? If it is a bug, we can make some quick fixes.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJylVktzozgQ_jX4ojKFAWM4-OBHkk3VTLK7ydTuLSVA2NqRESsJO86vn24BDjbeSWrWhW2hfqgfX7c6lflx_lgrQgtRiVrjlxT12xsvN0Rv5UETs6WGLBYq20YhyWjp-DNDMrmruGBAZKSQQsgDCmQyZwOWWiPJiTwhMjLeGcUrwZxgTWmjcrwRMqVizDUThOa5KwQwO_6K3KOSWuRWlaC8NE6wQE11SVM8XBLBNlTwN0Z4qY2qM8NlaZn86cSDxYvjx06wCnzySvQkcoIbx08InE7uXhbrNfK1bCsrg2tYxLyEKJQndWDWS-C_ctDgJ2idt3a8RfN7J_NUCmva1phKw8Lxb-HZNARXqg28vcF3pt5mf-XpzWtfHtU1j33NWcFLRjqjuTWaOKHXtyG-JKMb6MJge2L9nS0b5QQ-sBnYCIDCKwLeKRQnCcXMFcagNX-27nvzbj6kFtjjnvVR-Arv1vg-8d32892PTb-m5Zrl53wDw88T0PwuIP8K0K2INnV-bGuhKQYAB3C3JfGlReB9WUg3qypI_wRidK7tdAQytNZtmFlYgK0xZBxXellzkTOFMZotLULRp7uXp2_LZvH125dmsXhoSY9_Nv9_wwI8AnievCfEtfVxK1uNOvCRWUch_u399n0fvi8QXbCIu8Uk0nG7E1_RrzMqqEL3CzyiR7L5mi4hW84UxOIM3DOkiZY5_lEzdYRkRHYxSHT3gfzVqiSWyX2sbH9BjbZ-IRaoAZ6W4flYMQ1nenBixwZOgrfBQLf1ZXW56_08fo2y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