Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 1 | //===-- RegAllocLinearScan.cpp - Linear Scan register allocator -----------===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 081ce94 | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a linear scan register allocator. |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
| 14 | #define DEBUG_TYPE "regalloc" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 15 | #include "llvm/CodeGen/LiveIntervalAnalysis.h" |
| 16 | #include "PhysRegTracker.h" |
| 17 | #include "VirtRegMap.h" |
| 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/CodeGen/MachineFunctionPass.h" |
| 20 | #include "llvm/CodeGen/MachineInstr.h" |
Evan Cheng | 26d17df | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 21 | #include "llvm/CodeGen/MachineLoopInfo.h" |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 22 | #include "llvm/CodeGen/MachineRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 23 | #include "llvm/CodeGen/Passes.h" |
| 24 | #include "llvm/CodeGen/RegAllocRegistry.h" |
David Greene | 1d80f1b | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 25 | #include "llvm/CodeGen/RegisterCoalescer.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 26 | #include "llvm/Target/MRegisterInfo.h" |
| 27 | #include "llvm/Target/TargetMachine.h" |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 28 | #include "llvm/Target/TargetInstrInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/EquivalenceClasses.h" |
| 30 | #include "llvm/ADT/Statistic.h" |
| 31 | #include "llvm/ADT/STLExtras.h" |
| 32 | #include "llvm/Support/Debug.h" |
| 33 | #include "llvm/Support/Compiler.h" |
| 34 | #include <algorithm> |
| 35 | #include <set> |
| 36 | #include <queue> |
| 37 | #include <memory> |
| 38 | #include <cmath> |
| 39 | using namespace llvm; |
| 40 | |
| 41 | STATISTIC(NumIters , "Number of iterations performed"); |
| 42 | STATISTIC(NumBacktracks, "Number of times we had to backtrack"); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 43 | STATISTIC(NumCoalesce, "Number of copies coalesced"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 44 | |
| 45 | static RegisterRegAlloc |
| 46 | linearscanRegAlloc("linearscan", " linear scan register allocator", |
| 47 | createLinearScanRegisterAllocator); |
| 48 | |
| 49 | namespace { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 50 | struct VISIBILITY_HIDDEN RALinScan : public MachineFunctionPass { |
| 51 | static char ID; |
| 52 | RALinScan() : MachineFunctionPass((intptr_t)&ID) {} |
| 53 | |
| 54 | typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr; |
| 55 | typedef std::vector<IntervalPtr> IntervalPtrs; |
| 56 | private: |
| 57 | /// RelatedRegClasses - This structure is built the first time a function is |
| 58 | /// compiled, and keeps track of which register classes have registers that |
| 59 | /// belong to multiple classes or have aliases that are in other classes. |
| 60 | EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses; |
| 61 | std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg; |
| 62 | |
| 63 | MachineFunction* mf_; |
| 64 | const TargetMachine* tm_; |
| 65 | const MRegisterInfo* mri_; |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 66 | const TargetInstrInfo* tii_; |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 67 | MachineRegisterInfo *reginfo_; |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 68 | BitVector allocatableRegs_; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 69 | LiveIntervals* li_; |
Evan Cheng | 26d17df | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 70 | const MachineLoopInfo *loopInfo; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 71 | |
| 72 | /// handled_ - Intervals are added to the handled_ set in the order of their |
| 73 | /// start value. This is uses for backtracking. |
| 74 | std::vector<LiveInterval*> handled_; |
| 75 | |
| 76 | /// fixed_ - Intervals that correspond to machine registers. |
| 77 | /// |
| 78 | IntervalPtrs fixed_; |
| 79 | |
| 80 | /// active_ - Intervals that are currently being processed, and which have a |
| 81 | /// live range active for the current point. |
| 82 | IntervalPtrs active_; |
| 83 | |
| 84 | /// inactive_ - Intervals that are currently being processed, but which have |
| 85 | /// a hold at the current point. |
| 86 | IntervalPtrs inactive_; |
| 87 | |
| 88 | typedef std::priority_queue<LiveInterval*, |
| 89 | std::vector<LiveInterval*>, |
| 90 | greater_ptr<LiveInterval> > IntervalHeap; |
| 91 | IntervalHeap unhandled_; |
| 92 | std::auto_ptr<PhysRegTracker> prt_; |
| 93 | std::auto_ptr<VirtRegMap> vrm_; |
| 94 | std::auto_ptr<Spiller> spiller_; |
| 95 | |
| 96 | public: |
| 97 | virtual const char* getPassName() const { |
| 98 | return "Linear Scan Register Allocator"; |
| 99 | } |
| 100 | |
| 101 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 102 | AU.addRequired<LiveIntervals>(); |
David Greene | 1d80f1b | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 103 | // Make sure PassManager knows which analyses to make available |
| 104 | // to coalescing and which analyses coalescing invalidates. |
| 105 | AU.addRequiredTransitive<RegisterCoalescer>(); |
Evan Cheng | 26d17df | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 106 | AU.addRequired<MachineLoopInfo>(); |
Bill Wendling | 6226436 | 2008-01-04 20:54:55 +0000 | [diff] [blame^] | 107 | AU.addPreserved<MachineLoopInfo>(); |
| 108 | AU.addPreservedID(MachineDominatorsID); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 109 | MachineFunctionPass::getAnalysisUsage(AU); |
| 110 | } |
| 111 | |
| 112 | /// runOnMachineFunction - register allocate the whole function |
| 113 | bool runOnMachineFunction(MachineFunction&); |
| 114 | |
| 115 | private: |
| 116 | /// linearScan - the linear scan algorithm |
| 117 | void linearScan(); |
| 118 | |
| 119 | /// initIntervalSets - initialize the interval sets. |
| 120 | /// |
| 121 | void initIntervalSets(); |
| 122 | |
| 123 | /// processActiveIntervals - expire old intervals and move non-overlapping |
| 124 | /// ones to the inactive list. |
| 125 | void processActiveIntervals(unsigned CurPoint); |
| 126 | |
| 127 | /// processInactiveIntervals - expire old intervals and move overlapping |
| 128 | /// ones to the active list. |
| 129 | void processInactiveIntervals(unsigned CurPoint); |
| 130 | |
| 131 | /// assignRegOrStackSlotAtInterval - assign a register if one |
| 132 | /// is available, or spill. |
| 133 | void assignRegOrStackSlotAtInterval(LiveInterval* cur); |
| 134 | |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 135 | /// attemptTrivialCoalescing - If a simple interval is defined by a copy, |
| 136 | /// try allocate the definition the same register as the source register |
| 137 | /// if the register is not defined during live time of the interval. This |
| 138 | /// eliminate a copy. This is used to coalesce copies which were not |
| 139 | /// coalesced away before allocation either due to dest and src being in |
| 140 | /// different register classes or because the coalescer was overly |
| 141 | /// conservative. |
| 142 | unsigned attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg); |
| 143 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 144 | /// |
| 145 | /// register handling helpers |
| 146 | /// |
| 147 | |
| 148 | /// getFreePhysReg - return a free physical register for this virtual |
| 149 | /// register interval if we have one, otherwise return 0. |
| 150 | unsigned getFreePhysReg(LiveInterval* cur); |
| 151 | |
| 152 | /// assignVirt2StackSlot - assigns this virtual register to a |
| 153 | /// stack slot. returns the stack slot |
| 154 | int assignVirt2StackSlot(unsigned virtReg); |
| 155 | |
| 156 | void ComputeRelatedRegClasses(); |
| 157 | |
| 158 | template <typename ItTy> |
| 159 | void printIntervals(const char* const str, ItTy i, ItTy e) const { |
| 160 | if (str) DOUT << str << " intervals:\n"; |
| 161 | for (; i != e; ++i) { |
| 162 | DOUT << "\t" << *i->first << " -> "; |
| 163 | unsigned reg = i->first->reg; |
| 164 | if (MRegisterInfo::isVirtualRegister(reg)) { |
| 165 | reg = vrm_->getPhys(reg); |
| 166 | } |
| 167 | DOUT << mri_->getName(reg) << '\n'; |
| 168 | } |
| 169 | } |
| 170 | }; |
| 171 | char RALinScan::ID = 0; |
| 172 | } |
| 173 | |
| 174 | void RALinScan::ComputeRelatedRegClasses() { |
| 175 | const MRegisterInfo &MRI = *mri_; |
| 176 | |
| 177 | // First pass, add all reg classes to the union, and determine at least one |
| 178 | // reg class that each register is in. |
| 179 | bool HasAliases = false; |
| 180 | for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(), |
| 181 | E = MRI.regclass_end(); RCI != E; ++RCI) { |
| 182 | RelatedRegClasses.insert(*RCI); |
| 183 | for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end(); |
| 184 | I != E; ++I) { |
| 185 | HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0; |
| 186 | |
| 187 | const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I]; |
| 188 | if (PRC) { |
| 189 | // Already processed this register. Just make sure we know that |
| 190 | // multiple register classes share a register. |
| 191 | RelatedRegClasses.unionSets(PRC, *RCI); |
| 192 | } else { |
| 193 | PRC = *RCI; |
| 194 | } |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Second pass, now that we know conservatively what register classes each reg |
| 199 | // belongs to, add info about aliases. We don't need to do this for targets |
| 200 | // without register aliases. |
| 201 | if (HasAliases) |
| 202 | for (std::map<unsigned, const TargetRegisterClass*>::iterator |
| 203 | I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end(); |
| 204 | I != E; ++I) |
| 205 | for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS) |
| 206 | RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]); |
| 207 | } |
| 208 | |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 209 | /// attemptTrivialCoalescing - If a simple interval is defined by a copy, |
| 210 | /// try allocate the definition the same register as the source register |
| 211 | /// if the register is not defined during live time of the interval. This |
| 212 | /// eliminate a copy. This is used to coalesce copies which were not |
| 213 | /// coalesced away before allocation either due to dest and src being in |
| 214 | /// different register classes or because the coalescer was overly |
| 215 | /// conservative. |
| 216 | unsigned RALinScan::attemptTrivialCoalescing(LiveInterval &cur, unsigned Reg) { |
Evan Cheng | b6aa671 | 2007-11-04 08:32:21 +0000 | [diff] [blame] | 217 | if ((cur.preference && cur.preference == Reg) || !cur.containsOneValue()) |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 218 | return Reg; |
| 219 | |
| 220 | VNInfo *vni = cur.getValNumInfo(0); |
| 221 | if (!vni->def || vni->def == ~1U || vni->def == ~0U) |
| 222 | return Reg; |
| 223 | MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); |
| 224 | unsigned SrcReg, DstReg; |
| 225 | if (!CopyMI || !tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) |
| 226 | return Reg; |
| 227 | if (MRegisterInfo::isVirtualRegister(SrcReg)) |
| 228 | if (!vrm_->isAssignedReg(SrcReg)) |
| 229 | return Reg; |
| 230 | else |
| 231 | SrcReg = vrm_->getPhys(SrcReg); |
| 232 | if (Reg == SrcReg) |
| 233 | return Reg; |
| 234 | |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 235 | const TargetRegisterClass *RC = reginfo_->getRegClass(cur.reg); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 236 | if (!RC->contains(SrcReg)) |
| 237 | return Reg; |
| 238 | |
| 239 | // Try to coalesce. |
| 240 | if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) { |
Bill Wendling | 28f22ab | 2007-11-15 02:06:30 +0000 | [diff] [blame] | 241 | DOUT << "Coalescing: " << cur << " -> " << mri_->getName(SrcReg) << '\n'; |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 242 | vrm_->clearVirt(cur.reg); |
| 243 | vrm_->assignVirt2Phys(cur.reg, SrcReg); |
| 244 | ++NumCoalesce; |
| 245 | return SrcReg; |
| 246 | } |
| 247 | |
| 248 | return Reg; |
| 249 | } |
| 250 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 251 | bool RALinScan::runOnMachineFunction(MachineFunction &fn) { |
| 252 | mf_ = &fn; |
| 253 | tm_ = &fn.getTarget(); |
| 254 | mri_ = tm_->getRegisterInfo(); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 255 | tii_ = tm_->getInstrInfo(); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 256 | reginfo_ = &mf_->getRegInfo(); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 257 | allocatableRegs_ = mri_->getAllocatableSet(fn); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 258 | li_ = &getAnalysis<LiveIntervals>(); |
Evan Cheng | 26d17df | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 259 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | |
David Greene | 1d80f1b | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 261 | // We don't run the coalescer here because we have no reason to |
| 262 | // interact with it. If the coalescer requires interaction, it |
| 263 | // won't do anything. If it doesn't require interaction, we assume |
| 264 | // it was run as a separate pass. |
| 265 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 266 | // If this is the first function compiled, compute the related reg classes. |
| 267 | if (RelatedRegClasses.empty()) |
| 268 | ComputeRelatedRegClasses(); |
| 269 | |
| 270 | if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_)); |
| 271 | vrm_.reset(new VirtRegMap(*mf_)); |
| 272 | if (!spiller_.get()) spiller_.reset(createSpiller()); |
| 273 | |
| 274 | initIntervalSets(); |
| 275 | |
| 276 | linearScan(); |
| 277 | |
| 278 | // Rewrite spill code and update the PhysRegsUsed set. |
| 279 | spiller_->runOnMachineFunction(*mf_, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 280 | vrm_.reset(); // Free the VirtRegMap |
| 281 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | while (!unhandled_.empty()) unhandled_.pop(); |
| 283 | fixed_.clear(); |
| 284 | active_.clear(); |
| 285 | inactive_.clear(); |
| 286 | handled_.clear(); |
| 287 | |
| 288 | return true; |
| 289 | } |
| 290 | |
| 291 | /// initIntervalSets - initialize the interval sets. |
| 292 | /// |
| 293 | void RALinScan::initIntervalSets() |
| 294 | { |
| 295 | assert(unhandled_.empty() && fixed_.empty() && |
| 296 | active_.empty() && inactive_.empty() && |
| 297 | "interval sets should be empty on initialization"); |
| 298 | |
| 299 | for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { |
| 300 | if (MRegisterInfo::isPhysicalRegister(i->second.reg)) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 301 | reginfo_->setPhysRegUsed(i->second.reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 302 | fixed_.push_back(std::make_pair(&i->second, i->second.begin())); |
| 303 | } else |
| 304 | unhandled_.push(&i->second); |
| 305 | } |
| 306 | } |
| 307 | |
| 308 | void RALinScan::linearScan() |
| 309 | { |
| 310 | // linear scan algorithm |
| 311 | DOUT << "********** LINEAR SCAN **********\n"; |
| 312 | DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n'; |
| 313 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 314 | DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 315 | |
| 316 | while (!unhandled_.empty()) { |
| 317 | // pick the interval with the earliest start point |
| 318 | LiveInterval* cur = unhandled_.top(); |
| 319 | unhandled_.pop(); |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 320 | ++NumIters; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 321 | DOUT << "\n*** CURRENT ***: " << *cur << '\n'; |
| 322 | |
| 323 | processActiveIntervals(cur->beginNumber()); |
| 324 | processInactiveIntervals(cur->beginNumber()); |
| 325 | |
| 326 | assert(MRegisterInfo::isVirtualRegister(cur->reg) && |
| 327 | "Can only allocate virtual registers!"); |
| 328 | |
| 329 | // Allocating a virtual register. try to find a free |
| 330 | // physical register or spill an interval (possibly this one) in order to |
| 331 | // assign it one. |
| 332 | assignRegOrStackSlotAtInterval(cur); |
| 333 | |
| 334 | DEBUG(printIntervals("active", active_.begin(), active_.end())); |
| 335 | DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end())); |
| 336 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 337 | |
| 338 | // expire any remaining active intervals |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 339 | while (!active_.empty()) { |
| 340 | IntervalPtr &IP = active_.back(); |
| 341 | unsigned reg = IP.first->reg; |
| 342 | DOUT << "\tinterval " << *IP.first << " expired\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 343 | assert(MRegisterInfo::isVirtualRegister(reg) && |
| 344 | "Can only allocate virtual registers!"); |
| 345 | reg = vrm_->getPhys(reg); |
| 346 | prt_->delRegUse(reg); |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 347 | active_.pop_back(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 348 | } |
| 349 | |
| 350 | // expire any remaining inactive intervals |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 351 | DEBUG(for (IntervalPtrs::reverse_iterator |
Bill Wendling | 1817ab8 | 2007-11-15 00:40:48 +0000 | [diff] [blame] | 352 | i = inactive_.rbegin(); i != inactive_.rend(); ++i) |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 353 | DOUT << "\tinterval " << *i->first << " expired\n"); |
| 354 | inactive_.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 355 | |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 356 | // Add live-ins to every BB except for entry. Also perform trivial coalescing. |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 357 | MachineFunction::iterator EntryMBB = mf_->begin(); |
Evan Cheng | 12d6fcb | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 358 | SmallVector<MachineBasicBlock*, 8> LiveInMBBs; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 359 | for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 360 | LiveInterval &cur = i->second; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 361 | unsigned Reg = 0; |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 362 | bool isPhys = MRegisterInfo::isPhysicalRegister(cur.reg); |
| 363 | if (isPhys) |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 364 | Reg = i->second.reg; |
| 365 | else if (vrm_->isAssignedReg(cur.reg)) |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 366 | Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg)); |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 367 | if (!Reg) |
| 368 | continue; |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 369 | // Ignore splited live intervals. |
| 370 | if (!isPhys && vrm_->getPreSplitReg(cur.reg)) |
| 371 | continue; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 372 | for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end(); |
| 373 | I != E; ++I) { |
| 374 | const LiveRange &LR = *I; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 375 | if (li_->findLiveInMBBs(LR, LiveInMBBs)) { |
| 376 | for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i) |
| 377 | if (LiveInMBBs[i] != EntryMBB) |
| 378 | LiveInMBBs[i]->addLiveIn(Reg); |
Evan Cheng | 12d6fcb | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 379 | LiveInMBBs.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 380 | } |
| 381 | } |
| 382 | } |
| 383 | |
| 384 | DOUT << *vrm_; |
| 385 | } |
| 386 | |
| 387 | /// processActiveIntervals - expire old intervals and move non-overlapping ones |
| 388 | /// to the inactive list. |
| 389 | void RALinScan::processActiveIntervals(unsigned CurPoint) |
| 390 | { |
| 391 | DOUT << "\tprocessing active intervals:\n"; |
| 392 | |
| 393 | for (unsigned i = 0, e = active_.size(); i != e; ++i) { |
| 394 | LiveInterval *Interval = active_[i].first; |
| 395 | LiveInterval::iterator IntervalPos = active_[i].second; |
| 396 | unsigned reg = Interval->reg; |
| 397 | |
| 398 | IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); |
| 399 | |
| 400 | if (IntervalPos == Interval->end()) { // Remove expired intervals. |
| 401 | DOUT << "\t\tinterval " << *Interval << " expired\n"; |
| 402 | assert(MRegisterInfo::isVirtualRegister(reg) && |
| 403 | "Can only allocate virtual registers!"); |
| 404 | reg = vrm_->getPhys(reg); |
| 405 | prt_->delRegUse(reg); |
| 406 | |
| 407 | // Pop off the end of the list. |
| 408 | active_[i] = active_.back(); |
| 409 | active_.pop_back(); |
| 410 | --i; --e; |
| 411 | |
| 412 | } else if (IntervalPos->start > CurPoint) { |
| 413 | // Move inactive intervals to inactive list. |
| 414 | DOUT << "\t\tinterval " << *Interval << " inactive\n"; |
| 415 | assert(MRegisterInfo::isVirtualRegister(reg) && |
| 416 | "Can only allocate virtual registers!"); |
| 417 | reg = vrm_->getPhys(reg); |
| 418 | prt_->delRegUse(reg); |
| 419 | // add to inactive. |
| 420 | inactive_.push_back(std::make_pair(Interval, IntervalPos)); |
| 421 | |
| 422 | // Pop off the end of the list. |
| 423 | active_[i] = active_.back(); |
| 424 | active_.pop_back(); |
| 425 | --i; --e; |
| 426 | } else { |
| 427 | // Otherwise, just update the iterator position. |
| 428 | active_[i].second = IntervalPos; |
| 429 | } |
| 430 | } |
| 431 | } |
| 432 | |
| 433 | /// processInactiveIntervals - expire old intervals and move overlapping |
| 434 | /// ones to the active list. |
| 435 | void RALinScan::processInactiveIntervals(unsigned CurPoint) |
| 436 | { |
| 437 | DOUT << "\tprocessing inactive intervals:\n"; |
| 438 | |
| 439 | for (unsigned i = 0, e = inactive_.size(); i != e; ++i) { |
| 440 | LiveInterval *Interval = inactive_[i].first; |
| 441 | LiveInterval::iterator IntervalPos = inactive_[i].second; |
| 442 | unsigned reg = Interval->reg; |
| 443 | |
| 444 | IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); |
| 445 | |
| 446 | if (IntervalPos == Interval->end()) { // remove expired intervals. |
| 447 | DOUT << "\t\tinterval " << *Interval << " expired\n"; |
| 448 | |
| 449 | // Pop off the end of the list. |
| 450 | inactive_[i] = inactive_.back(); |
| 451 | inactive_.pop_back(); |
| 452 | --i; --e; |
| 453 | } else if (IntervalPos->start <= CurPoint) { |
| 454 | // move re-activated intervals in active list |
| 455 | DOUT << "\t\tinterval " << *Interval << " active\n"; |
| 456 | assert(MRegisterInfo::isVirtualRegister(reg) && |
| 457 | "Can only allocate virtual registers!"); |
| 458 | reg = vrm_->getPhys(reg); |
| 459 | prt_->addRegUse(reg); |
| 460 | // add to active |
| 461 | active_.push_back(std::make_pair(Interval, IntervalPos)); |
| 462 | |
| 463 | // Pop off the end of the list. |
| 464 | inactive_[i] = inactive_.back(); |
| 465 | inactive_.pop_back(); |
| 466 | --i; --e; |
| 467 | } else { |
| 468 | // Otherwise, just update the iterator position. |
| 469 | inactive_[i].second = IntervalPos; |
| 470 | } |
| 471 | } |
| 472 | } |
| 473 | |
| 474 | /// updateSpillWeights - updates the spill weights of the specifed physical |
| 475 | /// register and its weight. |
| 476 | static void updateSpillWeights(std::vector<float> &Weights, |
| 477 | unsigned reg, float weight, |
| 478 | const MRegisterInfo *MRI) { |
| 479 | Weights[reg] += weight; |
| 480 | for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as) |
| 481 | Weights[*as] += weight; |
| 482 | } |
| 483 | |
| 484 | static |
| 485 | RALinScan::IntervalPtrs::iterator |
| 486 | FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) { |
| 487 | for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); |
| 488 | I != E; ++I) |
| 489 | if (I->first == LI) return I; |
| 490 | return IP.end(); |
| 491 | } |
| 492 | |
| 493 | static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){ |
| 494 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 495 | RALinScan::IntervalPtr &IP = V[i]; |
| 496 | LiveInterval::iterator I = std::upper_bound(IP.first->begin(), |
| 497 | IP.second, Point); |
| 498 | if (I != IP.first->begin()) --I; |
| 499 | IP.second = I; |
| 500 | } |
| 501 | } |
| 502 | |
| 503 | /// assignRegOrStackSlotAtInterval - assign a register if one is available, or |
| 504 | /// spill. |
| 505 | void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) |
| 506 | { |
| 507 | DOUT << "\tallocating current interval: "; |
| 508 | |
| 509 | PhysRegTracker backupPrt = *prt_; |
| 510 | |
| 511 | std::vector<std::pair<unsigned, float> > SpillWeightsToAdd; |
| 512 | unsigned StartPosition = cur->beginNumber(); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 513 | const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 514 | const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 515 | |
| 516 | // If this live interval is defined by a move instruction and its source is |
| 517 | // assigned a physical register that is compatible with the target register |
| 518 | // class, then we should try to assign it the same register. |
| 519 | // This can happen when the move is from a larger register class to a smaller |
| 520 | // one, e.g. X86::mov32to32_. These move instructions are not coalescable. |
| 521 | if (!cur->preference && cur->containsOneValue()) { |
| 522 | VNInfo *vni = cur->getValNumInfo(0); |
| 523 | if (vni->def && vni->def != ~1U && vni->def != ~0U) { |
| 524 | MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); |
| 525 | unsigned SrcReg, DstReg; |
| 526 | if (tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) { |
| 527 | unsigned Reg = 0; |
| 528 | if (MRegisterInfo::isPhysicalRegister(SrcReg)) |
| 529 | Reg = SrcReg; |
| 530 | else if (vrm_->isAssignedReg(SrcReg)) |
| 531 | Reg = vrm_->getPhys(SrcReg); |
| 532 | if (Reg && allocatableRegs_[Reg] && RC->contains(Reg)) |
| 533 | cur->preference = Reg; |
| 534 | } |
| 535 | } |
| 536 | } |
| 537 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 538 | // for every interval in inactive we overlap with, mark the |
| 539 | // register as not free and update spill weights. |
| 540 | for (IntervalPtrs::const_iterator i = inactive_.begin(), |
| 541 | e = inactive_.end(); i != e; ++i) { |
| 542 | unsigned Reg = i->first->reg; |
| 543 | assert(MRegisterInfo::isVirtualRegister(Reg) && |
| 544 | "Can only allocate virtual registers!"); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 545 | const TargetRegisterClass *RegRC = reginfo_->getRegClass(Reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 546 | // If this is not in a related reg class to the register we're allocating, |
| 547 | // don't check it. |
| 548 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && |
| 549 | cur->overlapsFrom(*i->first, i->second-1)) { |
| 550 | Reg = vrm_->getPhys(Reg); |
| 551 | prt_->addRegUse(Reg); |
| 552 | SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight)); |
| 553 | } |
| 554 | } |
| 555 | |
| 556 | // Speculatively check to see if we can get a register right now. If not, |
| 557 | // we know we won't be able to by adding more constraints. If so, we can |
| 558 | // check to see if it is valid. Doing an exhaustive search of the fixed_ list |
| 559 | // is very bad (it contains all callee clobbered registers for any functions |
| 560 | // with a call), so we want to avoid doing that if possible. |
| 561 | unsigned physReg = getFreePhysReg(cur); |
| 562 | if (physReg) { |
| 563 | // We got a register. However, if it's in the fixed_ list, we might |
| 564 | // conflict with it. Check to see if we conflict with it or any of its |
| 565 | // aliases. |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 566 | SmallSet<unsigned, 8> RegAliases; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 567 | for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS) |
| 568 | RegAliases.insert(*AS); |
| 569 | |
| 570 | bool ConflictsWithFixed = false; |
| 571 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
| 572 | IntervalPtr &IP = fixed_[i]; |
| 573 | if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) { |
| 574 | // Okay, this reg is on the fixed list. Check to see if we actually |
| 575 | // conflict. |
| 576 | LiveInterval *I = IP.first; |
| 577 | if (I->endNumber() > StartPosition) { |
| 578 | LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); |
| 579 | IP.second = II; |
| 580 | if (II != I->begin() && II->start > StartPosition) |
| 581 | --II; |
| 582 | if (cur->overlapsFrom(*I, II)) { |
| 583 | ConflictsWithFixed = true; |
| 584 | break; |
| 585 | } |
| 586 | } |
| 587 | } |
| 588 | } |
| 589 | |
| 590 | // Okay, the register picked by our speculative getFreePhysReg call turned |
| 591 | // out to be in use. Actually add all of the conflicting fixed registers to |
| 592 | // prt so we can do an accurate query. |
| 593 | if (ConflictsWithFixed) { |
| 594 | // For every interval in fixed we overlap with, mark the register as not |
| 595 | // free and update spill weights. |
| 596 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
| 597 | IntervalPtr &IP = fixed_[i]; |
| 598 | LiveInterval *I = IP.first; |
| 599 | |
| 600 | const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg]; |
| 601 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && |
| 602 | I->endNumber() > StartPosition) { |
| 603 | LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); |
| 604 | IP.second = II; |
| 605 | if (II != I->begin() && II->start > StartPosition) |
| 606 | --II; |
| 607 | if (cur->overlapsFrom(*I, II)) { |
| 608 | unsigned reg = I->reg; |
| 609 | prt_->addRegUse(reg); |
| 610 | SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight)); |
| 611 | } |
| 612 | } |
| 613 | } |
| 614 | |
| 615 | // Using the newly updated prt_ object, which includes conflicts in the |
| 616 | // future, see if there are any registers available. |
| 617 | physReg = getFreePhysReg(cur); |
| 618 | } |
| 619 | } |
| 620 | |
| 621 | // Restore the physical register tracker, removing information about the |
| 622 | // future. |
| 623 | *prt_ = backupPrt; |
| 624 | |
| 625 | // if we find a free register, we are done: assign this virtual to |
| 626 | // the free physical register and add this interval to the active |
| 627 | // list. |
| 628 | if (physReg) { |
| 629 | DOUT << mri_->getName(physReg) << '\n'; |
| 630 | vrm_->assignVirt2Phys(cur->reg, physReg); |
| 631 | prt_->addRegUse(physReg); |
| 632 | active_.push_back(std::make_pair(cur, cur->begin())); |
| 633 | handled_.push_back(cur); |
| 634 | return; |
| 635 | } |
| 636 | DOUT << "no free registers\n"; |
| 637 | |
| 638 | // Compile the spill weights into an array that is better for scanning. |
| 639 | std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0); |
| 640 | for (std::vector<std::pair<unsigned, float> >::iterator |
| 641 | I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I) |
| 642 | updateSpillWeights(SpillWeights, I->first, I->second, mri_); |
| 643 | |
| 644 | // for each interval in active, update spill weights. |
| 645 | for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end(); |
| 646 | i != e; ++i) { |
| 647 | unsigned reg = i->first->reg; |
| 648 | assert(MRegisterInfo::isVirtualRegister(reg) && |
| 649 | "Can only allocate virtual registers!"); |
| 650 | reg = vrm_->getPhys(reg); |
| 651 | updateSpillWeights(SpillWeights, reg, i->first->weight, mri_); |
| 652 | } |
| 653 | |
| 654 | DOUT << "\tassigning stack slot at interval "<< *cur << ":\n"; |
| 655 | |
| 656 | // Find a register to spill. |
| 657 | float minWeight = HUGE_VALF; |
| 658 | unsigned minReg = cur->preference; // Try the preferred register first. |
| 659 | |
| 660 | if (!minReg || SpillWeights[minReg] == HUGE_VALF) |
| 661 | for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), |
| 662 | e = RC->allocation_order_end(*mf_); i != e; ++i) { |
| 663 | unsigned reg = *i; |
| 664 | if (minWeight > SpillWeights[reg]) { |
| 665 | minWeight = SpillWeights[reg]; |
| 666 | minReg = reg; |
| 667 | } |
| 668 | } |
| 669 | |
| 670 | // If we didn't find a register that is spillable, try aliases? |
| 671 | if (!minReg) { |
| 672 | for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), |
| 673 | e = RC->allocation_order_end(*mf_); i != e; ++i) { |
| 674 | unsigned reg = *i; |
| 675 | // No need to worry about if the alias register size < regsize of RC. |
| 676 | // We are going to spill all registers that alias it anyway. |
| 677 | for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) { |
| 678 | if (minWeight > SpillWeights[*as]) { |
| 679 | minWeight = SpillWeights[*as]; |
| 680 | minReg = *as; |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | |
| 685 | // All registers must have inf weight. Just grab one! |
| 686 | if (!minReg) |
| 687 | minReg = *RC->allocation_order_begin(*mf_); |
| 688 | } |
| 689 | |
| 690 | DOUT << "\t\tregister with min weight: " |
| 691 | << mri_->getName(minReg) << " (" << minWeight << ")\n"; |
| 692 | |
| 693 | // if the current has the minimum weight, we need to spill it and |
| 694 | // add any added intervals back to unhandled, and restart |
| 695 | // linearscan. |
| 696 | if (cur->weight != HUGE_VALF && cur->weight <= minWeight) { |
| 697 | DOUT << "\t\t\tspilling(c): " << *cur << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 698 | std::vector<LiveInterval*> added = |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 699 | li_->addIntervalsForSpills(*cur, loopInfo, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 700 | if (added.empty()) |
| 701 | return; // Early exit if all spills were folded. |
| 702 | |
| 703 | // Merge added with unhandled. Note that we know that |
| 704 | // addIntervalsForSpills returns intervals sorted by their starting |
| 705 | // point. |
| 706 | for (unsigned i = 0, e = added.size(); i != e; ++i) |
| 707 | unhandled_.push(added[i]); |
| 708 | return; |
| 709 | } |
| 710 | |
| 711 | ++NumBacktracks; |
| 712 | |
| 713 | // push the current interval back to unhandled since we are going |
| 714 | // to re-run at least this iteration. Since we didn't modify it it |
| 715 | // should go back right in the front of the list |
| 716 | unhandled_.push(cur); |
| 717 | |
| 718 | // otherwise we spill all intervals aliasing the register with |
| 719 | // minimum weight, rollback to the interval with the earliest |
| 720 | // start point and let the linear scan algorithm run again |
| 721 | std::vector<LiveInterval*> added; |
| 722 | assert(MRegisterInfo::isPhysicalRegister(minReg) && |
| 723 | "did not choose a register to spill?"); |
| 724 | BitVector toSpill(mri_->getNumRegs()); |
| 725 | |
| 726 | // We are going to spill minReg and all its aliases. |
| 727 | toSpill[minReg] = true; |
| 728 | for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as) |
| 729 | toSpill[*as] = true; |
| 730 | |
| 731 | // the earliest start of a spilled interval indicates up to where |
| 732 | // in handled we need to roll back |
| 733 | unsigned earliestStart = cur->beginNumber(); |
| 734 | |
| 735 | // set of spilled vregs (used later to rollback properly) |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 736 | SmallSet<unsigned, 32> spilled; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 737 | |
| 738 | // spill live intervals of virtual regs mapped to the physical register we |
| 739 | // want to clear (and its aliases). We only spill those that overlap with the |
| 740 | // current interval as the rest do not affect its allocation. we also keep |
| 741 | // track of the earliest start of all spilled live intervals since this will |
| 742 | // mark our rollback point. |
| 743 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { |
| 744 | unsigned reg = i->first->reg; |
| 745 | if (//MRegisterInfo::isVirtualRegister(reg) && |
| 746 | toSpill[vrm_->getPhys(reg)] && |
| 747 | cur->overlapsFrom(*i->first, i->second)) { |
| 748 | DOUT << "\t\t\tspilling(a): " << *i->first << '\n'; |
| 749 | earliestStart = std::min(earliestStart, i->first->beginNumber()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 750 | std::vector<LiveInterval*> newIs = |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 751 | li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 752 | std::copy(newIs.begin(), newIs.end(), std::back_inserter(added)); |
| 753 | spilled.insert(reg); |
| 754 | } |
| 755 | } |
| 756 | for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){ |
| 757 | unsigned reg = i->first->reg; |
| 758 | if (//MRegisterInfo::isVirtualRegister(reg) && |
| 759 | toSpill[vrm_->getPhys(reg)] && |
| 760 | cur->overlapsFrom(*i->first, i->second-1)) { |
| 761 | DOUT << "\t\t\tspilling(i): " << *i->first << '\n'; |
| 762 | earliestStart = std::min(earliestStart, i->first->beginNumber()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 763 | std::vector<LiveInterval*> newIs = |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 764 | li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 765 | std::copy(newIs.begin(), newIs.end(), std::back_inserter(added)); |
| 766 | spilled.insert(reg); |
| 767 | } |
| 768 | } |
| 769 | |
| 770 | DOUT << "\t\trolling back to: " << earliestStart << '\n'; |
| 771 | |
| 772 | // Scan handled in reverse order up to the earliest start of a |
| 773 | // spilled live interval and undo each one, restoring the state of |
| 774 | // unhandled. |
| 775 | while (!handled_.empty()) { |
| 776 | LiveInterval* i = handled_.back(); |
| 777 | // If this interval starts before t we are done. |
| 778 | if (i->beginNumber() < earliestStart) |
| 779 | break; |
| 780 | DOUT << "\t\t\tundo changes for: " << *i << '\n'; |
| 781 | handled_.pop_back(); |
| 782 | |
| 783 | // When undoing a live interval allocation we must know if it is active or |
| 784 | // inactive to properly update the PhysRegTracker and the VirtRegMap. |
| 785 | IntervalPtrs::iterator it; |
| 786 | if ((it = FindIntervalInVector(active_, i)) != active_.end()) { |
| 787 | active_.erase(it); |
| 788 | assert(!MRegisterInfo::isPhysicalRegister(i->reg)); |
| 789 | if (!spilled.count(i->reg)) |
| 790 | unhandled_.push(i); |
| 791 | prt_->delRegUse(vrm_->getPhys(i->reg)); |
| 792 | vrm_->clearVirt(i->reg); |
| 793 | } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) { |
| 794 | inactive_.erase(it); |
| 795 | assert(!MRegisterInfo::isPhysicalRegister(i->reg)); |
| 796 | if (!spilled.count(i->reg)) |
| 797 | unhandled_.push(i); |
| 798 | vrm_->clearVirt(i->reg); |
| 799 | } else { |
| 800 | assert(MRegisterInfo::isVirtualRegister(i->reg) && |
| 801 | "Can only allocate virtual registers!"); |
| 802 | vrm_->clearVirt(i->reg); |
| 803 | unhandled_.push(i); |
| 804 | } |
Evan Cheng | b6aa671 | 2007-11-04 08:32:21 +0000 | [diff] [blame] | 805 | |
| 806 | // It interval has a preference, it must be defined by a copy. Clear the |
| 807 | // preference now since the source interval allocation may have been undone |
| 808 | // as well. |
| 809 | i->preference = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 810 | } |
| 811 | |
| 812 | // Rewind the iterators in the active, inactive, and fixed lists back to the |
| 813 | // point we reverted to. |
| 814 | RevertVectorIteratorsTo(active_, earliestStart); |
| 815 | RevertVectorIteratorsTo(inactive_, earliestStart); |
| 816 | RevertVectorIteratorsTo(fixed_, earliestStart); |
| 817 | |
| 818 | // scan the rest and undo each interval that expired after t and |
| 819 | // insert it in active (the next iteration of the algorithm will |
| 820 | // put it in inactive if required) |
| 821 | for (unsigned i = 0, e = handled_.size(); i != e; ++i) { |
| 822 | LiveInterval *HI = handled_[i]; |
| 823 | if (!HI->expiredAt(earliestStart) && |
| 824 | HI->expiredAt(cur->beginNumber())) { |
| 825 | DOUT << "\t\t\tundo changes for: " << *HI << '\n'; |
| 826 | active_.push_back(std::make_pair(HI, HI->begin())); |
| 827 | assert(!MRegisterInfo::isPhysicalRegister(HI->reg)); |
| 828 | prt_->addRegUse(vrm_->getPhys(HI->reg)); |
| 829 | } |
| 830 | } |
| 831 | |
| 832 | // merge added with unhandled |
| 833 | for (unsigned i = 0, e = added.size(); i != e; ++i) |
| 834 | unhandled_.push(added[i]); |
| 835 | } |
| 836 | |
| 837 | /// getFreePhysReg - return a free physical register for this virtual register |
| 838 | /// interval if we have one, otherwise return 0. |
| 839 | unsigned RALinScan::getFreePhysReg(LiveInterval *cur) { |
| 840 | std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0); |
| 841 | unsigned MaxInactiveCount = 0; |
| 842 | |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 843 | const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 844 | const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); |
| 845 | |
| 846 | for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end(); |
| 847 | i != e; ++i) { |
| 848 | unsigned reg = i->first->reg; |
| 849 | assert(MRegisterInfo::isVirtualRegister(reg) && |
| 850 | "Can only allocate virtual registers!"); |
| 851 | |
| 852 | // If this is not in a related reg class to the register we're allocating, |
| 853 | // don't check it. |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 854 | const TargetRegisterClass *RegRC = reginfo_->getRegClass(reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 855 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) { |
| 856 | reg = vrm_->getPhys(reg); |
| 857 | ++inactiveCounts[reg]; |
| 858 | MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]); |
| 859 | } |
| 860 | } |
| 861 | |
| 862 | unsigned FreeReg = 0; |
| 863 | unsigned FreeRegInactiveCount = 0; |
| 864 | |
| 865 | // If copy coalescer has assigned a "preferred" register, check if it's |
| 866 | // available first. |
| 867 | if (cur->preference) |
| 868 | if (prt_->isRegAvail(cur->preference)) { |
| 869 | DOUT << "\t\tassigned the preferred register: " |
| 870 | << mri_->getName(cur->preference) << "\n"; |
| 871 | return cur->preference; |
| 872 | } else |
| 873 | DOUT << "\t\tunable to assign the preferred register: " |
| 874 | << mri_->getName(cur->preference) << "\n"; |
| 875 | |
| 876 | // Scan for the first available register. |
| 877 | TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_); |
| 878 | TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_); |
| 879 | for (; I != E; ++I) |
| 880 | if (prt_->isRegAvail(*I)) { |
| 881 | FreeReg = *I; |
| 882 | FreeRegInactiveCount = inactiveCounts[FreeReg]; |
| 883 | break; |
| 884 | } |
| 885 | |
| 886 | // If there are no free regs, or if this reg has the max inactive count, |
| 887 | // return this register. |
| 888 | if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg; |
| 889 | |
| 890 | // Continue scanning the registers, looking for the one with the highest |
| 891 | // inactive count. Alkis found that this reduced register pressure very |
| 892 | // slightly on X86 (in rev 1.94 of this file), though this should probably be |
| 893 | // reevaluated now. |
| 894 | for (; I != E; ++I) { |
| 895 | unsigned Reg = *I; |
| 896 | if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) { |
| 897 | FreeReg = Reg; |
| 898 | FreeRegInactiveCount = inactiveCounts[Reg]; |
| 899 | if (FreeRegInactiveCount == MaxInactiveCount) |
| 900 | break; // We found the one with the max inactive count. |
| 901 | } |
| 902 | } |
| 903 | |
| 904 | return FreeReg; |
| 905 | } |
| 906 | |
| 907 | FunctionPass* llvm::createLinearScanRegisterAllocator() { |
| 908 | return new RALinScan(); |
| 909 | } |