如何在基本块中插入 LLVM StoreInst

How to insert an LLVM StoreInst in a Basic Block

我正在阅读基本块中的说明。在分配指令之后,我想为该变量创建一个存储并将其插入到分配指令之后。 现在我可以找到

的分配指令
if(AllocaInst *AI=dyn_cast<AllocaInst>(&i))

但我不知道如何创建 StoreInst。我只想将数字10存储在其中,而不管变量是哪种类型。

我这样试过

StoreInst* stinst = new StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);

但我不知道用什么代替 Val、Ptr,以及如果需要指向 InsertBefore 的指针,如何获取下一条指令的地址

要在 指令后插入内容 ,您可以使用 insertAfter() method。你的情况:

AI->insertAfter(stinst)

要创建 StoreInst,您需要提供

  • Value *Val就是你要存储的。在您的情况下,您需要创建一个代表“10”整数的 Constant,然后将其传递到那里。
  • Value *Ptr 是您要放置值的位置。我想,你的情况是AI
  • nullptr for Instruction *InsertBefore,因为您是手动插入的。