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

    <tr>
        <th>Summary</th>
        <td>
            [AArch64][SLP] slp-vectorizer produce incorrect result
        </td>
    </tr>

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

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

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

<pre>
    Compiling with  
`clang --target=aarch64-linux-gnu -O3 -fno-vectorize` 
got the following test aborted with incorrect result `-606` in `dst[7]`:
```C
#include <stdlib.h>
struct cnum {
    int real; 
    int img; 
} src[1] = {{20000, 20000}};

inline int operations(int x, int y) {
  return (2*y*x + 0x10) >> 16;          
}

void slp_loop(int n, int val, int *dst) {
  int idx = 0;
  for (int i = 0; i < n; ++i) {
    int tmp0 = src[i].img;
    int tmp1 = src[i].real;
    int tmp2 = 0x131 - abs(tmp1);
    int tmp3 = 0x131 - abs(tmp0);
    int tmp4 = 0x91 - abs(tmp2);
    int tmp5 = 0x91 - abs(tmp3);
    int tmp6 = 0x1 - abs(tmp4);
    int tmp7 = 0x1 - abs(tmp5);
    dst[idx++] = operations(tmp0, val);
    dst[idx++] = operations(tmp1, val);
 dst[idx++] = operations(tmp2, val);
    dst[idx++] = operations(tmp3, val);
    dst[idx++] = operations(tmp4, val);
    dst[idx++] = operations(tmp5, val);
    dst[idx++] = operations(tmp6, val);
 dst[idx++] = operations(tmp7, val);
  }
}

int main(void) {
 int dst[8];
    slp_loop(1, 1000, dst);
    if(dst[7] != -597)
 /* SLP vectorizer error */         
      abort();
    return 0;
}
```
When slp-vectorizer disabled 
`clang --target=aarch64-linux-gnu -O3 -fno-vectorize -fno-slp-vectorize` 
the test passed.
Preliminary investigation showed that `llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp SLPVectorizerPass::tryToVectorizeList -> R.vectorizeTree()` is making things wr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