1 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // Collect the sequence of machine instructions for a basic block.
12 //===----------------------------------------------------------------------===//
14 #include "llvm/CodeGen/MachineBasicBlock.h"
15 #include "llvm/BasicBlock.h"
16 #include "llvm/CodeGen/MachineFunction.h"
17 #include "llvm/CodeGen/MachineInstr.h"
18 #include "llvm/Target/TargetInstrInfo.h"
19 #include "llvm/Target/TargetMachine.h"
20 #include "Support/LeakDetector.h"
23 MachineBasicBlock::~MachineBasicBlock() {
24 LeakDetector::removeGarbageObject(this);
29 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
30 // gets the next available unique MBB number. If it is removed from a
31 // MachineFunction, it goes back to being #-1.
32 void ilist_traits<MachineBasicBlock>::addNodeToList (MachineBasicBlock* N)
34 assert(N->Parent == 0 && "machine instruction already in a basic block");
36 N->Number = Parent->getNextMBBNumber();
37 LeakDetector::removeGarbageObject(N);
42 void ilist_traits<MachineBasicBlock>::removeNodeFromList (MachineBasicBlock* N)
44 assert(N->Parent != 0 && "machine instruction not in a basic block");
47 LeakDetector::addGarbageObject(N);
51 MachineInstr* ilist_traits<MachineInstr>::createNode()
53 MachineInstr* dummy = new MachineInstr(0, 0);
54 LeakDetector::removeGarbageObject(dummy);
58 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N)
60 assert(N->parent == 0 && "machine instruction already in a basic block");
62 LeakDetector::removeGarbageObject(N);
65 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N)
67 assert(N->parent != 0 && "machine instruction not in a basic block");
69 LeakDetector::addGarbageObject(N);
72 void ilist_traits<MachineInstr>::transferNodesFromList(
73 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
74 ilist_iterator<MachineInstr> first,
75 ilist_iterator<MachineInstr> last)
77 if (parent != toList.parent)
78 for (; first != last; ++first)
79 first->parent = toList.parent;
82 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
84 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
86 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
87 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
91 void MachineBasicBlock::dump() const
96 void MachineBasicBlock::print(std::ostream &OS) const
99 OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n";
102 const BasicBlock *LBB = getBasicBlock();
104 OS << "\n" << LBB->getName() << " (" << (const void*)this
105 << ", LLVM BB @" << (const void*) LBB << "):\n";
106 for (const_iterator I = begin(); I != end(); ++I) {
108 I->print(OS, &getParent()->getTarget());