Search code examples
llvmllvm-irbitcode

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


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!


Solution

  • 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);