Alloca Outside of Entry Block

Dear All,

Is it legal to have an alloca in a basic block other than a function's entry block?

-- John T.

How else could you generate code for:

#include <stdlib.h>

void *vp;
size_t foo(int);
void watch(void *);

void* bar(int i) {
   if (i)
     vp = alloca(foo(i));
   watch (vp);
}

? The answer should be yes.

Is it legal to have an alloca in a basic block other than a function's
entry block?

It is legal, but mem2reg will not promote as far as I know.

Ciao,

Duncan.

This is the right answer for C's alloca. The question probably referred to LLVM IR's alloca, however.

Is there a difference?

The code I gave produced the below with llvm-gcc. It appears to me to use llvm's alloca in other than the entry block, no?

; ModuleID = 'ta.c'
target datalayout = "e-p:32:32:32-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64-f80:128:128"
target triple = "i386-apple-darwin9"
@vp = common global i8* null ; <i8**> [#uses=2]

define i8* @bar(i32 %i) nounwind {
entry:
  %i_addr = alloca i32 ; <i32*> [#uses=3]
  %retval = alloca i8* ; <i8**> [#uses=1]
  %"alloca point" = bitcast i32 0 to i32 ; <i32> [#uses=0]
  store i32 %i, i32* %i_addr
  %tmp = load i32* %i_addr, align 4 ; <i32> [#uses=1]
  %tmp1 = icmp ne i32 %tmp, 0 ; <i1> [#uses=1]
  %tmp12 = zext i1 %tmp1 to i8 ; <i8> [#uses=1]
  %toBool = icmp ne i8 %tmp12, 0 ; <i1> [#uses=1]
  br i1 %toBool, label %bb, label %bb8

bb: ; preds = %entry
  %tmp3 = load i32* %i_addr, align 4 ; <i32> [#uses=1]
  %tmp4 = call i32 @foo( i32 %tmp3 ) nounwind ; <i32> [#uses=1]
  %tmp6 = alloca i8, i32 %tmp4 ; <i8*> [#uses=1]
  store i8* %tmp6, i8** @vp, align 4
  br label %bb8

bb8: ; preds = %bb, %entry
  %tmp9 = load i8** @vp, align 4 ; <i8*> [#uses=1]
  call void @watch( i8* %tmp9 ) nounwind
  br label %return

return: ; preds = %bb8
  %retval11 = load i8** %retval ; <i8*> [#uses=1]
  ret i8* %retval11
}

declare i32 @foo(i32)

declare void @watch(i8*)

Yes it is legal, but for performance reasons you want to avoid that.

-Tanya

John Criswell wrote:

Dear All,

Is it legal to have an alloca in a basic block other than a function's
entry block?
  

Thanks everyone for the answers. My original question was referring to the LLVM IR and not standard C.

So, the consensus seems to be that it is legal, which makes sense since LLVM supports the C alloca function.

FWIW, I'm not inserting allocas into code; rather I'm instrumenting them and found a test case where an alloca does not appear in an entry block (176.gcc from SPEC2000, if you're curious). I needed to know whether that was an error or whether my instrumentation needs to handle that properly.

Thanks again.

-- John T.

Yeah, legal, but the performance sucks because it codegens into a call
to the C library's alloca function.

It's probably worth documenting in LangRef because the difference in
the generated code is so striking.

-Eli

Thanks everyone for the answers. My original question was referring to
the LLVM IR and not standard C.

So, the consensus seems to be that it is legal, which makes sense since
LLVM supports the C alloca function.

Yeah, legal, but the performance sucks because it codegens into a call
to the C library's alloca function.

This is only true for some targets (windows?). Most targets just manipulate the stack pointer inline.

It's probably worth documenting in LangRef because the difference in
the generated code is so striking.

This would be fine,

-Chris