insertInto(cur_func, nullptr); // method 3 (newer?) llvm::BasicBlock *blk = llvm::BasicBlock::Create(*cont...">

Ways to insert a BasicBlock into a Function (original) (raw)

January 16, 2025, 3:38pm 1

Hi,
I’m currently doing IR generation for the first time and was wondering if there are any differences in these three ways of inserting a block:

llvm::Function *cur_func = builder->GetInsertBlock()->getParent();
// method 1
llvm::BasicBlock *blk =
    llvm::BasicBlock::Create(*context, "name", cur_func);
// method 2
llvm::BasicBlock *blk = llvm::BasicBlock::Create(*context, "name");
blk->insertInto(cur_func, nullptr);
// method 3 (newer?)
llvm::BasicBlock *blk = llvm::BasicBlock::Create(*context, "name");
cur_func->insert(cur_func->end(), blk);

With llvm 14 (debian default) the method 3 doesn’t seem to exist yet. Is there any difference between using these? It seems like under the hood it’s all just linked lists, but I thought maybe there’s some side-effects I should be aware of.

I think they all essentially do the same thing with subtle differences with what they return:

Method 1 with BasicBlock::Create (returns the block created):

Screenshot 2025-01-17 at 12.10.36 PM

Method 2 with insertInto (nothing returned):

Screenshot 2025-01-17 at 12.09.31 PM

Method 3 with insert (returns iterator to the block inserted):

Screenshot 2025-01-17 at 12.14.44 PM

So other than their returns, I don’t think there are any main side-effects/differences.