[LLVMdev] How does the memory of MachineInstr objects are managed?

Chris Lattner sabre at nondot.org
Tue Sep 27 08:41:31 PDT 2005

On Tue, 27 Sep 2005, Tzu-Chien Chiu wrote:
> A question about how the memory of object in LLVM are managed.


> I dived in some source files but still don't have any idea how the
> memory of MachineInstr object are managed. It doesn't look like
> reference counting.

Nope.  LLVM uses a very simple mem mgmt facility for it's IR.  In 
particular, most things (llvm basic blocks, functions, modules, machine 
basic blocks and machine functions) are all basically doubly linked lists 
of nodes.  If a nodes (e.g. an instruction) is inserted into a list (e.g. 
a basic block), it is owned by that basic block.

There are two methods provided, "remove" and "erase".  "remove" takes a 
node out of a list, and returns it.  Erase takes a node out of a list and 
deletes it.

> I'm writing an instruction scheudling code, the new order of
> MachineInstr* in a MachineBasicBlock is stored in a "schedule".  All
> MachineInstr* in MachineBasicBlock are first removed, and
> MachineInstr* are inserted back MachineBasicBlock in the scheduled
> order.

Ok.  You want to use the 'remove' class here obviously :)

> The question is: isn't the memory of MachineInstr* "released/freed"
> when it's removed from the MachineBasicBlock?

Not if you use 'remove' and not 'erase'.

> vector<MachineInstr*> S; // the schedule;
> // reference implementation: SparcV9/InstrSched/InstructionScheduling.cpp
> void recordSchedule(MachineBasicBlock &MBB) {
>  ...
>  while (I != MBB.end())
>    MBB.remove(I++); // **** Q: Doesn't it invalidate the pointers in
> 'S' ?? ****

Nope.  MBB's are doubly linked lists, not vectors.  The I++ increments the 
iterator to the next node before the instruction is removed from the basic 


>  vector<MachineInstr*>::iterator I = S.begin(); S.end() != I; ++ I) {
>    MBB.push_back(*I);
>  }
> }
> --
> Tzu-Chien Chiu - XGI Technology, Inc.
> URL: http://www.csie.nctu.edu.tw/~jwchiu/
> _______________________________________________
> LLVM Developers mailing list
> LLVMdev at cs.uiuc.edu         http://llvm.cs.uiuc.edu
> http://lists.cs.uiuc.edu/mailman/listinfo/llvmdev



More information about the llvm-dev mailing list