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 // 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) 27 { 28 assert(N->Parent == 0 && "machine instruction already in a basic block"); 29 N->Parent = parent; 30 N->Number = parent->getNextMBBNumber(); 31 LeakDetector::removeGarbageObject(N); 32 33 34 } 35 36 void ilist_traits<MachineBasicBlock>::removeNodeFromList (MachineBasicBlock* N) 37 { 38 assert(N->Parent != 0 && "machine instruction not in a basic block"); 39 N->Parent = 0; 40 N->Number = -1; 41 LeakDetector::addGarbageObject(N); 42 } 43 44 45 MachineInstr* ilist_traits<MachineInstr>::createNode() 46 { 47 MachineInstr* dummy = new MachineInstr(0, 0); 48 LeakDetector::removeGarbageObject(dummy); 49 return dummy; 50 } 51 52 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) 53 { 54 assert(N->parent == 0 && "machine instruction already in a basic block"); 55 N->parent = parent; 56 LeakDetector::removeGarbageObject(N); 57 } 58 59 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) 60 { 61 assert(N->parent != 0 && "machine instruction not in a basic block"); 62 N->parent = 0; 63 LeakDetector::addGarbageObject(N); 64 } 65 66 void ilist_traits<MachineInstr>::transferNodesFromList( 67 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 68 ilist_iterator<MachineInstr> first, 69 ilist_iterator<MachineInstr> last) 70 { 71 if (parent != toList.parent) 72 for (; first != last; ++first) 73 first->parent = toList.parent; 74 } 75 76 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() 77 { 78 const TargetInstrInfo& TII = getParent()->getTarget().getInstrInfo(); 79 iterator I = end(); 80 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 81 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 82 return I; 83 } 84 85 void MachineBasicBlock::dump() const 86 { 87 print(std::cerr); 88 } 89 90 void MachineBasicBlock::print(std::ostream &OS) const 91 { 92 if(!getParent()) { 93 OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n"; 94 return; 95 } 96 const BasicBlock *LBB = getBasicBlock(); 97 if(LBB) 98 OS << "\n" << LBB->getName() << " (" << (const void*)LBB << "):\n"; 99 for (const_iterator I = begin(); I != end(); ++I) { 100 OS << "\t"; 101 I->print(OS, getParent()->getTarget()); 102 } 103 } 104