7

How to determine all global variables of a LLVM module?

I want to modify them using a module pass.

Peter W.
  • 73
  • 3

2 Answers2

10

llvm::Module class has getGlobalList() method:

/// Get the Module's list of global variables.
GlobalListType         &getGlobalList()             { return GlobalList; }

So you can do something like:

for (auto &Global : M->getModule()->getGlobalList()) ...
Stanislav Pankevich
  • 11,044
  • 8
  • 69
  • 129
0

In LLVM-17, getGlobalList() has been made a private method. The list of global variables can be accessed via iterators (both mutable and const versions are available): global_begin() and global_end(). Referring to the code of Module.h makes it quite clear. A for loop like the following will do the needful:

   for (auto Global = M->getModule()->global_begin();
             Global != M->getModule()->global_end();
             ++Global)
Hari
  • 1,561
  • 4
  • 17
  • 26