LLVM API Documentation
00001 //===-- LiveIntervalAnalysis.cpp - Live Interval Analysis -----------------===// 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 // This file implements the LiveInterval analysis pass which is used 00011 // by the Linear Scan Register allocator. This pass linearizes the 00012 // basic blocks of the function in DFS order and uses the 00013 // LiveVariables pass to conservatively compute live intervals for 00014 // each virtual and physical register. 00015 // 00016 //===----------------------------------------------------------------------===// 00017 00018 #define DEBUG_TYPE "regalloc" 00019 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00020 #include "LiveRangeCalc.h" 00021 #include "llvm/ADT/DenseSet.h" 00022 #include "llvm/ADT/STLExtras.h" 00023 #include "llvm/Analysis/AliasAnalysis.h" 00024 #include "llvm/CodeGen/LiveVariables.h" 00025 #include "llvm/CodeGen/MachineDominators.h" 00026 #include "llvm/CodeGen/MachineInstr.h" 00027 #include "llvm/CodeGen/MachineRegisterInfo.h" 00028 #include "llvm/CodeGen/Passes.h" 00029 #include "llvm/CodeGen/VirtRegMap.h" 00030 #include "llvm/IR/Value.h" 00031 #include "llvm/Support/CommandLine.h" 00032 #include "llvm/Support/Debug.h" 00033 #include "llvm/Support/ErrorHandling.h" 00034 #include "llvm/Support/raw_ostream.h" 00035 #include "llvm/Target/TargetInstrInfo.h" 00036 #include "llvm/Target/TargetMachine.h" 00037 #include "llvm/Target/TargetRegisterInfo.h" 00038 #include <algorithm> 00039 #include <cmath> 00040 #include <limits> 00041 using namespace llvm; 00042 00043 char LiveIntervals::ID = 0; 00044 char &llvm::LiveIntervalsID = LiveIntervals::ID; 00045 INITIALIZE_PASS_BEGIN(LiveIntervals, "liveintervals", 00046 "Live Interval Analysis", false, false) 00047 INITIALIZE_AG_DEPENDENCY(AliasAnalysis) 00048 INITIALIZE_PASS_DEPENDENCY(LiveVariables) 00049 INITIALIZE_PASS_DEPENDENCY(MachineDominatorTree) 00050 INITIALIZE_PASS_DEPENDENCY(SlotIndexes) 00051 INITIALIZE_PASS_END(LiveIntervals, "liveintervals", 00052 "Live Interval Analysis", false, false) 00053 00054 void LiveIntervals::getAnalysisUsage(AnalysisUsage &AU) const { 00055 AU.setPreservesCFG(); 00056 AU.addRequired<AliasAnalysis>(); 00057 AU.addPreserved<AliasAnalysis>(); 00058 // LiveVariables isn't really required by this analysis, it is only required 00059 // here to make sure it is live during TwoAddressInstructionPass and 00060 // PHIElimination. This is temporary. 00061 AU.addRequired<LiveVariables>(); 00062 AU.addPreserved<LiveVariables>(); 00063 AU.addPreservedID(MachineLoopInfoID); 00064 AU.addRequiredTransitiveID(MachineDominatorsID); 00065 AU.addPreservedID(MachineDominatorsID); 00066 AU.addPreserved<SlotIndexes>(); 00067 AU.addRequiredTransitive<SlotIndexes>(); 00068 MachineFunctionPass::getAnalysisUsage(AU); 00069 } 00070 00071 LiveIntervals::LiveIntervals() : MachineFunctionPass(ID), 00072 DomTree(0), LRCalc(0) { 00073 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 00074 } 00075 00076 LiveIntervals::~LiveIntervals() { 00077 delete LRCalc; 00078 } 00079 00080 void LiveIntervals::releaseMemory() { 00081 // Free the live intervals themselves. 00082 for (unsigned i = 0, e = VirtRegIntervals.size(); i != e; ++i) 00083 delete VirtRegIntervals[TargetRegisterInfo::index2VirtReg(i)]; 00084 VirtRegIntervals.clear(); 00085 RegMaskSlots.clear(); 00086 RegMaskBits.clear(); 00087 RegMaskBlocks.clear(); 00088 00089 for (unsigned i = 0, e = RegUnitIntervals.size(); i != e; ++i) 00090 delete RegUnitIntervals[i]; 00091 RegUnitIntervals.clear(); 00092 00093 // Release VNInfo memory regions, VNInfo objects don't need to be dtor'd. 00094 VNInfoAllocator.Reset(); 00095 } 00096 00097 /// runOnMachineFunction - Register allocate the whole function 00098 /// 00099 bool LiveIntervals::runOnMachineFunction(MachineFunction &fn) { 00100 MF = &fn; 00101 MRI = &MF->getRegInfo(); 00102 TM = &fn.getTarget(); 00103 TRI = TM->getRegisterInfo(); 00104 TII = TM->getInstrInfo(); 00105 AA = &getAnalysis<AliasAnalysis>(); 00106 Indexes = &getAnalysis<SlotIndexes>(); 00107 DomTree = &getAnalysis<MachineDominatorTree>(); 00108 if (!LRCalc) 00109 LRCalc = new LiveRangeCalc(); 00110 00111 // Allocate space for all virtual registers. 00112 VirtRegIntervals.resize(MRI->getNumVirtRegs()); 00113 00114 computeVirtRegs(); 00115 computeRegMasks(); 00116 computeLiveInRegUnits(); 00117 00118 DEBUG(dump()); 00119 return true; 00120 } 00121 00122 /// print - Implement the dump method. 00123 void LiveIntervals::print(raw_ostream &OS, const Module* ) const { 00124 OS << "********** INTERVALS **********\n"; 00125 00126 // Dump the regunits. 00127 for (unsigned i = 0, e = RegUnitIntervals.size(); i != e; ++i) 00128 if (LiveInterval *LI = RegUnitIntervals[i]) 00129 OS << PrintRegUnit(i, TRI) << " = " << *LI << '\n'; 00130 00131 // Dump the virtregs. 00132 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 00133 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 00134 if (hasInterval(Reg)) 00135 OS << PrintReg(Reg) << " = " << getInterval(Reg) << '\n'; 00136 } 00137 00138 OS << "RegMasks:"; 00139 for (unsigned i = 0, e = RegMaskSlots.size(); i != e; ++i) 00140 OS << ' ' << RegMaskSlots[i]; 00141 OS << '\n'; 00142 00143 printInstrs(OS); 00144 } 00145 00146 void LiveIntervals::printInstrs(raw_ostream &OS) const { 00147 OS << "********** MACHINEINSTRS **********\n"; 00148 MF->print(OS, Indexes); 00149 } 00150 00151 #if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP) 00152 void LiveIntervals::dumpInstrs() const { 00153 printInstrs(dbgs()); 00154 } 00155 #endif 00156 00157 LiveInterval* LiveIntervals::createInterval(unsigned reg) { 00158 float Weight = TargetRegisterInfo::isPhysicalRegister(reg) ? HUGE_VALF : 0.0F; 00159 return new LiveInterval(reg, Weight); 00160 } 00161 00162 00163 /// computeVirtRegInterval - Compute the live interval of a virtual register, 00164 /// based on defs and uses. 00165 void LiveIntervals::computeVirtRegInterval(LiveInterval *LI) { 00166 assert(LRCalc && "LRCalc not initialized."); 00167 assert(LI->empty() && "Should only compute empty intervals."); 00168 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 00169 LRCalc->createDeadDefs(LI); 00170 LRCalc->extendToUses(LI); 00171 } 00172 00173 void LiveIntervals::computeVirtRegs() { 00174 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 00175 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 00176 if (MRI->reg_nodbg_empty(Reg)) 00177 continue; 00178 LiveInterval *LI = createInterval(Reg); 00179 VirtRegIntervals[Reg] = LI; 00180 computeVirtRegInterval(LI); 00181 } 00182 } 00183 00184 void LiveIntervals::computeRegMasks() { 00185 RegMaskBlocks.resize(MF->getNumBlockIDs()); 00186 00187 // Find all instructions with regmask operands. 00188 for (MachineFunction::iterator MBBI = MF->begin(), E = MF->end(); 00189 MBBI != E; ++MBBI) { 00190 MachineBasicBlock *MBB = MBBI; 00191 std::pair<unsigned, unsigned> &RMB = RegMaskBlocks[MBB->getNumber()]; 00192 RMB.first = RegMaskSlots.size(); 00193 for (MachineBasicBlock::iterator MI = MBB->begin(), ME = MBB->end(); 00194 MI != ME; ++MI) 00195 for (MIOperands MO(MI); MO.isValid(); ++MO) { 00196 if (!MO->isRegMask()) 00197 continue; 00198 RegMaskSlots.push_back(Indexes->getInstructionIndex(MI).getRegSlot()); 00199 RegMaskBits.push_back(MO->getRegMask()); 00200 } 00201 // Compute the number of register mask instructions in this block. 00202 RMB.second = RegMaskSlots.size() - RMB.first; 00203 } 00204 } 00205 00206 //===----------------------------------------------------------------------===// 00207 // Register Unit Liveness 00208 //===----------------------------------------------------------------------===// 00209 // 00210 // Fixed interference typically comes from ABI boundaries: Function arguments 00211 // and return values are passed in fixed registers, and so are exception 00212 // pointers entering landing pads. Certain instructions require values to be 00213 // present in specific registers. That is also represented through fixed 00214 // interference. 00215 // 00216 00217 /// computeRegUnitInterval - Compute the live interval of a register unit, based 00218 /// on the uses and defs of aliasing registers. The interval should be empty, 00219 /// or contain only dead phi-defs from ABI blocks. 00220 void LiveIntervals::computeRegUnitInterval(LiveInterval *LI) { 00221 unsigned Unit = LI->reg; 00222 00223 assert(LRCalc && "LRCalc not initialized."); 00224 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 00225 00226 // The physregs aliasing Unit are the roots and their super-registers. 00227 // Create all values as dead defs before extending to uses. Note that roots 00228 // may share super-registers. That's OK because createDeadDefs() is 00229 // idempotent. It is very rare for a register unit to have multiple roots, so 00230 // uniquing super-registers is probably not worthwhile. 00231 for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) { 00232 unsigned Root = *Roots; 00233 if (!MRI->reg_empty(Root)) 00234 LRCalc->createDeadDefs(LI, Root); 00235 for (MCSuperRegIterator Supers(Root, TRI); Supers.isValid(); ++Supers) { 00236 if (!MRI->reg_empty(*Supers)) 00237 LRCalc->createDeadDefs(LI, *Supers); 00238 } 00239 } 00240 00241 // Now extend LI to reach all uses. 00242 // Ignore uses of reserved registers. We only track defs of those. 00243 for (MCRegUnitRootIterator Roots(Unit, TRI); Roots.isValid(); ++Roots) { 00244 unsigned Root = *Roots; 00245 if (!MRI->isReserved(Root) && !MRI->reg_empty(Root)) 00246 LRCalc->extendToUses(LI, Root); 00247 for (MCSuperRegIterator Supers(Root, TRI); Supers.isValid(); ++Supers) { 00248 unsigned Reg = *Supers; 00249 if (!MRI->isReserved(Reg) && !MRI->reg_empty(Reg)) 00250 LRCalc->extendToUses(LI, Reg); 00251 } 00252 } 00253 } 00254 00255 00256 /// computeLiveInRegUnits - Precompute the live ranges of any register units 00257 /// that are live-in to an ABI block somewhere. Register values can appear 00258 /// without a corresponding def when entering the entry block or a landing pad. 00259 /// 00260 void LiveIntervals::computeLiveInRegUnits() { 00261 RegUnitIntervals.resize(TRI->getNumRegUnits()); 00262 DEBUG(dbgs() << "Computing live-in reg-units in ABI blocks.\n"); 00263 00264 // Keep track of the intervals allocated. 00265 SmallVector<LiveInterval*, 8> NewIntvs; 00266 00267 // Check all basic blocks for live-ins. 00268 for (MachineFunction::const_iterator MFI = MF->begin(), MFE = MF->end(); 00269 MFI != MFE; ++MFI) { 00270 const MachineBasicBlock *MBB = MFI; 00271 00272 // We only care about ABI blocks: Entry + landing pads. 00273 if ((MFI != MF->begin() && !MBB->isLandingPad()) || MBB->livein_empty()) 00274 continue; 00275 00276 // Create phi-defs at Begin for all live-in registers. 00277 SlotIndex Begin = Indexes->getMBBStartIdx(MBB); 00278 DEBUG(dbgs() << Begin << "\tBB#" << MBB->getNumber()); 00279 for (MachineBasicBlock::livein_iterator LII = MBB->livein_begin(), 00280 LIE = MBB->livein_end(); LII != LIE; ++LII) { 00281 for (MCRegUnitIterator Units(*LII, TRI); Units.isValid(); ++Units) { 00282 unsigned Unit = *Units; 00283 LiveInterval *Intv = RegUnitIntervals[Unit]; 00284 if (!Intv) { 00285 Intv = RegUnitIntervals[Unit] = new LiveInterval(Unit, HUGE_VALF); 00286 NewIntvs.push_back(Intv); 00287 } 00288 VNInfo *VNI = Intv->createDeadDef(Begin, getVNInfoAllocator()); 00289 (void)VNI; 00290 DEBUG(dbgs() << ' ' << PrintRegUnit(Unit, TRI) << '#' << VNI->id); 00291 } 00292 } 00293 DEBUG(dbgs() << '\n'); 00294 } 00295 DEBUG(dbgs() << "Created " << NewIntvs.size() << " new intervals.\n"); 00296 00297 // Compute the 'normal' part of the intervals. 00298 for (unsigned i = 0, e = NewIntvs.size(); i != e; ++i) 00299 computeRegUnitInterval(NewIntvs[i]); 00300 } 00301 00302 00303 /// shrinkToUses - After removing some uses of a register, shrink its live 00304 /// range to just the remaining uses. This method does not compute reaching 00305 /// defs for new uses, and it doesn't remove dead defs. 00306 bool LiveIntervals::shrinkToUses(LiveInterval *li, 00307 SmallVectorImpl<MachineInstr*> *dead) { 00308 DEBUG(dbgs() << "Shrink: " << *li << '\n'); 00309 assert(TargetRegisterInfo::isVirtualRegister(li->reg) 00310 && "Can only shrink virtual registers"); 00311 // Find all the values used, including PHI kills. 00312 SmallVector<std::pair<SlotIndex, VNInfo*>, 16> WorkList; 00313 00314 // Blocks that have already been added to WorkList as live-out. 00315 SmallPtrSet<MachineBasicBlock*, 16> LiveOut; 00316 00317 // Visit all instructions reading li->reg. 00318 for (MachineRegisterInfo::reg_iterator I = MRI->reg_begin(li->reg); 00319 MachineInstr *UseMI = I.skipInstruction();) { 00320 if (UseMI->isDebugValue() || !UseMI->readsVirtualRegister(li->reg)) 00321 continue; 00322 SlotIndex Idx = getInstructionIndex(UseMI).getRegSlot(); 00323 LiveRangeQuery LRQ(*li, Idx); 00324 VNInfo *VNI = LRQ.valueIn(); 00325 if (!VNI) { 00326 // This shouldn't happen: readsVirtualRegister returns true, but there is 00327 // no live value. It is likely caused by a target getting <undef> flags 00328 // wrong. 00329 DEBUG(dbgs() << Idx << '\t' << *UseMI 00330 << "Warning: Instr claims to read non-existent value in " 00331 << *li << '\n'); 00332 continue; 00333 } 00334 // Special case: An early-clobber tied operand reads and writes the 00335 // register one slot early. 00336 if (VNInfo *DefVNI = LRQ.valueDefined()) 00337 Idx = DefVNI->def; 00338 00339 WorkList.push_back(std::make_pair(Idx, VNI)); 00340 } 00341 00342 // Create a new live interval with only minimal live segments per def. 00343 LiveInterval NewLI(li->reg, 0); 00344 for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end(); 00345 I != E; ++I) { 00346 VNInfo *VNI = *I; 00347 if (VNI->isUnused()) 00348 continue; 00349 NewLI.addRange(LiveRange(VNI->def, VNI->def.getDeadSlot(), VNI)); 00350 } 00351 00352 // Keep track of the PHIs that are in use. 00353 SmallPtrSet<VNInfo*, 8> UsedPHIs; 00354 00355 // Extend intervals to reach all uses in WorkList. 00356 while (!WorkList.empty()) { 00357 SlotIndex Idx = WorkList.back().first; 00358 VNInfo *VNI = WorkList.back().second; 00359 WorkList.pop_back(); 00360 const MachineBasicBlock *MBB = getMBBFromIndex(Idx.getPrevSlot()); 00361 SlotIndex BlockStart = getMBBStartIdx(MBB); 00362 00363 // Extend the live range for VNI to be live at Idx. 00364 if (VNInfo *ExtVNI = NewLI.extendInBlock(BlockStart, Idx)) { 00365 (void)ExtVNI; 00366 assert(ExtVNI == VNI && "Unexpected existing value number"); 00367 // Is this a PHIDef we haven't seen before? 00368 if (!VNI->isPHIDef() || VNI->def != BlockStart || !UsedPHIs.insert(VNI)) 00369 continue; 00370 // The PHI is live, make sure the predecessors are live-out. 00371 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), 00372 PE = MBB->pred_end(); PI != PE; ++PI) { 00373 if (!LiveOut.insert(*PI)) 00374 continue; 00375 SlotIndex Stop = getMBBEndIdx(*PI); 00376 // A predecessor is not required to have a live-out value for a PHI. 00377 if (VNInfo *PVNI = li->getVNInfoBefore(Stop)) 00378 WorkList.push_back(std::make_pair(Stop, PVNI)); 00379 } 00380 continue; 00381 } 00382 00383 // VNI is live-in to MBB. 00384 DEBUG(dbgs() << " live-in at " << BlockStart << '\n'); 00385 NewLI.addRange(LiveRange(BlockStart, Idx, VNI)); 00386 00387 // Make sure VNI is live-out from the predecessors. 00388 for (MachineBasicBlock::const_pred_iterator PI = MBB->pred_begin(), 00389 PE = MBB->pred_end(); PI != PE; ++PI) { 00390 if (!LiveOut.insert(*PI)) 00391 continue; 00392 SlotIndex Stop = getMBBEndIdx(*PI); 00393 assert(li->getVNInfoBefore(Stop) == VNI && 00394 "Wrong value out of predecessor"); 00395 WorkList.push_back(std::make_pair(Stop, VNI)); 00396 } 00397 } 00398 00399 // Handle dead values. 00400 bool CanSeparate = false; 00401 for (LiveInterval::vni_iterator I = li->vni_begin(), E = li->vni_end(); 00402 I != E; ++I) { 00403 VNInfo *VNI = *I; 00404 if (VNI->isUnused()) 00405 continue; 00406 LiveInterval::iterator LII = NewLI.FindLiveRangeContaining(VNI->def); 00407 assert(LII != NewLI.end() && "Missing live range for PHI"); 00408 if (LII->end != VNI->def.getDeadSlot()) 00409 continue; 00410 if (VNI->isPHIDef()) { 00411 // This is a dead PHI. Remove it. 00412 VNI->markUnused(); 00413 NewLI.removeRange(*LII); 00414 DEBUG(dbgs() << "Dead PHI at " << VNI->def << " may separate interval\n"); 00415 CanSeparate = true; 00416 } else { 00417 // This is a dead def. Make sure the instruction knows. 00418 MachineInstr *MI = getInstructionFromIndex(VNI->def); 00419 assert(MI && "No instruction defining live value"); 00420 MI->addRegisterDead(li->reg, TRI); 00421 if (dead && MI->allDefsAreDead()) { 00422 DEBUG(dbgs() << "All defs dead: " << VNI->def << '\t' << *MI); 00423 dead->push_back(MI); 00424 } 00425 } 00426 } 00427 00428 // Move the trimmed ranges back. 00429 li->ranges.swap(NewLI.ranges); 00430 DEBUG(dbgs() << "Shrunk: " << *li << '\n'); 00431 return CanSeparate; 00432 } 00433 00434 void LiveIntervals::extendToIndices(LiveInterval *LI, 00435 ArrayRef<SlotIndex> Indices) { 00436 assert(LRCalc && "LRCalc not initialized."); 00437 LRCalc->reset(MF, getSlotIndexes(), DomTree, &getVNInfoAllocator()); 00438 for (unsigned i = 0, e = Indices.size(); i != e; ++i) 00439 LRCalc->extend(LI, Indices[i]); 00440 } 00441 00442 void LiveIntervals::pruneValue(LiveInterval *LI, SlotIndex Kill, 00443 SmallVectorImpl<SlotIndex> *EndPoints) { 00444 LiveRangeQuery LRQ(*LI, Kill); 00445 VNInfo *VNI = LRQ.valueOut(); 00446 if (!VNI) 00447 return; 00448 00449 MachineBasicBlock *KillMBB = Indexes->getMBBFromIndex(Kill); 00450 SlotIndex MBBStart, MBBEnd; 00451 tie(MBBStart, MBBEnd) = Indexes->getMBBRange(KillMBB); 00452 00453 // If VNI isn't live out from KillMBB, the value is trivially pruned. 00454 if (LRQ.endPoint() < MBBEnd) { 00455 LI->removeRange(Kill, LRQ.endPoint()); 00456 if (EndPoints) EndPoints->push_back(LRQ.endPoint()); 00457 return; 00458 } 00459 00460 // VNI is live out of KillMBB. 00461 LI->removeRange(Kill, MBBEnd); 00462 if (EndPoints) EndPoints->push_back(MBBEnd); 00463 00464 // Find all blocks that are reachable from KillMBB without leaving VNI's live 00465 // range. It is possible that KillMBB itself is reachable, so start a DFS 00466 // from each successor. 00467 typedef SmallPtrSet<MachineBasicBlock*, 9> VisitedTy; 00468 VisitedTy Visited; 00469 for (MachineBasicBlock::succ_iterator 00470 SuccI = KillMBB->succ_begin(), SuccE = KillMBB->succ_end(); 00471 SuccI != SuccE; ++SuccI) { 00472 for (df_ext_iterator<MachineBasicBlock*, VisitedTy> 00473 I = df_ext_begin(*SuccI, Visited), E = df_ext_end(*SuccI, Visited); 00474 I != E;) { 00475 MachineBasicBlock *MBB = *I; 00476 00477 // Check if VNI is live in to MBB. 00478 tie(MBBStart, MBBEnd) = Indexes->getMBBRange(MBB); 00479 LiveRangeQuery LRQ(*LI, MBBStart); 00480 if (LRQ.valueIn() != VNI) { 00481 // This block isn't part of the VNI live range. Prune the search. 00482 I.skipChildren(); 00483 continue; 00484 } 00485 00486 // Prune the search if VNI is killed in MBB. 00487 if (LRQ.endPoint() < MBBEnd) { 00488 LI->removeRange(MBBStart, LRQ.endPoint()); 00489 if (EndPoints) EndPoints->push_back(LRQ.endPoint()); 00490 I.skipChildren(); 00491 continue; 00492 } 00493 00494 // VNI is live through MBB. 00495 LI->removeRange(MBBStart, MBBEnd); 00496 if (EndPoints) EndPoints->push_back(MBBEnd); 00497 ++I; 00498 } 00499 } 00500 } 00501 00502 //===----------------------------------------------------------------------===// 00503 // Register allocator hooks. 00504 // 00505 00506 void LiveIntervals::addKillFlags(const VirtRegMap *VRM) { 00507 // Keep track of regunit ranges. 00508 SmallVector<std::pair<LiveInterval*, LiveInterval::iterator>, 8> RU; 00509 00510 for (unsigned i = 0, e = MRI->getNumVirtRegs(); i != e; ++i) { 00511 unsigned Reg = TargetRegisterInfo::index2VirtReg(i); 00512 if (MRI->reg_nodbg_empty(Reg)) 00513 continue; 00514 LiveInterval *LI = &getInterval(Reg); 00515 if (LI->empty()) 00516 continue; 00517 00518 // Find the regunit intervals for the assigned register. They may overlap 00519 // the virtual register live range, cancelling any kills. 00520 RU.clear(); 00521 for (MCRegUnitIterator Units(VRM->getPhys(Reg), TRI); Units.isValid(); 00522 ++Units) { 00523 LiveInterval *RUInt = &getRegUnit(*Units); 00524 if (RUInt->empty()) 00525 continue; 00526 RU.push_back(std::make_pair(RUInt, RUInt->find(LI->begin()->end))); 00527 } 00528 00529 // Every instruction that kills Reg corresponds to a live range end point. 00530 for (LiveInterval::iterator RI = LI->begin(), RE = LI->end(); RI != RE; 00531 ++RI) { 00532 // A block index indicates an MBB edge. 00533 if (RI->end.isBlock()) 00534 continue; 00535 MachineInstr *MI = getInstructionFromIndex(RI->end); 00536 if (!MI) 00537 continue; 00538 00539 // Check if any of the reguints are live beyond the end of RI. That could 00540 // happen when a physreg is defined as a copy of a virtreg: 00541 // 00542 // %EAX = COPY %vreg5 00543 // FOO %vreg5 <--- MI, cancel kill because %EAX is live. 00544 // BAR %EAX<kill> 00545 // 00546 // There should be no kill flag on FOO when %vreg5 is rewritten as %EAX. 00547 bool CancelKill = false; 00548 for (unsigned u = 0, e = RU.size(); u != e; ++u) { 00549 LiveInterval *RInt = RU[u].first; 00550 LiveInterval::iterator &I = RU[u].second; 00551 if (I == RInt->end()) 00552 continue; 00553 I = RInt->advanceTo(I, RI->end); 00554 if (I == RInt->end() || I->start >= RI->end) 00555 continue; 00556 // I is overlapping RI. 00557 CancelKill = true; 00558 break; 00559 } 00560 if (CancelKill) 00561 MI->clearRegisterKills(Reg, NULL); 00562 else 00563 MI->addRegisterKilled(Reg, NULL); 00564 } 00565 } 00566 } 00567 00568 MachineBasicBlock* 00569 LiveIntervals::intervalIsInOneMBB(const LiveInterval &LI) const { 00570 // A local live range must be fully contained inside the block, meaning it is 00571 // defined and killed at instructions, not at block boundaries. It is not 00572 // live in or or out of any block. 00573 // 00574 // It is technically possible to have a PHI-defined live range identical to a 00575 // single block, but we are going to return false in that case. 00576 00577 SlotIndex Start = LI.beginIndex(); 00578 if (Start.isBlock()) 00579 return NULL; 00580 00581 SlotIndex Stop = LI.endIndex(); 00582 if (Stop.isBlock()) 00583 return NULL; 00584 00585 // getMBBFromIndex doesn't need to search the MBB table when both indexes 00586 // belong to proper instructions. 00587 MachineBasicBlock *MBB1 = Indexes->getMBBFromIndex(Start); 00588 MachineBasicBlock *MBB2 = Indexes->getMBBFromIndex(Stop); 00589 return MBB1 == MBB2 ? MBB1 : NULL; 00590 } 00591 00592 bool 00593 LiveIntervals::hasPHIKill(const LiveInterval &LI, const VNInfo *VNI) const { 00594 for (LiveInterval::const_vni_iterator I = LI.vni_begin(), E = LI.vni_end(); 00595 I != E; ++I) { 00596 const VNInfo *PHI = *I; 00597 if (PHI->isUnused() || !PHI->isPHIDef()) 00598 continue; 00599 const MachineBasicBlock *PHIMBB = getMBBFromIndex(PHI->def); 00600 // Conservatively return true instead of scanning huge predecessor lists. 00601 if (PHIMBB->pred_size() > 100) 00602 return true; 00603 for (MachineBasicBlock::const_pred_iterator 00604 PI = PHIMBB->pred_begin(), PE = PHIMBB->pred_end(); PI != PE; ++PI) 00605 if (VNI == LI.getVNInfoBefore(Indexes->getMBBEndIdx(*PI))) 00606 return true; 00607 } 00608 return false; 00609 } 00610 00611 float 00612 LiveIntervals::getSpillWeight(bool isDef, bool isUse, unsigned loopDepth) { 00613 // Limit the loop depth ridiculousness. 00614 if (loopDepth > 200) 00615 loopDepth = 200; 00616 00617 // The loop depth is used to roughly estimate the number of times the 00618 // instruction is executed. Something like 10^d is simple, but will quickly 00619 // overflow a float. This expression behaves like 10^d for small d, but is 00620 // more tempered for large d. At d=200 we get 6.7e33 which leaves a bit of 00621 // headroom before overflow. 00622 // By the way, powf() might be unavailable here. For consistency, 00623 // We may take pow(double,double). 00624 float lc = std::pow(1 + (100.0 / (loopDepth + 10)), (double)loopDepth); 00625 00626 return (isDef + isUse) * lc; 00627 } 00628 00629 LiveRange LiveIntervals::addLiveRangeToEndOfBlock(unsigned reg, 00630 MachineInstr* startInst) { 00631 LiveInterval& Interval = getOrCreateInterval(reg); 00632 VNInfo* VN = Interval.getNextValue( 00633 SlotIndex(getInstructionIndex(startInst).getRegSlot()), 00634 getVNInfoAllocator()); 00635 LiveRange LR( 00636 SlotIndex(getInstructionIndex(startInst).getRegSlot()), 00637 getMBBEndIdx(startInst->getParent()), VN); 00638 Interval.addRange(LR); 00639 00640 return LR; 00641 } 00642 00643 00644 //===----------------------------------------------------------------------===// 00645 // Register mask functions 00646 //===----------------------------------------------------------------------===// 00647 00648 bool LiveIntervals::checkRegMaskInterference(LiveInterval &LI, 00649 BitVector &UsableRegs) { 00650 if (LI.empty()) 00651 return false; 00652 LiveInterval::iterator LiveI = LI.begin(), LiveE = LI.end(); 00653 00654 // Use a smaller arrays for local live ranges. 00655 ArrayRef<SlotIndex> Slots; 00656 ArrayRef<const uint32_t*> Bits; 00657 if (MachineBasicBlock *MBB = intervalIsInOneMBB(LI)) { 00658 Slots = getRegMaskSlotsInBlock(MBB->getNumber()); 00659 Bits = getRegMaskBitsInBlock(MBB->getNumber()); 00660 } else { 00661 Slots = getRegMaskSlots(); 00662 Bits = getRegMaskBits(); 00663 } 00664 00665 // We are going to enumerate all the register mask slots contained in LI. 00666 // Start with a binary search of RegMaskSlots to find a starting point. 00667 ArrayRef<SlotIndex>::iterator SlotI = 00668 std::lower_bound(Slots.begin(), Slots.end(), LiveI->start); 00669 ArrayRef<SlotIndex>::iterator SlotE = Slots.end(); 00670 00671 // No slots in range, LI begins after the last call. 00672 if (SlotI == SlotE) 00673 return false; 00674 00675 bool Found = false; 00676 for (;;) { 00677 assert(*SlotI >= LiveI->start); 00678 // Loop over all slots overlapping this segment. 00679 while (*SlotI < LiveI->end) { 00680 // *SlotI overlaps LI. Collect mask bits. 00681 if (!Found) { 00682 // This is the first overlap. Initialize UsableRegs to all ones. 00683 UsableRegs.clear(); 00684 UsableRegs.resize(TRI->getNumRegs(), true); 00685 Found = true; 00686 } 00687 // Remove usable registers clobbered by this mask. 00688 UsableRegs.clearBitsNotInMask(Bits[SlotI-Slots.begin()]); 00689 if (++SlotI == SlotE) 00690 return Found; 00691 } 00692 // *SlotI is beyond the current LI segment. 00693 LiveI = LI.advanceTo(LiveI, *SlotI); 00694 if (LiveI == LiveE) 00695 return Found; 00696 // Advance SlotI until it overlaps. 00697 while (*SlotI < LiveI->start) 00698 if (++SlotI == SlotE) 00699 return Found; 00700 } 00701 } 00702 00703 //===----------------------------------------------------------------------===// 00704 // IntervalUpdate class. 00705 //===----------------------------------------------------------------------===// 00706 00707 // HMEditor is a toolkit used by handleMove to trim or extend live intervals. 00708 class LiveIntervals::HMEditor { 00709 private: 00710 LiveIntervals& LIS; 00711 const MachineRegisterInfo& MRI; 00712 const TargetRegisterInfo& TRI; 00713 SlotIndex OldIdx; 00714 SlotIndex NewIdx; 00715 SmallPtrSet<LiveInterval*, 8> Updated; 00716 bool UpdateFlags; 00717 00718 public: 00719 HMEditor(LiveIntervals& LIS, const MachineRegisterInfo& MRI, 00720 const TargetRegisterInfo& TRI, 00721 SlotIndex OldIdx, SlotIndex NewIdx, bool UpdateFlags) 00722 : LIS(LIS), MRI(MRI), TRI(TRI), OldIdx(OldIdx), NewIdx(NewIdx), 00723 UpdateFlags(UpdateFlags) {} 00724 00725 // FIXME: UpdateFlags is a workaround that creates live intervals for all 00726 // physregs, even those that aren't needed for regalloc, in order to update 00727 // kill flags. This is wasteful. Eventually, LiveVariables will strip all kill 00728 // flags, and postRA passes will use a live register utility instead. 00729 LiveInterval *getRegUnitLI(unsigned Unit) { 00730 if (UpdateFlags) 00731 return &LIS.getRegUnit(Unit); 00732 return LIS.getCachedRegUnit(Unit); 00733 } 00734 00735 /// Update all live ranges touched by MI, assuming a move from OldIdx to 00736 /// NewIdx. 00737 void updateAllRanges(MachineInstr *MI) { 00738 DEBUG(dbgs() << "handleMove " << OldIdx << " -> " << NewIdx << ": " << *MI); 00739 bool hasRegMask = false; 00740 for (MIOperands MO(MI); MO.isValid(); ++MO) { 00741 if (MO->isRegMask()) 00742 hasRegMask = true; 00743 if (!MO->isReg()) 00744 continue; 00745 // Aggressively clear all kill flags. 00746 // They are reinserted by VirtRegRewriter. 00747 if (MO->isUse()) 00748 MO->setIsKill(false); 00749 00750 unsigned Reg = MO->getReg(); 00751 if (!Reg) 00752 continue; 00753 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 00754 updateRange(LIS.getInterval(Reg)); 00755 continue; 00756 } 00757 00758 // For physregs, only update the regunits that actually have a 00759 // precomputed live range. 00760 for (MCRegUnitIterator Units(Reg, &TRI); Units.isValid(); ++Units) 00761 if (LiveInterval *LI = getRegUnitLI(*Units)) 00762 updateRange(*LI); 00763 } 00764 if (hasRegMask) 00765 updateRegMaskSlots(); 00766 } 00767 00768 private: 00769 /// Update a single live range, assuming an instruction has been moved from 00770 /// OldIdx to NewIdx. 00771 void updateRange(LiveInterval &LI) { 00772 if (!Updated.insert(&LI)) 00773 return; 00774 DEBUG({ 00775 dbgs() << " "; 00776 if (TargetRegisterInfo::isVirtualRegister(LI.reg)) 00777 dbgs() << PrintReg(LI.reg); 00778 else 00779 dbgs() << PrintRegUnit(LI.reg, &TRI); 00780 dbgs() << ":\t" << LI << '\n'; 00781 }); 00782 if (SlotIndex::isEarlierInstr(OldIdx, NewIdx)) 00783 handleMoveDown(LI); 00784 else 00785 handleMoveUp(LI); 00786 DEBUG(dbgs() << " -->\t" << LI << '\n'); 00787 LI.verify(); 00788 } 00789 00790 /// Update LI to reflect an instruction has been moved downwards from OldIdx 00791 /// to NewIdx. 00792 /// 00793 /// 1. Live def at OldIdx: 00794 /// Move def to NewIdx, assert endpoint after NewIdx. 00795 /// 00796 /// 2. Live def at OldIdx, killed at NewIdx: 00797 /// Change to dead def at NewIdx. 00798 /// (Happens when bundling def+kill together). 00799 /// 00800 /// 3. Dead def at OldIdx: 00801 /// Move def to NewIdx, possibly across another live value. 00802 /// 00803 /// 4. Def at OldIdx AND at NewIdx: 00804 /// Remove live range [OldIdx;NewIdx) and value defined at OldIdx. 00805 /// (Happens when bundling multiple defs together). 00806 /// 00807 /// 5. Value read at OldIdx, killed before NewIdx: 00808 /// Extend kill to NewIdx. 00809 /// 00810 void handleMoveDown(LiveInterval &LI) { 00811 // First look for a kill at OldIdx. 00812 LiveInterval::iterator I = LI.find(OldIdx.getBaseIndex()); 00813 LiveInterval::iterator E = LI.end(); 00814 // Is LI even live at OldIdx? 00815 if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start)) 00816 return; 00817 00818 // Handle a live-in value. 00819 if (!SlotIndex::isSameInstr(I->start, OldIdx)) { 00820 bool isKill = SlotIndex::isSameInstr(OldIdx, I->end); 00821 // If the live-in value already extends to NewIdx, there is nothing to do. 00822 if (!SlotIndex::isEarlierInstr(I->end, NewIdx)) 00823 return; 00824 // Aggressively remove all kill flags from the old kill point. 00825 // Kill flags shouldn't be used while live intervals exist, they will be 00826 // reinserted by VirtRegRewriter. 00827 if (MachineInstr *KillMI = LIS.getInstructionFromIndex(I->end)) 00828 for (MIBundleOperands MO(KillMI); MO.isValid(); ++MO) 00829 if (MO->isReg() && MO->isUse()) 00830 MO->setIsKill(false); 00831 // Adjust I->end to reach NewIdx. This may temporarily make LI invalid by 00832 // overlapping ranges. Case 5 above. 00833 I->end = NewIdx.getRegSlot(I->end.isEarlyClobber()); 00834 // If this was a kill, there may also be a def. Otherwise we're done. 00835 if (!isKill) 00836 return; 00837 ++I; 00838 } 00839 00840 // Check for a def at OldIdx. 00841 if (I == E || !SlotIndex::isSameInstr(OldIdx, I->start)) 00842 return; 00843 // We have a def at OldIdx. 00844 VNInfo *DefVNI = I->valno; 00845 assert(DefVNI->def == I->start && "Inconsistent def"); 00846 DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber()); 00847 // If the defined value extends beyond NewIdx, just move the def down. 00848 // This is case 1 above. 00849 if (SlotIndex::isEarlierInstr(NewIdx, I->end)) { 00850 I->start = DefVNI->def; 00851 return; 00852 } 00853 // The remaining possibilities are now: 00854 // 2. Live def at OldIdx, killed at NewIdx: isSameInstr(I->end, NewIdx). 00855 // 3. Dead def at OldIdx: I->end = OldIdx.getDeadSlot(). 00856 // In either case, it is possible that there is an existing def at NewIdx. 00857 assert((I->end == OldIdx.getDeadSlot() || 00858 SlotIndex::isSameInstr(I->end, NewIdx)) && 00859 "Cannot move def below kill"); 00860 LiveInterval::iterator NewI = LI.advanceTo(I, NewIdx.getRegSlot()); 00861 if (NewI != E && SlotIndex::isSameInstr(NewI->start, NewIdx)) { 00862 // There is an existing def at NewIdx, case 4 above. The def at OldIdx is 00863 // coalesced into that value. 00864 assert(NewI->valno != DefVNI && "Multiple defs of value?"); 00865 LI.removeValNo(DefVNI); 00866 return; 00867 } 00868 // There was no existing def at NewIdx. Turn *I into a dead def at NewIdx. 00869 // If the def at OldIdx was dead, we allow it to be moved across other LI 00870 // values. The new range should be placed immediately before NewI, move any 00871 // intermediate ranges up. 00872 assert(NewI != I && "Inconsistent iterators"); 00873 std::copy(llvm::next(I), NewI, I); 00874 *llvm::prior(NewI) = LiveRange(DefVNI->def, NewIdx.getDeadSlot(), DefVNI); 00875 } 00876 00877 /// Update LI to reflect an instruction has been moved upwards from OldIdx 00878 /// to NewIdx. 00879 /// 00880 /// 1. Live def at OldIdx: 00881 /// Hoist def to NewIdx. 00882 /// 00883 /// 2. Dead def at OldIdx: 00884 /// Hoist def+end to NewIdx, possibly move across other values. 00885 /// 00886 /// 3. Dead def at OldIdx AND existing def at NewIdx: 00887 /// Remove value defined at OldIdx, coalescing it with existing value. 00888 /// 00889 /// 4. Live def at OldIdx AND existing def at NewIdx: 00890 /// Remove value defined at NewIdx, hoist OldIdx def to NewIdx. 00891 /// (Happens when bundling multiple defs together). 00892 /// 00893 /// 5. Value killed at OldIdx: 00894 /// Hoist kill to NewIdx, then scan for last kill between NewIdx and 00895 /// OldIdx. 00896 /// 00897 void handleMoveUp(LiveInterval &LI) { 00898 // First look for a kill at OldIdx. 00899 LiveInterval::iterator I = LI.find(OldIdx.getBaseIndex()); 00900 LiveInterval::iterator E = LI.end(); 00901 // Is LI even live at OldIdx? 00902 if (I == E || SlotIndex::isEarlierInstr(OldIdx, I->start)) 00903 return; 00904 00905 // Handle a live-in value. 00906 if (!SlotIndex::isSameInstr(I->start, OldIdx)) { 00907 // If the live-in value isn't killed here, there is nothing to do. 00908 if (!SlotIndex::isSameInstr(OldIdx, I->end)) 00909 return; 00910 // Adjust I->end to end at NewIdx. If we are hoisting a kill above 00911 // another use, we need to search for that use. Case 5 above. 00912 I->end = NewIdx.getRegSlot(I->end.isEarlyClobber()); 00913 ++I; 00914 // If OldIdx also defines a value, there couldn't have been another use. 00915 if (I == E || !SlotIndex::isSameInstr(I->start, OldIdx)) { 00916 // No def, search for the new kill. 00917 // This can never be an early clobber kill since there is no def. 00918 llvm::prior(I)->end = findLastUseBefore(LI.reg).getRegSlot(); 00919 return; 00920 } 00921 } 00922 00923 // Now deal with the def at OldIdx. 00924 assert(I != E && SlotIndex::isSameInstr(I->start, OldIdx) && "No def?"); 00925 VNInfo *DefVNI = I->valno; 00926 assert(DefVNI->def == I->start && "Inconsistent def"); 00927 DefVNI->def = NewIdx.getRegSlot(I->start.isEarlyClobber()); 00928 00929 // Check for an existing def at NewIdx. 00930 LiveInterval::iterator NewI = LI.find(NewIdx.getRegSlot()); 00931 if (SlotIndex::isSameInstr(NewI->start, NewIdx)) { 00932 assert(NewI->valno != DefVNI && "Same value defined more than once?"); 00933 // There is an existing def at NewIdx. 00934 if (I->end.isDead()) { 00935 // Case 3: Remove the dead def at OldIdx. 00936 LI.removeValNo(DefVNI); 00937 return; 00938 } 00939 // Case 4: Replace def at NewIdx with live def at OldIdx. 00940 I->start = DefVNI->def; 00941 LI.removeValNo(NewI->valno); 00942 return; 00943 } 00944 00945 // There is no existing def at NewIdx. Hoist DefVNI. 00946 if (!I->end.isDead()) { 00947 // Leave the end point of a live def. 00948 I->start = DefVNI->def; 00949 return; 00950 } 00951 00952 // DefVNI is a dead def. It may have been moved across other values in LI, 00953 // so move I up to NewI. Slide [NewI;I) down one position. 00954 std::copy_backward(NewI, I, llvm::next(I)); 00955 *NewI = LiveRange(DefVNI->def, NewIdx.getDeadSlot(), DefVNI); 00956 } 00957 00958 void updateRegMaskSlots() { 00959 SmallVectorImpl<SlotIndex>::iterator RI = 00960 std::lower_bound(LIS.RegMaskSlots.begin(), LIS.RegMaskSlots.end(), 00961 OldIdx); 00962 assert(RI != LIS.RegMaskSlots.end() && *RI == OldIdx.getRegSlot() && 00963 "No RegMask at OldIdx."); 00964 *RI = NewIdx.getRegSlot(); 00965 assert((RI == LIS.RegMaskSlots.begin() || 00966 SlotIndex::isEarlierInstr(*llvm::prior(RI), *RI)) && 00967 "Cannot move regmask instruction above another call"); 00968 assert((llvm::next(RI) == LIS.RegMaskSlots.end() || 00969 SlotIndex::isEarlierInstr(*RI, *llvm::next(RI))) && 00970 "Cannot move regmask instruction below another call"); 00971 } 00972 00973 // Return the last use of reg between NewIdx and OldIdx. 00974 SlotIndex findLastUseBefore(unsigned Reg) { 00975 00976 if (TargetRegisterInfo::isVirtualRegister(Reg)) { 00977 SlotIndex LastUse = NewIdx; 00978 for (MachineRegisterInfo::use_nodbg_iterator 00979 UI = MRI.use_nodbg_begin(Reg), 00980 UE = MRI.use_nodbg_end(); 00981 UI != UE; UI.skipInstruction()) { 00982 const MachineInstr* MI = &*UI; 00983 SlotIndex InstSlot = LIS.getSlotIndexes()->getInstructionIndex(MI); 00984 if (InstSlot > LastUse && InstSlot < OldIdx) 00985 LastUse = InstSlot; 00986 } 00987 return LastUse; 00988 } 00989 00990 // This is a regunit interval, so scanning the use list could be very 00991 // expensive. Scan upwards from OldIdx instead. 00992 assert(NewIdx < OldIdx && "Expected upwards move"); 00993 SlotIndexes *Indexes = LIS.getSlotIndexes(); 00994 MachineBasicBlock *MBB = Indexes->getMBBFromIndex(NewIdx); 00995 00996 // OldIdx may not correspond to an instruction any longer, so set MII to 00997 // point to the next instruction after OldIdx, or MBB->end(). 00998 MachineBasicBlock::iterator MII = MBB->end(); 00999 if (MachineInstr *MI = Indexes->getInstructionFromIndex( 01000 Indexes->getNextNonNullIndex(OldIdx))) 01001 if (MI->getParent() == MBB) 01002 MII = MI; 01003 01004 MachineBasicBlock::iterator Begin = MBB->begin(); 01005 while (MII != Begin) { 01006 if ((--MII)->isDebugValue()) 01007 continue; 01008 SlotIndex Idx = Indexes->getInstructionIndex(MII); 01009 01010 // Stop searching when NewIdx is reached. 01011 if (!SlotIndex::isEarlierInstr(NewIdx, Idx)) 01012 return NewIdx; 01013 01014 // Check if MII uses Reg. 01015 for (MIBundleOperands MO(MII); MO.isValid(); ++MO) 01016 if (MO->isReg() && 01017 TargetRegisterInfo::isPhysicalRegister(MO->getReg()) && 01018 TRI.hasRegUnit(MO->getReg(), Reg)) 01019 return Idx; 01020 } 01021 // Didn't reach NewIdx. It must be the first instruction in the block. 01022 return NewIdx; 01023 } 01024 }; 01025 01026 void LiveIntervals::handleMove(MachineInstr* MI, bool UpdateFlags) { 01027 assert(!MI->isBundled() && "Can't handle bundled instructions yet."); 01028 SlotIndex OldIndex = Indexes->getInstructionIndex(MI); 01029 Indexes->removeMachineInstrFromMaps(MI); 01030 SlotIndex NewIndex = Indexes->insertMachineInstrInMaps(MI); 01031 assert(getMBBStartIdx(MI->getParent()) <= OldIndex && 01032 OldIndex < getMBBEndIdx(MI->getParent()) && 01033 "Cannot handle moves across basic block boundaries."); 01034 01035 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); 01036 HME.updateAllRanges(MI); 01037 } 01038 01039 void LiveIntervals::handleMoveIntoBundle(MachineInstr* MI, 01040 MachineInstr* BundleStart, 01041 bool UpdateFlags) { 01042 SlotIndex OldIndex = Indexes->getInstructionIndex(MI); 01043 SlotIndex NewIndex = Indexes->getInstructionIndex(BundleStart); 01044 HMEditor HME(*this, *MRI, *TRI, OldIndex, NewIndex, UpdateFlags); 01045 HME.updateAllRanges(MI); 01046 } 01047 01048 void 01049 LiveIntervals::repairIntervalsInRange(MachineBasicBlock *MBB, 01050 MachineBasicBlock::iterator Begin, 01051 MachineBasicBlock::iterator End, 01052 ArrayRef<unsigned> OrigRegs) { 01053 // Find anchor points, which are at the beginning/end of blocks or at 01054 // instructions that already have indexes. 01055 while (Begin != MBB->begin() && !Indexes->hasIndex(Begin)) 01056 --Begin; 01057 while (End != MBB->end() && !Indexes->hasIndex(End)) 01058 ++End; 01059 01060 SlotIndex endIdx; 01061 if (End == MBB->end()) 01062 endIdx = getMBBEndIdx(MBB).getPrevSlot(); 01063 else 01064 endIdx = getInstructionIndex(End); 01065 01066 Indexes->repairIndexesInRange(MBB, Begin, End); 01067 01068 for (MachineBasicBlock::iterator I = End; I != Begin;) { 01069 --I; 01070 MachineInstr *MI = I; 01071 if (MI->isDebugValue()) 01072 continue; 01073 for (MachineInstr::const_mop_iterator MOI = MI->operands_begin(), 01074 MOE = MI->operands_end(); MOI != MOE; ++MOI) { 01075 if (MOI->isReg() && 01076 TargetRegisterInfo::isVirtualRegister(MOI->getReg()) && 01077 !hasInterval(MOI->getReg())) { 01078 LiveInterval &LI = getOrCreateInterval(MOI->getReg()); 01079 computeVirtRegInterval(&LI); 01080 } 01081 } 01082 } 01083 01084 for (unsigned i = 0, e = OrigRegs.size(); i != e; ++i) { 01085 unsigned Reg = OrigRegs[i]; 01086 if (!TargetRegisterInfo::isVirtualRegister(Reg)) 01087 continue; 01088 01089 LiveInterval &LI = getInterval(Reg); 01090 // FIXME: Should we support undefs that gain defs? 01091 if (!LI.hasAtLeastOneValue()) 01092 continue; 01093 01094 LiveInterval::iterator LII = LI.find(endIdx); 01095 SlotIndex lastUseIdx; 01096 if (LII != LI.end() && LII->start < endIdx) 01097 lastUseIdx = LII->end; 01098 else 01099 --LII; 01100 01101 for (MachineBasicBlock::iterator I = End; I != Begin;) { 01102 --I; 01103 MachineInstr *MI = I; 01104 if (MI->isDebugValue()) 01105 continue; 01106 01107 SlotIndex instrIdx = getInstructionIndex(MI); 01108 bool isStartValid = getInstructionFromIndex(LII->start); 01109 bool isEndValid = getInstructionFromIndex(LII->end); 01110 01111 // FIXME: This doesn't currently handle early-clobber or multiple removed 01112 // defs inside of the region to repair. 01113 for (MachineInstr::mop_iterator OI = MI->operands_begin(), 01114 OE = MI->operands_end(); OI != OE; ++OI) { 01115 const MachineOperand &MO = *OI; 01116 if (!MO.isReg() || MO.getReg() != Reg) 01117 continue; 01118 01119 if (MO.isDef()) { 01120 if (!isStartValid) { 01121 if (LII->end.isDead()) { 01122 SlotIndex prevStart; 01123 if (LII != LI.begin()) 01124 prevStart = llvm::prior(LII)->start; 01125 01126 // FIXME: This could be more efficient if there was a removeRange 01127 // method that returned an iterator. 01128 LI.removeRange(*LII, true); 01129 if (prevStart.isValid()) 01130 LII = LI.find(prevStart); 01131 else 01132 LII = LI.begin(); 01133 } else { 01134 LII->start = instrIdx.getRegSlot(); 01135 LII->valno->def = instrIdx.getRegSlot(); 01136 if (MO.getSubReg() && !MO.isUndef()) 01137 lastUseIdx = instrIdx.getRegSlot(); 01138 else 01139 lastUseIdx = SlotIndex(); 01140 continue; 01141 } 01142 } 01143 01144 if (!lastUseIdx.isValid()) { 01145 VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(), 01146 VNInfoAllocator); 01147 LiveRange LR(instrIdx.getRegSlot(), instrIdx.getDeadSlot(), VNI); 01148 LII = LI.addRange(LR); 01149 } else if (LII->start != instrIdx.getRegSlot()) { 01150 VNInfo *VNI = LI.getNextValue(instrIdx.getRegSlot(), 01151 VNInfoAllocator); 01152 LiveRange LR(instrIdx.getRegSlot(), lastUseIdx, VNI); 01153 LII = LI.addRange(LR); 01154 } 01155 01156 if (MO.getSubReg() && !MO.isUndef()) 01157 lastUseIdx = instrIdx.getRegSlot(); 01158 else 01159 lastUseIdx = SlotIndex(); 01160 } else if (MO.isUse()) { 01161 // FIXME: This should probably be handled outside of this branch, 01162 // either as part of the def case (for defs inside of the region) or 01163 // after the loop over the region. 01164 if (!isEndValid && !LII->end.isBlock()) 01165 LII->end = instrIdx.getRegSlot(); 01166 if (!lastUseIdx.isValid()) 01167 lastUseIdx = instrIdx.getRegSlot(); 01168 } 01169 } 01170 } 01171 } 01172 }