[llvm-dev] Replacing a function from one module into another one

Daniel Moya via llvm-dev llvm-dev at lists.llvm.org
Mon Aug 27 10:37:55 PDT 2018


Hello LLVM Developers,

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:

define i32 @foo2(i32 %a, i32 %b) #0 {
entry:
  %a.addr = alloca i32, align 4
  %b.addr = alloca i32, align 4
  store i32 %a, i32* %a.addr, align 4
  store i32 %b, i32* %b.addr, align 4

And the function that is going to replace that is:

define i32 @foo3(i32 %a, i32 %b) #0 {
entry:
  %a.addr = alloca i32, align 4
  %b.addr = alloca i32, align 4
  store i32 %a, i32* %a.addr, align 4
  store i32 %b, i32* %b.addr, align 4

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:

Use still stuck around after Def is destroyed:  store i32 %0, i32*
%a.addr1, align 4

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  Attribute::AttrKind
<http://llvm.org/doxygen/classllvm_1_1Attribute.html#aab7ee4b8fd1d3e7e4cea87868855e60e>
 ) 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.

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.

Regards,
Daniel Moya
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20180827/a56f8368/attachment.html>


More information about the llvm-dev mailing list