llvm - 将现有函数的参数放在对 LLVM 函数传递中另一个函数的调用中
问题描述
我正在写一个 LLVM 函数传递。我有一个函数foo(int a, int b)
,在某些情况下,我需要将它的调用替换为bar(int a, int b)
.
我想做的方式基本上是:
- 找到
foo()
我需要更换的 - 做一个
CallInst
到bar()
- 填充
CallInst::Create
参数foo()
- 打电话
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;
}
任何帮助将不胜感激。
解决方案
您想要的参数在 call 指令中,而不是在被调用的函数中,因此您需要使用前一个 cal 指令中的操作数填充新调用。
推荐阅读
- c# - 使用 XPath 获取子元素值,其中日期是元素值
- r - 在当前工作目录或具有 `pool` 和 `golem` 的父目录中找不到配置文件
- ruby-on-rails - Ruby on Rails API 命名空间作为默认路径
- java - 克隆列表地图的有效方法?
- delphi - 为什么这些 TShape 看起来如此不同?
- python - 裁剪 n 维数组的最后 2 维
- javascript - 两个子组件使用 vue.js 将数据传递给同一个父组件
- python - Dearpygui 状态框未正确更新
- android - 如何检测我的设备左/右音频平衡?
- javascript - 我在 Mongoose 中的 $push 方法不起作用