[llvm] [BasicAA] Use nuw attribute of GEPs (PR #98608)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Wed Aug 7 05:56:16 PDT 2024


nikic wrote:

> Thank you for reviewing and pointing out the issues with this patch - I see that I was not sufficiently considering how the GEP subtraction works in cases such as that example. I guess for this approach to work we'd need to prove that the constant offset between the GEPs does not cross the address space boundary, and its not obvious to me how to do this (without inbounds/nusw).

I think the necessary condition for the constant offset is just that the LHS offset is larger than the RHS offset, so that the subtraction does not overflow (in an unsigned sense).

An approach to make this work could be:

 * Swap LHS/RHS so that the larger GEP (in the number of variables) is on the left. I think doing this ahead of time is safer than trying to reason about IsNegated, because the IsNegated flag may get lost.
 * Subtract LHS - RHS as usual, but need to clear the NUW flag in some cases now.
 * If LHS.Offset ult RHS.Offset, clear NUW.
 * For variable indices, if LHS.Scale ult RHS.Scale, clear NUW.



https://github.com/llvm/llvm-project/pull/98608


More information about the llvm-commits mailing list