Free memory allocated by LLVM
llvm, memory-management
Solution
First of all, when deleting any kind of `Value`, make sure it doesn't have any `User`s anymore. Deleting used values will, obviously, lead to errors (in the form of an assertion). This can easily be tested by calling `getNumUses()`, or better (read: faster) `hasNUses(0)`.
When you're sure your value isn't used anymore, different kind of values sometimes need different ways to delete them. For your two cases:
- `Function`s can simply be deleted by calling `operator delete`. This makes sure the function is removed correctly from the `Module`.
- `Instruction`s should be deleted by calling `eraseFromParent()`. Or, equivalently, by first calling `removeFromParent()` and then deleting it manually.
Problem
I am struggling a little bit with finding how to free memory allocated by LLVM functions. For example, when I call the function `Function::Create()` to create an LLVM function, how can I free the memory allocated to it? Same actually applies to many LLVM functions like `IRBuilder::CreateAlloca()`, `IRBuilder::CreateStore()`, etc. Any idea?