[LLVMdev] Identifying the instructions that uses a pointer used as a function argument

Henrique Santos henrique.nazare.santos at gmail.com
Tue Nov 5 03:41:25 PST 2013


I think I understood the problem you're trying to solve. I didn't, however,
understand the problems you are having (maybe you're not running mem2reg?).
I do have a little piece of code that I think does (more or less) what you
want.
Take a look at it, try understanding it, and see if it helps you out.
You might have to change it a bit since you're replacing uses after an
instruction and not a basic block.
Replace the "auto"s with the iterator types if you're not using C++0x.
Also, sorry for the lack of comments. : )

void replaceUsesOfWithAfter(Value *V, Value *R, BasicBlock *BB) {
  set<Instruction*> Replace;
  for (auto UI = V->use_begin(), UE = V->use_end(); UI != UE; ++UI)
    if (Instruction *I = dyn_cast<Instruction>(*UI)) {
      if (I != R && DT_->dominates(BB, I->getParent()))
        Replace.insert(I);
      else if (PHINode *Phi = dyn_cast<PHINode>(*UI))
        for (unsigned Idx = 0; Idx < Phi->getNumIncomingValues(); ++Idx)
          if (Phi->getIncomingValue(Idx) == V &&
              DT_->dominates(BB, Phi->getIncomingBlock(Idx)))
            Phi->setIncomingValue(Idx, R);
    }

  for (auto& I : Replace)
    I->replaceUsesOfWith(V, R);
}

If this doesn't help, you could try showing us the actual IR you're running
the transformation on.

Just FMI, is it okay to post code on this mailing list?

H.



On Tue, Nov 5, 2013 at 8:59 AM, Arnamoy Bhattacharyya
<arnamoy at ualberta.ca>wrote:

> Hello all;
>
> So here is my goal:
>
> *** If there is a Call instruction to myFunction(int * val), I need to
> identify all the instructions that uses val in the IR and replace the
> uses with a newly created pointer.  I will also be changing the
> argument of myFunction to the new pointer.
>
> int * val = malloc/calloc;
> ...
> myFunction(val);
> ....
> *val = 45;
>
> becomes==
>
> int * val = malloc/calloc;
> int * val1 = malloc/calloc;
> ...
> myFunction(val1);
> ....
> *val1 = 45;
>
> I thought traversing the use-def chain of the function argument will
> give me the uses of the pointer but it is giving me only one use.
> (the call instruction to myFunc())
>
> Is it happening because val is a pointer and the value is stored into
> register first?  How can I identify the uses of val in the IR?  (And
> if again val is assigned to a new pointer, the uses of that new
> pointer?  e.g int * new_val = val; all the uses of new_val)
>
> **I don't know if this is the right path to go, but if I could get the
> name of the pointer used as an function argument, I could match the
> name against other instructions that use the same named pointer.  But
> this is also not working because first the pointer is loaded into
> register and then the register is passed in the argument.
>
> Sorry if the question does not make sense.  Any pointer or explanation
> will be appreciated.
>
> Thanks a lot!!
> --
> Arnamoy Bhattacharyya
> Athabasca Hall 143
> Department of Computing Science - University of Alberta
> Edmonton, Alberta, Canada, T6G 2E8
> 587-710-7073
> _______________________________________________
> 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/20131105/f354635d/attachment.html>


More information about the llvm-dev mailing list