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

Arnamoy Bhattacharyya arnamoy at ualberta.ca
Tue Nov 5 05:26:00 PST 2013


Thanks for the reply.

For the source code:

int main()
{
int a = 0;
int *p;
p = &a;
call_arnamoy(p);
int *p1;
p1 = p;
return 1;
}

The bit code:

  %retval = alloca i32, align 4
  %a = alloca i32, align 4
%p = alloca i32*, align 8
 %p1 = alloca i32*, align 8
 store i32 0, i32* %retval
store i32 0, i32* %a, align 4
store i32* %a, i32** %p, align 8
%0 = load i32** %p, align 8
%call = call i32 @arnamoy(i32* %0)
%1 = load i32** %p, align 8
store i32* %1, i32** %p1, align 8
ret i32 1

I need to know where the pointer "p" is used in the IR.  But when I
try to run the use_iterator on callInst->getArgOperand(0), it return
only one instruct that is the Call instruction itself.  But not the
store i32* %a, i32** %p, align 8, %1 = load i32** %p, align 8 and %0 =
load i32** %p, align 8  I need all of those.  I am guessing this is
happening because the argument is promoted to register.  Is there a
way of knowing the name of the variable the value of which is stored
in the register?

If it's not possible, any work-around suggestions?

Thanks a lot again;

On Tue, Nov 5, 2013 at 12:41 PM, Henrique Santos
<henrique.nazare.santos at gmail.com> wrote:
> 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
>
>



-- 
Arnamoy Bhattacharyya
Athabasca Hall 143
Department of Computing Science - University of Alberta
Edmonton, Alberta, Canada, T6G 2E8
587-710-7073



More information about the llvm-dev mailing list