<div dir="auto"><div>Hey Craig. Thank you for answering my question. You're correct, I did stupid and used the arguments from the Function*. The arguments of the CallInst worked perfectly.</div><div dir="auto"><br></div><div dir="auto">Many thanks!</div><div dir="auto"><br><div class="gmail_quote" dir="auto"><div dir="ltr" class="gmail_attr">On Thu, Apr 9, 2020, 04:19 Craig Topper <<a href="mailto:craig.topper@gmail.com">craig.topper@gmail.com</a>> wrote:<br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex"><div><div dir="auto">Is foo_func a Function*? If so that’s just the function declaration. You need to copy the arguments from the CallInst that calls foo.</div></div><div><br><div class="gmail_quote"><div dir="ltr" class="gmail_attr">On Wed, Apr 8, 2020 at 6:21 PM Joseph via llvm-dev <<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" rel="noreferrer">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">Hey,<br>
<br>
I'm writing an LLVM function pass. So far, the "Programmer's Manual" was <br>
a tremendous help.<br>
<br>
I have a function `foo(int a, int b)`, where in some instances, I'll <br>
need to replace it's call with `bar(int a, int b)`.<br>
<br>
The way I wanted to do it is to basically:<br>
* Locate the `foo()` I need to replace<br>
* Make a `CallInst` to `bar()`<br>
* Populate `CallInst::Create` with the arguments of `foo()`<br>
* Make a call to `ReplaceInstWithInst()` to have it work<br>
<br>
Everything is working just fine, but the arguments of `foo()` are not <br>
getting copied to `bar()`. When the replacement call is executed, the <br>
arguments of `bar()` are just null.<br>
<br>
Here's the relevant code:<br>
<br>
```c<br>
bool runOnFunction(Function& F) override<br>
{<br>
     CallInst* call_to_foo = 0;<br>
     Function* foo_func = 0;<br>
<br>
     /*<br>
     Loop over all calls in the function and populate foo_func when you <br>
find it.<br>
<br>
     If we reached below, that means the current function we're in has a <br>
call to<br>
     foo() (inside call_to_foo) that we need to replace with bar(). <br>
Also, foo_func<br>
     is pointing to a foo Function<br>
     */<br>
<br>
     Function* bar_func = get_bar_func();<br>
<br>
     // Collect foo args<br>
     // I believe here is the issue: the arguments are not copied<br>
     //  properly or there must be a deep-copy of sorts for it to work<br>
     std::vector<Value*> bar_func_args;<br>
     for (size_t i = 0; i < foo_func->arg_size(); i++) {<br>
         Argument* arg = foo_func->arg_begin() + i;<br>
         bar_func_args.push_back(arg);<br>
     }<br>
<br>
     auto* inst_to_replace = CallInst::Create(<br>
         bar_func, ArrayRef<Value*>(bar_func_args),<br>
         "bar_func");<br>
<br>
     ReplaceInstWithInst(<br>
     call_inst->getParent()->getInstList(),<br>
     BBI, inst_to_replace);<br>
<br>
     return true;<br>
}<br>
```<br>
<br>
Any help would be tremendously appreciated.<br>
<br>
-- <br>
Joseph<br>
<br>
_______________________________________________<br>
LLVM Developers mailing list<br>
<a href="mailto:llvm-dev@lists.llvm.org" target="_blank" rel="noreferrer">llvm-dev@lists.llvm.org</a><br>
<a href="https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev" rel="noreferrer noreferrer" target="_blank">https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev</a><br>
</blockquote></div></div>-- <br><div dir="ltr" data-smartmail="gmail_signature">~Craig</div>
</blockquote></div></div></div>