<div dir="ltr"><div>Hi Daniel,</div><div><br></div>[CloneFunctionInto](<a href="http://llvm.org/doxygen/namespacellvm.html#adea5a1faf61727d5a3c00684a22d8df6">http://llvm.org/doxygen/namespacellvm.html#adea5a1faf61727d5a3c00684a22d8df6</a>) helps you copy entire functions. It only takes care of the copy though, you'll have to empty out the replaced function manually.<div><br></div><div>Cheers,</div><div>Philip</div></div><br><div class="gmail_quote"><div dir="ltr">On Tue, Aug 28, 2018 at 5:53 PM Daniel Moya via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org">llvm-dev@lists.llvm.org</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div dir="ltr">Hello LLVM Developers,<div><br></div><div>I'm trying to replace a function defined in one module into another module (different files). The first issue I ran into was that llvm::Function does not have a method "moveBefore" or "moveAfter" as the llvm::BasicBlock or llvm::Instruction do, so I figured I would just move the BasicBlocks of the replacing function into the function that was being replaced, and then eliminate the original BasicBlocks. So far I had only one issue while eliminating the original BasicBlocks, I can only removeFromParent but not eraseFromParent, but the first function works fine anyways. For example, the original function is: </div><div><br></div><div><div>define i32 @foo2(i32 %a, i32 %b) #0 {</div><div>entry:</div><div>  %a.addr = alloca i32, align 4</div><div>  %b.addr = alloca i32, align 4</div><div>  store i32 %a, i32* %a.addr, align 4</div><div>  store i32 %b, i32* %b.addr, align 4</div></div><div><br></div><div>And the function that is going to replace that is:</div><div><br></div><div><div>define i32 @foo3(i32 %a, i32 %b) #0 {</div><div>entry:</div><div>  %a.addr = alloca i32, align 4</div><div>  %b.addr = alloca i32, align 4</div><div>  store i32 %a, i32* %a.addr, align 4</div><div>  store i32 %b, i32* %b.addr, align 4</div></div><div><br></div><div>So they are pretty much the same except for the name (the functions differ later in the code, but the error happens at the beginning). So what I do is move the entry block in foo3 before the entry block in foo2 and then "removeFromParent" the original entry block in the foo2. When I try to dump the Module, an error says:</div><div><br></div><div>Use still stuck around after Def is destroyed:  store i32 %0, i32* %a.addr1, align 4<br></div><div><br></div><div>Which I understand as that the new instruction "store" inserted in the new block in foo2 is still referring to the foo3 function's argument, which confused me at the beginning since I named both arguments the same to facilitate the process of substitution. What I thought as a solution was to then change the arguments of the function foo2 (the one that is being replaced) with the ones in the function foo3, to "move" the references, but I don't understand the difference between the functions of llvm::Function "addAttribute" and "addParamAttr", neither why they require to specify the attribute (or the <span style="color:rgb(0,0,0);font-family:Roboto,sans-serif;font-size:14px;background-color:rgb(249,250,252)"> </span><a class="m_-422644749051578610el" href="http://llvm.org/doxygen/classllvm_1_1Attribute.html#aab7ee4b8fd1d3e7e4cea87868855e60e" style="color:rgb(70,101,162);font-weight:bold;font-family:Roboto,sans-serif;font-size:14px" target="_blank">Attribute::AttrKind</a><span style="color:rgb(0,0,0);font-family:Roboto,sans-serif;font-size:14px;background-color:rgb(249,250,252)"> ) </span>if the function already requires the argument position. When I did a similar process to change the calling instruction in the main function, I used the functions "setArgOperand" and "getArgOperand" which worked just fine, and also the "replaceAllUsesWith", I'm looking for something similar in the case of the function's arguments and their use in the function's body.  </div><div><br></div><div>So the question is: How can I properly replace a function (in my case only the function's body, since I'm limiting already that both functions have the same header) defined in one module intro another module.</div><div><br></div><div>Regards,</div><div>Daniel Moya</div></div>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank">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>
</blockquote></div>