[llvm-dev] Placing an existing function's arguments in a call to another function in an LLVM Function Pass

Craig Topper via llvm-dev llvm-dev at lists.llvm.org
Wed Apr 8 19:19:06 PDT 2020


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.

On Wed, Apr 8, 2020 at 6:21 PM Joseph via llvm-dev <llvm-dev at lists.llvm.org>
wrote:

> Hey,
>
> I'm writing an LLVM function pass. So far, the "Programmer's Manual" was
> a tremendous help.
>
> I have a function `foo(int a, int b)`, where in some instances, I'll
> need to replace it's call with `bar(int a, int b)`.
>
> The way I wanted to do it is to basically:
> * Locate the `foo()` I need to replace
> * Make a `CallInst` to `bar()`
> * Populate `CallInst::Create` with the arguments of `foo()`
> * Make a call to `ReplaceInstWithInst()` to have it work
>
> Everything is working just fine, but the arguments of `foo()` are not
> getting copied to `bar()`. When the replacement call is executed, the
> arguments of `bar()` are just null.
>
> Here's the relevant code:
>
> ```c
> bool runOnFunction(Function& F) override
> {
>      CallInst* call_to_foo = 0;
>      Function* foo_func = 0;
>
>      /*
>      Loop over all calls in the function and populate foo_func when you
> find it.
>
>      If we reached below, that means the current function we're in has a
> call to
>      foo() (inside call_to_foo) that we need to replace with bar().
> Also, foo_func
>      is pointing to a foo Function
>      */
>
>      Function* bar_func = get_bar_func();
>
>      // Collect foo args
>      // I believe here is the issue: the arguments are not copied
>      //  properly or there must be a deep-copy of sorts for it to work
>      std::vector<Value*> bar_func_args;
>      for (size_t i = 0; i < foo_func->arg_size(); i++) {
>          Argument* arg = foo_func->arg_begin() + i;
>          bar_func_args.push_back(arg);
>      }
>
>      auto* inst_to_replace = CallInst::Create(
>          bar_func, ArrayRef<Value*>(bar_func_args),
>          "bar_func");
>
>      ReplaceInstWithInst(
>      call_inst->getParent()->getInstList(),
>      BBI, inst_to_replace);
>
>      return true;
> }
> ```
>
> Any help would be tremendously appreciated.
>
> --
> Joseph
>
> _______________________________________________
> LLVM Developers mailing list
> llvm-dev at lists.llvm.org
> https://lists.llvm.org/cgi-bin/mailman/listinfo/llvm-dev
>
-- 
~Craig
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.llvm.org/pipermail/llvm-dev/attachments/20200408/2922d4d8/attachment.html>


More information about the llvm-dev mailing list