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

    <tr>
        <th>Summary</th>
        <td>
            Miscompilation in LoopVectorizer
        </td>
    </tr>

    <tr>
      <th>Labels</th>
      <td>
            miscompilation,
            vectorization
      </td>
    </tr>

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

    <tr>
      <th>Reporter</th>
      <td>
          d-makogon
      </td>
    </tr>
</table>

<pre>
    Repro: https://godbolt.org/z/6MxeMx97G

The source IR is for the following code:
```java
int i = 1;
while ((i += 3) < 383) {
  arr[i] = 1;
  arr[i - 1] &= 1;
  arr[i] += 1;
}
```
The `arr` is filled with zeroes initially.
The program just stores `2` in each third element of the array starting from the index `4`.
After the vectorizer pass however the part of the loop that is processed in the vector loop is filled with just ones.

The issue in the vector loop is the following:
First we load the elements for which we need to do the addition of `1` (corresponding to the line `arr[i] += 1`) into `%strided.vec3`.
Then we store the ones into each third index: `store i32 1, ptr %21, align 4`;   `store i32 1, ptr %22, align 4`;   `...`.
And then we do the addition of `1` to the **previously loaded** vector of values: `%54 = add <8 x i32> %strided.vec3, <i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1, i32 1>`, and after that we perform the stores: 
```
%55 = extractelement <8 x i32> %54, i32 0
store i32 %55, ptr %21, align 4
```
```
%56 = extractelement <8 x i32> %54, i32 1
store i32 %56, ptr %22, align 4
```
...
So the issue is that the `+1` is done for the values that are loaded from the memory before the stores of `1`. So we end up storing `0+1=1` in each third index starting from 4.
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJykVVFv4jgQ_jXmZdQosUmAhzxAWU4n3b7sru7diSfEe04msg20_fUn2ylsabvS6SQExjMef_N9M2PpnD6OiDUrd6zcL-TJ92Rr9TDIf-hI46Ih9Vx_w8kSE1vovZ8cE1vGD4wfjqQaMj4je2T88ML4ofr6hF-fNqs_WL5n-TZ9_-gRHJ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