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 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it
24 // gets the next available unique MBB number. If it is removed from a
25 // MachineFunction, it goes back to being #-1.
26 void ilist_traits<MachineBasicBlock>::addNodeToList (MachineBasicBlock* N)
28 assert(N->Parent == 0 && "machine instruction already in a basic block");
30 N->Number = parent->getNextMBBNumber();
31 LeakDetector::removeGarbageObject(N);
36 void ilist_traits<MachineBasicBlock>::removeNodeFromList (MachineBasicBlock* N)
38 assert(N->Parent != 0 && "machine instruction not in a basic block");
41 LeakDetector::addGarbageObject(N);
45 MachineInstr* ilist_traits<MachineInstr>::createNode()
47 MachineInstr* dummy = new MachineInstr(0, 0);
48 LeakDetector::removeGarbageObject(dummy);
52 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N)
54 assert(N->parent == 0 && "machine instruction already in a basic block");
56 LeakDetector::removeGarbageObject(N);
59 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N)
61 assert(N->parent != 0 && "machine instruction not in a basic block");
63 LeakDetector::addGarbageObject(N);
66 void ilist_traits<MachineInstr>::transferNodesFromList(
67 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
68 ilist_iterator<MachineInstr> first,
69 ilist_iterator<MachineInstr> last)
71 if (parent != toList.parent)
72 for (; first != last; ++first)
73 first->parent = toList.parent;
76 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
78 const TargetInstrInfo& TII = getParent()->getTarget().getInstrInfo();
80 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
81 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
85 void MachineBasicBlock::dump() const
90 void MachineBasicBlock::print(std::ostream &OS) const
93 OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n";
96 const BasicBlock *LBB = getBasicBlock();
98 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n";
99 for (const_iterator I = begin(); I != end(); ++I) {
101 I->print(OS, getParent()->getTarget());