ModulePass and Strings

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?
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.


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 (). You basically inherit from InstVisitor like so: class MyPass : public ModulePass, InstVisitor { … } … 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: 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.