blob: bd44e81244e235a2e509a890617a47d27b0e9f17 [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
Chris Lattnercd3245a2006-12-19 22:41:21 +000038STATISTIC(NumIters , "Number of iterations performed");
39STATISTIC(NumBacktracks, "Number of times we had to backtrack");
40
41static RegisterRegAlloc
42linearscanRegAlloc("linearscan", " linear scan register allocator",
43 createLinearScanRegisterAllocator);
44
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000045namespace {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000046 static unsigned numIterations = 0;
47 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000048
Chris Lattnerf8c68f62006-06-28 22:17:39 +000049 struct VISIBILITY_HIDDEN RA : public MachineFunctionPass {
Chris Lattnercbb56252004-11-18 02:42:27 +000050 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
51 typedef std::vector<IntervalPtr> IntervalPtrs;
52 private:
Chris Lattnerb9805782005-08-23 22:27:31 +000053 /// RelatedRegClasses - This structure is built the first time a function is
54 /// compiled, and keeps track of which register classes have registers that
55 /// belong to multiple classes or have aliases that are in other classes.
56 EquivalenceClasses<const TargetRegisterClass*> RelatedRegClasses;
57 std::map<unsigned, const TargetRegisterClass*> OneClassForEachPhysReg;
58
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000059 MachineFunction* mf_;
60 const TargetMachine* tm_;
61 const MRegisterInfo* mri_;
62 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000063 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000064
65 /// handled_ - Intervals are added to the handled_ set in the order of their
66 /// start value. This is uses for backtracking.
67 std::vector<LiveInterval*> handled_;
68
69 /// fixed_ - Intervals that correspond to machine registers.
70 ///
71 IntervalPtrs fixed_;
72
73 /// active_ - Intervals that are currently being processed, and which have a
74 /// live range active for the current point.
75 IntervalPtrs active_;
76
77 /// inactive_ - Intervals that are currently being processed, but which have
78 /// a hold at the current point.
79 IntervalPtrs inactive_;
80
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000081 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000082 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 greater_ptr<LiveInterval> > IntervalHeap;
84 IntervalHeap unhandled_;
85 std::auto_ptr<PhysRegTracker> prt_;
86 std::auto_ptr<VirtRegMap> vrm_;
87 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000088
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000089 public:
90 virtual const char* getPassName() const {
91 return "Linear Scan Register Allocator";
92 }
93
94 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000095 AU.addRequired<LiveIntervals>();
96 MachineFunctionPass::getAnalysisUsage(AU);
97 }
98
99 /// runOnMachineFunction - register allocate the whole function
100 bool runOnMachineFunction(MachineFunction&);
101
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000102 private:
103 /// linearScan - the linear scan algorithm
104 void linearScan();
105
Chris Lattnercbb56252004-11-18 02:42:27 +0000106 /// initIntervalSets - initialize the interval sets.
107 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000108 void initIntervalSets();
109
Chris Lattnercbb56252004-11-18 02:42:27 +0000110 /// processActiveIntervals - expire old intervals and move non-overlapping
111 /// ones to the inactive list.
112 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000113
Chris Lattnercbb56252004-11-18 02:42:27 +0000114 /// processInactiveIntervals - expire old intervals and move overlapping
115 /// ones to the active list.
116 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000117
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000118 /// assignRegOrStackSlotAtInterval - assign a register if one
119 /// is available, or spill.
120 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
121
122 ///
123 /// register handling helpers
124 ///
125
Chris Lattnercbb56252004-11-18 02:42:27 +0000126 /// getFreePhysReg - return a free physical register for this virtual
127 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 unsigned getFreePhysReg(LiveInterval* cur);
129
130 /// assignVirt2StackSlot - assigns this virtual register to a
131 /// stack slot. returns the stack slot
132 int assignVirt2StackSlot(unsigned virtReg);
133
Chris Lattnerb9805782005-08-23 22:27:31 +0000134 void ComputeRelatedRegClasses();
135
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000136 template <typename ItTy>
137 void printIntervals(const char* const str, ItTy i, ItTy e) const {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000138 if (str) DOUT << str << " intervals:\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000139 for (; i != e; ++i) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000140 DOUT << "\t" << *i->first << " -> ";
Chris Lattnercbb56252004-11-18 02:42:27 +0000141 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000142 if (MRegisterInfo::isVirtualRegister(reg)) {
143 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000144 }
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000145 DOUT << mri_->getName(reg) << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000146 }
147 }
148 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000149}
150
Chris Lattnerb9805782005-08-23 22:27:31 +0000151void RA::ComputeRelatedRegClasses() {
152 const MRegisterInfo &MRI = *mri_;
153
154 // First pass, add all reg classes to the union, and determine at least one
155 // reg class that each register is in.
156 bool HasAliases = false;
157 for (MRegisterInfo::regclass_iterator RCI = MRI.regclass_begin(),
158 E = MRI.regclass_end(); RCI != E; ++RCI) {
159 RelatedRegClasses.insert(*RCI);
160 for (TargetRegisterClass::iterator I = (*RCI)->begin(), E = (*RCI)->end();
161 I != E; ++I) {
162 HasAliases = HasAliases || *MRI.getAliasSet(*I) != 0;
163
164 const TargetRegisterClass *&PRC = OneClassForEachPhysReg[*I];
165 if (PRC) {
166 // Already processed this register. Just make sure we know that
167 // multiple register classes share a register.
168 RelatedRegClasses.unionSets(PRC, *RCI);
169 } else {
170 PRC = *RCI;
171 }
172 }
173 }
174
175 // Second pass, now that we know conservatively what register classes each reg
176 // belongs to, add info about aliases. We don't need to do this for targets
177 // without register aliases.
178 if (HasAliases)
179 for (std::map<unsigned, const TargetRegisterClass*>::iterator
180 I = OneClassForEachPhysReg.begin(), E = OneClassForEachPhysReg.end();
181 I != E; ++I)
182 for (const unsigned *AS = MRI.getAliasSet(I->first); *AS; ++AS)
183 RelatedRegClasses.unionSets(I->second, OneClassForEachPhysReg[*AS]);
184}
185
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000186bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000187 mf_ = &fn;
188 tm_ = &fn.getTarget();
189 mri_ = tm_->getRegisterInfo();
190 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000191
Chris Lattnerb9805782005-08-23 22:27:31 +0000192 // If this is the first function compiled, compute the related reg classes.
193 if (RelatedRegClasses.empty())
194 ComputeRelatedRegClasses();
195
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000196 PhysRegsUsed = new bool[mri_->getNumRegs()];
197 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
198 fn.setUsedPhysRegs(PhysRegsUsed);
199
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000200 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
201 vrm_.reset(new VirtRegMap(*mf_));
202 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000203
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000204 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000205
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000206 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000207
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000208 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000209 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000210
Chris Lattner510a3ea2004-09-30 02:02:33 +0000211 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000212
213
214 while (!unhandled_.empty()) unhandled_.pop();
215 fixed_.clear();
216 active_.clear();
217 inactive_.clear();
218 handled_.clear();
219
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000220 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000221}
222
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000223/// initIntervalSets - initialize the interval sets.
224///
225void RA::initIntervalSets()
226{
227 assert(unhandled_.empty() && fixed_.empty() &&
228 active_.empty() && inactive_.empty() &&
229 "interval sets should be empty on initialization");
230
231 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000232 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
233 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000234 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000235 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000236 unhandled_.push(&i->second);
237 }
238}
239
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000240void RA::linearScan()
241{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000242 // linear scan algorithm
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000243 DOUT << "********** LINEAR SCAN **********\n";
244 DOUT << "********** Function: " << mf_->getFunction()->getName() << '\n';
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000245
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000246 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
247 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
248 DEBUG(printIntervals("active", active_.begin(), active_.end()));
249 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
250
251 while (!unhandled_.empty()) {
252 // pick the interval with the earliest start point
253 LiveInterval* cur = unhandled_.top();
254 unhandled_.pop();
255 ++numIterations;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000256 DOUT << "\n*** CURRENT ***: " << *cur << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000257
Chris Lattnercbb56252004-11-18 02:42:27 +0000258 processActiveIntervals(cur->beginNumber());
259 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000260
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000261 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
262 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000263
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000264 // Allocating a virtual register. try to find a free
265 // physical register or spill an interval (possibly this one) in order to
266 // assign it one.
267 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000268
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000269 DEBUG(printIntervals("active", active_.begin(), active_.end()));
270 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000271 }
272 numIntervals += li_->getNumIntervals();
Chris Lattner4c7e2272006-12-06 01:48:55 +0000273 NumIters += numIterations;
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000274
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000275 // expire any remaining active intervals
276 for (IntervalPtrs::reverse_iterator
277 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000278 unsigned reg = i->first->reg;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000279 DOUT << "\tinterval " << *i->first << " expired\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000280 assert(MRegisterInfo::isVirtualRegister(reg) &&
281 "Can only allocate virtual registers!");
282 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000283 prt_->delRegUse(reg);
284 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
285 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000286
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 // expire any remaining inactive intervals
288 for (IntervalPtrs::reverse_iterator
289 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000290 DOUT << "\tinterval " << *i->first << " expired\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000291 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
292 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000293
Evan Cheng9fc508f2007-02-16 09:05:02 +0000294 // A brute force way of adding live-ins to every BB.
295 for (MachineFunction::iterator MBB = mf_->begin(), E = mf_->end();
296 MBB != E; ++MBB) {
297 unsigned StartIdx = li_->getMBBStartIdx(MBB->getNumber());
298 for (IntervalPtrs::iterator i = fixed_.begin(), e = fixed_.end();
299 i != e; ++i)
300 if (i->first->liveAt(StartIdx))
301 MBB->addLiveIn(i->first->reg);
302
303 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
304 LiveInterval *HI = handled_[i];
305 if (HI->liveAt(StartIdx)) {
306 unsigned Reg = HI->reg;
307 if (MRegisterInfo::isVirtualRegister(Reg))
308 Reg = vrm_->getPhys(Reg);
309 MBB->addLiveIn(Reg);
310 }
311 }
312 }
313
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000314 DOUT << *vrm_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000315}
316
Chris Lattnercbb56252004-11-18 02:42:27 +0000317/// processActiveIntervals - expire old intervals and move non-overlapping ones
318/// to the inactive list.
319void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000320{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000321 DOUT << "\tprocessing active intervals:\n";
Chris Lattner23b71c12004-11-18 01:29:39 +0000322
Chris Lattnercbb56252004-11-18 02:42:27 +0000323 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
324 LiveInterval *Interval = active_[i].first;
325 LiveInterval::iterator IntervalPos = active_[i].second;
326 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000327
Chris Lattnercbb56252004-11-18 02:42:27 +0000328 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
329
330 if (IntervalPos == Interval->end()) { // Remove expired intervals.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000331 DOUT << "\t\tinterval " << *Interval << " expired\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000332 assert(MRegisterInfo::isVirtualRegister(reg) &&
333 "Can only allocate virtual registers!");
334 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000335 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000336
337 // Pop off the end of the list.
338 active_[i] = active_.back();
339 active_.pop_back();
340 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000341
Chris Lattnercbb56252004-11-18 02:42:27 +0000342 } else if (IntervalPos->start > CurPoint) {
343 // Move inactive intervals to inactive list.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000344 DOUT << "\t\tinterval " << *Interval << " inactive\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000345 assert(MRegisterInfo::isVirtualRegister(reg) &&
346 "Can only allocate virtual registers!");
347 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000348 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000349 // add to inactive.
350 inactive_.push_back(std::make_pair(Interval, IntervalPos));
351
352 // Pop off the end of the list.
353 active_[i] = active_.back();
354 active_.pop_back();
355 --i; --e;
356 } else {
357 // Otherwise, just update the iterator position.
358 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000359 }
360 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000361}
362
Chris Lattnercbb56252004-11-18 02:42:27 +0000363/// processInactiveIntervals - expire old intervals and move overlapping
364/// ones to the active list.
365void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000366{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000367 DOUT << "\tprocessing inactive intervals:\n";
Chris Lattner365b95f2004-11-18 04:13:02 +0000368
Chris Lattnercbb56252004-11-18 02:42:27 +0000369 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
370 LiveInterval *Interval = inactive_[i].first;
371 LiveInterval::iterator IntervalPos = inactive_[i].second;
372 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000373
Chris Lattnercbb56252004-11-18 02:42:27 +0000374 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000375
Chris Lattnercbb56252004-11-18 02:42:27 +0000376 if (IntervalPos == Interval->end()) { // remove expired intervals.
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000377 DOUT << "\t\tinterval " << *Interval << " expired\n";
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000378
Chris Lattnercbb56252004-11-18 02:42:27 +0000379 // Pop off the end of the list.
380 inactive_[i] = inactive_.back();
381 inactive_.pop_back();
382 --i; --e;
383 } else if (IntervalPos->start <= CurPoint) {
384 // move re-activated intervals in active list
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000385 DOUT << "\t\tinterval " << *Interval << " active\n";
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000386 assert(MRegisterInfo::isVirtualRegister(reg) &&
387 "Can only allocate virtual registers!");
388 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000389 prt_->addRegUse(reg);
390 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000391 active_.push_back(std::make_pair(Interval, IntervalPos));
392
393 // Pop off the end of the list.
394 inactive_[i] = inactive_.back();
395 inactive_.pop_back();
396 --i; --e;
397 } else {
398 // Otherwise, just update the iterator position.
399 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000400 }
401 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000402}
403
Chris Lattnercbb56252004-11-18 02:42:27 +0000404/// updateSpillWeights - updates the spill weights of the specifed physical
405/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000406static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000407 unsigned reg, float weight,
408 const MRegisterInfo *MRI) {
409 Weights[reg] += weight;
410 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
411 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000412}
413
Chris Lattnercbb56252004-11-18 02:42:27 +0000414static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
415 LiveInterval *LI) {
416 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
417 if (I->first == LI) return I;
418 return IP.end();
419}
420
Chris Lattner19828d42004-11-18 03:49:30 +0000421static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
422 for (unsigned i = 0, e = V.size(); i != e; ++i) {
423 RA::IntervalPtr &IP = V[i];
424 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
425 IP.second, Point);
426 if (I != IP.first->begin()) --I;
427 IP.second = I;
428 }
429}
Chris Lattnercbb56252004-11-18 02:42:27 +0000430
Chris Lattnercbb56252004-11-18 02:42:27 +0000431/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
432/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000433void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000434{
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000435 DOUT << "\tallocating current interval: ";
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000436
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000437 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000438
Chris Lattnera6c17502005-08-22 20:20:42 +0000439 std::vector<std::pair<unsigned, float> > SpillWeightsToAdd;
Chris Lattner365b95f2004-11-18 04:13:02 +0000440 unsigned StartPosition = cur->beginNumber();
Chris Lattnerb9805782005-08-23 22:27:31 +0000441 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
442 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
443
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000444 // for every interval in inactive we overlap with, mark the
Chris Lattnera6c17502005-08-22 20:20:42 +0000445 // register as not free and update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000446 for (IntervalPtrs::const_iterator i = inactive_.begin(),
447 e = inactive_.end(); i != e; ++i) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000448 unsigned Reg = i->first->reg;
449 assert(MRegisterInfo::isVirtualRegister(Reg) &&
450 "Can only allocate virtual registers!");
451 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(Reg);
452 // If this is not in a related reg class to the register we're allocating,
453 // don't check it.
454 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
455 cur->overlapsFrom(*i->first, i->second-1)) {
456 Reg = vrm_->getPhys(Reg);
457 prt_->addRegUse(Reg);
458 SpillWeightsToAdd.push_back(std::make_pair(Reg, i->first->weight));
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000459 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000460 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000461
462 // Speculatively check to see if we can get a register right now. If not,
463 // we know we won't be able to by adding more constraints. If so, we can
464 // check to see if it is valid. Doing an exhaustive search of the fixed_ list
465 // is very bad (it contains all callee clobbered registers for any functions
466 // with a call), so we want to avoid doing that if possible.
467 unsigned physReg = getFreePhysReg(cur);
468 if (physReg) {
469 // We got a register. However, if it's in the fixed_ list, we might
Chris Lattnere836ad62005-08-30 21:03:36 +0000470 // conflict with it. Check to see if we conflict with it or any of its
471 // aliases.
472 std::set<unsigned> RegAliases;
473 for (const unsigned *AS = mri_->getAliasSet(physReg); *AS; ++AS)
474 RegAliases.insert(*AS);
475
Chris Lattnera411cbc2005-08-22 20:59:30 +0000476 bool ConflictsWithFixed = false;
477 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
Jim Laskeye719d9f2006-10-24 14:35:25 +0000478 IntervalPtr &IP = fixed_[i];
479 if (physReg == IP.first->reg || RegAliases.count(IP.first->reg)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000480 // Okay, this reg is on the fixed list. Check to see if we actually
481 // conflict.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000482 LiveInterval *I = IP.first;
483 if (I->endNumber() > StartPosition) {
484 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
485 IP.second = II;
486 if (II != I->begin() && II->start > StartPosition)
487 --II;
Chris Lattnere836ad62005-08-30 21:03:36 +0000488 if (cur->overlapsFrom(*I, II)) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000489 ConflictsWithFixed = true;
Chris Lattnere836ad62005-08-30 21:03:36 +0000490 break;
491 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000492 }
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000493 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000494 }
Chris Lattnera411cbc2005-08-22 20:59:30 +0000495
496 // Okay, the register picked by our speculative getFreePhysReg call turned
497 // out to be in use. Actually add all of the conflicting fixed registers to
498 // prt so we can do an accurate query.
499 if (ConflictsWithFixed) {
Chris Lattnerb9805782005-08-23 22:27:31 +0000500 // For every interval in fixed we overlap with, mark the register as not
501 // free and update spill weights.
Chris Lattnera411cbc2005-08-22 20:59:30 +0000502 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
503 IntervalPtr &IP = fixed_[i];
504 LiveInterval *I = IP.first;
Chris Lattnerb9805782005-08-23 22:27:31 +0000505
506 const TargetRegisterClass *RegRC = OneClassForEachPhysReg[I->reg];
507 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader &&
508 I->endNumber() > StartPosition) {
Chris Lattnera411cbc2005-08-22 20:59:30 +0000509 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
510 IP.second = II;
511 if (II != I->begin() && II->start > StartPosition)
512 --II;
513 if (cur->overlapsFrom(*I, II)) {
514 unsigned reg = I->reg;
515 prt_->addRegUse(reg);
516 SpillWeightsToAdd.push_back(std::make_pair(reg, I->weight));
517 }
518 }
519 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000520
Chris Lattnera411cbc2005-08-22 20:59:30 +0000521 // Using the newly updated prt_ object, which includes conflicts in the
522 // future, see if there are any registers available.
523 physReg = getFreePhysReg(cur);
524 }
525 }
526
Chris Lattnera6c17502005-08-22 20:20:42 +0000527 // Restore the physical register tracker, removing information about the
528 // future.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000529 *prt_ = backupPrt;
Chris Lattnera6c17502005-08-22 20:20:42 +0000530
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000531 // if we find a free register, we are done: assign this virtual to
532 // the free physical register and add this interval to the active
533 // list.
534 if (physReg) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000535 DOUT << mri_->getName(physReg) << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 vrm_->assignVirt2Phys(cur->reg, physReg);
537 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000538 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000539 handled_.push_back(cur);
540 return;
541 }
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000542 DOUT << "no free registers\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000543
Chris Lattnera6c17502005-08-22 20:20:42 +0000544 // Compile the spill weights into an array that is better for scanning.
545 std::vector<float> SpillWeights(mri_->getNumRegs(), 0.0);
546 for (std::vector<std::pair<unsigned, float> >::iterator
547 I = SpillWeightsToAdd.begin(), E = SpillWeightsToAdd.end(); I != E; ++I)
548 updateSpillWeights(SpillWeights, I->first, I->second, mri_);
549
550 // for each interval in active, update spill weights.
551 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
552 i != e; ++i) {
553 unsigned reg = i->first->reg;
554 assert(MRegisterInfo::isVirtualRegister(reg) &&
555 "Can only allocate virtual registers!");
556 reg = vrm_->getPhys(reg);
557 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
558 }
559
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000560 DOUT << "\tassigning stack slot at interval "<< *cur << ":\n";
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000561
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000562 // Find a register to spill.
Jim Laskey7902c752006-11-07 12:25:45 +0000563 float minWeight = HUGE_VALF;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000564 unsigned minReg = 0;
Chris Lattnerb9805782005-08-23 22:27:31 +0000565 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
566 e = RC->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000567 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000568 if (minWeight > SpillWeights[reg]) {
569 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000570 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000571 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000572 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000573
574 // If we didn't find a register that is spillable, try aliases?
Evan Cheng3b6d56c2006-05-12 19:07:46 +0000575 if (!minReg) {
576 for (TargetRegisterClass::iterator i = RC->allocation_order_begin(*mf_),
577 e = RC->allocation_order_end(*mf_); i != e; ++i) {
578 unsigned reg = *i;
579 // No need to worry about if the alias register size < regsize of RC.
580 // We are going to spill all registers that alias it anyway.
581 for (const unsigned* as = mri_->getAliasSet(reg); *as; ++as) {
582 if (minWeight > SpillWeights[*as]) {
583 minWeight = SpillWeights[*as];
584 minReg = *as;
585 }
586 }
587 }
588
589 // All registers must have inf weight. Just grab one!
590 if (!minReg)
591 minReg = *RC->allocation_order_begin(*mf_);
592 }
Chris Lattnerc8e2c552006-03-25 23:00:56 +0000593
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000594 DOUT << "\t\tregister with min weight: "
595 << mri_->getName(minReg) << " (" << minWeight << ")\n";
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000596
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000597 // if the current has the minimum weight, we need to spill it and
598 // add any added intervals back to unhandled, and restart
599 // linearscan.
Jim Laskey7902c752006-11-07 12:25:45 +0000600 if (cur->weight != HUGE_VALF && cur->weight <= minWeight) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000601 DOUT << "\t\t\tspilling(c): " << *cur << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000602 int slot = vrm_->assignVirt2StackSlot(cur->reg);
603 std::vector<LiveInterval*> added =
604 li_->addIntervalsForSpills(*cur, *vrm_, slot);
605 if (added.empty())
606 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000607
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000608 // Merge added with unhandled. Note that we know that
609 // addIntervalsForSpills returns intervals sorted by their starting
610 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000611 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000612 unhandled_.push(added[i]);
613 return;
614 }
615
Chris Lattner19828d42004-11-18 03:49:30 +0000616 ++NumBacktracks;
617
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000618 // push the current interval back to unhandled since we are going
619 // to re-run at least this iteration. Since we didn't modify it it
620 // should go back right in the front of the list
621 unhandled_.push(cur);
622
623 // otherwise we spill all intervals aliasing the register with
624 // minimum weight, rollback to the interval with the earliest
625 // start point and let the linear scan algorithm run again
626 std::vector<LiveInterval*> added;
627 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
628 "did not choose a register to spill?");
629 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000630
631 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000632 toSpill[minReg] = true;
633 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
634 toSpill[*as] = true;
635
636 // the earliest start of a spilled interval indicates up to where
637 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000638 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000639
640 // set of spilled vregs (used later to rollback properly)
641 std::set<unsigned> spilled;
642
Chris Lattner19828d42004-11-18 03:49:30 +0000643 // spill live intervals of virtual regs mapped to the physical register we
644 // want to clear (and its aliases). We only spill those that overlap with the
645 // current interval as the rest do not affect its allocation. we also keep
646 // track of the earliest start of all spilled live intervals since this will
647 // mark our rollback point.
648 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000649 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000650 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000651 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000652 cur->overlapsFrom(*i->first, i->second)) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000653 DOUT << "\t\t\tspilling(a): " << *i->first << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000654 earliestStart = std::min(earliestStart, i->first->beginNumber());
655 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000656 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000657 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000658 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
659 spilled.insert(reg);
660 }
661 }
Chris Lattner19828d42004-11-18 03:49:30 +0000662 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000663 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000664 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000665 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000666 cur->overlapsFrom(*i->first, i->second-1)) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000667 DOUT << "\t\t\tspilling(i): " << *i->first << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000668 earliestStart = std::min(earliestStart, i->first->beginNumber());
669 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000670 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000671 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000672 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
673 spilled.insert(reg);
674 }
675 }
676
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000677 DOUT << "\t\trolling back to: " << earliestStart << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000678
679 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000680 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000681 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000682 while (!handled_.empty()) {
683 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000684 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000685 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000686 break;
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000687 DOUT << "\t\t\tundo changes for: " << *i << '\n';
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000688 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000689
690 // When undoing a live interval allocation we must know if it is active or
691 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000692 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000693 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000694 active_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000695 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
696 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000697 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000698 prt_->delRegUse(vrm_->getPhys(i->reg));
699 vrm_->clearVirt(i->reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000700 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000701 inactive_.erase(it);
Chris Lattnerffab4222006-02-23 06:44:17 +0000702 assert(!MRegisterInfo::isPhysicalRegister(i->reg));
703 if (!spilled.count(i->reg))
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000704 unhandled_.push(i);
Chris Lattnerffab4222006-02-23 06:44:17 +0000705 vrm_->clearVirt(i->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000706 } else {
707 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
708 "Can only allocate virtual registers!");
709 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000710 unhandled_.push(i);
711 }
712 }
713
Chris Lattner19828d42004-11-18 03:49:30 +0000714 // Rewind the iterators in the active, inactive, and fixed lists back to the
715 // point we reverted to.
716 RevertVectorIteratorsTo(active_, earliestStart);
717 RevertVectorIteratorsTo(inactive_, earliestStart);
718 RevertVectorIteratorsTo(fixed_, earliestStart);
719
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000720 // scan the rest and undo each interval that expired after t and
721 // insert it in active (the next iteration of the algorithm will
722 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000723 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
724 LiveInterval *HI = handled_[i];
725 if (!HI->expiredAt(earliestStart) &&
726 HI->expiredAt(cur->beginNumber())) {
Bill Wendling54fcc7f2006-11-17 00:50:36 +0000727 DOUT << "\t\t\tundo changes for: " << *HI << '\n';
Chris Lattnercbb56252004-11-18 02:42:27 +0000728 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerffab4222006-02-23 06:44:17 +0000729 assert(!MRegisterInfo::isPhysicalRegister(HI->reg));
730 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000731 }
732 }
733
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000734 // merge added with unhandled
735 for (unsigned i = 0, e = added.size(); i != e; ++i)
736 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000737}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000738
Chris Lattnercbb56252004-11-18 02:42:27 +0000739/// getFreePhysReg - return a free physical register for this virtual register
740/// interval if we have one, otherwise return 0.
Chris Lattnerffab4222006-02-23 06:44:17 +0000741unsigned RA::getFreePhysReg(LiveInterval *cur) {
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000742 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattnerf8355d92005-08-22 16:55:22 +0000743 unsigned MaxInactiveCount = 0;
744
Chris Lattnerb9805782005-08-23 22:27:31 +0000745 const TargetRegisterClass *RC = mf_->getSSARegMap()->getRegClass(cur->reg);
746 const TargetRegisterClass *RCLeader = RelatedRegClasses.getLeaderValue(RC);
747
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000748 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
749 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000750 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000751 assert(MRegisterInfo::isVirtualRegister(reg) &&
752 "Can only allocate virtual registers!");
Chris Lattnerb9805782005-08-23 22:27:31 +0000753
754 // If this is not in a related reg class to the register we're allocating,
755 // don't check it.
756 const TargetRegisterClass *RegRC = mf_->getSSARegMap()->getRegClass(reg);
757 if (RelatedRegClasses.getLeaderValue(RegRC) == RCLeader) {
758 reg = vrm_->getPhys(reg);
759 ++inactiveCounts[reg];
760 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
761 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000762 }
763
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000764 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000765
Chris Lattnerf8355d92005-08-22 16:55:22 +0000766 unsigned FreeReg = 0;
767 unsigned FreeRegInactiveCount = 0;
768
769 // Scan for the first available register.
770 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
771 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
772 for (; I != E; ++I)
773 if (prt_->isRegAvail(*I)) {
774 FreeReg = *I;
775 FreeRegInactiveCount = inactiveCounts[FreeReg];
776 break;
777 }
778
779 // If there are no free regs, or if this reg has the max inactive count,
780 // return this register.
781 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
782
783 // Continue scanning the registers, looking for the one with the highest
784 // inactive count. Alkis found that this reduced register pressure very
785 // slightly on X86 (in rev 1.94 of this file), though this should probably be
786 // reevaluated now.
787 for (; I != E; ++I) {
788 unsigned Reg = *I;
789 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
790 FreeReg = Reg;
791 FreeRegInactiveCount = inactiveCounts[Reg];
792 if (FreeRegInactiveCount == MaxInactiveCount)
793 break; // We found the one with the max inactive count.
794 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000795 }
Chris Lattnerf8355d92005-08-22 16:55:22 +0000796
797 return FreeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000798}
799
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000800FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000801 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000802}