Could you give an example of the old instruction cannot be removed safely? Since the new instruction would produce the same context/result of the old one, I suppose it is safe to remove the old one when the new instruction is ready (inserted after the old one). Anything I missed here? Thanks, -Thomson On Mon, Jun 18, 2012 at 7:31 PM, Christoph Erhardt <christoph at sicherha.de>wrote:> Hi Thomson, > > > 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? > whenever an instruction is replaced, all other instructions that use its > result (as an input operand) have to be told to use the result of the > new instruction instead. That's what CI->replaceAllUsesWith(Result) > does. Only then can the old instruction be removed from the basic block. > > Best regards, > Christoph >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120618/f993ef47/attachment.html>
Christoph Erhardt
2012-Jun-18 12:09 UTC
[LLVMdev] Which pass converts call printf to puts?
Hi Thomson,> Could you give an example of the old instruction cannot be removed > safely? Since the new instruction would produce the same context/result > of the old one, I suppose it is safe to remove the old one when the new > instruction is ready (inserted after the old one). Anything I missed here?yes, you're missing that LLVM programs are in SSA form: A new instruction always produces a new result. Imagine a graph in which every instruction is represented as a node and every use-relationship is represented as a directed edge. Now if you want to replace a node with another node, you have to make sure to properly reconnect all incoming and outgoing edges of the old node, otherwise they'll be dangling. Best regards, Christoph
This makes sense now. So if the instruction doesn't make any assignment, it would be unnecessary to replace any operand reference and the same instruction(CI) would be returned from the inner function to avoid this replacement, is this right? Thanks, -Thomson On Mon, Jun 18, 2012 at 8:09 PM, Christoph Erhardt <christoph at sicherha.de>wrote:> Hi Thomson, > > > Could you give an example of the old instruction cannot be removed > > safely? Since the new instruction would produce the same context/result > > of the old one, I suppose it is safe to remove the old one when the new > > instruction is ready (inserted after the old one). Anything I missed > here? > yes, you're missing that LLVM programs are in SSA form: A new > instruction always produces a new result. > > Imagine a graph in which every instruction is represented as a node and > every use-relationship is represented as a directed edge. Now if you > want to replace a node with another node, you have to make sure to > properly reconnect all incoming and outgoing edges of the old node, > otherwise they'll be dangling. > > Best regards, > Christoph >-------------- next part -------------- An HTML attachment was scrubbed... URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20120618/7d0d8338/attachment.html>