blob: e85826c04775756a88c892c9a4102d4271121c87 [file] [log] [blame]
Alkis Evlogimenos0e9ded72003-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 Evlogimenos1dd872c2004-02-24 08:58:30 +000013
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000014#define DEBUG_TYPE "regalloc"
15#include "llvm/Function.h"
Alkis Evlogimenos0e9ded72003-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 Evlogimenos0e9ded72003-11-20 03:32:25 +000021#include "llvm/Target/TargetMachine.h"
Reid Spencer7c16caa2004-09-01 22:55:40 +000022#include "llvm/Support/Debug.h"
23#include "llvm/ADT/Statistic.h"
24#include "llvm/ADT/STLExtras.h"
Chris Lattner85638332004-07-23 17:56:30 +000025#include "LiveIntervalAnalysis.h"
Alkis Evlogimenos14108592004-02-23 00:53:31 +000026#include "PhysRegTracker.h"
Alkis Evlogimenosc794a902004-02-23 23:08:11 +000027#include "VirtRegMap.h"
Alkis Evlogimenos2c5ddd22004-02-15 10:24:21 +000028#include <algorithm>
Alkis Evlogimenos2a54b5d2004-05-08 03:50:03 +000029#include <cmath>
Alkis Evlogimenosa5268e82004-05-30 07:24:39 +000030#include <set>
Alkis Evlogimenos1a876fa2004-07-22 08:14:44 +000031#include <queue>
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +000032using namespace llvm;
33
34namespace {
Alkis Evlogimenos8f3cc032004-07-04 07:59:06 +000035
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000036 Statistic<double> efficiency
37 ("regalloc", "Ratio of intervals processed over total intervals");
Chris Lattner850852c2004-11-18 03:49:30 +000038 Statistic<> NumBacktracks("regalloc", "Number of times we had to backtrack");
Alkis Evlogimenos8f3cc032004-07-04 07:59:06 +000039
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000040 static unsigned numIterations = 0;
41 static unsigned numIntervals = 0;
Alkis Evlogimenos21b3a5b2004-07-04 17:23:35 +000042
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +000047 MachineFunction* mf_;
48 const TargetMachine* tm_;
49 const MRegisterInfo* mri_;
50 LiveIntervals* li_;
Chris Lattnerae09d932005-01-23 22:45:13 +000051 bool *PhysRegsUsed;
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +000069 typedef std::priority_queue<LiveInterval*,
Chris Lattnera1f77792004-11-18 02:42:27 +000070 std::vector<LiveInterval*>,
Alkis Evlogimenosa6983082004-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 Evlogimenos0e9ded72003-11-20 03:32:25 +000076
Alkis Evlogimenosa6983082004-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 Evlogimenosa6983082004-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 Evlogimenosa6983082004-08-04 09:46:26 +000090 private:
91 /// linearScan - the linear scan algorithm
92 void linearScan();
93
Chris Lattnera1f77792004-11-18 02:42:27 +000094 /// initIntervalSets - initialize the interval sets.
95 ///
Alkis Evlogimenosa6983082004-08-04 09:46:26 +000096 void initIntervalSets();
97
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000101
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000105
Alkis Evlogimenosa6983082004-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 Lattnera1f77792004-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 Evlogimenosa6983082004-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 Lattnera1f77792004-11-18 02:42:27 +0000126 std::cerr << "\t" << *i->first << " -> ";
127 unsigned reg = i->first->reg;
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000128 if (MRegisterInfo::isVirtualRegister(reg)) {
129 reg = vrm_->getPhys(reg);
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000130 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000131 std::cerr << mri_->getName(reg) << '\n';
132 }
133 }
134 };
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000135}
136
137bool RA::runOnMachineFunction(MachineFunction &fn) {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000138 mf_ = &fn;
139 tm_ = &fn.getTarget();
140 mri_ = tm_->getRegisterInfo();
141 li_ = &getAnalysis<LiveIntervals>();
Chris Lattner08ec6032004-11-18 04:33:31 +0000142
Chris Lattnerae09d932005-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 Evlogimenosa6983082004-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 Evlogimenosc09b77e2003-12-21 05:43:40 +0000150
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000151 initIntervalSets();
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000152
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000153 linearScan();
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000154
Chris Lattnerae09d932005-01-23 22:45:13 +0000155 // Rewrite spill code and update the PhysRegsUsed set.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000156 spiller_->runOnMachineFunction(*mf_, *vrm_);
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000157
Chris Lattnerddd52292004-09-30 02:02:33 +0000158 vrm_.reset(); // Free the VirtRegMap
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000167 return true;
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000168}
169
Chris Lattnerb75e7902004-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 Lattnerae09d932005-01-23 22:45:13 +0000179 if (MRegisterInfo::isPhysicalRegister(i->second.reg)) {
180 PhysRegsUsed[i->second.reg] = true;
Chris Lattnerb75e7902004-11-18 06:01:45 +0000181 fixed_.push_back(std::make_pair(&i->second, i->second.begin()));
Chris Lattnerae09d932005-01-23 22:45:13 +0000182 } else
Chris Lattnerb75e7902004-11-18 06:01:45 +0000183 unhandled_.push(&i->second);
184 }
185}
186
Alkis Evlogimenos1dd872c2004-02-24 08:58:30 +0000187void RA::linearScan()
188{
Alkis Evlogimenosa6983082004-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 Evlogimenos0e9ded72003-11-20 03:32:25 +0000193
Alkis Evlogimenosa6983082004-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 Lattnera1f77792004-11-18 02:42:27 +0000206 processActiveIntervals(cur->beginNumber());
207 processInactiveIntervals(cur->beginNumber());
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000208
Chris Lattnerb75e7902004-11-18 06:01:45 +0000209 assert(MRegisterInfo::isVirtualRegister(cur->reg) &&
210 "Can only allocate virtual registers!");
Misha Brukman835702a2005-04-21 22:36:52 +0000211
Chris Lattnerb75e7902004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000216
Alkis Evlogimenos76eca062004-02-20 06:15:40 +0000217 DEBUG(printIntervals("active", active_.begin(), active_.end()));
218 DEBUG(printIntervals("inactive", inactive_.begin(), inactive_.end()));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000219 }
220 numIntervals += li_->getNumIntervals();
221 efficiency = double(numIterations) / double(numIntervals);
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000222
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000223 // expire any remaining active intervals
224 for (IntervalPtrs::reverse_iterator
225 i = active_.rbegin(); i != active_.rend(); ) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000226 unsigned reg = i->first->reg;
227 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Chris Lattnerb75e7902004-11-18 06:01:45 +0000228 assert(MRegisterInfo::isVirtualRegister(reg) &&
229 "Can only allocate virtual registers!");
230 reg = vrm_->getPhys(reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000231 prt_->delRegUse(reg);
232 i = IntervalPtrs::reverse_iterator(active_.erase(i.base()-1));
233 }
Alkis Evlogimenosae5b3d42004-01-07 09:20:58 +0000234
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000235 // expire any remaining inactive intervals
236 for (IntervalPtrs::reverse_iterator
237 i = inactive_.rbegin(); i != inactive_.rend(); ) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000238 DEBUG(std::cerr << "\tinterval " << *i->first << " expired\n");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000239 i = IntervalPtrs::reverse_iterator(inactive_.erase(i.base()-1));
240 }
Alkis Evlogimenos65bc9902004-01-13 20:42:08 +0000241
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000242 DEBUG(std::cerr << *vrm_);
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000243}
244
Chris Lattnera1f77792004-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 Evlogimenos0e9ded72003-11-20 03:32:25 +0000248{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000249 DEBUG(std::cerr << "\tprocessing active intervals:\n");
Chris Lattnera51f5ee2004-11-18 01:29:39 +0000250
Chris Lattnera1f77792004-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 Evlogimenosfae8f6a2004-09-01 22:52:29 +0000255
Chris Lattnera1f77792004-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 Lattnerb75e7902004-11-18 06:01:45 +0000260 assert(MRegisterInfo::isVirtualRegister(reg) &&
261 "Can only allocate virtual registers!");
262 reg = vrm_->getPhys(reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000263 prt_->delRegUse(reg);
Chris Lattnera1f77792004-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 Brukman835702a2005-04-21 22:36:52 +0000269
Chris Lattnera1f77792004-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 Lattnerb75e7902004-11-18 06:01:45 +0000273 assert(MRegisterInfo::isVirtualRegister(reg) &&
274 "Can only allocate virtual registers!");
275 reg = vrm_->getPhys(reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000276 prt_->delRegUse(reg);
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000287 }
288 }
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000289}
290
Chris Lattnera1f77792004-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 Evlogimenos0e9ded72003-11-20 03:32:25 +0000294{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000295 DEBUG(std::cerr << "\tprocessing inactive intervals:\n");
Chris Lattner49ff5f02004-11-18 04:13:02 +0000296
Chris Lattnera1f77792004-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 Lattnera51f5ee2004-11-18 01:29:39 +0000301
Chris Lattnera1f77792004-11-18 02:42:27 +0000302 IntervalPos = Interval->advanceTo(IntervalPos, CurPoint);
Misha Brukman835702a2005-04-21 22:36:52 +0000303
Chris Lattnera1f77792004-11-18 02:42:27 +0000304 if (IntervalPos == Interval->end()) { // remove expired intervals.
305 DEBUG(std::cerr << "\t\tinterval " << *Interval << " expired\n");
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000306
Chris Lattnera1f77792004-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 Lattnerb75e7902004-11-18 06:01:45 +0000314 assert(MRegisterInfo::isVirtualRegister(reg) &&
315 "Can only allocate virtual registers!");
316 reg = vrm_->getPhys(reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000317 prt_->addRegUse(reg);
318 // add to active
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000328 }
329 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000330}
331
Chris Lattnera1f77792004-11-18 02:42:27 +0000332/// updateSpillWeights - updates the spill weights of the specifed physical
333/// register and its weight.
Misha Brukman835702a2005-04-21 22:36:52 +0000334static void updateSpillWeights(std::vector<float> &Weights,
Chris Lattnerb75e7902004-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 Evlogimenos0e9ded72003-11-20 03:32:25 +0000340}
341
Chris Lattnera1f77792004-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 Lattner850852c2004-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 Lattnera1f77792004-11-18 02:42:27 +0000358
359
360/// assignRegOrStackSlotAtInterval - assign a register if one is available, or
361/// spill.
Alkis Evlogimenos1a876fa2004-07-22 08:14:44 +0000362void RA::assignRegOrStackSlotAtInterval(LiveInterval* cur)
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000363{
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000364 DEBUG(std::cerr << "\tallocating current interval: ");
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000365
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000366 PhysRegTracker backupPrt = *prt_;
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000367
Chris Lattnerb75e7902004-11-18 06:01:45 +0000368 std::vector<float> SpillWeights;
369 SpillWeights.assign(mri_->getNumRegs(), 0.0);
Alkis Evlogimenose82a7072004-02-06 18:08:18 +0000370
Chris Lattner49ff5f02004-11-18 04:13:02 +0000371 unsigned StartPosition = cur->beginNumber();
372
Chris Lattnerb75e7902004-11-18 06:01:45 +0000373 // for each interval in active, update spill weights.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000374 for (IntervalPtrs::const_iterator i = active_.begin(), e = active_.end();
375 i != e; ++i) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000376 unsigned reg = i->first->reg;
Chris Lattnerb75e7902004-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 Evlogimenosa6983082004-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 Lattner850852c2004-11-18 03:49:30 +0000387 if (cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000388 unsigned reg = i->first->reg;
Chris Lattnerb75e7902004-11-18 06:01:45 +0000389 assert(MRegisterInfo::isVirtualRegister(reg) &&
390 "Can only allocate virtual registers!");
391 reg = vrm_->getPhys(reg);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000392 prt_->addRegUse(reg);
Chris Lattnerb75e7902004-11-18 06:01:45 +0000393 updateSpillWeights(SpillWeights, reg, i->first->weight, mri_);
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000394 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000395 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000396
Chris Lattner850852c2004-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 Lattner49ff5f02004-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 Lattner08ec6032004-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 Lattnerb75e7902004-11-18 06:01:45 +0000410 updateSpillWeights(SpillWeights, reg, I->weight, mri_);
Chris Lattner08ec6032004-11-18 04:33:31 +0000411 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000412 }
Chris Lattner49ff5f02004-11-18 04:13:02 +0000413 }
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000414
Alkis Evlogimenosa6983082004-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 Lattnera1f77792004-11-18 02:42:27 +0000425 active_.push_back(std::make_pair(cur, cur->begin()));
Alkis Evlogimenosa6983082004-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 Lattner78100c42005-01-08 19:53:50 +0000433 float minWeight = float(HUGE_VAL);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000434 unsigned minReg = 0;
435 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Chris Lattnere09dbe22004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000438 unsigned reg = *i;
Chris Lattnerb75e7902004-11-18 06:01:45 +0000439 if (minWeight > SpillWeights[reg]) {
440 minWeight = SpillWeights[reg];
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000441 minReg = reg;
Alkis Evlogimenos7d7d7e82003-12-23 18:00:33 +0000442 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000443 }
444 DEBUG(std::cerr << "\t\tregister with min weight: "
445 << mri_->getName(minReg) << " (" << minWeight << ")\n");
Alkis Evlogimenos7d7d7e82003-12-23 18:00:33 +0000446
Alkis Evlogimenosa6983082004-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 Evlogimenose82a7072004-02-06 18:08:18 +0000457
Alkis Evlogimenosa6983082004-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 Evlogimenos1a876fa2004-07-22 08:14:44 +0000461 for (unsigned i = 0, e = added.size(); i != e; ++i)
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000462 unhandled_.push(added[i]);
463 return;
464 }
465
Chris Lattner850852c2004-11-18 03:49:30 +0000466 ++NumBacktracks;
467
Alkis Evlogimenosa6983082004-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 Lattner850852c2004-11-18 03:49:30 +0000480
481 // We are going to spill minReg and all its aliases.
Alkis Evlogimenosa6983082004-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 Lattnera51f5ee2004-11-18 01:29:39 +0000488 unsigned earliestStart = cur->beginNumber();
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000489
490 // set of spilled vregs (used later to rollback properly)
491 std::set<unsigned> spilled;
492
Chris Lattner850852c2004-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 Lattnera1f77792004-11-18 02:42:27 +0000499 unsigned reg = i->first->reg;
Chris Lattnerb75e7902004-11-18 06:01:45 +0000500 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000501 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner850852c2004-11-18 03:49:30 +0000502 cur->overlapsFrom(*i->first, i->second)) {
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000506 std::vector<LiveInterval*> newIs =
Chris Lattnera1f77792004-11-18 02:42:27 +0000507 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000508 std::copy(newIs.begin(), newIs.end(), std::back_inserter(added));
509 spilled.insert(reg);
510 }
511 }
Chris Lattner850852c2004-11-18 03:49:30 +0000512 for (IntervalPtrs::iterator i = inactive_.begin(); i != inactive_.end(); ++i){
Chris Lattnera1f77792004-11-18 02:42:27 +0000513 unsigned reg = i->first->reg;
Chris Lattnerb75e7902004-11-18 06:01:45 +0000514 if (//MRegisterInfo::isVirtualRegister(reg) &&
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000515 toSpill[vrm_->getPhys(reg)] &&
Chris Lattner850852c2004-11-18 03:49:30 +0000516 cur->overlapsFrom(*i->first, i->second-1)) {
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000520 std::vector<LiveInterval*> newIs =
Chris Lattnera1f77792004-11-18 02:42:27 +0000521 li_->addIntervalsForSpills(*i->first, *vrm_, slot);
Alkis Evlogimenosa6983082004-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 Lattnera1f77792004-11-18 02:42:27 +0000528
529 // Scan handled in reverse order up to the earliest start of a
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000530 // spilled live interval and undo each one, restoring the state of
Chris Lattnera1f77792004-11-18 02:42:27 +0000531 // unhandled.
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000532 while (!handled_.empty()) {
533 LiveInterval* i = handled_.back();
Chris Lattnera1f77792004-11-18 02:42:27 +0000534 // If this interval starts before t we are done.
Chris Lattnera51f5ee2004-11-18 01:29:39 +0000535 if (i->beginNumber() < earliestStart)
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000536 break;
537 DEBUG(std::cerr << "\t\t\tundo changes for: " << *i << '\n');
538 handled_.pop_back();
Chris Lattnera1f77792004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000542 IntervalPtrs::iterator it;
Chris Lattnera1f77792004-11-18 02:42:27 +0000543 if ((it = FindIntervalInVector(active_, i)) != active_.end()) {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000544 active_.erase(it);
545 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
Chris Lattnerb75e7902004-11-18 06:01:45 +0000546 assert(0 && "daksjlfd");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000547 prt_->delRegUse(i->reg);
548 unhandled_.push(i);
Chris Lattnera1f77792004-11-18 02:42:27 +0000549 } else {
Alkis Evlogimenosa6983082004-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 Lattnera1f77792004-11-18 02:42:27 +0000555 } else if ((it = FindIntervalInVector(inactive_, i)) != inactive_.end()) {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000556 inactive_.erase(it);
Chris Lattnerb75e7902004-11-18 06:01:45 +0000557 if (MRegisterInfo::isPhysicalRegister(i->reg)) {
558 assert(0 && "daksjlfd");
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000559 unhandled_.push(i);
Chris Lattnerb75e7902004-11-18 06:01:45 +0000560 } else {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000561 if (!spilled.count(i->reg))
562 unhandled_.push(i);
563 vrm_->clearVirt(i->reg);
564 }
Chris Lattnerb75e7902004-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 Evlogimenosa6983082004-08-04 09:46:26 +0000569 unhandled_.push(i);
570 }
571 }
572
Chris Lattner850852c2004-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 Evlogimenosa6983082004-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 Lattnera1f77792004-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 Lattnerb75e7902004-11-18 06:01:45 +0000588 if (MRegisterInfo::isPhysicalRegister(HI->reg)) {
589 assert(0 &&"sdflkajsdf");
Chris Lattnera1f77792004-11-18 02:42:27 +0000590 prt_->addRegUse(HI->reg);
Chris Lattnerb75e7902004-11-18 06:01:45 +0000591 } else
Chris Lattnera1f77792004-11-18 02:42:27 +0000592 prt_->addRegUse(vrm_->getPhys(HI->reg));
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000593 }
594 }
595
Alkis Evlogimenosa6983082004-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 Evlogimenos2c5ddd22004-02-15 10:24:21 +0000599}
Alkis Evlogimenose82a7072004-02-06 18:08:18 +0000600
Chris Lattnera1f77792004-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 Evlogimenos1a876fa2004-07-22 08:14:44 +0000603unsigned RA::getFreePhysReg(LiveInterval* cur)
Alkis Evlogimenosc09b77e2003-12-21 05:43:40 +0000604{
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000605 std::vector<unsigned> inactiveCounts(mri_->getNumRegs(), 0);
Chris Lattner83b821b2005-08-22 16:55:22 +0000606 unsigned MaxInactiveCount = 0;
607
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000608 for (IntervalPtrs::iterator i = inactive_.begin(), e = inactive_.end();
609 i != e; ++i) {
Chris Lattnera1f77792004-11-18 02:42:27 +0000610 unsigned reg = i->first->reg;
Chris Lattnerb75e7902004-11-18 06:01:45 +0000611 assert(MRegisterInfo::isVirtualRegister(reg) &&
612 "Can only allocate virtual registers!");
613 reg = vrm_->getPhys(reg);
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000614 ++inactiveCounts[reg];
Chris Lattner83b821b2005-08-22 16:55:22 +0000615 MaxInactiveCount = std::max(MaxInactiveCount, inactiveCounts[reg]);
Alkis Evlogimenos095c3a82004-09-02 21:23:32 +0000616 }
617
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000618 const TargetRegisterClass* rc = mf_->getSSARegMap()->getRegClass(cur->reg);
Alkis Evlogimenos43b587d2003-12-28 17:58:18 +0000619
Chris Lattner83b821b2005-08-22 16:55:22 +0000620 unsigned FreeReg = 0;
621 unsigned FreeRegInactiveCount = 0;
622
623 // Scan for the first available register.
624 TargetRegisterClass::iterator I = rc->allocation_order_begin(*mf_);
625 TargetRegisterClass::iterator E = rc->allocation_order_end(*mf_);
626 for (; I != E; ++I)
627 if (prt_->isRegAvail(*I)) {
628 FreeReg = *I;
629 FreeRegInactiveCount = inactiveCounts[FreeReg];
630 break;
631 }
632
633 // If there are no free regs, or if this reg has the max inactive count,
634 // return this register.
635 if (FreeReg == 0 || FreeRegInactiveCount == MaxInactiveCount) return FreeReg;
636
637 // Continue scanning the registers, looking for the one with the highest
638 // inactive count. Alkis found that this reduced register pressure very
639 // slightly on X86 (in rev 1.94 of this file), though this should probably be
640 // reevaluated now.
641 for (; I != E; ++I) {
642 unsigned Reg = *I;
643 if (prt_->isRegAvail(Reg) && FreeRegInactiveCount < inactiveCounts[Reg]) {
644 FreeReg = Reg;
645 FreeRegInactiveCount = inactiveCounts[Reg];
646 if (FreeRegInactiveCount == MaxInactiveCount)
647 break; // We found the one with the max inactive count.
648 }
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000649 }
Chris Lattner83b821b2005-08-22 16:55:22 +0000650
651 return FreeReg;
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000652}
653
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000654FunctionPass* llvm::createLinearScanRegisterAllocator() {
Alkis Evlogimenosa6983082004-08-04 09:46:26 +0000655 return new RA();
Alkis Evlogimenos0e9ded72003-11-20 03:32:25 +0000656}