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/TargetData.h" 19 #include "llvm/Target/TargetInstrInfo.h" 20 #include "llvm/Target/TargetMachine.h" 21 #include "llvm/Support/LeakDetector.h" 22 #include <iostream> 23 #include <algorithm> 24 using namespace llvm; 25 26 MachineBasicBlock::~MachineBasicBlock() { 27 LeakDetector::removeGarbageObject(this); 28 } 29 30 31 // MBBs start out as #-1. When a MBB is added to a MachineFunction, it 32 // gets the next available unique MBB number. If it is removed from a 33 // MachineFunction, it goes back to being #-1. 34 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { 35 assert(N->Parent == 0 && "machine instruction already in a basic block"); 36 N->Parent = Parent; 37 N->Number = Parent->addToMBBNumbering(N); 38 LeakDetector::removeGarbageObject(N); 39 } 40 41 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { 42 assert(N->Parent != 0 && "machine instruction not in a basic block"); 43 N->Parent->removeFromMBBNumbering(N->Number); 44 N->Number = -1; 45 N->Parent = 0; 46 LeakDetector::addGarbageObject(N); 47 } 48 49 50 MachineInstr* ilist_traits<MachineInstr>::createSentinel() { 51 MachineInstr* dummy = new MachineInstr(0, 0); 52 LeakDetector::removeGarbageObject(dummy); 53 return dummy; 54 } 55 56 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) { 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 assert(N->parent != 0 && "machine instruction not in a basic block"); 64 N->parent = 0; 65 LeakDetector::addGarbageObject(N); 66 } 67 68 void ilist_traits<MachineInstr>::transferNodesFromList( 69 iplist<MachineInstr, ilist_traits<MachineInstr> >& toList, 70 ilist_iterator<MachineInstr> first, 71 ilist_iterator<MachineInstr> last) { 72 if (parent != toList.parent) 73 for (; first != last; ++first) 74 first->parent = toList.parent; 75 } 76 77 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 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 print(std::cerr); 87 } 88 89 void MachineBasicBlock::print(std::ostream &OS) const { 90 if(!getParent()) { 91 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 92 << " is null\n"; 93 return; 94 } 95 96 const BasicBlock *LBB = getBasicBlock(); 97 if (LBB) 98 OS << "\n" << LBB->getName() << " (" << (const void*)this 99 << ", LLVM BB @" << (const void*) LBB << "):\n"; 100 // Print the preds of this block according to the CFG. 101 if (!pred_empty()) { 102 OS << " Predecessors according to CFG:"; 103 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 104 OS << " " << *PI; 105 OS << "\n"; 106 } 107 108 for (const_iterator I = begin(); I != end(); ++I) { 109 OS << "\t"; 110 I->print(OS, &getParent()->getTarget()); 111 } 112 113 // Print the successors of this block according to the CFG. 114 if (!succ_empty()) { 115 OS << " Successors according to CFG:"; 116 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 117 OS << " " << *SI; 118 OS << "\n"; 119 } 120 } 121 122 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 123 Successors.push_back(succ); 124 succ->addPredecessor(this); 125 } 126 127 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 128 succ->removePredecessor(this); 129 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 130 assert(I != Successors.end() && "Not a current successor!"); 131 Successors.erase(I); 132 } 133 134 void MachineBasicBlock::removeSuccessor(succ_iterator I) { 135 assert(I != Successors.end() && "Not a current successor!"); 136 (*I)->removePredecessor(this); 137 Successors.erase(I); 138 } 139 140 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 141 Predecessors.push_back(pred); 142 } 143 144 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 145 std::vector<MachineBasicBlock *>::iterator I = 146 std::find(Predecessors.begin(), Predecessors.end(), pred); 147 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 148 Predecessors.erase(I); 149 } 150