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