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/ADT/SmallPtrSet.h" 16 #include "llvm/ADT/SmallString.h" 17 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 18 #include "llvm/CodeGen/LiveVariables.h" 19 #include "llvm/CodeGen/MachineDominators.h" 20 #include "llvm/CodeGen/MachineFunction.h" 21 #include "llvm/CodeGen/MachineInstrBuilder.h" 22 #include "llvm/CodeGen/MachineLoopInfo.h" 23 #include "llvm/CodeGen/MachineRegisterInfo.h" 24 #include "llvm/CodeGen/SlotIndexes.h" 25 #include "llvm/IR/BasicBlock.h" 26 #include "llvm/IR/DataLayout.h" 27 #include "llvm/IR/LeakDetector.h" 28 #include "llvm/MC/MCAsmInfo.h" 29 #include "llvm/MC/MCContext.h" 30 #include "llvm/Support/Debug.h" 31 #include "llvm/Support/raw_ostream.h" 32 #include "llvm/Target/TargetInstrInfo.h" 33 #include "llvm/Target/TargetMachine.h" 34 #include "llvm/Target/TargetRegisterInfo.h" 35 #include "llvm/Target/TargetSubtargetInfo.h" 36 #include <algorithm> 37 using namespace llvm; 38 39 #define DEBUG_TYPE "codegen" 40 41 MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 42 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 43 AddressTaken(false), CachedMCSymbol(nullptr) { 44 Insts.Parent = this; 45 } 46 47 MachineBasicBlock::~MachineBasicBlock() { 48 LeakDetector::removeGarbageObject(this); 49 } 50 51 /// getSymbol - Return the MCSymbol for this basic block. 52 /// 53 MCSymbol *MachineBasicBlock::getSymbol() const { 54 if (!CachedMCSymbol) { 55 const MachineFunction *MF = getParent(); 56 MCContext &Ctx = MF->getContext(); 57 const TargetMachine &TM = MF->getTarget(); 58 const char *Prefix = 59 TM.getSubtargetImpl()->getDataLayout()->getPrivateGlobalPrefix(); 60 CachedMCSymbol = Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 61 Twine(MF->getFunctionNumber()) + 62 "_" + Twine(getNumber())); 63 } 64 65 return CachedMCSymbol; 66 } 67 68 69 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 70 MBB.print(OS); 71 return OS; 72 } 73 74 /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 75 /// parent pointer of the MBB, the MBB numbering, and any instructions in the 76 /// MBB to be on the right operand list for registers. 77 /// 78 /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 79 /// gets the next available unique MBB number. If it is removed from a 80 /// MachineFunction, it goes back to being #-1. 81 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 82 MachineFunction &MF = *N->getParent(); 83 N->Number = MF.addToMBBNumbering(N); 84 85 // Make sure the instructions have their operands in the reginfo lists. 86 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 87 for (MachineBasicBlock::instr_iterator 88 I = N->instr_begin(), E = N->instr_end(); I != E; ++I) 89 I->AddRegOperandsToUseLists(RegInfo); 90 91 LeakDetector::removeGarbageObject(N); 92 } 93 94 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 95 N->getParent()->removeFromMBBNumbering(N->Number); 96 N->Number = -1; 97 LeakDetector::addGarbageObject(N); 98 } 99 100 101 /// addNodeToList (MI) - When we add an instruction to a basic block 102 /// list, we update its parent pointer and add its operands from reg use/def 103 /// lists if appropriate. 104 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 105 assert(!N->getParent() && "machine instruction already in a basic block"); 106 N->setParent(Parent); 107 108 // Add the instruction's register operands to their corresponding 109 // use/def lists. 110 MachineFunction *MF = Parent->getParent(); 111 N->AddRegOperandsToUseLists(MF->getRegInfo()); 112 113 LeakDetector::removeGarbageObject(N); 114 } 115 116 /// removeNodeFromList (MI) - When we remove an instruction from a basic block 117 /// list, we update its parent pointer and remove its operands from reg use/def 118 /// lists if appropriate. 119 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 120 assert(N->getParent() && "machine instruction not in a basic block"); 121 122 // Remove from the use/def lists. 123 if (MachineFunction *MF = N->getParent()->getParent()) 124 N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); 125 126 N->setParent(nullptr); 127 128 LeakDetector::addGarbageObject(N); 129 } 130 131 /// transferNodesFromList (MI) - When moving a range of instructions from one 132 /// MBB list to another, we need to update the parent pointers and the use/def 133 /// lists. 134 void ilist_traits<MachineInstr>:: 135 transferNodesFromList(ilist_traits<MachineInstr> &fromList, 136 ilist_iterator<MachineInstr> first, 137 ilist_iterator<MachineInstr> last) { 138 assert(Parent->getParent() == fromList.Parent->getParent() && 139 "MachineInstr parent mismatch!"); 140 141 // Splice within the same MBB -> no change. 142 if (Parent == fromList.Parent) return; 143 144 // If splicing between two blocks within the same function, just update the 145 // parent pointers. 146 for (; first != last; ++first) 147 first->setParent(Parent); 148 } 149 150 void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 151 assert(!MI->getParent() && "MI is still in a block!"); 152 Parent->getParent()->DeleteMachineInstr(MI); 153 } 154 155 MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 156 instr_iterator I = instr_begin(), E = instr_end(); 157 while (I != E && I->isPHI()) 158 ++I; 159 assert((I == E || !I->isInsideBundle()) && 160 "First non-phi MI cannot be inside a bundle!"); 161 return I; 162 } 163 164 MachineBasicBlock::iterator 165 MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 166 iterator E = end(); 167 while (I != E && (I->isPHI() || I->isPosition() || I->isDebugValue())) 168 ++I; 169 // FIXME: This needs to change if we wish to bundle labels / dbg_values 170 // inside the bundle. 171 assert((I == E || !I->isInsideBundle()) && 172 "First non-phi / non-label instruction is inside a bundle!"); 173 return I; 174 } 175 176 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 177 iterator B = begin(), E = end(), I = E; 178 while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 179 ; /*noop */ 180 while (I != E && !I->isTerminator()) 181 ++I; 182 return I; 183 } 184 185 MachineBasicBlock::const_iterator 186 MachineBasicBlock::getFirstTerminator() const { 187 const_iterator B = begin(), E = end(), I = E; 188 while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 189 ; /*noop */ 190 while (I != E && !I->isTerminator()) 191 ++I; 192 return I; 193 } 194 195 MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 196 instr_iterator B = instr_begin(), E = instr_end(), I = E; 197 while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 198 ; /*noop */ 199 while (I != E && !I->isTerminator()) 200 ++I; 201 return I; 202 } 203 204 MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 205 // Skip over end-of-block dbg_value instructions. 206 instr_iterator B = instr_begin(), I = instr_end(); 207 while (I != B) { 208 --I; 209 // Return instruction that starts a bundle. 210 if (I->isDebugValue() || I->isInsideBundle()) 211 continue; 212 return I; 213 } 214 // The block is all debug values. 215 return end(); 216 } 217 218 MachineBasicBlock::const_iterator 219 MachineBasicBlock::getLastNonDebugInstr() const { 220 // Skip over end-of-block dbg_value instructions. 221 const_instr_iterator B = instr_begin(), I = instr_end(); 222 while (I != B) { 223 --I; 224 // Return instruction that starts a bundle. 225 if (I->isDebugValue() || I->isInsideBundle()) 226 continue; 227 return I; 228 } 229 // The block is all debug values. 230 return end(); 231 } 232 233 const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { 234 // A block with a landing pad successor only has one other successor. 235 if (succ_size() > 2) 236 return nullptr; 237 for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 238 if ((*I)->isLandingPad()) 239 return *I; 240 return nullptr; 241 } 242 243 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 244 void MachineBasicBlock::dump() const { 245 print(dbgs()); 246 } 247 #endif 248 249 StringRef MachineBasicBlock::getName() const { 250 if (const BasicBlock *LBB = getBasicBlock()) 251 return LBB->getName(); 252 else 253 return "(null)"; 254 } 255 256 /// Return a hopefully unique identifier for this block. 257 std::string MachineBasicBlock::getFullName() const { 258 std::string Name; 259 if (getParent()) 260 Name = (getParent()->getName() + ":").str(); 261 if (getBasicBlock()) 262 Name += getBasicBlock()->getName(); 263 else 264 Name += (Twine("BB") + Twine(getNumber())).str(); 265 return Name; 266 } 267 268 void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { 269 const MachineFunction *MF = getParent(); 270 if (!MF) { 271 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 272 << " is null\n"; 273 return; 274 } 275 276 if (Indexes) 277 OS << Indexes->getMBBStartIdx(this) << '\t'; 278 279 OS << "BB#" << getNumber() << ": "; 280 281 const char *Comma = ""; 282 if (const BasicBlock *LBB = getBasicBlock()) { 283 OS << Comma << "derived from LLVM BB "; 284 LBB->printAsOperand(OS, /*PrintType=*/false); 285 Comma = ", "; 286 } 287 if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 288 if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 289 if (Alignment) 290 OS << Comma << "Align " << Alignment << " (" << (1u << Alignment) 291 << " bytes)"; 292 293 OS << '\n'; 294 295 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 296 if (!livein_empty()) { 297 if (Indexes) OS << '\t'; 298 OS << " Live Ins:"; 299 for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 300 OS << ' ' << PrintReg(*I, TRI); 301 OS << '\n'; 302 } 303 // Print the preds of this block according to the CFG. 304 if (!pred_empty()) { 305 if (Indexes) OS << '\t'; 306 OS << " Predecessors according to CFG:"; 307 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 308 OS << " BB#" << (*PI)->getNumber(); 309 OS << '\n'; 310 } 311 312 for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) { 313 if (Indexes) { 314 if (Indexes->hasIndex(I)) 315 OS << Indexes->getInstructionIndex(I); 316 OS << '\t'; 317 } 318 OS << '\t'; 319 if (I->isInsideBundle()) 320 OS << " * "; 321 I->print(OS, &getParent()->getTarget()); 322 } 323 324 // Print the successors of this block according to the CFG. 325 if (!succ_empty()) { 326 if (Indexes) OS << '\t'; 327 OS << " Successors according to CFG:"; 328 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) { 329 OS << " BB#" << (*SI)->getNumber(); 330 if (!Weights.empty()) 331 OS << '(' << *getWeightIterator(SI) << ')'; 332 } 333 OS << '\n'; 334 } 335 } 336 337 void MachineBasicBlock::printAsOperand(raw_ostream &OS, bool /*PrintType*/) const { 338 OS << "BB#" << getNumber(); 339 } 340 341 void MachineBasicBlock::removeLiveIn(unsigned Reg) { 342 std::vector<unsigned>::iterator I = 343 std::find(LiveIns.begin(), LiveIns.end(), Reg); 344 if (I != LiveIns.end()) 345 LiveIns.erase(I); 346 } 347 348 bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 349 livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 350 return I != livein_end(); 351 } 352 353 unsigned 354 MachineBasicBlock::addLiveIn(unsigned PhysReg, const TargetRegisterClass *RC) { 355 assert(getParent() && "MBB must be inserted in function"); 356 assert(TargetRegisterInfo::isPhysicalRegister(PhysReg) && "Expected physreg"); 357 assert(RC && "Register class is required"); 358 assert((isLandingPad() || this == &getParent()->front()) && 359 "Only the entry block and landing pads can have physreg live ins"); 360 361 bool LiveIn = isLiveIn(PhysReg); 362 iterator I = SkipPHIsAndLabels(begin()), E = end(); 363 MachineRegisterInfo &MRI = getParent()->getRegInfo(); 364 const TargetInstrInfo &TII = *getParent()->getSubtarget().getInstrInfo(); 365 366 // Look for an existing copy. 367 if (LiveIn) 368 for (;I != E && I->isCopy(); ++I) 369 if (I->getOperand(1).getReg() == PhysReg) { 370 unsigned VirtReg = I->getOperand(0).getReg(); 371 if (!MRI.constrainRegClass(VirtReg, RC)) 372 llvm_unreachable("Incompatible live-in register class."); 373 return VirtReg; 374 } 375 376 // No luck, create a virtual register. 377 unsigned VirtReg = MRI.createVirtualRegister(RC); 378 BuildMI(*this, I, DebugLoc(), TII.get(TargetOpcode::COPY), VirtReg) 379 .addReg(PhysReg, RegState::Kill); 380 if (!LiveIn) 381 addLiveIn(PhysReg); 382 return VirtReg; 383 } 384 385 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 386 getParent()->splice(NewAfter, this); 387 } 388 389 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 390 MachineFunction::iterator BBI = NewBefore; 391 getParent()->splice(++BBI, this); 392 } 393 394 void MachineBasicBlock::updateTerminator() { 395 const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 396 // A block with no successors has no concerns with fall-through edges. 397 if (this->succ_empty()) return; 398 399 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 400 SmallVector<MachineOperand, 4> Cond; 401 DebugLoc dl; // FIXME: this is nowhere 402 bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 403 (void) B; 404 assert(!B && "UpdateTerminators requires analyzable predecessors!"); 405 if (Cond.empty()) { 406 if (TBB) { 407 // The block has an unconditional branch. If its successor is now 408 // its layout successor, delete the branch. 409 if (isLayoutSuccessor(TBB)) 410 TII->RemoveBranch(*this); 411 } else { 412 // The block has an unconditional fallthrough. If its successor is not 413 // its layout successor, insert a branch. First we have to locate the 414 // only non-landing-pad successor, as that is the fallthrough block. 415 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 416 if ((*SI)->isLandingPad()) 417 continue; 418 assert(!TBB && "Found more than one non-landing-pad successor!"); 419 TBB = *SI; 420 } 421 422 // If there is no non-landing-pad successor, the block has no 423 // fall-through edges to be concerned with. 424 if (!TBB) 425 return; 426 427 // Finally update the unconditional successor to be reached via a branch 428 // if it would not be reached by fallthrough. 429 if (!isLayoutSuccessor(TBB)) 430 TII->InsertBranch(*this, TBB, nullptr, Cond, dl); 431 } 432 } else { 433 if (FBB) { 434 // The block has a non-fallthrough conditional branch. If one of its 435 // successors is its layout successor, rewrite it to a fallthrough 436 // conditional branch. 437 if (isLayoutSuccessor(TBB)) { 438 if (TII->ReverseBranchCondition(Cond)) 439 return; 440 TII->RemoveBranch(*this); 441 TII->InsertBranch(*this, FBB, nullptr, Cond, dl); 442 } else if (isLayoutSuccessor(FBB)) { 443 TII->RemoveBranch(*this); 444 TII->InsertBranch(*this, TBB, nullptr, Cond, dl); 445 } 446 } else { 447 // Walk through the successors and find the successor which is not 448 // a landing pad and is not the conditional branch destination (in TBB) 449 // as the fallthrough successor. 450 MachineBasicBlock *FallthroughBB = nullptr; 451 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 452 if ((*SI)->isLandingPad() || *SI == TBB) 453 continue; 454 assert(!FallthroughBB && "Found more than one fallthrough successor."); 455 FallthroughBB = *SI; 456 } 457 if (!FallthroughBB && canFallThrough()) { 458 // We fallthrough to the same basic block as the conditional jump 459 // targets. Remove the conditional jump, leaving unconditional 460 // fallthrough. 461 // FIXME: This does not seem like a reasonable pattern to support, but it 462 // has been seen in the wild coming out of degenerate ARM test cases. 463 TII->RemoveBranch(*this); 464 465 // Finally update the unconditional successor to be reached via a branch 466 // if it would not be reached by fallthrough. 467 if (!isLayoutSuccessor(TBB)) 468 TII->InsertBranch(*this, TBB, nullptr, Cond, dl); 469 return; 470 } 471 472 // The block has a fallthrough conditional branch. 473 if (isLayoutSuccessor(TBB)) { 474 if (TII->ReverseBranchCondition(Cond)) { 475 // We can't reverse the condition, add an unconditional branch. 476 Cond.clear(); 477 TII->InsertBranch(*this, FallthroughBB, nullptr, Cond, dl); 478 return; 479 } 480 TII->RemoveBranch(*this); 481 TII->InsertBranch(*this, FallthroughBB, nullptr, Cond, dl); 482 } else if (!isLayoutSuccessor(FallthroughBB)) { 483 TII->RemoveBranch(*this); 484 TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl); 485 } 486 } 487 } 488 } 489 490 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) { 491 492 // If we see non-zero value for the first time it means we actually use Weight 493 // list, so we fill all Weights with 0's. 494 if (weight != 0 && Weights.empty()) 495 Weights.resize(Successors.size()); 496 497 if (weight != 0 || !Weights.empty()) 498 Weights.push_back(weight); 499 500 Successors.push_back(succ); 501 succ->addPredecessor(this); 502 } 503 504 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 505 succ->removePredecessor(this); 506 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 507 assert(I != Successors.end() && "Not a current successor!"); 508 509 // If Weight list is empty it means we don't use it (disabled optimization). 510 if (!Weights.empty()) { 511 weight_iterator WI = getWeightIterator(I); 512 Weights.erase(WI); 513 } 514 515 Successors.erase(I); 516 } 517 518 MachineBasicBlock::succ_iterator 519 MachineBasicBlock::removeSuccessor(succ_iterator I) { 520 assert(I != Successors.end() && "Not a current successor!"); 521 522 // If Weight list is empty it means we don't use it (disabled optimization). 523 if (!Weights.empty()) { 524 weight_iterator WI = getWeightIterator(I); 525 Weights.erase(WI); 526 } 527 528 (*I)->removePredecessor(this); 529 return Successors.erase(I); 530 } 531 532 void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 533 MachineBasicBlock *New) { 534 if (Old == New) 535 return; 536 537 succ_iterator E = succ_end(); 538 succ_iterator NewI = E; 539 succ_iterator OldI = E; 540 for (succ_iterator I = succ_begin(); I != E; ++I) { 541 if (*I == Old) { 542 OldI = I; 543 if (NewI != E) 544 break; 545 } 546 if (*I == New) { 547 NewI = I; 548 if (OldI != E) 549 break; 550 } 551 } 552 assert(OldI != E && "Old is not a successor of this block"); 553 Old->removePredecessor(this); 554 555 // If New isn't already a successor, let it take Old's place. 556 if (NewI == E) { 557 New->addPredecessor(this); 558 *OldI = New; 559 return; 560 } 561 562 // New is already a successor. 563 // Update its weight instead of adding a duplicate edge. 564 if (!Weights.empty()) { 565 weight_iterator OldWI = getWeightIterator(OldI); 566 *getWeightIterator(NewI) += *OldWI; 567 Weights.erase(OldWI); 568 } 569 Successors.erase(OldI); 570 } 571 572 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 573 Predecessors.push_back(pred); 574 } 575 576 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 577 pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); 578 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 579 Predecessors.erase(I); 580 } 581 582 void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 583 if (this == fromMBB) 584 return; 585 586 while (!fromMBB->succ_empty()) { 587 MachineBasicBlock *Succ = *fromMBB->succ_begin(); 588 uint32_t Weight = 0; 589 590 // If Weight list is empty it means we don't use it (disabled optimization). 591 if (!fromMBB->Weights.empty()) 592 Weight = *fromMBB->Weights.begin(); 593 594 addSuccessor(Succ, Weight); 595 fromMBB->removeSuccessor(Succ); 596 } 597 } 598 599 void 600 MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 601 if (this == fromMBB) 602 return; 603 604 while (!fromMBB->succ_empty()) { 605 MachineBasicBlock *Succ = *fromMBB->succ_begin(); 606 uint32_t Weight = 0; 607 if (!fromMBB->Weights.empty()) 608 Weight = *fromMBB->Weights.begin(); 609 addSuccessor(Succ, Weight); 610 fromMBB->removeSuccessor(Succ); 611 612 // Fix up any PHI nodes in the successor. 613 for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), 614 ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) 615 for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 616 MachineOperand &MO = MI->getOperand(i); 617 if (MO.getMBB() == fromMBB) 618 MO.setMBB(this); 619 } 620 } 621 } 622 623 bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 624 return std::find(pred_begin(), pred_end(), MBB) != pred_end(); 625 } 626 627 bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 628 return std::find(succ_begin(), succ_end(), MBB) != succ_end(); 629 } 630 631 bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 632 MachineFunction::const_iterator I(this); 633 return std::next(I) == MachineFunction::const_iterator(MBB); 634 } 635 636 bool MachineBasicBlock::canFallThrough() { 637 MachineFunction::iterator Fallthrough = this; 638 ++Fallthrough; 639 // If FallthroughBlock is off the end of the function, it can't fall through. 640 if (Fallthrough == getParent()->end()) 641 return false; 642 643 // If FallthroughBlock isn't a successor, no fallthrough is possible. 644 if (!isSuccessor(Fallthrough)) 645 return false; 646 647 // Analyze the branches, if any, at the end of the block. 648 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 649 SmallVector<MachineOperand, 4> Cond; 650 const TargetInstrInfo *TII = getParent()->getSubtarget().getInstrInfo(); 651 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 652 // If we couldn't analyze the branch, examine the last instruction. 653 // If the block doesn't end in a known control barrier, assume fallthrough 654 // is possible. The isPredicated check is needed because this code can be 655 // called during IfConversion, where an instruction which is normally a 656 // Barrier is predicated and thus no longer an actual control barrier. 657 return empty() || !back().isBarrier() || TII->isPredicated(&back()); 658 } 659 660 // If there is no branch, control always falls through. 661 if (!TBB) return true; 662 663 // If there is some explicit branch to the fallthrough block, it can obviously 664 // reach, even though the branch should get folded to fall through implicitly. 665 if (MachineFunction::iterator(TBB) == Fallthrough || 666 MachineFunction::iterator(FBB) == Fallthrough) 667 return true; 668 669 // If it's an unconditional branch to some block not the fall through, it 670 // doesn't fall through. 671 if (Cond.empty()) return false; 672 673 // Otherwise, if it is conditional and has no explicit false block, it falls 674 // through. 675 return FBB == nullptr; 676 } 677 678 MachineBasicBlock * 679 MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 680 // Splitting the critical edge to a landing pad block is non-trivial. Don't do 681 // it in this generic function. 682 if (Succ->isLandingPad()) 683 return nullptr; 684 685 MachineFunction *MF = getParent(); 686 DebugLoc dl; // FIXME: this is nowhere 687 688 // Performance might be harmed on HW that implements branching using exec mask 689 // where both sides of the branches are always executed. 690 if (MF->getTarget().requiresStructuredCFG()) 691 return nullptr; 692 693 // We may need to update this's terminator, but we can't do that if 694 // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 695 const TargetInstrInfo *TII = MF->getSubtarget().getInstrInfo(); 696 MachineBasicBlock *TBB = nullptr, *FBB = nullptr; 697 SmallVector<MachineOperand, 4> Cond; 698 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 699 return nullptr; 700 701 // Avoid bugpoint weirdness: A block may end with a conditional branch but 702 // jumps to the same MBB is either case. We have duplicate CFG edges in that 703 // case that we can't handle. Since this never happens in properly optimized 704 // code, just skip those edges. 705 if (TBB && TBB == FBB) { 706 DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" 707 << getNumber() << '\n'); 708 return nullptr; 709 } 710 711 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 712 MF->insert(std::next(MachineFunction::iterator(this)), NMBB); 713 DEBUG(dbgs() << "Splitting critical edge:" 714 " BB#" << getNumber() 715 << " -- BB#" << NMBB->getNumber() 716 << " -- BB#" << Succ->getNumber() << '\n'); 717 718 LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>(); 719 SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>(); 720 if (LIS) 721 LIS->insertMBBInMaps(NMBB); 722 else if (Indexes) 723 Indexes->insertMBBInMaps(NMBB); 724 725 // On some targets like Mips, branches may kill virtual registers. Make sure 726 // that LiveVariables is properly updated after updateTerminator replaces the 727 // terminators. 728 LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); 729 730 // Collect a list of virtual registers killed by the terminators. 731 SmallVector<unsigned, 4> KilledRegs; 732 if (LV) 733 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 734 I != E; ++I) { 735 MachineInstr *MI = I; 736 for (MachineInstr::mop_iterator OI = MI->operands_begin(), 737 OE = MI->operands_end(); OI != OE; ++OI) { 738 if (!OI->isReg() || OI->getReg() == 0 || 739 !OI->isUse() || !OI->isKill() || OI->isUndef()) 740 continue; 741 unsigned Reg = OI->getReg(); 742 if (TargetRegisterInfo::isPhysicalRegister(Reg) || 743 LV->getVarInfo(Reg).removeKill(MI)) { 744 KilledRegs.push_back(Reg); 745 DEBUG(dbgs() << "Removing terminator kill: " << *MI); 746 OI->setIsKill(false); 747 } 748 } 749 } 750 751 SmallVector<unsigned, 4> UsedRegs; 752 if (LIS) { 753 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 754 I != E; ++I) { 755 MachineInstr *MI = I; 756 757 for (MachineInstr::mop_iterator OI = MI->operands_begin(), 758 OE = MI->operands_end(); OI != OE; ++OI) { 759 if (!OI->isReg() || OI->getReg() == 0) 760 continue; 761 762 unsigned Reg = OI->getReg(); 763 if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end()) 764 UsedRegs.push_back(Reg); 765 } 766 } 767 } 768 769 ReplaceUsesOfBlockWith(Succ, NMBB); 770 771 // If updateTerminator() removes instructions, we need to remove them from 772 // SlotIndexes. 773 SmallVector<MachineInstr*, 4> Terminators; 774 if (Indexes) { 775 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 776 I != E; ++I) 777 Terminators.push_back(I); 778 } 779 780 updateTerminator(); 781 782 if (Indexes) { 783 SmallVector<MachineInstr*, 4> NewTerminators; 784 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 785 I != E; ++I) 786 NewTerminators.push_back(I); 787 788 for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), 789 E = Terminators.end(); I != E; ++I) { 790 if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) == 791 NewTerminators.end()) 792 Indexes->removeMachineInstrFromMaps(*I); 793 } 794 } 795 796 // Insert unconditional "jump Succ" instruction in NMBB if necessary. 797 NMBB->addSuccessor(Succ); 798 if (!NMBB->isLayoutSuccessor(Succ)) { 799 Cond.clear(); 800 MF->getSubtarget().getInstrInfo()->InsertBranch(*NMBB, Succ, nullptr, Cond, 801 dl); 802 803 if (Indexes) { 804 for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end(); 805 I != E; ++I) { 806 // Some instructions may have been moved to NMBB by updateTerminator(), 807 // so we first remove any instruction that already has an index. 808 if (Indexes->hasIndex(I)) 809 Indexes->removeMachineInstrFromMaps(I); 810 Indexes->insertMachineInstrInMaps(I); 811 } 812 } 813 } 814 815 // Fix PHI nodes in Succ so they refer to NMBB instead of this 816 for (MachineBasicBlock::instr_iterator 817 i = Succ->instr_begin(),e = Succ->instr_end(); 818 i != e && i->isPHI(); ++i) 819 for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 820 if (i->getOperand(ni+1).getMBB() == this) 821 i->getOperand(ni+1).setMBB(NMBB); 822 823 // Inherit live-ins from the successor 824 for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), 825 E = Succ->livein_end(); I != E; ++I) 826 NMBB->addLiveIn(*I); 827 828 // Update LiveVariables. 829 const TargetRegisterInfo *TRI = MF->getSubtarget().getRegisterInfo(); 830 if (LV) { 831 // Restore kills of virtual registers that were killed by the terminators. 832 while (!KilledRegs.empty()) { 833 unsigned Reg = KilledRegs.pop_back_val(); 834 for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 835 if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false)) 836 continue; 837 if (TargetRegisterInfo::isVirtualRegister(Reg)) 838 LV->getVarInfo(Reg).Kills.push_back(I); 839 DEBUG(dbgs() << "Restored terminator kill: " << *I); 840 break; 841 } 842 } 843 // Update relevant live-through information. 844 LV->addNewBlock(NMBB, this, Succ); 845 } 846 847 if (LIS) { 848 // After splitting the edge and updating SlotIndexes, live intervals may be 849 // in one of two situations, depending on whether this block was the last in 850 // the function. If the original block was the last in the function, all live 851 // intervals will end prior to the beginning of the new split block. If the 852 // original block was not at the end of the function, all live intervals will 853 // extend to the end of the new split block. 854 855 bool isLastMBB = 856 std::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 857 858 SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 859 SlotIndex PrevIndex = StartIndex.getPrevSlot(); 860 SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 861 862 // Find the registers used from NMBB in PHIs in Succ. 863 SmallSet<unsigned, 8> PHISrcRegs; 864 for (MachineBasicBlock::instr_iterator 865 I = Succ->instr_begin(), E = Succ->instr_end(); 866 I != E && I->isPHI(); ++I) { 867 for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 868 if (I->getOperand(ni+1).getMBB() == NMBB) { 869 MachineOperand &MO = I->getOperand(ni); 870 unsigned Reg = MO.getReg(); 871 PHISrcRegs.insert(Reg); 872 if (MO.isUndef()) 873 continue; 874 875 LiveInterval &LI = LIS->getInterval(Reg); 876 VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 877 assert(VNI && "PHI sources should be live out of their predecessors."); 878 LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 879 } 880 } 881 } 882 883 MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 884 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 885 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 886 if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 887 continue; 888 889 LiveInterval &LI = LIS->getInterval(Reg); 890 if (!LI.liveAt(PrevIndex)) 891 continue; 892 893 bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 894 if (isLiveOut && isLastMBB) { 895 VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 896 assert(VNI && "LiveInterval should have VNInfo where it is live."); 897 LI.addSegment(LiveInterval::Segment(StartIndex, EndIndex, VNI)); 898 } else if (!isLiveOut && !isLastMBB) { 899 LI.removeSegment(StartIndex, EndIndex); 900 } 901 } 902 903 // Update all intervals for registers whose uses may have been modified by 904 // updateTerminator(). 905 LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 906 } 907 908 if (MachineDominatorTree *MDT = 909 P->getAnalysisIfAvailable<MachineDominatorTree>()) { 910 // Update dominator information. 911 MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); 912 913 bool IsNewIDom = true; 914 for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); 915 PI != E; ++PI) { 916 MachineBasicBlock *PredBB = *PI; 917 if (PredBB == NMBB) 918 continue; 919 if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { 920 IsNewIDom = false; 921 break; 922 } 923 } 924 925 // We know "this" dominates the newly created basic block. 926 MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); 927 928 // If all the other predecessors of "Succ" are dominated by "Succ" itself 929 // then the new block is the new immediate dominator of "Succ". Otherwise, 930 // the new block doesn't dominate anything. 931 if (IsNewIDom) 932 MDT->changeImmediateDominator(SucccDTNode, NewDTNode); 933 } 934 935 if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) 936 if (MachineLoop *TIL = MLI->getLoopFor(this)) { 937 // If one or the other blocks were not in a loop, the new block is not 938 // either, and thus LI doesn't need to be updated. 939 if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 940 if (TIL == DestLoop) { 941 // Both in the same loop, the NMBB joins loop. 942 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 943 } else if (TIL->contains(DestLoop)) { 944 // Edge from an outer loop to an inner loop. Add to the outer loop. 945 TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 946 } else if (DestLoop->contains(TIL)) { 947 // Edge from an inner loop to an outer loop. Add to the outer loop. 948 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 949 } else { 950 // Edge from two loops with no containment relation. Because these 951 // are natural loops, we know that the destination block must be the 952 // header of its loop (adding a branch into a loop elsewhere would 953 // create an irreducible loop). 954 assert(DestLoop->getHeader() == Succ && 955 "Should not create irreducible loops!"); 956 if (MachineLoop *P = DestLoop->getParentLoop()) 957 P->addBasicBlockToLoop(NMBB, MLI->getBase()); 958 } 959 } 960 } 961 962 return NMBB; 963 } 964 965 /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 966 /// neighboring instructions so the bundle won't be broken by removing MI. 967 static void unbundleSingleMI(MachineInstr *MI) { 968 // Removing the first instruction in a bundle. 969 if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 970 MI->unbundleFromSucc(); 971 // Removing the last instruction in a bundle. 972 if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 973 MI->unbundleFromPred(); 974 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 975 // are already fine. 976 } 977 978 MachineBasicBlock::instr_iterator 979 MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 980 unbundleSingleMI(I); 981 return Insts.erase(I); 982 } 983 984 MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 985 unbundleSingleMI(MI); 986 MI->clearFlag(MachineInstr::BundledPred); 987 MI->clearFlag(MachineInstr::BundledSucc); 988 return Insts.remove(MI); 989 } 990 991 MachineBasicBlock::instr_iterator 992 MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 993 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 994 "Cannot insert instruction with bundle flags"); 995 // Set the bundle flags when inserting inside a bundle. 996 if (I != instr_end() && I->isBundledWithPred()) { 997 MI->setFlag(MachineInstr::BundledPred); 998 MI->setFlag(MachineInstr::BundledSucc); 999 } 1000 return Insts.insert(I, MI); 1001 } 1002 1003 /// removeFromParent - This method unlinks 'this' from the containing function, 1004 /// and returns it, but does not delete it. 1005 MachineBasicBlock *MachineBasicBlock::removeFromParent() { 1006 assert(getParent() && "Not embedded in a function!"); 1007 getParent()->remove(this); 1008 return this; 1009 } 1010 1011 1012 /// eraseFromParent - This method unlinks 'this' from the containing function, 1013 /// and deletes it. 1014 void MachineBasicBlock::eraseFromParent() { 1015 assert(getParent() && "Not embedded in a function!"); 1016 getParent()->erase(this); 1017 } 1018 1019 1020 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 1021 /// 'Old', change the code and CFG so that it branches to 'New' instead. 1022 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 1023 MachineBasicBlock *New) { 1024 assert(Old != New && "Cannot replace self with self!"); 1025 1026 MachineBasicBlock::instr_iterator I = instr_end(); 1027 while (I != instr_begin()) { 1028 --I; 1029 if (!I->isTerminator()) break; 1030 1031 // Scan the operands of this machine instruction, replacing any uses of Old 1032 // with New. 1033 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 1034 if (I->getOperand(i).isMBB() && 1035 I->getOperand(i).getMBB() == Old) 1036 I->getOperand(i).setMBB(New); 1037 } 1038 1039 // Update the successor information. 1040 replaceSuccessor(Old, New); 1041 } 1042 1043 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 1044 /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 1045 /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 1046 /// null. 1047 /// 1048 /// Besides DestA and DestB, retain other edges leading to LandingPads 1049 /// (currently there can be only one; we don't check or require that here). 1050 /// Note it is possible that DestA and/or DestB are LandingPads. 1051 bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 1052 MachineBasicBlock *DestB, 1053 bool isCond) { 1054 // The values of DestA and DestB frequently come from a call to the 1055 // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 1056 // values from there. 1057 // 1058 // 1. If both DestA and DestB are null, then the block ends with no branches 1059 // (it falls through to its successor). 1060 // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 1061 // with only an unconditional branch. 1062 // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 1063 // with a conditional branch that falls through to a successor (DestB). 1064 // 4. If DestA and DestB is set and isCond is true, then the block ends with a 1065 // conditional branch followed by an unconditional branch. DestA is the 1066 // 'true' destination and DestB is the 'false' destination. 1067 1068 bool Changed = false; 1069 1070 MachineFunction::iterator FallThru = 1071 std::next(MachineFunction::iterator(this)); 1072 1073 if (!DestA && !DestB) { 1074 // Block falls through to successor. 1075 DestA = FallThru; 1076 DestB = FallThru; 1077 } else if (DestA && !DestB) { 1078 if (isCond) 1079 // Block ends in conditional jump that falls through to successor. 1080 DestB = FallThru; 1081 } else { 1082 assert(DestA && DestB && isCond && 1083 "CFG in a bad state. Cannot correct CFG edges"); 1084 } 1085 1086 // Remove superfluous edges. I.e., those which aren't destinations of this 1087 // basic block, duplicate edges, or landing pads. 1088 SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 1089 MachineBasicBlock::succ_iterator SI = succ_begin(); 1090 while (SI != succ_end()) { 1091 const MachineBasicBlock *MBB = *SI; 1092 if (!SeenMBBs.insert(MBB) || 1093 (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 1094 // This is a superfluous edge, remove it. 1095 SI = removeSuccessor(SI); 1096 Changed = true; 1097 } else { 1098 ++SI; 1099 } 1100 } 1101 1102 return Changed; 1103 } 1104 1105 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 1106 /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 1107 DebugLoc 1108 MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 1109 DebugLoc DL; 1110 instr_iterator E = instr_end(); 1111 if (MBBI == E) 1112 return DL; 1113 1114 // Skip debug declarations, we don't want a DebugLoc from them. 1115 while (MBBI != E && MBBI->isDebugValue()) 1116 MBBI++; 1117 if (MBBI != E) 1118 DL = MBBI->getDebugLoc(); 1119 return DL; 1120 } 1121 1122 /// getSuccWeight - Return weight of the edge from this block to MBB. 1123 /// 1124 uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const { 1125 if (Weights.empty()) 1126 return 0; 1127 1128 return *getWeightIterator(Succ); 1129 } 1130 1131 /// Set successor weight of a given iterator. 1132 void MachineBasicBlock::setSuccWeight(succ_iterator I, uint32_t weight) { 1133 if (Weights.empty()) 1134 return; 1135 *getWeightIterator(I) = weight; 1136 } 1137 1138 /// getWeightIterator - Return wight iterator corresonding to the I successor 1139 /// iterator 1140 MachineBasicBlock::weight_iterator MachineBasicBlock:: 1141 getWeightIterator(MachineBasicBlock::succ_iterator I) { 1142 assert(Weights.size() == Successors.size() && "Async weight list!"); 1143 size_t index = std::distance(Successors.begin(), I); 1144 assert(index < Weights.size() && "Not a current successor!"); 1145 return Weights.begin() + index; 1146 } 1147 1148 /// getWeightIterator - Return wight iterator corresonding to the I successor 1149 /// iterator 1150 MachineBasicBlock::const_weight_iterator MachineBasicBlock:: 1151 getWeightIterator(MachineBasicBlock::const_succ_iterator I) const { 1152 assert(Weights.size() == Successors.size() && "Async weight list!"); 1153 const size_t index = std::distance(Successors.begin(), I); 1154 assert(index < Weights.size() && "Not a current successor!"); 1155 return Weights.begin() + index; 1156 } 1157 1158 /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 1159 /// as of just before "MI". 1160 /// 1161 /// Search is localised to a neighborhood of 1162 /// Neighborhood instructions before (searching for defs or kills) and N 1163 /// instructions after (searching just for defs) MI. 1164 MachineBasicBlock::LivenessQueryResult 1165 MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 1166 unsigned Reg, MachineInstr *MI, 1167 unsigned Neighborhood) { 1168 unsigned N = Neighborhood; 1169 MachineBasicBlock *MBB = MI->getParent(); 1170 1171 // Start by searching backwards from MI, looking for kills, reads or defs. 1172 1173 MachineBasicBlock::iterator I(MI); 1174 // If this is the first insn in the block, don't search backwards. 1175 if (I != MBB->begin()) { 1176 do { 1177 --I; 1178 1179 MachineOperandIteratorBase::PhysRegInfo Analysis = 1180 MIOperands(I).analyzePhysReg(Reg, TRI); 1181 1182 if (Analysis.Defines) 1183 // Outputs happen after inputs so they take precedence if both are 1184 // present. 1185 return Analysis.DefinesDead ? LQR_Dead : LQR_Live; 1186 1187 if (Analysis.Kills || Analysis.Clobbers) 1188 // Register killed, so isn't live. 1189 return LQR_Dead; 1190 1191 else if (Analysis.ReadsOverlap) 1192 // Defined or read without a previous kill - live. 1193 return Analysis.Reads ? LQR_Live : LQR_OverlappingLive; 1194 1195 } while (I != MBB->begin() && --N > 0); 1196 } 1197 1198 // Did we get to the start of the block? 1199 if (I == MBB->begin()) { 1200 // If so, the register's state is definitely defined by the live-in state. 1201 for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); 1202 RAI.isValid(); ++RAI) { 1203 if (MBB->isLiveIn(*RAI)) 1204 return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive; 1205 } 1206 1207 return LQR_Dead; 1208 } 1209 1210 N = Neighborhood; 1211 1212 // Try searching forwards from MI, looking for reads or defs. 1213 I = MachineBasicBlock::iterator(MI); 1214 // If this is the last insn in the block, don't search forwards. 1215 if (I != MBB->end()) { 1216 for (++I; I != MBB->end() && N > 0; ++I, --N) { 1217 MachineOperandIteratorBase::PhysRegInfo Analysis = 1218 MIOperands(I).analyzePhysReg(Reg, TRI); 1219 1220 if (Analysis.ReadsOverlap) 1221 // Used, therefore must have been live. 1222 return (Analysis.Reads) ? 1223 LQR_Live : LQR_OverlappingLive; 1224 1225 else if (Analysis.Clobbers || Analysis.Defines) 1226 // Defined (but not read) therefore cannot have been live. 1227 return LQR_Dead; 1228 } 1229 } 1230 1231 // At this point we have no idea of the liveness of the register. 1232 return LQR_Unknown; 1233 } 1234