LLVM C++ API memory management

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?

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

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 :slight_smile:

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

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 once again.

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?

I'll try.