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 "llvm/Support/LeakDetector.h" 21 #include <iostream> 22 using namespace llvm; 23 24 MachineBasicBlock::~MachineBasicBlock() { 25 LeakDetector::removeGarbageObject(this); 26 } 27 28 29 30 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it 31 // gets the next available unique MBB number. If it is removed from a 32 // MachineFunction, it goes back to being #-1. 33 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { 34 assert(N->Parent == 0 && "machine instruction already in a basic block"); 35 N->Parent = Parent; 36 N->Number = Parent->addToMBBNumbering(N); 37 LeakDetector::removeGarbageObject(N); 38 } 39 40 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { 41 assert(N->Parent != 0 && "machine instruction not in a basic block"); 42 N->Parent->removeFromMBBNumbering(N->Number); 43 N->Number = -1; 44 N->Parent = 0; 45 LeakDetector::addGarbageObject(N); 46 } 47 48 49 MachineInstr* ilist_traits<MachineInstr>::createNode() { 50 MachineInstr* dummy = new MachineInstr(0, 0); 51 LeakDetector::removeGarbageObject(dummy); 52 return dummy; 53 } 54 55 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) 56 { 57 assert(N->parent == 0 && "machine instruction already in a basic block"); 58 N->parent = parent; 59 LeakDetector::removeGarbageObject(N); 60 } 61 62 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) 63 { 64 assert(N->parent != 0 && "machine instruction not in a basic block"); 65 N->parent = 0; 66 LeakDetector::addGarbageObject(N); 67 } 68 69 void ilist_traits<MachineInstr>::transferNodesFromList( 70 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 71 ilist_iterator<MachineInstr> first, 72 ilist_iterator<MachineInstr> last) 73 { 74 if (parent != toList.parent) 75 for (; first != last; ++first) 76 first->parent = toList.parent; 77 } 78 79 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() 80 { 81 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); 82 iterator I = end(); 83 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 84 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 85 return I; 86 } 87 88 void MachineBasicBlock::dump() const 89 { 90 print(std::cerr); 91 } 92 93 void MachineBasicBlock::print(std::ostream &OS) const 94 { 95 if(!getParent()) { 96 OS << "Can't print out MachineBasicBlock because parent MachineFunction is null\n"; 97 return; 98 } 99 100 const BasicBlock *LBB = getBasicBlock(); 101 if (LBB) 102 OS << "\n" << LBB->getName() << " (" << (const void*)this 103 << ", LLVM BB @" << (const void*) LBB << "):\n"; 104 for (const_iterator I = begin(); I != end(); ++I) { 105 OS << "\t"; 106 I->print(OS, &getParent()->getTarget()); 107 } 108 } 109