<div dir="ltr">Hello there, Pierre,<div><br></div><div>From your example code, there are two things we need to define:</div><div>1. This instruction is a definition (which can be used by others):</div><div>   <b>%call</b> = call noalias i8* @malloc(i64 10) #2</div><div>2. This instruction is a user of that definition showed above:</div><div>   store i8* <b>%call</b>, i8** %ptr, align 8<br><div class="gmail_extra"><br></div><div class="gmail_extra">What you are doing when you cast a Use of an instruction is that you are going through all the uses of an Instruction, which ends up being the same instruction from 1. As an example, if you had these three instructions in your code:</div><div class="gmail_extra">[...]<br clear="all"><div><div class="gmail_signature" data-smartmail="gmail_signature">   <b>%call</b> = call noalias i8* @malloc(i64 10) #2</div><div class="gmail_signature" data-smartmail="gmail_signature">[...]</div><div class="gmail_signature" data-smartmail="gmail_signature">   store i8* <b>%call</b>, i8** %ptr, align 8</div><div class="gmail_signature" data-smartmail="gmail_signature">[...]</div><div class="gmail_signature" data-smartmail="gmail_signature">   %arrayidx = getelementptr inbounds i8, i8* <b>%call</b>, i32 %add.i</div><div class="gmail_signature" data-smartmail="gmail_signature">[...]</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">And you iterate through all the uses, you would get:</div><div class="gmail_signature" data-smartmail="gmail_signature">Instruction: <b>%call</b> = call noalias i8* @malloc(i64 10) #2</div><div class="gmail_signature" data-smartmail="gmail_signature">Instruction: <b>%call</b> = call noalias i8* @malloc(i64 10) #2<br></div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">As a result. This is because the same %call, defined by this instruction, is being used on each of the instructions above (the store and the gep). Now, if you want to get the <u>user</u> of an instruction (which would be the store and the gep instructions above), you would have to explicitly ask for the user with:</div><div class="gmail_signature" data-smartmail="gmail_signature">Instruction *I = dyn_cast<Instruction>(<b>use_i->getUser()</b>)<br></div><div class="gmail_signature" data-smartmail="gmail_signature">Which results in:</div><div class="gmail_signature" data-smartmail="gmail_signature"><div class="gmail_signature" data-smartmail="gmail_signature">Instruction: store i8* <b>%call</b>, i8** %ptr, align 8</div><div class="gmail_signature" data-smartmail="gmail_signature">Instruction: %arrayidx = getelementptr inbounds i8, i8* <b>%call</b>, i32 %add.i<br></div><div><br></div></div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">However, keep in mind this line will get you all the instructions that <u>use</u> that %call value, and not the %call definition itself. In other words, you will not get "<b>%call</b> = call noalias i8* @malloc(i64 10) #2" from this example code of yours, but only the store.</div><div class="gmail_signature" data-smartmail="gmail_signature"><br></div><div class="gmail_signature" data-smartmail="gmail_signature">I hope this helps.</div><div class="gmail_signature" data-smartmail="gmail_signature"><br>--<br>Cristianno Martins<br>PhD Student of Computer Science<br>University of Campinas<br><a href="mailto:cmartins@ic.unicamp.br" target="_blank">cmartins@ic.unicamp.br</a><br><a href="mailto:cristiannomartins@hotmail.com" target="_blank"></a></div></div>
<br><div class="gmail_quote">On Thu, Jun 30, 2016 at 6:05 AM, Pierre Gagelin via llvm-dev <span dir="ltr"><<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">llvm-dev@lists.llvm.org</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0px 0px 0px 0.8ex;border-left-width:1px;border-left-color:rgb(204,204,204);border-left-style:solid;padding-left:1ex"><div dir="ltr"><div><div><div><div><div><div><div>Hi,<br><br></div>I try to follow the use of the value yielded by a malloc call. Here follows a concrete example of what I expect and what I get.<br><br>define void @main() #0 {<br>entry:<br>  %ptr = alloca i8*, align 8<br>  %call = call noalias i8* @malloc(i64 10) #2<br>  store i8* %call, i8** %ptr, align 8<br>  %0 = load i8*, i8** %ptr, align 8<br>  call void @free(i8* %0) #2<br>  ret void<br>}<br><br></div>It is a simple program that allocates and frees a pointer. Now I intercepted the call instruction to malloc and iterated over its uses. I expected to have 2 results:<br><br>  %call = call noalias i8* @malloc(i64 10) #2<br></div>and<br>  store i8* %call, i8** %ptr, align 8<br><br></div>But I only get the first one. Is it normal? What should I do to get both? Here the piece of code I used to iterate:<br><br>CallInst *CI = ...The call to malloc...<br>errs() << "  Value use:\n";<br>  for(Value::use_iterator i = CI->use_begin(), e = CI->use_end(); i != e; ++i) {<br>    Use *use_i = &*i;<br>    if(Instruction *I = dyn_cast<Instruction>(use_i)) {<br>      errs() << "    instruction: " <<<br>        *I << "\n";<br>    }<br>  }<br><br></div>And I get this output:<br><br>  Value use:<br>    instruction:   %call = call noalias i8* @malloc(i64 10) #2<br><br></div>Thanks for your time and help,<br></div>Pierre<br><div><div><div><br></div></div></div></div>
<br>_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a><br>
<a href="http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer" target="_blank">http://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
<br></blockquote></div><br></div></div></div>