Compiler flag to optimize loops when a pointer is a 64-bit but allocs<4Gb

For an example like
Compiler Explorer,
where we have an assert() which limits the allocations to 4Gb, we would call the function len() where the pointer difference cannot exceed 32-bits.
However in LLVM IR we would still have a trunc(to i32) /and(, 4294967295) . In this specfic example where we are use the function len() to only determine if the string is null or zero, we can actually optimise the entire loop in len() if we can avoid the trunc/and.
To handle this we are considering a compiler flag which specifies the maximum allocations restricted to a 32-bit(<4Gb). This in particular could be useful for many applications out there that started as 32-bit and aren’t (fully) ported to 64-bit.

Any thoughts if someone has landed in scenarios like this and if there are alternate ways to handle the same.

2 Likes