[LLVMdev] LLVM C++ API memory management

Chris Lattner clattner at apple.com
Mon Nov 10 13:29:52 PST 2008


On Nov 10, 2008, at 6:11 AM, Gregory Petrosyan wrote:

> While reading LLVM API docs, I have noticed a lot of plain  
> "new"/"delete"
> statements, and lots of bare pointers ("Value* = foo.getSomeValue()"  
> etc.).
>
> How exactly LLVM manages it's own memory? Who is responsible for
> allocating/deallocating things? Why smart pointers are not used?

The ownership model is very simple.  We have a simple structured  
system (e.g. module is a list of functions, function is a list of  
blocks, blocks are a list of instructions).  When a node is inserted  
into a parent container (e.g. an instruction in a basic block) then  
the parent owns the node.

This means that if you create an instruction and insert it into a  
block, you're fine.  If you remove an instruction from a block, you  
have to either reinsert it or delete it.  The "remove" methods remove  
a node from its parent and returns it, the "erase" methods do a remove 
+delete.

> Thanks in advance. I hope I haven't missed some basic chapter about  
> this
> in tutorial :-)

Unfortunately this is not really documented anywhere.  It would be  
really really good to add a section about this in the Programmer's  
Manual.  Can you write up a patch?

-Chris



More information about the llvm-dev mailing list