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