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