[LLVMdev] how to get and modify a global variable inside a module

Eduardo erocha.ssa at gmail.com
Wed Dec 5 05:58:16 PST 2012

Hi Dong Chen,
I realized you are actually executing the IR, right? I am not sure if
the things I mentioned apply in that case. Actually, I wrote a
optimization pass that modify the IR. It modifies global variables and
the final IR is intended to be compiled.
A snippet of what I do looks like this:

class TestClass : public llvm::ModulePass {
  TestClass() : llvm::ModulePass(TestClass::ID) { }
  virtual bool runOnModule(llvm::Module &m) {
	  for (llvm::Module::global_iterator ii = m.global_begin();
			  ii != m.global_end(); ++ii) {
		  GlobalVariable* gv = ii;
// here you can modify the gv variable (actually I am creating new
globals and deleting the old ones)
	      for (llvm::Value::use_iterator jj = gv->use_begin();
	           jj != gv->use_end(); ++jj) {
// jj is a reference for the user of the global variable

On Wed, Dec 5, 2012 at 11:37 AM, Dong Chen <jameschennerd at gmail.com> wrote:
> hi Eduardo,
> thanks for your attention, i checked the iterator, but i didn't see any
> fucntion can return or modify the main memory address of a global variable
> inside the module.
> can you provide some reference code or program can achieve this?
> thank you very much
> --
> View this message in context: http://llvm.1065342.n5.nabble.com/how-to-get-and-modify-a-global-variable-inside-a-module-tp52289p52294.html
> Sent from the LLVM - Dev mailing list archive at Nabble.com.
> _______________________________________________
> 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