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