将现有函数的参数放在对 LLVM 函数传递中另一个函数的调用中
Placing an existing function's arguments in a call to another function in an LLVM Function Pass
我正在编写 LLVM 函数传递。我有一个函数 foo(int a, int b)
,在某些情况下,我需要用 bar(int a, int b)
.
替换它的调用
我想做的基本上是:
- 找到我需要更换的
foo()
- 做一个
CallInst
到bar()
- 用
foo()
的参数填充 CallInst::Create
- 打电话给
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;
}
如有任何帮助,我们将不胜感激。
您需要的参数在调用指令中,而不是在被调用的函数中,因此您需要使用 operands from the previous cal instruction.
填充新调用
我正在编写 LLVM 函数传递。我有一个函数 foo(int a, int b)
,在某些情况下,我需要用 bar(int a, int b)
.
我想做的基本上是:
- 找到我需要更换的
foo()
- 做一个
CallInst
到bar()
- 用
foo()
的参数填充 - 打电话给
ReplaceInstWithInst()
让它工作
CallInst::Create
一切正常,但 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;
}
如有任何帮助,我们将不胜感激。
您需要的参数在调用指令中,而不是在被调用的函数中,因此您需要使用 operands from the previous cal instruction.
填充新调用