Mahwish
Mahwish

Reputation: 101

How to insert more than one call instruction using CreateCall in LLVM

I am running a module pass on my source code using llvm. For a certain instruction, I want to insert 2 or 3 instructions before the next instruction in the code. What I am currently doing is passing Instruction->getNextNode() as the last argument to IRBuilder CreateCall() function to insert the instruction before next node in the code. How to insert more than one instruction before next node.

Upvotes: 0

Views: 432

Answers (1)

Eli Bendersky
Eli Bendersky

Reputation: 273686

Just save the instruction into some Instruction* and you keep passing the same instruction as the anchor to insert before, and it just works out. Consider this:

    foo
    bar
--> baz

baz is the last instruction. Now, you insert abc before baz:

    foo
    bar
    abc
--> baz

And now you insert bcd before baz:

    foo
    bar
    abc
    bcd
--> baz

Keep inserting before baz and you'll get the expected order for the inserted instructions. If you wanted to insert bcd before abc and not after, just pass it abc as the "instruction to insert before" at creation.

Upvotes: 1

Related Questions