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

Duncan Sands duncan.sands at gmail.com
Thu Sep 26 08:49:24 PDT 2013

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

Ciao, Duncan.

```