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