[LLVMdev] Memory allocation (or deallocation) model?

Gordon Henriksen gordonhenriksen at mac.com
Mon Dec 3 19:33:05 PST 2007


On 2007-12-03, at 21:59, Jonathan Brandmeyer wrote:

> On Mon, 2007-12-03 at 18:56 -0800, Chris Lattner wrote:
>
>> Other IR objects (like instructions) have very simple ownership.   
>> An instruction is owned by its basic block, a bb is owned by the  
>> function, a function is owned by thet module.
>
> If an instruction is initially allocated with its Instruction  
> *insertAtEnd parameter defaulted to null, and then later appended to  
> a BasicBlock, will the BasicBlock take ownership of the Instruction?

Yes.

There is a containment structure to the IR. If an object is inserted  
into that hierarchy, memory management is automatic. 'delete Function'  
recursively deletes GlobalVariables and Functions, and so on down to  
Instructions. If you remove an object from that tree, or never insert  
it, you are responsible for 'delete'ing it.

There are useful methods like 'eraseFromParent' which combine these  
operations, and others like 'removeFromParent' that allow reinsertion  
or 'delete'ion.

— Gordon





More information about the llvm-dev mailing list