MachineBasicBlock.cpp revision cb406c25973b4e88a6c10ad839ef1beeb3664715
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 ; /*noop */ 86 if (I != end() && !TII.isTerminatorInstr(I->getOpcode())) ++I; 87 return I; 88} 89 90void MachineBasicBlock::dump() const { 91 print(*cerr.stream()); 92} 93 94static inline void OutputReg(std::ostream &os, unsigned RegNo, 95 const MRegisterInfo *MRI = 0) { 96 if (!RegNo || MRegisterInfo::isPhysicalRegister(RegNo)) { 97 if (MRI) 98 os << " %" << MRI->get(RegNo).Name; 99 else 100 os << " %mreg(" << RegNo << ")"; 101 } else 102 os << " %reg" << RegNo; 103} 104 105void MachineBasicBlock::print(std::ostream &OS) const { 106 const MachineFunction *MF = getParent(); 107 if(!MF) { 108 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 109 << " is null\n"; 110 return; 111 } 112 113 const BasicBlock *LBB = getBasicBlock(); 114 OS << "\n"; 115 if (LBB) OS << LBB->getName() << ": "; 116 OS << (const void*)this 117 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber(); 118 if (isLandingPad()) OS << ", EH LANDING PAD"; 119 OS << ":\n"; 120 121 const MRegisterInfo *MRI = MF->getTarget().getRegisterInfo(); 122 if (!livein_empty()) { 123 OS << "Live Ins:"; 124 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 125 OutputReg(OS, *I, MRI); 126 OS << "\n"; 127 } 128 // Print the preds of this block according to the CFG. 129 if (!pred_empty()) { 130 OS << " Predecessors according to CFG:"; 131 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 132 OS << " " << *PI << " (#" << (*PI)->getNumber() << ")"; 133 OS << "\n"; 134 } 135 136 for (const_iterator I = begin(); I != end(); ++I) { 137 OS << "\t"; 138 I->print(OS, &getParent()->getTarget()); 139 } 140 141 // Print the successors of this block according to the CFG. 142 if (!succ_empty()) { 143 OS << " Successors according to CFG:"; 144 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 145 OS << " " << *SI << " (#" << (*SI)->getNumber() << ")"; 146 OS << "\n"; 147 } 148} 149 150void MachineBasicBlock::removeLiveIn(unsigned Reg) { 151 livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 152 assert(I != livein_end() && "Not a live in!"); 153 LiveIns.erase(I); 154} 155 156void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 157 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); 158 getParent()->getBasicBlockList().splice(NewAfter, BBList, this); 159} 160 161void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 162 MachineFunction::BasicBlockListType &BBList =getParent()->getBasicBlockList(); 163 MachineFunction::iterator BBI = NewBefore; 164 getParent()->getBasicBlockList().splice(++BBI, BBList, this); 165} 166 167 168void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 169 Successors.push_back(succ); 170 succ->addPredecessor(this); 171} 172 173void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 174 succ->removePredecessor(this); 175 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 176 assert(I != Successors.end() && "Not a current successor!"); 177 Successors.erase(I); 178} 179 180MachineBasicBlock::succ_iterator MachineBasicBlock::removeSuccessor(succ_iterator I) { 181 assert(I != Successors.end() && "Not a current successor!"); 182 (*I)->removePredecessor(this); 183 return(Successors.erase(I)); 184} 185 186void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 187 Predecessors.push_back(pred); 188} 189 190void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 191 std::vector<MachineBasicBlock *>::iterator I = 192 std::find(Predecessors.begin(), Predecessors.end(), pred); 193 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 194 Predecessors.erase(I); 195} 196 197bool MachineBasicBlock::isSuccessor(MachineBasicBlock *MBB) const { 198 std::vector<MachineBasicBlock *>::const_iterator I = 199 std::find(Successors.begin(), Successors.end(), MBB); 200 return I != Successors.end(); 201} 202 203/// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 204/// 'Old', change the code and CFG so that it branches to 'New' instead. 205void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 206 MachineBasicBlock *New) { 207 assert(Old != New && "Cannot replace self with self!"); 208 209 MachineBasicBlock::iterator I = end(); 210 while (I != begin()) { 211 --I; 212 if (!(I->getInstrDescriptor()->Flags & M_TERMINATOR_FLAG)) break; 213 214 // Scan the operands of this machine instruction, replacing any uses of Old 215 // with New. 216 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 217 if (I->getOperand(i).isMachineBasicBlock() && 218 I->getOperand(i).getMachineBasicBlock() == Old) 219 I->getOperand(i).setMachineBasicBlock(New); 220 } 221 222 // Update the successor information. If New was already a successor, just 223 // remove the link to Old instead of creating another one. PR 1444. 224 removeSuccessor(Old); 225 if (!isSuccessor(New)) 226 addSuccessor(New); 227} 228 229/// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 230/// CFG to be inserted. If we have proven that MBB can only branch to DestA and 231/// DestB, remove any other MBB successors from the CFG. DestA and DestB can 232/// be null. 233/// Besides DestA and DestB, retain other edges leading to LandingPads (currently 234/// there can be only one; we don't check or require that here). 235/// Note it is possible that DestA and/or DestB are LandingPads. 236bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 237 MachineBasicBlock *DestB, 238 bool isCond) { 239 bool MadeChange = false; 240 bool AddedFallThrough = false; 241 242 MachineBasicBlock *FallThru = getNext(); 243 244 // If this block ends with a conditional branch that falls through to its 245 // successor, set DestB as the successor. 246 if (isCond) { 247 if (DestB == 0 && FallThru != getParent()->end()) { 248 DestB = FallThru; 249 AddedFallThrough = true; 250 } 251 } else { 252 // If this is an unconditional branch with no explicit dest, it must just be 253 // a fallthrough into DestB. 254 if (DestA == 0 && FallThru != getParent()->end()) { 255 DestA = FallThru; 256 AddedFallThrough = true; 257 } 258 } 259 260 MachineBasicBlock::succ_iterator SI = succ_begin(); 261 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB; 262 while (SI != succ_end()) { 263 if (*SI == DestA && DestA == DestB) { 264 DestA = DestB = 0; 265 ++SI; 266 } else if (*SI == DestA) { 267 DestA = 0; 268 ++SI; 269 } else if (*SI == DestB) { 270 DestB = 0; 271 ++SI; 272 } else if ((*SI)->isLandingPad() && 273 *SI!=OrigDestA && *SI!=OrigDestB) { 274 ++SI; 275 } else { 276 // Otherwise, this is a superfluous edge, remove it. 277 SI = removeSuccessor(SI); 278 MadeChange = true; 279 } 280 } 281 if (!AddedFallThrough) { 282 assert(DestA == 0 && DestB == 0 && 283 "MachineCFG is missing edges!"); 284 } else if (isCond) { 285 assert(DestA == 0 && "MachineCFG is missing edges!"); 286 } 287 return MadeChange; 288} 289