Размещение аргументов существующей функции при вызове другой функции в проходе функции LLVM - PullRequest
1 голос
/ 06 апреля 2020

Я пишу проход функции LLVM. У меня есть функция foo(int a, int b), где в некоторых случаях мне нужно будет заменить ее вызов на bar(int a, int b).

Я хотел сделать это следующим образом:

  • Найдите foo() Мне нужно заменить
  • Сделать CallInst на bar()
  • Заполнить CallInst::Create аргументами foo()
  • Make вызов ReplaceInstWithInst(), чтобы он работал

Все работает просто отлично, но аргументы 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;
}

Любая помощь будет принята с благодарностью.

1 Ответ

1 голос
/ 07 апреля 2020

Требуемые аргументы находятся в инструкции вызова, а не в вызываемой функции, поэтому вам нужно заполнить новый вызов, используя операнды из предыдущей инструкции cal .

...