[LLVMdev] Why function pointer is different from other data type?

Nick Lewycky nicholas at mxc.ca
Mon Apr 12 08:21:26 PDT 2010


Hao Shen wrote:
> Dear all,
>
> I compiled c program with llvm and found something strange with
> function pointer as following small example .
>
> ------------- In C Code --------------
> float a (int value) {
>      return value + 1;
> };
>
> typedef float (*funcptr_t) (int);
>
> funcptr_t get_ptr(funcptr_t p) {
>      return p;
> }
>
> float result = get_ptr(a)(4);
>
> ------------- In LLVM Code --------------
>
> %4 = call float (i32)* (float (i32)*)* @get_ptr(float (i32)* @a1)
> nounwind ;<float (i32)*>  [#uses=1]
>                ~~~~~~~~~~~~~~~~~~~~  VERY STRANGE RETURN TYPE !!!
> %5 = call float %4(i32 4) nounwind              ;<float>  [#uses=1]
>
>
> Why we need duplicated return type?

Maybe I've just been reading LLVM IR for too long, but this looks 
completely normal to me. :-)

To answer the question in the subject line, a function pointer is just 
yet another type, like a struct pointer or a char pointer, etc. The 
'float (i32)' indicates a function that takes an i32 and returns a 
float, so 'float (i32)*' is the type of a pointer to that function. This 
is your funcptr_t. That makes 'float (i32)* (float (i32)*)*' the exact 
type you'd expect for get_ptr(); it takes a funcptr_t and it returns a 
funcptr_t. Note that the type is the type of @get_ptr itself, not the 
type that @get_ptr returns.

Why a duplicated return type? The part after the ; is a plain-text 
comment. By default LLVM prints <return type> [#uses=X] for every 
non-void instruction. Note that the <> aren't part of the type in this case.

Nick



More information about the llvm-dev mailing list