1 //===-- RegAllocGreedy.cpp - greedy register allocator --------------------===// 2 // 3 // The LLVM Compiler Infrastructure 4 // 5 // This file is distributed under the University of Illinois Open Source 6 // License. See LICENSE.TXT for details. 7 // 8 //===----------------------------------------------------------------------===// 9 // 10 // This file defines the RAGreedy function pass for register allocation in 11 // optimized builds. 12 // 13 //===----------------------------------------------------------------------===// 14 15 #define DEBUG_TYPE "regalloc" 16 #include "AllocationOrder.h" 17 #include "InterferenceCache.h" 18 #include "LiveDebugVariables.h" 19 #include "LiveRangeEdit.h" 20 #include "RegAllocBase.h" 21 #include "Spiller.h" 22 #include "SpillPlacement.h" 23 #include "SplitKit.h" 24 #include "VirtRegMap.h" 25 #include "llvm/ADT/Statistic.h" 26 #include "llvm/Analysis/AliasAnalysis.h" 27 #include "llvm/Function.h" 28 #include "llvm/PassAnalysisSupport.h" 29 #include "llvm/CodeGen/CalcSpillWeights.h" 30 #include "llvm/CodeGen/EdgeBundles.h" 31 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 32 #include "llvm/CodeGen/LiveStackAnalysis.h" 33 #include "llvm/CodeGen/MachineDominators.h" 34 #include "llvm/CodeGen/MachineFunctionPass.h" 35 #include "llvm/CodeGen/MachineLoopInfo.h" 36 #include "llvm/CodeGen/MachineLoopRanges.h" 37 #include "llvm/CodeGen/MachineRegisterInfo.h" 38 #include "llvm/CodeGen/Passes.h" 39 #include "llvm/CodeGen/RegAllocRegistry.h" 40 #include "llvm/CodeGen/RegisterCoalescer.h" 41 #include "llvm/Target/TargetOptions.h" 42 #include "llvm/Support/Debug.h" 43 #include "llvm/Support/ErrorHandling.h" 44 #include "llvm/Support/raw_ostream.h" 45 #include "llvm/Support/Timer.h" 46 47 #include <queue> 48 49 using namespace llvm; 50 51 STATISTIC(NumGlobalSplits, "Number of split global live ranges"); 52 STATISTIC(NumLocalSplits, "Number of split local live ranges"); 53 STATISTIC(NumEvicted, "Number of interferences evicted"); 54 55 static RegisterRegAlloc greedyRegAlloc("greedy", "greedy register allocator", 56 createGreedyRegisterAllocator); 57 58 namespace { 59 class RAGreedy : public MachineFunctionPass, 60 public RegAllocBase, 61 private LiveRangeEdit::Delegate { 62 63 // context 64 MachineFunction *MF; 65 BitVector ReservedRegs; 66 67 // analyses 68 SlotIndexes *Indexes; 69 LiveStacks *LS; 70 MachineDominatorTree *DomTree; 71 MachineLoopInfo *Loops; 72 MachineLoopRanges *LoopRanges; 73 EdgeBundles *Bundles; 74 SpillPlacement *SpillPlacer; 75 76 // state 77 std::auto_ptr<Spiller> SpillerInstance; 78 std::priority_queue<std::pair<unsigned, unsigned> > Queue; 79 80 // Live ranges pass through a number of stages as we try to allocate them. 81 // Some of the stages may also create new live ranges: 82 // 83 // - Region splitting. 84 // - Per-block splitting. 85 // - Local splitting. 86 // - Spilling. 87 // 88 // Ranges produced by one of the stages skip the previous stages when they are 89 // dequeued. This improves performance because we can skip interference checks 90 // that are unlikely to give any results. It also guarantees that the live 91 // range splitting algorithm terminates, something that is otherwise hard to 92 // ensure. 93 enum LiveRangeStage { 94 RS_New, ///< Never seen before. 95 RS_First, ///< First time in the queue. 96 RS_Second, ///< Second time in the queue. 97 RS_Global, ///< Produced by global splitting. 98 RS_Local, ///< Produced by local splitting. 99 RS_Spill ///< Produced by spilling. 100 }; 101 102 IndexedMap<unsigned char, VirtReg2IndexFunctor> LRStage; 103 104 LiveRangeStage getStage(const LiveInterval &VirtReg) const { 105 return LiveRangeStage(LRStage[VirtReg.reg]); 106 } 107 108 template<typename Iterator> 109 void setStage(Iterator Begin, Iterator End, LiveRangeStage NewStage) { 110 LRStage.resize(MRI->getNumVirtRegs()); 111 for (;Begin != End; ++Begin) { 112 unsigned Reg = (*Begin)->reg; 113 if (LRStage[Reg] == RS_New) 114 LRStage[Reg] = NewStage; 115 } 116 } 117 118 // splitting state. 119 std::auto_ptr<SplitAnalysis> SA; 120 std::auto_ptr<SplitEditor> SE; 121 122 /// Cached per-block interference maps 123 InterferenceCache IntfCache; 124 125 /// All basic blocks where the current register has uses. 126 SmallVector<SpillPlacement::BlockConstraint, 8> SplitConstraints; 127 128 /// Global live range splitting candidate info. 129 struct GlobalSplitCandidate { 130 unsigned PhysReg; 131 BitVector LiveBundles; 132 SmallVector<unsigned, 8> ActiveBlocks; 133 134 void reset(unsigned Reg) { 135 PhysReg = Reg; 136 LiveBundles.clear(); 137 ActiveBlocks.clear(); 138 } 139 }; 140 141 /// Candidate info for for each PhysReg in AllocationOrder. 142 /// This vector never shrinks, but grows to the size of the largest register 143 /// class. 144 SmallVector<GlobalSplitCandidate, 32> GlobalCand; 145 146 /// For every instruction in SA->UseSlots, store the previous non-copy 147 /// instruction. 148 SmallVector<SlotIndex, 8> PrevSlot; 149 150 public: 151 RAGreedy(); 152 153 /// Return the pass name. 154 virtual const char* getPassName() const { 155 return "Greedy Register Allocator"; 156 } 157 158 /// RAGreedy analysis usage. 159 virtual void getAnalysisUsage(AnalysisUsage &AU) const; 160 virtual void releaseMemory(); 161 virtual Spiller &spiller() { return *SpillerInstance; } 162 virtual void enqueue(LiveInterval *LI); 163 virtual LiveInterval *dequeue(); 164 virtual unsigned selectOrSplit(LiveInterval&, 165 SmallVectorImpl<LiveInterval*>&); 166 167 /// Perform register allocation. 168 virtual bool runOnMachineFunction(MachineFunction &mf); 169 170 static char ID; 171 172 private: 173 void LRE_WillEraseInstruction(MachineInstr*); 174 bool LRE_CanEraseVirtReg(unsigned); 175 void LRE_WillShrinkVirtReg(unsigned); 176 void LRE_DidCloneVirtReg(unsigned, unsigned); 177 178 bool addSplitConstraints(InterferenceCache::Cursor, float&); 179 void addThroughConstraints(InterferenceCache::Cursor, ArrayRef<unsigned>); 180 void growRegion(GlobalSplitCandidate &Cand, InterferenceCache::Cursor); 181 float calcGlobalSplitCost(GlobalSplitCandidate&, InterferenceCache::Cursor); 182 void splitAroundRegion(LiveInterval&, GlobalSplitCandidate&, 183 SmallVectorImpl<LiveInterval*>&); 184 void calcGapWeights(unsigned, SmallVectorImpl<float>&); 185 SlotIndex getPrevMappedIndex(const MachineInstr*); 186 void calcPrevSlots(); 187 unsigned nextSplitPoint(unsigned); 188 bool canEvictInterference(LiveInterval&, unsigned, float&); 189 190 unsigned tryEvict(LiveInterval&, AllocationOrder&, 191 SmallVectorImpl<LiveInterval*>&); 192 unsigned tryRegionSplit(LiveInterval&, AllocationOrder&, 193 SmallVectorImpl<LiveInterval*>&); 194 unsigned tryLocalSplit(LiveInterval&, AllocationOrder&, 195 SmallVectorImpl<LiveInterval*>&); 196 unsigned trySplit(LiveInterval&, AllocationOrder&, 197 SmallVectorImpl<LiveInterval*>&); 198 }; 199 } // end anonymous namespace 200 201 char RAGreedy::ID = 0; 202 203 FunctionPass* llvm::createGreedyRegisterAllocator() { 204 return new RAGreedy(); 205 } 206 207 RAGreedy::RAGreedy(): MachineFunctionPass(ID), LRStage(RS_New) { 208 initializeLiveDebugVariablesPass(*PassRegistry::getPassRegistry()); 209 initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 210 initializeLiveIntervalsPass(*PassRegistry::getPassRegistry()); 211 initializeSlotIndexesPass(*PassRegistry::getPassRegistry()); 212 initializeStrongPHIEliminationPass(*PassRegistry::getPassRegistry()); 213 initializeRegisterCoalescerAnalysisGroup(*PassRegistry::getPassRegistry()); 214 initializeCalculateSpillWeightsPass(*PassRegistry::getPassRegistry()); 215 initializeLiveStacksPass(*PassRegistry::getPassRegistry()); 216 initializeMachineDominatorTreePass(*PassRegistry::getPassRegistry()); 217 initializeMachineLoopInfoPass(*PassRegistry::getPassRegistry()); 218 initializeMachineLoopRangesPass(*PassRegistry::getPassRegistry()); 219 initializeVirtRegMapPass(*PassRegistry::getPassRegistry()); 220 initializeEdgeBundlesPass(*PassRegistry::getPassRegistry()); 221 initializeSpillPlacementPass(*PassRegistry::getPassRegistry()); 222 } 223 224 void RAGreedy::getAnalysisUsage(AnalysisUsage &AU) const { 225 AU.setPreservesCFG(); 226 AU.addRequired<AliasAnalysis>(); 227 AU.addPreserved<AliasAnalysis>(); 228 AU.addRequired<LiveIntervals>(); 229 AU.addRequired<SlotIndexes>(); 230 AU.addPreserved<SlotIndexes>(); 231 AU.addRequired<LiveDebugVariables>(); 232 AU.addPreserved<LiveDebugVariables>(); 233 if (StrongPHIElim) 234 AU.addRequiredID(StrongPHIEliminationID); 235 AU.addRequiredTransitive<RegisterCoalescer>(); 236 AU.addRequired<CalculateSpillWeights>(); 237 AU.addRequired<LiveStacks>(); 238 AU.addPreserved<LiveStacks>(); 239 AU.addRequired<MachineDominatorTree>(); 240 AU.addPreserved<MachineDominatorTree>(); 241 AU.addRequired<MachineLoopInfo>(); 242 AU.addPreserved<MachineLoopInfo>(); 243 AU.addRequired<MachineLoopRanges>(); 244 AU.addPreserved<MachineLoopRanges>(); 245 AU.addRequired<VirtRegMap>(); 246 AU.addPreserved<VirtRegMap>(); 247 AU.addRequired<EdgeBundles>(); 248 AU.addRequired<SpillPlacement>(); 249 MachineFunctionPass::getAnalysisUsage(AU); 250 } 251 252 253 //===----------------------------------------------------------------------===// 254 // LiveRangeEdit delegate methods 255 //===----------------------------------------------------------------------===// 256 257 void RAGreedy::LRE_WillEraseInstruction(MachineInstr *MI) { 258 // LRE itself will remove from SlotIndexes and parent basic block. 259 VRM->RemoveMachineInstrFromMaps(MI); 260 } 261 262 bool RAGreedy::LRE_CanEraseVirtReg(unsigned VirtReg) { 263 if (unsigned PhysReg = VRM->getPhys(VirtReg)) { 264 unassign(LIS->getInterval(VirtReg), PhysReg); 265 return true; 266 } 267 // Unassigned virtreg is probably in the priority queue. 268 // RegAllocBase will erase it after dequeueing. 269 return false; 270 } 271 272 void RAGreedy::LRE_WillShrinkVirtReg(unsigned VirtReg) { 273 unsigned PhysReg = VRM->getPhys(VirtReg); 274 if (!PhysReg) 275 return; 276 277 // Register is assigned, put it back on the queue for reassignment. 278 LiveInterval &LI = LIS->getInterval(VirtReg); 279 unassign(LI, PhysReg); 280 enqueue(&LI); 281 } 282 283 void RAGreedy::LRE_DidCloneVirtReg(unsigned New, unsigned Old) { 284 // LRE may clone a virtual register because dead code elimination causes it to 285 // be split into connected components. Ensure that the new register gets the 286 // same stage as the parent. 287 LRStage.grow(New); 288 LRStage[New] = LRStage[Old]; 289 } 290 291 void RAGreedy::releaseMemory() { 292 SpillerInstance.reset(0); 293 LRStage.clear(); 294 GlobalCand.clear(); 295 RegAllocBase::releaseMemory(); 296 } 297 298 void RAGreedy::enqueue(LiveInterval *LI) { 299 // Prioritize live ranges by size, assigning larger ranges first. 300 // The queue holds (size, reg) pairs. 301 const unsigned Size = LI->getSize(); 302 const unsigned Reg = LI->reg; 303 assert(TargetRegisterInfo::isVirtualRegister(Reg) && 304 "Can only enqueue virtual registers"); 305 unsigned Prio; 306 307 LRStage.grow(Reg); 308 if (LRStage[Reg] == RS_New) 309 LRStage[Reg] = RS_First; 310 311 if (LRStage[Reg] == RS_Second) 312 // Unsplit ranges that couldn't be allocated immediately are deferred until 313 // everything else has been allocated. Long ranges are allocated last so 314 // they are split against realistic interference. 315 Prio = (1u << 31) - Size; 316 else { 317 // Everything else is allocated in long->short order. Long ranges that don't 318 // fit should be spilled ASAP so they don't create interference. 319 Prio = (1u << 31) + Size; 320 321 // Boost ranges that have a physical register hint. 322 if (TargetRegisterInfo::isPhysicalRegister(VRM->getRegAllocPref(Reg))) 323 Prio |= (1u << 30); 324 } 325 326 Queue.push(std::make_pair(Prio, Reg)); 327 } 328 329 LiveInterval *RAGreedy::dequeue() { 330 if (Queue.empty()) 331 return 0; 332 LiveInterval *LI = &LIS->getInterval(Queue.top().second); 333 Queue.pop(); 334 return LI; 335 } 336 337 //===----------------------------------------------------------------------===// 338 // Interference eviction 339 //===----------------------------------------------------------------------===// 340 341 /// canEvict - Return true if all interferences between VirtReg and PhysReg can 342 /// be evicted. 343 /// Return false if any interference is heavier than MaxWeight. 344 /// On return, set MaxWeight to the maximal spill weight of an interference. 345 bool RAGreedy::canEvictInterference(LiveInterval &VirtReg, unsigned PhysReg, 346 float &MaxWeight) { 347 float Weight = 0; 348 for (const unsigned *AliasI = TRI->getOverlaps(PhysReg); *AliasI; ++AliasI) { 349 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); 350 // If there is 10 or more interferences, chances are one is heavier. 351 if (Q.collectInterferingVRegs(10, MaxWeight) >= 10) 352 return false; 353 354 // Check if any interfering live range is heavier than MaxWeight. 355 for (unsigned i = Q.interferingVRegs().size(); i; --i) { 356 LiveInterval *Intf = Q.interferingVRegs()[i - 1]; 357 if (TargetRegisterInfo::isPhysicalRegister(Intf->reg)) 358 return false; 359 if (Intf->weight >= MaxWeight) 360 return false; 361 Weight = std::max(Weight, Intf->weight); 362 } 363 } 364 MaxWeight = Weight; 365 return true; 366 } 367 368 /// tryEvict - Try to evict all interferences for a physreg. 369 /// @param VirtReg Currently unassigned virtual register. 370 /// @param Order Physregs to try. 371 /// @return Physreg to assign VirtReg, or 0. 372 unsigned RAGreedy::tryEvict(LiveInterval &VirtReg, 373 AllocationOrder &Order, 374 SmallVectorImpl<LiveInterval*> &NewVRegs){ 375 NamedRegionTimer T("Evict", TimerGroupName, TimePassesIsEnabled); 376 377 // Keep track of the lightest single interference seen so far. 378 float BestWeight = VirtReg.weight; 379 unsigned BestPhys = 0; 380 381 Order.rewind(); 382 while (unsigned PhysReg = Order.next()) { 383 float Weight = BestWeight; 384 if (!canEvictInterference(VirtReg, PhysReg, Weight)) 385 continue; 386 387 // This is an eviction candidate. 388 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << " interference = " 389 << Weight << '\n'); 390 if (BestPhys && Weight >= BestWeight) 391 continue; 392 393 // Best so far. 394 BestPhys = PhysReg; 395 BestWeight = Weight; 396 // Stop if the hint can be used. 397 if (Order.isHint(PhysReg)) 398 break; 399 } 400 401 if (!BestPhys) 402 return 0; 403 404 DEBUG(dbgs() << "evicting " << PrintReg(BestPhys, TRI) << " interference\n"); 405 for (const unsigned *AliasI = TRI->getOverlaps(BestPhys); *AliasI; ++AliasI) { 406 LiveIntervalUnion::Query &Q = query(VirtReg, *AliasI); 407 assert(Q.seenAllInterferences() && "Didn't check all interfererences."); 408 for (unsigned i = 0, e = Q.interferingVRegs().size(); i != e; ++i) { 409 LiveInterval *Intf = Q.interferingVRegs()[i]; 410 unassign(*Intf, VRM->getPhys(Intf->reg)); 411 ++NumEvicted; 412 NewVRegs.push_back(Intf); 413 } 414 } 415 return BestPhys; 416 } 417 418 419 //===----------------------------------------------------------------------===// 420 // Region Splitting 421 //===----------------------------------------------------------------------===// 422 423 /// addSplitConstraints - Fill out the SplitConstraints vector based on the 424 /// interference pattern in Physreg and its aliases. Add the constraints to 425 /// SpillPlacement and return the static cost of this split in Cost, assuming 426 /// that all preferences in SplitConstraints are met. 427 /// Return false if there are no bundles with positive bias. 428 bool RAGreedy::addSplitConstraints(InterferenceCache::Cursor Intf, 429 float &Cost) { 430 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 431 432 // Reset interference dependent info. 433 SplitConstraints.resize(UseBlocks.size()); 434 float StaticCost = 0; 435 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 436 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 437 SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 438 439 BC.Number = BI.MBB->getNumber(); 440 Intf.moveToBlock(BC.Number); 441 BC.Entry = BI.LiveIn ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 442 BC.Exit = BI.LiveOut ? SpillPlacement::PrefReg : SpillPlacement::DontCare; 443 444 if (!Intf.hasInterference()) 445 continue; 446 447 // Number of spill code instructions to insert. 448 unsigned Ins = 0; 449 450 // Interference for the live-in value. 451 if (BI.LiveIn) { 452 if (Intf.first() <= Indexes->getMBBStartIdx(BC.Number)) 453 BC.Entry = SpillPlacement::MustSpill, ++Ins; 454 else if (Intf.first() < BI.FirstUse) 455 BC.Entry = SpillPlacement::PrefSpill, ++Ins; 456 else if (Intf.first() < (BI.LiveThrough ? BI.LastUse : BI.Kill)) 457 ++Ins; 458 } 459 460 // Interference for the live-out value. 461 if (BI.LiveOut) { 462 if (Intf.last() >= SA->getLastSplitPoint(BC.Number)) 463 BC.Exit = SpillPlacement::MustSpill, ++Ins; 464 else if (Intf.last() > BI.LastUse) 465 BC.Exit = SpillPlacement::PrefSpill, ++Ins; 466 else if (Intf.last() > (BI.LiveThrough ? BI.FirstUse : BI.Def)) 467 ++Ins; 468 } 469 470 // Accumulate the total frequency of inserted spill code. 471 if (Ins) 472 StaticCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 473 } 474 Cost = StaticCost; 475 476 // Add constraints for use-blocks. Note that these are the only constraints 477 // that may add a positive bias, it is downhill from here. 478 SpillPlacer->addConstraints(SplitConstraints); 479 return SpillPlacer->scanActiveBundles(); 480 } 481 482 483 /// addThroughConstraints - Add constraints and links to SpillPlacer from the 484 /// live-through blocks in Blocks. 485 void RAGreedy::addThroughConstraints(InterferenceCache::Cursor Intf, 486 ArrayRef<unsigned> Blocks) { 487 const unsigned GroupSize = 8; 488 SpillPlacement::BlockConstraint BCS[GroupSize]; 489 unsigned TBS[GroupSize]; 490 unsigned B = 0, T = 0; 491 492 for (unsigned i = 0; i != Blocks.size(); ++i) { 493 unsigned Number = Blocks[i]; 494 Intf.moveToBlock(Number); 495 496 if (!Intf.hasInterference()) { 497 assert(T < GroupSize && "Array overflow"); 498 TBS[T] = Number; 499 if (++T == GroupSize) { 500 SpillPlacer->addLinks(ArrayRef<unsigned>(TBS, T)); 501 T = 0; 502 } 503 continue; 504 } 505 506 assert(B < GroupSize && "Array overflow"); 507 BCS[B].Number = Number; 508 509 // Interference for the live-in value. 510 if (Intf.first() <= Indexes->getMBBStartIdx(Number)) 511 BCS[B].Entry = SpillPlacement::MustSpill; 512 else 513 BCS[B].Entry = SpillPlacement::PrefSpill; 514 515 // Interference for the live-out value. 516 if (Intf.last() >= SA->getLastSplitPoint(Number)) 517 BCS[B].Exit = SpillPlacement::MustSpill; 518 else 519 BCS[B].Exit = SpillPlacement::PrefSpill; 520 521 if (++B == GroupSize) { 522 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); 523 SpillPlacer->addConstraints(Array); 524 B = 0; 525 } 526 } 527 528 ArrayRef<SpillPlacement::BlockConstraint> Array(BCS, B); 529 SpillPlacer->addConstraints(Array); 530 SpillPlacer->addLinks(ArrayRef<unsigned>(TBS, T)); 531 } 532 533 void RAGreedy::growRegion(GlobalSplitCandidate &Cand, 534 InterferenceCache::Cursor Intf) { 535 // Keep track of through blocks that have not been added to SpillPlacer. 536 BitVector Todo = SA->getThroughBlocks(); 537 SmallVectorImpl<unsigned> &ActiveBlocks = Cand.ActiveBlocks; 538 unsigned AddedTo = 0; 539 #ifndef NDEBUG 540 unsigned Visited = 0; 541 #endif 542 543 for (;;) { 544 ArrayRef<unsigned> NewBundles = SpillPlacer->getRecentPositive(); 545 if (NewBundles.empty()) 546 break; 547 // Find new through blocks in the periphery of PrefRegBundles. 548 for (int i = 0, e = NewBundles.size(); i != e; ++i) { 549 unsigned Bundle = NewBundles[i]; 550 // Look at all blocks connected to Bundle in the full graph. 551 ArrayRef<unsigned> Blocks = Bundles->getBlocks(Bundle); 552 for (ArrayRef<unsigned>::iterator I = Blocks.begin(), E = Blocks.end(); 553 I != E; ++I) { 554 unsigned Block = *I; 555 if (!Todo.test(Block)) 556 continue; 557 Todo.reset(Block); 558 // This is a new through block. Add it to SpillPlacer later. 559 ActiveBlocks.push_back(Block); 560 #ifndef NDEBUG 561 ++Visited; 562 #endif 563 } 564 } 565 // Any new blocks to add? 566 if (ActiveBlocks.size() > AddedTo) { 567 ArrayRef<unsigned> Add(&ActiveBlocks[AddedTo], 568 ActiveBlocks.size() - AddedTo); 569 addThroughConstraints(Intf, Add); 570 AddedTo = ActiveBlocks.size(); 571 } 572 // Perhaps iterating can enable more bundles? 573 SpillPlacer->iterate(); 574 } 575 DEBUG(dbgs() << ", v=" << Visited); 576 } 577 578 /// calcGlobalSplitCost - Return the global split cost of following the split 579 /// pattern in LiveBundles. This cost should be added to the local cost of the 580 /// interference pattern in SplitConstraints. 581 /// 582 float RAGreedy::calcGlobalSplitCost(GlobalSplitCandidate &Cand, 583 InterferenceCache::Cursor Intf) { 584 float GlobalCost = 0; 585 const BitVector &LiveBundles = Cand.LiveBundles; 586 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 587 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 588 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 589 SpillPlacement::BlockConstraint &BC = SplitConstraints[i]; 590 bool RegIn = LiveBundles[Bundles->getBundle(BC.Number, 0)]; 591 bool RegOut = LiveBundles[Bundles->getBundle(BC.Number, 1)]; 592 unsigned Ins = 0; 593 594 if (BI.LiveIn) 595 Ins += RegIn != (BC.Entry == SpillPlacement::PrefReg); 596 if (BI.LiveOut) 597 Ins += RegOut != (BC.Exit == SpillPlacement::PrefReg); 598 if (Ins) 599 GlobalCost += Ins * SpillPlacer->getBlockFrequency(BC.Number); 600 } 601 602 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { 603 unsigned Number = Cand.ActiveBlocks[i]; 604 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; 605 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; 606 if (!RegIn && !RegOut) 607 continue; 608 if (RegIn && RegOut) { 609 // We need double spill code if this block has interference. 610 Intf.moveToBlock(Number); 611 if (Intf.hasInterference()) 612 GlobalCost += 2*SpillPlacer->getBlockFrequency(Number); 613 continue; 614 } 615 // live-in / stack-out or stack-in live-out. 616 GlobalCost += SpillPlacer->getBlockFrequency(Number); 617 } 618 return GlobalCost; 619 } 620 621 /// splitAroundRegion - Split VirtReg around the region determined by 622 /// LiveBundles. Make an effort to avoid interference from PhysReg. 623 /// 624 /// The 'register' interval is going to contain as many uses as possible while 625 /// avoiding interference. The 'stack' interval is the complement constructed by 626 /// SplitEditor. It will contain the rest. 627 /// 628 void RAGreedy::splitAroundRegion(LiveInterval &VirtReg, 629 GlobalSplitCandidate &Cand, 630 SmallVectorImpl<LiveInterval*> &NewVRegs) { 631 const BitVector &LiveBundles = Cand.LiveBundles; 632 633 DEBUG({ 634 dbgs() << "Splitting around region for " << PrintReg(Cand.PhysReg, TRI) 635 << " with bundles"; 636 for (int i = LiveBundles.find_first(); i>=0; i = LiveBundles.find_next(i)) 637 dbgs() << " EB#" << i; 638 dbgs() << ".\n"; 639 }); 640 641 InterferenceCache::Cursor Intf(IntfCache, Cand.PhysReg); 642 LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 643 SE->reset(LREdit); 644 645 // Create the main cross-block interval. 646 const unsigned MainIntv = SE->openIntv(); 647 648 // First add all defs that are live out of a block. 649 ArrayRef<SplitAnalysis::BlockInfo> UseBlocks = SA->getUseBlocks(); 650 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 651 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 652 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)]; 653 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)]; 654 655 // Create separate intervals for isolated blocks with multiple uses. 656 if (!RegIn && !RegOut && BI.FirstUse != BI.LastUse) { 657 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " isolated.\n"); 658 SE->splitSingleBlock(BI); 659 SE->selectIntv(MainIntv); 660 continue; 661 } 662 663 // Should the register be live out? 664 if (!BI.LiveOut || !RegOut) 665 continue; 666 667 SlotIndex Start, Stop; 668 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB); 669 Intf.moveToBlock(BI.MBB->getNumber()); 670 DEBUG(dbgs() << "BB#" << BI.MBB->getNumber() << " -> EB#" 671 << Bundles->getBundle(BI.MBB->getNumber(), 1) 672 << " [" << Start << ';' 673 << SA->getLastSplitPoint(BI.MBB->getNumber()) << '-' << Stop 674 << ") intf [" << Intf.first() << ';' << Intf.last() << ')'); 675 676 // The interference interval should either be invalid or overlap MBB. 677 assert((!Intf.hasInterference() || Intf.first() < Stop) 678 && "Bad interference"); 679 assert((!Intf.hasInterference() || Intf.last() > Start) 680 && "Bad interference"); 681 682 // Check interference leaving the block. 683 if (!Intf.hasInterference()) { 684 // Block is interference-free. 685 DEBUG(dbgs() << ", no interference"); 686 if (!BI.LiveThrough) { 687 DEBUG(dbgs() << ", not live-through.\n"); 688 SE->useIntv(SE->enterIntvBefore(BI.Def), Stop); 689 continue; 690 } 691 if (!RegIn) { 692 // Block is live-through, but entry bundle is on the stack. 693 // Reload just before the first use. 694 DEBUG(dbgs() << ", not live-in, enter before first use.\n"); 695 SE->useIntv(SE->enterIntvBefore(BI.FirstUse), Stop); 696 continue; 697 } 698 DEBUG(dbgs() << ", live-through.\n"); 699 continue; 700 } 701 702 // Block has interference. 703 DEBUG(dbgs() << ", interference to " << Intf.last()); 704 705 if (!BI.LiveThrough && Intf.last() <= BI.Def) { 706 // The interference doesn't reach the outgoing segment. 707 DEBUG(dbgs() << " doesn't affect def from " << BI.Def << '\n'); 708 SE->useIntv(BI.Def, Stop); 709 continue; 710 } 711 712 SlotIndex LastSplitPoint = SA->getLastSplitPoint(BI.MBB->getNumber()); 713 if (Intf.last().getBoundaryIndex() < BI.LastUse) { 714 // There are interference-free uses at the end of the block. 715 // Find the first use that can get the live-out register. 716 SmallVectorImpl<SlotIndex>::const_iterator UI = 717 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(), 718 Intf.last().getBoundaryIndex()); 719 assert(UI != SA->UseSlots.end() && "Couldn't find last use"); 720 SlotIndex Use = *UI; 721 assert(Use <= BI.LastUse && "Couldn't find last use"); 722 // Only attempt a split befroe the last split point. 723 if (Use.getBaseIndex() <= LastSplitPoint) { 724 DEBUG(dbgs() << ", free use at " << Use << ".\n"); 725 SlotIndex SegStart = SE->enterIntvBefore(Use); 726 assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 727 assert(SegStart < LastSplitPoint && "Impossible split point"); 728 SE->useIntv(SegStart, Stop); 729 continue; 730 } 731 } 732 733 // Interference is after the last use. 734 DEBUG(dbgs() << " after last use.\n"); 735 SlotIndex SegStart = SE->enterIntvAtEnd(*BI.MBB); 736 assert(SegStart >= Intf.last() && "Couldn't avoid interference"); 737 } 738 739 // Now all defs leading to live bundles are handled, do everything else. 740 for (unsigned i = 0; i != UseBlocks.size(); ++i) { 741 const SplitAnalysis::BlockInfo &BI = UseBlocks[i]; 742 bool RegIn = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 0)]; 743 bool RegOut = LiveBundles[Bundles->getBundle(BI.MBB->getNumber(), 1)]; 744 745 // Is the register live-in? 746 if (!BI.LiveIn || !RegIn) 747 continue; 748 749 // We have an incoming register. Check for interference. 750 SlotIndex Start, Stop; 751 tie(Start, Stop) = Indexes->getMBBRange(BI.MBB); 752 Intf.moveToBlock(BI.MBB->getNumber()); 753 DEBUG(dbgs() << "EB#" << Bundles->getBundle(BI.MBB->getNumber(), 0) 754 << " -> BB#" << BI.MBB->getNumber() << " [" << Start << ';' 755 << SA->getLastSplitPoint(BI.MBB->getNumber()) << '-' << Stop 756 << ')'); 757 758 // Check interference entering the block. 759 if (!Intf.hasInterference()) { 760 // Block is interference-free. 761 DEBUG(dbgs() << ", no interference"); 762 if (!BI.LiveThrough) { 763 DEBUG(dbgs() << ", killed in block.\n"); 764 SE->useIntv(Start, SE->leaveIntvAfter(BI.Kill)); 765 continue; 766 } 767 if (!RegOut) { 768 SlotIndex LastSplitPoint = SA->getLastSplitPoint(BI.MBB->getNumber()); 769 // Block is live-through, but exit bundle is on the stack. 770 // Spill immediately after the last use. 771 if (BI.LastUse < LastSplitPoint) { 772 DEBUG(dbgs() << ", uses, stack-out.\n"); 773 SE->useIntv(Start, SE->leaveIntvAfter(BI.LastUse)); 774 continue; 775 } 776 // The last use is after the last split point, it is probably an 777 // indirect jump. 778 DEBUG(dbgs() << ", uses at " << BI.LastUse << " after split point " 779 << LastSplitPoint << ", stack-out.\n"); 780 SlotIndex SegEnd = SE->leaveIntvBefore(LastSplitPoint); 781 SE->useIntv(Start, SegEnd); 782 // Run a double interval from the split to the last use. 783 // This makes it possible to spill the complement without affecting the 784 // indirect branch. 785 SE->overlapIntv(SegEnd, BI.LastUse); 786 continue; 787 } 788 // Register is live-through. 789 DEBUG(dbgs() << ", uses, live-through.\n"); 790 SE->useIntv(Start, Stop); 791 continue; 792 } 793 794 // Block has interference. 795 DEBUG(dbgs() << ", interference from " << Intf.first()); 796 797 if (!BI.LiveThrough && Intf.first() >= BI.Kill) { 798 // The interference doesn't reach the outgoing segment. 799 DEBUG(dbgs() << " doesn't affect kill at " << BI.Kill << '\n'); 800 SE->useIntv(Start, BI.Kill); 801 continue; 802 } 803 804 if (Intf.first().getBaseIndex() > BI.FirstUse) { 805 // There are interference-free uses at the beginning of the block. 806 // Find the last use that can get the register. 807 SmallVectorImpl<SlotIndex>::const_iterator UI = 808 std::lower_bound(SA->UseSlots.begin(), SA->UseSlots.end(), 809 Intf.first().getBaseIndex()); 810 assert(UI != SA->UseSlots.begin() && "Couldn't find first use"); 811 SlotIndex Use = (--UI)->getBoundaryIndex(); 812 DEBUG(dbgs() << ", free use at " << *UI << ".\n"); 813 SlotIndex SegEnd = SE->leaveIntvAfter(Use); 814 assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 815 SE->useIntv(Start, SegEnd); 816 continue; 817 } 818 819 // Interference is before the first use. 820 DEBUG(dbgs() << " before first use.\n"); 821 SlotIndex SegEnd = SE->leaveIntvAtTop(*BI.MBB); 822 assert(SegEnd <= Intf.first() && "Couldn't avoid interference"); 823 } 824 825 // Handle live-through blocks. 826 for (unsigned i = 0, e = Cand.ActiveBlocks.size(); i != e; ++i) { 827 unsigned Number = Cand.ActiveBlocks[i]; 828 bool RegIn = LiveBundles[Bundles->getBundle(Number, 0)]; 829 bool RegOut = LiveBundles[Bundles->getBundle(Number, 1)]; 830 DEBUG(dbgs() << "Live through BB#" << Number << '\n'); 831 if (RegIn && RegOut) { 832 Intf.moveToBlock(Number); 833 if (!Intf.hasInterference()) { 834 SE->useIntv(Indexes->getMBBStartIdx(Number), 835 Indexes->getMBBEndIdx(Number)); 836 continue; 837 } 838 } 839 MachineBasicBlock *MBB = MF->getBlockNumbered(Number); 840 if (RegIn) 841 SE->leaveIntvAtTop(*MBB); 842 if (RegOut) 843 SE->enterIntvAtEnd(*MBB); 844 } 845 846 // FIXME: Should we be more aggressive about splitting the stack region into 847 // per-block segments? The current approach allows the stack region to 848 // separate into connected components. Some components may be allocatable. 849 SE->finish(); 850 ++NumGlobalSplits; 851 852 if (VerifyEnabled) 853 MF->verify(this, "After splitting live range around region"); 854 } 855 856 unsigned RAGreedy::tryRegionSplit(LiveInterval &VirtReg, AllocationOrder &Order, 857 SmallVectorImpl<LiveInterval*> &NewVRegs) { 858 float BestCost = 0; 859 const unsigned NoCand = ~0u; 860 unsigned BestCand = NoCand; 861 862 Order.rewind(); 863 for (unsigned Cand = 0; unsigned PhysReg = Order.next(); ++Cand) { 864 if (GlobalCand.size() <= Cand) 865 GlobalCand.resize(Cand+1); 866 GlobalCand[Cand].reset(PhysReg); 867 868 SpillPlacer->prepare(GlobalCand[Cand].LiveBundles); 869 float Cost; 870 InterferenceCache::Cursor Intf(IntfCache, PhysReg); 871 if (!addSplitConstraints(Intf, Cost)) { 872 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tno positive bundles\n"); 873 continue; 874 } 875 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << "\tstatic = " << Cost); 876 if (BestCand != NoCand && Cost >= BestCost) { 877 DEBUG(dbgs() << " worse than " 878 << PrintReg(GlobalCand[BestCand].PhysReg, TRI) << '\n'); 879 continue; 880 } 881 growRegion(GlobalCand[Cand], Intf); 882 883 SpillPlacer->finish(); 884 885 // No live bundles, defer to splitSingleBlocks(). 886 if (!GlobalCand[Cand].LiveBundles.any()) { 887 DEBUG(dbgs() << " no bundles.\n"); 888 continue; 889 } 890 891 Cost += calcGlobalSplitCost(GlobalCand[Cand], Intf); 892 DEBUG({ 893 dbgs() << ", total = " << Cost << " with bundles"; 894 for (int i = GlobalCand[Cand].LiveBundles.find_first(); i>=0; 895 i = GlobalCand[Cand].LiveBundles.find_next(i)) 896 dbgs() << " EB#" << i; 897 dbgs() << ".\n"; 898 }); 899 if (BestCand == NoCand || Cost < BestCost) { 900 BestCand = Cand; 901 BestCost = 0.98f * Cost; // Prevent rounding effects. 902 } 903 } 904 905 if (BestCand == NoCand) 906 return 0; 907 908 splitAroundRegion(VirtReg, GlobalCand[BestCand], NewVRegs); 909 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Global); 910 return 0; 911 } 912 913 914 //===----------------------------------------------------------------------===// 915 // Local Splitting 916 //===----------------------------------------------------------------------===// 917 918 919 /// calcGapWeights - Compute the maximum spill weight that needs to be evicted 920 /// in order to use PhysReg between two entries in SA->UseSlots. 921 /// 922 /// GapWeight[i] represents the gap between UseSlots[i] and UseSlots[i+1]. 923 /// 924 void RAGreedy::calcGapWeights(unsigned PhysReg, 925 SmallVectorImpl<float> &GapWeight) { 926 assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 927 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 928 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 929 const unsigned NumGaps = Uses.size()-1; 930 931 // Start and end points for the interference check. 932 SlotIndex StartIdx = BI.LiveIn ? BI.FirstUse.getBaseIndex() : BI.FirstUse; 933 SlotIndex StopIdx = BI.LiveOut ? BI.LastUse.getBoundaryIndex() : BI.LastUse; 934 935 GapWeight.assign(NumGaps, 0.0f); 936 937 // Add interference from each overlapping register. 938 for (const unsigned *AI = TRI->getOverlaps(PhysReg); *AI; ++AI) { 939 if (!query(const_cast<LiveInterval&>(SA->getParent()), *AI) 940 .checkInterference()) 941 continue; 942 943 // We know that VirtReg is a continuous interval from FirstUse to LastUse, 944 // so we don't need InterferenceQuery. 945 // 946 // Interference that overlaps an instruction is counted in both gaps 947 // surrounding the instruction. The exception is interference before 948 // StartIdx and after StopIdx. 949 // 950 LiveIntervalUnion::SegmentIter IntI = PhysReg2LiveUnion[*AI].find(StartIdx); 951 for (unsigned Gap = 0; IntI.valid() && IntI.start() < StopIdx; ++IntI) { 952 // Skip the gaps before IntI. 953 while (Uses[Gap+1].getBoundaryIndex() < IntI.start()) 954 if (++Gap == NumGaps) 955 break; 956 if (Gap == NumGaps) 957 break; 958 959 // Update the gaps covered by IntI. 960 const float weight = IntI.value()->weight; 961 for (; Gap != NumGaps; ++Gap) { 962 GapWeight[Gap] = std::max(GapWeight[Gap], weight); 963 if (Uses[Gap+1].getBaseIndex() >= IntI.stop()) 964 break; 965 } 966 if (Gap == NumGaps) 967 break; 968 } 969 } 970 } 971 972 /// getPrevMappedIndex - Return the slot index of the last non-copy instruction 973 /// before MI that has a slot index. If MI is the first mapped instruction in 974 /// its block, return the block start index instead. 975 /// 976 SlotIndex RAGreedy::getPrevMappedIndex(const MachineInstr *MI) { 977 assert(MI && "Missing MachineInstr"); 978 const MachineBasicBlock *MBB = MI->getParent(); 979 MachineBasicBlock::const_iterator B = MBB->begin(), I = MI; 980 while (I != B) 981 if (!(--I)->isDebugValue() && !I->isCopy()) 982 return Indexes->getInstructionIndex(I); 983 return Indexes->getMBBStartIdx(MBB); 984 } 985 986 /// calcPrevSlots - Fill in the PrevSlot array with the index of the previous 987 /// real non-copy instruction for each instruction in SA->UseSlots. 988 /// 989 void RAGreedy::calcPrevSlots() { 990 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 991 PrevSlot.clear(); 992 PrevSlot.reserve(Uses.size()); 993 for (unsigned i = 0, e = Uses.size(); i != e; ++i) { 994 const MachineInstr *MI = Indexes->getInstructionFromIndex(Uses[i]); 995 PrevSlot.push_back(getPrevMappedIndex(MI).getDefIndex()); 996 } 997 } 998 999 /// nextSplitPoint - Find the next index into SA->UseSlots > i such that it may 1000 /// be beneficial to split before UseSlots[i]. 1001 /// 1002 /// 0 is always a valid split point 1003 unsigned RAGreedy::nextSplitPoint(unsigned i) { 1004 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 1005 const unsigned Size = Uses.size(); 1006 assert(i != Size && "No split points after the end"); 1007 // Allow split before i when Uses[i] is not adjacent to the previous use. 1008 while (++i != Size && PrevSlot[i].getBaseIndex() <= Uses[i-1].getBaseIndex()) 1009 ; 1010 return i; 1011 } 1012 1013 /// tryLocalSplit - Try to split VirtReg into smaller intervals inside its only 1014 /// basic block. 1015 /// 1016 unsigned RAGreedy::tryLocalSplit(LiveInterval &VirtReg, AllocationOrder &Order, 1017 SmallVectorImpl<LiveInterval*> &NewVRegs) { 1018 assert(SA->getUseBlocks().size() == 1 && "Not a local interval"); 1019 const SplitAnalysis::BlockInfo &BI = SA->getUseBlocks().front(); 1020 1021 // Note that it is possible to have an interval that is live-in or live-out 1022 // while only covering a single block - A phi-def can use undef values from 1023 // predecessors, and the block could be a single-block loop. 1024 // We don't bother doing anything clever about such a case, we simply assume 1025 // that the interval is continuous from FirstUse to LastUse. We should make 1026 // sure that we don't do anything illegal to such an interval, though. 1027 1028 const SmallVectorImpl<SlotIndex> &Uses = SA->UseSlots; 1029 if (Uses.size() <= 2) 1030 return 0; 1031 const unsigned NumGaps = Uses.size()-1; 1032 1033 DEBUG({ 1034 dbgs() << "tryLocalSplit: "; 1035 for (unsigned i = 0, e = Uses.size(); i != e; ++i) 1036 dbgs() << ' ' << SA->UseSlots[i]; 1037 dbgs() << '\n'; 1038 }); 1039 1040 // For every use, find the previous mapped non-copy instruction. 1041 // We use this to detect valid split points, and to estimate new interval 1042 // sizes. 1043 calcPrevSlots(); 1044 1045 unsigned BestBefore = NumGaps; 1046 unsigned BestAfter = 0; 1047 float BestDiff = 0; 1048 1049 const float blockFreq = SpillPlacer->getBlockFrequency(BI.MBB->getNumber()); 1050 SmallVector<float, 8> GapWeight; 1051 1052 Order.rewind(); 1053 while (unsigned PhysReg = Order.next()) { 1054 // Keep track of the largest spill weight that would need to be evicted in 1055 // order to make use of PhysReg between UseSlots[i] and UseSlots[i+1]. 1056 calcGapWeights(PhysReg, GapWeight); 1057 1058 // Try to find the best sequence of gaps to close. 1059 // The new spill weight must be larger than any gap interference. 1060 1061 // We will split before Uses[SplitBefore] and after Uses[SplitAfter]. 1062 unsigned SplitBefore = 0, SplitAfter = nextSplitPoint(1) - 1; 1063 1064 // MaxGap should always be max(GapWeight[SplitBefore..SplitAfter-1]). 1065 // It is the spill weight that needs to be evicted. 1066 float MaxGap = GapWeight[0]; 1067 for (unsigned i = 1; i != SplitAfter; ++i) 1068 MaxGap = std::max(MaxGap, GapWeight[i]); 1069 1070 for (;;) { 1071 // Live before/after split? 1072 const bool LiveBefore = SplitBefore != 0 || BI.LiveIn; 1073 const bool LiveAfter = SplitAfter != NumGaps || BI.LiveOut; 1074 1075 DEBUG(dbgs() << PrintReg(PhysReg, TRI) << ' ' 1076 << Uses[SplitBefore] << '-' << Uses[SplitAfter] 1077 << " i=" << MaxGap); 1078 1079 // Stop before the interval gets so big we wouldn't be making progress. 1080 if (!LiveBefore && !LiveAfter) { 1081 DEBUG(dbgs() << " all\n"); 1082 break; 1083 } 1084 // Should the interval be extended or shrunk? 1085 bool Shrink = true; 1086 if (MaxGap < HUGE_VALF) { 1087 // Estimate the new spill weight. 1088 // 1089 // Each instruction reads and writes the register, except the first 1090 // instr doesn't read when !FirstLive, and the last instr doesn't write 1091 // when !LastLive. 1092 // 1093 // We will be inserting copies before and after, so the total number of 1094 // reads and writes is 2 * EstUses. 1095 // 1096 const unsigned EstUses = 2*(SplitAfter - SplitBefore) + 1097 2*(LiveBefore + LiveAfter); 1098 1099 // Try to guess the size of the new interval. This should be trivial, 1100 // but the slot index of an inserted copy can be a lot smaller than the 1101 // instruction it is inserted before if there are many dead indexes 1102 // between them. 1103 // 1104 // We measure the distance from the instruction before SplitBefore to 1105 // get a conservative estimate. 1106 // 1107 // The final distance can still be different if inserting copies 1108 // triggers a slot index renumbering. 1109 // 1110 const float EstWeight = normalizeSpillWeight(blockFreq * EstUses, 1111 PrevSlot[SplitBefore].distance(Uses[SplitAfter])); 1112 // Would this split be possible to allocate? 1113 // Never allocate all gaps, we wouldn't be making progress. 1114 float Diff = EstWeight - MaxGap; 1115 DEBUG(dbgs() << " w=" << EstWeight << " d=" << Diff); 1116 if (Diff > 0) { 1117 Shrink = false; 1118 if (Diff > BestDiff) { 1119 DEBUG(dbgs() << " (best)"); 1120 BestDiff = Diff; 1121 BestBefore = SplitBefore; 1122 BestAfter = SplitAfter; 1123 } 1124 } 1125 } 1126 1127 // Try to shrink. 1128 if (Shrink) { 1129 SplitBefore = nextSplitPoint(SplitBefore); 1130 if (SplitBefore < SplitAfter) { 1131 DEBUG(dbgs() << " shrink\n"); 1132 // Recompute the max when necessary. 1133 if (GapWeight[SplitBefore - 1] >= MaxGap) { 1134 MaxGap = GapWeight[SplitBefore]; 1135 for (unsigned i = SplitBefore + 1; i != SplitAfter; ++i) 1136 MaxGap = std::max(MaxGap, GapWeight[i]); 1137 } 1138 continue; 1139 } 1140 MaxGap = 0; 1141 } 1142 1143 // Try to extend the interval. 1144 if (SplitAfter >= NumGaps) { 1145 DEBUG(dbgs() << " end\n"); 1146 break; 1147 } 1148 1149 DEBUG(dbgs() << " extend\n"); 1150 for (unsigned e = nextSplitPoint(SplitAfter + 1) - 1; 1151 SplitAfter != e; ++SplitAfter) 1152 MaxGap = std::max(MaxGap, GapWeight[SplitAfter]); 1153 continue; 1154 } 1155 } 1156 1157 // Didn't find any candidates? 1158 if (BestBefore == NumGaps) 1159 return 0; 1160 1161 DEBUG(dbgs() << "Best local split range: " << Uses[BestBefore] 1162 << '-' << Uses[BestAfter] << ", " << BestDiff 1163 << ", " << (BestAfter - BestBefore + 1) << " instrs\n"); 1164 1165 LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 1166 SE->reset(LREdit); 1167 1168 SE->openIntv(); 1169 SlotIndex SegStart = SE->enterIntvBefore(Uses[BestBefore]); 1170 SlotIndex SegStop = SE->leaveIntvAfter(Uses[BestAfter]); 1171 SE->useIntv(SegStart, SegStop); 1172 SE->finish(); 1173 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Local); 1174 ++NumLocalSplits; 1175 1176 return 0; 1177 } 1178 1179 //===----------------------------------------------------------------------===// 1180 // Live Range Splitting 1181 //===----------------------------------------------------------------------===// 1182 1183 /// trySplit - Try to split VirtReg or one of its interferences, making it 1184 /// assignable. 1185 /// @return Physreg when VirtReg may be assigned and/or new NewVRegs. 1186 unsigned RAGreedy::trySplit(LiveInterval &VirtReg, AllocationOrder &Order, 1187 SmallVectorImpl<LiveInterval*>&NewVRegs) { 1188 // Local intervals are handled separately. 1189 if (LIS->intervalIsInOneMBB(VirtReg)) { 1190 NamedRegionTimer T("Local Splitting", TimerGroupName, TimePassesIsEnabled); 1191 SA->analyze(&VirtReg); 1192 return tryLocalSplit(VirtReg, Order, NewVRegs); 1193 } 1194 1195 NamedRegionTimer T("Global Splitting", TimerGroupName, TimePassesIsEnabled); 1196 1197 // Don't iterate global splitting. 1198 // Move straight to spilling if this range was produced by a global split. 1199 if (getStage(VirtReg) >= RS_Global) 1200 return 0; 1201 1202 SA->analyze(&VirtReg); 1203 1204 // First try to split around a region spanning multiple blocks. 1205 unsigned PhysReg = tryRegionSplit(VirtReg, Order, NewVRegs); 1206 if (PhysReg || !NewVRegs.empty()) 1207 return PhysReg; 1208 1209 // Then isolate blocks with multiple uses. 1210 SplitAnalysis::BlockPtrSet Blocks; 1211 if (SA->getMultiUseBlocks(Blocks)) { 1212 LiveRangeEdit LREdit(VirtReg, NewVRegs, this); 1213 SE->reset(LREdit); 1214 SE->splitSingleBlocks(Blocks); 1215 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Global); 1216 if (VerifyEnabled) 1217 MF->verify(this, "After splitting live range around basic blocks"); 1218 } 1219 1220 // Don't assign any physregs. 1221 return 0; 1222 } 1223 1224 1225 //===----------------------------------------------------------------------===// 1226 // Main Entry Point 1227 //===----------------------------------------------------------------------===// 1228 1229 unsigned RAGreedy::selectOrSplit(LiveInterval &VirtReg, 1230 SmallVectorImpl<LiveInterval*> &NewVRegs) { 1231 // First try assigning a free register. 1232 AllocationOrder Order(VirtReg.reg, *VRM, ReservedRegs); 1233 while (unsigned PhysReg = Order.next()) { 1234 if (!checkPhysRegInterference(VirtReg, PhysReg)) 1235 return PhysReg; 1236 } 1237 1238 if (unsigned PhysReg = tryEvict(VirtReg, Order, NewVRegs)) 1239 return PhysReg; 1240 1241 assert(NewVRegs.empty() && "Cannot append to existing NewVRegs"); 1242 1243 // The first time we see a live range, don't try to split or spill. 1244 // Wait until the second time, when all smaller ranges have been allocated. 1245 // This gives a better picture of the interference to split around. 1246 LiveRangeStage Stage = getStage(VirtReg); 1247 if (Stage == RS_First) { 1248 LRStage[VirtReg.reg] = RS_Second; 1249 DEBUG(dbgs() << "wait for second round\n"); 1250 NewVRegs.push_back(&VirtReg); 1251 return 0; 1252 } 1253 1254 assert(Stage < RS_Spill && "Cannot allocate after spilling"); 1255 1256 // Try splitting VirtReg or interferences. 1257 unsigned PhysReg = trySplit(VirtReg, Order, NewVRegs); 1258 if (PhysReg || !NewVRegs.empty()) 1259 return PhysReg; 1260 1261 // Finally spill VirtReg itself. 1262 NamedRegionTimer T("Spiller", TimerGroupName, TimePassesIsEnabled); 1263 LiveRangeEdit LRE(VirtReg, NewVRegs, this); 1264 spiller().spill(LRE); 1265 setStage(NewVRegs.begin(), NewVRegs.end(), RS_Spill); 1266 1267 if (VerifyEnabled) 1268 MF->verify(this, "After spilling"); 1269 1270 // The live virtual register requesting allocation was spilled, so tell 1271 // the caller not to allocate anything during this round. 1272 return 0; 1273 } 1274 1275 bool RAGreedy::runOnMachineFunction(MachineFunction &mf) { 1276 DEBUG(dbgs() << "********** GREEDY REGISTER ALLOCATION **********\n" 1277 << "********** Function: " 1278 << ((Value*)mf.getFunction())->getName() << '\n'); 1279 1280 MF = &mf; 1281 if (VerifyEnabled) 1282 MF->verify(this, "Before greedy register allocator"); 1283 1284 RegAllocBase::init(getAnalysis<VirtRegMap>(), getAnalysis<LiveIntervals>()); 1285 Indexes = &getAnalysis<SlotIndexes>(); 1286 DomTree = &getAnalysis<MachineDominatorTree>(); 1287 ReservedRegs = TRI->getReservedRegs(*MF); 1288 SpillerInstance.reset(createInlineSpiller(*this, *MF, *VRM)); 1289 Loops = &getAnalysis<MachineLoopInfo>(); 1290 LoopRanges = &getAnalysis<MachineLoopRanges>(); 1291 Bundles = &getAnalysis<EdgeBundles>(); 1292 SpillPlacer = &getAnalysis<SpillPlacement>(); 1293 1294 SA.reset(new SplitAnalysis(*VRM, *LIS, *Loops)); 1295 SE.reset(new SplitEditor(*SA, *LIS, *VRM, *DomTree)); 1296 LRStage.clear(); 1297 LRStage.resize(MRI->getNumVirtRegs()); 1298 IntfCache.init(MF, &PhysReg2LiveUnion[0], Indexes, TRI); 1299 1300 allocatePhysRegs(); 1301 addMBBLiveIns(MF); 1302 LIS->addKillFlags(); 1303 1304 // Run rewriter 1305 { 1306 NamedRegionTimer T("Rewriter", TimerGroupName, TimePassesIsEnabled); 1307 VRM->rewrite(Indexes); 1308 } 1309 1310 // Write out new DBG_VALUE instructions. 1311 getAnalysis<LiveDebugVariables>().emitDebugValues(VRM); 1312 1313 // The pass output is in VirtRegMap. Release all the transient data. 1314 releaseMemory(); 1315 1316 return true; 1317 } 1318