blob: 170febcc50be7183d28fc088695791bb7459fc7d [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>
Jeff Cohen97af7512006-12-02 02:22:01 +000035#include <cmath>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000036using namespace llvm;
37
38namespace {
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000039
Chris Lattner4c7e2272006-12-06 01:48:55 +000040 static Statistic<> NumIters
41 ("regalloc", "Number of iterations performed");
Andrew Lenharthed41f1b2006-07-20 17:28:38 +000042 static Statistic<> NumBacktracks
43 ("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000044
Jim Laskey13ec7022006-08-01 14:21:23 +000045 static RegisterRegAlloc
46 linearscanRegAlloc("linearscan", " linear scan register allocator",
47 createLinearScanRegisterAllocator);
48
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000049 static unsigned numIterations = 0;
50 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000051
Chris Lattnerf8c68f62006-06-28 22:17:39 +000052 struct VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattnercbb56252004-11-18 02:42:27 +000053 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
54 typedef std::vector<IntervalPtr> IntervalPtrs;
55 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000056 /// RelatedRegClasses - This structure is built the first time a function is
57 /// compiled, and keeps track of which register classes have registers that
58 /// belong to multiple classes or have aliases that are in other classes.
59 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
60 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
61
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000062 MachineFunction* mf_;
63 const TargetMachine* tm_;
64 const MRegisterInfo* mri_;
65 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000066 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000067
68 /// handled_ - Intervals are added to the handled_ set in the order of their
69 /// start value. This is uses for backtracking.
70 std::vector<LiveInterval*> handled_;
71
72 /// fixed_ - Intervals that correspond to machine registers.
73 ///
74 IntervalPtrs fixed_;
75
76 /// active_ - Intervals that are currently being processed, and which have a
77 /// live range active for the current point.
78 IntervalPtrs active_;
79
80 /// inactive_ - Intervals that are currently being processed, but which have
81 /// a hold at the current point.
82 IntervalPtrs inactive_;
83
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000084 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000085 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 greater_ptr<LiveInterval> > IntervalHeap;
87 IntervalHeap unhandled_;
88 std::auto_ptr<PhysRegTracker> prt_;
89 std::auto_ptr<VirtRegMap> vrm_;
90 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000091
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000092 public:
93 virtual const char* getPassName() const {
94 return "Linear Scan Register Allocator";
95 }
96
97 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000098 AU.addRequired<LiveIntervals>();
99 MachineFunctionPass::getAnalysisUsage(AU);
100 }
101
102 /// runOnMachineFunction - register allocate the whole function
103 bool runOnMachineFunction(MachineFunction&);
104
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 private:
106 /// linearScan - the linear scan algorithm
107 void linearScan();
108
Chris Lattnercbb56252004-11-18 02:42:27 +0000109 /// initIntervalSets - initialize the interval sets.
110 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000111 void initIntervalSets();
112
Chris Lattnercbb56252004-11-18 02:42:27 +0000113 /// processActiveIntervals - expire old intervals and move non-overlapping
114 /// ones to the inactive list.
115 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000116
Chris Lattnercbb56252004-11-18 02:42:27 +0000117 /// processInactiveIntervals - expire old intervals and move overlapping
118 /// ones to the active list.
119 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000120
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000121 /// assignRegOrStackSlotAtInterval - assign a register if one
122 /// is available, or spill.
123 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
124
125 ///
126 /// register handling helpers
127 ///
128
Chris Lattnercbb56252004-11-18 02:42:27 +0000129 /// getFreePhysReg - return a free physical register for this virtual
130 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 unsigned getFreePhysReg(LiveInterval* cur);
132
133 /// assignVirt2StackSlot - assigns this virtual register to a
134 /// stack slot. returns the stack slot
135 int assignVirt2StackSlot(unsigned virtReg);
136
Chris Lattnerb9805782005-08-23 22:27:31 +0000137 void ComputeRelatedRegClasses();
138
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000139 template <typename ItTy>
140 void printIntervals(const char* const str, ItTy i, ItTy e) const {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000141 if (str) DOUT << str << " intervals:\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000142 for (; i != e; ++i) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000143 DOUT << "\t" << *i->first << " -> ";
Chris Lattnercbb56252004-11-18 02:42:27 +0000144 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000145 if (MRegisterInfo::isVirtualRegister(reg)) {
146 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000147 }
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000148 DOUT << mri_->getName(reg) << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000149 }
150 }
151 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000152}
153
Chris Lattnerb9805782005-08-23 22:27:31 +0000154void RA::ComputeRelatedRegClasses() {
155 const MRegisterInfo &MRI = *mri_;
156
157 // First pass, add all reg classes to the union, and determine at least one
158 // reg class that each register is in.
159 bool HasAliases = false;
160 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
161 E = MRI.regclass_end(); RCI != E; ++RCI) {
162 RelatedRegClasses.insert(*RCI);
163 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
164 I != E; ++I) {
165 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
166
167 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
168 if (PRC) {
169 // Already processed this register. Just make sure we know that
170 // multiple register classes share a register.
171 RelatedRegClasses.unionSets(PRC, *RCI);
172 } else {
173 PRC = *RCI;
174 }
175 }
176 }
177
178 // Second pass, now that we know conservatively what register classes each reg
179 // belongs to, add info about aliases. We don't need to do this for targets
180 // without register aliases.
181 if (HasAliases)
182 for (std::map<unsigned, const TargetRegisterClass*>::iterator
183 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
184 I != E; ++I)
185 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
186 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
187}
188
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000189bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000190 mf_ = &fn;
191 tm_ = &fn.getTarget();
192 mri_ = tm_->getRegisterInfo();
193 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000194
Chris Lattnerb9805782005-08-23 22:27:31 +0000195 // If this is the first function compiled, compute the related reg classes.
196 if (RelatedRegClasses.empty())
197 ComputeRelatedRegClasses();
198
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000199 PhysRegsUsed = new bool[mri_->getNumRegs()];
200 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
201 fn.setUsedPhysRegs(PhysRegsUsed);
202
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000203 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
204 vrm_.reset(new VirtRegMap(*mf_));
205 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000206
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000207 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000208
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000209 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000210
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000211 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000212 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000213
Chris Lattner510a3ea2004-09-30 02:02:33 +0000214 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000215
216
217 while (!unhandled_.empty()) unhandled_.pop();
218 fixed_.clear();
219 active_.clear();
220 inactive_.clear();
221 handled_.clear();
222
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000223 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000224}
225
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000226/// initIntervalSets - initialize the interval sets.
227///
228void RA::initIntervalSets()
229{
230 assert(unhandled_.empty() && fixed_.empty() &&
231 active_.empty() && inactive_.empty() &&
232 "interval sets should be empty on initialization");
233
234 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000235 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
236 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000237 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000238 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000239 unhandled_.push(&i->second);
240 }
241}
242
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000243void RA::linearScan()
244{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000245 // linear scan algorithm
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000246 DOUT << "********** LINEAR SCAN **********\n";
247 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000248
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000249 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
250 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
251 DEBUG(printIntervals("active", active_.begin(), active_.end()));
252 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
253
254 while (!unhandled_.empty()) {
255 // pick the interval with the earliest start point
256 LiveInterval* cur = unhandled_.top();
257 unhandled_.pop();
258 ++numIterations;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000259 DOUT << "\n*** CURRENT ***: " << *cur << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000260
Chris Lattnercbb56252004-11-18 02:42:27 +0000261 processActiveIntervals(cur->beginNumber());
262 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000263
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000264 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
265 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000266
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000267 // Allocating a virtual register. try to find a free
268 // physical register or spill an interval (possibly this one) in order to
269 // assign it one.
270 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000271
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000272 DEBUG(printIntervals("active", active_.begin(), active_.end()));
273 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000274 }
275 numIntervals += li_->getNumIntervals();
Chris Lattner4c7e2272006-12-06 01:48:55 +0000276 NumIters += numIterations;
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000277
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000278 // expire any remaining active intervals
279 for (IntervalPtrs::reverse_iterator
280 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000281 unsigned reg = i->first->reg;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000282 DOUT << "\tinterval " << *i->first << " expired\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000283 assert(MRegisterInfo::isVirtualRegister(reg) &&
284 "Can only allocate virtual registers!");
285 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000286 prt_->delRegUse(reg);
287 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
288 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000289
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000290 // expire any remaining inactive intervals
291 for (IntervalPtrs::reverse_iterator
292 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000293 DOUT << "\tinterval " << *i->first << " expired\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000294 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
295 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000296
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000297 DOUT << *vrm_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000298}
299
Chris Lattnercbb56252004-11-18 02:42:27 +0000300/// processActiveIntervals - expire old intervals and move non-overlapping ones
301/// to the inactive list.
302void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000303{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000304 DOUT << "\tprocessing active intervals:\n";
Chris Lattner23b71c12004-11-18 01:29:39 +0000305
Chris Lattnercbb56252004-11-18 02:42:27 +0000306 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
307 LiveInterval *Interval = active_[i].first;
308 LiveInterval::iterator IntervalPos = active_[i].second;
309 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000310
Chris Lattnercbb56252004-11-18 02:42:27 +0000311 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
312
313 if (IntervalPos == Interval->end()) { // Remove expired intervals.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000314 DOUT << "\t\tinterval " << *Interval << " expired\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000315 assert(MRegisterInfo::isVirtualRegister(reg) &&
316 "Can only allocate virtual registers!");
317 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000318 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000319
320 // Pop off the end of the list.
321 active_[i] = active_.back();
322 active_.pop_back();
323 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000324
Chris Lattnercbb56252004-11-18 02:42:27 +0000325 } else if (IntervalPos->start > CurPoint) {
326 // Move inactive intervals to inactive list.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000327 DOUT << "\t\tinterval " << *Interval << " inactive\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000328 assert(MRegisterInfo::isVirtualRegister(reg) &&
329 "Can only allocate virtual registers!");
330 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000331 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000332 // add to inactive.
333 inactive_.push_back(std::make_pair(Interval, IntervalPos));
334
335 // Pop off the end of the list.
336 active_[i] = active_.back();
337 active_.pop_back();
338 --i; --e;
339 } else {
340 // Otherwise, just update the iterator position.
341 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000342 }
343 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000344}
345
Chris Lattnercbb56252004-11-18 02:42:27 +0000346/// processInactiveIntervals - expire old intervals and move overlapping
347/// ones to the active list.
348void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000349{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000350 DOUT << "\tprocessing inactive intervals:\n";
Chris Lattner365b95f2004-11-18 04:13:02 +0000351
Chris Lattnercbb56252004-11-18 02:42:27 +0000352 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
353 LiveInterval *Interval = inactive_[i].first;
354 LiveInterval::iterator IntervalPos = inactive_[i].second;
355 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000356
Chris Lattnercbb56252004-11-18 02:42:27 +0000357 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000358
Chris Lattnercbb56252004-11-18 02:42:27 +0000359 if (IntervalPos == Interval->end()) { // remove expired intervals.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000360 DOUT << "\t\tinterval " << *Interval << " expired\n";
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000361
Chris Lattnercbb56252004-11-18 02:42:27 +0000362 // Pop off the end of the list.
363 inactive_[i] = inactive_.back();
364 inactive_.pop_back();
365 --i; --e;
366 } else if (IntervalPos->start <= CurPoint) {
367 // move re-activated intervals in active list
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000368 DOUT << "\t\tinterval " << *Interval << " active\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000369 assert(MRegisterInfo::isVirtualRegister(reg) &&
370 "Can only allocate virtual registers!");
371 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000372 prt_->addRegUse(reg);
373 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000374 active_.push_back(std::make_pair(Interval, IntervalPos));
375
376 // Pop off the end of the list.
377 inactive_[i] = inactive_.back();
378 inactive_.pop_back();
379 --i; --e;
380 } else {
381 // Otherwise, just update the iterator position.
382 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000383 }
384 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000385}
386
Chris Lattnercbb56252004-11-18 02:42:27 +0000387/// updateSpillWeights - updates the spill weights of the specifed physical
388/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000389static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000390 unsigned reg, float weight,
391 const MRegisterInfo *MRI) {
392 Weights[reg] += weight;
393 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
394 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000395}
396
Chris Lattnercbb56252004-11-18 02:42:27 +0000397static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
398 LiveInterval *LI) {
399 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
400 if (I->first == LI) return I;
401 return IP.end();
402}
403
Chris Lattner19828d42004-11-18 03:49:30 +0000404static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
405 for (unsigned i = 0, e = V.size(); i != e; ++i) {
406 RA::IntervalPtr &IP = V[i];
407 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
408 IP.second, Point);
409 if (I != IP.first->begin()) --I;
410 IP.second = I;
411 }
412}
Chris Lattnercbb56252004-11-18 02:42:27 +0000413
Chris Lattnercbb56252004-11-18 02:42:27 +0000414/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
415/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000416void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000417{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000418 DOUT << "\tallocating current interval: ";
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000419
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000420 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000421
Chris Lattnera6c17502005-08-22 20:20:42 +0000422 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000423 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000424 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
425 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
426
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000427 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000428 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000429 for (IntervalPtrs::const_iterator i = inactive_.begin(),
430 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000431 unsigned Reg = i->first->reg;
432 assert(MRegisterInfo::isVirtualRegister(Reg) &&
433 "Can only allocate virtual registers!");
434 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
435 // If this is not in a related reg class to the register we're allocating,
436 // don't check it.
437 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
438 cur->overlapsFrom(*i->first, i->second-1)) {
439 Reg = vrm_->getPhys(Reg);
440 prt_->addRegUse(Reg);
441 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000442 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000443 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000444
445 // Speculatively check to see if we can get a register right now. If not,
446 // we know we won't be able to by adding more constraints. If so, we can
447 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
448 // is very bad (it contains all callee clobbered registers for any functions
449 // with a call), so we want to avoid doing that if possible.
450 unsigned physReg = getFreePhysReg(cur);
451 if (physReg) {
452 // We got a register. However, if it's in the fixed_ list, we might
Chris Lattnere836ad62005-08-30 21:03:36 +0000453 // conflict with it. Check to see if we conflict with it or any of its
454 // aliases.
455 std::set<unsigned> RegAliases;
456 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
457 RegAliases.insert(*AS);
458
Chris Lattnera411cbc2005-08-22 20:59:30 +0000459 bool ConflictsWithFixed = false;
460 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Jim Laskeye719d9f2006-10-24 14:35:25 +0000461 IntervalPtr &IP = fixed_[i];
462 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000463 // Okay, this reg is on the fixed list. Check to see if we actually
464 // conflict.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000465 LiveInterval *I = IP.first;
466 if (I->endNumber() > StartPosition) {
467 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
468 IP.second = II;
469 if (II != I->begin() && II->start > StartPosition)
470 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000471 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000472 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000473 break;
474 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000475 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000476 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000477 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000478
479 // Okay, the register picked by our speculative getFreePhysReg call turned
480 // out to be in use. Actually add all of the conflicting fixed registers to
481 // prt so we can do an accurate query.
482 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000483 // For every interval in fixed we overlap with, mark the register as not
484 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000485 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
486 IntervalPtr &IP = fixed_[i];
487 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000488
489 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
490 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
491 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000492 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
493 IP.second = II;
494 if (II != I->begin() && II->start > StartPosition)
495 --II;
496 if (cur->overlapsFrom(*I, II)) {
497 unsigned reg = I->reg;
498 prt_->addRegUse(reg);
499 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
500 }
501 }
502 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000503
Chris Lattnera411cbc2005-08-22 20:59:30 +0000504 // Using the newly updated prt_ object, which includes conflicts in the
505 // future, see if there are any registers available.
506 physReg = getFreePhysReg(cur);
507 }
508 }
509
Chris Lattnera6c17502005-08-22 20:20:42 +0000510 // Restore the physical register tracker, removing information about the
511 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000512 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000513
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000514 // if we find a free register, we are done: assign this virtual to
515 // the free physical register and add this interval to the active
516 // list.
517 if (physReg) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000518 DOUT << mri_->getName(physReg) << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000519 vrm_->assignVirt2Phys(cur->reg, physReg);
520 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000521 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000522 handled_.push_back(cur);
523 return;
524 }
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000525 DOUT << "no free registers\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000526
Chris Lattnera6c17502005-08-22 20:20:42 +0000527 // Compile the spill weights into an array that is better for scanning.
528 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
529 for (std::vector<std::pair<unsigned, float> >::iterator
530 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
531 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
532
533 // for each interval in active, update spill weights.
534 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
535 i != e; ++i) {
536 unsigned reg = i->first->reg;
537 assert(MRegisterInfo::isVirtualRegister(reg) &&
538 "Can only allocate virtual registers!");
539 reg = vrm_->getPhys(reg);
540 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
541 }
542
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000543 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000545 // Find a register to spill.
Jim Laskey7902c752006-11-07 12:25:45 +0000546 float minWeight = HUGE_VALF;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000547 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000548 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
549 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000550 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000551 if (minWeight > SpillWeights[reg]) {
552 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000553 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000554 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000555 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000556
557 // If we didn't find a register that is spillable, try aliases?
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000558 if (!minReg) {
559 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
560 e = RC->allocation_order_end(*mf_); i != e; ++i) {
561 unsigned reg = *i;
562 // No need to worry about if the alias register size < regsize of RC.
563 // We are going to spill all registers that alias it anyway.
564 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) {
565 if (minWeight > SpillWeights[*as]) {
566 minWeight = SpillWeights[*as];
567 minReg = *as;
568 }
569 }
570 }
571
572 // All registers must have inf weight. Just grab one!
573 if (!minReg)
574 minReg = *RC->allocation_order_begin(*mf_);
575 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000576
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000577 DOUT << "\t\tregister with min weight: "
578 << mri_->getName(minReg) << " (" << minWeight << ")\n";
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000579
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000580 // if the current has the minimum weight, we need to spill it and
581 // add any added intervals back to unhandled, and restart
582 // linearscan.
Jim Laskey7902c752006-11-07 12:25:45 +0000583 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000584 DOUT << "\t\t\tspilling(c): " << *cur << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000585 int slot = vrm_->assignVirt2StackSlot(cur->reg);
586 std::vector<LiveInterval*> added =
587 li_->addIntervalsForSpills(*cur, *vrm_, slot);
588 if (added.empty())
589 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000590
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000591 // Merge added with unhandled. Note that we know that
592 // addIntervalsForSpills returns intervals sorted by their starting
593 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000594 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000595 unhandled_.push(added[i]);
596 return;
597 }
598
Chris Lattner19828d42004-11-18 03:49:30 +0000599 ++NumBacktracks;
600
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000601 // push the current interval back to unhandled since we are going
602 // to re-run at least this iteration. Since we didn't modify it it
603 // should go back right in the front of the list
604 unhandled_.push(cur);
605
606 // otherwise we spill all intervals aliasing the register with
607 // minimum weight, rollback to the interval with the earliest
608 // start point and let the linear scan algorithm run again
609 std::vector<LiveInterval*> added;
610 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
611 "did not choose a register to spill?");
612 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000613
614 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615 toSpill[minReg] = true;
616 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
617 toSpill[*as] = true;
618
619 // the earliest start of a spilled interval indicates up to where
620 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000621 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000622
623 // set of spilled vregs (used later to rollback properly)
624 std::set<unsigned> spilled;
625
Chris Lattner19828d42004-11-18 03:49:30 +0000626 // spill live intervals of virtual regs mapped to the physical register we
627 // want to clear (and its aliases). We only spill those that overlap with the
628 // current interval as the rest do not affect its allocation. we also keep
629 // track of the earliest start of all spilled live intervals since this will
630 // mark our rollback point.
631 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000632 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000633 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000634 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000635 cur->overlapsFrom(*i->first, i->second)) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000636 DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000637 earliestStart = std::min(earliestStart, i->first->beginNumber());
638 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000639 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000640 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000641 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
642 spilled.insert(reg);
643 }
644 }
Chris Lattner19828d42004-11-18 03:49:30 +0000645 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000646 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000647 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000648 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000649 cur->overlapsFrom(*i->first, i->second-1)) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000650 DOUT << "\t\t\tspilling(i): " << *i->first << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000651 earliestStart = std::min(earliestStart, i->first->beginNumber());
652 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000653 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000654 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000655 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
656 spilled.insert(reg);
657 }
658 }
659
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000660 DOUT << "\t\trolling back to: " << earliestStart << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000661
662 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000663 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000664 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 while (!handled_.empty()) {
666 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000667 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000668 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000669 break;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000670 DOUT << "\t\t\tundo changes for: " << *i << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000671 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000672
673 // When undoing a live interval allocation we must know if it is active or
674 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000675 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000676 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000677 active_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000678 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
679 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000681 prt_->delRegUse(vrm_->getPhys(i->reg));
682 vrm_->clearVirt(i->reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000683 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000684 inactive_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000685 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
686 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000687 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000688 vrm_->clearVirt(i->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000689 } else {
690 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
691 "Can only allocate virtual registers!");
692 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000693 unhandled_.push(i);
694 }
695 }
696
Chris Lattner19828d42004-11-18 03:49:30 +0000697 // Rewind the iterators in the active, inactive, and fixed lists back to the
698 // point we reverted to.
699 RevertVectorIteratorsTo(active_, earliestStart);
700 RevertVectorIteratorsTo(inactive_, earliestStart);
701 RevertVectorIteratorsTo(fixed_, earliestStart);
702
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000703 // scan the rest and undo each interval that expired after t and
704 // insert it in active (the next iteration of the algorithm will
705 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000706 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
707 LiveInterval *HI = handled_[i];
708 if (!HI->expiredAt(earliestStart) &&
709 HI->expiredAt(cur->beginNumber())) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000710 DOUT << "\t\t\tundo changes for: " << *HI << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000711 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerffab4222006-02-23 06:44:17 +0000712 assert(!MRegisterInfo::isPhysicalRegister(HI->reg));
713 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000714 }
715 }
716
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000717 // merge added with unhandled
718 for (unsigned i = 0, e = added.size(); i != e; ++i)
719 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000720}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000721
Chris Lattnercbb56252004-11-18 02:42:27 +0000722/// getFreePhysReg - return a free physical register for this virtual register
723/// interval if we have one, otherwise return 0.
Chris Lattnerffab4222006-02-23 06:44:17 +0000724unsigned RA::getFreePhysReg(LiveInterval *cur) {
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000725 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000726 unsigned MaxInactiveCount = 0;
727
Chris Lattnerb9805782005-08-23 22:27:31 +0000728 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
729 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
730
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000731 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
732 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000733 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000734 assert(MRegisterInfo::isVirtualRegister(reg) &&
735 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000736
737 // If this is not in a related reg class to the register we're allocating,
738 // don't check it.
739 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
740 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
741 reg = vrm_->getPhys(reg);
742 ++inactiveCounts[reg];
743 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
744 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000745 }
746
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000747 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000748
Chris Lattnerf8355d92005-08-22 16:55:22 +0000749 unsigned FreeReg = 0;
750 unsigned FreeRegInactiveCount = 0;
751
752 // Scan for the first available register.
753 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
754 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
755 for (; I != E; ++I)
756 if (prt_->isRegAvail(*I)) {
757 FreeReg = *I;
758 FreeRegInactiveCount = inactiveCounts[FreeReg];
759 break;
760 }
761
762 // If there are no free regs, or if this reg has the max inactive count,
763 // return this register.
764 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
765
766 // Continue scanning the registers, looking for the one with the highest
767 // inactive count. Alkis found that this reduced register pressure very
768 // slightly on X86 (in rev 1.94 of this file), though this should probably be
769 // reevaluated now.
770 for (; I != E; ++I) {
771 unsigned Reg = *I;
772 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
773 FreeReg = Reg;
774 FreeRegInactiveCount = inactiveCounts[Reg];
775 if (FreeRegInactiveCount == MaxInactiveCount)
776 break; // We found the one with the max inactive count.
777 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000778 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000779
780 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000781}
782
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000783FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000784 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000785}