blob: 5463e4e4b6996cbfb0670a8691d60652e4fc66d3 [file] [log] [blame]
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +00001//===-- 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 Evlogimenos0d6c5b62004-02-24 08:58:30 +000013
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000014#define DEBUG_TYPE "regalloc"
Chris Lattner3c3fe462005-09-21 04:19:09 +000015#include "llvm/CodeGen/LiveIntervalAnalysis.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000016#include "PhysRegTracker.h"
17#include "VirtRegMap.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000018#include "llvm/Function.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000019#include "llvm/CodeGen/MachineFunctionPass.h"
20#include "llvm/CodeGen/MachineInstr.h"
Jim Laskey13ec7022006-08-01 14:21:23 +000021#include "llvm/CodeGen/MachinePassRegistry.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000022#include "llvm/CodeGen/Passes.h"
23#include "llvm/CodeGen/SSARegMap.h"
24#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000025#include "llvm/Target/TargetMachine.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000026#include "llvm/ADT/EquivalenceClasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000027#include "llvm/ADT/Statistic.h"
28#include "llvm/ADT/STLExtras.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000029#include "llvm/Support/Debug.h"
Chris Lattnerf8c68f62006-06-28 22:17:39 +000030#include "llvm/Support/Visibility.h"
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000031#include <algorithm>
Alkis Evlogimenos880e8e42004-05-08 03:50:03 +000032#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000033#include <iostream>
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +000034#include <set>
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +000035#include <queue>
Duraid Madina30059612005-12-28 04:55:42 +000036#include <memory>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000037using namespace llvm;
38
39namespace {
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000040
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000041 static Statistic<double> efficiency
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000042 ("regalloc", "Ratio of intervals processed over total intervals");
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000043 static Statistic<> NumBacktracks
44 ("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000045
Jim Laskey13ec7022006-08-01 14:21:23 +000046 static RegisterRegAlloc
47 linearscanRegAlloc("linearscan", " linear scan register allocator",
48 createLinearScanRegisterAllocator);
49
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000050 static unsigned numIterations = 0;
51 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000052
Chris Lattnerf8c68f62006-06-28 22:17:39 +000053 struct VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattnercbb56252004-11-18 02:42:27 +000054 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
55 typedef std::vector<IntervalPtr> IntervalPtrs;
56 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000057 /// RelatedRegClasses - This structure is built the first time a function is
58 /// compiled, and keeps track of which register classes have registers that
59 /// belong to multiple classes or have aliases that are in other classes.
60 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
61 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
62
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000063 MachineFunction* mf_;
64 const TargetMachine* tm_;
65 const MRegisterInfo* mri_;
66 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000067 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000068
69 /// handled_ - Intervals are added to the handled_ set in the order of their
70 /// start value. This is uses for backtracking.
71 std::vector<LiveInterval*> handled_;
72
73 /// fixed_ - Intervals that correspond to machine registers.
74 ///
75 IntervalPtrs fixed_;
76
77 /// active_ - Intervals that are currently being processed, and which have a
78 /// live range active for the current point.
79 IntervalPtrs active_;
80
81 /// inactive_ - Intervals that are currently being processed, but which have
82 /// a hold at the current point.
83 IntervalPtrs inactive_;
84
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000086 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000087 greater_ptr<LiveInterval> > IntervalHeap;
88 IntervalHeap unhandled_;
89 std::auto_ptr<PhysRegTracker> prt_;
90 std::auto_ptr<VirtRegMap> vrm_;
91 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000092
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000093 public:
94 virtual const char* getPassName() const {
95 return "Linear Scan Register Allocator";
96 }
97
98 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000099 AU.addRequired<LiveIntervals>();
100 MachineFunctionPass::getAnalysisUsage(AU);
101 }
102
103 /// runOnMachineFunction - register allocate the whole function
104 bool runOnMachineFunction(MachineFunction&);
105
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000106 private:
107 /// linearScan - the linear scan algorithm
108 void linearScan();
109
Chris Lattnercbb56252004-11-18 02:42:27 +0000110 /// initIntervalSets - initialize the interval sets.
111 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000112 void initIntervalSets();
113
Chris Lattnercbb56252004-11-18 02:42:27 +0000114 /// processActiveIntervals - expire old intervals and move non-overlapping
115 /// ones to the inactive list.
116 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000117
Chris Lattnercbb56252004-11-18 02:42:27 +0000118 /// processInactiveIntervals - expire old intervals and move overlapping
119 /// ones to the active list.
120 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000121
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000122 /// assignRegOrStackSlotAtInterval - assign a register if one
123 /// is available, or spill.
124 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
125
126 ///
127 /// register handling helpers
128 ///
129
Chris Lattnercbb56252004-11-18 02:42:27 +0000130 /// getFreePhysReg - return a free physical register for this virtual
131 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000132 unsigned getFreePhysReg(LiveInterval* cur);
133
134 /// assignVirt2StackSlot - assigns this virtual register to a
135 /// stack slot. returns the stack slot
136 int assignVirt2StackSlot(unsigned virtReg);
137
Chris Lattnerb9805782005-08-23 22:27:31 +0000138 void ComputeRelatedRegClasses();
139
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000140 template <typename ItTy>
141 void printIntervals(const char* const str, ItTy i, ItTy e) const {
142 if (str) std::cerr << str << " intervals:\n";
143 for (; i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000144 std::cerr << "\t" << *i->first << " -> ";
145 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000146 if (MRegisterInfo::isVirtualRegister(reg)) {
147 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000148 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000149 std::cerr << mri_->getName(reg) << '\n';
150 }
151 }
152 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000153}
154
Chris Lattnerb9805782005-08-23 22:27:31 +0000155void RA::ComputeRelatedRegClasses() {
156 const MRegisterInfo &MRI = *mri_;
157
158 // First pass, add all reg classes to the union, and determine at least one
159 // reg class that each register is in.
160 bool HasAliases = false;
161 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
162 E = MRI.regclass_end(); RCI != E; ++RCI) {
163 RelatedRegClasses.insert(*RCI);
164 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
165 I != E; ++I) {
166 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
167
168 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
169 if (PRC) {
170 // Already processed this register. Just make sure we know that
171 // multiple register classes share a register.
172 RelatedRegClasses.unionSets(PRC, *RCI);
173 } else {
174 PRC = *RCI;
175 }
176 }
177 }
178
179 // Second pass, now that we know conservatively what register classes each reg
180 // belongs to, add info about aliases. We don't need to do this for targets
181 // without register aliases.
182 if (HasAliases)
183 for (std::map<unsigned, const TargetRegisterClass*>::iterator
184 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
185 I != E; ++I)
186 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
187 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
188}
189
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000190bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000191 mf_ = &fn;
192 tm_ = &fn.getTarget();
193 mri_ = tm_->getRegisterInfo();
194 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000195
Chris Lattnerb9805782005-08-23 22:27:31 +0000196 // If this is the first function compiled, compute the related reg classes.
197 if (RelatedRegClasses.empty())
198 ComputeRelatedRegClasses();
199
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000200 PhysRegsUsed = new bool[mri_->getNumRegs()];
201 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
202 fn.setUsedPhysRegs(PhysRegsUsed);
203
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000204 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
205 vrm_.reset(new VirtRegMap(*mf_));
206 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000207
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000208 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000209
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000210 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000211
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000212 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000213 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000214
Chris Lattner510a3ea2004-09-30 02:02:33 +0000215 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000216
217
218 while (!unhandled_.empty()) unhandled_.pop();
219 fixed_.clear();
220 active_.clear();
221 inactive_.clear();
222 handled_.clear();
223
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000224 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000225}
226
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000227/// initIntervalSets - initialize the interval sets.
228///
229void RA::initIntervalSets()
230{
231 assert(unhandled_.empty() && fixed_.empty() &&
232 active_.empty() && inactive_.empty() &&
233 "interval sets should be empty on initialization");
234
235 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000236 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
237 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000238 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000239 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000240 unhandled_.push(&i->second);
241 }
242}
243
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000244void RA::linearScan()
245{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000246 // linear scan algorithm
247 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
248 DEBUG(std::cerr << "********** Function: "
249 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000250
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000251 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
252 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
253 DEBUG(printIntervals("active", active_.begin(), active_.end()));
254 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
255
256 while (!unhandled_.empty()) {
257 // pick the interval with the earliest start point
258 LiveInterval* cur = unhandled_.top();
259 unhandled_.pop();
260 ++numIterations;
261 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
262
Chris Lattnercbb56252004-11-18 02:42:27 +0000263 processActiveIntervals(cur->beginNumber());
264 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000265
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000266 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
267 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000268
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000269 // Allocating a virtual register. try to find a free
270 // physical register or spill an interval (possibly this one) in order to
271 // assign it one.
272 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000273
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000274 DEBUG(printIntervals("active", active_.begin(), active_.end()));
275 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 }
277 numIntervals += li_->getNumIntervals();
278 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000279
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000280 // expire any remaining active intervals
281 for (IntervalPtrs::reverse_iterator
282 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000283 unsigned reg = i->first->reg;
284 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000285 assert(MRegisterInfo::isVirtualRegister(reg) &&
286 "Can only allocate virtual registers!");
287 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000288 prt_->delRegUse(reg);
289 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
290 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000291
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000292 // expire any remaining inactive intervals
293 for (IntervalPtrs::reverse_iterator
294 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000295 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000296 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
297 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000298
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000300}
301
Chris Lattnercbb56252004-11-18 02:42:27 +0000302/// processActiveIntervals - expire old intervals and move non-overlapping ones
303/// to the inactive list.
304void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000305{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000306 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattner23b71c12004-11-18 01:29:39 +0000307
Chris Lattnercbb56252004-11-18 02:42:27 +0000308 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
309 LiveInterval *Interval = active_[i].first;
310 LiveInterval::iterator IntervalPos = active_[i].second;
311 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000312
Chris Lattnercbb56252004-11-18 02:42:27 +0000313 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
314
315 if (IntervalPos == Interval->end()) { // Remove expired intervals.
316 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000317 assert(MRegisterInfo::isVirtualRegister(reg) &&
318 "Can only allocate virtual registers!");
319 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000320 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000321
322 // Pop off the end of the list.
323 active_[i] = active_.back();
324 active_.pop_back();
325 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000326
Chris Lattnercbb56252004-11-18 02:42:27 +0000327 } else if (IntervalPos->start > CurPoint) {
328 // Move inactive intervals to inactive list.
329 DEBUG(std::cerr << "\t\tinterval " << *Interval << " inactive\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000330 assert(MRegisterInfo::isVirtualRegister(reg) &&
331 "Can only allocate virtual registers!");
332 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000333 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000334 // add to inactive.
335 inactive_.push_back(std::make_pair(Interval, IntervalPos));
336
337 // Pop off the end of the list.
338 active_[i] = active_.back();
339 active_.pop_back();
340 --i; --e;
341 } else {
342 // Otherwise, just update the iterator position.
343 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000344 }
345 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000346}
347
Chris Lattnercbb56252004-11-18 02:42:27 +0000348/// processInactiveIntervals - expire old intervals and move overlapping
349/// ones to the active list.
350void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000351{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000352 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner365b95f2004-11-18 04:13:02 +0000353
Chris Lattnercbb56252004-11-18 02:42:27 +0000354 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
355 LiveInterval *Interval = inactive_[i].first;
356 LiveInterval::iterator IntervalPos = inactive_[i].second;
357 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000358
Chris Lattnercbb56252004-11-18 02:42:27 +0000359 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000360
Chris Lattnercbb56252004-11-18 02:42:27 +0000361 if (IntervalPos == Interval->end()) { // remove expired intervals.
362 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000363
Chris Lattnercbb56252004-11-18 02:42:27 +0000364 // Pop off the end of the list.
365 inactive_[i] = inactive_.back();
366 inactive_.pop_back();
367 --i; --e;
368 } else if (IntervalPos->start <= CurPoint) {
369 // move re-activated intervals in active list
370 DEBUG(std::cerr << "\t\tinterval " << *Interval << " active\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000371 assert(MRegisterInfo::isVirtualRegister(reg) &&
372 "Can only allocate virtual registers!");
373 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000374 prt_->addRegUse(reg);
375 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000376 active_.push_back(std::make_pair(Interval, IntervalPos));
377
378 // Pop off the end of the list.
379 inactive_[i] = inactive_.back();
380 inactive_.pop_back();
381 --i; --e;
382 } else {
383 // Otherwise, just update the iterator position.
384 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000385 }
386 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000387}
388
Chris Lattnercbb56252004-11-18 02:42:27 +0000389/// updateSpillWeights - updates the spill weights of the specifed physical
390/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000391static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000392 unsigned reg, float weight,
393 const MRegisterInfo *MRI) {
394 Weights[reg] += weight;
395 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
396 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000397}
398
Chris Lattnercbb56252004-11-18 02:42:27 +0000399static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
400 LiveInterval *LI) {
401 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
402 if (I->first == LI) return I;
403 return IP.end();
404}
405
Chris Lattner19828d42004-11-18 03:49:30 +0000406static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
407 for (unsigned i = 0, e = V.size(); i != e; ++i) {
408 RA::IntervalPtr &IP = V[i];
409 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
410 IP.second, Point);
411 if (I != IP.first->begin()) --I;
412 IP.second = I;
413 }
414}
Chris Lattnercbb56252004-11-18 02:42:27 +0000415
Chris Lattnercbb56252004-11-18 02:42:27 +0000416/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
417/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000418void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000419{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000420 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000421
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000423
Chris Lattnera6c17502005-08-22 20:20:42 +0000424 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000425 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000426 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
427 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
428
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000429 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000430 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000431 for (IntervalPtrs::const_iterator i = inactive_.begin(),
432 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000433 unsigned Reg = i->first->reg;
434 assert(MRegisterInfo::isVirtualRegister(Reg) &&
435 "Can only allocate virtual registers!");
436 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
437 // If this is not in a related reg class to the register we're allocating,
438 // don't check it.
439 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
440 cur->overlapsFrom(*i->first, i->second-1)) {
441 Reg = vrm_->getPhys(Reg);
442 prt_->addRegUse(Reg);
443 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000444 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000445 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000446
447 // Speculatively check to see if we can get a register right now. If not,
448 // we know we won't be able to by adding more constraints. If so, we can
449 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
450 // is very bad (it contains all callee clobbered registers for any functions
451 // with a call), so we want to avoid doing that if possible.
452 unsigned physReg = getFreePhysReg(cur);
453 if (physReg) {
454 // We got a register. However, if it's in the fixed_ list, we might
Chris Lattnere836ad62005-08-30 21:03:36 +0000455 // conflict with it. Check to see if we conflict with it or any of its
456 // aliases.
457 std::set<unsigned> RegAliases;
458 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
459 RegAliases.insert(*AS);
460
Chris Lattnera411cbc2005-08-22 20:59:30 +0000461 bool ConflictsWithFixed = false;
462 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Chris Lattnere836ad62005-08-30 21:03:36 +0000463 if (physReg == fixed_[i].first->reg ||
464 RegAliases.count(fixed_[i].first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000465 // Okay, this reg is on the fixed list. Check to see if we actually
466 // conflict.
467 IntervalPtr &IP = fixed_[i];
468 LiveInterval *I = IP.first;
469 if (I->endNumber() > StartPosition) {
470 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
471 IP.second = II;
472 if (II != I->begin() && II->start > StartPosition)
473 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000474 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000475 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000476 break;
477 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000478 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000479 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000480 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000481
482 // Okay, the register picked by our speculative getFreePhysReg call turned
483 // out to be in use. Actually add all of the conflicting fixed registers to
484 // prt so we can do an accurate query.
485 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000486 // For every interval in fixed we overlap with, mark the register as not
487 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000488 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
489 IntervalPtr &IP = fixed_[i];
490 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000491
492 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
493 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
494 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000495 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
496 IP.second = II;
497 if (II != I->begin() && II->start > StartPosition)
498 --II;
499 if (cur->overlapsFrom(*I, II)) {
500 unsigned reg = I->reg;
501 prt_->addRegUse(reg);
502 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
503 }
504 }
505 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000506
Chris Lattnera411cbc2005-08-22 20:59:30 +0000507 // Using the newly updated prt_ object, which includes conflicts in the
508 // future, see if there are any registers available.
509 physReg = getFreePhysReg(cur);
510 }
511 }
512
Chris Lattnera6c17502005-08-22 20:20:42 +0000513 // Restore the physical register tracker, removing information about the
514 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000515 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000516
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000517 // if we find a free register, we are done: assign this virtual to
518 // the free physical register and add this interval to the active
519 // list.
520 if (physReg) {
521 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
522 vrm_->assignVirt2Phys(cur->reg, physReg);
523 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000524 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000525 handled_.push_back(cur);
526 return;
527 }
528 DEBUG(std::cerr << "no free registers\n");
529
Chris Lattnera6c17502005-08-22 20:20:42 +0000530 // Compile the spill weights into an array that is better for scanning.
531 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
532 for (std::vector<std::pair<unsigned, float> >::iterator
533 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
534 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
535
536 // for each interval in active, update spill weights.
537 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
538 i != e; ++i) {
539 unsigned reg = i->first->reg;
540 assert(MRegisterInfo::isVirtualRegister(reg) &&
541 "Can only allocate virtual registers!");
542 reg = vrm_->getPhys(reg);
543 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
544 }
545
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
547
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000548 // Find a register to spill.
Chris Lattner5e5fb942005-01-08 19:53:50 +0000549 float minWeight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000550 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000551 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
552 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000553 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000554 if (minWeight > SpillWeights[reg]) {
555 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000556 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000557 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000558 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000559
560 // If we didn't find a register that is spillable, try aliases?
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000561 if (!minReg) {
562 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
563 e = RC->allocation_order_end(*mf_); i != e; ++i) {
564 unsigned reg = *i;
565 // No need to worry about if the alias register size < regsize of RC.
566 // We are going to spill all registers that alias it anyway.
567 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) {
568 if (minWeight > SpillWeights[*as]) {
569 minWeight = SpillWeights[*as];
570 minReg = *as;
571 }
572 }
573 }
574
575 // All registers must have inf weight. Just grab one!
576 if (!minReg)
577 minReg = *RC->allocation_order_begin(*mf_);
578 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000579
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000580 DEBUG(std::cerr << "\t\tregister with min weight: "
581 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000582
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000583 // if the current has the minimum weight, we need to spill it and
584 // add any added intervals back to unhandled, and restart
585 // linearscan.
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000586 if (cur->weight != float(HUGE_VAL) && cur->weight <= minWeight) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000587 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
588 int slot = vrm_->assignVirt2StackSlot(cur->reg);
589 std::vector<LiveInterval*> added =
590 li_->addIntervalsForSpills(*cur, *vrm_, slot);
591 if (added.empty())
592 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000593
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 // Merge added with unhandled. Note that we know that
595 // addIntervalsForSpills returns intervals sorted by their starting
596 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000597 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000598 unhandled_.push(added[i]);
599 return;
600 }
601
Chris Lattner19828d42004-11-18 03:49:30 +0000602 ++NumBacktracks;
603
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000604 // push the current interval back to unhandled since we are going
605 // to re-run at least this iteration. Since we didn't modify it it
606 // should go back right in the front of the list
607 unhandled_.push(cur);
608
609 // otherwise we spill all intervals aliasing the register with
610 // minimum weight, rollback to the interval with the earliest
611 // start point and let the linear scan algorithm run again
612 std::vector<LiveInterval*> added;
613 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
614 "did not choose a register to spill?");
615 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000616
617 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000618 toSpill[minReg] = true;
619 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
620 toSpill[*as] = true;
621
622 // the earliest start of a spilled interval indicates up to where
623 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000624 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000625
626 // set of spilled vregs (used later to rollback properly)
627 std::set<unsigned> spilled;
628
Chris Lattner19828d42004-11-18 03:49:30 +0000629 // spill live intervals of virtual regs mapped to the physical register we
630 // want to clear (and its aliases). We only spill those that overlap with the
631 // current interval as the rest do not affect its allocation. we also keep
632 // track of the earliest start of all spilled live intervals since this will
633 // mark our rollback point.
634 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000635 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000636 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000637 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000638 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000639 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
640 earliestStart = std::min(earliestStart, i->first->beginNumber());
641 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000642 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000643 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000644 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
645 spilled.insert(reg);
646 }
647 }
Chris Lattner19828d42004-11-18 03:49:30 +0000648 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000649 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000650 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000651 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000652 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000653 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
654 earliestStart = std::min(earliestStart, i->first->beginNumber());
655 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000656 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000657 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000658 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
659 spilled.insert(reg);
660 }
661 }
662
663 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnercbb56252004-11-18 02:42:27 +0000664
665 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000666 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000667 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000668 while (!handled_.empty()) {
669 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000670 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000671 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000672 break;
673 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
674 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000675
676 // When undoing a live interval allocation we must know if it is active or
677 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000678 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000679 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 active_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000681 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
682 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000683 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000684 prt_->delRegUse(vrm_->getPhys(i->reg));
685 vrm_->clearVirt(i->reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000686 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000687 inactive_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000688 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
689 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000690 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000691 vrm_->clearVirt(i->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000692 } else {
693 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
694 "Can only allocate virtual registers!");
695 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000696 unhandled_.push(i);
697 }
698 }
699
Chris Lattner19828d42004-11-18 03:49:30 +0000700 // Rewind the iterators in the active, inactive, and fixed lists back to the
701 // point we reverted to.
702 RevertVectorIteratorsTo(active_, earliestStart);
703 RevertVectorIteratorsTo(inactive_, earliestStart);
704 RevertVectorIteratorsTo(fixed_, earliestStart);
705
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000706 // scan the rest and undo each interval that expired after t and
707 // insert it in active (the next iteration of the algorithm will
708 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000709 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
710 LiveInterval *HI = handled_[i];
711 if (!HI->expiredAt(earliestStart) &&
712 HI->expiredAt(cur->beginNumber())) {
713 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
714 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerffab4222006-02-23 06:44:17 +0000715 assert(!MRegisterInfo::isPhysicalRegister(HI->reg));
716 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000717 }
718 }
719
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000720 // merge added with unhandled
721 for (unsigned i = 0, e = added.size(); i != e; ++i)
722 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000723}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000724
Chris Lattnercbb56252004-11-18 02:42:27 +0000725/// getFreePhysReg - return a free physical register for this virtual register
726/// interval if we have one, otherwise return 0.
Chris Lattnerffab4222006-02-23 06:44:17 +0000727unsigned RA::getFreePhysReg(LiveInterval *cur) {
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000728 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000729 unsigned MaxInactiveCount = 0;
730
Chris Lattnerb9805782005-08-23 22:27:31 +0000731 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
732 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
733
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000734 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
735 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000736 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000737 assert(MRegisterInfo::isVirtualRegister(reg) &&
738 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000739
740 // If this is not in a related reg class to the register we're allocating,
741 // don't check it.
742 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
743 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
744 reg = vrm_->getPhys(reg);
745 ++inactiveCounts[reg];
746 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
747 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000748 }
749
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000750 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000751
Chris Lattnerf8355d92005-08-22 16:55:22 +0000752 unsigned FreeReg = 0;
753 unsigned FreeRegInactiveCount = 0;
754
755 // Scan for the first available register.
756 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
757 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
758 for (; I != E; ++I)
759 if (prt_->isRegAvail(*I)) {
760 FreeReg = *I;
761 FreeRegInactiveCount = inactiveCounts[FreeReg];
762 break;
763 }
764
765 // If there are no free regs, or if this reg has the max inactive count,
766 // return this register.
767 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
768
769 // Continue scanning the registers, looking for the one with the highest
770 // inactive count. Alkis found that this reduced register pressure very
771 // slightly on X86 (in rev 1.94 of this file), though this should probably be
772 // reevaluated now.
773 for (; I != E; ++I) {
774 unsigned Reg = *I;
775 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
776 FreeReg = Reg;
777 FreeRegInactiveCount = inactiveCounts[Reg];
778 if (FreeRegInactiveCount == MaxInactiveCount)
779 break; // We found the one with the max inactive count.
780 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000781 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000782
783 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000784}
785
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000786FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000787 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000788}