Ammar114
Ammar114

Reputation: 35

How to insert an LLVM StoreInst in a Basic Block

I am going through instructions in a basic block. After an allocation instruction I want to make a store for that variable and insert it right after the allocation instruction. Right now I am able to find the Allocation instruction with

if(AllocaInst *AI=dyn_cast<AllocaInst>(&i))

but i do not know how to create the StoreInst. I just want to store the number 10 in it regardless of which type the variable is.

I tried like this

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

but i do not know what to put in place of Val, Ptr, and how to get the adress of the next instruction if it needs a pointer to InsertBefore

Upvotes: 2

Views: 1553

Answers (1)

arrowd
arrowd

Reputation: 34401

To insert something after instruction you can use insertAfter() method. In your case:

AI->insertAfter(stinst)

And to create StoreInst you need to provide it with

  • Value *Val is what you want to store. In your case you'd need to create a Constant that would represent "10" integer and then pass it there.
  • Value *Ptr is where you want to put the value. I guess, it is AI in your case.
  • nullptr for Instruction *InsertBefore, because you are inserting it manually.

Upvotes: 1

Related Questions