LLVM API Documentation
00001 //===-- RegAllocLinearScan.cpp - Linear Scan 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 implements a linear scan register allocator. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #define DEBUG_TYPE "regalloc" 00015 #include "PhysRegTracker.h" 00016 #include "VirtRegMap.h" 00017 #include "llvm/Function.h" 00018 #include "llvm/CodeGen/LiveIntervalAnalysis.h" 00019 #include "llvm/CodeGen/LiveStackAnalysis.h" 00020 #include "llvm/CodeGen/MachineFunctionPass.h" 00021 #include "llvm/CodeGen/MachineInstr.h" 00022 #include "llvm/CodeGen/MachineLoopInfo.h" 00023 #include "llvm/CodeGen/MachineRegisterInfo.h" 00024 #include "llvm/CodeGen/Passes.h" 00025 #include "llvm/CodeGen/RegAllocRegistry.h" 00026 #include "llvm/CodeGen/RegisterCoalescer.h" 00027 #include "llvm/Target/TargetRegisterInfo.h" 00028 #include "llvm/Target/TargetMachine.h" 00029 #include "llvm/Target/TargetOptions.h" 00030 #include "llvm/Target/TargetInstrInfo.h" 00031 #include "llvm/ADT/EquivalenceClasses.h" 00032 #include "llvm/ADT/SmallSet.h" 00033 #include "llvm/ADT/Statistic.h" 00034 #include "llvm/ADT/STLExtras.h" 00035 #include "llvm/Support/Debug.h" 00036 #include "llvm/Support/Compiler.h" 00037 #include <algorithm> 00038 #include <set> 00039 #include <queue> 00040 #include <memory> 00041 #include <cmath> 00042 using namespace llvm; 00043 00044 STATISTIC(NumIters , "Number of iterations performed"); 00045 STATISTIC(NumBacktracks, "Number of times we had to backtrack"); 00046 STATISTIC(NumCoalesce, "Number of copies coalesced"); 00047 00048 static cl::opt<bool> 00049 NewHeuristic("new-spilling-heuristic", 00050 cl::desc("Use new spilling heuristic"), 00051 cl::init(false), cl::Hidden); 00052 00053 static cl::opt<bool> 00054 PreSplitIntervals("pre-alloc-split", 00055 cl::desc("Pre-register allocation live interval splitting"), 00056 cl::init(false), cl::Hidden); 00057 00058 static RegisterRegAlloc 00059 linearscanRegAlloc("linearscan", "linear scan register allocator", 00060 createLinearScanRegisterAllocator); 00061 00062 namespace { 00063 struct VISIBILITY_HIDDEN RALinScan : public MachineFunctionPass { 00064 static char ID; 00065 RALinScan() : MachineFunctionPass(&ID) {} 00066 00067 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr; 00068 typedef SmallVector<IntervalPtr, 32> IntervalPtrs; 00069 private: 00070 /// RelatedRegClasses - This structure is built the first time a function is 00071 /// compiled, and keeps track of which register classes have registers that 00072 /// belong to multiple classes or have aliases that are in other classes. 00073 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses; 00074 DenseMap<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg; 00075 00076 MachineFunction* mf_; 00077 MachineRegisterInfo* mri_; 00078 const TargetMachine* tm_; 00079 const TargetRegisterInfo* tri_; 00080 const TargetInstrInfo* tii_; 00081 BitVector allocatableRegs_; 00082 LiveIntervals* li_; 00083 LiveStacks* ls_; 00084 const MachineLoopInfo *loopInfo; 00085 00086 /// handled_ - Intervals are added to the handled_ set in the order of their 00087 /// start value. This is uses for backtracking. 00088 std::vector<LiveInterval*> handled_; 00089 00090 /// fixed_ - Intervals that correspond to machine registers. 00091 /// 00092 IntervalPtrs fixed_; 00093 00094 /// active_ - Intervals that are currently being processed, and which have a 00095 /// live range active for the current point. 00096 IntervalPtrs active_; 00097 00098 /// inactive_ - Intervals that are currently being processed, but which have 00099 /// a hold at the current point. 00100 IntervalPtrs inactive_; 00101 00102 typedef std::priority_queue<LiveInterval*, 00103 SmallVector<LiveInterval*, 64>, 00104 greater_ptr<LiveInterval> > IntervalHeap; 00105 IntervalHeap unhandled_; 00106 std::auto_ptr<PhysRegTracker> prt_; 00107 std::auto_ptr<VirtRegMap> vrm_; 00108 std::auto_ptr<Spiller> spiller_; 00109 00110 public: 00111 virtual const char* getPassName() const { 00112 return "Linear Scan Register Allocator"; 00113 } 00114 00115 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00116 AU.addRequired<LiveIntervals>(); 00117 if (StrongPHIElim) 00118 AU.addRequiredID(StrongPHIEliminationID); 00119 // Make sure PassManager knows which analyses to make available 00120 // to coalescing and which analyses coalescing invalidates. 00121 AU.addRequiredTransitive<RegisterCoalescer>(); 00122 if (PreSplitIntervals) 00123 AU.addRequiredID(PreAllocSplittingID); 00124 AU.addRequired<LiveStacks>(); 00125 AU.addPreserved<LiveStacks>(); 00126 AU.addRequired<MachineLoopInfo>(); 00127 AU.addPreserved<MachineLoopInfo>(); 00128 AU.addPreservedID(MachineDominatorsID); 00129 MachineFunctionPass::getAnalysisUsage(AU); 00130 } 00131 00132 /// runOnMachineFunction - register allocate the whole function 00133 bool runOnMachineFunction(MachineFunction&); 00134 00135 private: 00136 /// linearScan - the linear scan algorithm 00137 void linearScan(); 00138 00139 /// initIntervalSets - initialize the interval sets. 00140 /// 00141 void initIntervalSets(); 00142 00143 /// processActiveIntervals - expire old intervals and move non-overlapping 00144 /// ones to the inactive list. 00145 void processActiveIntervals(unsigned CurPoint); 00146 00147 /// processInactiveIntervals - expire old intervals and move overlapping 00148 /// ones to the active list. 00149 void processInactiveIntervals(unsigned CurPoint); 00150 00151 /// assignRegOrStackSlotAtInterval - assign a register if one 00152 /// is available, or spill. 00153 void assignRegOrStackSlotAtInterval(LiveInterval* cur); 00154 00155 /// findIntervalsToSpill - Determine the intervals to spill for the 00156 /// specified interval. It's passed the physical registers whose spill 00157 /// weight is the lowest among all the registers whose live intervals 00158 /// conflict with the interval. 00159 void findIntervalsToSpill(LiveInterval *cur, 00160 std::vector<std::pair<unsigned,float> > &Candidates, 00161 unsigned NumCands, 00162 SmallVector<LiveInterval*, 8> &SpillIntervals); 00163 00164 /// attemptTrivialCoalescing - If a simple interval is defined by a copy, 00165 /// try allocate the definition the same register as the source register 00166 /// if the register is not defined during live time of the interval. This 00167 /// eliminate a copy. This is used to coalesce copies which were not 00168 /// coalesced away before allocation either due to dest and src being in 00169 /// different register classes or because the coalescer was overly 00170 /// conservative. 00171 unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg); 00172 00173 /// 00174 /// register handling helpers 00175 /// 00176 00177 /// getFreePhysReg - return a free physical register for this virtual 00178 /// register interval if we have one, otherwise return 0. 00179 unsigned getFreePhysReg(LiveInterval* cur); 00180 00181 /// assignVirt2StackSlot - assigns this virtual register to a 00182 /// stack slot. returns the stack slot 00183 int assignVirt2StackSlot(unsigned virtReg); 00184 00185 void ComputeRelatedRegClasses(); 00186 00187 template <typename ItTy> 00188 void printIntervals(const char* const str, ItTy i, ItTy e) const { 00189 if (str) DOUT << str << " intervals:\n"; 00190 for (; i != e; ++i) { 00191 DOUT << "\t" << *i->first << " -> "; 00192 unsigned reg = i->first->reg; 00193 if (TargetRegisterInfo::isVirtualRegister(reg)) { 00194 reg = vrm_->getPhys(reg); 00195 } 00196 DOUT << tri_->getName(reg) << '\n'; 00197 } 00198 } 00199 }; 00200 char RALinScan::ID = 0; 00201 } 00202 00203 static RegisterPass<RALinScan> 00204 X("linearscan-regalloc", "Linear Scan Register Allocator"); 00205 00206 void RALinScan::ComputeRelatedRegClasses() { 00207 const TargetRegisterInfo &TRI = *tri_; 00208 00209 // First pass, add all reg classes to the union, and determine at least one 00210 // reg class that each register is in. 00211 bool HasAliases = false; 00212 for (TargetRegisterInfo::regclass_iterator RCI = TRI.regclass_begin(), 00213 E = TRI.regclass_end(); RCI != E; ++RCI) { 00214 RelatedRegClasses.insert(*RCI); 00215 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end(); 00216 I != E; ++I) { 00217 HasAliases = HasAliases || *TRI.getAliasSet(*I) != 0; 00218 00219 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I]; 00220 if (PRC) { 00221 // Already processed this register. Just make sure we know that 00222 // multiple register classes share a register. 00223 RelatedRegClasses.unionSets(PRC, *RCI); 00224 } else { 00225 PRC = *RCI; 00226 } 00227 } 00228 } 00229 00230 // Second pass, now that we know conservatively what register classes each reg 00231 // belongs to, add info about aliases. We don't need to do this for targets 00232 // without register aliases. 00233 if (HasAliases) 00234 for (DenseMap<unsigned, const TargetRegisterClass*>::iterator 00235 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end(); 00236 I != E; ++I) 00237 for (const unsigned *AS = TRI.getAliasSet(I->first); *AS; ++AS) 00238 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]); 00239 } 00240 00241 /// attemptTrivialCoalescing - If a simple interval is defined by a copy, 00242 /// try allocate the definition the same register as the source register 00243 /// if the register is not defined during live time of the interval. This 00244 /// eliminate a copy. This is used to coalesce copies which were not 00245 /// coalesced away before allocation either due to dest and src being in 00246 /// different register classes or because the coalescer was overly 00247 /// conservative. 00248 unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) { 00249 if ((cur.preference && cur.preference == Reg) || !cur.containsOneValue()) 00250 return Reg; 00251 00252 VNInfo *vni = cur.getValNumInfo(0); 00253 if (!vni->def || vni->def == ~1U || vni->def == ~0U) 00254 return Reg; 00255 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); 00256 unsigned SrcReg, DstReg; 00257 if (!CopyMI || !tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) 00258 return Reg; 00259 if (TargetRegisterInfo::isVirtualRegister(SrcReg)) { 00260 if (!vrm_->isAssignedReg(SrcReg)) 00261 return Reg; 00262 else 00263 SrcReg = vrm_->getPhys(SrcReg); 00264 } 00265 if (Reg == SrcReg) 00266 return Reg; 00267 00268 const TargetRegisterClass *RC = mri_->getRegClass(cur.reg); 00269 if (!RC->contains(SrcReg)) 00270 return Reg; 00271 00272 // Try to coalesce. 00273 if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) { 00274 DOUT << "Coalescing: " << cur << " -> " << tri_->getName(SrcReg) 00275 << '\n'; 00276 vrm_->clearVirt(cur.reg); 00277 vrm_->assignVirt2Phys(cur.reg, SrcReg); 00278 ++NumCoalesce; 00279 return SrcReg; 00280 } 00281 00282 return Reg; 00283 } 00284 00285 bool RALinScan::runOnMachineFunction(MachineFunction &fn) { 00286 mf_ = &fn; 00287 mri_ = &fn.getRegInfo(); 00288 tm_ = &fn.getTarget(); 00289 tri_ = tm_->getRegisterInfo(); 00290 tii_ = tm_->getInstrInfo(); 00291 allocatableRegs_ = tri_->getAllocatableSet(fn); 00292 li_ = &getAnalysis<LiveIntervals>(); 00293 ls_ = &getAnalysis<LiveStacks>(); 00294 loopInfo = &getAnalysis<MachineLoopInfo>(); 00295 00296 // We don't run the coalescer here because we have no reason to 00297 // interact with it. If the coalescer requires interaction, it 00298 // won't do anything. If it doesn't require interaction, we assume 00299 // it was run as a separate pass. 00300 00301 // If this is the first function compiled, compute the related reg classes. 00302 if (RelatedRegClasses.empty()) 00303 ComputeRelatedRegClasses(); 00304 00305 if (!prt_.get()) prt_.reset(new PhysRegTracker(*tri_)); 00306 vrm_.reset(new VirtRegMap(*mf_)); 00307 if (!spiller_.get()) spiller_.reset(createSpiller()); 00308 00309 initIntervalSets(); 00310 00311 linearScan(); 00312 00313 // Rewrite spill code and update the PhysRegsUsed set. 00314 spiller_->runOnMachineFunction(*mf_, *vrm_); 00315 vrm_.reset(); // Free the VirtRegMap 00316 00317 assert(unhandled_.empty() && "Unhandled live intervals remain!"); 00318 fixed_.clear(); 00319 active_.clear(); 00320 inactive_.clear(); 00321 handled_.clear(); 00322 00323 return true; 00324 } 00325 00326 /// initIntervalSets - initialize the interval sets. 00327 /// 00328 void RALinScan::initIntervalSets() 00329 { 00330 assert(unhandled_.empty() && fixed_.empty() && 00331 active_.empty() && inactive_.empty() && 00332 "interval sets should be empty on initialization"); 00333 00334 handled_.reserve(li_->getNumIntervals()); 00335 00336 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { 00337 if (TargetRegisterInfo::isPhysicalRegister(i->second->reg)) { 00338 mri_->setPhysRegUsed(i->second->reg); 00339 fixed_.push_back(std::make_pair(i->second, i->second->begin())); 00340 } else 00341 unhandled_.push(i->second); 00342 } 00343 } 00344 00345 void RALinScan::linearScan() 00346 { 00347 // linear scan algorithm 00348 DOUT << "********** LINEAR SCAN **********\n"; 00349 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n'; 00350 00351 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end())); 00352 00353 while (!unhandled_.empty()) { 00354 // pick the interval with the earliest start point 00355 LiveInterval* cur = unhandled_.top(); 00356 unhandled_.pop(); 00357 ++NumIters; 00358 DOUT << "\n*** CURRENT ***: " << *cur << '\n'; 00359 00360 if (!cur->empty()) { 00361 processActiveIntervals(cur->beginNumber()); 00362 processInactiveIntervals(cur->beginNumber()); 00363 00364 assert(TargetRegisterInfo::isVirtualRegister(cur->reg) && 00365 "Can only allocate virtual registers!"); 00366 } 00367 00368 // Allocating a virtual register. try to find a free 00369 // physical register or spill an interval (possibly this one) in order to 00370 // assign it one. 00371 assignRegOrStackSlotAtInterval(cur); 00372 00373 DEBUG(printIntervals("active", active_.begin(), active_.end())); 00374 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end())); 00375 } 00376 00377 // expire any remaining active intervals 00378 while (!active_.empty()) { 00379 IntervalPtr &IP = active_.back(); 00380 unsigned reg = IP.first->reg; 00381 DOUT << "\tinterval " << *IP.first << " expired\n"; 00382 assert(TargetRegisterInfo::isVirtualRegister(reg) && 00383 "Can only allocate virtual registers!"); 00384 reg = vrm_->getPhys(reg); 00385 prt_->delRegUse(reg); 00386 active_.pop_back(); 00387 } 00388 00389 // expire any remaining inactive intervals 00390 DEBUG(for (IntervalPtrs::reverse_iterator 00391 i = inactive_.rbegin(); i != inactive_.rend(); ++i) 00392 DOUT << "\tinterval " << *i->first << " expired\n"); 00393 inactive_.clear(); 00394 00395 // Add live-ins to every BB except for entry. Also perform trivial coalescing. 00396 MachineFunction::iterator EntryMBB = mf_->begin(); 00397 SmallVector<MachineBasicBlock*, 8> LiveInMBBs; 00398 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { 00399 LiveInterval &cur = *i->second; 00400 unsigned Reg = 0; 00401 bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg); 00402 if (isPhys) 00403 Reg = cur.reg; 00404 else if (vrm_->isAssignedReg(cur.reg)) 00405 Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg)); 00406 if (!Reg) 00407 continue; 00408 // Ignore splited live intervals. 00409 if (!isPhys && vrm_->getPreSplitReg(cur.reg)) 00410 continue; 00411 for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end(); 00412 I != E; ++I) { 00413 const LiveRange &LR = *I; 00414 if (li_->findLiveInMBBs(LR.start, LR.end, LiveInMBBs)) { 00415 for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i) 00416 if (LiveInMBBs[i] != EntryMBB) 00417 LiveInMBBs[i]->addLiveIn(Reg); 00418 LiveInMBBs.clear(); 00419 } 00420 } 00421 } 00422 00423 DOUT << *vrm_; 00424 } 00425 00426 /// processActiveIntervals - expire old intervals and move non-overlapping ones 00427 /// to the inactive list. 00428 void RALinScan::processActiveIntervals(unsigned CurPoint) 00429 { 00430 DOUT << "\tprocessing active intervals:\n"; 00431 00432 for (unsigned i = 0, e = active_.size(); i != e; ++i) { 00433 LiveInterval *Interval = active_[i].first; 00434 LiveInterval::iterator IntervalPos = active_[i].second; 00435 unsigned reg = Interval->reg; 00436 00437 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); 00438 00439 if (IntervalPos == Interval->end()) { // Remove expired intervals. 00440 DOUT << "\t\tinterval " << *Interval << " expired\n"; 00441 assert(TargetRegisterInfo::isVirtualRegister(reg) && 00442 "Can only allocate virtual registers!"); 00443 reg = vrm_->getPhys(reg); 00444 prt_->delRegUse(reg); 00445 00446 // Pop off the end of the list. 00447 active_[i] = active_.back(); 00448 active_.pop_back(); 00449 --i; --e; 00450 00451 } else if (IntervalPos->start > CurPoint) { 00452 // Move inactive intervals to inactive list. 00453 DOUT << "\t\tinterval " << *Interval << " inactive\n"; 00454 assert(TargetRegisterInfo::isVirtualRegister(reg) && 00455 "Can only allocate virtual registers!"); 00456 reg = vrm_->getPhys(reg); 00457 prt_->delRegUse(reg); 00458 // add to inactive. 00459 inactive_.push_back(std::make_pair(Interval, IntervalPos)); 00460 00461 // Pop off the end of the list. 00462 active_[i] = active_.back(); 00463 active_.pop_back(); 00464 --i; --e; 00465 } else { 00466 // Otherwise, just update the iterator position. 00467 active_[i].second = IntervalPos; 00468 } 00469 } 00470 } 00471 00472 /// processInactiveIntervals - expire old intervals and move overlapping 00473 /// ones to the active list. 00474 void RALinScan::processInactiveIntervals(unsigned CurPoint) 00475 { 00476 DOUT << "\tprocessing inactive intervals:\n"; 00477 00478 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) { 00479 LiveInterval *Interval = inactive_[i].first; 00480 LiveInterval::iterator IntervalPos = inactive_[i].second; 00481 unsigned reg = Interval->reg; 00482 00483 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); 00484 00485 if (IntervalPos == Interval->end()) { // remove expired intervals. 00486 DOUT << "\t\tinterval " << *Interval << " expired\n"; 00487 00488 // Pop off the end of the list. 00489 inactive_[i] = inactive_.back(); 00490 inactive_.pop_back(); 00491 --i; --e; 00492 } else if (IntervalPos->start <= CurPoint) { 00493 // move re-activated intervals in active list 00494 DOUT << "\t\tinterval " << *Interval << " active\n"; 00495 assert(TargetRegisterInfo::isVirtualRegister(reg) && 00496 "Can only allocate virtual registers!"); 00497 reg = vrm_->getPhys(reg); 00498 prt_->addRegUse(reg); 00499 // add to active 00500 active_.push_back(std::make_pair(Interval, IntervalPos)); 00501 00502 // Pop off the end of the list. 00503 inactive_[i] = inactive_.back(); 00504 inactive_.pop_back(); 00505 --i; --e; 00506 } else { 00507 // Otherwise, just update the iterator position. 00508 inactive_[i].second = IntervalPos; 00509 } 00510 } 00511 } 00512 00513 /// updateSpillWeights - updates the spill weights of the specifed physical 00514 /// register and its weight. 00515 static void updateSpillWeights(std::vector<float> &Weights, 00516 unsigned reg, float weight, 00517 const TargetRegisterInfo *TRI) { 00518 Weights[reg] += weight; 00519 for (const unsigned* as = TRI->getAliasSet(reg); *as; ++as) 00520 Weights[*as] += weight; 00521 } 00522 00523 static 00524 RALinScan::IntervalPtrs::iterator 00525 FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) { 00526 for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); 00527 I != E; ++I) 00528 if (I->first == LI) return I; 00529 return IP.end(); 00530 } 00531 00532 static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){ 00533 for (unsigned i = 0, e = V.size(); i != e; ++i) { 00534 RALinScan::IntervalPtr &IP = V[i]; 00535 LiveInterval::iterator I = std::upper_bound(IP.first->begin(), 00536 IP.second, Point); 00537 if (I != IP.first->begin()) --I; 00538 IP.second = I; 00539 } 00540 } 00541 00542 /// addStackInterval - Create a LiveInterval for stack if the specified live 00543 /// interval has been spilled. 00544 static void addStackInterval(LiveInterval *cur, LiveStacks *ls_, 00545 LiveIntervals *li_, float &Weight, 00546 VirtRegMap &vrm_) { 00547 int SS = vrm_.getStackSlot(cur->reg); 00548 if (SS == VirtRegMap::NO_STACK_SLOT) 00549 return; 00550 LiveInterval &SI = ls_->getOrCreateInterval(SS); 00551 SI.weight += Weight; 00552 00553 VNInfo *VNI; 00554 if (SI.hasAtLeastOneValue()) 00555 VNI = SI.getValNumInfo(0); 00556 else 00557 VNI = SI.getNextValue(~0U, 0, ls_->getVNInfoAllocator()); 00558 00559 LiveInterval &RI = li_->getInterval(cur->reg); 00560 // FIXME: This may be overly conservative. 00561 SI.MergeRangesInAsValue(RI, VNI); 00562 } 00563 00564 /// getConflictWeight - Return the number of conflicts between cur 00565 /// live interval and defs and uses of Reg weighted by loop depthes. 00566 static float getConflictWeight(LiveInterval *cur, unsigned Reg, 00567 LiveIntervals *li_, 00568 MachineRegisterInfo *mri_, 00569 const MachineLoopInfo *loopInfo) { 00570 float Conflicts = 0; 00571 for (MachineRegisterInfo::reg_iterator I = mri_->reg_begin(Reg), 00572 E = mri_->reg_end(); I != E; ++I) { 00573 MachineInstr *MI = &*I; 00574 if (cur->liveAt(li_->getInstructionIndex(MI))) { 00575 unsigned loopDepth = loopInfo->getLoopDepth(MI->getParent()); 00576 Conflicts += powf(10.0f, (float)loopDepth); 00577 } 00578 } 00579 return Conflicts; 00580 } 00581 00582 /// findIntervalsToSpill - Determine the intervals to spill for the 00583 /// specified interval. It's passed the physical registers whose spill 00584 /// weight is the lowest among all the registers whose live intervals 00585 /// conflict with the interval. 00586 void RALinScan::findIntervalsToSpill(LiveInterval *cur, 00587 std::vector<std::pair<unsigned,float> > &Candidates, 00588 unsigned NumCands, 00589 SmallVector<LiveInterval*, 8> &SpillIntervals) { 00590 // We have figured out the *best* register to spill. But there are other 00591 // registers that are pretty good as well (spill weight within 3%). Spill 00592 // the one that has fewest defs and uses that conflict with cur. 00593 float Conflicts[3] = { 0.0f, 0.0f, 0.0f }; 00594 SmallVector<LiveInterval*, 8> SLIs[3]; 00595 00596 DOUT << "\tConsidering " << NumCands << " candidates: "; 00597 DEBUG(for (unsigned i = 0; i != NumCands; ++i) 00598 DOUT << tri_->getName(Candidates[i].first) << " "; 00599 DOUT << "\n";); 00600 00601 // Calculate the number of conflicts of each candidate. 00602 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { 00603 unsigned Reg = i->first->reg; 00604 unsigned PhysReg = vrm_->getPhys(Reg); 00605 if (!cur->overlapsFrom(*i->first, i->second)) 00606 continue; 00607 for (unsigned j = 0; j < NumCands; ++j) { 00608 unsigned Candidate = Candidates[j].first; 00609 if (tri_->regsOverlap(PhysReg, Candidate)) { 00610 if (NumCands > 1) 00611 Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo); 00612 SLIs[j].push_back(i->first); 00613 } 00614 } 00615 } 00616 00617 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){ 00618 unsigned Reg = i->first->reg; 00619 unsigned PhysReg = vrm_->getPhys(Reg); 00620 if (!cur->overlapsFrom(*i->first, i->second-1)) 00621 continue; 00622 for (unsigned j = 0; j < NumCands; ++j) { 00623 unsigned Candidate = Candidates[j].first; 00624 if (tri_->regsOverlap(PhysReg, Candidate)) { 00625 if (NumCands > 1) 00626 Conflicts[j] += getConflictWeight(cur, Reg, li_, mri_, loopInfo); 00627 SLIs[j].push_back(i->first); 00628 } 00629 } 00630 } 00631 00632 // Which is the best candidate? 00633 unsigned BestCandidate = 0; 00634 float MinConflicts = Conflicts[0]; 00635 for (unsigned i = 1; i != NumCands; ++i) { 00636 if (Conflicts[i] < MinConflicts) { 00637 BestCandidate = i; 00638 MinConflicts = Conflicts[i]; 00639 } 00640 } 00641 00642 std::copy(SLIs[BestCandidate].begin(), SLIs[BestCandidate].end(), 00643 std::back_inserter(SpillIntervals)); 00644 } 00645 00646 namespace { 00647 struct WeightCompare { 00648 typedef std::pair<unsigned, float> RegWeightPair; 00649 bool operator()(const RegWeightPair &LHS, const RegWeightPair &RHS) const { 00650 return LHS.second < RHS.second; 00651 } 00652 }; 00653 } 00654 00655 static bool weightsAreClose(float w1, float w2) { 00656 if (!NewHeuristic) 00657 return false; 00658 00659 float diff = w1 - w2; 00660 if (diff <= 0.02f) // Within 0.02f 00661 return true; 00662 return (diff / w2) <= 0.05f; // Within 5%. 00663 } 00664 00665 /// assignRegOrStackSlotAtInterval - assign a register if one is available, or 00666 /// spill. 00667 void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) 00668 { 00669 DOUT << "\tallocating current interval: "; 00670 00671 // This is an implicitly defined live interval, just assign any register. 00672 const TargetRegisterClass *RC = mri_->getRegClass(cur->reg); 00673 if (cur->empty()) { 00674 unsigned physReg = cur->preference; 00675 if (!physReg) 00676 physReg = *RC->allocation_order_begin(*mf_); 00677 DOUT << tri_->getName(physReg) << '\n'; 00678 // Note the register is not really in use. 00679 vrm_->assignVirt2Phys(cur->reg, physReg); 00680 return; 00681 } 00682 00683 PhysRegTracker backupPrt = *prt_; 00684 00685 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd; 00686 unsigned StartPosition = cur->beginNumber(); 00687 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); 00688 00689 // If this live interval is defined by a move instruction and its source is 00690 // assigned a physical register that is compatible with the target register 00691 // class, then we should try to assign it the same register. 00692 // This can happen when the move is from a larger register class to a smaller 00693 // one, e.g. X86::mov32to32_. These move instructions are not coalescable. 00694 if (!cur->preference && cur->containsOneValue()) { 00695 VNInfo *vni = cur->getValNumInfo(0); 00696 if (vni->def && vni->def != ~1U && vni->def != ~0U) { 00697 MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); 00698 unsigned SrcReg, DstReg; 00699 if (CopyMI && tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) { 00700 unsigned Reg = 0; 00701 if (TargetRegisterInfo::isPhysicalRegister(SrcReg)) 00702 Reg = SrcReg; 00703 else if (vrm_->isAssignedReg(SrcReg)) 00704 Reg = vrm_->getPhys(SrcReg); 00705 if (Reg && allocatableRegs_[Reg] && RC->contains(Reg)) 00706 cur->preference = Reg; 00707 } 00708 } 00709 } 00710 00711 // for every interval in inactive we overlap with, mark the 00712 // register as not free and update spill weights. 00713 for (IntervalPtrs::const_iterator i = inactive_.begin(), 00714 e = inactive_.end(); i != e; ++i) { 00715 unsigned Reg = i->first->reg; 00716 assert(TargetRegisterInfo::isVirtualRegister(Reg) && 00717 "Can only allocate virtual registers!"); 00718 const TargetRegisterClass *RegRC = mri_->getRegClass(Reg); 00719 // If this is not in a related reg class to the register we're allocating, 00720 // don't check it. 00721 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && 00722 cur->overlapsFrom(*i->first, i->second-1)) { 00723 Reg = vrm_->getPhys(Reg); 00724 prt_->addRegUse(Reg); 00725 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight)); 00726 } 00727 } 00728 00729 // Speculatively check to see if we can get a register right now. If not, 00730 // we know we won't be able to by adding more constraints. If so, we can 00731 // check to see if it is valid. Doing an exhaustive search of the fixed_ list 00732 // is very bad (it contains all callee clobbered registers for any functions 00733 // with a call), so we want to avoid doing that if possible. 00734 unsigned physReg = getFreePhysReg(cur); 00735 unsigned BestPhysReg = physReg; 00736 if (physReg) { 00737 // We got a register. However, if it's in the fixed_ list, we might 00738 // conflict with it. Check to see if we conflict with it or any of its 00739 // aliases. 00740 SmallSet<unsigned, 8> RegAliases; 00741 for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS) 00742 RegAliases.insert(*AS); 00743 00744 bool ConflictsWithFixed = false; 00745 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { 00746 IntervalPtr &IP = fixed_[i]; 00747 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) { 00748 // Okay, this reg is on the fixed list. Check to see if we actually 00749 // conflict. 00750 LiveInterval *I = IP.first; 00751 if (I->endNumber() > StartPosition) { 00752 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); 00753 IP.second = II; 00754 if (II != I->begin() && II->start > StartPosition) 00755 --II; 00756 if (cur->overlapsFrom(*I, II)) { 00757 ConflictsWithFixed = true; 00758 break; 00759 } 00760 } 00761 } 00762 } 00763 00764 // Okay, the register picked by our speculative getFreePhysReg call turned 00765 // out to be in use. Actually add all of the conflicting fixed registers to 00766 // prt so we can do an accurate query. 00767 if (ConflictsWithFixed) { 00768 // For every interval in fixed we overlap with, mark the register as not 00769 // free and update spill weights. 00770 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { 00771 IntervalPtr &IP = fixed_[i]; 00772 LiveInterval *I = IP.first; 00773 00774 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg]; 00775 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && 00776 I->endNumber() > StartPosition) { 00777 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); 00778 IP.second = II; 00779 if (II != I->begin() && II->start > StartPosition) 00780 --II; 00781 if (cur->overlapsFrom(*I, II)) { 00782 unsigned reg = I->reg; 00783 prt_->addRegUse(reg); 00784 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight)); 00785 } 00786 } 00787 } 00788 00789 // Using the newly updated prt_ object, which includes conflicts in the 00790 // future, see if there are any registers available. 00791 physReg = getFreePhysReg(cur); 00792 } 00793 } 00794 00795 // Restore the physical register tracker, removing information about the 00796 // future. 00797 *prt_ = backupPrt; 00798 00799 // if we find a free register, we are done: assign this virtual to 00800 // the free physical register and add this interval to the active 00801 // list. 00802 if (physReg) { 00803 DOUT << tri_->getName(physReg) << '\n'; 00804 vrm_->assignVirt2Phys(cur->reg, physReg); 00805 prt_->addRegUse(physReg); 00806 active_.push_back(std::make_pair(cur, cur->begin())); 00807 handled_.push_back(cur); 00808 return; 00809 } 00810 DOUT << "no free registers\n"; 00811 00812 // Compile the spill weights into an array that is better for scanning. 00813 std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0f); 00814 for (std::vector<std::pair<unsigned, float> >::iterator 00815 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I) 00816 updateSpillWeights(SpillWeights, I->first, I->second, tri_); 00817 00818 // for each interval in active, update spill weights. 00819 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end(); 00820 i != e; ++i) { 00821 unsigned reg = i->first->reg; 00822 assert(TargetRegisterInfo::isVirtualRegister(reg) && 00823 "Can only allocate virtual registers!"); 00824 reg = vrm_->getPhys(reg); 00825 updateSpillWeights(SpillWeights, reg, i->first->weight, tri_); 00826 } 00827 00828 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n"; 00829 00830 // Find a register to spill. 00831 float minWeight = HUGE_VALF; 00832 unsigned minReg = 0; /*cur->preference*/; // Try the preferred register first. 00833 00834 bool Found = false; 00835 std::vector<std::pair<unsigned,float> > RegsWeights; 00836 if (!minReg || SpillWeights[minReg] == HUGE_VALF) 00837 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), 00838 e = RC->allocation_order_end(*mf_); i != e; ++i) { 00839 unsigned reg = *i; 00840 float regWeight = SpillWeights[reg]; 00841 if (minWeight > regWeight) 00842 Found = true; 00843 RegsWeights.push_back(std::make_pair(reg, regWeight)); 00844 } 00845 00846 // If we didn't find a register that is spillable, try aliases? 00847 if (!Found) { 00848 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), 00849 e = RC->allocation_order_end(*mf_); i != e; ++i) { 00850 unsigned reg = *i; 00851 // No need to worry about if the alias register size < regsize of RC. 00852 // We are going to spill all registers that alias it anyway. 00853 for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) 00854 RegsWeights.push_back(std::make_pair(*as, SpillWeights[*as])); 00855 } 00856 } 00857 00858 // Sort all potential spill candidates by weight. 00859 std::sort(RegsWeights.begin(), RegsWeights.end(), WeightCompare()); 00860 minReg = RegsWeights[0].first; 00861 minWeight = RegsWeights[0].second; 00862 if (minWeight == HUGE_VALF) { 00863 // All registers must have inf weight. Just grab one! 00864 minReg = BestPhysReg ? BestPhysReg : *RC->allocation_order_begin(*mf_); 00865 if (cur->weight == HUGE_VALF || 00866 li_->getApproximateInstructionCount(*cur) == 0) { 00867 // Spill a physical register around defs and uses. 00868 li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_); 00869 assignRegOrStackSlotAtInterval(cur); 00870 return; 00871 } 00872 } 00873 00874 // Find up to 3 registers to consider as spill candidates. 00875 unsigned LastCandidate = RegsWeights.size() >= 3 ? 3 : 1; 00876 while (LastCandidate > 1) { 00877 if (weightsAreClose(RegsWeights[LastCandidate-1].second, minWeight)) 00878 break; 00879 --LastCandidate; 00880 } 00881 00882 DOUT << "\t\tregister(s) with min weight(s): "; 00883 DEBUG(for (unsigned i = 0; i != LastCandidate; ++i) 00884 DOUT << tri_->getName(RegsWeights[i].first) 00885 << " (" << RegsWeights[i].second << ")\n"); 00886 00887 // if the current has the minimum weight, we need to spill it and 00888 // add any added intervals back to unhandled, and restart 00889 // linearscan. 00890 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) { 00891 DOUT << "\t\t\tspilling(c): " << *cur << '\n'; 00892 float SSWeight; 00893 SmallVector<LiveInterval*, 8> spillIs; 00894 std::vector<LiveInterval*> added = 00895 li_->addIntervalsForSpills(*cur, spillIs, loopInfo