[LLVMdev] Clarification between <type> and <ty> for alloca instruction

Tim Northover t.p.northover at gmail.com
Thu Aug 15 04:58:52 PDT 2013


Hi Dan,

> It is not stated how the "<ty>" type is used as we are told the type of
> result is type* and sizeof(<type>)*NumElements is the amount of memory
> so from my perspective it looks like <ty> is never used which would seem
> to make stating i32 twice in the following example redundant
>
> %ptr = alloca i32, i32 4
>
> My guess is that if <NumElements> is specified then the amount of memory
> actually allocated would be sizeof(<ty>)*NumElements (instead of
> sizeof(<type>)*NumElements) but the pointer type of result is <type>. Is
> that correct?

It doesn't really affect the output at all. If NumElements doesn't
overflow <ty> then the semantics don't depend on it at all.

In C-like code it's sort of the difference between these two functions:

    void ty_is_i16(unsigned short NumElements) {
        int arr[NumElements];
        use_array(arr);
    }

    void ty_is_i32(unsigned int NumElements) {
        int arr[NumElements];
        use_array(arr);
    }

you might compile them to

    define void @ty_is_i16(i16 %NumElements) {
        %arr = alloca i32, i16 %NumElements
        call void @use_array(i32* %arr)
        ret void
    }

    define void @ty_is_i32(i32 %NumElements) {
        %arr = alloca i32, i32 %NumElements
        call void @use_array(i32* %arr)
        ret void
    }

Of course, if NumElements *does* overflow they're different:
    alloca i32, i8 257
will allocate 1 i32, but
    alloca i32, i32 257
will allocate 257 of them.

It's mostly there because when NumElements is a run-time value (as in
the C examples) it has to have *some* type in LLVM IR.

Cheers.

Tim.



More information about the llvm-dev mailing list