[LLVMdev] InstVisitor Example

Luke Dalessandro luked at cs.rochester.edu
Tue Apr 14 10:49:08 PDT 2009


On Apr 14, 2009, at 12:48 PM, Brice Lin wrote:

> I just read the LLVM Programmer's Manual, which mentions (but
> specifically does not include any details of) the InstVisitor
> template. Could someone please provide an example of how to use this
> template to find (as an example) all CallSites for the function
> strcpy?

If this is really what you want to do, then the easiest method is to  
just get the declaration of the strcpy function, and iterate over its  
uses.

   Function* const strcpy = m.getFunction("strcpy");
   for (Value::use_iterator i = strcpy->use_begin(), e = strcpy- 
 >use_end(); i != e; ++i)
     if (Instruction* const use = dyn_cast<Instruction>(i)) {
       // Do what you need here
       CallSite call(use);
       ...
     }

If you want to know how to use InstVisitor for other purposes, you  
just derive a class from the InstVisitor template, and overload the  
visit routines that you want.

   class StrcpyVisitor : public InstVisitor<StrcpyVisitor> {
     void handleStrcpy(CallSite strcpy);
     string strcpyName;
    public:
     void visitCallInst(CallInst& call) {
       if (call.getName() == strcpyName)
         handleStrcpy(&call);
     }
     void visitInvokeInst(InvokeInst& invoke) {
       if (call.getName() == strcpyName)
         handleStrcpy(&invoke);
     }
   };

Then, you probably want to inherit from ModulePass, or FunctionPass,  
or BasicBlockPass, or whatever makes sense, and in the relevant  
runOnX(X& x) callback you say:

   bool runOnX(X& x) {
     visit(x);
     return true/false/something_computed;
   }

Hope this helps,
Luke

>
>
> Thanks,
> Brice Lin
> _______________________________________________
> 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