How to insert the first instruction to an empty basic block in llvm

1.4k Views Asked by At

Inserting a new llvm instruction to a non empty basic block is indeed pretty straight forward: simply iterate existing instructions until you reach the desired place and use the

newInst->insertBefore(thatInst);

command. However, when I look at the BasicBlock interface here, I can't seem to find how to insert that first instruction? I mean when a fresh basic block has just been allocated and it is still empty, how does one insert the first instruction inside? thanks!

1

There are 1 best solutions below

0
On BEST ANSWER

You can use an IRBuilder like this:

llvm::IRBuilder builder(basicBlock);
builder.createAdd(...); // Replace "Add" as appropriate

Or, if you want to insert an already existing instruction object:

builder.insert(instruction);