[LLVMdev] How to get the name and argument of a function

songlh at cs.wisc.edu songlh at cs.wisc.edu
Wed Jan 19 11:47:40 PST 2011


Thanks a lot!

I finally fix my problem.

My code is like this:


//CallInst* pCall   pCall is a printf called in my situation
if( ConstantExpr * pCE = dyn_cast<ConstantExpr>( pCall->getArgOperand(0))){

  if( GlobalVariable * pGV = dyn_cast<GlobalVariable>( pCE->getOperand(0))){

    if( ConstantArray * pCA = dyn_cast<ConstantArray>(
                                  pGV->getInitializer()
                                  )){
        Err << pCA->getAsString() << "\n";
     }
  }

}

I find dump() method is very useful, since it can get me some indications
about which class document I need to check.

thanks a lot!

Linhai









> On 1/17/2011 8:53 PM, songlh at cs.wisc.edu wrote:
>> Thanks a lot!
>>
>> I can get the call place now. But still have problems in getting the
>> argument.
>>
>> CallInst * pCall;
>>
>> ......
>>
>> pCall->getArgOperand(0)->dump();
>>
>> I can get "i8* getelementptr inbounds ([5 x i8]* @.str, i32 0, i32 0)"
>
> In this particular case, the argument to the call is an LLVM GEP
> Constant Expression.  It's like a GEP instruction except that all of the
> GEP arguments are constant, so the result of the GEP is also a
> constant.  In general, the argument to printf can an LLVM Value *, and
> there are lots of sub-classes derived from Value.  For example, it could
> be a GEP ConstantExpr, a Cast ConstExpr, an Instruction, etc., etc.
>
> Look at the doxygen docs for the ConstantExpr class.  You can use that
> information to figure out how to write code that peers into the
> ConstExpr to see what global value it is indexing off of.
>
> -- John T.
>
> P.S.  I grew up in Wisconsin and like Badger Hockey at UW-Madison.  Go
> Bucky!
>
>> I think this means that the variable is a global constant string. Do you
>> know how to get the name of this global string, and how to use the name
>> to
>> get the string value?
>>
>> Btw: when I use pCall->getArgOperand(0)->getName(), I can only get "".
>>
>> thanks a lot!
>>
>> Linhai
>>
>>> songlh at cs.wisc.edu wrote:
>>>> Hi everyone:
>>>>
>>>>      The code I am analyzing is :
>>>>
>>>>      int main()
>>>>      {
>>>>          int i = 0;
>>>>          printf("hello world!");
>>>>          printf( "%d" , i );
>>>>      }
>>>>
>>>>
>>>>
>>>>      I want to get each place where printf is called, and the argument
>>>> used
>>>> during that call.
>>>>
>>>>      so I write llvm pass code like:
>>>>
>>>>      void Myfunction( Function&   F){
>>>>          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))){
>>>>                      //how could I get the function name and argument
>>>>                      //used here
>>>>                  }
>>>>              }
>>>>
>>>>          }
>>>>      }
>>>>
>>>>
>>>>      How could I get the function name and the arguments when I know
>>>> an
>>>> Instruction is a function call?
>>> There's a worked example at
>>> http://wiki.llvm.org/HowTo:_Find_all_call_sites_of_a_function .
>>>
>>> With the CallInst/InvokeInst you can query getArgOperand() to get the
>>> arguments or getCallee() which return a Function -- or it might not. If
>>> the call is an indirect call (ie., function pointer) then you don't
>>> know
>>> what it's calling. Ignoring indirect calls, you can call getName() on a
>>> function to get its name.
>>>
>>> Personally, I rely heavily on the doxygen to find my way around the
>>> API:
>>> http://llvm.org/doxygen/hierarchy.html . Look up "CallInst" for
>>> example.
>>> The getName() method isn't on Function, but all the way on the base
>>> class Value.
>>>
>>> Nick
>>>
>>>>      thanks a lot!
>>>>
>>>>      Best wishes!
>>>>
>>>>                                                    Linhai Song
>>>>
>>>> _______________________________________________
>>>> LLVM Developers mailing list
>>>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>>>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>>>>
>>>
>>
>> _______________________________________________
>> LLVM Developers mailing list
>> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
>> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev
>
>





More information about the llvm-dev mailing list