The optimization in CodeGenPrepare, where GEPs are unmerged across indirect branches must respect the types of both GEPs and their sizes when adjusting the indices.
The sample here shows the bug:
https://godbolt.org/z/fvYvvGGjE
The value %elementValuePtr addresses the second field of the %struct.Blub. It is therefore a GEP with index 1 and type i8.
The value %nextArrayElement addresses the next array element. It is therefore a GEP with index 1 and type %struct.Blub.
Both values point to completely different addresses, even if the indices are the same, due to the types being different.
However, after CodeGenPrepare has run, %nextArrayElement is a bitcast from %elementValuePtr, meaning both were treated as equal.
The cause for this is that the unmerging optimization does not take types into consideration.
It sees both GEPs have %currentArrayElement as source operand and therefore tries to rewrite %nextArrayElement in terms of %elementValuePtr.
It changes the index to the difference of the two GEPs. As both indices are 1, the difference is 0. As the indices are 0 the GEP is later replaced with a simple bitcast in CodeGenPrepare.
Before adjusting the indices, the types of the GEPs have to be aligned and the indices scaled accordingly for the optimization to be correct.
Due to the size of the struct being 16 and the %elementValuePtr pointing to offset 1, the correct index for the unmerged %nextArrayElement would be 15.
I adjusted the optimization and added a simple test case that asserts the correct index is preserved.
I assume this bug emerged from the opaque pointer change as GEPs like %elementValuePtr that access the struct field based of type i8 did not naturally occur before.