在LLVM自定义的pass中向IR中插入一个函数并将指令作为参数传入该函数
时间: 2023-12-10 17:05:05 浏览: 271
在 LLVM 自定义的 pass 中向 IR 中插入一个函数并将指令作为参数传入该函数,可以按照以下步骤进行:
1. 定义一个新的函数类型,该函数类型包含一个指向整数类型的指针作为参数,该指针指向要传递的指令。
```c++
FunctionType* funcType = FunctionType::get(Type::getVoidTy(Context), {Type::getInt32PtrTy(Context)}, false);
```
2. 创建一个新的函数,该函数的名称为你想要插入的函数名称,并将其插入到模块中。
```c++
Function* newFunc = Function::Create(funcType, GlobalValue::ExternalLinkage, "newFunc", &M);
```
3. 在函数中创建一个基本块。在基本块中,你可以使用 IRBuilder 创建一个新的指令,然后将指令作为参数传递给该函数。
```c++
BasicBlock* BB = BasicBlock::Create(Context, "entry", newFunc);
IRBuilder<> builder(BB);
Value* arg = newFunc->arg_begin();
Value* inst = builder.CreateLoad(arg);
```
4. 将该函数插入到 IR 中的每个位置,并将指令作为参数传递给该函数。
```c++
for (auto& F : M) {
for (auto& B : F) {
for (auto& I : B) {
if (auto* callInst = dyn_cast<CallInst>(&I)) {
CallInst* newCallInst = CallInst::Create(newFunc, {inst}, "", callInst);
ReplaceInstWithInst(callInst, newCallInst);
}
}
}
}
```
这些步骤将在 LLVM 自定义的 pass 中向 IR 中插入一个函数并将指令作为参数传递给该函数。
阅读全文
相关推荐
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![-](https://img-home.csdnimg.cn/images/20241231045053.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)