首页 > 解决方案 > 如何制定新的 LLVM 指令?

问题描述

我正在尝试做出指示

%a = i32 add %b, %c

进入

%a = i32 mul %b, %c

我一直在寻找几个小时,但到目前为止我发现的答案是与创建在 Instructions.h(前 AllocaInst)中定义的新指令相关的答案,而不是 mul、sdiv、div、指令。我还阅读了https://llvm.org/docs/ProgrammersManual.html#creating-and-inserting-new-instructions 但我无法获得示例“auto *newInst = new Instruction(...);” 工作,因为即使在查看https://llvm.org/doxygen/classllvm_1_1Instruction.html#a2b30181f228bc6318130557d7ffca945之后我也不知道应该是什么参数

编辑:我认为我已经成功地创建了指令本身,但不是我无法以正确的方式替换它。

Instruction *ni = BinaryOperator::CreateMul(inst.getOperand(0), newvalue);
ReplaceInstWithInst(*i, ni);

其中 *i 来自对基本块的迭代(例如 (auto &i : bb))

标签: llvmllvm-irllvm-c++-api

解决方案


解决了!

Instruction *ni = BinaryOperator::CreateMul(inst.getOperand(0), newvalue);
bb.getInstList().insert(inst.getIterator(), ni);

推荐阅读