llvm-project/llvm/test/Transforms/SeparateConstOffsetFromGEP
Meredith Julian cfbd53c981
[LLVM] [SeparateConstOffsetFromGEP] Fix sep-const-offset-from-gep invalid assumption (#183402)
`SeparateConstOffsetFromGEP` assumed the index of a GEP was non-negative
(and therefore previous sext/add could be reordered safely) if the GEP
was marked `inbounds`. This can only be assumed if the GEP is working
off of the base address for the object (counter example:
https://alive2.llvm.org/ce/z/FjGgWp).

This fix removes the general assumption of inbounds GEPs and replaces it
with new checks. The transform is valid when:

1. Value tracking shows the index is known non-negative.
2. The GEP is inbounds and the offset from the base ptr is 0.
3. The GEP is inbounds and the offset is within the threshold `(2^(N-1)
- C + 1) * stride`, where N is the bit width of the index, C is a
positive constant in the add, and stride is the type size of the GEP.
4. The GEP is inbounds and the object size is within the threshold
`(2^(N-1) - C + 1) * stride` for positive C or `(2^(N-1) + C) * stride`
for negative C.

Alive2 showing the constraints on the threshold:
https://alive2.llvm.org/ce/z/nBHM4m
2026-03-11 12:14:32 -07:00
..