MachineBasicBlock.cpp revision 0370fad74b48388412c52d1325512f2c218487fa
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/MRegisterInfo.h" 19#include "llvm/Target/TargetData.h" 20#include "llvm/Target/TargetInstrInfo.h" 21#include "llvm/Target/TargetMachine.h" 22#include "llvm/Support/LeakDetector.h" 23#include <algorithm> 24using namespace llvm; 25 26MachineBasicBlock::~MachineBasicBlock() { 27 LeakDetector::removeGarbageObject(this); 28} 29 30std::ostream& llvm::operator<<(std::ostream &OS, const MachineBasicBlock &MBB) { 31 MBB.print(OS); 32 return OS; 33} 34 35// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 36// gets the next available unique MBB number. If it is removed from a 37// MachineFunction, it goes back to being #-1. 38void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock* N) { 39 assert(N->Parent == 0 && "machine instruction already in a basic block"); 40 N->Parent = Parent; 41 N->Number = Parent->addToMBBNumbering(N); 42 LeakDetector::removeGarbageObject(N); 43} 44 45void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock* N) { 46 assert(N->Parent != 0 && "machine instruction not in a basic block"); 47 N->Parent->removeFromMBBNumbering(N->Number); 48 N->Number = -1; 49 N->Parent = 0; 50 LeakDetector::addGarbageObject(N); 51} 52 53 54MachineInstr* ilist_traits<MachineInstr>::createSentinel() { 55 MachineInstr* dummy = new MachineInstr(); 56 LeakDetector::removeGarbageObject(dummy); 57 return dummy; 58} 59 60void ilist_traits<MachineInstr>::addNodeToList(MachineInstr* N) { 61 assert(N->parent == 0 && "machine instruction already in a basic block"); 62 N->parent = parent; 63 LeakDetector::removeGarbageObject(N); 64} 65 66void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr* N) { 67 assert(N->parent != 0 && "machine instruction not in a basic block"); 68 N->parent = 0; 69 LeakDetector::addGarbageObject(N); 70} 71 72void ilist_traits<MachineInstr>::transferNodesFromList( 73 iplist<MachineInstr, ilist_traits<MachineInstr> >& fromList, 74 ilist_iterator<MachineInstr> first, 75 ilist_iterator<MachineInstr> last) { 76 if (parent != fromList.parent) 77 for (; first != last; ++first) 78 first->parent = parent; 79} 80 81MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 82 const TargetInstrInfo& TII = *getParent()->getTarget().getInstrInfo(); 83 iterator I = end(); 84 while (I != begin() && TII.isTerminatorInstr((--I)->getOpcode())); 85 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 86 return I; 87} 88 89void MachineBasicBlock::dump() const { 90 print(*cerr.stream()); 91} 92 93static inline void OutputReg(std::ostream &os, unsigned RegNo, 94 const MRegisterInfo *MRI = 0) { 95 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) { 96 if (MRI) 97 os << " %" << MRI->get(RegNo).Name; 98 else 99 os << " %mreg(" << RegNo << ")"; 100 } else 101 os << " %reg" << RegNo; 102} 103 104void MachineBasicBlock::print(std::ostream &OS) const { 105 const MachineFunction *MF = getParent(); 106 if(!MF) { 107 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 108 << " is null\n"; 109 return; 110 } 111 112 const BasicBlock *LBB = getBasicBlock(); 113 OS << "\n"; 114 if (LBB) OS << LBB->getName() << ": "; 115 OS << (const void*)this 116 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber(); 117 if (isLandingPad()) OS << ", EH LANDING PAD"; 118 OS << ":\n"; 119 120 const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo(); 121 if (livein_begin() != livein_end()) { 122 OS << "Live Ins:"; 123 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 124 OutputReg(OS, *I, MRI); 125 OS << "\n"; 126 } 127 // Print the preds of this block according to the CFG. 128 if (!pred_empty()) { 129 OS << " Predecessors according to CFG:"; 130 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 131 OS << " " << *PI << " (#" << (*PI)->getNumber() << ")"; 132 OS << "\n"; 133 } 134 135 for (const_iterator I = begin(); I != end(); ++I) { 136 OS << "\t"; 137 I->print(OS, &getParent()->getTarget()); 138 } 139 140 // Print the successors of this block according to the CFG. 141 if (!succ_empty()) { 142 OS << " Successors according to CFG:"; 143 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 144 OS << " " << *SI << " (#" << (*SI)->getNumber() << ")"; 145 OS << "\n"; 146 } 147} 148 149void MachineBasicBlock::removeLiveIn(unsigned Reg) { 150 livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 151 assert(I != livein_end() && "Not a live in!"); 152 LiveIns.erase(I); 153} 154 155void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 156 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); 157 getParent()->getBasicBlockList().splice(NewAfter, BBList, this); 158} 159 160void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 161 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); 162 MachineFunction::iterator BBI = NewBefore; 163 getParent()->getBasicBlockList().splice(++BBI, BBList, this); 164} 165 166 167void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 168 Successors.push_back(succ); 169 succ->addPredecessor(this); 170} 171 172void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 173 succ->removePredecessor(this); 174 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 175 assert(I != Successors.end() && "Not a current successor!"); 176 Successors.erase(I); 177} 178 179void MachineBasicBlock::removeSuccessor(succ_iterator I) { 180 assert(I != Successors.end() && "Not a current successor!"); 181 (*I)->removePredecessor(this); 182 Successors.erase(I); 183} 184 185void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 186 Predecessors.push_back(pred); 187} 188 189void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 190 std::vector<MachineBasicBlock *>::iterator I = 191 std::find(Predecessors.begin(), Predecessors.end(), pred); 192 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 193 Predecessors.erase(I); 194} 195 196bool MachineBasicBlock::isSuccessor(MachineBasicBlock *MBB) const { 197 std::vector<MachineBasicBlock *>::const_iterator I = 198 std::find(Successors.begin(), Successors.end(), MBB); 199 return I != Successors.end(); 200} 201 202/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 203/// 'Old', change the code and CFG so that it branches to 'New' instead. 204void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 205 MachineBasicBlock *New) { 206 assert(Old != New && "Cannot replace self with self!"); 207 208 MachineBasicBlock::iterator I = end(); 209 while (I != begin()) { 210 --I; 211 if (!(I->getInstrDescriptor()->Flags & M_TERMINATOR_FLAG)) break; 212 213 // Scan the operands of this machine instruction, replacing any uses of Old 214 // with New. 215 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 216 if (I->getOperand(i).isMachineBasicBlock() && 217 I->getOperand(i).getMachineBasicBlock() == Old) 218 I->getOperand(i).setMachineBasicBlock(New); 219 } 220 221 // Update the successor information. If New was already a successor, just 222 // remove the link to Old instead of creating another one. PR 1444. 223 removeSuccessor(Old); 224 if (!isSuccessor(New)) 225 addSuccessor(New); 226} 227 228