How to create forward reference to BasicBlock?

Hi,

Can anyone tell me if there’s a straighforward way to create a new BasicBlock without inserting it into a function’s basic block list? I want to do this so I can create a forward reference to a block that’s position in the function is not yet known.

I’ve tried:

Function function
Builder builder;

bb = BasicBlock::Create(function,…)
bb.eraseFromParent()


add other blocks to function and build instructions in those blocks

function.getBasicBlockList.push_back(bb)

but I get an assert failure from push_back:

t1: SymbolTableListTraitsImpl.h:68: void llvm::SymbolTableListTraits<ValueSubClass, ItemParentClass>::addNodeToList(ValueSubClass*) [with ValueSubClass = llvm::BasicBlock, ItemParentClass = llvm::Function]: Assertion `V->getParent() == 0 && “Value already in a container!!”’ failed.

Should I expect this to work? Can a BasicBlock only exist within a Function’s basic block list? Is there a better way to create a forward reference for branch instructions to IR code that is not yet generated?

Thanks in advance,
– James

1 Like

You can create BasicBlock without inserting it into a Function. Parent
is an optional parameter for BasicBlock constructor. See
include/llvm/BasicBlock.h

2010/1/13 Devang Patel <devang.patel@gmail.com>

Hi,

Can anyone tell me if there’s a straighforward way to create a new
BasicBlock without inserting it into a function’s basic block list? I want
to do this so I can create a forward reference to a block that’s position in
the function is not yet known.

I’ve tried:

Function function
Builder builder;

bb = BasicBlock::Create(function,…)
bb.eraseFromParent()


add other blocks to function and build instructions in those blocks

function.getBasicBlockList.push_back(bb)

but I get an assert failure from push_back:

t1: SymbolTableListTraitsImpl.h:68: void
llvm::SymbolTableListTraits<ValueSubClass,

::addNodeToList(ValueSubClass*) [with ValueSubClass =
llvm::BasicBlock, ItemParentClass = llvm::Function]: Assertion
`V->getParent() == 0 && “Value already in a container!!”’ failed.

Should I expect this to work? Can a BasicBlock only exist within a
Function’s basic block list? Is there a better way to create a forward
reference for branch instructions to IR code that is not yet generated?

You can create BasicBlock without inserting it into a Function. Parent
is an optional parameter for BasicBlock constructor. See
include/llvm/BasicBlock.h

Thanks.

– James