# [llvm] r191200 - InstSimplify: Fold equality comparisons between non-inbounds GEPs.

Benjamin Kramer benny.kra at gmail.com
Thu Sep 26 09:09:14 PDT 2013

```On 26.09.2013, at 17:49, Duncan Sands <duncan.sands at gmail.com> wrote:

> Hi Benjamin,
>
> On 23/09/13 16:16, Benjamin Kramer wrote:
>> Author: d0k
>> Date: Mon Sep 23 09:16:38 2013
>> New Revision: 191200
>>
>> URL: http://llvm.org/viewvc/llvm-project?rev=191200&view=rev
>> Log:
>> InstSimplify: Fold equality comparisons between non-inbounds GEPs.
>>
>> Overflow doesn't affect the correctness of equalities. Computing this is cheap,
>> we just reuse the computation for the inbounds case and try to peel of more
>> non-inbounds GEPs. This pattern is unlikely to ever appear in code generated by
>> Clang, but SCEV occasionally produces it.
> ...
>> --- llvm/trunk/test/Transforms/InstSimplify/compare.ll (original)
>> +++ llvm/trunk/test/Transforms/InstSimplify/compare.ll Mon Sep 23 09:16:38 2013
>> @@ -717,3 +717,12 @@ define i1 @alloca_gep(i64 %a, i64 %b) {
>>    ret i1 %cmp
>>  ; CHECK-NEXT: ret i1 false
>>  }
>> +
>> +define i1 @non_inbounds_gep_compare(i64* %a) {
>> +; CHECK-LABEL: @non_inbounds_gep_compare(
>> +; Equality compares with non-inbounds GEPs can be folded.
>> +  %x = getelementptr i64* %a, i64 42
>> +  %cmp = icmp eq i64* %a, %x
>> +  ret i1 %cmp
>> +; CHECK-NEXT: ret i1 false
>> +}
>
> Suppose this example was
>  %x = getelementptr i64* %a, i64 4294967296
> i.e. offset of 2^32.
> On a 32 bit machine, %x is equal to %a, so the comparison should not be folded
> to "false" on such machines.

Yup, and accumulateConstantOffset does its computation in the intptr width and gets this right. Your example is correctly folded to "true". Did you have another example where we get it wrong now?

- Ben

```