[cfe-dev] GEP index type

Eli Friedman eli.friedman at gmail.com
Sat Dec 10 13:47:36 PST 2011


On Sat, Dec 10, 2011 at 9:20 AM, Rotem, Nadav <nadav.rotem at intel.com> wrote:
> Hi,
>
> I have a question regarding the LLVM-IR which is generated by clang. When I compile the code below on a 64-bit system, clang generates a GEP instruction with a 64 bit index.
>
> int demo(int *A, short i) { return A[i]; }
>
> becomes:
>
> %4 = sext i16 %3 to i64
> %6 = getelementptr inbounds i32* %5, i64 %4
>
> I would expect to see an i16 index, since the index is known to be within the 0..16 bit range. Why does clang widen the index to a 64bit value ?

Primarily because this exposes the sign extension to the optimizer,
which is generally good; iIRC, instcombine does the same thing.

Also, in some cases we have to do the extension explicitly to preserve
the IR semantics: for example, suppose "i" was declared as an unsigned
short.

-Eli



More information about the cfe-dev mailing list