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

    <tr>
        <th>Summary</th>
        <td>
            [Clang] vec_perm returns incorrect mapping (all 0 bytes) when optimization options of -O2 and higher are used
        </td>
    </tr>

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

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

    <tr>
      <th>Reporter</th>
      <td>
          bahareh-farhadi
      </td>
    </tr>
</table>

<pre>
    `vec_perm` function does not do correct mapping when optimization flags of `-O2` and higher are used. The result vector consists of all 0 bytes.

**Test case:**
```
$ cat t.c
#include <stdio.h>
#include <stdlib.h>
#include <vecintrin.h>
 
int main(){
        vector unsigned char v1;
        v1 = (vector unsigned char){0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0a, 0x0b, 0x0c, 0x0d, 0x0e, 0x0f, 0xff};
        vector unsigned char table0 = (vector unsigned char){0,1,1,2,1,2,2,3, 1,2,2,3,2,3,3,4};
        vector unsigned char temp = vec_perm(table0,table0,v1);
        int i;
        printf ("%s", "temp ");
        for (i=0; i<16; i++)
        {
 if (i>0) printf (" ");
                printf ("%02x", temp[i]);
 }
        printf ("\n");
}
```

**Command to reproduce the actual behaviour:**
```
$ clang -v
Ubuntu clang version 14.0.0-1ubuntu1.1
Target: s390x-unknown-linux-gnu
...
```
```
$ clang -mvx -mzvector -O2 t.c; ./a.out
temp 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
```
**Expected behaviour seen with -O1:** 
```
$ clang -mvx -mzvector -O1 t.c; ./a.out
temp 01 01 02 01 02 02 03 01 02 02 03 02 03 03 04 04
```

 
**GCC behaviour (expected):**
```
$ gcc -v
...
gcc version 11.3.0 (Ubuntu 11.3.0-1ubuntu1~22.04)
$ gcc -mvx -mzvector -O2 t.c; ./a.out
temp 01 01 02 01 02 02 03 01 02 02 03 02 03 03 04 04
$ gcc -mvx -mzvector -O3 t.c; ./a.out
temp 01 01 02 01 02 02 03 01 02 02 03 02 03 03 04 04
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy0Vk9z6yYQ_zT4siMNfyRbPvjwbMc95pKeOwghiVYCDyDH7x362Tsg4jhOnCadqQfpJy_sssv-2FnunOq0lBtUblG5X_DJ98Zuat5zK_us5bbnjVrUpvm5QUt8kuKPo7QjWmJoJy28MhoaIx1o46ExIIy1UngY-fGodAfPvdRgjl6N6hePq9uBdw5MC2iJs0caLHHdQK-6XlrgVsLkZJPDUy_BSjcNHk5SeGNBGO2U81GZDwNgqH966XKE9wj_SG8axpN0HgR3ErEkSbNLnEZaXIDgHnwuXgRMaTFMjQTEds43yuQ9Yg93ZgdV350-SaG0t0pfrYAZlA7nozSiFaJrtNqm2fRL0U46ZqYB0XMLJ4LY7ToCiO0B0eojjdkyPmOC6A7wGdOELGGRsEy4TLhKWCVcJ-QJ64QiYZNQJmxnbFu02r_3-aPYPK8Hib8QDKI7kh56heGJUd0KXjA8xdfdkeMxOnMhO61mFxHdXT5OJDh1azCkVr2THq3SvoWYb4po6SLsAFE6bxb-vjfWGht0FGJ7jNg2GN6R5fxFt3Gs32q8ckm1SfUBI7p-68G9_e65i-k5ORy8ReVWoXL_xkA4208iLnf6ZsuLwu2NvLrEOzOOoTJ4A1YerWkmIcH3ErjwEx-glj0_KTPZL1zygesOstMs-b2etJ-S8CStC2WJFDnOcUamOElyMq994raTHrEf4Ngan7NJ_6XNs84Gpadz1ulpXpbn-ceb3_VlPJ0hG38lBmaPNFYhtoUc0QPPzeRnlUgQjL817uwdxsP5KIWXzevpgZNSw7PyPWSP5HKW8N0AyKcBkDjoy5sCZm-_5zcDXAAuPuEGXEfz2253FQiilUzhRa79Gys6IS6cuCQwCC-cIDnLcbCbKDMLLiT5m9I81NH1jc3v5fY_Hc29rdj_l4VFs2HNmq35Qm7IitCqqGiBF_2GF5wVZN0uBa3ZsqyWRSl4teJLVlSiqFcLtaGYFrgghJQEFzgXtKklxVQ0siHliqMCy5GrIR-G05gb2y2Uc5PcVBVbrxYDr-XgYodCaWRfLCr7hd2E9Vk9dQ4VeAi9wasFr_wQ25pd1Chf6zlY6SerHSh9264gWl11FqFyvm9gwh-jYxcSUvtB87KY7LDpvT-6SMIDoodO-X6qc2FGRA_BxwTZ0Zo_pfCIHmLIDtFDjPqfAAAA__9v9Jjn">