[PATCH] D159099: [CodeGenPrepare] Unmerging GEPs across indirect branches must respect types

Maurice Heumann via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 29 07:51:41 PDT 2023


momo5502 created this revision.
Herald added a subscriber: hiraditya.
Herald added a project: All.
momo5502 requested review of this revision.
Herald added a project: LLVM.
Herald added a subscriber: llvm-commits.

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`.

As 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.

However, index 1 does not translate to the same offset for the different types `i8` and `%struct.Blub`.
The resulting pointers are not the same.

Before adjusting the indices, the types of the GEPs have to be aligned and the indices scaled accordingly for the optimization to be correct.


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D159099

Files:
  llvm/lib/CodeGen/CodeGenPrepare.cpp
  llvm/test/CodeGen/Generic/indirect-br-gep-unmerge.ll

-------------- next part --------------
A non-text attachment was scrubbed...
Name: D159099.554329.patch
Type: text/x-patch
Size: 5525 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20230829/74353dd3/attachment-0001.bin>


More information about the llvm-commits mailing list