1 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 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. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // Collect the sequence of machine instructions for a basic block. 11 // 12 //===----------------------------------------------------------------------===// 13 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" 21 using namespace llvm; 22 23 MachineBasicBlock::~MachineBasicBlock() { 24 LeakDetector::removeGarbageObject(this); 25 } 26 27 28 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 { 34 assert(N->Parent == 0 && "machine instruction already in a basic block"); 35 N->Parent = Parent; 36 N->Number = Parent->getNextMBBNumber(); 37 LeakDetector::removeGarbageObject(N); 38 39 40 } 41 42 void ilist_traits<MachineBasicBlock>::removeNodeFromList (MachineBasicBlock* N) 43 { 44 assert(N->Parent != 0 && "machine instruction not in a basic block"); 45 N->Parent = 0; 46 N->Number = -1; 47 LeakDetector::addGarbageObject(N); 48 } 49 50 51 MachineInstr* ilist_traits<MachineInstr>::createNode() 52 { 53 MachineInstr* dummy = new MachineInstr(0, 0); 54 LeakDetector::removeGarbageObject(dummy); 55 return dummy; 56 } 57 58 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) 59 { 60 assert(N->parent == 0 && "machine instruction already in a basic block"); 61 N->parent = parent; 62 LeakDetector::removeGarbageObject(N); 63 } 64 65 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) 66 { 67 assert(N->parent != 0 && "machine instruction not in a basic block"); 68 N->parent = 0; 69 LeakDetector::addGarbageObject(N); 70 } 71 72 void ilist_traits<MachineInstr>::transferNodesFromList( 73 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 74 ilist_iterator<MachineInstr> first, 75 ilist_iterator<MachineInstr> last) 76 { 77 if (parent != toList.parent) 78 for (; first != last; ++first) 79 first->parent = toList.parent; 80 } 81 82 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() 83 { 84 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); 85 iterator I = end(); 86 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 87 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 88 return I; 89 } 90 91 void MachineBasicBlock::dump() const 92 { 93 print(std::cerr); 94 } 95 96 void MachineBasicBlock::print(std::ostream &OS) const 97 { 98 if(!getParent()) { 99 OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n"; 100 return; 101 } 102 const BasicBlock *LBB = getBasicBlock(); 103 if(LBB) 104 OS << "\n" << LBB->getName() << " (" << (const void*)this 105 << ", LLVM BB @" << (const void*) LBB << "):\n"; 106 for (const_iterator I = begin(); I != end(); ++I) { 107 OS << "\t"; 108 I->print(OS, getParent()->getTarget()); 109 } 110 } 111