guillau4
guillau4

Reputation: 177

Get name of a function argument

I am creating an LLVM pass and I don't understand something : when I look into the .ll file the argument of a function has a name :

call void @_ZNK2xi9spawnable9SpawnableIFvbdEEclEbd( %"class.xi::spawnable::Spawnable.0"* nonnull @_ZN2xi9spawnable2f2E, i1 zeroext %9, double %10)

So here the first argument name seems to be _ZN2xi9spawnable2f2E.

But in my pass when I use the function getName() it returns me an empty string. When I access the full argument I obtain : %"class.xi::spawnable::Spawnable.0"* %1

How can I obtain the same name as in the .ll file?

EDIT: This is a part of the code (I tried to clean it up a little so maybe there are some missing brackets)

virtual bool runOnFunction(Function &F){
  LoopInfo &LI = getAnalysis<LoopInfoWrapperPass>().getLoopInfo();
  std::string Name = demangle(F.getName ());     
  outs() << "Function "<< *(F.getFunctionType()) <<" " << Name << " {\n";
  for(LoopInfo::iterator i = LI.begin(), e = LI.end(); i!=e; ++i)
    BlocksInLoop (*i,0);
  for( Function::iterator b = F.begin() , be = F.end() ;b != be; ++b){
    for(BasicBlock::iterator i = b->begin() , ie = b->end();i != ie; ++i){
      if(isa<CallInst>(&(*i)) || isa<InvokeInst>(&(*i))){
        if (!(i->getMetadata("seen"))){
          Function * fct =NULL;
          if (isa<CallInst>(&(*i)))
            fct = cast<CallInst>(&(*i))->getCalledFunction();
          if (isa<InvokeInst>(&(*i)))
            fct = cast<InvokeInst>(&(*i))->getCalledFunction(); 
          if (fct){
            outs()<<"Call " << *(fct->getFunctionType()) <<" "<< demangle(fct->getName()) << "\n";
          for(Function::arg_iterator argi=fct->arg_begin(),arge=fct->arg_end(); argi!=arge;argi++ )
            outs()<< argi->getName()<<"\n";
          }
        }
      }
    }
  }
  outs() << "}\n";
  return(false);
};

Upvotes: 1

Views: 1155

Answers (1)

arrowd
arrowd

Reputation: 34411

You are analyzing not the call site, but the function itself. When you are looking at the function, you only have formal parameters and can't know what values are passed there.

Instead of calling ->getCalledFunction() and iterating over its args, you should iterate over cast<CallInst>(&(*i)) operands. See ->op_begin() and value_op_begin() methods.

Upvotes: 3

Related Questions