<font face="verdana,sans-serif">Ok. So it seems CI->eraseFromParent() removed the old instruction and the new one is inserted right after this one in the inner function in the case of printf->puts. There is another line CI->repalceAllUsesWith(Result). I think this line could also do the replacement besides inserting the new one in the inner function. What's the difference of these 2 replacement methods?</font><div>
<font face="verdana, sans-serif"><br></font></div><div><font face="verdana, sans-serif">Also thanks for your reminder of CC the mailing list.</font></div><div><font face="verdana, sans-serif">-Thomson</font></div><div><br>
<div class="gmail_quote">On Mon, Jun 18, 2012 at 4:08 PM, Christoph Erhardt <span dir="ltr"><<a href="mailto:christoph@sicherha.de" target="_blank">christoph@sicherha.de</a>></span> wrote:<br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
Hi Thomson,<br>
<br>
the new call to puts() is inserted right away, whereas the old call to<br>
printf() is removed a bit later in SimplifyLibCalls::runOnFunction(). If<br>
you browse the code a bit and backtrack the call stack to see what<br>
happens with the return value of PrintFOpt::OptimizeFixedFormatString(),<br>
you will stumble upon this segment in SimplifyLibCalls.cpp:1703ff.:<br>
<br>
// Try to optimize this call.<br>
Value *Result = LCO->OptimizeCall(CI, TD, TLI, Builder);<br>
if (Result == 0) continue;<br>
<br>
DEBUG(dbgs() << "SimplifyLibCalls simplified: " << *CI;<br>
dbgs() << " into: " << *Result << "\n");<br>
<br>
// Something changed!<br>
Changed = true;<br>
++NumSimplified;<br>
<br>
// Inspect the instruction after the call (which was potentially just<br>
// added) next.<br>
I = CI; ++I;<br>
<br>
if (CI != Result && !CI->use_empty()) {<br>
CI->replaceAllUsesWith(Result);<br>
if (!Result->hasName())<br>
Result->takeName(CI);<br>
}<br>
CI->eraseFromParent();<br>
<br>
Best regards,<br>
Christoph<br>
<br>
P.S. When answering, don't forget to CC the mailing list.<br>
<div class="HOEnZb"><div class="h5"><br>
On 18/06/2012 09:22, Thomson wrote:<br>
> Thanks for all your information. I got the pass in SimplifyLibCalls.cpp.<br>
><br>
> I looked at the code, but am still a little confused about the IR<br>
> instruction replacement model. Use the following specific optimization<br>
> as example, it looks to me that even if a new call instruction (puts) is<br>
> created in EmitPutS, but the returned one is still the original one<br>
> (CI). So I am very curious about how the call instruction is replaced here.<br>
><br>
> // printf("%s\n", str) --> puts(str)<br>
> if (FormatStr == "%s\n" && CI->getNumArgOperands() > 1 &&<br>
> CI->getArgOperand(1)->getType()->isPointerTy()) {<br>
> EmitPutS(CI->getArgOperand(1), B, TD);<br>
> return CI;<br>
> }<br>
><br>
> -Thomson<br>
</div></div></blockquote></div><br><br>
</div>