tags:

views:

282

answers:

2

I've been looking at some LLVM assembly produced by llvm-gcc lately and I've noticed a recurring statement of which I'm not sure its purpose.

For example, the following C program:

int main(void)
{
   void (*f)(void) = (0x21332);
   f();
}

When compiled with "llvm-gcc -emit-llvm -S" will produce the following code (irrelevant parts removed):

define i32 @main() nounwind {
entry:
   %retval = alloca i32  ; <i32*> [#uses=1]
   %f = alloca void ()*  ; <void ()**> [#uses=2]
   %"alloca point" = bitcast i32 0 to i32    ; <i32> [#uses=0]
   store void ()* inttoptr (i64 135986 to void ()*), void ()** %f, align 4
   %0 = load void ()** %f, align 4   ; <void ()*> [#uses=1]
   call void %0() nounwind
   br label %return

I'm interested in the purpose of the line:

%"alloca point" = bitcast i32 0 to i32   ; <i32> [#uses=0]

Doesn't seem to do anything as the variable it assigns to is never used again and the bitcast itself is pointless. All I can think of is that its inserted really as a nop for later code generation / analysis purposes, indicating interesting parts of the code.

A: 

Found this on the internets: Allocas whose size can be determined at compile time will be allocated space on the stack when the stack frame size is calculated. For variable sized allocas the target specific code will have to resize the stack, adjusting the frame pointer and stack pointer as needed, and adjust the locations for the outgoing parameters to the top of the stack.

sounds like its there to make some stack space work out correctly.

Joe Caffeine
The question isn't about alloca itself. The OP is asking about the instruction named "alloca point", which appears to be a no-op.
Jay Conrod
+4  A: 

From the llvm-gcc source: gcc/llvm-convert.cpp, it's just used as a helper Value* and it will be removed by a dead instruction elimination pass.

// Create a dummy instruction in the entry block as a marker to insert new
// alloc instructions before.  It doesn't matter what this instruction is,
// it is dead.  This allows us to insert allocas in order without having to
// scan for an insertion point. Use BitCast for int -> int
Nathan Howell
Yes this is right. Also asked this question on LLVM mailing list and got similar respone, to be precise:"It's there as a placeholder for where to insert temporaries: alloca's go before the bitcast, and actual code generation starts after it. It's simply a convenience for the front-end; instcombine will eliminate it, and basically everything else will simply ignore it."
David Terei