首页 > 解决方案 > 将现有函数的参数放在对 LLVM 函数传递中另一个函数的调用中

问题描述

我正在写一个 LLVM 函数传递。我有一个函数foo(int a, int b),在某些情况下,我需要将它的调用替换为bar(int a, int b).

我想做的方式基本上是:

一切正常,但 的参数foo()没有被复制到bar(). 执行替换调用时,参数为bar()空。

以下是相关代码:

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;
}

任何帮助将不胜感激。

标签: llvmllvm-clangllvm-c++-api

解决方案


您想要的参数在 call 指令中,而不是在被调用的函数中,因此您需要使用前一个 cal 指令中的操作数填充新调用。


推荐阅读