[LLVMdev] ModulePass and Strings

John Criswell criswell at illinois.edu
Mon Nov 21 08:05:09 PST 2011


On 11/21/11 6:35 AM, Deu Arm wrote:
> Hi everybody,
>
> I am writing an LLVM pass and I want to iterate over the whole module 
> (including global variables), that's why I use ModulePass instead of 
> FunctionPass. But I don't know how to do it. Using Module::iterator 
> seams to iterate only over functions. But I need to iterate over all 
> the Instructions in the module. How should I do such an iteration?

To iterate over all the instructions, you need essentially need to do 
the following:

for all Functions F in Module M)
     for all Basic Blocks BB in Function F
         for all Instructions I in Basic Block BB
             do_something_with I

That said, if you need to process certain instructions within the 
module, the best thing to do is to make your pass inherit from the 
InstVisitor<> class 
(http://llvm.org/doxygen/classllvm_1_1InstCombiner.html).  You basically 
inherit from InstVisitor like so:

class MyPass : public ModulePass, InstVisitor<MyPass> {
     ...
}

... and then implement a visit method for each type of instruction that 
you want to handle.  In your runOnModule() method, call visit(M) to 
start iterating over every instruction in the Module.  The visit() 
method will call the appropriate visitXXX() method for each instruction.

For an example, see the Load/Store Instrumentation pass from SAFECode:

http://llvm.org/viewvc/llvm-project/safecode/trunk/lib/InsertPoolChecks/LoadStoreChecks.cpp?view=markup&pathrev=144620
http://llvm.org/viewvc/llvm-project/safecode/trunk/include/safecode/LoadStoreChecks.h?view=markup&pathrev=144620

> Also, I would like to find all the "string" variables and do some 
> changes to them. But LLVM's IR language does not have such a type. 
> Tell me, please, what should I use.

Strings are usually encoded as GlobalVariable's.  To find them, use the 
global_begin() and global_end() iterators to iterate over all 
GlobalVariables in the program.  Strings are pointer to arrays of type i8.

-- John T.

>
> Sincerely,
> Shuhmacher
>
>
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20111121/f7753aff/attachment.html>


More information about the llvm-dev mailing list