[LLVMdev] Reassociating expressions involving GEPs

Dan Gohman gohman at apple.com
Mon Mar 9 11:02:55 PDT 2009


On Mar 8, 2009, at 9:14 PM, Chris Lattner wrote:

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


These two sentences contain a contradiction. GEPing outside of an array
may lead to overflow, because in general one doesn't know where an
array will be placed within the address space. If GEP overflow is
undefined, then it's not fine to GEP outside the array, in general.

Dan




More information about the llvm-dev mailing list