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 | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 26 | #include "llvm/Target/TargetRegisterInfo.h" |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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_; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 65 | const TargetRegisterInfo* tri_; |
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; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 164 | if (TargetRegisterInfo::isVirtualRegister(reg)) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 165 | reg = vrm_->getPhys(reg); |
| 166 | } |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 167 | DOUT << tri_->getName(reg) << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 168 | } |
| 169 | } |
| 170 | }; |
| 171 | char RALinScan::ID = 0; |
| 172 | } |
| 173 | |
| 174 | void RALinScan::ComputeRelatedRegClasses() { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 175 | const TargetRegisterInfo &TRI = *tri_; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 180 | for (TargetRegisterInfo::regclass_iterator RCI = TRI.regclass_begin(), |
| 181 | E = TRI.regclass_end(); RCI != E; ++RCI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 182 | RelatedRegClasses.insert(*RCI); |
| 183 | for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end(); |
| 184 | I != E; ++I) { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 185 | HasAliases = HasAliases || *TRI.getAliasSet(*I) != 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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) |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 205 | for (const unsigned *AS = TRI.getAliasSet(I->first); *AS; ++AS) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 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; |
Anton Korobeynikov | 6a4a933 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 227 | if (TargetRegisterInfo::isVirtualRegister(SrcReg)) { |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 228 | if (!vrm_->isAssignedReg(SrcReg)) |
| 229 | return Reg; |
| 230 | else |
| 231 | SrcReg = vrm_->getPhys(SrcReg); |
Anton Korobeynikov | 6a4a933 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 232 | } |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 233 | if (Reg == SrcReg) |
| 234 | return Reg; |
| 235 | |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 236 | const TargetRegisterClass *RC = reginfo_->getRegClass(cur.reg); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 237 | if (!RC->contains(SrcReg)) |
| 238 | return Reg; |
| 239 | |
| 240 | // Try to coalesce. |
| 241 | if (!li_->conflictsWithPhysRegDef(cur, *vrm_, SrcReg)) { |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 242 | DOUT << "Coalescing: " << cur << " -> " << tri_->getName(SrcReg) |
Bill Wendling | 8eeb979 | 2008-02-26 21:11:01 +0000 | [diff] [blame] | 243 | << '\n'; |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 244 | vrm_->clearVirt(cur.reg); |
| 245 | vrm_->assignVirt2Phys(cur.reg, SrcReg); |
| 246 | ++NumCoalesce; |
| 247 | return SrcReg; |
| 248 | } |
| 249 | |
| 250 | return Reg; |
| 251 | } |
| 252 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 253 | bool RALinScan::runOnMachineFunction(MachineFunction &fn) { |
| 254 | mf_ = &fn; |
| 255 | tm_ = &fn.getTarget(); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 256 | tri_ = tm_->getRegisterInfo(); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 257 | tii_ = tm_->getInstrInfo(); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 258 | reginfo_ = &mf_->getRegInfo(); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 259 | allocatableRegs_ = tri_->getAllocatableSet(fn); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 260 | li_ = &getAnalysis<LiveIntervals>(); |
Evan Cheng | 26d17df | 2007-12-11 02:09:15 +0000 | [diff] [blame] | 261 | loopInfo = &getAnalysis<MachineLoopInfo>(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 262 | |
David Greene | 1d80f1b | 2007-09-06 16:18:45 +0000 | [diff] [blame] | 263 | // We don't run the coalescer here because we have no reason to |
| 264 | // interact with it. If the coalescer requires interaction, it |
| 265 | // won't do anything. If it doesn't require interaction, we assume |
| 266 | // it was run as a separate pass. |
| 267 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 268 | // If this is the first function compiled, compute the related reg classes. |
| 269 | if (RelatedRegClasses.empty()) |
| 270 | ComputeRelatedRegClasses(); |
| 271 | |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 272 | if (!prt_.get()) prt_.reset(new PhysRegTracker(*tri_)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 273 | vrm_.reset(new VirtRegMap(*mf_)); |
| 274 | if (!spiller_.get()) spiller_.reset(createSpiller()); |
| 275 | |
| 276 | initIntervalSets(); |
| 277 | |
| 278 | linearScan(); |
| 279 | |
| 280 | // Rewrite spill code and update the PhysRegsUsed set. |
| 281 | spiller_->runOnMachineFunction(*mf_, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 282 | vrm_.reset(); // Free the VirtRegMap |
| 283 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 284 | while (!unhandled_.empty()) unhandled_.pop(); |
| 285 | fixed_.clear(); |
| 286 | active_.clear(); |
| 287 | inactive_.clear(); |
| 288 | handled_.clear(); |
| 289 | |
| 290 | return true; |
| 291 | } |
| 292 | |
| 293 | /// initIntervalSets - initialize the interval sets. |
| 294 | /// |
| 295 | void RALinScan::initIntervalSets() |
| 296 | { |
| 297 | assert(unhandled_.empty() && fixed_.empty() && |
| 298 | active_.empty() && inactive_.empty() && |
| 299 | "interval sets should be empty on initialization"); |
| 300 | |
| 301 | for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 302 | if (TargetRegisterInfo::isPhysicalRegister(i->second.reg)) { |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 303 | reginfo_->setPhysRegUsed(i->second.reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 304 | fixed_.push_back(std::make_pair(&i->second, i->second.begin())); |
| 305 | } else |
| 306 | unhandled_.push(&i->second); |
| 307 | } |
| 308 | } |
| 309 | |
| 310 | void RALinScan::linearScan() |
| 311 | { |
| 312 | // linear scan algorithm |
| 313 | DOUT << "********** LINEAR SCAN **********\n"; |
| 314 | DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n'; |
| 315 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 316 | DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end())); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 317 | |
| 318 | while (!unhandled_.empty()) { |
| 319 | // pick the interval with the earliest start point |
| 320 | LiveInterval* cur = unhandled_.top(); |
| 321 | unhandled_.pop(); |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 322 | ++NumIters; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 323 | DOUT << "\n*** CURRENT ***: " << *cur << '\n'; |
| 324 | |
Evan Cheng | a318699 | 2008-04-03 16:40:27 +0000 | [diff] [blame^] | 325 | if (!cur->empty()) { |
| 326 | processActiveIntervals(cur->beginNumber()); |
| 327 | processInactiveIntervals(cur->beginNumber()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 328 | |
Evan Cheng | a318699 | 2008-04-03 16:40:27 +0000 | [diff] [blame^] | 329 | assert(TargetRegisterInfo::isVirtualRegister(cur->reg) && |
| 330 | "Can only allocate virtual registers!"); |
| 331 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 332 | |
| 333 | // Allocating a virtual register. try to find a free |
| 334 | // physical register or spill an interval (possibly this one) in order to |
| 335 | // assign it one. |
| 336 | assignRegOrStackSlotAtInterval(cur); |
| 337 | |
| 338 | DEBUG(printIntervals("active", active_.begin(), active_.end())); |
| 339 | DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end())); |
| 340 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 341 | |
| 342 | // expire any remaining active intervals |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 343 | while (!active_.empty()) { |
| 344 | IntervalPtr &IP = active_.back(); |
| 345 | unsigned reg = IP.first->reg; |
| 346 | DOUT << "\tinterval " << *IP.first << " expired\n"; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 347 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 348 | "Can only allocate virtual registers!"); |
| 349 | reg = vrm_->getPhys(reg); |
| 350 | prt_->delRegUse(reg); |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 351 | active_.pop_back(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 352 | } |
| 353 | |
| 354 | // expire any remaining inactive intervals |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 355 | DEBUG(for (IntervalPtrs::reverse_iterator |
Bill Wendling | 1817ab8 | 2007-11-15 00:40:48 +0000 | [diff] [blame] | 356 | i = inactive_.rbegin(); i != inactive_.rend(); ++i) |
Evan Cheng | d48f2bc | 2007-10-16 21:09:14 +0000 | [diff] [blame] | 357 | DOUT << "\tinterval " << *i->first << " expired\n"); |
| 358 | inactive_.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 359 | |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 360 | // 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] | 361 | MachineFunction::iterator EntryMBB = mf_->begin(); |
Evan Cheng | 12d6fcb | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 362 | SmallVector<MachineBasicBlock*, 8> LiveInMBBs; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 363 | for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) { |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 364 | LiveInterval &cur = i->second; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 365 | unsigned Reg = 0; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 366 | bool isPhys = TargetRegisterInfo::isPhysicalRegister(cur.reg); |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 367 | if (isPhys) |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 368 | Reg = i->second.reg; |
| 369 | else if (vrm_->isAssignedReg(cur.reg)) |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 370 | Reg = attemptTrivialCoalescing(cur, vrm_->getPhys(cur.reg)); |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 371 | if (!Reg) |
| 372 | continue; |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 373 | // Ignore splited live intervals. |
| 374 | if (!isPhys && vrm_->getPreSplitReg(cur.reg)) |
| 375 | continue; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 376 | for (LiveInterval::Ranges::const_iterator I = cur.begin(), E = cur.end(); |
| 377 | I != E; ++I) { |
| 378 | const LiveRange &LR = *I; |
Evan Cheng | f5cdf12 | 2007-10-17 02:12:22 +0000 | [diff] [blame] | 379 | if (li_->findLiveInMBBs(LR, LiveInMBBs)) { |
| 380 | for (unsigned i = 0, e = LiveInMBBs.size(); i != e; ++i) |
| 381 | if (LiveInMBBs[i] != EntryMBB) |
| 382 | LiveInMBBs[i]->addLiveIn(Reg); |
Evan Cheng | 12d6fcb | 2007-10-17 06:53:44 +0000 | [diff] [blame] | 383 | LiveInMBBs.clear(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 384 | } |
| 385 | } |
| 386 | } |
| 387 | |
| 388 | DOUT << *vrm_; |
| 389 | } |
| 390 | |
| 391 | /// processActiveIntervals - expire old intervals and move non-overlapping ones |
| 392 | /// to the inactive list. |
| 393 | void RALinScan::processActiveIntervals(unsigned CurPoint) |
| 394 | { |
| 395 | DOUT << "\tprocessing active intervals:\n"; |
| 396 | |
| 397 | for (unsigned i = 0, e = active_.size(); i != e; ++i) { |
| 398 | LiveInterval *Interval = active_[i].first; |
| 399 | LiveInterval::iterator IntervalPos = active_[i].second; |
| 400 | unsigned reg = Interval->reg; |
| 401 | |
| 402 | IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); |
| 403 | |
| 404 | if (IntervalPos == Interval->end()) { // Remove expired intervals. |
| 405 | DOUT << "\t\tinterval " << *Interval << " expired\n"; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 406 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 407 | "Can only allocate virtual registers!"); |
| 408 | reg = vrm_->getPhys(reg); |
| 409 | prt_->delRegUse(reg); |
| 410 | |
| 411 | // Pop off the end of the list. |
| 412 | active_[i] = active_.back(); |
| 413 | active_.pop_back(); |
| 414 | --i; --e; |
| 415 | |
| 416 | } else if (IntervalPos->start > CurPoint) { |
| 417 | // Move inactive intervals to inactive list. |
| 418 | DOUT << "\t\tinterval " << *Interval << " inactive\n"; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 419 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 420 | "Can only allocate virtual registers!"); |
| 421 | reg = vrm_->getPhys(reg); |
| 422 | prt_->delRegUse(reg); |
| 423 | // add to inactive. |
| 424 | inactive_.push_back(std::make_pair(Interval, IntervalPos)); |
| 425 | |
| 426 | // Pop off the end of the list. |
| 427 | active_[i] = active_.back(); |
| 428 | active_.pop_back(); |
| 429 | --i; --e; |
| 430 | } else { |
| 431 | // Otherwise, just update the iterator position. |
| 432 | active_[i].second = IntervalPos; |
| 433 | } |
| 434 | } |
| 435 | } |
| 436 | |
| 437 | /// processInactiveIntervals - expire old intervals and move overlapping |
| 438 | /// ones to the active list. |
| 439 | void RALinScan::processInactiveIntervals(unsigned CurPoint) |
| 440 | { |
| 441 | DOUT << "\tprocessing inactive intervals:\n"; |
| 442 | |
| 443 | for (unsigned i = 0, e = inactive_.size(); i != e; ++i) { |
| 444 | LiveInterval *Interval = inactive_[i].first; |
| 445 | LiveInterval::iterator IntervalPos = inactive_[i].second; |
| 446 | unsigned reg = Interval->reg; |
| 447 | |
| 448 | IntervalPos = Interval->advanceTo(IntervalPos, CurPoint); |
| 449 | |
| 450 | if (IntervalPos == Interval->end()) { // remove expired intervals. |
| 451 | DOUT << "\t\tinterval " << *Interval << " expired\n"; |
| 452 | |
| 453 | // Pop off the end of the list. |
| 454 | inactive_[i] = inactive_.back(); |
| 455 | inactive_.pop_back(); |
| 456 | --i; --e; |
| 457 | } else if (IntervalPos->start <= CurPoint) { |
| 458 | // move re-activated intervals in active list |
| 459 | DOUT << "\t\tinterval " << *Interval << " active\n"; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 460 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 461 | "Can only allocate virtual registers!"); |
| 462 | reg = vrm_->getPhys(reg); |
| 463 | prt_->addRegUse(reg); |
| 464 | // add to active |
| 465 | active_.push_back(std::make_pair(Interval, IntervalPos)); |
| 466 | |
| 467 | // Pop off the end of the list. |
| 468 | inactive_[i] = inactive_.back(); |
| 469 | inactive_.pop_back(); |
| 470 | --i; --e; |
| 471 | } else { |
| 472 | // Otherwise, just update the iterator position. |
| 473 | inactive_[i].second = IntervalPos; |
| 474 | } |
| 475 | } |
| 476 | } |
| 477 | |
| 478 | /// updateSpillWeights - updates the spill weights of the specifed physical |
| 479 | /// register and its weight. |
| 480 | static void updateSpillWeights(std::vector<float> &Weights, |
| 481 | unsigned reg, float weight, |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 482 | const TargetRegisterInfo *TRI) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 483 | Weights[reg] += weight; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 484 | for (const unsigned* as = TRI->getAliasSet(reg); *as; ++as) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 485 | Weights[*as] += weight; |
| 486 | } |
| 487 | |
| 488 | static |
| 489 | RALinScan::IntervalPtrs::iterator |
| 490 | FindIntervalInVector(RALinScan::IntervalPtrs &IP, LiveInterval *LI) { |
| 491 | for (RALinScan::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); |
| 492 | I != E; ++I) |
| 493 | if (I->first == LI) return I; |
| 494 | return IP.end(); |
| 495 | } |
| 496 | |
| 497 | static void RevertVectorIteratorsTo(RALinScan::IntervalPtrs &V, unsigned Point){ |
| 498 | for (unsigned i = 0, e = V.size(); i != e; ++i) { |
| 499 | RALinScan::IntervalPtr &IP = V[i]; |
| 500 | LiveInterval::iterator I = std::upper_bound(IP.first->begin(), |
| 501 | IP.second, Point); |
| 502 | if (I != IP.first->begin()) --I; |
| 503 | IP.second = I; |
| 504 | } |
| 505 | } |
| 506 | |
| 507 | /// assignRegOrStackSlotAtInterval - assign a register if one is available, or |
| 508 | /// spill. |
| 509 | void RALinScan::assignRegOrStackSlotAtInterval(LiveInterval* cur) |
| 510 | { |
| 511 | DOUT << "\tallocating current interval: "; |
| 512 | |
Evan Cheng | a318699 | 2008-04-03 16:40:27 +0000 | [diff] [blame^] | 513 | // This is an implicitly defined live interval, just assign any register. |
| 514 | const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg); |
| 515 | if (cur->empty()) { |
| 516 | unsigned physReg = cur->preference; |
| 517 | if (!physReg) |
| 518 | physReg = *RC->allocation_order_begin(*mf_); |
| 519 | DOUT << tri_->getName(physReg) << '\n'; |
| 520 | // Note the register is not really in use. |
| 521 | vrm_->assignVirt2Phys(cur->reg, physReg); |
| 522 | handled_.push_back(cur); |
| 523 | return; |
| 524 | } |
| 525 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 526 | PhysRegTracker backupPrt = *prt_; |
| 527 | |
| 528 | std::vector<std::pair<unsigned, float> > SpillWeightsToAdd; |
| 529 | unsigned StartPosition = cur->beginNumber(); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 530 | const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 531 | |
| 532 | // If this live interval is defined by a move instruction and its source is |
| 533 | // assigned a physical register that is compatible with the target register |
| 534 | // class, then we should try to assign it the same register. |
| 535 | // This can happen when the move is from a larger register class to a smaller |
| 536 | // one, e.g. X86::mov32to32_. These move instructions are not coalescable. |
| 537 | if (!cur->preference && cur->containsOneValue()) { |
| 538 | VNInfo *vni = cur->getValNumInfo(0); |
| 539 | if (vni->def && vni->def != ~1U && vni->def != ~0U) { |
| 540 | MachineInstr *CopyMI = li_->getInstructionFromIndex(vni->def); |
| 541 | unsigned SrcReg, DstReg; |
| 542 | if (tii_->isMoveInstr(*CopyMI, SrcReg, DstReg)) { |
| 543 | unsigned Reg = 0; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 544 | if (TargetRegisterInfo::isPhysicalRegister(SrcReg)) |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 545 | Reg = SrcReg; |
| 546 | else if (vrm_->isAssignedReg(SrcReg)) |
| 547 | Reg = vrm_->getPhys(SrcReg); |
| 548 | if (Reg && allocatableRegs_[Reg] && RC->contains(Reg)) |
| 549 | cur->preference = Reg; |
| 550 | } |
| 551 | } |
| 552 | } |
| 553 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 554 | // for every interval in inactive we overlap with, mark the |
| 555 | // register as not free and update spill weights. |
| 556 | for (IntervalPtrs::const_iterator i = inactive_.begin(), |
| 557 | e = inactive_.end(); i != e; ++i) { |
| 558 | unsigned Reg = i->first->reg; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 559 | assert(TargetRegisterInfo::isVirtualRegister(Reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 560 | "Can only allocate virtual registers!"); |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 561 | const TargetRegisterClass *RegRC = reginfo_->getRegClass(Reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 562 | // If this is not in a related reg class to the register we're allocating, |
| 563 | // don't check it. |
| 564 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && |
| 565 | cur->overlapsFrom(*i->first, i->second-1)) { |
| 566 | Reg = vrm_->getPhys(Reg); |
| 567 | prt_->addRegUse(Reg); |
| 568 | SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight)); |
| 569 | } |
| 570 | } |
| 571 | |
| 572 | // Speculatively check to see if we can get a register right now. If not, |
| 573 | // we know we won't be able to by adding more constraints. If so, we can |
| 574 | // check to see if it is valid. Doing an exhaustive search of the fixed_ list |
| 575 | // is very bad (it contains all callee clobbered registers for any functions |
| 576 | // with a call), so we want to avoid doing that if possible. |
| 577 | unsigned physReg = getFreePhysReg(cur); |
Evan Cheng | 14cc83f | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 578 | unsigned BestPhysReg = physReg; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 579 | if (physReg) { |
| 580 | // We got a register. However, if it's in the fixed_ list, we might |
| 581 | // conflict with it. Check to see if we conflict with it or any of its |
| 582 | // aliases. |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 583 | SmallSet<unsigned, 8> RegAliases; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 584 | for (const unsigned *AS = tri_->getAliasSet(physReg); *AS; ++AS) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 585 | RegAliases.insert(*AS); |
| 586 | |
| 587 | bool ConflictsWithFixed = false; |
| 588 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
| 589 | IntervalPtr &IP = fixed_[i]; |
| 590 | if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) { |
| 591 | // Okay, this reg is on the fixed list. Check to see if we actually |
| 592 | // conflict. |
| 593 | LiveInterval *I = IP.first; |
| 594 | if (I->endNumber() > StartPosition) { |
| 595 | LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); |
| 596 | IP.second = II; |
| 597 | if (II != I->begin() && II->start > StartPosition) |
| 598 | --II; |
| 599 | if (cur->overlapsFrom(*I, II)) { |
| 600 | ConflictsWithFixed = true; |
| 601 | break; |
| 602 | } |
| 603 | } |
| 604 | } |
| 605 | } |
| 606 | |
| 607 | // Okay, the register picked by our speculative getFreePhysReg call turned |
| 608 | // out to be in use. Actually add all of the conflicting fixed registers to |
| 609 | // prt so we can do an accurate query. |
| 610 | if (ConflictsWithFixed) { |
| 611 | // For every interval in fixed we overlap with, mark the register as not |
| 612 | // free and update spill weights. |
| 613 | for (unsigned i = 0, e = fixed_.size(); i != e; ++i) { |
| 614 | IntervalPtr &IP = fixed_[i]; |
| 615 | LiveInterval *I = IP.first; |
| 616 | |
| 617 | const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg]; |
| 618 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader && |
| 619 | I->endNumber() > StartPosition) { |
| 620 | LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition); |
| 621 | IP.second = II; |
| 622 | if (II != I->begin() && II->start > StartPosition) |
| 623 | --II; |
| 624 | if (cur->overlapsFrom(*I, II)) { |
| 625 | unsigned reg = I->reg; |
| 626 | prt_->addRegUse(reg); |
| 627 | SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight)); |
| 628 | } |
| 629 | } |
| 630 | } |
| 631 | |
| 632 | // Using the newly updated prt_ object, which includes conflicts in the |
| 633 | // future, see if there are any registers available. |
| 634 | physReg = getFreePhysReg(cur); |
| 635 | } |
| 636 | } |
| 637 | |
| 638 | // Restore the physical register tracker, removing information about the |
| 639 | // future. |
| 640 | *prt_ = backupPrt; |
| 641 | |
| 642 | // if we find a free register, we are done: assign this virtual to |
| 643 | // the free physical register and add this interval to the active |
| 644 | // list. |
| 645 | if (physReg) { |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 646 | DOUT << tri_->getName(physReg) << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 647 | vrm_->assignVirt2Phys(cur->reg, physReg); |
| 648 | prt_->addRegUse(physReg); |
| 649 | active_.push_back(std::make_pair(cur, cur->begin())); |
| 650 | handled_.push_back(cur); |
| 651 | return; |
| 652 | } |
| 653 | DOUT << "no free registers\n"; |
| 654 | |
| 655 | // Compile the spill weights into an array that is better for scanning. |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 656 | std::vector<float> SpillWeights(tri_->getNumRegs(), 0.0); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 657 | for (std::vector<std::pair<unsigned, float> >::iterator |
| 658 | I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I) |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 659 | updateSpillWeights(SpillWeights, I->first, I->second, tri_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 660 | |
| 661 | // for each interval in active, update spill weights. |
| 662 | for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end(); |
| 663 | i != e; ++i) { |
| 664 | unsigned reg = i->first->reg; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 665 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 666 | "Can only allocate virtual registers!"); |
| 667 | reg = vrm_->getPhys(reg); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 668 | updateSpillWeights(SpillWeights, reg, i->first->weight, tri_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 669 | } |
| 670 | |
| 671 | DOUT << "\tassigning stack slot at interval "<< *cur << ":\n"; |
| 672 | |
| 673 | // Find a register to spill. |
| 674 | float minWeight = HUGE_VALF; |
| 675 | unsigned minReg = cur->preference; // Try the preferred register first. |
| 676 | |
| 677 | if (!minReg || SpillWeights[minReg] == HUGE_VALF) |
| 678 | for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), |
| 679 | e = RC->allocation_order_end(*mf_); i != e; ++i) { |
| 680 | unsigned reg = *i; |
| 681 | if (minWeight > SpillWeights[reg]) { |
| 682 | minWeight = SpillWeights[reg]; |
| 683 | minReg = reg; |
| 684 | } |
| 685 | } |
| 686 | |
| 687 | // If we didn't find a register that is spillable, try aliases? |
| 688 | if (!minReg) { |
| 689 | for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_), |
| 690 | e = RC->allocation_order_end(*mf_); i != e; ++i) { |
| 691 | unsigned reg = *i; |
| 692 | // No need to worry about if the alias register size < regsize of RC. |
| 693 | // We are going to spill all registers that alias it anyway. |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 694 | for (const unsigned* as = tri_->getAliasSet(reg); *as; ++as) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 695 | if (minWeight > SpillWeights[*as]) { |
| 696 | minWeight = SpillWeights[*as]; |
| 697 | minReg = *as; |
| 698 | } |
| 699 | } |
| 700 | } |
| 701 | |
| 702 | // All registers must have inf weight. Just grab one! |
Evan Cheng | 14cc83f | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 703 | if (!minReg) { |
Evan Cheng | 3903333 | 2008-03-13 17:42:48 +0000 | [diff] [blame] | 704 | minReg = BestPhysReg ? BestPhysReg : *RC->allocation_order_begin(*mf_); |
| 705 | if (cur->weight == HUGE_VALF || cur->getSize() == 1) |
| 706 | // Spill a physical register around defs and uses. |
| 707 | li_->spillPhysRegAroundRegDefsUses(*cur, minReg, *vrm_); |
Evan Cheng | 14cc83f | 2008-03-11 07:19:34 +0000 | [diff] [blame] | 708 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 709 | } |
| 710 | |
| 711 | DOUT << "\t\tregister with min weight: " |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 712 | << tri_->getName(minReg) << " (" << minWeight << ")\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 713 | |
| 714 | // if the current has the minimum weight, we need to spill it and |
| 715 | // add any added intervals back to unhandled, and restart |
| 716 | // linearscan. |
| 717 | if (cur->weight != HUGE_VALF && cur->weight <= minWeight) { |
| 718 | DOUT << "\t\t\tspilling(c): " << *cur << '\n'; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 719 | std::vector<LiveInterval*> added = |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 720 | li_->addIntervalsForSpills(*cur, loopInfo, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 721 | if (added.empty()) |
| 722 | return; // Early exit if all spills were folded. |
| 723 | |
| 724 | // Merge added with unhandled. Note that we know that |
| 725 | // addIntervalsForSpills returns intervals sorted by their starting |
| 726 | // point. |
| 727 | for (unsigned i = 0, e = added.size(); i != e; ++i) |
| 728 | unhandled_.push(added[i]); |
| 729 | return; |
| 730 | } |
| 731 | |
| 732 | ++NumBacktracks; |
| 733 | |
| 734 | // push the current interval back to unhandled since we are going |
| 735 | // to re-run at least this iteration. Since we didn't modify it it |
| 736 | // should go back right in the front of the list |
| 737 | unhandled_.push(cur); |
| 738 | |
| 739 | // otherwise we spill all intervals aliasing the register with |
| 740 | // minimum weight, rollback to the interval with the earliest |
| 741 | // start point and let the linear scan algorithm run again |
| 742 | std::vector<LiveInterval*> added; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 743 | assert(TargetRegisterInfo::isPhysicalRegister(minReg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 744 | "did not choose a register to spill?"); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 745 | BitVector toSpill(tri_->getNumRegs()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 746 | |
| 747 | // We are going to spill minReg and all its aliases. |
| 748 | toSpill[minReg] = true; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 749 | for (const unsigned* as = tri_->getAliasSet(minReg); *as; ++as) |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 750 | toSpill[*as] = true; |
| 751 | |
| 752 | // the earliest start of a spilled interval indicates up to where |
| 753 | // in handled we need to roll back |
| 754 | unsigned earliestStart = cur->beginNumber(); |
| 755 | |
| 756 | // set of spilled vregs (used later to rollback properly) |
Evan Cheng | c4c75f5 | 2007-11-03 07:20:12 +0000 | [diff] [blame] | 757 | SmallSet<unsigned, 32> spilled; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 758 | |
| 759 | // spill live intervals of virtual regs mapped to the physical register we |
| 760 | // want to clear (and its aliases). We only spill those that overlap with the |
| 761 | // current interval as the rest do not affect its allocation. we also keep |
| 762 | // track of the earliest start of all spilled live intervals since this will |
| 763 | // mark our rollback point. |
| 764 | for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) { |
| 765 | unsigned reg = i->first->reg; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 766 | if (//TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 767 | toSpill[vrm_->getPhys(reg)] && |
| 768 | cur->overlapsFrom(*i->first, i->second)) { |
| 769 | DOUT << "\t\t\tspilling(a): " << *i->first << '\n'; |
| 770 | earliestStart = std::min(earliestStart, i->first->beginNumber()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 771 | std::vector<LiveInterval*> newIs = |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 772 | li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 773 | std::copy(newIs.begin(), newIs.end(), std::back_inserter(added)); |
| 774 | spilled.insert(reg); |
| 775 | } |
| 776 | } |
| 777 | for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){ |
| 778 | unsigned reg = i->first->reg; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 779 | if (//TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 780 | toSpill[vrm_->getPhys(reg)] && |
| 781 | cur->overlapsFrom(*i->first, i->second-1)) { |
| 782 | DOUT << "\t\t\tspilling(i): " << *i->first << '\n'; |
| 783 | earliestStart = std::min(earliestStart, i->first->beginNumber()); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 784 | std::vector<LiveInterval*> newIs = |
Evan Cheng | cecc822 | 2007-11-17 00:40:40 +0000 | [diff] [blame] | 785 | li_->addIntervalsForSpills(*i->first, loopInfo, *vrm_); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 786 | std::copy(newIs.begin(), newIs.end(), std::back_inserter(added)); |
| 787 | spilled.insert(reg); |
| 788 | } |
| 789 | } |
| 790 | |
| 791 | DOUT << "\t\trolling back to: " << earliestStart << '\n'; |
| 792 | |
| 793 | // Scan handled in reverse order up to the earliest start of a |
| 794 | // spilled live interval and undo each one, restoring the state of |
| 795 | // unhandled. |
| 796 | while (!handled_.empty()) { |
| 797 | LiveInterval* i = handled_.back(); |
| 798 | // If this interval starts before t we are done. |
| 799 | if (i->beginNumber() < earliestStart) |
| 800 | break; |
| 801 | DOUT << "\t\t\tundo changes for: " << *i << '\n'; |
| 802 | handled_.pop_back(); |
| 803 | |
| 804 | // When undoing a live interval allocation we must know if it is active or |
| 805 | // inactive to properly update the PhysRegTracker and the VirtRegMap. |
| 806 | IntervalPtrs::iterator it; |
| 807 | if ((it = FindIntervalInVector(active_, i)) != active_.end()) { |
| 808 | active_.erase(it); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 809 | assert(!TargetRegisterInfo::isPhysicalRegister(i->reg)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 810 | if (!spilled.count(i->reg)) |
| 811 | unhandled_.push(i); |
| 812 | prt_->delRegUse(vrm_->getPhys(i->reg)); |
| 813 | vrm_->clearVirt(i->reg); |
| 814 | } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) { |
| 815 | inactive_.erase(it); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 816 | assert(!TargetRegisterInfo::isPhysicalRegister(i->reg)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 817 | if (!spilled.count(i->reg)) |
| 818 | unhandled_.push(i); |
| 819 | vrm_->clearVirt(i->reg); |
| 820 | } else { |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 821 | assert(TargetRegisterInfo::isVirtualRegister(i->reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 822 | "Can only allocate virtual registers!"); |
| 823 | vrm_->clearVirt(i->reg); |
| 824 | unhandled_.push(i); |
| 825 | } |
Evan Cheng | b6aa671 | 2007-11-04 08:32:21 +0000 | [diff] [blame] | 826 | |
| 827 | // It interval has a preference, it must be defined by a copy. Clear the |
| 828 | // preference now since the source interval allocation may have been undone |
| 829 | // as well. |
| 830 | i->preference = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 831 | } |
| 832 | |
| 833 | // Rewind the iterators in the active, inactive, and fixed lists back to the |
| 834 | // point we reverted to. |
| 835 | RevertVectorIteratorsTo(active_, earliestStart); |
| 836 | RevertVectorIteratorsTo(inactive_, earliestStart); |
| 837 | RevertVectorIteratorsTo(fixed_, earliestStart); |
| 838 | |
| 839 | // scan the rest and undo each interval that expired after t and |
| 840 | // insert it in active (the next iteration of the algorithm will |
| 841 | // put it in inactive if required) |
| 842 | for (unsigned i = 0, e = handled_.size(); i != e; ++i) { |
| 843 | LiveInterval *HI = handled_[i]; |
| 844 | if (!HI->expiredAt(earliestStart) && |
| 845 | HI->expiredAt(cur->beginNumber())) { |
| 846 | DOUT << "\t\t\tundo changes for: " << *HI << '\n'; |
| 847 | active_.push_back(std::make_pair(HI, HI->begin())); |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 848 | assert(!TargetRegisterInfo::isPhysicalRegister(HI->reg)); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 849 | prt_->addRegUse(vrm_->getPhys(HI->reg)); |
| 850 | } |
| 851 | } |
| 852 | |
| 853 | // merge added with unhandled |
| 854 | for (unsigned i = 0, e = added.size(); i != e; ++i) |
| 855 | unhandled_.push(added[i]); |
| 856 | } |
| 857 | |
| 858 | /// getFreePhysReg - return a free physical register for this virtual register |
| 859 | /// interval if we have one, otherwise return 0. |
| 860 | unsigned RALinScan::getFreePhysReg(LiveInterval *cur) { |
Chris Lattner | 9f6dc2c | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 861 | SmallVector<unsigned, 256> inactiveCounts; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 862 | unsigned MaxInactiveCount = 0; |
| 863 | |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 864 | const TargetRegisterClass *RC = reginfo_->getRegClass(cur->reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 865 | const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC); |
| 866 | |
| 867 | for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end(); |
| 868 | i != e; ++i) { |
| 869 | unsigned reg = i->first->reg; |
Dan Gohman | 1e57df3 | 2008-02-10 18:45:23 +0000 | [diff] [blame] | 870 | assert(TargetRegisterInfo::isVirtualRegister(reg) && |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 871 | "Can only allocate virtual registers!"); |
| 872 | |
| 873 | // If this is not in a related reg class to the register we're allocating, |
| 874 | // don't check it. |
Chris Lattner | 1b98919 | 2007-12-31 04:13:23 +0000 | [diff] [blame] | 875 | const TargetRegisterClass *RegRC = reginfo_->getRegClass(reg); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 876 | if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) { |
| 877 | reg = vrm_->getPhys(reg); |
Chris Lattner | 9f6dc2c | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 878 | if (inactiveCounts.size() <= reg) |
| 879 | inactiveCounts.resize(reg+1); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 880 | ++inactiveCounts[reg]; |
| 881 | MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]); |
| 882 | } |
| 883 | } |
| 884 | |
| 885 | unsigned FreeReg = 0; |
| 886 | unsigned FreeRegInactiveCount = 0; |
| 887 | |
| 888 | // If copy coalescer has assigned a "preferred" register, check if it's |
| 889 | // available first. |
Anton Korobeynikov | 6a4a933 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 890 | if (cur->preference) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 891 | if (prt_->isRegAvail(cur->preference)) { |
| 892 | DOUT << "\t\tassigned the preferred register: " |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 893 | << tri_->getName(cur->preference) << "\n"; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 894 | return cur->preference; |
| 895 | } else |
| 896 | DOUT << "\t\tunable to assign the preferred register: " |
Bill Wendling | 9b0baeb | 2008-02-26 21:47:57 +0000 | [diff] [blame] | 897 | << tri_->getName(cur->preference) << "\n"; |
Anton Korobeynikov | 6a4a933 | 2008-02-20 12:07:57 +0000 | [diff] [blame] | 898 | } |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 899 | |
| 900 | // Scan for the first available register. |
| 901 | TargetRegisterClass::iterator I = RC->allocation_order_begin(*mf_); |
| 902 | TargetRegisterClass::iterator E = RC->allocation_order_end(*mf_); |
Evan Cheng | af091bd | 2008-03-24 23:28:21 +0000 | [diff] [blame] | 903 | assert(I != E && "No allocatable register in this register class!"); |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 904 | for (; I != E; ++I) |
| 905 | if (prt_->isRegAvail(*I)) { |
| 906 | FreeReg = *I; |
Chris Lattner | 9f6dc2c | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 907 | if (FreeReg < inactiveCounts.size()) |
| 908 | FreeRegInactiveCount = inactiveCounts[FreeReg]; |
| 909 | else |
| 910 | FreeRegInactiveCount = 0; |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 911 | break; |
| 912 | } |
Chris Lattner | 9f6dc2c | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 913 | |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 914 | // If there are no free regs, or if this reg has the max inactive count, |
| 915 | // return this register. |
| 916 | if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg; |
| 917 | |
| 918 | // Continue scanning the registers, looking for the one with the highest |
| 919 | // inactive count. Alkis found that this reduced register pressure very |
| 920 | // slightly on X86 (in rev 1.94 of this file), though this should probably be |
| 921 | // reevaluated now. |
| 922 | for (; I != E; ++I) { |
| 923 | unsigned Reg = *I; |
Chris Lattner | 9f6dc2c | 2008-02-26 22:08:41 +0000 | [diff] [blame] | 924 | if (prt_->isRegAvail(Reg) && Reg < inactiveCounts.size() && |
| 925 | FreeRegInactiveCount < inactiveCounts[Reg]) { |
Dan Gohman | f17a25c | 2007-07-18 16:29:46 +0000 | [diff] [blame] | 926 | FreeReg = Reg; |
| 927 | FreeRegInactiveCount = inactiveCounts[Reg]; |
| 928 | if (FreeRegInactiveCount == MaxInactiveCount) |
| 929 | break; // We found the one with the max inactive count. |
| 930 | } |
| 931 | } |
| 932 | |
| 933 | return FreeReg; |
| 934 | } |
| 935 | |
| 936 | FunctionPass* llvm::createLinearScanRegisterAllocator() { |
| 937 | return new RALinScan(); |
| 938 | } |