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

    <tr>
        <th>Summary</th>
        <td>
            WRONG code when enabling Loop Vectorizer with -Os
        </td>
    </tr>

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

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

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

<pre>
    This program should print 1:

cat wrong0.i
```
int printf(const char *, ...);
int *IntPtr = 0;
int Val, ValB;
int *IntPtrArr5[5] = {0, 0, 0, 0, 0};
int *IntPtr2 = &ValB;
int main() {
  for (int IV1 = 0; IV1 < 5; IV1++)
 IntPtrArr5[IV1] = &Val;
  for (int IV2 = 1; IV2 <= 4; IV2 += 1) {
 int **PtrPtr = &IntPtr2;    // -> IntPtr2          
    int **PtrPtr2 = &IntPtr;    // -> IntPtr
    *PtrPtr2 = IntPtrArr5[IV2]; // -> IntPtr -> Val  
    *IntPtr2 ^= 1;              //    ValB = 1         Val = 1 (2nd iter)
    *PtrPtr = IntPtrArr5[IV2];  // -> IntPtr2 -> Val
  }
 printf("checksum = %X\n", Val);
}
```
In the first iteration, IntPtr2 points to ValB and sets it to 1. Then, only in the second iteration does IntPtr2 point to Val and set that to 1. I wonder if the loopvectorizer has done something wrong, or if there is some later pass possibly that is messing it up..?

```
rm a.out; clang -O0 -march=z16 wrong0.i -o a.out -w; ./a.out
checksum = 1

rm a.out; clang -Os -march=z16 wrong0.i -o a.out -w -fno-vectorize -fno-slp-vectorize; ./a.out
checksum = 1

rm a.out; clang -Os -march=z16 wrong0.i -o a.out -w -fno-slp-vectorize; ./a.out
checksum = 0
```

@fhahn @david-arm @nilanjana87 
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJy8VV2PqzYQ_TXDywgEEyDkgYfN5qbaqupdVVfbvhpwgm-NjWxno72_vjJ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