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) {
33 assert(N->Parent == 0 && "machine instruction already in a basic block");
35 N->Number = Parent->addToMBBNumbering(N);
36 LeakDetector::removeGarbageObject(N);
39 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) {
40 assert(N->Parent != 0 && "machine instruction not in a basic block");
41 N->Parent->removeFromMBBNumbering(N->Number);
44 LeakDetector::addGarbageObject(N);
48 MachineInstr* ilist_traits<MachineInstr>::createNode() {
49 MachineInstr* dummy = new MachineInstr(0, 0);
50 LeakDetector::removeGarbageObject(dummy);
54 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N)
56 assert(N->parent == 0 && "machine instruction already in a basic block");
58 LeakDetector::removeGarbageObject(N);
61 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N)
63 assert(N->parent != 0 && "machine instruction not in a basic block");
65 LeakDetector::addGarbageObject(N);
68 void ilist_traits<MachineInstr>::transferNodesFromList(
69 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList,
70 ilist_iterator<MachineInstr> first,
71 ilist_iterator<MachineInstr> last)
73 if (parent != toList.parent)
74 for (; first != last; ++first)
75 first->parent = toList.parent;
78 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator()
80 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo();
82 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode()));
83 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I;
87 void MachineBasicBlock::dump() const
92 void MachineBasicBlock::print(std::ostream &OS) const
95 OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n";
98 const BasicBlock *LBB = getBasicBlock();
100 OS << "\n" << LBB->getName() << " (" << (const void*)this
101 << ", LLVM BB @" << (const void*) LBB << "):\n";
102 for (const_iterator I = begin(); I != end(); ++I) {
104 I->print(OS, &getParent()->getTarget());