我想制作一个新的指令
%a = i32 add %b, %c
变成
%a = i32 mul %b, %c
我找了好几个小时,但到目前为止,我发现的答案都是与创建新的指令有关的,这些指令定义在 Instructions.h (ex AllocaInst) 中,而不是 mul, sdiv, div, instructions.我还阅读了 https:/llvm.orgdocsProgrammersManual.html#creating-and-inserting-new-instructions。但我不能让 "auto *newInst = new Instruction(...); "这个例子工作,因为我不知道应该是什么参数,即使在看了 https:/llvm.orgdoxygenclassllvm_1_1Instruction.html#a2b30181f228bc6318130557d7ffca945。
EDIT:我想我已经成功地创建了指令本身,但不是我难以用正确的方式替换它。
Instruction *ni = BinaryOperator::CreateMul(inst.getOperand(0), newvalue);
ReplaceInstWithInst(*i, ni);
其中*i来自于对一个基本块的迭代(ex for (auto &i : bb))
解决了!
Instruction *ni = BinaryOperator::CreateMul(inst.getOperand(0), newvalue);
bb.getInstList().insert(inst.getIterator(), ni);