[LLVMdev] allocating an array

HyperQuantum hyperquantum at gmail.com
Tue Jul 22 04:25:57 PDT 2008


Thanks, Bill and Duncan, for your suggestions. I've changed my
approach, and use a pointer instead of an array now.

So this code:

  var size : nat = 9
  var x : [size] int

now becomes:

%size = alloca i32              ; <i32*> [#uses=2]
store i32 9, i32* %size
%x = alloca { i32, i32* }               ; <{ i32, i32* }*> [#uses=6]
load i32* %size         ; <i32>:1 [#uses=1]
getelementptr { i32, i32* }* %x, i32 0, i32 0  ; <i32*>:2 [#uses=1]
store i32 %1, i32* %2
%arr_storage = alloca i32, i32 %1            ; <i32*> [#uses=1]
%arr_dataptr_addr = getelementptr { i32, i32* }* %x, i32 0, i32 1 ;
<i32**> [#uses=1]
store i32* %arr_storage, i32** %arr_dataptr_addr

(did some manual optimizations on this code, as my front end generates
really dumb code)

So I need two allocas now, one for the structure and one for the
storage of the elements.

Now I'm wondering: do you think it's a bad idea to use alloca for
allocating an unknown number of elements? In that case, should I just
always use the malloc instruction instead, or do a runtime decision to
choose between alloca and malloc?


Regards,
Kevin André




More information about the llvm-dev mailing list