LLVM API Documentation
00001 //===-- RegAllocGreedy.cpp - greedy register allocator --------------------===// 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 defines the RAGreedy function pass for register allocation in 00011 // optimized builds. 00012 // 00013 //===----------------------------------------------------------------------===// 00014 00015 #define DEBUG_TYPE "regalloc" 00016 #include "llvm/CodeGen/Passes.h" 00017 #include "AllocationOrder.h" 00018 #include "InterferenceCache.h" 00019 #include "LiveDebugVariables.h" 00020 #include "RegAllocBase.h" 00021 #include "SpillPlacement.h" 00022 #include "Spiller.h" 00023 #include "SplitKit.h" 00024 #include "llvm/ADT/Statistic.h" 00025 #include "llvm/Analysis/AliasAnalysis.h" 00026 #include "llvm/CodeGen/CalcSpillWeights.h" 00027 #include "llvm/CodeGen/EdgeBundles.h" 00028 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00029 #include "llvm/CodeGen/LiveRangeEdit.h" 00030 #include "llvm/CodeGen/LiveRegMatrix.h" 00031 #include "llvm/CodeGen/LiveStackAnalysis.h" 00032 #include "llvm/CodeGen/MachineDominators.h" 00033 #include "llvm/CodeGen/MachineFunctionPass.h" 00034 #include "llvm/CodeGen/MachineLoopInfo.h" 00035 #include "llvm/CodeGen/MachineRegisterInfo.h" 00036 #include "llvm/CodeGen/RegAllocRegistry.h" 00037 #include "llvm/CodeGen/VirtRegMap.h" 00038 #include "llvm/PassAnalysisSupport.h" 00039 #include "llvm/Support/CommandLine.h" 00040 #include "llvm/Support/Debug.h" 00041 #include "llvm/Support/ErrorHandling.h" 00042 #include "llvm/Support/Timer.h" 00043 #include "llvm/Support/raw_ostream.h" 00044 #include <queue> 00045 00046 using namespace llvm; 00047 00048 STATISTIC(NumGlobalSplits, "Number of split global live ranges"); 00049 STATISTIC(NumLocalSplits, "Number of split local live ranges"); 00050 STATISTIC(NumEvicted, "Number of interferences evicted"); 00051 00052 static cl::opt<SplitEditor::ComplementSpillMode> 00053 SplitSpillMode("split-spill-mode", cl::Hidden, 00054 cl::desc("Spill mode for splitting live ranges"), 00055 cl::values(clEnumValN(SplitEditor::SM_Partition, "default", "Default"), 00056 clEnumValN(SplitEditor::SM_Size, "size", "Optimize for size"), 00057 clEnumValN(SplitEditor::SM_Speed, "speed", "Optimize for speed"), 00058 clEnumValEnd), 00059 cl::init(SplitEditor::SM_Partition)); 00060 00061 static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", 00062 createGreedyRegisterAllocator); 00063 00064 namespace { 00065 class RAGreedy : public MachineFunctionPass, 00066 public RegAllocBase, 00067 private LiveRangeEdit::Delegate { 00068 00069 // context 00070 MachineFunction *MF; 00071 00072 // analyses 00073 SlotIndexes *Indexes; 00074 MachineDominatorTree *DomTree; 00075 MachineLoopInfo *Loops; 00076 EdgeBundles *Bundles; 00077 SpillPlacement *SpillPlacer; 00078 LiveDebugVariables *DebugVars; 00079 00080 // state 00081 OwningPtr<Spiller> SpillerInstance; 00082 std::priority_queue<std::pair<unsigned, unsigned> > Queue; 00083 unsigned NextCascade; 00084 00085 // Live ranges pass through a number of stages as we try to allocate them. 00086 // Some of the stages may also create new live ranges: 00087 // 00088 // - Region splitting. 00089 // - Per-block splitting. 00090 // - Local splitting. 00091 // - Spilling. 00092 // 00093 // Ranges produced by one of the stages skip the previous stages when they are 00094 // dequeued. This improves performance because we can skip interference checks 00095 // that are unlikely to give any results. It also guarantees that the live 00096 // range splitting algorithm terminates, something that is otherwise hard to 00097 // ensure. 00098 enum LiveRangeStage { 00099 /// Newly created live range that has never been queued. 00100 RS_New, 00101 00102 /// Only attempt assignment and eviction. Then requeue as RS_Split. 00103 RS_Assign, 00104 00105 /// Attempt live range splitting if assignment is impossible. 00106 RS_Split, 00107 00108 /// Attempt more aggressive live range splitting that is guaranteed to make 00109 /// progress. This is used for split products that may not be making 00110 /// progress. 00111 RS_Split2, 00112 00113 /// Live range will be spilled. No more splitting will be attempted. 00114 RS_Spill, 00115 00116 /// There is nothing more we can do to this live range. Abort compilation 00117 /// if it can't be assigned. 00118 RS_Done 00119 }; 00120 00121 static const char *const StageName[]; 00122 00123 // RegInfo - Keep additional information about each live range. 00124 struct RegInfo { 00125 LiveRangeStage Stage; 00126 00127 // Cascade - Eviction loop prevention. See canEvictInterference(). 00128 unsigned Cascade; 00129 00130 RegInfo() : Stage(RS_New), Cascade(0) {} 00131 }; 00132 00133 IndexedMap<RegInfo, VirtReg2IndexFunctor> ExtraRegInfo; 00134 00135 LiveRangeStage getStage(const LiveInterval &VirtReg) const { 00136 return ExtraRegInfo[VirtReg.reg].Stage; 00137 } 00138 00139 void setStage(const LiveInterval &VirtReg, LiveRangeStage Stage) { 00140 ExtraRegInfo.resize(MRI->getNumVirtRegs()); 00141 ExtraRegInfo[VirtReg.reg].Stage = Stage; 00142 } 00143 00144 template<typename Iterator> 00145 void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) { 00146 ExtraRegInfo.resize(MRI->getNumVirtRegs()); 00147 for (;Begin != End; ++Begin) { 00148 unsigned Reg = (*Begin)->reg; 00149 if (ExtraRegInfo[Reg].Stage == RS_New) 00150 ExtraRegInfo[Reg].Stage = NewStage; 00151 } 00152 } 00153 00154 /// Cost of evicting interference. 00155 struct EvictionCost { 00156 unsigned BrokenHints; ///< Total number of broken hints. 00157 float MaxWeight; ///< Maximum spill weight evicted. 00158 00159 EvictionCost(unsigned B = 0) : BrokenHints(B), MaxWeight(0) {} 00160 00161 bool operator<(const EvictionCost &O) const { 00162 if (BrokenHints != O.BrokenHints) 00163 return BrokenHints < O.BrokenHints; 00164 return MaxWeight < O.MaxWeight; 00165 } 00166 }; 00167 00168 // splitting state. 00169 OwningPtr<SplitAnalysis> SA; 00170 OwningPtr<SplitEditor> SE; 00171 00172 /// Cached per-block interference maps 00173 InterferenceCache IntfCache; 00174 00175 /// All basic blocks where the current register has uses. 00176 SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints; 00177 00178 /// Global live range splitting candidate info. 00179 struct GlobalSplitCandidate { 00180 // Register intended for assignment, or 0. 00181 unsigned PhysReg; 00182 00183 // SplitKit interval index for this candidate. 00184 unsigned IntvIdx; 00185 00186 // Interference for PhysReg. 00187 InterferenceCache::Cursor Intf; 00188 00189 // Bundles where this candidate should be live. 00190 BitVector LiveBundles; 00191 SmallVector<unsigned, 8> ActiveBlocks; 00192 00193 void reset(InterferenceCache &Cache, unsigned Reg) { 00194 PhysReg = Reg; 00195 IntvIdx = 0; 00196 Intf.setPhysReg(Cache, Reg); 00197 LiveBundles.clear(); 00198 ActiveBlocks.clear(); 00199 } 00200 00201 // Set B[i] = C for every live bundle where B[i] was NoCand. 00202 unsigned getBundles(SmallVectorImpl<unsigned> &B, unsigned C) { 00203 unsigned Count = 0; 00204 for (int i = LiveBundles.find_first(); i >= 0; 00205 i = LiveBundles.find_next(i)) 00206 if (B[i] == NoCand) { 00207 B[i] = C; 00208 Count++; 00209 } 00210 return Count; 00211 } 00212 }; 00213 00214 /// Candidate info for for each PhysReg in AllocationOrder. 00215 /// This vector never shrinks, but grows to the size of the largest register 00216 /// class. 00217 SmallVector<GlobalSplitCandidate, 32> GlobalCand; 00218 00219 enum { NoCand = ~0u }; 00220 00221 /// Candidate map. Each edge bundle is assigned to a GlobalCand entry, or to 00222 /// NoCand which indicates the stack interval. 00223 SmallVector<unsigned, 32> BundleCand; 00224 00225 public: 00226 RAGreedy(); 00227 00228 /// Return the pass name. 00229 virtual const char* getPassName() const { 00230 return "Greedy Register Allocator"; 00231 } 00232 00233 /// RAGreedy analysis usage. 00234 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 00235 virtual void releaseMemory(); 00236 virtual Spiller &spiller() { return *SpillerInstance; } 00237 virtual void enqueue(LiveInterval *LI); 00238 virtual LiveInterval *dequeue(); 00239 virtual unsigned selectOrSplit(LiveInterval&, 00240 SmallVectorImpl<LiveInterval*>&); 00241 00242 /// Perform register allocation. 00243 virtual bool runOnMachineFunction(MachineFunction &mf); 00244 00245 static char ID; 00246 00247 private: 00248 bool LRE_CanEraseVirtReg(unsigned); 00249 void LRE_WillShrinkVirtReg(unsigned); 00250 void LRE_DidCloneVirtReg(unsigned, unsigned); 00251 00252 float calcSpillCost(); 00253 bool addSplitConstraints(InterferenceCache::Cursor, float&); 00254 void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>); 00255 void growRegion(GlobalSplitCandidate &Cand); 00256 float calcGlobalSplitCost(GlobalSplitCandidate&); 00257 bool calcCompactRegion(GlobalSplitCandidate&); 00258 void splitAroundRegion(LiveRangeEdit&, ArrayRef<unsigned>); 00259 void calcGapWeights(unsigned, SmallVectorImpl<float>&); 00260 bool shouldEvict(LiveInterval &A, bool, LiveInterval &B, bool); 00261 bool canEvictInterference(LiveInterval&, unsigned, bool, EvictionCost&); 00262 void evictInterference(LiveInterval&, unsigned, 00263 SmallVectorImpl<LiveInterval*>&); 00264 00265 unsigned tryAssign(LiveInterval&, AllocationOrder&, 00266 SmallVectorImpl<LiveInterval*>&); 00267 unsigned tryEvict(LiveInterval&, AllocationOrder&, 00268 SmallVectorImpl<LiveInterval*>&, unsigned = ~0u); 00269 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, 00270 SmallVectorImpl<LiveInterval*>&); 00271 unsigned tryBlockSplit(LiveInterval&, AllocationOrder&, 00272 SmallVectorImpl<LiveInterval*>&); 00273 unsigned tryInstructionSplit(LiveInterval&, AllocationOrder&, 00274 SmallVectorImpl<LiveInterval*>&); 00275 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, 00276 SmallVectorImpl<LiveInterval*>&); 00277 unsigned trySplit(LiveInterval&, AllocationOrder&, 00278 SmallVectorImpl<LiveInterval*>&); 00279 }; 00280 } // end anonymous namespace 00281 00282 char RAGreedy::ID = 0; 00283 00284 #ifndef NDEBUG 00285 const char *const RAGreedy::StageName[] = { 00286 "RS_New", 00287 "RS_Assign", 00288 "RS_Split", 00289 "RS_Split2", 00290 "RS_Spill", 00291 "RS_Done" 00292 }; 00293 #endif 00294 00295 // Hysteresis to use when comparing floats. 00296 // This helps stabilize decisions based on float comparisons. 00297 const float Hysteresis = 0.98f; 00298 00299 00300 FunctionPass* llvm::createGreedyRegisterAllocator() { 00301 return new RAGreedy(); 00302 } 00303 00304 RAGreedy::RAGreedy(): MachineFunctionPass(ID) { 00305 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 00306 initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 00307 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 00308 initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 00309 initializeRegisterCoalescerPass(*PassRegistry::getPassRegistry()); 00310 initializeMachineSchedulerPass(*PassRegistry::getPassRegistry()); 00311 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); 00312 initializeLiveStacksPass(*PassRegistry::getPassRegistry()); 00313 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 00314 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 00315 initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); 00316 initializeLiveRegMatrixPass(*PassRegistry::getPassRegistry()); 00317 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); 00318 initializeSpillPlacementPass(*PassRegistry::getPassRegistry()); 00319 } 00320 00321 void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { 00322 AU.setPreservesCFG(); 00323 AU.addRequired<AliasAnalysis>(); 00324 AU.addPreserved<AliasAnalysis>(); 00325 AU.addRequired<LiveIntervals>(); 00326 AU.addPreserved<LiveIntervals>(); 00327 AU.addRequired<SlotIndexes>(); 00328 AU.addPreserved<SlotIndexes>(); 00329 AU.addRequired<LiveDebugVariables>(); 00330 AU.addPreserved<LiveDebugVariables>(); 00331 AU.addRequired<LiveStacks>(); 00332 AU.addPreserved<LiveStacks>(); 00333 AU.addRequired<CalculateSpillWeights>(); 00334 AU.addRequired<MachineDominatorTree>(); 00335 AU.addPreserved<MachineDominatorTree>(); 00336 AU.addRequired<MachineLoopInfo>(); 00337 AU.addPreserved<MachineLoopInfo>(); 00338 AU.addRequired<VirtRegMap>(); 00339 AU.addPreserved<VirtRegMap>(); 00340 AU.addRequired<LiveRegMatrix>(); 00341 AU.addPreserved<LiveRegMatrix>(); 00342 AU.addRequired<EdgeBundles>(); 00343 AU.addRequired<SpillPlacement>(); 00344 MachineFunctionPass::getAnalysisUsage(AU); 00345 } 00346 00347 00348 //===----------------------------------------------------------------------===// 00349 // LiveRangeEdit delegate methods 00350 //===----------------------------------------------------------------------===// 00351 00352 bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { 00353 if (VRM->hasPhys(VirtReg)) { 00354 Matrix->unassign(LIS->getInterval(VirtReg)); 00355 return true; 00356 } 00357 // Unassigned virtreg is probably in the priority queue. 00358 // RegAllocBase will erase it after dequeueing. 00359 return false; 00360 } 00361 00362 void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { 00363 if (!VRM->hasPhys(VirtReg)) 00364 return; 00365 00366 // Register is assigned, put it back on the queue for reassignment. 00367 LiveInterval &LI = LIS->getInterval(VirtReg); 00368 Matrix->unassign(LI); 00369 enqueue(&LI); 00370 } 00371 00372 void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { 00373 // Cloning a register we haven't even heard about yet? Just ignore it. 00374 if (!ExtraRegInfo.inBounds(Old)) 00375 return; 00376 00377 // LRE may clone a virtual register because dead code elimination causes it to 00378 // be split into connected components. The new components are much smaller 00379 // than the original, so they should get a new chance at being assigned. 00380 // same stage as the parent. 00381 ExtraRegInfo[Old].Stage = RS_Assign; 00382 ExtraRegInfo.grow(New); 00383 ExtraRegInfo[New] = ExtraRegInfo[Old]; 00384 } 00385 00386 void RAGreedy::releaseMemory() { 00387 SpillerInstance.reset(0); 00388 ExtraRegInfo.clear(); 00389 GlobalCand.clear(); 00390 } 00391 00392 void RAGreedy::enqueue(LiveInterval *LI) { 00393 // Prioritize live ranges by size, assigning larger ranges first. 00394 // The queue holds (size, reg) pairs. 00395 const unsigned Size = LI->getSize(); 00396 const unsigned Reg = LI->reg; 00397 assert(TargetRegisterInfo::isVirtualRegister(Reg) && 00398 "Can only enqueue virtual registers"); 00399 unsigned Prio; 00400 00401 ExtraRegInfo.grow(Reg); 00402 if (ExtraRegInfo[Reg].Stage == RS_New) 00403 ExtraRegInfo[Reg].Stage = RS_Assign; 00404 00405 if (ExtraRegInfo[Reg].Stage == RS_Split) { 00406 // Unsplit ranges that couldn't be allocated immediately are deferred until 00407 // everything else has been allocated. 00408 Prio = Size; 00409 } else { 00410 // Everything is allocated in long->short order. Long ranges that don't fit 00411 // should be spilled (or split) ASAP so they don't create interference. 00412 Prio = (1u << 31) + Size; 00413 00414 // Boost ranges that have a physical register hint. 00415 if (VRM->hasKnownPreference(Reg)) 00416 Prio |= (1u << 30); 00417 } 00418 00419 Queue.push(std::make_pair(Prio, ~Reg)); 00420 } 00421 00422 LiveInterval *RAGreedy::dequeue() { 00423 if (Queue.empty()) 00424 return 0; 00425 LiveInterval *LI = &LIS->getInterval(~Queue.top().second); 00426 Queue.pop(); 00427 return LI; 00428 } 00429 00430 00431 //===----------------------------------------------------------------------===// 00432 // Direct Assignment 00433 //===----------------------------------------------------------------------===// 00434 00435 /// tryAssign - Try to assign VirtReg to an available register. 00436 unsigned RAGreedy::tryAssign(LiveInterval &VirtReg, 00437 AllocationOrder &Order, 00438 SmallVectorImpl<LiveInterval*> &NewVRegs) { 00439 Order.rewind(); 00440 unsigned PhysReg; 00441 while ((PhysReg = Order.next())) 00442 if (!Matrix->checkInterference(VirtReg, PhysReg)) 00443 break; 00444 if (!PhysReg || Order.isHint()) 00445 return PhysReg; 00446 00447 // PhysReg is available, but there may be a better choice. 00448 00449 // If we missed a simple hint, try to cheaply evict interference from the 00450 // preferred register. 00451 if (unsigned Hint = MRI->getSimpleHint(VirtReg.reg)) 00452 if (Order.isHint(Hint)) { 00453 DEBUG(dbgs() << "missed hint " << PrintReg(Hint, TRI) << '\n'); 00454 EvictionCost MaxCost(1); 00455 if (canEvictInterference(VirtReg, Hint, true, MaxCost)) { 00456 evictInterference(VirtReg, Hint, NewVRegs); 00457 return Hint; 00458 } 00459 } 00460 00461 // Try to evict interference from a cheaper alternative. 00462 unsigned Cost = TRI->getCostPerUse(PhysReg); 00463 00464 // Most registers have 0 additional cost. 00465 if (!Cost) 00466 return PhysReg; 00467 00468 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " is available at cost " << Cost 00469 << '\n'); 00470 unsigned CheapReg = tryEvict(VirtReg, Order, NewVRegs, Cost); 00471 return CheapReg ? CheapReg : PhysReg; 00472 } 00473 00474 00475 //===----------------------------------------------------------------------===// 00476 // Interference eviction 00477 //===----------------------------------------------------------------------===// 00478 00479 /// shouldEvict - determine if A should evict the assigned live range B. The 00480 /// eviction policy defined by this function together with the allocation order 00481 /// defined by enqueue() decides which registers ultimately end up being split 00482 /// and spilled. 00483 /// 00484 /// Cascade numbers are used to prevent infinite loops if this function is a 00485 /// cyclic relation. 00486 /// 00487 /// @param A The live range to be assigned. 00488 /// @param IsHint True when A is about to be assigned to its preferred 00489 /// register. 00490 /// @param B The live range to be evicted. 00491 /// @param BreaksHint True when B is already assigned to its preferred register. 00492 bool RAGreedy::shouldEvict(LiveInterval &A, bool IsHint, 00493 LiveInterval &B, bool BreaksHint) { 00494 bool CanSplit = getStage(B) < RS_Spill; 00495 00496 // Be fairly aggressive about following hints as long as the evictee can be 00497 // split. 00498 if (CanSplit && IsHint && !BreaksHint) 00499 return true; 00500 00501 return A.weight > B.weight; 00502 } 00503 00504 /// canEvictInterference - Return true if all interferences between VirtReg and 00505 /// PhysReg can be evicted. When OnlyCheap is set, don't do anything 00506 /// 00507 /// @param VirtReg Live range that is about to be assigned. 00508 /// @param PhysReg Desired register for assignment. 00509 /// @param IsHint True when PhysReg is VirtReg's preferred register. 00510 /// @param MaxCost Only look for cheaper candidates and update with new cost 00511 /// when returning true. 00512 /// @returns True when interference can be evicted cheaper than MaxCost. 00513 bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, 00514 bool IsHint, EvictionCost &MaxCost) { 00515 // It is only possible to evict virtual register interference. 00516 if (Matrix->checkInterference(VirtReg, PhysReg) > LiveRegMatrix::IK_VirtReg) 00517 return false; 00518 00519 // Find VirtReg's cascade number. This will be unassigned if VirtReg was never 00520 // involved in an eviction before. If a cascade number was assigned, deny 00521 // evicting anything with the same or a newer cascade number. This prevents 00522 // infinite eviction loops. 00523 // 00524 // This works out so a register without a cascade number is allowed to evict 00525 // anything, and it can be evicted by anything. 00526 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; 00527 if (!Cascade) 00528 Cascade = NextCascade; 00529 00530 EvictionCost Cost; 00531 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 00532 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); 00533 // If there is 10 or more interferences, chances are one is heavier. 00534 if (Q.collectInterferingVRegs(10) >= 10) 00535 return false; 00536 00537 // Check if any interfering live range is heavier than MaxWeight. 00538 for (unsigned i = Q.interferingVRegs().size(); i; --i) { 00539 LiveInterval *Intf = Q.interferingVRegs()[i - 1]; 00540 assert(TargetRegisterInfo::isVirtualRegister(Intf->reg) && 00541 "Only expecting virtual register interference from query"); 00542 // Never evict spill products. They cannot split or spill. 00543 if (getStage(*Intf) == RS_Done) 00544 return false; 00545 // Once a live range becomes small enough, it is urgent that we find a 00546 // register for it. This is indicated by an infinite spill weight. These 00547 // urgent live ranges get to evict almost anything. 00548 // 00549 // Also allow urgent evictions of unspillable ranges from a strictly 00550 // larger allocation order. 00551 bool Urgent = !VirtReg.isSpillable() && 00552 (Intf->isSpillable() || 00553 RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(VirtReg.reg)) < 00554 RegClassInfo.getNumAllocatableRegs(MRI->getRegClass(Intf->reg))); 00555 // Only evict older cascades or live ranges without a cascade. 00556 unsigned IntfCascade = ExtraRegInfo[Intf->reg].Cascade; 00557 if (Cascade <= IntfCascade) { 00558 if (!Urgent) 00559 return false; 00560 // We permit breaking cascades for urgent evictions. It should be the 00561 // last resort, though, so make it really expensive. 00562 Cost.BrokenHints += 10; 00563 } 00564 // Would this break a satisfied hint? 00565 bool BreaksHint = VRM->hasPreferredPhys(Intf->reg); 00566 // Update eviction cost. 00567 Cost.BrokenHints += BreaksHint; 00568 Cost.MaxWeight = std::max(Cost.MaxWeight, Intf->weight); 00569 // Abort if this would be too expensive. 00570 if (!(Cost < MaxCost)) 00571 return false; 00572 // Finally, apply the eviction policy for non-urgent evictions. 00573 if (!Urgent && !shouldEvict(VirtReg, IsHint, *Intf, BreaksHint)) 00574 return false; 00575 } 00576 } 00577 MaxCost = Cost; 00578 return true; 00579 } 00580 00581 /// evictInterference - Evict any interferring registers that prevent VirtReg 00582 /// from being assigned to Physreg. This assumes that canEvictInterference 00583 /// returned true. 00584 void RAGreedy::evictInterference(LiveInterval &VirtReg, unsigned PhysReg, 00585 SmallVectorImpl<LiveInterval*> &NewVRegs) { 00586 // Make sure that VirtReg has a cascade number, and assign that cascade 00587 // number to every evicted register. These live ranges than then only be 00588 // evicted by a newer cascade, preventing infinite loops. 00589 unsigned Cascade = ExtraRegInfo[VirtReg.reg].Cascade; 00590 if (!Cascade) 00591 Cascade = ExtraRegInfo[VirtReg.reg].Cascade = NextCascade++; 00592 00593 DEBUG(dbgs() << "evicting " << PrintReg(PhysReg, TRI) 00594 << " interference: Cascade " << Cascade << '\n'); 00595 00596 // Collect all interfering virtregs first. 00597 SmallVector<LiveInterval*, 8> Intfs; 00598 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 00599 LiveIntervalUnion::Query &Q = Matrix->query(VirtReg, *Units); 00600 assert(Q.seenAllInterferences() && "Didn't check all interfererences."); 00601 ArrayRef<LiveInterval*> IVR = Q.interferingVRegs(); 00602 Intfs.append(IVR.begin(), IVR.end()); 00603 } 00604 00605 // Evict them second. This will invalidate the queries. 00606 for (unsigned i = 0, e = Intfs.size(); i != e; ++i) { 00607 LiveInterval *Intf = Intfs[i]; 00608 // The same VirtReg may be present in multiple RegUnits. Skip duplicates. 00609 if (!VRM->hasPhys(Intf->reg)) 00610 continue; 00611 Matrix->unassign(*Intf); 00612 assert((ExtraRegInfo[Intf->reg].Cascade < Cascade || 00613 VirtReg.isSpillable() < Intf->isSpillable()) && 00614 "Cannot decrease cascade number, illegal eviction"); 00615 ExtraRegInfo[Intf->reg].Cascade = Cascade; 00616 ++NumEvicted; 00617 NewVRegs.push_back(Intf); 00618 } 00619 } 00620 00621 /// tryEvict - Try to evict all interferences for a physreg. 00622 /// @param VirtReg Currently unassigned virtual register. 00623 /// @param Order Physregs to try. 00624 /// @return Physreg to assign VirtReg, or 0. 00625 unsigned RAGreedy::tryEvict(LiveInterval &VirtReg, 00626 AllocationOrder &Order, 00627 SmallVectorImpl<LiveInterval*> &NewVRegs, 00628 unsigned CostPerUseLimit) { 00629 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled); 00630 00631 // Keep track of the cheapest interference seen so far. 00632 EvictionCost BestCost(~0u); 00633 unsigned BestPhys = 0; 00634 unsigned OrderLimit = Order.getOrder().size(); 00635 00636 // When we are just looking for a reduced cost per use, don't break any 00637 // hints, and only evict smaller spill weights. 00638 if (CostPerUseLimit < ~0u) { 00639 BestCost.BrokenHints = 0; 00640 BestCost.MaxWeight = VirtReg.weight; 00641 00642 // Check of any registers in RC are below CostPerUseLimit. 00643 const TargetRegisterClass *RC = MRI->getRegClass(VirtReg.reg); 00644 unsigned MinCost = RegClassInfo.getMinCost(RC); 00645 if (MinCost >= CostPerUseLimit) { 00646 DEBUG(dbgs() << RC->getName() << " minimum cost = " << MinCost 00647 << ", no cheaper registers to be found.\n"); 00648 return 0; 00649 } 00650 00651 // It is normal for register classes to have a long tail of registers with 00652 // the same cost. We don't need to look at them if they're too expensive. 00653 if (TRI->getCostPerUse(Order.getOrder().back()) >= CostPerUseLimit) { 00654 OrderLimit = RegClassInfo.getLastCostChange(RC); 00655 DEBUG(dbgs() << "Only trying the first " << OrderLimit << " regs.\n"); 00656 } 00657 } 00658 00659 Order.rewind(); 00660 while (unsigned PhysReg = Order.nextWithDups(OrderLimit)) { 00661 if (TRI->getCostPerUse(PhysReg) >= CostPerUseLimit) 00662 continue; 00663 // The first use of a callee-saved register in a function has cost 1. 00664 // Don't start using a CSR when the CostPerUseLimit is low. 00665 if (CostPerUseLimit == 1) 00666 if (unsigned CSR = RegClassInfo.getLastCalleeSavedAlias(PhysReg)) 00667 if (!MRI->isPhysRegUsed(CSR)) { 00668 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " would clobber CSR " 00669 << PrintReg(CSR, TRI) << '\n'); 00670 continue; 00671 } 00672 00673 if (!canEvictInterference(VirtReg, PhysReg, false, BestCost)) 00674 continue; 00675 00676 // Best so far. 00677 BestPhys = PhysReg; 00678 00679 // Stop if the hint can be used. 00680 if (Order.isHint()) 00681 break; 00682 } 00683 00684 if (!BestPhys) 00685 return 0; 00686 00687 evictInterference(VirtReg, BestPhys, NewVRegs); 00688 return BestPhys; 00689 } 00690 00691 00692 //===----------------------------------------------------------------------===// 00693 // Region Splitting 00694 //===----------------------------------------------------------------------===// 00695 00696 /// addSplitConstraints - Fill out the SplitConstraints vector based on the 00697 /// interference pattern in Physreg and its aliases. Add the constraints to 00698 /// SpillPlacement and return the static cost of this split in Cost, assuming 00699 /// that all preferences in SplitConstraints are met. 00700 /// Return false if there are no bundles with positive bias. 00701 bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf, 00702 float &Cost) { 00703 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 00704 00705 // Reset interference dependent info. 00706 SplitConstraints.resize(UseBlocks.size()); 00707 float StaticCost = 0; 00708 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 00709 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 00710 SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 00711 00712 BC.Number = BI.MBB->getNumber(); 00713 Intf.moveToBlock(BC.Number); 00714 BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 00715 BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 00716 BC.ChangesValue = BI.FirstDef.isValid(); 00717 00718 if (!Intf.hasInterference()) 00719 continue; 00720 00721 // Number of spill code instructions to insert. 00722 unsigned Ins = 0; 00723 00724 // Interference for the live-in value. 00725 if (BI.LiveIn) { 00726 if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) 00727 BC.Entry = SpillPlacement::MustSpill, ++Ins; 00728 else if (Intf.first() < BI.FirstInstr) 00729 BC.Entry = SpillPlacement::PrefSpill, ++Ins; 00730 else if (Intf.first() < BI.LastInstr) 00731 ++Ins; 00732 } 00733 00734 // Interference for the live-out value. 00735 if (BI.LiveOut) { 00736 if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) 00737 BC.Exit = SpillPlacement::MustSpill, ++Ins; 00738 else if (Intf.last() > BI.LastInstr) 00739 BC.Exit = SpillPlacement::PrefSpill, ++Ins; 00740 else if (Intf.last() > BI.FirstInstr) 00741 ++Ins; 00742 } 00743 00744 // Accumulate the total frequency of inserted spill code. 00745 if (Ins) 00746 StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 00747 } 00748 Cost = StaticCost; 00749 00750 // Add constraints for use-blocks. Note that these are the only constraints 00751 // that may add a positive bias, it is downhill from here. 00752 SpillPlacer->addConstraints(SplitConstraints); 00753 return SpillPlacer->scanActiveBundles(); 00754 } 00755 00756 00757 /// addThroughConstraints - Add constraints and links to SpillPlacer from the 00758 /// live-through blocks in Blocks. 00759 void RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf, 00760 ArrayRef<unsigned> Blocks) { 00761 const unsigned GroupSize = 8; 00762 SpillPlacement::BlockConstraint BCS[GroupSize]; 00763 unsigned TBS[GroupSize]; 00764 unsigned B = 0, T = 0; 00765 00766 for (unsigned i = 0; i != Blocks.size(); ++i) { 00767 unsigned Number = Blocks[i]; 00768 Intf.moveToBlock(Number); 00769 00770 if (!Intf.hasInterference()) { 00771 assert(T < GroupSize && "Array overflow"); 00772 TBS[T] = Number; 00773 if (++T == GroupSize) { 00774 SpillPlacer->addLinks(makeArrayRef(TBS, T)); 00775 T = 0; 00776 } 00777 continue; 00778 } 00779 00780 assert(B < GroupSize && "Array overflow"); 00781 BCS[B].Number = Number; 00782 00783 // Interference for the live-in value. 00784 if (Intf.first() <= Indexes->getMBBStartIdx(Number)) 00785 BCS[B].Entry = SpillPlacement::MustSpill; 00786 else 00787 BCS[B].Entry = SpillPlacement::PrefSpill; 00788 00789 // Interference for the live-out value. 00790 if (Intf.last() >= SA->getLastSplitPoint(Number)) 00791 BCS[B].Exit = SpillPlacement::MustSpill; 00792 else 00793 BCS[B].Exit = SpillPlacement::PrefSpill; 00794 00795 if (++B == GroupSize) { 00796 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); 00797 SpillPlacer->addConstraints(Array); 00798 B = 0; 00799 } 00800 } 00801 00802 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); 00803 SpillPlacer->addConstraints(Array); 00804 SpillPlacer->addLinks(makeArrayRef(TBS, T)); 00805 } 00806 00807 void RAGreedy::growRegion(GlobalSplitCandidate &Cand) { 00808 // Keep track of through blocks that have not been added to SpillPlacer. 00809 BitVector Todo = SA->getThroughBlocks(); 00810 SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks; 00811 unsigned AddedTo = 0; 00812 #ifndef NDEBUG 00813 unsigned Visited = 0; 00814 #endif 00815 00816 for (;;) { 00817 ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive(); 00818 // Find new through blocks in the periphery of PrefRegBundles. 00819 for (int i = 0, e = NewBundles.size(); i != e; ++i) { 00820 unsigned Bundle = NewBundles[i]; 00821 // Look at all blocks connected to Bundle in the full graph. 00822 ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle); 00823 for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end(); 00824 I != E; ++I) { 00825 unsigned Block = *I; 00826 if (!Todo.test(Block)) 00827 continue; 00828 Todo.reset(Block); 00829 // This is a new through block. Add it to SpillPlacer later. 00830 ActiveBlocks.push_back(Block); 00831 #ifndef NDEBUG 00832 ++Visited; 00833 #endif 00834 } 00835 } 00836 // Any new blocks to add? 00837 if (ActiveBlocks.size() == AddedTo) 00838 break; 00839 00840 // Compute through constraints from the interference, or assume that all 00841 // through blocks prefer spilling when forming compact regions. 00842 ArrayRef<unsigned> NewBlocks = makeArrayRef(ActiveBlocks).slice(AddedTo); 00843 if (Cand.PhysReg) 00844 addThroughConstraints(Cand.Intf, NewBlocks); 00845 else 00846 // Provide a strong negative bias on through blocks to prevent unwanted 00847 // liveness on loop backedges. 00848 SpillPlacer->addPrefSpill(NewBlocks, /* Strong= */ true); 00849 AddedTo = ActiveBlocks.size(); 00850 00851 // Perhaps iterating can enable more bundles? 00852 SpillPlacer->iterate(); 00853 } 00854 DEBUG(dbgs() << ", v=" << Visited); 00855 } 00856 00857 /// calcCompactRegion - Compute the set of edge bundles that should be live 00858 /// when splitting the current live range into compact regions. Compact 00859 /// regions can be computed without looking at interference. They are the 00860 /// regions formed by removing all the live-through blocks from the live range. 00861 /// 00862 /// Returns false if the current live range is already compact, or if the 00863 /// compact regions would form single block regions anyway. 00864 bool RAGreedy::calcCompactRegion(GlobalSplitCandidate &Cand) { 00865 // Without any through blocks, the live range is already compact. 00866 if (!SA->getNumThroughBlocks()) 00867 return false; 00868 00869 // Compact regions don't correspond to any physreg. 00870 Cand.reset(IntfCache, 0); 00871 00872 DEBUG(dbgs() << "Compact region bundles"); 00873 00874 // Use the spill placer to determine the live bundles. GrowRegion pretends 00875 // that all the through blocks have interference when PhysReg is unset. 00876 SpillPlacer->prepare(Cand.LiveBundles); 00877 00878 // The static split cost will be zero since Cand.Intf reports no interference. 00879 float Cost; 00880 if (!addSplitConstraints(Cand.Intf, Cost)) { 00881 DEBUG(dbgs() << ", none.\n"); 00882 return false; 00883 } 00884 00885 growRegion(Cand); 00886 SpillPlacer->finish(); 00887 00888 if (!Cand.LiveBundles.any()) { 00889 DEBUG(dbgs() << ", none.\n"); 00890 return false; 00891 } 00892 00893 DEBUG({ 00894 for (int i = Cand.LiveBundles.find_first(); i>=0; 00895 i = Cand.LiveBundles.find_next(i)) 00896 dbgs() << " EB#" << i; 00897 dbgs() << ".\n"; 00898 }); 00899 return true; 00900 } 00901 00902 /// calcSpillCost - Compute how expensive it would be to split the live range in 00903 /// SA around all use blocks instead of forming bundle regions. 00904 float RAGreedy::calcSpillCost() { 00905 float Cost = 0; 00906 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 00907 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 00908 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 00909 unsigned Number = BI.MBB->getNumber(); 00910 // We normally only need one spill instruction - a load or a store. 00911 Cost += SpillPlacer->getBlockFrequency(Number); 00912 00913 // Unless the value is redefined in the block. 00914 if (BI.LiveIn && BI.LiveOut && BI.FirstDef) 00915 Cost += SpillPlacer->getBlockFrequency(Number); 00916 } 00917 return Cost; 00918 } 00919 00920 /// calcGlobalSplitCost - Return the global split cost of following the split 00921 /// pattern in LiveBundles. This cost should be added to the local cost of the 00922 /// interference pattern in SplitConstraints. 00923 /// 00924 float RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand) { 00925 float GlobalCost = 0; 00926 const BitVector &LiveBundles = Cand.LiveBundles; 00927 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 00928 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 00929 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 00930 SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 00931 bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)]; 00932 bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)]; 00933 unsigned Ins = 0; 00934 00935 if (BI.LiveIn) 00936 Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); 00937 if (BI.LiveOut) 00938 Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); 00939 if (Ins) 00940 GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 00941 } 00942 00943 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { 00944 unsigned Number = Cand.ActiveBlocks[i]; 00945 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; 00946 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; 00947 if (!RegIn && !RegOut) 00948 continue; 00949 if (RegIn && RegOut) { 00950 // We need double spill code if this block has interference. 00951 Cand.Intf.moveToBlock(Number); 00952 if (Cand.Intf.hasInterference()) 00953 GlobalCost += 2*SpillPlacer->getBlockFrequency(Number); 00954 continue; 00955 } 00956 // live-in / stack-out or stack-in live-out. 00957 GlobalCost += SpillPlacer->getBlockFrequency(Number); 00958 } 00959 return GlobalCost; 00960 } 00961 00962 /// splitAroundRegion - Split the current live range around the regions 00963 /// determined by BundleCand and GlobalCand. 00964 /// 00965 /// Before calling this function, GlobalCand and BundleCand must be initialized 00966 /// so each bundle is assigned to a valid candidate, or NoCand for the 00967 /// stack-bound bundles. The shared SA/SE SplitAnalysis and SplitEditor 00968 /// objects must be initialized for the current live range, and intervals 00969 /// created for the used candidates. 00970 /// 00971 /// @param LREdit The LiveRangeEdit object handling the current split. 00972 /// @param UsedCands List of used GlobalCand entries. Every BundleCand value 00973 /// must appear in this list. 00974 void RAGreedy::splitAroundRegion(LiveRangeEdit &LREdit, 00975 ArrayRef<unsigned> UsedCands) { 00976 // These are the intervals created for new global ranges. We may create more 00977 // intervals for local ranges. 00978 const unsigned NumGlobalIntvs = LREdit.size(); 00979 DEBUG(dbgs() << "splitAroundRegion with " << NumGlobalIntvs << " globals.\n"); 00980 assert(NumGlobalIntvs && "No global intervals configured"); 00981 00982 // Isolate even single instructions when dealing with a proper sub-class. 00983 // That guarantees register class inflation for the stack interval because it 00984 // is all copies. 00985 unsigned Reg = SA->getParent().reg; 00986 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); 00987 00988 // First handle all the blocks with uses. 00989 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 00990 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 00991 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 00992 unsigned Number = BI.MBB->getNumber(); 00993 unsigned IntvIn = 0, IntvOut = 0; 00994 SlotIndex IntfIn, IntfOut; 00995 if (BI.LiveIn) { 00996 unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)]; 00997 if (CandIn != NoCand) { 00998 GlobalSplitCandidate &Cand = GlobalCand[CandIn]; 00999 IntvIn = Cand.IntvIdx; 01000 Cand.Intf.moveToBlock(Number); 01001 IntfIn = Cand.Intf.first(); 01002 } 01003 } 01004 if (BI.LiveOut) { 01005 unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)]; 01006 if (CandOut != NoCand) { 01007 GlobalSplitCandidate &Cand = GlobalCand[CandOut]; 01008 IntvOut = Cand.IntvIdx; 01009 Cand.Intf.moveToBlock(Number); 01010 IntfOut = Cand.Intf.last(); 01011 } 01012 } 01013 01014 // Create separate intervals for isolated blocks with multiple uses. 01015 if (!IntvIn && !IntvOut) { 01016 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n"); 01017 if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) 01018 SE->splitSingleBlock(BI); 01019 continue; 01020 } 01021 01022 if (IntvIn && IntvOut) 01023 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); 01024 else if (IntvIn) 01025 SE->splitRegInBlock(BI, IntvIn, IntfIn); 01026 else 01027 SE->splitRegOutBlock(BI, IntvOut, IntfOut); 01028 } 01029 01030 // Handle live-through blocks. The relevant live-through blocks are stored in 01031 // the ActiveBlocks list with each candidate. We need to filter out 01032 // duplicates. 01033 BitVector Todo = SA->getThroughBlocks(); 01034 for (unsigned c = 0; c != UsedCands.size(); ++c) { 01035 ArrayRef<unsigned> Blocks = GlobalCand[UsedCands[c]].ActiveBlocks; 01036 for (unsigned i = 0, e = Blocks.size(); i != e; ++i) { 01037 unsigned Number = Blocks[i]; 01038 if (!Todo.test(Number)) 01039 continue; 01040 Todo.reset(Number); 01041 01042 unsigned IntvIn = 0, IntvOut = 0; 01043 SlotIndex IntfIn, IntfOut; 01044 01045 unsigned CandIn = BundleCand[Bundles->getBundle(Number, 0)]; 01046 if (CandIn != NoCand) { 01047 GlobalSplitCandidate &Cand = GlobalCand[CandIn]; 01048 IntvIn = Cand.IntvIdx; 01049 Cand.Intf.moveToBlock(Number); 01050 IntfIn = Cand.Intf.first(); 01051 } 01052 01053 unsigned CandOut = BundleCand[Bundles->getBundle(Number, 1)]; 01054 if (CandOut != NoCand) { 01055 GlobalSplitCandidate &Cand = GlobalCand[CandOut]; 01056 IntvOut = Cand.IntvIdx; 01057 Cand.Intf.moveToBlock(Number); 01058 IntfOut = Cand.Intf.last(); 01059 } 01060 if (!IntvIn && !IntvOut) 01061 continue; 01062 SE->splitLiveThroughBlock(Number, IntvIn, IntfIn, IntvOut, IntfOut); 01063 } 01064 } 01065 01066 ++NumGlobalSplits; 01067 01068 SmallVector<unsigned, 8> IntvMap; 01069 SE->finish(&IntvMap); 01070 DebugVars->splitRegister(Reg, LREdit.regs()); 01071 01072 ExtraRegInfo.resize(MRI->getNumVirtRegs()); 01073 unsigned OrigBlocks = SA->getNumLiveBlocks(); 01074 01075 // Sort out the new intervals created by splitting. We get four kinds: 01076 // - Remainder intervals should not be split again. 01077 // - Candidate intervals can be assigned to Cand.PhysReg. 01078 // - Block-local splits are candidates for local splitting. 01079 // - DCE leftovers should go back on the queue. 01080 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { 01081 LiveInterval &Reg = *LREdit.get(i); 01082 01083 // Ignore old intervals from DCE. 01084 if (getStage(Reg) != RS_New) 01085 continue; 01086 01087 // Remainder interval. Don't try splitting again, spill if it doesn't 01088 // allocate. 01089 if (IntvMap[i] == 0) { 01090 setStage(Reg, RS_Spill); 01091 continue; 01092 } 01093 01094 // Global intervals. Allow repeated splitting as long as the number of live 01095 // blocks is strictly decreasing. 01096 if (IntvMap[i] < NumGlobalIntvs) { 01097 if (SA->countLiveBlocks(&Reg) >= OrigBlocks) { 01098 DEBUG(dbgs() << "Main interval covers the same " << OrigBlocks 01099 << " blocks as original.\n"); 01100 // Don't allow repeated splitting as a safe guard against looping. 01101 setStage(Reg, RS_Split2); 01102 } 01103 continue; 01104 } 01105 01106 // Other intervals are treated as new. This includes local intervals created 01107 // for blocks with multiple uses, and anything created by DCE. 01108 } 01109 01110 if (VerifyEnabled) 01111 MF->verify(this, "After splitting live range around region"); 01112 } 01113 01114 unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 01115 SmallVectorImpl<LiveInterval*> &NewVRegs) { 01116 unsigned NumCands = 0; 01117 unsigned BestCand = NoCand; 01118 float BestCost; 01119 SmallVector<unsigned, 8> UsedCands; 01120 01121 // Check if we can split this live range around a compact region. 01122 bool HasCompact = calcCompactRegion(GlobalCand.front()); 01123 if (HasCompact) { 01124 // Yes, keep GlobalCand[0] as the compact region candidate. 01125 NumCands = 1; 01126 BestCost = HUGE_VALF; 01127 } else { 01128 // No benefit from the compact region, our fallback will be per-block 01129 // splitting. Make sure we find a solution that is cheaper than spilling. 01130 BestCost = Hysteresis * calcSpillCost(); 01131 DEBUG(dbgs() << "Cost of isolating all blocks = " << BestCost << '\n'); 01132 } 01133 01134 Order.rewind(); 01135 while (unsigned PhysReg = Order.next()) { 01136 // Discard bad candidates before we run out of interference cache cursors. 01137 // This will only affect register classes with a lot of registers (>32). 01138 if (NumCands == IntfCache.getMaxCursors()) { 01139 unsigned WorstCount = ~0u; 01140 unsigned Worst = 0; 01141 for (unsigned i = 0; i != NumCands; ++i) { 01142 if (i == BestCand || !GlobalCand[i].PhysReg) 01143 continue; 01144 unsigned Count = GlobalCand[i].LiveBundles.count(); 01145 if (Count < WorstCount) 01146 Worst = i, WorstCount = Count; 01147 } 01148 --NumCands; 01149 GlobalCand[Worst] = GlobalCand[NumCands]; 01150 if (BestCand == NumCands) 01151 BestCand = Worst; 01152 } 01153 01154 if (GlobalCand.size() <= NumCands) 01155 GlobalCand.resize(NumCands+1); 01156 GlobalSplitCandidate &Cand = GlobalCand[NumCands]; 01157 Cand.reset(IntfCache, PhysReg); 01158 01159 SpillPlacer->prepare(Cand.LiveBundles); 01160 float Cost; 01161 if (!addSplitConstraints(Cand.Intf, Cost)) { 01162 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n"); 01163 continue; 01164 } 01165 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost); 01166 if (Cost >= BestCost) { 01167 DEBUG({ 01168 if (BestCand == NoCand) 01169 dbgs() << " worse than no bundles\n"; 01170 else 01171 dbgs() << " worse than " 01172 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n'; 01173 }); 01174 continue; 01175 } 01176 growRegion(Cand); 01177 01178 SpillPlacer->finish(); 01179 01180 // No live bundles, defer to splitSingleBlocks(). 01181 if (!Cand.LiveBundles.any()) { 01182 DEBUG(dbgs() << " no bundles.\n"); 01183 continue; 01184 } 01185 01186 Cost += calcGlobalSplitCost(Cand); 01187 DEBUG({ 01188 dbgs() << ", total = " << Cost << " with bundles"; 01189 for (int i = Cand.LiveBundles.find_first(); i>=0; 01190 i = Cand.LiveBundles.find_next(i)) 01191 dbgs() << " EB#" << i; 01192 dbgs() << ".\n"; 01193 }); 01194 if (Cost < BestCost) { 01195 BestCand = NumCands; 01196 BestCost = Hysteresis * Cost; // Prevent rounding effects. 01197 } 01198 ++NumCands; 01199 } 01200 01201 // No solutions found, fall back to single block splitting. 01202 if (!HasCompact && BestCand == NoCand) 01203 return 0; 01204 01205 // Prepare split editor. 01206 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 01207 SE->reset(LREdit, SplitSpillMode); 01208 01209 // Assign all edge bundles to the preferred candidate, or NoCand. 01210 BundleCand.assign(Bundles->getNumBundles(), NoCand); 01211 01212 // Assign bundles for the best candidate region. 01213 if (BestCand != NoCand) { 01214 GlobalSplitCandidate &Cand = GlobalCand[BestCand]; 01215 if (unsigned B = Cand.getBundles(BundleCand, BestCand)) { 01216 UsedCands.push_back(BestCand); 01217 Cand.IntvIdx = SE->openIntv(); 01218 DEBUG(dbgs() << "Split for " << PrintReg(Cand.PhysReg, TRI) << " in " 01219 << B << " bundles, intv " << Cand.IntvIdx << ".\n"); 01220 (void)B; 01221 } 01222 } 01223 01224 // Assign bundles for the compact region. 01225 if (HasCompact) { 01226 GlobalSplitCandidate &Cand = GlobalCand.front(); 01227 assert(!Cand.PhysReg && "Compact region has no physreg"); 01228 if (unsigned B = Cand.getBundles(BundleCand, 0)) { 01229 UsedCands.push_back(0); 01230 Cand.IntvIdx = SE->openIntv(); 01231 DEBUG(dbgs() << "Split for compact region in " << B << " bundles, intv " 01232 << Cand.IntvIdx << ".\n"); 01233 (void)B; 01234 } 01235 } 01236 01237 splitAroundRegion(LREdit, UsedCands); 01238 return 0; 01239 } 01240 01241 01242 //===----------------------------------------------------------------------===// 01243 // Per-Block Splitting 01244 //===----------------------------------------------------------------------===// 01245 01246 /// tryBlockSplit - Split a global live range around every block with uses. This 01247 /// creates a lot of local live ranges, that will be split by tryLocalSplit if 01248 /// they don't allocate. 01249 unsigned RAGreedy::tryBlockSplit(LiveInterval &VirtReg, AllocationOrder &Order, 01250 SmallVectorImpl<LiveInterval*> &NewVRegs) { 01251 assert(&SA->getParent() == &VirtReg && "Live range wasn't analyzed"); 01252 unsigned Reg = VirtReg.reg; 01253 bool SingleInstrs = RegClassInfo.isProperSubClass(MRI->getRegClass(Reg)); 01254 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 01255 SE->reset(LREdit, SplitSpillMode); 01256 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 01257 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 01258 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 01259 if (SA->shouldSplitSingleBlock(BI, SingleInstrs)) 01260 SE->splitSingleBlock(BI); 01261 } 01262 // No blocks were split. 01263 if (LREdit.empty()) 01264 return 0; 01265 01266 // We did split for some blocks. 01267 SmallVector<unsigned, 8> IntvMap; 01268 SE->finish(&IntvMap); 01269 01270 // Tell LiveDebugVariables about the new ranges. 01271 DebugVars->splitRegister(Reg, LREdit.regs()); 01272 01273 ExtraRegInfo.resize(MRI->getNumVirtRegs()); 01274 01275 // Sort out the new intervals created by splitting. The remainder interval 01276 // goes straight to spilling, the new local ranges get to stay RS_New. 01277 for (unsigned i = 0, e = LREdit.size(); i != e; ++i) { 01278 LiveInterval &LI = *LREdit.get(i); 01279 if (getStage(LI) == RS_New && IntvMap[i] == 0) 01280 setStage(LI, RS_Spill); 01281 } 01282 01283 if (VerifyEnabled) 01284 MF->verify(this, "After splitting live range around basic blocks"); 01285 return 0; 01286 } 01287 01288 01289 //===----------------------------------------------------------------------===// 01290 // Per-Instruction Splitting 01291 //===----------------------------------------------------------------------===// 01292 01293 /// tryInstructionSplit - Split a live range around individual instructions. 01294 /// This is normally not worthwhile since the spiller is doing essentially the 01295 /// same thing. However, when the live range is in a constrained register 01296 /// class, it may help to insert copies such that parts of the live range can 01297 /// be moved to a larger register class. 01298 /// 01299 /// This is similar to spilling to a larger register class. 01300 unsigned 01301 RAGreedy::tryInstructionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 01302 SmallVectorImpl<LiveInterval*> &NewVRegs) { 01303 // There is no point to this if there are no larger sub-classes. 01304 if (!RegClassInfo.isProperSubClass(MRI->getRegClass(VirtReg.reg))) 01305 return 0; 01306 01307 // Always enable split spill mode, since we're effectively spilling to a 01308 // register. 01309 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 01310 SE->reset(LREdit, SplitEditor::SM_Size); 01311 01312 ArrayRef<SlotIndex> Uses = SA->getUseSlots(); 01313 if (Uses.size() <= 1) 01314 return 0; 01315 01316 DEBUG(dbgs() << "Split around " << Uses.size() << " individual instrs.\n"); 01317 01318 // Split around every non-copy instruction. 01319 for (unsigned i = 0; i != Uses.size(); ++i) { 01320 if (const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i])) 01321 if (MI->isFullCopy()) { 01322 DEBUG(dbgs() << " skip:\t" << Uses[i] << '\t' << *MI); 01323 continue; 01324 } 01325 SE->openIntv(); 01326 SlotIndex SegStart = SE->enterIntvBefore(Uses[i]); 01327 SlotIndex SegStop = SE->leaveIntvAfter(Uses[i]); 01328 SE->useIntv(SegStart, SegStop); 01329 } 01330 01331 if (LREdit.empty()) { 01332 DEBUG(dbgs() << "All uses were copies.\n"); 01333 return 0; 01334 } 01335 01336 SmallVector<unsigned, 8> IntvMap; 01337 SE->finish(&IntvMap); 01338 DebugVars->splitRegister(VirtReg.reg, LREdit.regs()); 01339 ExtraRegInfo.resize(MRI->getNumVirtRegs()); 01340 01341 // Assign all new registers to RS_Spill. This was the last chance. 01342 setStage(LREdit.begin(), LREdit.end(), RS_Spill); 01343 return 0; 01344 } 01345 01346 01347 //===----------------------------------------------------------------------===// 01348 // Local Splitting 01349 //===----------------------------------------------------------------------===// 01350 01351 01352 /// calcGapWeights - Compute the maximum spill weight that needs to be evicted 01353 /// in order to use PhysReg between two entries in SA->UseSlots. 01354 /// 01355 /// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. 01356 /// 01357 void RAGreedy::calcGapWeights(unsigned PhysReg, 01358 SmallVectorImpl<float> &GapWeight) { 01359 assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 01360 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 01361 ArrayRef<SlotIndex> Uses = SA->getUseSlots(); 01362 const unsigned NumGaps = Uses.size()-1; 01363 01364 // Start and end points for the interference check. 01365 SlotIndex StartIdx = 01366 BI.LiveIn ? BI.FirstInstr.getBaseIndex() : BI.FirstInstr; 01367 SlotIndex StopIdx = 01368 BI.LiveOut ? BI.LastInstr.getBoundaryIndex() : BI.LastInstr; 01369 01370 GapWeight.assign(NumGaps, 0.0f); 01371 01372 // Add interference from each overlapping register. 01373 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 01374 if (!Matrix->query(const_cast<LiveInterval&>(SA->getParent()), *Units) 01375 .checkInterference()) 01376 continue; 01377 01378 // We know that VirtReg is a continuous interval from FirstInstr to 01379 // LastInstr, so we don't need InterferenceQuery. 01380 // 01381 // Interference that overlaps an instruction is counted in both gaps 01382 // surrounding the instruction. The exception is interference before 01383 // StartIdx and after StopIdx. 01384 // 01385 LiveIntervalUnion::SegmentIter IntI = 01386 Matrix->getLiveUnions()[*Units] .find(StartIdx); 01387 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { 01388 // Skip the gaps before IntI. 01389 while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) 01390 if (++Gap == NumGaps) 01391 break; 01392 if (Gap == NumGaps) 01393 break; 01394 01395 // Update the gaps covered by IntI. 01396 const float weight = IntI.value()->weight; 01397 for (; Gap != NumGaps; ++Gap) { 01398 GapWeight[Gap] = std::max(GapWeight[Gap], weight); 01399 if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) 01400 break; 01401 } 01402 if (Gap == NumGaps) 01403 break; 01404 } 01405 } 01406 01407 // Add fixed interference. 01408 for (MCRegUnitIterator Units(PhysReg, TRI); Units.isValid(); ++Units) { 01409 const LiveInterval &LI = LIS->getRegUnit(*Units); 01410 LiveInterval::const_iterator I = LI.find(StartIdx); 01411 LiveInterval::const_iterator E = LI.end(); 01412 01413 // Same loop as above. Mark any overlapped gaps as HUGE_VALF. 01414 for (unsigned Gap = 0; I != E && I->start < StopIdx; ++I) { 01415 while (Uses[Gap+1].getBoundaryIndex() < I->start) 01416 if (++Gap == NumGaps) 01417 break; 01418 if (Gap == NumGaps) 01419 break; 01420 01421 for (; Gap != NumGaps; ++Gap) { 01422 GapWeight[Gap] = HUGE_VALF; 01423 if (Uses[Gap+1].getBaseIndex() >= I->end) 01424 break; 01425 } 01426 if (Gap == NumGaps) 01427 break; 01428 } 01429 } 01430 } 01431 01432 /// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only 01433 /// basic block. 01434 /// 01435 unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, 01436 SmallVectorImpl<LiveInterval*> &NewVRegs) { 01437 assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 01438 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 01439 01440 // Note that it is possible to have an interval that is live-in or live-out 01441 // while only covering a single block - A phi-def can use undef values from 01442 // predecessors, and the block could be a single-block loop. 01443 // We don't bother doing anything clever about such a case, we simply assume 01444 // that the interval is continuous from FirstInstr to LastInstr. We should 01445 // make sure that we don't do anything illegal to such an interval, though. 01446 01447 ArrayRef<SlotIndex> Uses = SA->getUseSlots(); 01448 if (Uses.size() <= 2) 01449 return 0; 01450 const unsigned NumGaps = Uses.size()-1; 01451 01452 DEBUG({ 01453 dbgs() << "tryLocalSplit: "; 01454 for (unsigned i = 0, e = Uses.size(); i != e; ++i) 01455 dbgs() << ' ' << Uses[i]; 01456 dbgs() << '\n'; 01457 }); 01458 01459 // If VirtReg is live across any register mask operands, compute a list of 01460 // gaps with register masks. 01461 SmallVector<unsigned, 8> RegMaskGaps; 01462 if (Matrix->checkRegMaskInterference(VirtReg)) { 01463 // Get regmask slots for the whole block. 01464 ArrayRef<SlotIndex> RMS = LIS->getRegMaskSlotsInBlock(BI.MBB->getNumber()); 01465 DEBUG(dbgs() << RMS.size() << " regmasks in block:"); 01466 // Constrain to VirtReg's live range. 01467 unsigned ri = std::lower_bound(RMS.begin(), RMS.end(), 01468 Uses.front().getRegSlot()) - RMS.begin(); 01469 unsigned re = RMS.size(); 01470 for (unsigned i = 0; i != NumGaps && ri != re; ++i) { 01471 // Look for Uses[i] <= RMS <= Uses[i+1]. 01472 assert(!SlotIndex::isEarlierInstr(RMS[ri], Uses[i])); 01473 if (SlotIndex::isEarlierInstr(Uses[i+1], RMS[ri])) 01474 continue; 01475 // Skip a regmask on the same instruction as the last use. It doesn't 01476 // overlap the live range. 01477 if (SlotIndex::isSameInstr(Uses[i+1], RMS[ri]) && i+1 == NumGaps) 01478 break; 01479 DEBUG(dbgs() << ' ' << RMS[ri] << ':' << Uses[i] << '-' << Uses[i+1]); 01480 RegMaskGaps.push_back(i); 01481 // Advance ri to the next gap. A regmask on one of the uses counts in 01482 // both gaps. 01483 while (ri != re && SlotIndex::isEarlierInstr(RMS[ri], Uses[i+1])) 01484 ++ri; 01485 } 01486 DEBUG(dbgs() << '\n'); 01487 } 01488 01489 // Since we allow local split results to be split again, there is a risk of 01490 // creating infinite loops. It is tempting to require that the new live 01491 // ranges have less instructions than the original. That would guarantee 01492 // convergence, but it is too strict. A live range with 3 instructions can be 01493 // split 2+3 (including the COPY), and we want to allow that. 01494 // 01495 // Instead we use these rules: 01496 // 01497 // 1. Allow any split for ranges with getStage() < RS_Split2. (Except for the 01498 // noop split, of course). 01499 // 2. Require progress be made for ranges with getStage() == RS_Split2. All 01500 // the new ranges must have fewer instructions than before the split. 01501 // 3. New ranges with the same number of instructions are marked RS_Split2, 01502 // smaller ranges are marked RS_New. 01503 // 01504 // These rules allow a 3 -> 2+3 split once, which we need. They also prevent 01505 // excessive splitting and infinite loops. 01506 // 01507 bool ProgressRequired = getStage(VirtReg) >= RS_Split2; 01508 01509 // Best split candidate. 01510 unsigned BestBefore = NumGaps; 01511 unsigned BestAfter = 0; 01512 float BestDiff = 0; 01513 01514 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber()); 01515 SmallVector<float, 8> GapWeight; 01516 01517 Order.rewind(); 01518 while (unsigned PhysReg = Order.next()) { 01519 // Keep track of the largest spill weight that would need to be evicted in 01520 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. 01521 calcGapWeights(PhysReg, GapWeight); 01522 01523 // Remove any gaps with regmask clobbers. 01524 if (Matrix->checkRegMaskInterference(VirtReg, PhysReg)) 01525 for (unsigned i = 0, e = RegMaskGaps.size(); i != e; ++i) 01526 GapWeight[RegMaskGaps[i]] = HUGE_VALF; 01527 01528 // Try to find the best sequence of gaps to close. 01529 // The new spill weight must be larger than any gap interference. 01530 01531 // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. 01532 unsigned SplitBefore = 0, SplitAfter = 1; 01533 01534 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). 01535 // It is the spill weight that needs to be evicted. 01536 float MaxGap = GapWeight[0]; 01537 01538 for (;;) { 01539 // Live before/after split? 01540 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; 01541 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; 01542 01543 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' ' 01544 << Uses[SplitBefore] << '-' << Uses[SplitAfter] 01545 << " i=" << MaxGap); 01546 01547 // Stop before the interval gets so big we wouldn't be making progress. 01548 if (!LiveBefore && !LiveAfter) { 01549 DEBUG(dbgs() << " all\n"); 01550 break; 01551 } 01552 // Should the interval be extended or shrunk? 01553 bool Shrink = true; 01554 01555 // How many gaps would the new range have? 01556 unsigned NewGaps = LiveBefore + SplitAfter - SplitBefore + LiveAfter; 01557 01558 // Legally, without causing looping? 01559 bool Legal = !ProgressRequired || NewGaps < NumGaps; 01560 01561 if (Legal && MaxGap < HUGE_VALF) { 01562 // Estimate the new spill weight. Each instruction reads or writes the 01563 // register. Conservatively assume there are no read-modify-write 01564 // instructions. 01565 // 01566 // Try to guess the size of the new interval. 01567 const float EstWeight = normalizeSpillWeight(blockFreq * (NewGaps + 1), 01568 Uses[SplitBefore].distance(Uses[SplitAfter]) + 01569 (LiveBefore + LiveAfter)*SlotIndex::InstrDist); 01570 // Would this split be possible to allocate? 01571 // Never allocate all gaps, we wouldn't be making progress. 01572 DEBUG(dbgs() << " w=" << EstWeight); 01573 if (EstWeight * Hysteresis >= MaxGap) { 01574 Shrink = false; 01575 float Diff = EstWeight - MaxGap; 01576 if (Diff > BestDiff) { 01577 DEBUG(dbgs() << " (best)"); 01578 BestDiff = Hysteresis * Diff; 01579 BestBefore = SplitBefore; 01580 BestAfter = SplitAfter; 01581 } 01582 } 01583 } 01584 01585 // Try to shrink. 01586 if (Shrink) { 01587 if (++SplitBefore < SplitAfter) { 01588 DEBUG(dbgs() << " shrink\n"); 01589 // Recompute the max when necessary. 01590 if (GapWeight[SplitBefore - 1] >= MaxGap) { 01591 MaxGap = GapWeight[SplitBefore]; 01592 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) 01593 MaxGap = std::max(MaxGap, GapWeight[i]); 01594 } 01595 continue; 01596 } 01597 MaxGap = 0; 01598 } 01599 01600 // Try to extend the interval. 01601 if (SplitAfter >= NumGaps) { 01602 DEBUG(dbgs() << " end\n"); 01603 break; 01604 } 01605 01606 DEBUG(dbgs() << " extend\n"); 01607 MaxGap = std::max(MaxGap, GapWeight[SplitAfter++]); 01608 } 01609 } 01610 01611 // Didn't find any candidates? 01612 if (BestBefore == NumGaps) 01613 return 0; 01614 01615 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] 01616 << '-' << Uses[BestAfter] << ", " << BestDiff 01617 << ", " << (BestAfter - BestBefore + 1) << " instrs\n"); 01618 01619 LiveRangeEdit LREdit(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 01620 SE->reset(LREdit); 01621 01622 SE->openIntv(); 01623 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); 01624 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); 01625 SE->useIntv(SegStart, SegStop); 01626 SmallVector<unsigned, 8> IntvMap; 01627 SE->finish(&IntvMap); 01628 DebugVars->splitRegister(VirtReg.reg, LREdit.regs()); 01629 01630 // If the new range has the same number of instructions as before, mark it as 01631 // RS_Split2 so the next split will be forced to make progress. Otherwise, 01632 // leave the new intervals as RS_New so they can compete. 01633 bool LiveBefore = BestBefore != 0 || BI.LiveIn; 01634 bool LiveAfter = BestAfter != NumGaps || BI.LiveOut; 01635 unsigned NewGaps = LiveBefore + BestAfter - BestBefore + LiveAfter; 01636 if (NewGaps >= NumGaps) { 01637 DEBUG(dbgs() << "Tagging non-progress ranges: "); 01638 assert(!ProgressRequired && "Didn't make progress when it was required."); 01639 for (unsigned i = 0, e = IntvMap.size(); i != e; ++i) 01640 if (IntvMap[i] == 1) { 01641 setStage(*LREdit.get(i), RS_Split2); 01642 DEBUG(dbgs() << PrintReg(LREdit.get(i)->reg)); 01643 } 01644 DEBUG(dbgs() << '\n'); 01645 } 01646 ++NumLocalSplits; 01647 01648 return 0; 01649 } 01650 01651 //===----------------------------------------------------------------------===// 01652 // Live Range Splitting 01653 //===----------------------------------------------------------------------===// 01654 01655 /// trySplit - Try to split VirtReg or one of its interferences, making it 01656 /// assignable. 01657 /// @return Physreg when VirtReg may be assigned and/or new NewVRegs. 01658 unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, 01659 SmallVectorImpl<LiveInterval*>&NewVRegs) { 01660 // Ranges must be Split2 or less. 01661 if (getStage(VirtReg) >= RS_Spill) 01662 return 0; 01663 01664 // Local intervals are handled separately. 01665 if (LIS->intervalIsInOneMBB(VirtReg)) { 01666 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled); 01667 SA->analyze(&VirtReg); 01668 unsigned PhysReg = tryLocalSplit(VirtReg, Order, NewVRegs); 01669 if (PhysReg || !NewVRegs.empty()) 01670 return PhysReg; 01671 return tryInstructionSplit(VirtReg, Order, NewVRegs); 01672 } 01673 01674 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled); 01675 01676 SA->analyze(&VirtReg); 01677 01678 // FIXME: SplitAnalysis may repair broken live ranges coming from the 01679 // coalescer. That may cause the range to become allocatable which means that 01680 // tryRegionSplit won't be making progress. This check should be replaced with 01681 // an assertion when the coalescer is fixed. 01682 if (SA->didRepairRange()) { 01683 // VirtReg has changed, so all cached queries are invalid. 01684 Matrix->invalidateVirtRegs(); 01685 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) 01686 return PhysReg; 01687 } 01688 01689 // First try to split around a region spanning multiple blocks. RS_Split2 01690 // ranges already made dubious progress with region splitting, so they go 01691 // straight to single block splitting. 01692 if (getStage(VirtReg) < RS_Split2) { 01693 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); 01694 if (PhysReg || !NewVRegs.empty()) 01695 return PhysReg; 01696 } 01697 01698 // Then isolate blocks. 01699 return tryBlockSplit(VirtReg, Order, NewVRegs); 01700 } 01701 01702 01703 //===----------------------------------------------------------------------===// 01704 // Main Entry Point 01705 //===----------------------------------------------------------------------===// 01706 01707 unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, 01708 SmallVectorImpl<LiveInterval*> &NewVRegs) { 01709 // First try assigning a free register. 01710 AllocationOrder Order(VirtReg.reg, *VRM, RegClassInfo); 01711 if (unsigned PhysReg = tryAssign(VirtReg, Order, NewVRegs)) 01712 return PhysReg; 01713 01714 LiveRangeStage Stage = getStage(VirtReg); 01715 DEBUG(dbgs() << StageName[Stage] 01716 << " Cascade " << ExtraRegInfo[VirtReg.reg].Cascade << '\n'); 01717 01718 // Try to evict a less worthy live range, but only for ranges from the primary 01719 // queue. The RS_Split ranges already failed to do this, and they should not 01720 // get a second chance until they have been split. 01721 if (Stage != RS_Split) 01722 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs)) 01723 return PhysReg; 01724 01725 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs"); 01726 01727 // The first time we see a live range, don't try to split or spill. 01728 // Wait until the second time, when all smaller ranges have been allocated. 01729 // This gives a better picture of the interference to split around. 01730 if (Stage < RS_Split) { 01731 setStage(VirtReg, RS_Split); 01732 DEBUG(dbgs() << "wait for second round\n"); 01733 NewVRegs.push_back(&VirtReg); 01734 return 0; 01735 } 01736 01737 // If we couldn't allocate a register from spilling, there is probably some 01738 // invalid inline assembly. The base class wil report it. 01739 if (Stage >= RS_Done || !VirtReg.isSpillable()) 01740 return ~0u; 01741 01742 // Try splitting VirtReg or interferences. 01743 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); 01744 if (PhysReg || !NewVRegs.empty()) 01745 return PhysReg; 01746 01747 // Finally spill VirtReg itself. 01748 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); 01749 LiveRangeEdit LRE(&VirtReg, NewVRegs, *MF, *LIS, VRM, this); 01750 spiller().spill(LRE); 01751 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Done); 01752 01753 if (VerifyEnabled) 01754 MF->verify(this, "After spilling"); 01755 01756 // The live virtual register requesting allocation was spilled, so tell 01757 // the caller not to allocate anything during this round. 01758 return 0; 01759 } 01760 01761 bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { 01762 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" 01763 << "********** Function: " << mf.getName() << '\n'); 01764 01765 MF = &mf; 01766 if (VerifyEnabled) 01767 MF->verify(this, "Before greedy register allocator"); 01768 01769 RegAllocBase::init(getAnalysis<VirtRegMap>(), 01770 getAnalysis<LiveIntervals>(), 01771 getAnalysis<LiveRegMatrix>()); 01772 Indexes = &getAnalysis<SlotIndexes>(); 01773 DomTree = &getAnalysis<MachineDominatorTree>(); 01774 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); 01775 Loops = &getAnalysis<MachineLoopInfo>(); 01776 Bundles = &getAnalysis<EdgeBundles>(); 01777 SpillPlacer = &getAnalysis<SpillPlacement>(); 01778 DebugVars = &getAnalysis<LiveDebugVariables>(); 01779 01780 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); 01781 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree)); 01782 ExtraRegInfo.clear(); 01783 ExtraRegInfo.resize(MRI->getNumVirtRegs()); 01784 NextCascade = 1; 01785 IntfCache.init(MF, Matrix->getLiveUnions(), Indexes, LIS, TRI); 01786 GlobalCand.resize(32); // This will grow as needed. 01787 01788 allocatePhysRegs(); 01789 releaseMemory(); 01790 return true; 01791 }