[LLVMdev] Accessing merged globals through PointerType

Rob Jansen jansen at cs.umn.edu
Sat Oct 13 10:11:32 PDT 2012


Hi,

I am trying to create a pass that is similar to the GlobalMerge code found
here:
http://llvm.org/docs/doxygen/html/GlobalMerge_8cpp_source.html

I am concerned with lines 149-163 of the above file. From the documentation
at the top of the file, it will convert this:

static int foo[N], bar[N], baz[N];

for (i = 0; i < N; ++i) {

  foo[i] = bar[i] * baz[i];
}

Into something like this (slightly modified):

struct mergestruct {
  int foo[N];
  int bar[N];
  int baz[N];
};

static struct mergestruct merged;
for (i = 0; i < N; ++i) {
  merged.foo[i] = merged.bar[i] * merged.baz[i];
}

Great, now in addition I want to use an extra pointer to access the
elements, like this:

struct mergestruct *merged_ptr = &merged;
for (i = 0; i < N; ++i) {
  merged_ptr->foo[i] = merged_ptr->bar[i] * merged_ptr->baz[i];
}

So I add something like this after line 153:

PointerType *MergedPointerType = PointerType::get(MergedTy, 0);
GlobalVariable *MergedPointer = new GlobalVariable(M, MergedPointerType,
false,
  GlobalValue::ExternalLinkage, MergedGV, "_MergedGlobalsPtr");

This is where I'm stuck. How can I use replaceAllUsesWith() to replace uses
of each global with accesses through the new MergedPointer instead of
through indices into the MergedGV struct as is currently done? Is this
possible?

Thanks,
Rob
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20121013/ab9a170a/attachment.html>


More information about the llvm-dev mailing list