1 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // 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/Target/TargetRegisterInfo.h" 18 #include "llvm/Target/TargetData.h" 19 #include "llvm/Target/TargetInstrDesc.h" 20 #include "llvm/Target/TargetMachine.h" 21 #include "llvm/Support/LeakDetector.h" 22 #include "llvm/Support/raw_ostream.h" 23 #include <algorithm> 24 using namespace llvm; 25 26 MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 27 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 28 AddressTaken(false) { 29 Insts.Parent = this; 30 } 31 32 MachineBasicBlock::~MachineBasicBlock() { 33 LeakDetector::removeGarbageObject(this); 34 } 35 36 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 37 MBB.print(OS); 38 return OS; 39 } 40 41 /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 42 /// parent pointer of the MBB, the MBB numbering, and any instructions in the 43 /// MBB to be on the right operand list for registers. 44 /// 45 /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 46 /// gets the next available unique MBB number. If it is removed from a 47 /// MachineFunction, it goes back to being #-1. 48 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 49 MachineFunction &MF = *N->getParent(); 50 N->Number = MF.addToMBBNumbering(N); 51 52 // Make sure the instructions have their operands in the reginfo lists. 53 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 54 for (MachineBasicBlock::iterator I = N->begin(), E = N->end(); I != E; ++I) 55 I->AddRegOperandsToUseLists(RegInfo); 56 57 LeakDetector::removeGarbageObject(N); 58 } 59 60 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 61 N->getParent()->removeFromMBBNumbering(N->Number); 62 N->Number = -1; 63 LeakDetector::addGarbageObject(N); 64 } 65 66 67 /// addNodeToList (MI) - When we add an instruction to a basic block 68 /// list, we update its parent pointer and add its operands from reg use/def 69 /// lists if appropriate. 70 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 71 assert(N->getParent() == 0 && "machine instruction already in a basic block"); 72 N->setParent(Parent); 73 74 // Add the instruction's register operands to their corresponding 75 // use/def lists. 76 MachineFunction *MF = Parent->getParent(); 77 N->AddRegOperandsToUseLists(MF->getRegInfo()); 78 79 LeakDetector::removeGarbageObject(N); 80 } 81 82 /// removeNodeFromList (MI) - When we remove an instruction from a basic block 83 /// list, we update its parent pointer and remove its operands from reg use/def 84 /// lists if appropriate. 85 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 86 assert(N->getParent() != 0 && "machine instruction not in a basic block"); 87 88 // Remove from the use/def lists. 89 N->RemoveRegOperandsFromUseLists(); 90 91 N->setParent(0); 92 93 LeakDetector::addGarbageObject(N); 94 } 95 96 /// transferNodesFromList (MI) - When moving a range of instructions from one 97 /// MBB list to another, we need to update the parent pointers and the use/def 98 /// lists. 99 void ilist_traits<MachineInstr>:: 100 transferNodesFromList(ilist_traits<MachineInstr> &fromList, 101 MachineBasicBlock::iterator first, 102 MachineBasicBlock::iterator last) { 103 assert(Parent->getParent() == fromList.Parent->getParent() && 104 "MachineInstr parent mismatch!"); 105 106 // Splice within the same MBB -> no change. 107 if (Parent == fromList.Parent) return; 108 109 // If splicing between two blocks within the same function, just update the 110 // parent pointers. 111 for (; first != last; ++first) 112 first->setParent(Parent); 113 } 114 115 void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 116 assert(!MI->getParent() && "MI is still in a block!"); 117 Parent->getParent()->DeleteMachineInstr(MI); 118 } 119 120 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 121 iterator I = end(); 122 while (I != begin() && (--I)->getDesc().isTerminator()) 123 ; /*noop */ 124 if (I != end() && !I->getDesc().isTerminator()) ++I; 125 return I; 126 } 127 128 /// isOnlyReachableViaFallthough - Return true if this basic block has 129 /// exactly one predecessor and the control transfer mechanism between 130 /// the predecessor and this block is a fall-through. 131 bool MachineBasicBlock::isOnlyReachableByFallthrough() const { 132 // If this is a landing pad, it isn't a fall through. If it has no preds, 133 // then nothing falls through to it. 134 if (isLandingPad() || pred_empty()) 135 return false; 136 137 // If there isn't exactly one predecessor, it can't be a fall through. 138 const_pred_iterator PI = pred_begin(), PI2 = PI; 139 ++PI2; 140 if (PI2 != pred_end()) 141 return false; 142 143 // The predecessor has to be immediately before this block. 144 const MachineBasicBlock *Pred = *PI; 145 146 if (!Pred->isLayoutSuccessor(this)) 147 return false; 148 149 // If the block is completely empty, then it definitely does fall through. 150 if (Pred->empty()) 151 return true; 152 153 // Otherwise, check the last instruction. 154 const MachineInstr &LastInst = Pred->back(); 155 return !LastInst.getDesc().isBarrier(); 156 } 157 158 void MachineBasicBlock::dump() const { 159 print(errs()); 160 } 161 162 static inline void OutputReg(raw_ostream &os, unsigned RegNo, 163 const TargetRegisterInfo *TRI = 0) { 164 if (!RegNo || TargetRegisterInfo::isPhysicalRegister(RegNo)) { 165 if (TRI) 166 os << " %" << TRI->get(RegNo).Name; 167 else 168 os << " %mreg(" << RegNo << ")"; 169 } else 170 os << " %reg" << RegNo; 171 } 172 173 void MachineBasicBlock::print(raw_ostream &OS) const { 174 const MachineFunction *MF = getParent(); 175 if (!MF) { 176 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 177 << " is null\n"; 178 return; 179 } 180 181 const BasicBlock *LBB = getBasicBlock(); 182 OS << '\n'; 183 if (LBB) OS << LBB->getName() << ": "; 184 OS << (const void*)this 185 << ", LLVM BB @" << (const void*) LBB << ", ID#" << getNumber(); 186 if (Alignment) OS << ", Alignment " << Alignment; 187 if (isLandingPad()) OS << ", EH LANDING PAD"; 188 OS << ":\n"; 189 190 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 191 if (!livein_empty()) { 192 OS << "Live Ins:"; 193 for (const_livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 194 OutputReg(OS, *I, TRI); 195 OS << '\n'; 196 } 197 // Print the preds of this block according to the CFG. 198 if (!pred_empty()) { 199 OS << " Predecessors according to CFG:"; 200 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 201 OS << ' ' << *PI << " (#" << (*PI)->getNumber() << ')'; 202 OS << '\n'; 203 } 204 205 for (const_iterator I = begin(); I != end(); ++I) { 206 OS << '\t'; 207 I->print(OS, &getParent()->getTarget()); 208 } 209 210 // Print the successors of this block according to the CFG. 211 if (!succ_empty()) { 212 OS << " Successors according to CFG:"; 213 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) 214 OS << ' ' << *SI << " (#" << (*SI)->getNumber() << ')'; 215 OS << '\n'; 216 } 217 } 218 219 void MachineBasicBlock::removeLiveIn(unsigned Reg) { 220 livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 221 assert(I != livein_end() && "Not a live in!"); 222 LiveIns.erase(I); 223 } 224 225 bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 226 const_livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 227 return I != livein_end(); 228 } 229 230 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 231 getParent()->splice(NewAfter, this); 232 } 233 234 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 235 MachineFunction::iterator BBI = NewBefore; 236 getParent()->splice(++BBI, this); 237 } 238 239 240 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ) { 241 Successors.push_back(succ); 242 succ->addPredecessor(this); 243 } 244 245 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 246 succ->removePredecessor(this); 247 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 248 assert(I != Successors.end() && "Not a current successor!"); 249 Successors.erase(I); 250 } 251 252 MachineBasicBlock::succ_iterator 253 MachineBasicBlock::removeSuccessor(succ_iterator I) { 254 assert(I != Successors.end() && "Not a current successor!"); 255 (*I)->removePredecessor(this); 256 return Successors.erase(I); 257 } 258 259 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 260 Predecessors.push_back(pred); 261 } 262 263 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 264 std::vector<MachineBasicBlock *>::iterator I = 265 std::find(Predecessors.begin(), Predecessors.end(), pred); 266 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 267 Predecessors.erase(I); 268 } 269 270 void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 271 if (this == fromMBB) 272 return; 273 274 for (MachineBasicBlock::succ_iterator I = fromMBB->succ_begin(), 275 E = fromMBB->succ_end(); I != E; ++I) 276 addSuccessor(*I); 277 278 while (!fromMBB->succ_empty()) 279 fromMBB->removeSuccessor(fromMBB->succ_begin()); 280 } 281 282 bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 283 std::vector<MachineBasicBlock *>::const_iterator I = 284 std::find(Successors.begin(), Successors.end(), MBB); 285 return I != Successors.end(); 286 } 287 288 bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 289 MachineFunction::const_iterator I(this); 290 return next(I) == MachineFunction::const_iterator(MBB); 291 } 292 293 /// removeFromParent - This method unlinks 'this' from the containing function, 294 /// and returns it, but does not delete it. 295 MachineBasicBlock *MachineBasicBlock::removeFromParent() { 296 assert(getParent() && "Not embedded in a function!"); 297 getParent()->remove(this); 298 return this; 299 } 300 301 302 /// eraseFromParent - This method unlinks 'this' from the containing function, 303 /// and deletes it. 304 void MachineBasicBlock::eraseFromParent() { 305 assert(getParent() && "Not embedded in a function!"); 306 getParent()->erase(this); 307 } 308 309 310 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 311 /// 'Old', change the code and CFG so that it branches to 'New' instead. 312 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 313 MachineBasicBlock *New) { 314 assert(Old != New && "Cannot replace self with self!"); 315 316 MachineBasicBlock::iterator I = end(); 317 while (I != begin()) { 318 --I; 319 if (!I->getDesc().isTerminator()) break; 320 321 // Scan the operands of this machine instruction, replacing any uses of Old 322 // with New. 323 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 324 if (I->getOperand(i).isMBB() && 325 I->getOperand(i).getMBB() == Old) 326 I->getOperand(i).setMBB(New); 327 } 328 329 // Update the successor information. 330 removeSuccessor(Old); 331 addSuccessor(New); 332 } 333 334 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 335 /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 336 /// DestB, remove any other MBB successors from the CFG. DestA and DestB can 337 /// be null. 338 /// Besides DestA and DestB, retain other edges leading to LandingPads 339 /// (currently there can be only one; we don't check or require that here). 340 /// Note it is possible that DestA and/or DestB are LandingPads. 341 bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 342 MachineBasicBlock *DestB, 343 bool isCond) { 344 bool MadeChange = false; 345 bool AddedFallThrough = false; 346 347 MachineFunction::iterator FallThru = next(MachineFunction::iterator(this)); 348 349 // If this block ends with a conditional branch that falls through to its 350 // successor, set DestB as the successor. 351 if (isCond) { 352 if (DestB == 0 && FallThru != getParent()->end()) { 353 DestB = FallThru; 354 AddedFallThrough = true; 355 } 356 } else { 357 // If this is an unconditional branch with no explicit dest, it must just be 358 // a fallthrough into DestB. 359 if (DestA == 0 && FallThru != getParent()->end()) { 360 DestA = FallThru; 361 AddedFallThrough = true; 362 } 363 } 364 365 MachineBasicBlock::succ_iterator SI = succ_begin(); 366 MachineBasicBlock *OrigDestA = DestA, *OrigDestB = DestB; 367 while (SI != succ_end()) { 368 if (*SI == DestA && DestA == DestB) { 369 DestA = DestB = 0; 370 ++SI; 371 } else if (*SI == DestA) { 372 DestA = 0; 373 ++SI; 374 } else if (*SI == DestB) { 375 DestB = 0; 376 ++SI; 377 } else if ((*SI)->isLandingPad() && 378 *SI!=OrigDestA && *SI!=OrigDestB) { 379 ++SI; 380 } else { 381 // Otherwise, this is a superfluous edge, remove it. 382 SI = removeSuccessor(SI); 383 MadeChange = true; 384 } 385 } 386 if (!AddedFallThrough) { 387 assert(DestA == 0 && DestB == 0 && 388 "MachineCFG is missing edges!"); 389 } else if (isCond) { 390 assert(DestA == 0 && "MachineCFG is missing edges!"); 391 } 392 return MadeChange; 393 } 394