[LLVMdev] Reassociating expressions involving GEPs

Chris Lattner clattner at apple.com
Sun Mar 8 21:14:40 PDT 2009


On Feb 25, 2009, at 12:12 PM, Stefanus Du Toit wrote:
>> Although, I'm not completely sure the transformation is safe, at  
>> least
>> the way you're stating it; unlike add, GEP has undefined overflow, so
>> this isn't right in cases like %call == %tmp4 == INT_MIN.
>
> Hmm, you raise a good point. There's a similar issue even without
> overflow, e.g. (gep p, (add -1, t)). The lang ref isn't exactly clear
> on this, but one interpretation says that if p points to the start of
> an array allocation, (gep p, -1) has undefined behaviour. Perhaps
> someone (Chris?) can clarify whether that's what's meant, or whether
> only loads and stores out of bounds are considered undefined. The
> sentences in question are:
>
> "Note that it is undefined to access an array out of bounds: array and
> pointer indexes must always be within the defined bounds of the array
> type."

GEP overflow is undefined, but this sentence means that *accesses* to  
an array must be within its bounds.  It is fine to GEP outside the  
array as long as you readjust the pointer back before access.

-Chris



More information about the llvm-dev mailing list