blob: 0fb8f3d13638788317182efe78719a431e7c75b0 [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"
22#include "llvm/CodeGen/SSARegMap.h"
23#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000024#include "llvm/Target/TargetMachine.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000025#include "llvm/ADT/EquivalenceClasses.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000026#include "llvm/ADT/Statistic.h"
27#include "llvm/ADT/STLExtras.h"
Chris Lattnerb9805782005-08-23 22:27:31 +000028#include "llvm/Support/Debug.h"
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000029#include <algorithm>
Alkis Evlogimenos880e8e42004-05-08 03:50:03 +000030#include <cmath>
Chris Lattner2c2c6c62006-01-22 23:41:00 +000031#include <iostream>
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
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000039 Statistic<double> efficiency
40 ("regalloc", "Ratio of intervals processed over total intervals");
Chris Lattner19828d42004-11-18 03:49:30 +000041 Statistic<> NumBacktracks("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000042
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000043 static unsigned numIterations = 0;
44 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000045
Chris Lattnercbb56252004-11-18 02:42:27 +000046 struct RA : public MachineFunctionPass {
47 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
48 typedef std::vector<IntervalPtr> IntervalPtrs;
49 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000050 /// RelatedRegClasses - This structure is built the first time a function is
51 /// compiled, and keeps track of which register classes have registers that
52 /// belong to multiple classes or have aliases that are in other classes.
53 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
54 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
55
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000056 MachineFunction* mf_;
57 const TargetMachine* tm_;
58 const MRegisterInfo* mri_;
59 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000060 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000061
62 /// handled_ - Intervals are added to the handled_ set in the order of their
63 /// start value. This is uses for backtracking.
64 std::vector<LiveInterval*> handled_;
65
66 /// fixed_ - Intervals that correspond to machine registers.
67 ///
68 IntervalPtrs fixed_;
69
70 /// active_ - Intervals that are currently being processed, and which have a
71 /// live range active for the current point.
72 IntervalPtrs active_;
73
74 /// inactive_ - Intervals that are currently being processed, but which have
75 /// a hold at the current point.
76 IntervalPtrs inactive_;
77
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000078 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000079 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000080 greater_ptr<LiveInterval> > IntervalHeap;
81 IntervalHeap unhandled_;
82 std::auto_ptr<PhysRegTracker> prt_;
83 std::auto_ptr<VirtRegMap> vrm_;
84 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000085
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000086 public:
87 virtual const char* getPassName() const {
88 return "Linear Scan Register Allocator";
89 }
90
91 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000092 AU.addRequired<LiveIntervals>();
93 MachineFunctionPass::getAnalysisUsage(AU);
94 }
95
96 /// runOnMachineFunction - register allocate the whole function
97 bool runOnMachineFunction(MachineFunction&);
98
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000099 private:
100 /// linearScan - the linear scan algorithm
101 void linearScan();
102
Chris Lattnercbb56252004-11-18 02:42:27 +0000103 /// initIntervalSets - initialize the interval sets.
104 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105 void initIntervalSets();
106
Chris Lattnercbb56252004-11-18 02:42:27 +0000107 /// processActiveIntervals - expire old intervals and move non-overlapping
108 /// ones to the inactive list.
109 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000110
Chris Lattnercbb56252004-11-18 02:42:27 +0000111 /// processInactiveIntervals - expire old intervals and move overlapping
112 /// ones to the active list.
113 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000114
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000115 /// assignRegOrStackSlotAtInterval - assign a register if one
116 /// is available, or spill.
117 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
118
119 ///
120 /// register handling helpers
121 ///
122
Chris Lattnercbb56252004-11-18 02:42:27 +0000123 /// getFreePhysReg - return a free physical register for this virtual
124 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000125 unsigned getFreePhysReg(LiveInterval* cur);
126
127 /// assignVirt2StackSlot - assigns this virtual register to a
128 /// stack slot. returns the stack slot
129 int assignVirt2StackSlot(unsigned virtReg);
130
Chris Lattnerb9805782005-08-23 22:27:31 +0000131 void ComputeRelatedRegClasses();
132
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000133 template <typename ItTy>
134 void printIntervals(const char* const str, ItTy i, ItTy e) const {
135 if (str) std::cerr << str << " intervals:\n";
136 for (; i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000137 std::cerr << "\t" << *i->first << " -> ";
138 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000139 if (MRegisterInfo::isVirtualRegister(reg)) {
140 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000141 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000142 std::cerr << mri_->getName(reg) << '\n';
143 }
144 }
145 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000146}
147
Chris Lattnerb9805782005-08-23 22:27:31 +0000148void RA::ComputeRelatedRegClasses() {
149 const MRegisterInfo &MRI = *mri_;
150
151 // First pass, add all reg classes to the union, and determine at least one
152 // reg class that each register is in.
153 bool HasAliases = false;
154 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
155 E = MRI.regclass_end(); RCI != E; ++RCI) {
156 RelatedRegClasses.insert(*RCI);
157 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
158 I != E; ++I) {
159 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
160
161 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
162 if (PRC) {
163 // Already processed this register. Just make sure we know that
164 // multiple register classes share a register.
165 RelatedRegClasses.unionSets(PRC, *RCI);
166 } else {
167 PRC = *RCI;
168 }
169 }
170 }
171
172 // Second pass, now that we know conservatively what register classes each reg
173 // belongs to, add info about aliases. We don't need to do this for targets
174 // without register aliases.
175 if (HasAliases)
176 for (std::map<unsigned, const TargetRegisterClass*>::iterator
177 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
178 I != E; ++I)
179 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
180 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
181}
182
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000183bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000184 mf_ = &fn;
185 tm_ = &fn.getTarget();
186 mri_ = tm_->getRegisterInfo();
187 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000188
Chris Lattnerb9805782005-08-23 22:27:31 +0000189 // If this is the first function compiled, compute the related reg classes.
190 if (RelatedRegClasses.empty())
191 ComputeRelatedRegClasses();
192
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000193 PhysRegsUsed = new bool[mri_->getNumRegs()];
194 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
195 fn.setUsedPhysRegs(PhysRegsUsed);
196
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000197 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
198 vrm_.reset(new VirtRegMap(*mf_));
199 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000200
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000201 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000202
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000203 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000204
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000205 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000206 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000207
Chris Lattner510a3ea2004-09-30 02:02:33 +0000208 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000209
210
211 while (!unhandled_.empty()) unhandled_.pop();
212 fixed_.clear();
213 active_.clear();
214 inactive_.clear();
215 handled_.clear();
216
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000217 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000218}
219
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000220/// initIntervalSets - initialize the interval sets.
221///
222void RA::initIntervalSets()
223{
224 assert(unhandled_.empty() && fixed_.empty() &&
225 active_.empty() && inactive_.empty() &&
226 "interval sets should be empty on initialization");
227
228 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000229 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
230 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000231 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000232 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000233 unhandled_.push(&i->second);
234 }
235}
236
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000237void RA::linearScan()
238{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000239 // linear scan algorithm
240 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
241 DEBUG(std::cerr << "********** Function: "
242 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000243
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000244 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
245 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
246 DEBUG(printIntervals("active", active_.begin(), active_.end()));
247 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
248
249 while (!unhandled_.empty()) {
250 // pick the interval with the earliest start point
251 LiveInterval* cur = unhandled_.top();
252 unhandled_.pop();
253 ++numIterations;
254 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
255
Chris Lattnercbb56252004-11-18 02:42:27 +0000256 processActiveIntervals(cur->beginNumber());
257 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000258
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000259 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
260 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000261
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000262 // Allocating a virtual register. try to find a free
263 // physical register or spill an interval (possibly this one) in order to
264 // assign it one.
265 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000266
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000267 DEBUG(printIntervals("active", active_.begin(), active_.end()));
268 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000269 }
270 numIntervals += li_->getNumIntervals();
271 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000272
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000273 // expire any remaining active intervals
274 for (IntervalPtrs::reverse_iterator
275 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000276 unsigned reg = i->first->reg;
277 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000278 assert(MRegisterInfo::isVirtualRegister(reg) &&
279 "Can only allocate virtual registers!");
280 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000281 prt_->delRegUse(reg);
282 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
283 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000284
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000285 // expire any remaining inactive intervals
286 for (IntervalPtrs::reverse_iterator
287 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000288 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000289 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
290 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000291
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000292 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000293}
294
Chris Lattnercbb56252004-11-18 02:42:27 +0000295/// processActiveIntervals - expire old intervals and move non-overlapping ones
296/// to the inactive list.
297void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000298{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000299 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattner23b71c12004-11-18 01:29:39 +0000300
Chris Lattnercbb56252004-11-18 02:42:27 +0000301 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
302 LiveInterval *Interval = active_[i].first;
303 LiveInterval::iterator IntervalPos = active_[i].second;
304 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000305
Chris Lattnercbb56252004-11-18 02:42:27 +0000306 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
307
308 if (IntervalPos == Interval->end()) { // Remove expired intervals.
309 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000310 assert(MRegisterInfo::isVirtualRegister(reg) &&
311 "Can only allocate virtual registers!");
312 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000313 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000314
315 // Pop off the end of the list.
316 active_[i] = active_.back();
317 active_.pop_back();
318 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000319
Chris Lattnercbb56252004-11-18 02:42:27 +0000320 } else if (IntervalPos->start > CurPoint) {
321 // Move inactive intervals to inactive list.
322 DEBUG(std::cerr << "\t\tinterval " << *Interval << " inactive\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000323 assert(MRegisterInfo::isVirtualRegister(reg) &&
324 "Can only allocate virtual registers!");
325 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000326 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000327 // add to inactive.
328 inactive_.push_back(std::make_pair(Interval, IntervalPos));
329
330 // Pop off the end of the list.
331 active_[i] = active_.back();
332 active_.pop_back();
333 --i; --e;
334 } else {
335 // Otherwise, just update the iterator position.
336 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000337 }
338 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000339}
340
Chris Lattnercbb56252004-11-18 02:42:27 +0000341/// processInactiveIntervals - expire old intervals and move overlapping
342/// ones to the active list.
343void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000344{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000345 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner365b95f2004-11-18 04:13:02 +0000346
Chris Lattnercbb56252004-11-18 02:42:27 +0000347 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
348 LiveInterval *Interval = inactive_[i].first;
349 LiveInterval::iterator IntervalPos = inactive_[i].second;
350 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000351
Chris Lattnercbb56252004-11-18 02:42:27 +0000352 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000353
Chris Lattnercbb56252004-11-18 02:42:27 +0000354 if (IntervalPos == Interval->end()) { // remove expired intervals.
355 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000356
Chris Lattnercbb56252004-11-18 02:42:27 +0000357 // Pop off the end of the list.
358 inactive_[i] = inactive_.back();
359 inactive_.pop_back();
360 --i; --e;
361 } else if (IntervalPos->start <= CurPoint) {
362 // move re-activated intervals in active list
363 DEBUG(std::cerr << "\t\tinterval " << *Interval << " active\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000364 assert(MRegisterInfo::isVirtualRegister(reg) &&
365 "Can only allocate virtual registers!");
366 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000367 prt_->addRegUse(reg);
368 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000369 active_.push_back(std::make_pair(Interval, IntervalPos));
370
371 // Pop off the end of the list.
372 inactive_[i] = inactive_.back();
373 inactive_.pop_back();
374 --i; --e;
375 } else {
376 // Otherwise, just update the iterator position.
377 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000378 }
379 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000380}
381
Chris Lattnercbb56252004-11-18 02:42:27 +0000382/// updateSpillWeights - updates the spill weights of the specifed physical
383/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000384static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000385 unsigned reg, float weight,
386 const MRegisterInfo *MRI) {
387 Weights[reg] += weight;
388 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
389 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000390}
391
Chris Lattnercbb56252004-11-18 02:42:27 +0000392static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
393 LiveInterval *LI) {
394 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
395 if (I->first == LI) return I;
396 return IP.end();
397}
398
Chris Lattner19828d42004-11-18 03:49:30 +0000399static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
400 for (unsigned i = 0, e = V.size(); i != e; ++i) {
401 RA::IntervalPtr &IP = V[i];
402 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
403 IP.second, Point);
404 if (I != IP.first->begin()) --I;
405 IP.second = I;
406 }
407}
Chris Lattnercbb56252004-11-18 02:42:27 +0000408
Chris Lattnercbb56252004-11-18 02:42:27 +0000409/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
410/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000411void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000412{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000413 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000414
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000415 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000416
Chris Lattnera6c17502005-08-22 20:20:42 +0000417 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000418 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000419 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
420 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
421
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000422 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000423 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000424 for (IntervalPtrs::const_iterator i = inactive_.begin(),
425 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000426 unsigned Reg = i->first->reg;
427 assert(MRegisterInfo::isVirtualRegister(Reg) &&
428 "Can only allocate virtual registers!");
429 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
430 // If this is not in a related reg class to the register we're allocating,
431 // don't check it.
432 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
433 cur->overlapsFrom(*i->first, i->second-1)) {
434 Reg = vrm_->getPhys(Reg);
435 prt_->addRegUse(Reg);
436 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000437 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000438 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000439
440 // Speculatively check to see if we can get a register right now. If not,
441 // we know we won't be able to by adding more constraints. If so, we can
442 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
443 // is very bad (it contains all callee clobbered registers for any functions
444 // with a call), so we want to avoid doing that if possible.
445 unsigned physReg = getFreePhysReg(cur);
446 if (physReg) {
447 // We got a register. However, if it's in the fixed_ list, we might
Chris Lattnere836ad62005-08-30 21:03:36 +0000448 // conflict with it. Check to see if we conflict with it or any of its
449 // aliases.
450 std::set<unsigned> RegAliases;
451 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
452 RegAliases.insert(*AS);
453
Chris Lattnera411cbc2005-08-22 20:59:30 +0000454 bool ConflictsWithFixed = false;
455 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Chris Lattnere836ad62005-08-30 21:03:36 +0000456 if (physReg == fixed_[i].first->reg ||
457 RegAliases.count(fixed_[i].first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000458 // Okay, this reg is on the fixed list. Check to see if we actually
459 // conflict.
460 IntervalPtr &IP = fixed_[i];
461 LiveInterval *I = IP.first;
462 if (I->endNumber() > StartPosition) {
463 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
464 IP.second = II;
465 if (II != I->begin() && II->start > StartPosition)
466 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000467 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000468 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000469 break;
470 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000471 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000472 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000473 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000474
475 // Okay, the register picked by our speculative getFreePhysReg call turned
476 // out to be in use. Actually add all of the conflicting fixed registers to
477 // prt so we can do an accurate query.
478 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000479 // For every interval in fixed we overlap with, mark the register as not
480 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000481 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
482 IntervalPtr &IP = fixed_[i];
483 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000484
485 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
486 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
487 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000488 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
489 IP.second = II;
490 if (II != I->begin() && II->start > StartPosition)
491 --II;
492 if (cur->overlapsFrom(*I, II)) {
493 unsigned reg = I->reg;
494 prt_->addRegUse(reg);
495 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
496 }
497 }
498 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000499
Chris Lattnera411cbc2005-08-22 20:59:30 +0000500 // Using the newly updated prt_ object, which includes conflicts in the
501 // future, see if there are any registers available.
502 physReg = getFreePhysReg(cur);
503 }
504 }
505
Chris Lattnera6c17502005-08-22 20:20:42 +0000506 // Restore the physical register tracker, removing information about the
507 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000508 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000509
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000510 // if we find a free register, we are done: assign this virtual to
511 // the free physical register and add this interval to the active
512 // list.
513 if (physReg) {
514 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
515 vrm_->assignVirt2Phys(cur->reg, physReg);
516 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000517 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000518 handled_.push_back(cur);
519 return;
520 }
521 DEBUG(std::cerr << "no free registers\n");
522
Chris Lattnera6c17502005-08-22 20:20:42 +0000523 // Compile the spill weights into an array that is better for scanning.
524 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
525 for (std::vector<std::pair<unsigned, float> >::iterator
526 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
527 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
528
529 // for each interval in active, update spill weights.
530 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
531 i != e; ++i) {
532 unsigned reg = i->first->reg;
533 assert(MRegisterInfo::isVirtualRegister(reg) &&
534 "Can only allocate virtual registers!");
535 reg = vrm_->getPhys(reg);
536 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
537 }
538
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
540
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000541 // Find a register to spill.
Chris Lattner5e5fb942005-01-08 19:53:50 +0000542 float minWeight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000544 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
545 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000546 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000547 if (minWeight > SpillWeights[reg]) {
548 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000549 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000550 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000551 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000552
553 // If we didn't find a register that is spillable, try aliases?
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000554 if (!minReg) {
555 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
556 e = RC->allocation_order_end(*mf_); i != e; ++i) {
557 unsigned reg = *i;
558 // No need to worry about if the alias register size < regsize of RC.
559 // We are going to spill all registers that alias it anyway.
560 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) {
561 if (minWeight > SpillWeights[*as]) {
562 minWeight = SpillWeights[*as];
563 minReg = *as;
564 }
565 }
566 }
567
568 // All registers must have inf weight. Just grab one!
569 if (!minReg)
570 minReg = *RC->allocation_order_begin(*mf_);
571 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000572
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000573 DEBUG(std::cerr << "\t\tregister with min weight: "
574 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000575
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000576 // if the current has the minimum weight, we need to spill it and
577 // add any added intervals back to unhandled, and restart
578 // linearscan.
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000579 if (cur->weight != float(HUGE_VAL) && cur->weight <= minWeight) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000580 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
581 int slot = vrm_->assignVirt2StackSlot(cur->reg);
582 std::vector<LiveInterval*> added =
583 li_->addIntervalsForSpills(*cur, *vrm_, slot);
584 if (added.empty())
585 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000586
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000587 // Merge added with unhandled. Note that we know that
588 // addIntervalsForSpills returns intervals sorted by their starting
589 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000590 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000591 unhandled_.push(added[i]);
592 return;
593 }
594
Chris Lattner19828d42004-11-18 03:49:30 +0000595 ++NumBacktracks;
596
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000597 // push the current interval back to unhandled since we are going
598 // to re-run at least this iteration. Since we didn't modify it it
599 // should go back right in the front of the list
600 unhandled_.push(cur);
601
602 // otherwise we spill all intervals aliasing the register with
603 // minimum weight, rollback to the interval with the earliest
604 // start point and let the linear scan algorithm run again
605 std::vector<LiveInterval*> added;
606 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
607 "did not choose a register to spill?");
608 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000609
610 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000611 toSpill[minReg] = true;
612 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
613 toSpill[*as] = true;
614
615 // the earliest start of a spilled interval indicates up to where
616 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000617 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000618
619 // set of spilled vregs (used later to rollback properly)
620 std::set<unsigned> spilled;
621
Chris Lattner19828d42004-11-18 03:49:30 +0000622 // spill live intervals of virtual regs mapped to the physical register we
623 // want to clear (and its aliases). We only spill those that overlap with the
624 // current interval as the rest do not affect its allocation. we also keep
625 // track of the earliest start of all spilled live intervals since this will
626 // mark our rollback point.
627 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000628 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000629 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000630 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000631 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000632 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
633 earliestStart = std::min(earliestStart, i->first->beginNumber());
634 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000635 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000636 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000637 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
638 spilled.insert(reg);
639 }
640 }
Chris Lattner19828d42004-11-18 03:49:30 +0000641 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000642 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000643 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000644 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000645 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000646 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
647 earliestStart = std::min(earliestStart, i->first->beginNumber());
648 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000649 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000650 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000651 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
652 spilled.insert(reg);
653 }
654 }
655
656 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnercbb56252004-11-18 02:42:27 +0000657
658 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000659 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000660 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000661 while (!handled_.empty()) {
662 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000663 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000664 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 break;
666 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
667 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000668
669 // When undoing a live interval allocation we must know if it is active or
670 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000671 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000672 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000673 active_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000674 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
675 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000676 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000677 prt_->delRegUse(vrm_->getPhys(i->reg));
678 vrm_->clearVirt(i->reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000679 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 inactive_.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 vrm_->clearVirt(i->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000685 } else {
686 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
687 "Can only allocate virtual registers!");
688 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000689 unhandled_.push(i);
690 }
691 }
692
Chris Lattner19828d42004-11-18 03:49:30 +0000693 // Rewind the iterators in the active, inactive, and fixed lists back to the
694 // point we reverted to.
695 RevertVectorIteratorsTo(active_, earliestStart);
696 RevertVectorIteratorsTo(inactive_, earliestStart);
697 RevertVectorIteratorsTo(fixed_, earliestStart);
698
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000699 // scan the rest and undo each interval that expired after t and
700 // insert it in active (the next iteration of the algorithm will
701 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000702 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
703 LiveInterval *HI = handled_[i];
704 if (!HI->expiredAt(earliestStart) &&
705 HI->expiredAt(cur->beginNumber())) {
706 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
707 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerffab4222006-02-23 06:44:17 +0000708 assert(!MRegisterInfo::isPhysicalRegister(HI->reg));
709 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000710 }
711 }
712
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000713 // merge added with unhandled
714 for (unsigned i = 0, e = added.size(); i != e; ++i)
715 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000716}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000717
Chris Lattnercbb56252004-11-18 02:42:27 +0000718/// getFreePhysReg - return a free physical register for this virtual register
719/// interval if we have one, otherwise return 0.
Chris Lattnerffab4222006-02-23 06:44:17 +0000720unsigned RA::getFreePhysReg(LiveInterval *cur) {
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000721 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000722 unsigned MaxInactiveCount = 0;
723
Chris Lattnerb9805782005-08-23 22:27:31 +0000724 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
725 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
726
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000727 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
728 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000729 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000730 assert(MRegisterInfo::isVirtualRegister(reg) &&
731 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000732
733 // If this is not in a related reg class to the register we're allocating,
734 // don't check it.
735 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
736 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
737 reg = vrm_->getPhys(reg);
738 ++inactiveCounts[reg];
739 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
740 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000741 }
742
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000743 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000744
Chris Lattnerf8355d92005-08-22 16:55:22 +0000745 unsigned FreeReg = 0;
746 unsigned FreeRegInactiveCount = 0;
747
748 // Scan for the first available register.
749 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
750 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
751 for (; I != E; ++I)
752 if (prt_->isRegAvail(*I)) {
753 FreeReg = *I;
754 FreeRegInactiveCount = inactiveCounts[FreeReg];
755 break;
756 }
757
758 // If there are no free regs, or if this reg has the max inactive count,
759 // return this register.
760 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
761
762 // Continue scanning the registers, looking for the one with the highest
763 // inactive count. Alkis found that this reduced register pressure very
764 // slightly on X86 (in rev 1.94 of this file), though this should probably be
765 // reevaluated now.
766 for (; I != E; ++I) {
767 unsigned Reg = *I;
768 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
769 FreeReg = Reg;
770 FreeRegInactiveCount = inactiveCounts[Reg];
771 if (FreeRegInactiveCount == MaxInactiveCount)
772 break; // We found the one with the max inactive count.
773 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000774 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000775
776 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000777}
778
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000779FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000780 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000781}