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 #include <algorithm> 23 using namespace llvm; 24 25 MachineBasicBlock::~MachineBasicBlock() { 26 LeakDetector::removeGarbageObject(this); 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>::createSentinel() { 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 assert(N->parent == 0 && "machine instruction already in a basic block"); 57 N->parent = parent; 58 LeakDetector::removeGarbageObject(N); 59 } 60 61 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) { 62 assert(N->parent != 0 && "machine instruction not in a basic block"); 63 N->parent = 0; 64 LeakDetector::addGarbageObject(N); 65 } 66 67 void ilist_traits<MachineInstr>::transferNodesFromList( 68 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 69 ilist_iterator<MachineInstr> first, 70 ilist_iterator<MachineInstr> last) { 71 if (parent != toList.parent) 72 for (; first != last; ++first) 73 first->parent = toList.parent; 74 } 75 76 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 77 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); 78 iterator I = end(); 79 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 80 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 81 return I; 82 } 83 84 void MachineBasicBlock::dump() const { 85 print(std::cerr); 86 } 87 88 void MachineBasicBlock::print(std::ostream &OS) const { 89 if(!getParent()) { 90 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 91 << " is null\n"; 92 return; 93 } 94 95 const BasicBlock *LBB = getBasicBlock(); 96 if (LBB) 97 OS << "\n" << LBB->getName() << " (" << (const void*)this 98 << ", LLVM BB @" << (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 // Print the successors of this block according to the CFG. 105 if (!succ_empty()) { 106 OS << " Successors according to CFG:"; 107 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 108 OS << " " << *SI; 109 OS << "\n"; 110 } 111 } 112 113 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 114 Successors.push_back(succ); 115 succ->addPredecessor(this); 116 } 117 118 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 119 succ->removePredecessor(this); 120 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 121 assert(I != Successors.end() && "Not a current successor!"); 122 Successors.erase(I); 123 } 124 125 void MachineBasicBlock::removeSuccessor(succ_iterator I) { 126 assert(I != Successors.end() && "Not a current successor!"); 127 (*I)->removePredecessor(this); 128 Successors.erase(I); 129 } 130 131 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 132 Predecessors.push_back(pred); 133 } 134 135 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 136 std::vector<MachineBasicBlock *>::iterator I = 137 std::find(Predecessors.begin(), Predecessors.end(), pred); 138 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 139 Predecessors.erase(I); 140 } 141