create instruction after specified instruction


What I want to do is to locate a specific instruction and then create my own instructions. I am able to locate the instruction I want, however, when I want to create the new instructions I can place them before the specified one or at the end of the basic block.
for example, I have the following IR code:

%9 = add nsw i32 %8, 2
store i32 %9, i32* %x, align 4

I want to insert some instructions between the add and the store instructions.
I have tried the IRBuilder and the BinaryOperator::Create, but I cannot create what I want.

Any suggestions?


I think you could do this manually by doing..

BasicBlock *pb = ...;
Instruction *pi = ...;
Instruction *newInst = new Instruction(...);

pb->getInstList().insertAfter(pi, newInst);

If you'd like to use the Builder you could also do this...

1. Look one instruction ahead, if there is an instruction (in your
example, store) after your current instruction (in your example add)
then create the IRBuilder on that (in your example, store). That way
the builder will create instructions after your instruction of
2. If there is no instruction after your current instruction then
create an IRBuilder from the BasicBlock so it will insert instructions
at the end.

There might be a better way to do this but I'm afraid I'm only
familiar with a very small subset of LLVM's APIs.