[LLVMdev] Problem about the type of Function's arguement in llvm

Hongbin Zheng etherzhhb at gmail.com
Sun Apr 22 07:16:42 PDT 2012


hi

On Sun, Apr 22, 2012 at 8:36 PM, Jianfei Hu <hujianfei258 at gmail.com> wrote:
> 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.
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




More information about the llvm-dev mailing list