[LLVMdev] create instruction after specified instruction

1,974 views
Skip to first unread message

Vasileios Koutsoumpos

unread,
Jun 5, 2014, 5:58:40 AM6/5/14
to LLVM Dev
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?

Regards,
Vasileios
_______________________________________________
LLVM Developers mailing list
LLV...@cs.uiuc.edu http://llvm.cs.uiuc.edu
http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev

Dan Liew

unread,
Jun 6, 2014, 6:39:53 AM6/6/14
to Vasileios Koutsoumpos, LLVM Dev
On 5 June 2014 10:55, Vasileios Koutsoumpos
<bill_kou...@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
Reply all
Reply to author
Forward
0 new messages