blob: 18a3db81500d931c3a41c06b5484090bd66c1d2c [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"
21#include "llvm/CodeGen/Passes.h"
Jim Laskeyeb577ba2006-08-02 12:30:23 +000022#include "llvm/CodeGen/RegAllocRegistry.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000023#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 Lattnera4f0b3a2006-08-27 12:54:02 +000030#include "llvm/Support/Compiler.h"
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000031#include <algorithm>
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +000032#include <set>
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +000033#include <queue>
Duraid Madina30059612005-12-28 04:55:42 +000034#include <memory>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000035using namespace llvm;
36
37namespace {
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000038
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000039 static Statistic<double> efficiency
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000040 ("regalloc", "Ratio of intervals processed over total intervals");
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000041 static Statistic<> NumBacktracks
42 ("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000043
Jim Laskey13ec7022006-08-01 14:21:23 +000044 static RegisterRegAlloc
45 linearscanRegAlloc("linearscan", " linear scan register allocator",
46 createLinearScanRegisterAllocator);
47
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000048 static unsigned numIterations = 0;
49 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000050
Chris Lattnerf8c68f62006-06-28 22:17:39 +000051 struct VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattnercbb56252004-11-18 02:42:27 +000052 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
53 typedef std::vector<IntervalPtr> IntervalPtrs;
54 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000055 /// RelatedRegClasses - This structure is built the first time a function is
56 /// compiled, and keeps track of which register classes have registers that
57 /// belong to multiple classes or have aliases that are in other classes.
58 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
59 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
60
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000061 MachineFunction* mf_;
62 const TargetMachine* tm_;
63 const MRegisterInfo* mri_;
64 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000065 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000066
67 /// handled_ - Intervals are added to the handled_ set in the order of their
68 /// start value. This is uses for backtracking.
69 std::vector<LiveInterval*> handled_;
70
71 /// fixed_ - Intervals that correspond to machine registers.
72 ///
73 IntervalPtrs fixed_;
74
75 /// active_ - Intervals that are currently being processed, and which have a
76 /// live range active for the current point.
77 IntervalPtrs active_;
78
79 /// inactive_ - Intervals that are currently being processed, but which have
80 /// a hold at the current point.
81 IntervalPtrs inactive_;
82
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000084 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000085 greater_ptr<LiveInterval> > IntervalHeap;
86 IntervalHeap unhandled_;
87 std::auto_ptr<PhysRegTracker> prt_;
88 std::auto_ptr<VirtRegMap> vrm_;
89 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000090
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000091 public:
92 virtual const char* getPassName() const {
93 return "Linear Scan Register Allocator";
94 }
95
96 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000097 AU.addRequired<LiveIntervals>();
98 MachineFunctionPass::getAnalysisUsage(AU);
99 }
100
101 /// runOnMachineFunction - register allocate the whole function
102 bool runOnMachineFunction(MachineFunction&);
103
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000104 private:
105 /// linearScan - the linear scan algorithm
106 void linearScan();
107
Chris Lattnercbb56252004-11-18 02:42:27 +0000108 /// initIntervalSets - initialize the interval sets.
109 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000110 void initIntervalSets();
111
Chris Lattnercbb56252004-11-18 02:42:27 +0000112 /// processActiveIntervals - expire old intervals and move non-overlapping
113 /// ones to the inactive list.
114 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115
Chris Lattnercbb56252004-11-18 02:42:27 +0000116 /// processInactiveIntervals - expire old intervals and move overlapping
117 /// ones to the active list.
118 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000119
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120 /// assignRegOrStackSlotAtInterval - assign a register if one
121 /// is available, or spill.
122 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
123
124 ///
125 /// register handling helpers
126 ///
127
Chris Lattnercbb56252004-11-18 02:42:27 +0000128 /// getFreePhysReg - return a free physical register for this virtual
129 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000130 unsigned getFreePhysReg(LiveInterval* cur);
131
132 /// assignVirt2StackSlot - assigns this virtual register to a
133 /// stack slot. returns the stack slot
134 int assignVirt2StackSlot(unsigned virtReg);
135
Chris Lattnerb9805782005-08-23 22:27:31 +0000136 void ComputeRelatedRegClasses();
137
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000138 template <typename ItTy>
139 void printIntervals(const char* const str, ItTy i, ItTy e) const {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000140 if (str) DOUT << str << " intervals:\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000141 for (; i != e; ++i) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000142 DOUT << "\t" << *i->first << " -> ";
Chris Lattnercbb56252004-11-18 02:42:27 +0000143 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000144 if (MRegisterInfo::isVirtualRegister(reg)) {
145 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000146 }
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000147 DOUT << mri_->getName(reg) << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000148 }
149 }
150 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000151}
152
Chris Lattnerb9805782005-08-23 22:27:31 +0000153void RA::ComputeRelatedRegClasses() {
154 const MRegisterInfo &MRI = *mri_;
155
156 // First pass, add all reg classes to the union, and determine at least one
157 // reg class that each register is in.
158 bool HasAliases = false;
159 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
160 E = MRI.regclass_end(); RCI != E; ++RCI) {
161 RelatedRegClasses.insert(*RCI);
162 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
163 I != E; ++I) {
164 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
165
166 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
167 if (PRC) {
168 // Already processed this register. Just make sure we know that
169 // multiple register classes share a register.
170 RelatedRegClasses.unionSets(PRC, *RCI);
171 } else {
172 PRC = *RCI;
173 }
174 }
175 }
176
177 // Second pass, now that we know conservatively what register classes each reg
178 // belongs to, add info about aliases. We don't need to do this for targets
179 // without register aliases.
180 if (HasAliases)
181 for (std::map<unsigned, const TargetRegisterClass*>::iterator
182 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
183 I != E; ++I)
184 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
185 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
186}
187
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000188bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000189 mf_ = &fn;
190 tm_ = &fn.getTarget();
191 mri_ = tm_->getRegisterInfo();
192 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000193
Chris Lattnerb9805782005-08-23 22:27:31 +0000194 // If this is the first function compiled, compute the related reg classes.
195 if (RelatedRegClasses.empty())
196 ComputeRelatedRegClasses();
197
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000198 PhysRegsUsed = new bool[mri_->getNumRegs()];
199 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
200 fn.setUsedPhysRegs(PhysRegsUsed);
201
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000202 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
203 vrm_.reset(new VirtRegMap(*mf_));
204 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000205
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000206 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000207
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000208 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000209
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000210 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000211 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000212
Chris Lattner510a3ea2004-09-30 02:02:33 +0000213 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000214
215
216 while (!unhandled_.empty()) unhandled_.pop();
217 fixed_.clear();
218 active_.clear();
219 inactive_.clear();
220 handled_.clear();
221
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000222 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000223}
224
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000225/// initIntervalSets - initialize the interval sets.
226///
227void RA::initIntervalSets()
228{
229 assert(unhandled_.empty() && fixed_.empty() &&
230 active_.empty() && inactive_.empty() &&
231 "interval sets should be empty on initialization");
232
233 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000234 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
235 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000236 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000237 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000238 unhandled_.push(&i->second);
239 }
240}
241
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000242void RA::linearScan()
243{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000244 // linear scan algorithm
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000245 DOUT << "********** LINEAR SCAN **********\n";
246 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000247
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000248 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
249 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
250 DEBUG(printIntervals("active", active_.begin(), active_.end()));
251 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
252
253 while (!unhandled_.empty()) {
254 // pick the interval with the earliest start point
255 LiveInterval* cur = unhandled_.top();
256 unhandled_.pop();
257 ++numIterations;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000258 DOUT << "\n*** CURRENT ***: " << *cur << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000259
Chris Lattnercbb56252004-11-18 02:42:27 +0000260 processActiveIntervals(cur->beginNumber());
261 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000262
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000263 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
264 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000265
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000266 // Allocating a virtual register. try to find a free
267 // physical register or spill an interval (possibly this one) in order to
268 // assign it one.
269 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000270
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000271 DEBUG(printIntervals("active", active_.begin(), active_.end()));
272 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000273 }
274 numIntervals += li_->getNumIntervals();
275 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000276
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000277 // expire any remaining active intervals
278 for (IntervalPtrs::reverse_iterator
279 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000280 unsigned reg = i->first->reg;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000281 DOUT << "\tinterval " << *i->first << " expired\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000282 assert(MRegisterInfo::isVirtualRegister(reg) &&
283 "Can only allocate virtual registers!");
284 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000285 prt_->delRegUse(reg);
286 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
287 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000288
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000289 // expire any remaining inactive intervals
290 for (IntervalPtrs::reverse_iterator
291 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000292 DOUT << "\tinterval " << *i->first << " expired\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000293 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
294 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000295
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000296 DOUT << *vrm_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000297}
298
Chris Lattnercbb56252004-11-18 02:42:27 +0000299/// processActiveIntervals - expire old intervals and move non-overlapping ones
300/// to the inactive list.
301void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000302{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000303 DOUT << "\tprocessing active intervals:\n";
Chris Lattner23b71c12004-11-18 01:29:39 +0000304
Chris Lattnercbb56252004-11-18 02:42:27 +0000305 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
306 LiveInterval *Interval = active_[i].first;
307 LiveInterval::iterator IntervalPos = active_[i].second;
308 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000309
Chris Lattnercbb56252004-11-18 02:42:27 +0000310 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
311
312 if (IntervalPos == Interval->end()) { // Remove expired intervals.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000313 DOUT << "\t\tinterval " << *Interval << " expired\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000314 assert(MRegisterInfo::isVirtualRegister(reg) &&
315 "Can only allocate virtual registers!");
316 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000317 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000318
319 // Pop off the end of the list.
320 active_[i] = active_.back();
321 active_.pop_back();
322 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000323
Chris Lattnercbb56252004-11-18 02:42:27 +0000324 } else if (IntervalPos->start > CurPoint) {
325 // Move inactive intervals to inactive list.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000326 DOUT << "\t\tinterval " << *Interval << " inactive\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000327 assert(MRegisterInfo::isVirtualRegister(reg) &&
328 "Can only allocate virtual registers!");
329 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000330 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000331 // add to inactive.
332 inactive_.push_back(std::make_pair(Interval, IntervalPos));
333
334 // Pop off the end of the list.
335 active_[i] = active_.back();
336 active_.pop_back();
337 --i; --e;
338 } else {
339 // Otherwise, just update the iterator position.
340 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000341 }
342 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000343}
344
Chris Lattnercbb56252004-11-18 02:42:27 +0000345/// processInactiveIntervals - expire old intervals and move overlapping
346/// ones to the active list.
347void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000348{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000349 DOUT << "\tprocessing inactive intervals:\n";
Chris Lattner365b95f2004-11-18 04:13:02 +0000350
Chris Lattnercbb56252004-11-18 02:42:27 +0000351 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
352 LiveInterval *Interval = inactive_[i].first;
353 LiveInterval::iterator IntervalPos = inactive_[i].second;
354 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000355
Chris Lattnercbb56252004-11-18 02:42:27 +0000356 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000357
Chris Lattnercbb56252004-11-18 02:42:27 +0000358 if (IntervalPos == Interval->end()) { // remove expired intervals.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000359 DOUT << "\t\tinterval " << *Interval << " expired\n";
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000360
Chris Lattnercbb56252004-11-18 02:42:27 +0000361 // Pop off the end of the list.
362 inactive_[i] = inactive_.back();
363 inactive_.pop_back();
364 --i; --e;
365 } else if (IntervalPos->start <= CurPoint) {
366 // move re-activated intervals in active list
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000367 DOUT << "\t\tinterval " << *Interval << " active\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000368 assert(MRegisterInfo::isVirtualRegister(reg) &&
369 "Can only allocate virtual registers!");
370 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000371 prt_->addRegUse(reg);
372 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000373 active_.push_back(std::make_pair(Interval, IntervalPos));
374
375 // Pop off the end of the list.
376 inactive_[i] = inactive_.back();
377 inactive_.pop_back();
378 --i; --e;
379 } else {
380 // Otherwise, just update the iterator position.
381 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000382 }
383 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000384}
385
Chris Lattnercbb56252004-11-18 02:42:27 +0000386/// updateSpillWeights - updates the spill weights of the specifed physical
387/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000388static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000389 unsigned reg, float weight,
390 const MRegisterInfo *MRI) {
391 Weights[reg] += weight;
392 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
393 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000394}
395
Chris Lattnercbb56252004-11-18 02:42:27 +0000396static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
397 LiveInterval *LI) {
398 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
399 if (I->first == LI) return I;
400 return IP.end();
401}
402
Chris Lattner19828d42004-11-18 03:49:30 +0000403static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
404 for (unsigned i = 0, e = V.size(); i != e; ++i) {
405 RA::IntervalPtr &IP = V[i];
406 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
407 IP.second, Point);
408 if (I != IP.first->begin()) --I;
409 IP.second = I;
410 }
411}
Chris Lattnercbb56252004-11-18 02:42:27 +0000412
Chris Lattnercbb56252004-11-18 02:42:27 +0000413/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
414/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000415void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000416{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000417 DOUT << "\tallocating current interval: ";
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000418
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000419 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000420
Chris Lattnera6c17502005-08-22 20:20:42 +0000421 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000422 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000423 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
424 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
425
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000427 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000428 for (IntervalPtrs::const_iterator i = inactive_.begin(),
429 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000430 unsigned Reg = i->first->reg;
431 assert(MRegisterInfo::isVirtualRegister(Reg) &&
432 "Can only allocate virtual registers!");
433 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
434 // If this is not in a related reg class to the register we're allocating,
435 // don't check it.
436 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
437 cur->overlapsFrom(*i->first, i->second-1)) {
438 Reg = vrm_->getPhys(Reg);
439 prt_->addRegUse(Reg);
440 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000441 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000442 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000443
444 // Speculatively check to see if we can get a register right now. If not,
445 // we know we won't be able to by adding more constraints. If so, we can
446 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
447 // is very bad (it contains all callee clobbered registers for any functions
448 // with a call), so we want to avoid doing that if possible.
449 unsigned physReg = getFreePhysReg(cur);
450 if (physReg) {
451 // We got a register. However, if it's in the fixed_ list, we might
Chris Lattnere836ad62005-08-30 21:03:36 +0000452 // conflict with it. Check to see if we conflict with it or any of its
453 // aliases.
454 std::set<unsigned> RegAliases;
455 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
456 RegAliases.insert(*AS);
457
Chris Lattnera411cbc2005-08-22 20:59:30 +0000458 bool ConflictsWithFixed = false;
459 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Jim Laskeye719d9f2006-10-24 14:35:25 +0000460 IntervalPtr &IP = fixed_[i];
461 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000462 // Okay, this reg is on the fixed list. Check to see if we actually
463 // conflict.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000464 LiveInterval *I = IP.first;
465 if (I->endNumber() > StartPosition) {
466 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
467 IP.second = II;
468 if (II != I->begin() && II->start > StartPosition)
469 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000470 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000471 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000472 break;
473 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000474 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000475 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000476 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000477
478 // Okay, the register picked by our speculative getFreePhysReg call turned
479 // out to be in use. Actually add all of the conflicting fixed registers to
480 // prt so we can do an accurate query.
481 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000482 // For every interval in fixed we overlap with, mark the register as not
483 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000484 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
485 IntervalPtr &IP = fixed_[i];
486 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000487
488 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
489 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
490 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000491 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
492 IP.second = II;
493 if (II != I->begin() && II->start > StartPosition)
494 --II;
495 if (cur->overlapsFrom(*I, II)) {
496 unsigned reg = I->reg;
497 prt_->addRegUse(reg);
498 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
499 }
500 }
501 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000502
Chris Lattnera411cbc2005-08-22 20:59:30 +0000503 // Using the newly updated prt_ object, which includes conflicts in the
504 // future, see if there are any registers available.
505 physReg = getFreePhysReg(cur);
506 }
507 }
508
Chris Lattnera6c17502005-08-22 20:20:42 +0000509 // Restore the physical register tracker, removing information about the
510 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000511 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000512
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000513 // if we find a free register, we are done: assign this virtual to
514 // the free physical register and add this interval to the active
515 // list.
516 if (physReg) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000517 DOUT << mri_->getName(physReg) << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000518 vrm_->assignVirt2Phys(cur->reg, physReg);
519 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000520 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000521 handled_.push_back(cur);
522 return;
523 }
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000524 DOUT << "no free registers\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000525
Chris Lattnera6c17502005-08-22 20:20:42 +0000526 // Compile the spill weights into an array that is better for scanning.
527 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
528 for (std::vector<std::pair<unsigned, float> >::iterator
529 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
530 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
531
532 // for each interval in active, update spill weights.
533 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
534 i != e; ++i) {
535 unsigned reg = i->first->reg;
536 assert(MRegisterInfo::isVirtualRegister(reg) &&
537 "Can only allocate virtual registers!");
538 reg = vrm_->getPhys(reg);
539 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
540 }
541
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000542 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000544 // Find a register to spill.
Jim Laskey7902c752006-11-07 12:25:45 +0000545 float minWeight = HUGE_VALF;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000547 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
548 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000549 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000550 if (minWeight > SpillWeights[reg]) {
551 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000552 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000553 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000554 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000555
556 // If we didn't find a register that is spillable, try aliases?
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000557 if (!minReg) {
558 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
559 e = RC->allocation_order_end(*mf_); i != e; ++i) {
560 unsigned reg = *i;
561 // No need to worry about if the alias register size < regsize of RC.
562 // We are going to spill all registers that alias it anyway.
563 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) {
564 if (minWeight > SpillWeights[*as]) {
565 minWeight = SpillWeights[*as];
566 minReg = *as;
567 }
568 }
569 }
570
571 // All registers must have inf weight. Just grab one!
572 if (!minReg)
573 minReg = *RC->allocation_order_begin(*mf_);
574 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000575
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000576 DOUT << "\t\tregister with min weight: "
577 << mri_->getName(minReg) << " (" << minWeight << ")\n";
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000578
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000579 // if the current has the minimum weight, we need to spill it and
580 // add any added intervals back to unhandled, and restart
581 // linearscan.
Jim Laskey7902c752006-11-07 12:25:45 +0000582 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000583 DOUT << "\t\t\tspilling(c): " << *cur << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000584 int slot = vrm_->assignVirt2StackSlot(cur->reg);
585 std::vector<LiveInterval*> added =
586 li_->addIntervalsForSpills(*cur, *vrm_, slot);
587 if (added.empty())
588 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000589
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000590 // Merge added with unhandled. Note that we know that
591 // addIntervalsForSpills returns intervals sorted by their starting
592 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000593 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000594 unhandled_.push(added[i]);
595 return;
596 }
597
Chris Lattner19828d42004-11-18 03:49:30 +0000598 ++NumBacktracks;
599
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000600 // push the current interval back to unhandled since we are going
601 // to re-run at least this iteration. Since we didn't modify it it
602 // should go back right in the front of the list
603 unhandled_.push(cur);
604
605 // otherwise we spill all intervals aliasing the register with
606 // minimum weight, rollback to the interval with the earliest
607 // start point and let the linear scan algorithm run again
608 std::vector<LiveInterval*> added;
609 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
610 "did not choose a register to spill?");
611 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000612
613 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000614 toSpill[minReg] = true;
615 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
616 toSpill[*as] = true;
617
618 // the earliest start of a spilled interval indicates up to where
619 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000620 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000621
622 // set of spilled vregs (used later to rollback properly)
623 std::set<unsigned> spilled;
624
Chris Lattner19828d42004-11-18 03:49:30 +0000625 // spill live intervals of virtual regs mapped to the physical register we
626 // want to clear (and its aliases). We only spill those that overlap with the
627 // current interval as the rest do not affect its allocation. we also keep
628 // track of the earliest start of all spilled live intervals since this will
629 // mark our rollback point.
630 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000631 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000632 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000633 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000634 cur->overlapsFrom(*i->first, i->second)) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000635 DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000636 earliestStart = std::min(earliestStart, i->first->beginNumber());
637 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000638 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000639 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000640 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
641 spilled.insert(reg);
642 }
643 }
Chris Lattner19828d42004-11-18 03:49:30 +0000644 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000645 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000646 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000647 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000648 cur->overlapsFrom(*i->first, i->second-1)) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000649 DOUT << "\t\t\tspilling(i): " << *i->first << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000650 earliestStart = std::min(earliestStart, i->first->beginNumber());
651 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000652 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000653 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000654 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
655 spilled.insert(reg);
656 }
657 }
658
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000659 DOUT << "\t\trolling back to: " << earliestStart << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000660
661 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000662 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000663 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000664 while (!handled_.empty()) {
665 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000666 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000667 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000668 break;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000669 DOUT << "\t\t\tundo changes for: " << *i << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000670 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000671
672 // When undoing a live interval allocation we must know if it is active or
673 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000674 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000675 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000676 active_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000677 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
678 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000679 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000680 prt_->delRegUse(vrm_->getPhys(i->reg));
681 vrm_->clearVirt(i->reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000682 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000683 inactive_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000684 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
685 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000686 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000687 vrm_->clearVirt(i->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000688 } else {
689 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
690 "Can only allocate virtual registers!");
691 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000692 unhandled_.push(i);
693 }
694 }
695
Chris Lattner19828d42004-11-18 03:49:30 +0000696 // Rewind the iterators in the active, inactive, and fixed lists back to the
697 // point we reverted to.
698 RevertVectorIteratorsTo(active_, earliestStart);
699 RevertVectorIteratorsTo(inactive_, earliestStart);
700 RevertVectorIteratorsTo(fixed_, earliestStart);
701
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000702 // scan the rest and undo each interval that expired after t and
703 // insert it in active (the next iteration of the algorithm will
704 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000705 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
706 LiveInterval *HI = handled_[i];
707 if (!HI->expiredAt(earliestStart) &&
708 HI->expiredAt(cur->beginNumber())) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000709 DOUT << "\t\t\tundo changes for: " << *HI << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000710 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerffab4222006-02-23 06:44:17 +0000711 assert(!MRegisterInfo::isPhysicalRegister(HI->reg));
712 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000713 }
714 }
715
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000716 // merge added with unhandled
717 for (unsigned i = 0, e = added.size(); i != e; ++i)
718 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000719}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000720
Chris Lattnercbb56252004-11-18 02:42:27 +0000721/// getFreePhysReg - return a free physical register for this virtual register
722/// interval if we have one, otherwise return 0.
Chris Lattnerffab4222006-02-23 06:44:17 +0000723unsigned RA::getFreePhysReg(LiveInterval *cur) {
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000724 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000725 unsigned MaxInactiveCount = 0;
726
Chris Lattnerb9805782005-08-23 22:27:31 +0000727 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
728 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
729
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000730 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
731 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000732 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000733 assert(MRegisterInfo::isVirtualRegister(reg) &&
734 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000735
736 // If this is not in a related reg class to the register we're allocating,
737 // don't check it.
738 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
739 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
740 reg = vrm_->getPhys(reg);
741 ++inactiveCounts[reg];
742 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
743 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000744 }
745
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000746 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000747
Chris Lattnerf8355d92005-08-22 16:55:22 +0000748 unsigned FreeReg = 0;
749 unsigned FreeRegInactiveCount = 0;
750
751 // Scan for the first available register.
752 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
753 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
754 for (; I != E; ++I)
755 if (prt_->isRegAvail(*I)) {
756 FreeReg = *I;
757 FreeRegInactiveCount = inactiveCounts[FreeReg];
758 break;
759 }
760
761 // If there are no free regs, or if this reg has the max inactive count,
762 // return this register.
763 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
764
765 // Continue scanning the registers, looking for the one with the highest
766 // inactive count. Alkis found that this reduced register pressure very
767 // slightly on X86 (in rev 1.94 of this file), though this should probably be
768 // reevaluated now.
769 for (; I != E; ++I) {
770 unsigned Reg = *I;
771 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
772 FreeReg = Reg;
773 FreeRegInactiveCount = inactiveCounts[Reg];
774 if (FreeRegInactiveCount == MaxInactiveCount)
775 break; // We found the one with the max inactive count.
776 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000777 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000778
779 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000780}
781
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000782FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000783 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000784}