blob: 61cc11e413d23f9d58f2338ef8169194570ff787 [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"
15#include "llvm/Function.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000016#include "llvm/CodeGen/MachineFunctionPass.h"
17#include "llvm/CodeGen/MachineInstr.h"
18#include "llvm/CodeGen/Passes.h"
19#include "llvm/CodeGen/SSARegMap.h"
20#include "llvm/Target/MRegisterInfo.h"
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000021#include "llvm/Target/TargetMachine.h"
Reid Spencer551ccae2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Chris Lattnera3b8b5c2004-07-23 17:56:30 +000025#include "LiveIntervalAnalysis.h"
Alkis Evlogimenos888b1a62004-02-23 00:53:31 +000026#include "PhysRegTracker.h"
Alkis Evlogimenos34d9bc92004-02-23 23:08:11 +000027#include "VirtRegMap.h"
Alkis Evlogimenos843b1602004-02-15 10:24:21 +000028#include <algorithm>
Alkis Evlogimenos880e8e42004-05-08 03:50:03 +000029#include <cmath>
Alkis Evlogimenos26f5a692004-05-30 07:24:39 +000030#include <set>
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +000031#include <queue>
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000032using namespace llvm;
33
34namespace {
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000035
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000036 Statistic<double> efficiency
37 ("regalloc", "Ratio of intervals processed over total intervals");
Chris Lattner19828d42004-11-18 03:49:30 +000038 Statistic<> NumBacktracks("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenosd55b2b12004-07-04 07:59:06 +000039
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000040 static unsigned numIterations = 0;
41 static unsigned numIntervals = 0;
Alkis Evlogimenosc1560952004-07-04 17:23:35 +000042
Chris Lattnercbb56252004-11-18 02:42:27 +000043 struct RA : public MachineFunctionPass {
44 typedef std::pair<LiveInterval*, LiveInterval::iterator> IntervalPtr;
45 typedef std::vector<IntervalPtr> IntervalPtrs;
46 private:
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000047 MachineFunction* mf_;
48 const TargetMachine* tm_;
49 const MRegisterInfo* mri_;
50 LiveIntervals* li_;
Chris Lattnerb0f31bf2005-01-23 22:45:13 +000051 bool *PhysRegsUsed;
Chris Lattnercbb56252004-11-18 02:42:27 +000052
53 /// handled_ - Intervals are added to the handled_ set in the order of their
54 /// start value. This is uses for backtracking.
55 std::vector<LiveInterval*> handled_;
56
57 /// fixed_ - Intervals that correspond to machine registers.
58 ///
59 IntervalPtrs fixed_;
60
61 /// active_ - Intervals that are currently being processed, and which have a
62 /// live range active for the current point.
63 IntervalPtrs active_;
64
65 /// inactive_ - Intervals that are currently being processed, but which have
66 /// a hold at the current point.
67 IntervalPtrs inactive_;
68
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000069 typedef std::priority_queue<LiveInterval*,
Chris Lattnercbb56252004-11-18 02:42:27 +000070 std::vector<LiveInterval*>,
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000071 greater_ptr<LiveInterval> > IntervalHeap;
72 IntervalHeap unhandled_;
73 std::auto_ptr<PhysRegTracker> prt_;
74 std::auto_ptr<VirtRegMap> vrm_;
75 std::auto_ptr<Spiller> spiller_;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +000076
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000077 public:
78 virtual const char* getPassName() const {
79 return "Linear Scan Register Allocator";
80 }
81
82 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000083 AU.addRequired<LiveIntervals>();
84 MachineFunctionPass::getAnalysisUsage(AU);
85 }
86
87 /// runOnMachineFunction - register allocate the whole function
88 bool runOnMachineFunction(MachineFunction&);
89
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000090 private:
91 /// linearScan - the linear scan algorithm
92 void linearScan();
93
Chris Lattnercbb56252004-11-18 02:42:27 +000094 /// initIntervalSets - initialize the interval sets.
95 ///
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +000096 void initIntervalSets();
97
Chris Lattnercbb56252004-11-18 02:42:27 +000098 /// processActiveIntervals - expire old intervals and move non-overlapping
99 /// ones to the inactive list.
100 void processActiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000101
Chris Lattnercbb56252004-11-18 02:42:27 +0000102 /// processInactiveIntervals - expire old intervals and move overlapping
103 /// ones to the active list.
104 void processInactiveIntervals(unsigned CurPoint);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000105
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000106 /// assignRegOrStackSlotAtInterval - assign a register if one
107 /// is available, or spill.
108 void assignRegOrStackSlotAtInterval(LiveInterval* cur);
109
110 ///
111 /// register handling helpers
112 ///
113
Chris Lattnercbb56252004-11-18 02:42:27 +0000114 /// getFreePhysReg - return a free physical register for this virtual
115 /// register interval if we have one, otherwise return 0.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000116 unsigned getFreePhysReg(LiveInterval* cur);
117
118 /// assignVirt2StackSlot - assigns this virtual register to a
119 /// stack slot. returns the stack slot
120 int assignVirt2StackSlot(unsigned virtReg);
121
122 template <typename ItTy>
123 void printIntervals(const char* const str, ItTy i, ItTy e) const {
124 if (str) std::cerr << str << " intervals:\n";
125 for (; i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000126 std::cerr << "\t" << *i->first << " -> ";
127 unsigned reg = i->first->reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000128 if (MRegisterInfo::isVirtualRegister(reg)) {
129 reg = vrm_->getPhys(reg);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000130 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000131 std::cerr << mri_->getName(reg) << '\n';
132 }
133 }
134 };
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000135}
136
137bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000138 mf_ = &fn;
139 tm_ = &fn.getTarget();
140 mri_ = tm_->getRegisterInfo();
141 li_ = &getAnalysis<LiveIntervals>();
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000142
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000143 PhysRegsUsed = new bool[mri_->getNumRegs()];
144 std::fill(PhysRegsUsed, PhysRegsUsed+mri_->getNumRegs(), false);
145 fn.setUsedPhysRegs(PhysRegsUsed);
146
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000147 if (!prt_.get()) prt_.reset(new PhysRegTracker(*mri_));
148 vrm_.reset(new VirtRegMap(*mf_));
149 if (!spiller_.get()) spiller_.reset(createSpiller());
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000150
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000151 initIntervalSets();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000152
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000153 linearScan();
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000154
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000155 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000156 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000157
Chris Lattner510a3ea2004-09-30 02:02:33 +0000158 vrm_.reset(); // Free the VirtRegMap
Chris Lattnercbb56252004-11-18 02:42:27 +0000159
160
161 while (!unhandled_.empty()) unhandled_.pop();
162 fixed_.clear();
163 active_.clear();
164 inactive_.clear();
165 handled_.clear();
166
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000167 return true;
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000168}
169
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000170/// initIntervalSets - initialize the interval sets.
171///
172void RA::initIntervalSets()
173{
174 assert(unhandled_.empty() && fixed_.empty() &&
175 active_.empty() && inactive_.empty() &&
176 "interval sets should be empty on initialization");
177
178 for (LiveIntervals::iterator i = li_->begin(), e = li_->end(); i != e; ++i) {
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000179 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
180 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000181 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerb0f31bf2005-01-23 22:45:13 +0000182 } else
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000183 unhandled_.push(&i->second);
184 }
185}
186
Alkis Evlogimenos0d6c5b62004-02-24 08:58:30 +0000187void RA::linearScan()
188{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000189 // linear scan algorithm
190 DEBUG(std::cerr << "********** LINEAR SCAN **********\n");
191 DEBUG(std::cerr << "********** Function: "
192 << mf_->getFunction()->getName() << '\n');
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000193
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000194 // DEBUG(printIntervals("unhandled", unhandled_.begin(), unhandled_.end()));
195 DEBUG(printIntervals("fixed", fixed_.begin(), fixed_.end()));
196 DEBUG(printIntervals("active", active_.begin(), active_.end()));
197 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
198
199 while (!unhandled_.empty()) {
200 // pick the interval with the earliest start point
201 LiveInterval* cur = unhandled_.top();
202 unhandled_.pop();
203 ++numIterations;
204 DEBUG(std::cerr << "\n*** CURRENT ***: " << *cur << '\n');
205
Chris Lattnercbb56252004-11-18 02:42:27 +0000206 processActiveIntervals(cur->beginNumber());
207 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000208
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000209 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
210 "Can only allocate virtual registers!");
Misha Brukmanedf128a2005-04-21 22:36:52 +0000211
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000212 // Allocating a virtual register. try to find a free
213 // physical register or spill an interval (possibly this one) in order to
214 // assign it one.
215 assignRegOrStackSlotAtInterval(cur);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000216
Alkis Evlogimenos39a0d5c2004-02-20 06:15:40 +0000217 DEBUG(printIntervals("active", active_.begin(), active_.end()));
218 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000219 }
220 numIntervals += li_->getNumIntervals();
221 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000222
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000223 // expire any remaining active intervals
224 for (IntervalPtrs::reverse_iterator
225 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000226 unsigned reg = i->first->reg;
227 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000228 assert(MRegisterInfo::isVirtualRegister(reg) &&
229 "Can only allocate virtual registers!");
230 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000231 prt_->delRegUse(reg);
232 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
233 }
Alkis Evlogimenos7d629b52004-01-07 09:20:58 +0000234
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000235 // expire any remaining inactive intervals
236 for (IntervalPtrs::reverse_iterator
237 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000238 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000239 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
240 }
Alkis Evlogimenosb7be1152004-01-13 20:42:08 +0000241
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000242 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000243}
244
Chris Lattnercbb56252004-11-18 02:42:27 +0000245/// processActiveIntervals - expire old intervals and move non-overlapping ones
246/// to the inactive list.
247void RA::processActiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000248{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000249 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattner23b71c12004-11-18 01:29:39 +0000250
Chris Lattnercbb56252004-11-18 02:42:27 +0000251 for (unsigned i = 0, e = active_.size(); i != e; ++i) {
252 LiveInterval *Interval = active_[i].first;
253 LiveInterval::iterator IntervalPos = active_[i].second;
254 unsigned reg = Interval->reg;
Alkis Evlogimenosed543732004-09-01 22:52:29 +0000255
Chris Lattnercbb56252004-11-18 02:42:27 +0000256 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
257
258 if (IntervalPos == Interval->end()) { // Remove expired intervals.
259 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000260 assert(MRegisterInfo::isVirtualRegister(reg) &&
261 "Can only allocate virtual registers!");
262 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000263 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000264
265 // Pop off the end of the list.
266 active_[i] = active_.back();
267 active_.pop_back();
268 --i; --e;
Misha Brukmanedf128a2005-04-21 22:36:52 +0000269
Chris Lattnercbb56252004-11-18 02:42:27 +0000270 } else if (IntervalPos->start > CurPoint) {
271 // Move inactive intervals to inactive list.
272 DEBUG(std::cerr << "\t\tinterval " << *Interval << " inactive\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000273 assert(MRegisterInfo::isVirtualRegister(reg) &&
274 "Can only allocate virtual registers!");
275 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000276 prt_->delRegUse(reg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000277 // add to inactive.
278 inactive_.push_back(std::make_pair(Interval, IntervalPos));
279
280 // Pop off the end of the list.
281 active_[i] = active_.back();
282 active_.pop_back();
283 --i; --e;
284 } else {
285 // Otherwise, just update the iterator position.
286 active_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000287 }
288 }
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000289}
290
Chris Lattnercbb56252004-11-18 02:42:27 +0000291/// processInactiveIntervals - expire old intervals and move overlapping
292/// ones to the active list.
293void RA::processInactiveIntervals(unsigned CurPoint)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000294{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000295 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner365b95f2004-11-18 04:13:02 +0000296
Chris Lattnercbb56252004-11-18 02:42:27 +0000297 for (unsigned i = 0, e = inactive_.size(); i != e; ++i) {
298 LiveInterval *Interval = inactive_[i].first;
299 LiveInterval::iterator IntervalPos = inactive_[i].second;
300 unsigned reg = Interval->reg;
Chris Lattner23b71c12004-11-18 01:29:39 +0000301
Chris Lattnercbb56252004-11-18 02:42:27 +0000302 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukmanedf128a2005-04-21 22:36:52 +0000303
Chris Lattnercbb56252004-11-18 02:42:27 +0000304 if (IntervalPos == Interval->end()) { // remove expired intervals.
305 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000306
Chris Lattnercbb56252004-11-18 02:42:27 +0000307 // Pop off the end of the list.
308 inactive_[i] = inactive_.back();
309 inactive_.pop_back();
310 --i; --e;
311 } else if (IntervalPos->start <= CurPoint) {
312 // move re-activated intervals in active list
313 DEBUG(std::cerr << "\t\tinterval " << *Interval << " active\n");
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000314 assert(MRegisterInfo::isVirtualRegister(reg) &&
315 "Can only allocate virtual registers!");
316 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000317 prt_->addRegUse(reg);
318 // add to active
Chris Lattnercbb56252004-11-18 02:42:27 +0000319 active_.push_back(std::make_pair(Interval, IntervalPos));
320
321 // Pop off the end of the list.
322 inactive_[i] = inactive_.back();
323 inactive_.pop_back();
324 --i; --e;
325 } else {
326 // Otherwise, just update the iterator position.
327 inactive_[i].second = IntervalPos;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000328 }
329 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000330}
331
Chris Lattnercbb56252004-11-18 02:42:27 +0000332/// updateSpillWeights - updates the spill weights of the specifed physical
333/// register and its weight.
Misha Brukmanedf128a2005-04-21 22:36:52 +0000334static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000335 unsigned reg, float weight,
336 const MRegisterInfo *MRI) {
337 Weights[reg] += weight;
338 for (const unsigned* as = MRI->getAliasSet(reg); *as; ++as)
339 Weights[*as] += weight;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000340}
341
Chris Lattnercbb56252004-11-18 02:42:27 +0000342static RA::IntervalPtrs::iterator FindIntervalInVector(RA::IntervalPtrs &IP,
343 LiveInterval *LI) {
344 for (RA::IntervalPtrs::iterator I = IP.begin(), E = IP.end(); I != E; ++I)
345 if (I->first == LI) return I;
346 return IP.end();
347}
348
Chris Lattner19828d42004-11-18 03:49:30 +0000349static void RevertVectorIteratorsTo(RA::IntervalPtrs &V, unsigned Point) {
350 for (unsigned i = 0, e = V.size(); i != e; ++i) {
351 RA::IntervalPtr &IP = V[i];
352 LiveInterval::iterator I = std::upper_bound(IP.first->begin(),
353 IP.second, Point);
354 if (I != IP.first->begin()) --I;
355 IP.second = I;
356 }
357}
Chris Lattnercbb56252004-11-18 02:42:27 +0000358
359
360/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
361/// spill.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000362void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000363{
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000364 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000365
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000366 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000367
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000368 std::vector<float> SpillWeights;
369 SpillWeights.assign(mri_->getNumRegs(), 0.0);
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000370
Chris Lattner365b95f2004-11-18 04:13:02 +0000371 unsigned StartPosition = cur->beginNumber();
372
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000373 // for each interval in active, update spill weights.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000374 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
375 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000376 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000377 assert(MRegisterInfo::isVirtualRegister(reg) &&
378 "Can only allocate virtual registers!");
379 reg = vrm_->getPhys(reg);
380 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000381 }
382
383 // for every interval in inactive we overlap with, mark the
384 // register as not free and update spill weights
385 for (IntervalPtrs::const_iterator i = inactive_.begin(),
386 e = inactive_.end(); i != e; ++i) {
Chris Lattner19828d42004-11-18 03:49:30 +0000387 if (cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000388 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000389 assert(MRegisterInfo::isVirtualRegister(reg) &&
390 "Can only allocate virtual registers!");
391 reg = vrm_->getPhys(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000392 prt_->addRegUse(reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000393 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000394 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000395 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000396
Chris Lattner19828d42004-11-18 03:49:30 +0000397 // For every interval in fixed we overlap with, mark the register as not free
398 // and update spill weights.
Chris Lattner365b95f2004-11-18 04:13:02 +0000399 for (unsigned i = 0, e = fixed_.size(); i != e; ++i) {
400 IntervalPtr &IP = fixed_[i];
401 LiveInterval *I = IP.first;
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000402 if (I->endNumber() > StartPosition) {
403 LiveInterval::iterator II = I->advanceTo(IP.second, StartPosition);
404 IP.second = II;
405 if (II != I->begin() && II->start > StartPosition)
406 --II;
407 if (cur->overlapsFrom(*I, II)) {
408 unsigned reg = I->reg;
409 prt_->addRegUse(reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000410 updateSpillWeights(SpillWeights, reg, I->weight, mri_);
Chris Lattnerf348e3a2004-11-18 04:33:31 +0000411 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000412 }
Chris Lattner365b95f2004-11-18 04:13:02 +0000413 }
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000414
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000415 unsigned physReg = getFreePhysReg(cur);
416 // restore the physical register tracker
417 *prt_ = backupPrt;
418 // if we find a free register, we are done: assign this virtual to
419 // the free physical register and add this interval to the active
420 // list.
421 if (physReg) {
422 DEBUG(std::cerr << mri_->getName(physReg) << '\n');
423 vrm_->assignVirt2Phys(cur->reg, physReg);
424 prt_->addRegUse(physReg);
Chris Lattnercbb56252004-11-18 02:42:27 +0000425 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000426 handled_.push_back(cur);
427 return;
428 }
429 DEBUG(std::cerr << "no free registers\n");
430
431 DEBUG(std::cerr << "\tassigning stack slot at interval "<< *cur << ":\n");
432
Chris Lattner5e5fb942005-01-08 19:53:50 +0000433 float minWeight = float(HUGE_VAL);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000434 unsigned minReg = 0;
435 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Chris Lattner5b210342004-12-15 07:04:32 +0000436 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_),
437 e = rc->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000438 unsigned reg = *i;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000439 if (minWeight > SpillWeights[reg]) {
440 minWeight = SpillWeights[reg];
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000441 minReg = reg;
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000442 }
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000443 }
444 DEBUG(std::cerr << "\t\tregister with min weight: "
445 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos3bf564a2003-12-23 18:00:33 +0000446
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000447 // if the current has the minimum weight, we need to spill it and
448 // add any added intervals back to unhandled, and restart
449 // linearscan.
450 if (cur->weight <= minWeight) {
451 DEBUG(std::cerr << "\t\t\tspilling(c): " << *cur << '\n';);
452 int slot = vrm_->assignVirt2StackSlot(cur->reg);
453 std::vector<LiveInterval*> added =
454 li_->addIntervalsForSpills(*cur, *vrm_, slot);
455 if (added.empty())
456 return; // Early exit if all spills were folded.
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000457
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000458 // Merge added with unhandled. Note that we know that
459 // addIntervalsForSpills returns intervals sorted by their starting
460 // point.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000461 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000462 unhandled_.push(added[i]);
463 return;
464 }
465
Chris Lattner19828d42004-11-18 03:49:30 +0000466 ++NumBacktracks;
467
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000468 // push the current interval back to unhandled since we are going
469 // to re-run at least this iteration. Since we didn't modify it it
470 // should go back right in the front of the list
471 unhandled_.push(cur);
472
473 // otherwise we spill all intervals aliasing the register with
474 // minimum weight, rollback to the interval with the earliest
475 // start point and let the linear scan algorithm run again
476 std::vector<LiveInterval*> added;
477 assert(MRegisterInfo::isPhysicalRegister(minReg) &&
478 "did not choose a register to spill?");
479 std::vector<bool> toSpill(mri_->getNumRegs(), false);
Chris Lattner19828d42004-11-18 03:49:30 +0000480
481 // We are going to spill minReg and all its aliases.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000482 toSpill[minReg] = true;
483 for (const unsigned* as = mri_->getAliasSet(minReg); *as; ++as)
484 toSpill[*as] = true;
485
486 // the earliest start of a spilled interval indicates up to where
487 // in handled we need to roll back
Chris Lattner23b71c12004-11-18 01:29:39 +0000488 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000489
490 // set of spilled vregs (used later to rollback properly)
491 std::set<unsigned> spilled;
492
Chris Lattner19828d42004-11-18 03:49:30 +0000493 // spill live intervals of virtual regs mapped to the physical register we
494 // want to clear (and its aliases). We only spill those that overlap with the
495 // current interval as the rest do not affect its allocation. we also keep
496 // track of the earliest start of all spilled live intervals since this will
497 // mark our rollback point.
498 for (IntervalPtrs::iterator i = active_.begin(); i != active_.end(); ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000499 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000500 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000501 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000502 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000503 DEBUG(std::cerr << "\t\t\tspilling(a): " << *i->first << '\n');
504 earliestStart = std::min(earliestStart, i->first->beginNumber());
505 int slot = vrm_->assignVirt2StackSlot(i->first->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000506 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000507 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000508 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
509 spilled.insert(reg);
510 }
511 }
Chris Lattner19828d42004-11-18 03:49:30 +0000512 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnercbb56252004-11-18 02:42:27 +0000513 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000514 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000515 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner19828d42004-11-18 03:49:30 +0000516 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000517 DEBUG(std::cerr << "\t\t\tspilling(i): " << *i->first << '\n');
518 earliestStart = std::min(earliestStart, i->first->beginNumber());
519 int slot = vrm_->assignVirt2StackSlot(reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000520 std::vector<LiveInterval*> newIs =
Chris Lattnercbb56252004-11-18 02:42:27 +0000521 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000522 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
523 spilled.insert(reg);
524 }
525 }
526
527 DEBUG(std::cerr << "\t\trolling back to: " << earliestStart << '\n');
Chris Lattnercbb56252004-11-18 02:42:27 +0000528
529 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000530 // spilled live interval and undo each one, restoring the state of
Chris Lattnercbb56252004-11-18 02:42:27 +0000531 // unhandled.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000532 while (!handled_.empty()) {
533 LiveInterval* i = handled_.back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000534 // If this interval starts before t we are done.
Chris Lattner23b71c12004-11-18 01:29:39 +0000535 if (i->beginNumber() < earliestStart)
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000536 break;
537 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
538 handled_.pop_back();
Chris Lattnercbb56252004-11-18 02:42:27 +0000539
540 // When undoing a live interval allocation we must know if it is active or
541 // inactive to properly update the PhysRegTracker and the VirtRegMap.
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000542 IntervalPtrs::iterator it;
Chris Lattnercbb56252004-11-18 02:42:27 +0000543 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000544 active_.erase(it);
545 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000546 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000547 prt_->delRegUse(i->reg);
548 unhandled_.push(i);
Chris Lattnercbb56252004-11-18 02:42:27 +0000549 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000550 if (!spilled.count(i->reg))
551 unhandled_.push(i);
552 prt_->delRegUse(vrm_->getPhys(i->reg));
553 vrm_->clearVirt(i->reg);
554 }
Chris Lattnercbb56252004-11-18 02:42:27 +0000555 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000556 inactive_.erase(it);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000557 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
558 assert(0 && "daksjlfd");
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000559 unhandled_.push(i);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000560 } else {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000561 if (!spilled.count(i->reg))
562 unhandled_.push(i);
563 vrm_->clearVirt(i->reg);
564 }
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000565 } else {
566 assert(MRegisterInfo::isVirtualRegister(i->reg) &&
567 "Can only allocate virtual registers!");
568 vrm_->clearVirt(i->reg);
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000569 unhandled_.push(i);
570 }
571 }
572
Chris Lattner19828d42004-11-18 03:49:30 +0000573 // Rewind the iterators in the active, inactive, and fixed lists back to the
574 // point we reverted to.
575 RevertVectorIteratorsTo(active_, earliestStart);
576 RevertVectorIteratorsTo(inactive_, earliestStart);
577 RevertVectorIteratorsTo(fixed_, earliestStart);
578
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000579 // scan the rest and undo each interval that expired after t and
580 // insert it in active (the next iteration of the algorithm will
581 // put it in inactive if required)
Chris Lattnercbb56252004-11-18 02:42:27 +0000582 for (unsigned i = 0, e = handled_.size(); i != e; ++i) {
583 LiveInterval *HI = handled_[i];
584 if (!HI->expiredAt(earliestStart) &&
585 HI->expiredAt(cur->beginNumber())) {
586 DEBUG(std::cerr << "\t\t\tundo changes for: " << *HI << '\n');
587 active_.push_back(std::make_pair(HI, HI->begin()));
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000588 if (MRegisterInfo::isPhysicalRegister(HI->reg)) {
589 assert(0 &&"sdflkajsdf");
Chris Lattnercbb56252004-11-18 02:42:27 +0000590 prt_->addRegUse(HI->reg);
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000591 } else
Chris Lattnercbb56252004-11-18 02:42:27 +0000592 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000593 }
594 }
595
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000596 // merge added with unhandled
597 for (unsigned i = 0, e = added.size(); i != e; ++i)
598 unhandled_.push(added[i]);
Alkis Evlogimenos843b1602004-02-15 10:24:21 +0000599}
Alkis Evlogimenosf5eaf162004-02-06 18:08:18 +0000600
Chris Lattnercbb56252004-11-18 02:42:27 +0000601/// getFreePhysReg - return a free physical register for this virtual register
602/// interval if we have one, otherwise return 0.
Alkis Evlogimenos53eb3732004-07-22 08:14:44 +0000603unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenos169cfd02003-12-21 05:43:40 +0000604{
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000605 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
606 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
607 i != e; ++i) {
Chris Lattnercbb56252004-11-18 02:42:27 +0000608 unsigned reg = i->first->reg;
Chris Lattnerc8b9f332004-11-18 06:01:45 +0000609 assert(MRegisterInfo::isVirtualRegister(reg) &&
610 "Can only allocate virtual registers!");
611 reg = vrm_->getPhys(reg);
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000612 ++inactiveCounts[reg];
613 }
614
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000615 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos26bfc082003-12-28 17:58:18 +0000616
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000617 unsigned freeReg = 0;
Nate Begeman80ad87d2004-12-07 05:25:53 +0000618 for (TargetRegisterClass::iterator i = rc->allocation_order_begin(*mf_),
619 e = rc->allocation_order_end(*mf_); i != e; ++i) {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000620 unsigned reg = *i;
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000621 if (prt_->isRegAvail(reg) &&
622 (!freeReg || inactiveCounts[freeReg] < inactiveCounts[reg]))
623 freeReg = reg;
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000624 }
Alkis Evlogimenos84f5bcb2004-09-02 21:23:32 +0000625 return freeReg;
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000626}
627
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000628FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenos1a8ea012004-08-04 09:46:26 +0000629 return new RA();
Alkis Evlogimenosff0cbe12003-11-20 03:32:25 +0000630}