Pointer comparison folding


I am looking at some code that does address comparisons to check whether a given pointer is within a certain memory range. For example:

if (0xff00 <= &a[x] && &a[x] < 0xffff)

This results in IR like:
%2 = getelementptr inbounds [100 x i32], [100 x i32]* @a, i32 0, i32 %0, !dbg !9

%3 = icmp uge i32* %2, inttoptr (i32 65280 to i32*), !dbg !10

%4 = icmp ult i32* %2, inttoptr (i32 65535 to i32*), !dbg !11

Suppose that ‘a’ is a global and we know the range of addresses where it can be allocated. Is it a safe transformation to use that range and constant fold the icmps? Is so, is it still safe if the gep offset ‘x’ is unknown?



I don’t think you can safely assume an address or range for a global at compile-time and make decisions based on this. It’s the linker’s job to calculate an address for this, and the final physical address is another thing that depends on your platform, e.g a hosted environment, or a an embedded system with all sorts of different memories where you can place data. But I am always a bit out of my comfort zone with linkers, so others my have a better answer.


If the address is known precisely, I think the icmp should be allowed to be constant-folded.
The equivalent assembly will always say true, so there is no reason to prevent it from optimizing it.
If gep offset ‘x’ is unknown, it is an open question, but if gep has inbounds the folding should still be allowed otherwise the result is poison.