Problem about the type of Function's arguement in llvm

in the tutorial of official llvm doc, chapter 3, it deals with arguement of function as follow:

for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
++AI, ++Idx) {
AI->setName(Args[Idx]);

// NamedValues is map<string, Value*>
NamedValues[Args[Idx]] = AI;

and when it try to get the value of arguement, it simply does:

Value *VariableExprAST::Codegen() {
Value *V = NamedValues[Name];
return V ? V : ErrorV(“Unknown variable name”);
}

It means that we can access value of arguement directly.

However, in llvm.org/demo and the chapter 6 of tutorial, it deals with arguemnt:

  1. allocate memory for arguement
  2. then store the arguemnt into the memory.
  3. CreateLoad() to load the arguement when accessing the value of arguement.

I have several problems:
Which way should I follow to get the vale of arguement?

When I tried to use CreateLoad(arguement), the program gives a segmentation error.
Does this result from the wrong type of argument, unsuitable to be the params of CreateLoad()?
If it is, what’s the type of Function->arg_begin() ? (The source code is too nested for me to find the original type of that)

hi

in the tutorial of official llvm doc, chapter 3, it deals with arguement of
function as follow:

for (Function::arg_iterator AI = F->arg_begin(); Idx != Args.size();
++AI, ++Idx) {
AI->setName(Args[Idx]);

// NamedValues is map&lt;string, Value\*&gt;
NamedValues\[Args\[Idx\]\] = AI;

and when it try to get the value of arguement, it simply does:

Value *VariableExprAST::Codegen() {
Value *V = NamedValues[Name];
return V ? V : ErrorV("Unknown variable name");
}

It means that we can access value of arguement directly.

Yes, it is possible to access the values (virtual registers) direclty,
but you may need to construct SSA form by yourself.

However, in llvm.org/demo and the chapter 6 of tutorial, it deals with
arguemnt:
1. allocate memory for arguement
2. then store the arguemnt into the memory.
3. CreateLoad() to load the arguement when accessing the value of arguement.

I have several problems:
Which way should I follow to get the vale of arguement?

If you do not want to construct SSA from by yourself, you probably
should follow this way. You can schedule the "mem2reg"[1] after you
build the IR, it will promote alloca/load/store into scalar virtual
registers for you. (I remember the tutorial had a discussion about
this.)

When I tried to use CreateLoad(arguement), the program gives a segmentation
error.
Does this result from the wrong type of argument, unsuitable to be the
params of CreateLoad()?

Could you paste your code? Had you initialize the IRBuilder with
insert position?

If it is, what's the type of Function->arg_begin() ? (The source code is too
nested for me to find the original type of that)

best regards
ether
[1]http://llvm.org/docs/doxygen/html/Mem2Reg_8cpp_source.html

I read the tutorial doc and some info of SSA, finally understand it. Thanks
for your help.

And the segmentation error of loading arguementation occurs, (gdb error
info)
Program received signal SIGSEGV, Segmentation fault.
0x0855bb68 in llvm::LoadInst::LoadInst(llvm::Value*, char const*, bool,
llvm::Instruction*) ()
code is like follows:
//#include necessary header files

int main(){
    InitializeNativeTarget();
    LLVMContext Context;
    module = new Module("Program", Context);

    vector<Type *> argslist;
    argslist.push_back(Type::getDoubleTy(Context));
    FunctionType *funType = FunctionType::get(Type::getDoubleTy(Context),
argslist, false);
    Function *fun= cast<Function> ( module->getOrInsertFunction("fun",
funType));

    BasicBlock * block = BasicBlock::Create(Context, "mainBlock", fun);
    IRBuilder<> builder(block);
    Function::arg_iterator itr = fun->arg_begin();
    builder.CreateLoad(itr);

    Value *result = ConstantFP::get(Type::getDoubleTy(getGlobalContext()),
1.0);
    builder.CreateRet(result);

    vector<GenericValue> args;
    ExecutionEngine *e = EngineBuilder(module).create();
    outs()<<*module<<"\n";
    outs().flush();
    return 0;
}

I'm confused that arg_iterator itr ( Function::arg_iterator itr =
fun->arg_begin(); ) can be used in CreateStore() function, however, it can
not be used as the arguments of CreateLoad() function.

I read the tutorial doc and some info of SSA, finally understand it. Thanks
for your help.

And the segmentation error of loading arguementation occurs, (gdb error
info)
Program received signal SIGSEGV, Segmentation fault.
0x0855bb68 in llvm::LoadInst::LoadInst(llvm::Value*, char const*, bool,
llvm::Instruction*) ()
code is like follows:
//#include necessary header files

int main(){
InitializeNativeTarget();
LLVMContext Context;
module = new Module("Program", Context);

vector&lt;Type \*&gt; argslist;
argslist\.push\_back\(Type::getDoubleTy\(Context\)\);
FunctionType \*funType = FunctionType::get\(Type::getDoubleTy\(Context\),

argslist, false);
Function *fun= cast<Function> ( module->getOrInsertFunction("fun",
funType));

BasicBlock \* block = BasicBlock::Create\(Context, &quot;mainBlock&quot;, fun\);
IRBuilder&lt;&gt; builder\(block\);
Function::arg\_iterator itr = fun\-&gt;arg\_begin\(\);
builder\.CreateLoad\(itr\);

Value \*result = ConstantFP::get\(Type::getDoubleTy\(getGlobalContext\(\)\),

1.0);
builder.CreateRet(result);

vector&lt;GenericValue&gt; args;
ExecutionEngine \*e  = EngineBuilder\(module\)\.create\(\);
outs\(\)&lt;&lt;\*module&lt;&lt;&quot;\\n&quot;;
outs\(\)\.flush\(\);
return 0;

}

I'm confused that arg_iterator itr ( Function::arg_iterator itr =
fun->arg_begin(); ) can be used in CreateStore() function, however, it can
not be used as the arguments of CreateLoad() function.

You cannot load from the argument in because the type of the argument
is double instead of double*, in fact, the C equivalence of the LLVM
IR create by you is something looks like this:
double fun(double a) {
  c = *a; // Illegal, a is not a pointer
  ...
}

To get the value of the argument, you can use the Argument object
directly (the first way in your first mail) or follow the second way
described in you first mail:

1. allocate memory for arguement
2. then store the arguemnt into the memory.
3. CreateLoad() to load the arguement when accessing the value of arguement.

best regards
ether