[LLVMdev] create instruction after specified instruction

Dan Liew dan at su-root.co.uk
Fri Jun 6 03:37:18 PDT 2014


On 5 June 2014 10:55, Vasileios Koutsoumpos
<bill_koutsoumpos at hotmail.com> wrote:
> Hello,
>
> 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
interest.
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.

Thanks,
Dan



More information about the llvm-dev mailing list