c++ - 如何插入 LLVM 指令?

标签 c++ llvm llvm-ir

我已经搜索了几个小时,但找不到任何可以帮助我的东西。我正在从事一个涉及 FunctionPass 的项目。我已经实现了一个 runOnFunction(Function &f) 方法并且工作正常。基本上它需要:

1) 检测存储指令

2) 将存储指令的内存地址转换为Integer

3) 使用按位与操作 (0000FFFF) 改变整数

4) 将整数转换回指针

到目前为止,我得到了以下内容:

 virtual bool runOnFunction(Function &F) {
  for (Function::iterator bb = F.begin(), bbe = F.end(); bb != bbe; ++bb) {
    BasicBlock& b = *bb;
    for (BasicBlock::iterator i = b.begin(), ie = b.end(); i != ie; ++i) {
      if(StoreInst *si = dyn_cast<StoreInst>(&*i)) {
        PtrToIntInst* ptrToInt = new PtrToIntInst(si->getPointerOperand(), IntegerType::get(si->getContext(), 32), "", si);
      }
    }
  }
  return true;
}

我这辈子都想不出如何实际插入指令,甚至找不到创建 AND 指令的方法。如果有人能指出我正确的方向,那就太好了。

提前致谢。

最佳答案

我建议看一下 Programmer's Manual - 它对基础知识的覆盖面相当不错。

特别是,有 a section about creating and inserting new instructions .最简单的方法就是提供一个现有指令作为新指令构造函数的最后一个参数,然后该指令将紧接在现有指令之前插入该指令。

或者,如果您只想添加到它的末尾,您可以传递封闭的基本 block (但请记住您需要注意终止符!)。最后,您可以在封闭的基本 block 上调用 getInstList(),然后调用 insertpush_back 以在其中插入新指令。

顺便说一句,您不必遍历所有 block 然后遍历每个 block 中的所有指令,您可以直接遍历指令;见the section about the instruction iterator in the programmer's manual .

关于c++ - 如何插入 LLVM 指令?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13370306/

相关文章:

C++ 多线程 - 如何处理对共享变量的写入

llvm - 在 macOS 上安装 `brew install llvm` 会导致 "Segmentation fault: 11"

llvm - LLVM 的 SelectInst 的好处

c - LLVM API 指针类型支持

compiler-construction - LLVM中的部分应用

c++ - 询问用户是否要添加重复信息?

c++ - 保护新交所算法的安全

C++Builder编译问题

api - 使用 clang API 编译和运行 C 代码

c++ - 为什么矢量化在这个 for 循环中没有好处?