LLVM API Documentation
00001 //===-- llvm/CodeGen/MachineBasicBlock.cpp ----------------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // Collect the sequence of machine instructions for a basic block. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #include "llvm/CodeGen/MachineBasicBlock.h" 00015 #include "llvm/ADT/SmallPtrSet.h" 00016 #include "llvm/ADT/SmallString.h" 00017 #include "llvm/Assembly/Writer.h" 00018 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00019 #include "llvm/CodeGen/LiveVariables.h" 00020 #include "llvm/CodeGen/MachineDominators.h" 00021 #include "llvm/CodeGen/MachineFunction.h" 00022 #include "llvm/CodeGen/MachineLoopInfo.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/CodeGen/SlotIndexes.h" 00025 #include "llvm/IR/BasicBlock.h" 00026 #include "llvm/IR/DataLayout.h" 00027 #include "llvm/MC/MCAsmInfo.h" 00028 #include "llvm/MC/MCContext.h" 00029 #include "llvm/Support/Debug.h" 00030 #include "llvm/Support/LeakDetector.h" 00031 #include "llvm/Support/raw_ostream.h" 00032 #include "llvm/Target/TargetInstrInfo.h" 00033 #include "llvm/Target/TargetMachine.h" 00034 #include "llvm/Target/TargetRegisterInfo.h" 00035 #include <algorithm> 00036 using namespace llvm; 00037 00038 MachineBasicBlock::MachineBasicBlock(MachineFunction &mf, const BasicBlock *bb) 00039 : BB(bb), Number(-1), xParent(&mf), Alignment(0), IsLandingPad(false), 00040 AddressTaken(false), CachedMCSymbol(NULL) { 00041 Insts.Parent = this; 00042 } 00043 00044 MachineBasicBlock::~MachineBasicBlock() { 00045 LeakDetector::removeGarbageObject(this); 00046 } 00047 00048 /// getSymbol - Return the MCSymbol for this basic block. 00049 /// 00050 MCSymbol *MachineBasicBlock::getSymbol() const { 00051 if (!CachedMCSymbol) { 00052 const MachineFunction *MF = getParent(); 00053 MCContext &Ctx = MF->getContext(); 00054 const char *Prefix = Ctx.getAsmInfo().getPrivateGlobalPrefix(); 00055 CachedMCSymbol = Ctx.GetOrCreateSymbol(Twine(Prefix) + "BB" + 00056 Twine(MF->getFunctionNumber()) + 00057 "_" + Twine(getNumber())); 00058 } 00059 00060 return CachedMCSymbol; 00061 } 00062 00063 00064 raw_ostream &llvm::operator<<(raw_ostream &OS, const MachineBasicBlock &MBB) { 00065 MBB.print(OS); 00066 return OS; 00067 } 00068 00069 /// addNodeToList (MBB) - When an MBB is added to an MF, we need to update the 00070 /// parent pointer of the MBB, the MBB numbering, and any instructions in the 00071 /// MBB to be on the right operand list for registers. 00072 /// 00073 /// MBBs start out as #-1. When a MBB is added to a MachineFunction, it 00074 /// gets the next available unique MBB number. If it is removed from a 00075 /// MachineFunction, it goes back to being #-1. 00076 void ilist_traits<MachineBasicBlock>::addNodeToList(MachineBasicBlock *N) { 00077 MachineFunction &MF = *N->getParent(); 00078 N->Number = MF.addToMBBNumbering(N); 00079 00080 // Make sure the instructions have their operands in the reginfo lists. 00081 MachineRegisterInfo &RegInfo = MF.getRegInfo(); 00082 for (MachineBasicBlock::instr_iterator 00083 I = N->instr_begin(), E = N->instr_end(); I != E; ++I) 00084 I->AddRegOperandsToUseLists(RegInfo); 00085 00086 LeakDetector::removeGarbageObject(N); 00087 } 00088 00089 void ilist_traits<MachineBasicBlock>::removeNodeFromList(MachineBasicBlock *N) { 00090 N->getParent()->removeFromMBBNumbering(N->Number); 00091 N->Number = -1; 00092 LeakDetector::addGarbageObject(N); 00093 } 00094 00095 00096 /// addNodeToList (MI) - When we add an instruction to a basic block 00097 /// list, we update its parent pointer and add its operands from reg use/def 00098 /// lists if appropriate. 00099 void ilist_traits<MachineInstr>::addNodeToList(MachineInstr *N) { 00100 assert(N->getParent() == 0 && "machine instruction already in a basic block"); 00101 N->setParent(Parent); 00102 00103 // Add the instruction's register operands to their corresponding 00104 // use/def lists. 00105 MachineFunction *MF = Parent->getParent(); 00106 N->AddRegOperandsToUseLists(MF->getRegInfo()); 00107 00108 LeakDetector::removeGarbageObject(N); 00109 } 00110 00111 /// removeNodeFromList (MI) - When we remove an instruction from a basic block 00112 /// list, we update its parent pointer and remove its operands from reg use/def 00113 /// lists if appropriate. 00114 void ilist_traits<MachineInstr>::removeNodeFromList(MachineInstr *N) { 00115 assert(N->getParent() != 0 && "machine instruction not in a basic block"); 00116 00117 // Remove from the use/def lists. 00118 if (MachineFunction *MF = N->getParent()->getParent()) 00119 N->RemoveRegOperandsFromUseLists(MF->getRegInfo()); 00120 00121 N->setParent(0); 00122 00123 LeakDetector::addGarbageObject(N); 00124 } 00125 00126 /// transferNodesFromList (MI) - When moving a range of instructions from one 00127 /// MBB list to another, we need to update the parent pointers and the use/def 00128 /// lists. 00129 void ilist_traits<MachineInstr>:: 00130 transferNodesFromList(ilist_traits<MachineInstr> &fromList, 00131 ilist_iterator<MachineInstr> first, 00132 ilist_iterator<MachineInstr> last) { 00133 assert(Parent->getParent() == fromList.Parent->getParent() && 00134 "MachineInstr parent mismatch!"); 00135 00136 // Splice within the same MBB -> no change. 00137 if (Parent == fromList.Parent) return; 00138 00139 // If splicing between two blocks within the same function, just update the 00140 // parent pointers. 00141 for (; first != last; ++first) 00142 first->setParent(Parent); 00143 } 00144 00145 void ilist_traits<MachineInstr>::deleteNode(MachineInstr* MI) { 00146 assert(!MI->getParent() && "MI is still in a block!"); 00147 Parent->getParent()->DeleteMachineInstr(MI); 00148 } 00149 00150 MachineBasicBlock::iterator MachineBasicBlock::getFirstNonPHI() { 00151 instr_iterator I = instr_begin(), E = instr_end(); 00152 while (I != E && I->isPHI()) 00153 ++I; 00154 assert((I == E || !I->isInsideBundle()) && 00155 "First non-phi MI cannot be inside a bundle!"); 00156 return I; 00157 } 00158 00159 MachineBasicBlock::iterator 00160 MachineBasicBlock::SkipPHIsAndLabels(MachineBasicBlock::iterator I) { 00161 iterator E = end(); 00162 while (I != E && (I->isPHI() || I->isLabel() || I->isDebugValue())) 00163 ++I; 00164 // FIXME: This needs to change if we wish to bundle labels / dbg_values 00165 // inside the bundle. 00166 assert((I == E || !I->isInsideBundle()) && 00167 "First non-phi / non-label instruction is inside a bundle!"); 00168 return I; 00169 } 00170 00171 MachineBasicBlock::iterator MachineBasicBlock::getFirstTerminator() { 00172 iterator B = begin(), E = end(), I = E; 00173 while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 00174 ; /*noop */ 00175 while (I != E && !I->isTerminator()) 00176 ++I; 00177 return I; 00178 } 00179 00180 MachineBasicBlock::const_iterator 00181 MachineBasicBlock::getFirstTerminator() const { 00182 const_iterator B = begin(), E = end(), I = E; 00183 while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 00184 ; /*noop */ 00185 while (I != E && !I->isTerminator()) 00186 ++I; 00187 return I; 00188 } 00189 00190 MachineBasicBlock::instr_iterator MachineBasicBlock::getFirstInstrTerminator() { 00191 instr_iterator B = instr_begin(), E = instr_end(), I = E; 00192 while (I != B && ((--I)->isTerminator() || I->isDebugValue())) 00193 ; /*noop */ 00194 while (I != E && !I->isTerminator()) 00195 ++I; 00196 return I; 00197 } 00198 00199 MachineBasicBlock::iterator MachineBasicBlock::getLastNonDebugInstr() { 00200 // Skip over end-of-block dbg_value instructions. 00201 instr_iterator B = instr_begin(), I = instr_end(); 00202 while (I != B) { 00203 --I; 00204 // Return instruction that starts a bundle. 00205 if (I->isDebugValue() || I->isInsideBundle()) 00206 continue; 00207 return I; 00208 } 00209 // The block is all debug values. 00210 return end(); 00211 } 00212 00213 MachineBasicBlock::const_iterator 00214 MachineBasicBlock::getLastNonDebugInstr() const { 00215 // Skip over end-of-block dbg_value instructions. 00216 const_instr_iterator B = instr_begin(), I = instr_end(); 00217 while (I != B) { 00218 --I; 00219 // Return instruction that starts a bundle. 00220 if (I->isDebugValue() || I->isInsideBundle()) 00221 continue; 00222 return I; 00223 } 00224 // The block is all debug values. 00225 return end(); 00226 } 00227 00228 const MachineBasicBlock *MachineBasicBlock::getLandingPadSuccessor() const { 00229 // A block with a landing pad successor only has one other successor. 00230 if (succ_size() > 2) 00231 return 0; 00232 for (const_succ_iterator I = succ_begin(), E = succ_end(); I != E; ++I) 00233 if ((*I)->isLandingPad()) 00234 return *I; 00235 return 0; 00236 } 00237 00238 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 00239 void MachineBasicBlock::dump() const { 00240 print(dbgs()); 00241 } 00242 #endif 00243 00244 StringRef MachineBasicBlock::getName() const { 00245 if (const BasicBlock *LBB = getBasicBlock()) 00246 return LBB->getName(); 00247 else 00248 return "(null)"; 00249 } 00250 00251 /// Return a hopefully unique identifier for this block. 00252 std::string MachineBasicBlock::getFullName() const { 00253 std::string Name; 00254 if (getParent()) 00255 Name = (getParent()->getName() + ":").str(); 00256 if (getBasicBlock()) 00257 Name += getBasicBlock()->getName(); 00258 else 00259 Name += (Twine("BB") + Twine(getNumber())).str(); 00260 return Name; 00261 } 00262 00263 void MachineBasicBlock::print(raw_ostream &OS, SlotIndexes *Indexes) const { 00264 const MachineFunction *MF = getParent(); 00265 if (!MF) { 00266 OS << "Can't print out MachineBasicBlock because parent MachineFunction" 00267 << " is null\n"; 00268 return; 00269 } 00270 00271 if (Indexes) 00272 OS << Indexes->getMBBStartIdx(this) << '\t'; 00273 00274 OS << "BB#" << getNumber() << ": "; 00275 00276 const char *Comma = ""; 00277 if (const BasicBlock *LBB = getBasicBlock()) { 00278 OS << Comma << "derived from LLVM BB "; 00279 WriteAsOperand(OS, LBB, /*PrintType=*/false); 00280 Comma = ", "; 00281 } 00282 if (isLandingPad()) { OS << Comma << "EH LANDING PAD"; Comma = ", "; } 00283 if (hasAddressTaken()) { OS << Comma << "ADDRESS TAKEN"; Comma = ", "; } 00284 if (Alignment) 00285 OS << Comma << "Align " << Alignment << " (" << (1u << Alignment) 00286 << " bytes)"; 00287 00288 OS << '\n'; 00289 00290 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 00291 if (!livein_empty()) { 00292 if (Indexes) OS << '\t'; 00293 OS << " Live Ins:"; 00294 for (livein_iterator I = livein_begin(),E = livein_end(); I != E; ++I) 00295 OS << ' ' << PrintReg(*I, TRI); 00296 OS << '\n'; 00297 } 00298 // Print the preds of this block according to the CFG. 00299 if (!pred_empty()) { 00300 if (Indexes) OS << '\t'; 00301 OS << " Predecessors according to CFG:"; 00302 for (const_pred_iterator PI = pred_begin(), E = pred_end(); PI != E; ++PI) 00303 OS << " BB#" << (*PI)->getNumber(); 00304 OS << '\n'; 00305 } 00306 00307 for (const_instr_iterator I = instr_begin(); I != instr_end(); ++I) { 00308 if (Indexes) { 00309 if (Indexes->hasIndex(I)) 00310 OS << Indexes->getInstructionIndex(I); 00311 OS << '\t'; 00312 } 00313 OS << '\t'; 00314 if (I->isInsideBundle()) 00315 OS << " * "; 00316 I->print(OS, &getParent()->getTarget()); 00317 } 00318 00319 // Print the successors of this block according to the CFG. 00320 if (!succ_empty()) { 00321 if (Indexes) OS << '\t'; 00322 OS << " Successors according to CFG:"; 00323 for (const_succ_iterator SI = succ_begin(), E = succ_end(); SI != E; ++SI) { 00324 OS << " BB#" << (*SI)->getNumber(); 00325 if (!Weights.empty()) 00326 OS << '(' << *getWeightIterator(SI) << ')'; 00327 } 00328 OS << '\n'; 00329 } 00330 } 00331 00332 void MachineBasicBlock::removeLiveIn(unsigned Reg) { 00333 std::vector<unsigned>::iterator I = 00334 std::find(LiveIns.begin(), LiveIns.end(), Reg); 00335 if (I != LiveIns.end()) 00336 LiveIns.erase(I); 00337 } 00338 00339 bool MachineBasicBlock::isLiveIn(unsigned Reg) const { 00340 livein_iterator I = std::find(livein_begin(), livein_end(), Reg); 00341 return I != livein_end(); 00342 } 00343 00344 void MachineBasicBlock::moveBefore(MachineBasicBlock *NewAfter) { 00345 getParent()->splice(NewAfter, this); 00346 } 00347 00348 void MachineBasicBlock::moveAfter(MachineBasicBlock *NewBefore) { 00349 MachineFunction::iterator BBI = NewBefore; 00350 getParent()->splice(++BBI, this); 00351 } 00352 00353 void MachineBasicBlock::updateTerminator() { 00354 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 00355 // A block with no successors has no concerns with fall-through edges. 00356 if (this->succ_empty()) return; 00357 00358 MachineBasicBlock *TBB = 0, *FBB = 0; 00359 SmallVector<MachineOperand, 4> Cond; 00360 DebugLoc dl; // FIXME: this is nowhere 00361 bool B = TII->AnalyzeBranch(*this, TBB, FBB, Cond); 00362 (void) B; 00363 assert(!B && "UpdateTerminators requires analyzable predecessors!"); 00364 if (Cond.empty()) { 00365 if (TBB) { 00366 // The block has an unconditional branch. If its successor is now 00367 // its layout successor, delete the branch. 00368 if (isLayoutSuccessor(TBB)) 00369 TII->RemoveBranch(*this); 00370 } else { 00371 // The block has an unconditional fallthrough. If its successor is not 00372 // its layout successor, insert a branch. First we have to locate the 00373 // only non-landing-pad successor, as that is the fallthrough block. 00374 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 00375 if ((*SI)->isLandingPad()) 00376 continue; 00377 assert(!TBB && "Found more than one non-landing-pad successor!"); 00378 TBB = *SI; 00379 } 00380 00381 // If there is no non-landing-pad successor, the block has no 00382 // fall-through edges to be concerned with. 00383 if (!TBB) 00384 return; 00385 00386 // Finally update the unconditional successor to be reached via a branch 00387 // if it would not be reached by fallthrough. 00388 if (!isLayoutSuccessor(TBB)) 00389 TII->InsertBranch(*this, TBB, 0, Cond, dl); 00390 } 00391 } else { 00392 if (FBB) { 00393 // The block has a non-fallthrough conditional branch. If one of its 00394 // successors is its layout successor, rewrite it to a fallthrough 00395 // conditional branch. 00396 if (isLayoutSuccessor(TBB)) { 00397 if (TII->ReverseBranchCondition(Cond)) 00398 return; 00399 TII->RemoveBranch(*this); 00400 TII->InsertBranch(*this, FBB, 0, Cond, dl); 00401 } else if (isLayoutSuccessor(FBB)) { 00402 TII->RemoveBranch(*this); 00403 TII->InsertBranch(*this, TBB, 0, Cond, dl); 00404 } 00405 } else { 00406 // Walk through the successors and find the successor which is not 00407 // a landing pad and is not the conditional branch destination (in TBB) 00408 // as the fallthrough successor. 00409 MachineBasicBlock *FallthroughBB = 0; 00410 for (succ_iterator SI = succ_begin(), SE = succ_end(); SI != SE; ++SI) { 00411 if ((*SI)->isLandingPad() || *SI == TBB) 00412 continue; 00413 assert(!FallthroughBB && "Found more than one fallthrough successor."); 00414 FallthroughBB = *SI; 00415 } 00416 if (!FallthroughBB && canFallThrough()) { 00417 // We fallthrough to the same basic block as the conditional jump 00418 // targets. Remove the conditional jump, leaving unconditional 00419 // fallthrough. 00420 // FIXME: This does not seem like a reasonable pattern to support, but it 00421 // has been seen in the wild coming out of degenerate ARM test cases. 00422 TII->RemoveBranch(*this); 00423 00424 // Finally update the unconditional successor to be reached via a branch 00425 // if it would not be reached by fallthrough. 00426 if (!isLayoutSuccessor(TBB)) 00427 TII->InsertBranch(*this, TBB, 0, Cond, dl); 00428 return; 00429 } 00430 00431 // The block has a fallthrough conditional branch. 00432 if (isLayoutSuccessor(TBB)) { 00433 if (TII->ReverseBranchCondition(Cond)) { 00434 // We can't reverse the condition, add an unconditional branch. 00435 Cond.clear(); 00436 TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); 00437 return; 00438 } 00439 TII->RemoveBranch(*this); 00440 TII->InsertBranch(*this, FallthroughBB, 0, Cond, dl); 00441 } else if (!isLayoutSuccessor(FallthroughBB)) { 00442 TII->RemoveBranch(*this); 00443 TII->InsertBranch(*this, TBB, FallthroughBB, Cond, dl); 00444 } 00445 } 00446 } 00447 } 00448 00449 void MachineBasicBlock::addSuccessor(MachineBasicBlock *succ, uint32_t weight) { 00450 00451 // If we see non-zero value for the first time it means we actually use Weight 00452 // list, so we fill all Weights with 0's. 00453 if (weight != 0 && Weights.empty()) 00454 Weights.resize(Successors.size()); 00455 00456 if (weight != 0 || !Weights.empty()) 00457 Weights.push_back(weight); 00458 00459 Successors.push_back(succ); 00460 succ->addPredecessor(this); 00461 } 00462 00463 void MachineBasicBlock::removeSuccessor(MachineBasicBlock *succ) { 00464 succ->removePredecessor(this); 00465 succ_iterator I = std::find(Successors.begin(), Successors.end(), succ); 00466 assert(I != Successors.end() && "Not a current successor!"); 00467 00468 // If Weight list is empty it means we don't use it (disabled optimization). 00469 if (!Weights.empty()) { 00470 weight_iterator WI = getWeightIterator(I); 00471 Weights.erase(WI); 00472 } 00473 00474 Successors.erase(I); 00475 } 00476 00477 MachineBasicBlock::succ_iterator 00478 MachineBasicBlock::removeSuccessor(succ_iterator I) { 00479 assert(I != Successors.end() && "Not a current successor!"); 00480 00481 // If Weight list is empty it means we don't use it (disabled optimization). 00482 if (!Weights.empty()) { 00483 weight_iterator WI = getWeightIterator(I); 00484 Weights.erase(WI); 00485 } 00486 00487 (*I)->removePredecessor(this); 00488 return Successors.erase(I); 00489 } 00490 00491 void MachineBasicBlock::replaceSuccessor(MachineBasicBlock *Old, 00492 MachineBasicBlock *New) { 00493 if (Old == New) 00494 return; 00495 00496 succ_iterator E = succ_end(); 00497 succ_iterator NewI = E; 00498 succ_iterator OldI = E; 00499 for (succ_iterator I = succ_begin(); I != E; ++I) { 00500 if (*I == Old) { 00501 OldI = I; 00502 if (NewI != E) 00503 break; 00504 } 00505 if (*I == New) { 00506 NewI = I; 00507 if (OldI != E) 00508 break; 00509 } 00510 } 00511 assert(OldI != E && "Old is not a successor of this block"); 00512 Old->removePredecessor(this); 00513 00514 // If New isn't already a successor, let it take Old's place. 00515 if (NewI == E) { 00516 New->addPredecessor(this); 00517 *OldI = New; 00518 return; 00519 } 00520 00521 // New is already a successor. 00522 // Update its weight instead of adding a duplicate edge. 00523 if (!Weights.empty()) { 00524 weight_iterator OldWI = getWeightIterator(OldI); 00525 *getWeightIterator(NewI) += *OldWI; 00526 Weights.erase(OldWI); 00527 } 00528 Successors.erase(OldI); 00529 } 00530 00531 void MachineBasicBlock::addPredecessor(MachineBasicBlock *pred) { 00532 Predecessors.push_back(pred); 00533 } 00534 00535 void MachineBasicBlock::removePredecessor(MachineBasicBlock *pred) { 00536 pred_iterator I = std::find(Predecessors.begin(), Predecessors.end(), pred); 00537 assert(I != Predecessors.end() && "Pred is not a predecessor of this block!"); 00538 Predecessors.erase(I); 00539 } 00540 00541 void MachineBasicBlock::transferSuccessors(MachineBasicBlock *fromMBB) { 00542 if (this == fromMBB) 00543 return; 00544 00545 while (!fromMBB->succ_empty()) { 00546 MachineBasicBlock *Succ = *fromMBB->succ_begin(); 00547 uint32_t Weight = 0; 00548 00549 // If Weight list is empty it means we don't use it (disabled optimization). 00550 if (!fromMBB->Weights.empty()) 00551 Weight = *fromMBB->Weights.begin(); 00552 00553 addSuccessor(Succ, Weight); 00554 fromMBB->removeSuccessor(Succ); 00555 } 00556 } 00557 00558 void 00559 MachineBasicBlock::transferSuccessorsAndUpdatePHIs(MachineBasicBlock *fromMBB) { 00560 if (this == fromMBB) 00561 return; 00562 00563 while (!fromMBB->succ_empty()) { 00564 MachineBasicBlock *Succ = *fromMBB->succ_begin(); 00565 uint32_t Weight = 0; 00566 if (!fromMBB->Weights.empty()) 00567 Weight = *fromMBB->Weights.begin(); 00568 addSuccessor(Succ, Weight); 00569 fromMBB->removeSuccessor(Succ); 00570 00571 // Fix up any PHI nodes in the successor. 00572 for (MachineBasicBlock::instr_iterator MI = Succ->instr_begin(), 00573 ME = Succ->instr_end(); MI != ME && MI->isPHI(); ++MI) 00574 for (unsigned i = 2, e = MI->getNumOperands()+1; i != e; i += 2) { 00575 MachineOperand &MO = MI->getOperand(i); 00576 if (MO.getMBB() == fromMBB) 00577 MO.setMBB(this); 00578 } 00579 } 00580 } 00581 00582 bool MachineBasicBlock::isPredecessor(const MachineBasicBlock *MBB) const { 00583 return std::find(pred_begin(), pred_end(), MBB) != pred_end(); 00584 } 00585 00586 bool MachineBasicBlock::isSuccessor(const MachineBasicBlock *MBB) const { 00587 return std::find(succ_begin(), succ_end(), MBB) != succ_end(); 00588 } 00589 00590 bool MachineBasicBlock::isLayoutSuccessor(const MachineBasicBlock *MBB) const { 00591 MachineFunction::const_iterator I(this); 00592 return llvm::next(I) == MachineFunction::const_iterator(MBB); 00593 } 00594 00595 bool MachineBasicBlock::canFallThrough() { 00596 MachineFunction::iterator Fallthrough = this; 00597 ++Fallthrough; 00598 // If FallthroughBlock is off the end of the function, it can't fall through. 00599 if (Fallthrough == getParent()->end()) 00600 return false; 00601 00602 // If FallthroughBlock isn't a successor, no fallthrough is possible. 00603 if (!isSuccessor(Fallthrough)) 00604 return false; 00605 00606 // Analyze the branches, if any, at the end of the block. 00607 MachineBasicBlock *TBB = 0, *FBB = 0; 00608 SmallVector<MachineOperand, 4> Cond; 00609 const TargetInstrInfo *TII = getParent()->getTarget().getInstrInfo(); 00610 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) { 00611 // If we couldn't analyze the branch, examine the last instruction. 00612 // If the block doesn't end in a known control barrier, assume fallthrough 00613 // is possible. The isPredicated check is needed because this code can be 00614 // called during IfConversion, where an instruction which is normally a 00615 // Barrier is predicated and thus no longer an actual control barrier. 00616 return empty() || !back().isBarrier() || TII->isPredicated(&back()); 00617 } 00618 00619 // If there is no branch, control always falls through. 00620 if (TBB == 0) return true; 00621 00622 // If there is some explicit branch to the fallthrough block, it can obviously 00623 // reach, even though the branch should get folded to fall through implicitly. 00624 if (MachineFunction::iterator(TBB) == Fallthrough || 00625 MachineFunction::iterator(FBB) == Fallthrough) 00626 return true; 00627 00628 // If it's an unconditional branch to some block not the fall through, it 00629 // doesn't fall through. 00630 if (Cond.empty()) return false; 00631 00632 // Otherwise, if it is conditional and has no explicit false block, it falls 00633 // through. 00634 return FBB == 0; 00635 } 00636 00637 MachineBasicBlock * 00638 MachineBasicBlock::SplitCriticalEdge(MachineBasicBlock *Succ, Pass *P) { 00639 // Splitting the critical edge to a landing pad block is non-trivial. Don't do 00640 // it in this generic function. 00641 if (Succ->isLandingPad()) 00642 return NULL; 00643 00644 MachineFunction *MF = getParent(); 00645 DebugLoc dl; // FIXME: this is nowhere 00646 00647 // We may need to update this's terminator, but we can't do that if 00648 // AnalyzeBranch fails. If this uses a jump table, we won't touch it. 00649 const TargetInstrInfo *TII = MF->getTarget().getInstrInfo(); 00650 MachineBasicBlock *TBB = 0, *FBB = 0; 00651 SmallVector<MachineOperand, 4> Cond; 00652 if (TII->AnalyzeBranch(*this, TBB, FBB, Cond)) 00653 return NULL; 00654 00655 // Avoid bugpoint weirdness: A block may end with a conditional branch but 00656 // jumps to the same MBB is either case. We have duplicate CFG edges in that 00657 // case that we can't handle. Since this never happens in properly optimized 00658 // code, just skip those edges. 00659 if (TBB && TBB == FBB) { 00660 DEBUG(dbgs() << "Won't split critical edge after degenerate BB#" 00661 << getNumber() << '\n'); 00662 return NULL; 00663 } 00664 00665 MachineBasicBlock *NMBB = MF->CreateMachineBasicBlock(); 00666 MF->insert(llvm::next(MachineFunction::iterator(this)), NMBB); 00667 DEBUG(dbgs() << "Splitting critical edge:" 00668 " BB#" << getNumber() 00669 << " -- BB#" << NMBB->getNumber() 00670 << " -- BB#" << Succ->getNumber() << '\n'); 00671 00672 LiveIntervals *LIS = P->getAnalysisIfAvailable<LiveIntervals>(); 00673 SlotIndexes *Indexes = P->getAnalysisIfAvailable<SlotIndexes>(); 00674 if (LIS) 00675 LIS->insertMBBInMaps(NMBB); 00676 else if (Indexes) 00677 Indexes->insertMBBInMaps(NMBB); 00678 00679 // On some targets like Mips, branches may kill virtual registers. Make sure 00680 // that LiveVariables is properly updated after updateTerminator replaces the 00681 // terminators. 00682 LiveVariables *LV = P->getAnalysisIfAvailable<LiveVariables>(); 00683 00684 // Collect a list of virtual registers killed by the terminators. 00685 SmallVector<unsigned, 4> KilledRegs; 00686 if (LV) 00687 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 00688 I != E; ++I) { 00689 MachineInstr *MI = I; 00690 for (MachineInstr::mop_iterator OI = MI->operands_begin(), 00691 OE = MI->operands_end(); OI != OE; ++OI) { 00692 if (!OI->isReg() || OI->getReg() == 0 || 00693 !OI->isUse() || !OI->isKill() || OI->isUndef()) 00694 continue; 00695 unsigned Reg = OI->getReg(); 00696 if (TargetRegisterInfo::isPhysicalRegister(Reg) || 00697 LV->getVarInfo(Reg).removeKill(MI)) { 00698 KilledRegs.push_back(Reg); 00699 DEBUG(dbgs() << "Removing terminator kill: " << *MI); 00700 OI->setIsKill(false); 00701 } 00702 } 00703 } 00704 00705 SmallVector<unsigned, 4> UsedRegs; 00706 if (LIS) { 00707 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 00708 I != E; ++I) { 00709 MachineInstr *MI = I; 00710 00711 for (MachineInstr::mop_iterator OI = MI->operands_begin(), 00712 OE = MI->operands_end(); OI != OE; ++OI) { 00713 if (!OI->isReg() || OI->getReg() == 0) 00714 continue; 00715 00716 unsigned Reg = OI->getReg(); 00717 if (std::find(UsedRegs.begin(), UsedRegs.end(), Reg) == UsedRegs.end()) 00718 UsedRegs.push_back(Reg); 00719 } 00720 } 00721 } 00722 00723 ReplaceUsesOfBlockWith(Succ, NMBB); 00724 00725 // If updateTerminator() removes instructions, we need to remove them from 00726 // SlotIndexes. 00727 SmallVector<MachineInstr*, 4> Terminators; 00728 if (Indexes) { 00729 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 00730 I != E; ++I) 00731 Terminators.push_back(I); 00732 } 00733 00734 updateTerminator(); 00735 00736 if (Indexes) { 00737 SmallVector<MachineInstr*, 4> NewTerminators; 00738 for (instr_iterator I = getFirstInstrTerminator(), E = instr_end(); 00739 I != E; ++I) 00740 NewTerminators.push_back(I); 00741 00742 for (SmallVectorImpl<MachineInstr*>::iterator I = Terminators.begin(), 00743 E = Terminators.end(); I != E; ++I) { 00744 if (std::find(NewTerminators.begin(), NewTerminators.end(), *I) == 00745 NewTerminators.end()) 00746 Indexes->removeMachineInstrFromMaps(*I); 00747 } 00748 } 00749 00750 // Insert unconditional "jump Succ" instruction in NMBB if necessary. 00751 NMBB->addSuccessor(Succ); 00752 if (!NMBB->isLayoutSuccessor(Succ)) { 00753 Cond.clear(); 00754 MF->getTarget().getInstrInfo()->InsertBranch(*NMBB, Succ, NULL, Cond, dl); 00755 00756 if (Indexes) { 00757 for (instr_iterator I = NMBB->instr_begin(), E = NMBB->instr_end(); 00758 I != E; ++I) { 00759 // Some instructions may have been moved to NMBB by updateTerminator(), 00760 // so we first remove any instruction that already has an index. 00761 if (Indexes->hasIndex(I)) 00762 Indexes->removeMachineInstrFromMaps(I); 00763 Indexes->insertMachineInstrInMaps(I); 00764 } 00765 } 00766 } 00767 00768 // Fix PHI nodes in Succ so they refer to NMBB instead of this 00769 for (MachineBasicBlock::instr_iterator 00770 i = Succ->instr_begin(),e = Succ->instr_end(); 00771 i != e && i->isPHI(); ++i) 00772 for (unsigned ni = 1, ne = i->getNumOperands(); ni != ne; ni += 2) 00773 if (i->getOperand(ni+1).getMBB() == this) 00774 i->getOperand(ni+1).setMBB(NMBB); 00775 00776 // Inherit live-ins from the successor 00777 for (MachineBasicBlock::livein_iterator I = Succ->livein_begin(), 00778 E = Succ->livein_end(); I != E; ++I) 00779 NMBB->addLiveIn(*I); 00780 00781 // Update LiveVariables. 00782 const TargetRegisterInfo *TRI = MF->getTarget().getRegisterInfo(); 00783 if (LV) { 00784 // Restore kills of virtual registers that were killed by the terminators. 00785 while (!KilledRegs.empty()) { 00786 unsigned Reg = KilledRegs.pop_back_val(); 00787 for (instr_iterator I = instr_end(), E = instr_begin(); I != E;) { 00788 if (!(--I)->addRegisterKilled(Reg, TRI, /* addIfNotFound= */ false)) 00789 continue; 00790 if (TargetRegisterInfo::isVirtualRegister(Reg)) 00791 LV->getVarInfo(Reg).Kills.push_back(I); 00792 DEBUG(dbgs() << "Restored terminator kill: " << *I); 00793 break; 00794 } 00795 } 00796 // Update relevant live-through information. 00797 LV->addNewBlock(NMBB, this, Succ); 00798 } 00799 00800 if (LIS) { 00801 // After splitting the edge and updating SlotIndexes, live intervals may be 00802 // in one of two situations, depending on whether this block was the last in 00803 // the function. If the original block was the last in the function, all live 00804 // intervals will end prior to the beginning of the new split block. If the 00805 // original block was not at the end of the function, all live intervals will 00806 // extend to the end of the new split block. 00807 00808 bool isLastMBB = 00809 llvm::next(MachineFunction::iterator(NMBB)) == getParent()->end(); 00810 00811 SlotIndex StartIndex = Indexes->getMBBEndIdx(this); 00812 SlotIndex PrevIndex = StartIndex.getPrevSlot(); 00813 SlotIndex EndIndex = Indexes->getMBBEndIdx(NMBB); 00814 00815 // Find the registers used from NMBB in PHIs in Succ. 00816 SmallSet<unsigned, 8> PHISrcRegs; 00817 for (MachineBasicBlock::instr_iterator 00818 I = Succ->instr_begin(), E = Succ->instr_end(); 00819 I != E && I->isPHI(); ++I) { 00820 for (unsigned ni = 1, ne = I->getNumOperands(); ni != ne; ni += 2) { 00821 if (I->getOperand(ni+1).getMBB() == NMBB) { 00822 MachineOperand &MO = I->getOperand(ni); 00823 unsigned Reg = MO.getReg(); 00824 PHISrcRegs.insert(Reg); 00825 if (MO.isUndef()) 00826 continue; 00827 00828 LiveInterval &LI = LIS->getInterval(Reg); 00829 VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 00830 assert(VNI && "PHI sources should be live out of their predecessors."); 00831 LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); 00832 } 00833 } 00834 } 00835 00836 MachineRegisterInfo *MRI = &getParent()->getRegInfo(); 00837 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 00838 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 00839 if (PHISrcRegs.count(Reg) || !LIS->hasInterval(Reg)) 00840 continue; 00841 00842 LiveInterval &LI = LIS->getInterval(Reg); 00843 if (!LI.liveAt(PrevIndex)) 00844 continue; 00845 00846 bool isLiveOut = LI.liveAt(LIS->getMBBStartIdx(Succ)); 00847 if (isLiveOut && isLastMBB) { 00848 VNInfo *VNI = LI.getVNInfoAt(PrevIndex); 00849 assert(VNI && "LiveInterval should have VNInfo where it is live."); 00850 LI.addRange(LiveRange(StartIndex, EndIndex, VNI)); 00851 } else if (!isLiveOut && !isLastMBB) { 00852 LI.removeRange(StartIndex, EndIndex); 00853 } 00854 } 00855 00856 // Update all intervals for registers whose uses may have been modified by 00857 // updateTerminator(). 00858 LIS->repairIntervalsInRange(this, getFirstTerminator(), end(), UsedRegs); 00859 } 00860 00861 if (MachineDominatorTree *MDT = 00862 P->getAnalysisIfAvailable<MachineDominatorTree>()) { 00863 // Update dominator information. 00864 MachineDomTreeNode *SucccDTNode = MDT->getNode(Succ); 00865 00866 bool IsNewIDom = true; 00867 for (const_pred_iterator PI = Succ->pred_begin(), E = Succ->pred_end(); 00868 PI != E; ++PI) { 00869 MachineBasicBlock *PredBB = *PI; 00870 if (PredBB == NMBB) 00871 continue; 00872 if (!MDT->dominates(SucccDTNode, MDT->getNode(PredBB))) { 00873 IsNewIDom = false; 00874 break; 00875 } 00876 } 00877 00878 // We know "this" dominates the newly created basic block. 00879 MachineDomTreeNode *NewDTNode = MDT->addNewBlock(NMBB, this); 00880 00881 // If all the other predecessors of "Succ" are dominated by "Succ" itself 00882 // then the new block is the new immediate dominator of "Succ". Otherwise, 00883 // the new block doesn't dominate anything. 00884 if (IsNewIDom) 00885 MDT->changeImmediateDominator(SucccDTNode, NewDTNode); 00886 } 00887 00888 if (MachineLoopInfo *MLI = P->getAnalysisIfAvailable<MachineLoopInfo>()) 00889 if (MachineLoop *TIL = MLI->getLoopFor(this)) { 00890 // If one or the other blocks were not in a loop, the new block is not 00891 // either, and thus LI doesn't need to be updated. 00892 if (MachineLoop *DestLoop = MLI->getLoopFor(Succ)) { 00893 if (TIL == DestLoop) { 00894 // Both in the same loop, the NMBB joins loop. 00895 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 00896 } else if (TIL->contains(DestLoop)) { 00897 // Edge from an outer loop to an inner loop. Add to the outer loop. 00898 TIL->addBasicBlockToLoop(NMBB, MLI->getBase()); 00899 } else if (DestLoop->contains(TIL)) { 00900 // Edge from an inner loop to an outer loop. Add to the outer loop. 00901 DestLoop->addBasicBlockToLoop(NMBB, MLI->getBase()); 00902 } else { 00903 // Edge from two loops with no containment relation. Because these 00904 // are natural loops, we know that the destination block must be the 00905 // header of its loop (adding a branch into a loop elsewhere would 00906 // create an irreducible loop). 00907 assert(DestLoop->getHeader() == Succ && 00908 "Should not create irreducible loops!"); 00909 if (MachineLoop *P = DestLoop->getParentLoop()) 00910 P->addBasicBlockToLoop(NMBB, MLI->getBase()); 00911 } 00912 } 00913 } 00914 00915 return NMBB; 00916 } 00917 00918 /// Prepare MI to be removed from its bundle. This fixes bundle flags on MI's 00919 /// neighboring instructions so the bundle won't be broken by removing MI. 00920 static void unbundleSingleMI(MachineInstr *MI) { 00921 // Removing the first instruction in a bundle. 00922 if (MI->isBundledWithSucc() && !MI->isBundledWithPred()) 00923 MI->unbundleFromSucc(); 00924 // Removing the last instruction in a bundle. 00925 if (MI->isBundledWithPred() && !MI->isBundledWithSucc()) 00926 MI->unbundleFromPred(); 00927 // If MI is not bundled, or if it is internal to a bundle, the neighbor flags 00928 // are already fine. 00929 } 00930 00931 MachineBasicBlock::instr_iterator 00932 MachineBasicBlock::erase(MachineBasicBlock::instr_iterator I) { 00933 unbundleSingleMI(I); 00934 return Insts.erase(I); 00935 } 00936 00937 MachineInstr *MachineBasicBlock::remove_instr(MachineInstr *MI) { 00938 unbundleSingleMI(MI); 00939 MI->clearFlag(MachineInstr::BundledPred); 00940 MI->clearFlag(MachineInstr::BundledSucc); 00941 return Insts.remove(MI); 00942 } 00943 00944 MachineBasicBlock::instr_iterator 00945 MachineBasicBlock::insert(instr_iterator I, MachineInstr *MI) { 00946 assert(!MI->isBundledWithPred() && !MI->isBundledWithSucc() && 00947 "Cannot insert instruction with bundle flags"); 00948 // Set the bundle flags when inserting inside a bundle. 00949 if (I != instr_end() && I->isBundledWithPred()) { 00950 MI->setFlag(MachineInstr::BundledPred); 00951 MI->setFlag(MachineInstr::BundledSucc); 00952 } 00953 return Insts.insert(I, MI); 00954 } 00955 00956 /// removeFromParent - This method unlinks 'this' from the containing function, 00957 /// and returns it, but does not delete it. 00958 MachineBasicBlock *MachineBasicBlock::removeFromParent() { 00959 assert(getParent() && "Not embedded in a function!"); 00960 getParent()->remove(this); 00961 return this; 00962 } 00963 00964 00965 /// eraseFromParent - This method unlinks 'this' from the containing function, 00966 /// and deletes it. 00967 void MachineBasicBlock::eraseFromParent() { 00968 assert(getParent() && "Not embedded in a function!"); 00969 getParent()->erase(this); 00970 } 00971 00972 00973 /// ReplaceUsesOfBlockWith - Given a machine basic block that branched to 00974 /// 'Old', change the code and CFG so that it branches to 'New' instead. 00975 void MachineBasicBlock::ReplaceUsesOfBlockWith(MachineBasicBlock *Old, 00976 MachineBasicBlock *New) { 00977 assert(Old != New && "Cannot replace self with self!"); 00978 00979 MachineBasicBlock::instr_iterator I = instr_end(); 00980 while (I != instr_begin()) { 00981 --I; 00982 if (!I->isTerminator()) break; 00983 00984 // Scan the operands of this machine instruction, replacing any uses of Old 00985 // with New. 00986 for (unsigned i = 0, e = I->getNumOperands(); i != e; ++i) 00987 if (I->getOperand(i).isMBB() && 00988 I->getOperand(i).getMBB() == Old) 00989 I->getOperand(i).setMBB(New); 00990 } 00991 00992 // Update the successor information. 00993 replaceSuccessor(Old, New); 00994 } 00995 00996 /// CorrectExtraCFGEdges - Various pieces of code can cause excess edges in the 00997 /// CFG to be inserted. If we have proven that MBB can only branch to DestA and 00998 /// DestB, remove any other MBB successors from the CFG. DestA and DestB can be 00999 /// null. 01000 /// 01001 /// Besides DestA and DestB, retain other edges leading to LandingPads 01002 /// (currently there can be only one; we don't check or require that here). 01003 /// Note it is possible that DestA and/or DestB are LandingPads. 01004 bool MachineBasicBlock::CorrectExtraCFGEdges(MachineBasicBlock *DestA, 01005 MachineBasicBlock *DestB, 01006 bool isCond) { 01007 // The values of DestA and DestB frequently come from a call to the 01008 // 'TargetInstrInfo::AnalyzeBranch' method. We take our meaning of the initial 01009 // values from there. 01010 // 01011 // 1. If both DestA and DestB are null, then the block ends with no branches 01012 // (it falls through to its successor). 01013 // 2. If DestA is set, DestB is null, and isCond is false, then the block ends 01014 // with only an unconditional branch. 01015 // 3. If DestA is set, DestB is null, and isCond is true, then the block ends 01016 // with a conditional branch that falls through to a successor (DestB). 01017 // 4. If DestA and DestB is set and isCond is true, then the block ends with a 01018 // conditional branch followed by an unconditional branch. DestA is the 01019 // 'true' destination and DestB is the 'false' destination. 01020 01021 bool Changed = false; 01022 01023 MachineFunction::iterator FallThru = 01024 llvm::next(MachineFunction::iterator(this)); 01025 01026 if (DestA == 0 && DestB == 0) { 01027 // Block falls through to successor. 01028 DestA = FallThru; 01029 DestB = FallThru; 01030 } else if (DestA != 0 && DestB == 0) { 01031 if (isCond) 01032 // Block ends in conditional jump that falls through to successor. 01033 DestB = FallThru; 01034 } else { 01035 assert(DestA && DestB && isCond && 01036 "CFG in a bad state. Cannot correct CFG edges"); 01037 } 01038 01039 // Remove superfluous edges. I.e., those which aren't destinations of this 01040 // basic block, duplicate edges, or landing pads. 01041 SmallPtrSet<const MachineBasicBlock*, 8> SeenMBBs; 01042 MachineBasicBlock::succ_iterator SI = succ_begin(); 01043 while (SI != succ_end()) { 01044 const MachineBasicBlock *MBB = *SI; 01045 if (!SeenMBBs.insert(MBB) || 01046 (MBB != DestA && MBB != DestB && !MBB->isLandingPad())) { 01047 // This is a superfluous edge, remove it. 01048 SI = removeSuccessor(SI); 01049 Changed = true; 01050 } else { 01051 ++SI; 01052 } 01053 } 01054 01055 return Changed; 01056 } 01057 01058 /// findDebugLoc - find the next valid DebugLoc starting at MBBI, skipping 01059 /// any DBG_VALUE instructions. Return UnknownLoc if there is none. 01060 DebugLoc 01061 MachineBasicBlock::findDebugLoc(instr_iterator MBBI) { 01062 DebugLoc DL; 01063 instr_iterator E = instr_end(); 01064 if (MBBI == E) 01065 return DL; 01066 01067 // Skip debug declarations, we don't want a DebugLoc from them. 01068 while (MBBI != E && MBBI->isDebugValue()) 01069 MBBI++; 01070 if (MBBI != E) 01071 DL = MBBI->getDebugLoc(); 01072 return DL; 01073 } 01074 01075 /// getSuccWeight - Return weight of the edge from this block to MBB. 01076 /// 01077 uint32_t MachineBasicBlock::getSuccWeight(const_succ_iterator Succ) const { 01078 if (Weights.empty()) 01079 return 0; 01080 01081 return *getWeightIterator(Succ); 01082 } 01083 01084 /// getWeightIterator - Return wight iterator corresonding to the I successor 01085 /// iterator 01086 MachineBasicBlock::weight_iterator MachineBasicBlock:: 01087 getWeightIterator(MachineBasicBlock::succ_iterator I) { 01088 assert(Weights.size() == Successors.size() && "Async weight list!"); 01089 size_t index = std::distance(Successors.begin(), I); 01090 assert(index < Weights.size() && "Not a current successor!"); 01091 return Weights.begin() + index; 01092 } 01093 01094 /// getWeightIterator - Return wight iterator corresonding to the I successor 01095 /// iterator 01096 MachineBasicBlock::const_weight_iterator MachineBasicBlock:: 01097 getWeightIterator(MachineBasicBlock::const_succ_iterator I) const { 01098 assert(Weights.size() == Successors.size() && "Async weight list!"); 01099 const size_t index = std::distance(Successors.begin(), I); 01100 assert(index < Weights.size() && "Not a current successor!"); 01101 return Weights.begin() + index; 01102 } 01103 01104 /// Return whether (physical) register "Reg" has been <def>ined and not <kill>ed 01105 /// as of just before "MI". 01106 /// 01107 /// Search is localised to a neighborhood of 01108 /// Neighborhood instructions before (searching for defs or kills) and N 01109 /// instructions after (searching just for defs) MI. 01110 MachineBasicBlock::LivenessQueryResult 01111 MachineBasicBlock::computeRegisterLiveness(const TargetRegisterInfo *TRI, 01112 unsigned Reg, MachineInstr *MI, 01113 unsigned Neighborhood) { 01114 unsigned N = Neighborhood; 01115 MachineBasicBlock *MBB = MI->getParent(); 01116 01117 // Start by searching backwards from MI, looking for kills, reads or defs. 01118 01119 MachineBasicBlock::iterator I(MI); 01120 // If this is the first insn in the block, don't search backwards. 01121 if (I != MBB->begin()) { 01122 do { 01123 --I; 01124 01125 MachineOperandIteratorBase::PhysRegInfo Analysis = 01126 MIOperands(I).analyzePhysReg(Reg, TRI); 01127 01128 if (Analysis.Defines) 01129 // Outputs happen after inputs so they take precedence if both are 01130 // present. 01131 return Analysis.DefinesDead ? LQR_Dead : LQR_Live; 01132 01133 if (Analysis.Kills || Analysis.Clobbers) 01134 // Register killed, so isn't live. 01135 return LQR_Dead; 01136 01137 else if (Analysis.ReadsOverlap) 01138 // Defined or read without a previous kill - live. 01139 return Analysis.Reads ? LQR_Live : LQR_OverlappingLive; 01140 01141 } while (I != MBB->begin() && --N > 0); 01142 } 01143 01144 // Did we get to the start of the block? 01145 if (I == MBB->begin()) { 01146 // If so, the register's state is definitely defined by the live-in state. 01147 for (MCRegAliasIterator RAI(Reg, TRI, /*IncludeSelf=*/true); 01148 RAI.isValid(); ++RAI) { 01149 if (MBB->isLiveIn(*RAI)) 01150 return (*RAI == Reg) ? LQR_Live : LQR_OverlappingLive; 01151 } 01152 01153 return LQR_Dead; 01154 } 01155 01156 N = Neighborhood; 01157 01158 // Try searching forwards from MI, looking for reads or defs. 01159 I = MachineBasicBlock::iterator(MI); 01160 // If this is the last insn in the block, don't search forwards. 01161 if (I != MBB->end()) { 01162 for (++I; I != MBB->end() && N > 0; ++I, --N) { 01163 MachineOperandIteratorBase::PhysRegInfo Analysis = 01164 MIOperands(I).analyzePhysReg(Reg, TRI); 01165 01166 if (Analysis.ReadsOverlap) 01167 // Used, therefore must have been live. 01168 return (Analysis.Reads) ? 01169 LQR_Live : LQR_OverlappingLive; 01170 01171 else if (Analysis.Clobbers || Analysis.Defines) 01172 // Defined (but not read) therefore cannot have been live. 01173 return LQR_Dead; 01174 } 01175 } 01176 01177 // At this point we have no idea of the liveness of the register. 01178 return LQR_Unknown; 01179 } 01180 01181 void llvm::WriteAsOperand(raw_ostream &OS, const MachineBasicBlock *MBB, 01182 bool t) { 01183 OS << "BB#" << MBB->getNumber(); 01184 } 01185