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

    <tr>
        <th>Summary</th>
        <td>
            Incorrect code gen on array of linked lists with optimizer
        </td>
    </tr>

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

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

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

<pre>
    This came up trying to compile the basic example from boost intrusive sets here https://www.boost.org/doc/libs/1_82_0/doc/html/intrusive/unordered_set_unordered_multiset.html, using boost 1.78.0.

A minimal reproducer is this:
```
extern "C" void printf(...);

struct MyList {
    MyList() {
 next = this;
    }

    bool empty() {
        return next == this;
    }

    MyList *next;
};

int main() {
 MyList list[100];

    int empty = 0;
    int non_empty = 0;

 for (auto& l : list) {
        if (l.empty()) {
 ++empty;
        } else {
            ++non_empty;
        }
 }

    printf("%d %d", empty, non_empty);
}
```

This fails on Clang >= 15, and works fine on Clang 14. Compile with -O2 and run the binary to see incorrect behaviour. The code is creating an array of linked lists, which are all constructed to point their next node to themselves, so essentially it is an array whose elements are their own address. The `empty` function checks that this invariant is maintained, which it is not for every second element, so the output I get is
```
50 50
```

I bisected the optimisation passes and found that the first failing pass is
```
BISECT: running pass (188) LoopVectorizePass on main
```
</pre>
<img width="1px" height="1px" alt="" src="http://email.email.llvm.org/o/eJyMVV1v6jgT_jXmZtTIcRoIF1wUOEiVzqt3pa32tnLiCfGpY0f2BMr59Ss7fJS2u1qEAvHMPPPMl0eGoPcWccXKNSu3MzlS5_zql_TeiUVVzGqnTquXTgdoZI8wDkD-pO0eyEHj-kEbBOoQahl0A_gu-8EgtN71UDsXCLQlPwZ9QAhIATr0CB3REFjxxMSOid3xeMySbub8nomdcg0TO6PrwMQuf63EK7-edtQbJnZXUCZ2o3VeoUf1GpBeb2_9aEgHpGyy2cAYIu-JVZ4tqoxnjG8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