MachineBasicBlock.cpp revision c21c5eeb4f56f160e79522df2d3aab5cfe73c05d
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 <algorithm> 23using namespace llvm; 24 25MachineBasicBlock::~MachineBasicBlock() { 26 LeakDetector::removeGarbageObject(this); 27} 28 29std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) { 30 MBB.print(OS); 31 return OS; 32} 33 34OStream& llvm::operator<<(OStream &OS, const MachineBasicBlock &MBB) { 35 if (OS.stream()) *OS.stream() << MBB; 36 return OS; 37} 38 39// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 40// gets the next available unique MBB number. If it is removed from a 41// MachineFunction, it goes back to being #-1. 42void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { 43 assert(N->Parent == 0 && "machine instruction already in a basic block"); 44 N->Parent = Parent; 45 N->Number = Parent->addToMBBNumbering(N); 46 LeakDetector::removeGarbageObject(N); 47} 48 49void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { 50 assert(N->Parent != 0 && "machine instruction not in a basic block"); 51 N->Parent->removeFromMBBNumbering(N->Number); 52 N->Number = -1; 53 N->Parent = 0; 54 LeakDetector::addGarbageObject(N); 55} 56 57 58MachineInstr* ilist_traits<MachineInstr>::createSentinel() { 59 MachineInstr* dummy = new MachineInstr(); 60 LeakDetector::removeGarbageObject(dummy); 61 return dummy; 62} 63 64void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) { 65 assert(N->parent == 0 && "machine instruction already in a basic block"); 66 N->parent = parent; 67 LeakDetector::removeGarbageObject(N); 68} 69 70void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) { 71 assert(N->parent != 0 && "machine instruction not in a basic block"); 72 N->parent = 0; 73 LeakDetector::addGarbageObject(N); 74} 75 76void ilist_traits<MachineInstr>::transferNodesFromList( 77 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList, 78 ilist_iterator<MachineInstr> first, 79 ilist_iterator<MachineInstr> last) { 80 if (parent != fromList.parent) 81 for (; first != last; ++first) 82 first->parent = parent; 83} 84 85MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 86 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); 87 iterator I = end(); 88 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 89 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 90 return I; 91} 92 93void MachineBasicBlock::dump() const { 94 print(*cerr.stream()); 95} 96 97void MachineBasicBlock::print(std::ostream &OS) const { 98 if(!getParent()) { 99 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 100 << " is null\n"; 101 return; 102 } 103 104 const BasicBlock *LBB = getBasicBlock(); 105 OS << "\n"; 106 if (LBB) OS << LBB->getName(); 107 OS << " (" << (const void*)this 108 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber()<< "):\n"; 109 // Print the preds of this block according to the CFG. 110 if (!pred_empty()) { 111 OS << " Predecessors according to CFG:"; 112 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 113 OS << " " << *PI; 114 OS << "\n"; 115 } 116 117 for (const_iterator I = begin(); I != end(); ++I) { 118 OS << "\t"; 119 I->print(OS, &getParent()->getTarget()); 120 } 121 122 // Print the successors of this block according to the CFG. 123 if (!succ_empty()) { 124 OS << " Successors according to CFG:"; 125 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 126 OS << " " << *SI; 127 OS << "\n"; 128 } 129} 130 131void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 132 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); 133 getParent()->getBasicBlockList().splice(NewAfter, BBList, this); 134} 135 136void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 137 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); 138 MachineFunction::iterator BBI = NewBefore; 139 getParent()->getBasicBlockList().splice(++BBI, BBList, this); 140} 141 142 143void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 144 Successors.push_back(succ); 145 succ->addPredecessor(this); 146} 147 148void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 149 succ->removePredecessor(this); 150 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 151 assert(I != Successors.end() && "Not a current successor!"); 152 Successors.erase(I); 153} 154 155void MachineBasicBlock::removeSuccessor(succ_iterator I) { 156 assert(I != Successors.end() && "Not a current successor!"); 157 (*I)->removePredecessor(this); 158 Successors.erase(I); 159} 160 161void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 162 Predecessors.push_back(pred); 163} 164 165void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 166 std::vector<MachineBasicBlock *>::iterator I = 167 std::find(Predecessors.begin(), Predecessors.end(), pred); 168 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 169 Predecessors.erase(I); 170} 171