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