Function arguments pass by value or by reference.

Now, I am using LLVM-3.3 do some process with functions, however there are some difficult things I can’t handle by myself. So,

I want get your help to get it down properly.

Q1. There is a function declaration:

call i32 @create(i64* %tid, %union.t* %pab, i8* (i8*)* @worker, i8* null) // callInst

Store instruction goes like this:

store i8* (i32, double, i32*)* %fp, i8* (i32, double, i32*)** %fp.addr // storeInst

I want to determine the type of the operands are function pointer or not? (That’s what I want)

However, callInst->getOperand(2)->getType()->getTypeID() always return 14 that’a the enum number of

PointerTypeID. For the store instruction, first operand is the same as the operation with call instruction.

How can I get the function pointer properly?

Q2. Function arguments can be passed by value or by pointer in C-programming language.

int arr[10];
struct node {
int a;
int att[10];
double ul;
};

struct node Node;

testStruct(Node);

testStructPointer(&Node);

After compiled and change it to .ll file, it looks like this:

call void @testStruct(%struct.node* byval align 8 @Node), !dbg !160

call void @testStructPointer(%struct.node* @Node), !dbg !161

You can see that function named testStruction’s parameter is passed by value while the testStructPointer passed by pointer.

I used callInst->getOperand(0)->getType()->dump(), it always return the same %struct.node*.

I want to know is there some properly library functions can be used to get the precise parameters type?

I want to know the functions arguments are passed by value or by pointer?

Any advice would be appreciate. Thanks a lot.:slight_smile:

Instead of looking at the Type ID, you should use isa to determine if the value has function type: if (isa(callInst->getOperand(2)->getType()) { … } In LLVM, functions are global variables and therefore are a pointer type. In LLVM, all parameters are passed by value unless they have the byval attribute. There’s probably a method in the Argument class that will tell you whether the argument has the byval attribute. If it’s not an attribute of the Argument class, it’s probably part of the type of the Function. Just out of curiosity, are you using the doxygen documentation? The doxygen documentation is invaluable; if you have a basic understanding of how LLVM uses its class hierarchy to represent the IR, you can usually find what you need via the doxygen docs. Regards, John Criswell

I think you mean that they are "by reference unless they have a byval"
attribute?

The "ByVal" is an attribute on the call instruction.

Here's where my compiler sets that:
https://github.com/Leporacanthicus/lacsap/blob/master/expr.cpp#L1260
(The "ByVal" attribute itself is stored in the attribute container on line
1235)

And also matching attributes in the function itself, see:
https://github.com/Leporacanthicus/lacsap/blob/master/expr.cpp#L1386

Sorry. You are correct. I got the SSA value (which is always passed by value) mixed up with the memory to which it points. It’s been awhile since I’ve had to deal with byval. I took a quick look at Doxygen; the Argument class also has a byval attribute (Argument::hasByValAttr()). I think both the call instruction and the Argument have the attribute and need to match in order to get defined behavior. Regards, John Criswell