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):
Method 2 with insertInto (nothing returned):
Method 3 with insert (returns iterator to the block inserted):
So other than their returns, I don’t think there are any main side-effects/differences.