blob: 10a02a3ee381dfd44c8574a75e16b735f3c56cf3 [file] [log] [blame]
Jim Stichnothd97c7df2014-06-04 11:57:08 -07001//===- subzero/src/IceRegAlloc.cpp - Linear-scan implementation -----------===//
2//
3// The Subzero Code Generator
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
10// This file implements the LinearScan class, which performs the
11// linear-scan register allocation after liveness analysis has been
12// performed.
13//
14//===----------------------------------------------------------------------===//
15
16#include "IceCfg.h"
17#include "IceInst.h"
18#include "IceOperand.h"
19#include "IceRegAlloc.h"
20#include "IceTargetLowering.h"
21
22namespace Ice {
23
24// Implements the linear-scan algorithm. Based on "Linear Scan
25// Register Allocation in the Context of SSA Form and Register
26// Constraints" by Hanspeter Mössenböck and Michael Pfeiffer,
27// ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe02.PDF . This
28// implementation is modified to take affinity into account and allow
29// two interfering variables to share the same register in certain
30// cases.
31//
32// Requires running Cfg::liveness(Liveness_RangesFull) in
33// preparation. Results are assigned to Variable::RegNum for each
34// Variable.
35void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
36 assert(RegMaskFull.any()); // Sanity check
37 Unhandled.clear();
38 Handled.clear();
39 Inactive.clear();
40 Active.clear();
41 Ostream &Str = Func->getContext()->getStrDump();
42 Func->setCurrentNode(NULL);
43
44 // Gather the live ranges of all variables and add them to the
45 // Unhandled set. TODO: Unhandled is a set<> which is based on a
46 // balanced binary tree, so inserting live ranges for N variables is
47 // O(N log N) complexity. N may be proportional to the number of
48 // instructions, thanks to temporary generation during lowering. As
49 // a result, it may be useful to design a better data structure for
50 // storing Func->getVariables().
51 const VarList &Vars = Func->getVariables();
52 for (VarList::const_iterator I = Vars.begin(), E = Vars.end(); I != E; ++I) {
53 Variable *Var = *I;
54 // Explicitly don't consider zero-weight variables, which are
55 // meant to be spill slots.
56 if (Var->getWeight() == RegWeight::Zero)
57 continue;
58 // Don't bother if the variable has a null live range, which means
59 // it was never referenced.
60 if (Var->getLiveRange().isEmpty())
61 continue;
62 Unhandled.insert(LiveRangeWrapper(Var));
63 if (Var->hasReg()) {
64 Var->setRegNumTmp(Var->getRegNum());
65 Var->setLiveRangeInfiniteWeight();
66 }
67 }
68
69 // RegUses[I] is the number of live ranges (variables) that register
70 // I is currently assigned to. It can be greater than 1 as a result
71 // of Variable::AllowRegisterOverlap.
72 std::vector<int> RegUses(RegMaskFull.size());
73 // Unhandled is already set to all ranges in increasing order of
74 // start points.
75 assert(Active.empty());
76 assert(Inactive.empty());
77 assert(Handled.empty());
78 UnorderedRanges::iterator Next;
79
80 while (!Unhandled.empty()) {
81 LiveRangeWrapper Cur = *Unhandled.begin();
82 Unhandled.erase(Unhandled.begin());
83 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
84 Str << "\nConsidering ";
85 Cur.dump(Func);
86 Str << "\n";
87 }
88 const llvm::SmallBitVector RegMask =
89 RegMaskFull &
90 Func->getTarget()->getRegisterSetForType(Cur.Var->getType());
91
92 // Check for precolored ranges. If Cur is precolored, it
93 // definitely gets that register. Previously processed live
94 // ranges would have avoided that register due to it being
95 // precolored. Future processed live ranges won't evict that
96 // register because the live range has infinite weight.
97 if (Cur.Var->hasReg()) {
98 int32_t RegNum = Cur.Var->getRegNum();
99 // RegNumTmp should have already been set above.
100 assert(Cur.Var->getRegNumTmp() == RegNum);
101 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
102 Str << "Precoloring ";
103 Cur.dump(Func);
104 Str << "\n";
105 }
106 Active.push_back(Cur);
107 assert(RegUses[RegNum] >= 0);
108 ++RegUses[RegNum];
109 continue;
110 }
111
112 // Check for active ranges that have expired or become inactive.
113 for (UnorderedRanges::iterator I = Active.begin(), E = Active.end(); I != E;
114 I = Next) {
115 Next = I;
116 ++Next;
117 LiveRangeWrapper Item = *I;
118 bool Moved = false;
119 if (Item.endsBefore(Cur)) {
120 // Move Item from Active to Handled list.
121 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
122 Str << "Expiring ";
123 Item.dump(Func);
124 Str << "\n";
125 }
126 Active.erase(I);
127 Handled.push_back(Item);
128 Moved = true;
129 } else if (!Item.overlapsStart(Cur)) {
130 // Move Item from Active to Inactive list.
131 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
132 Str << "Inactivating ";
133 Item.dump(Func);
134 Str << "\n";
135 }
136 Active.erase(I);
137 Inactive.push_back(Item);
138 Moved = true;
139 }
140 if (Moved) {
141 // Decrement Item from RegUses[].
142 assert(Item.Var->hasRegTmp());
143 int32_t RegNum = Item.Var->getRegNumTmp();
144 --RegUses[RegNum];
145 assert(RegUses[RegNum] >= 0);
146 }
147 }
148
149 // Check for inactive ranges that have expired or reactivated.
150 for (UnorderedRanges::iterator I = Inactive.begin(), E = Inactive.end();
151 I != E; I = Next) {
152 Next = I;
153 ++Next;
154 LiveRangeWrapper Item = *I;
155 if (Item.endsBefore(Cur)) {
156 // Move Item from Inactive to Handled list.
157 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
158 Str << "Expiring ";
159 Item.dump(Func);
160 Str << "\n";
161 }
162 Inactive.erase(I);
163 Handled.push_back(Item);
164 } else if (Item.overlapsStart(Cur)) {
165 // Move Item from Inactive to Active list.
166 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
167 Str << "Reactivating ";
168 Item.dump(Func);
169 Str << "\n";
170 }
171 Inactive.erase(I);
172 Active.push_back(Item);
173 // Increment Item in RegUses[].
174 assert(Item.Var->hasRegTmp());
175 int32_t RegNum = Item.Var->getRegNumTmp();
176 assert(RegUses[RegNum] >= 0);
177 ++RegUses[RegNum];
178 }
179 }
180
181 // Calculate available registers into Free[].
182 llvm::SmallBitVector Free = RegMask;
183 for (SizeT i = 0; i < RegMask.size(); ++i) {
184 if (RegUses[i] > 0)
185 Free[i] = false;
186 }
187
188 // Remove registers from the Free[] list where an Inactive range
189 // overlaps with the current range.
190 for (UnorderedRanges::const_iterator I = Inactive.begin(),
191 E = Inactive.end();
192 I != E; ++I) {
193 LiveRangeWrapper Item = *I;
194 if (Item.overlaps(Cur)) {
195 int32_t RegNum = Item.Var->getRegNumTmp();
196 // Don't assert(Free[RegNum]) because in theory (though
197 // probably never in practice) there could be two inactive
198 // variables that were allowed marked with
199 // AllowRegisterOverlap.
200 Free[RegNum] = false;
201 }
202 }
203
204 // Remove registers from the Free[] list where an Unhandled range
205 // overlaps with the current range and is precolored.
206 // Cur.endsBefore(*I) is an early exit check that turns a
207 // guaranteed O(N^2) algorithm into expected linear complexity.
Jim Stichnothca662e92014-07-10 15:32:36 -0700208 llvm::SmallBitVector PrecoloredUnhandled(RegMask.size());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700209 for (OrderedRanges::const_iterator I = Unhandled.begin(),
210 E = Unhandled.end();
211 I != E && !Cur.endsBefore(*I); ++I) {
212 LiveRangeWrapper Item = *I;
213 if (Item.Var->hasReg() && Item.overlaps(Cur)) {
214 Free[Item.Var->getRegNum()] = false; // Note: getRegNum not getRegNumTmp
Jim Stichnothca662e92014-07-10 15:32:36 -0700215 PrecoloredUnhandled[Item.Var->getRegNum()] = true;
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700216 }
217 }
218
219 // Print info about physical register availability.
220 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
221 for (SizeT i = 0; i < RegMask.size(); ++i) {
222 if (RegMask[i]) {
223 Str << Func->getTarget()->getRegName(i, IceType_i32)
Jim Stichnothca662e92014-07-10 15:32:36 -0700224 << "(U=" << RegUses[i] << ",F=" << Free[i]
225 << ",P=" << PrecoloredUnhandled[i] << ") ";
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700226 }
227 }
228 Str << "\n";
229 }
230
231 Variable *Prefer = Cur.Var->getPreferredRegister();
232 int32_t PreferReg = Prefer && Prefer->hasRegTmp() ? Prefer->getRegNumTmp()
233 : Variable::NoRegister;
Jim Stichnothca662e92014-07-10 15:32:36 -0700234 bool AllowedToOverlap = Cur.Var->getRegisterOverlap() &&
235 PreferReg != Variable::NoRegister &&
236 !PrecoloredUnhandled[PreferReg];
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700237 if (PreferReg != Variable::NoRegister &&
Jim Stichnothca662e92014-07-10 15:32:36 -0700238 (AllowedToOverlap || Free[PreferReg])) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700239 // First choice: a preferred register that is either free or is
240 // allowed to overlap with its linked variable.
241 Cur.Var->setRegNumTmp(PreferReg);
242 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
243 Str << "Preferring ";
244 Cur.dump(Func);
245 Str << "\n";
246 }
247 assert(RegUses[PreferReg] >= 0);
248 ++RegUses[PreferReg];
249 Active.push_back(Cur);
250 } else if (Free.any()) {
251 // Second choice: any free register. TODO: After explicit
252 // affinity is considered, is there a strategy better than just
253 // picking the lowest-numbered available register?
254 int32_t RegNum = Free.find_first();
255 Cur.Var->setRegNumTmp(RegNum);
256 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
257 Str << "Allocating ";
258 Cur.dump(Func);
259 Str << "\n";
260 }
261 assert(RegUses[RegNum] >= 0);
262 ++RegUses[RegNum];
263 Active.push_back(Cur);
264 } else {
265 // Fallback: there are no free registers, so we look for the
266 // lowest-weight register and see if Cur has higher weight.
267 std::vector<RegWeight> Weights(RegMask.size());
268 // Check Active ranges.
269 for (UnorderedRanges::const_iterator I = Active.begin(), E = Active.end();
270 I != E; ++I) {
271 LiveRangeWrapper Item = *I;
272 assert(Item.overlaps(Cur));
273 int32_t RegNum = Item.Var->getRegNumTmp();
274 assert(Item.Var->hasRegTmp());
275 Weights[RegNum].addWeight(Item.range().getWeight());
276 }
277 // Same as above, but check Inactive ranges instead of Active.
278 for (UnorderedRanges::const_iterator I = Inactive.begin(),
279 E = Inactive.end();
280 I != E; ++I) {
281 LiveRangeWrapper Item = *I;
282 int32_t RegNum = Item.Var->getRegNumTmp();
283 assert(Item.Var->hasRegTmp());
284 if (Item.overlaps(Cur))
285 Weights[RegNum].addWeight(Item.range().getWeight());
286 }
287 // Check Unhandled ranges that overlap Cur and are precolored.
288 // Cur.endsBefore(*I) is an early exit check that turns a
289 // guaranteed O(N^2) algorithm into expected linear complexity.
290 for (OrderedRanges::const_iterator I = Unhandled.begin(),
291 E = Unhandled.end();
292 I != E && !Cur.endsBefore(*I); ++I) {
293 LiveRangeWrapper Item = *I;
294 int32_t RegNum = Item.Var->getRegNumTmp();
295 if (RegNum < 0)
296 continue;
297 if (Item.overlaps(Cur))
298 Weights[RegNum].setWeight(RegWeight::Inf);
299 }
300
301 // All the weights are now calculated. Find the register with
302 // smallest weight.
303 int32_t MinWeightIndex = RegMask.find_first();
304 // MinWeightIndex must be valid because of the initial
305 // RegMask.any() test.
306 assert(MinWeightIndex >= 0);
307 for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) {
308 if (RegMask[i] && Weights[i] < Weights[MinWeightIndex])
309 MinWeightIndex = i;
310 }
311
312 if (Cur.range().getWeight() <= Weights[MinWeightIndex]) {
313 // Cur doesn't have priority over any other live ranges, so
314 // don't allocate any register to it, and move it to the
315 // Handled state.
316 Handled.push_back(Cur);
317 if (Cur.range().getWeight().isInf()) {
318 Func->setError("Unable to find a physical register for an "
319 "infinite-weight live range");
320 }
321 } else {
322 // Evict all live ranges in Active that register number
323 // MinWeightIndex is assigned to.
324 for (UnorderedRanges::iterator I = Active.begin(), E = Active.end();
325 I != E; I = Next) {
326 Next = I;
327 ++Next;
328 LiveRangeWrapper Item = *I;
329 if (Item.Var->getRegNumTmp() == MinWeightIndex) {
330 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
331 Str << "Evicting ";
332 Item.dump(Func);
333 Str << "\n";
334 }
335 --RegUses[MinWeightIndex];
336 assert(RegUses[MinWeightIndex] >= 0);
337 Item.Var->setRegNumTmp(Variable::NoRegister);
338 Active.erase(I);
339 Handled.push_back(Item);
340 }
341 }
342 // Do the same for Inactive.
343 for (UnorderedRanges::iterator I = Inactive.begin(), E = Inactive.end();
344 I != E; I = Next) {
345 Next = I;
346 ++Next;
347 LiveRangeWrapper Item = *I;
Jim Stichnoth68e28192014-07-24 08:48:15 -0700348 // Note: The Item.overlaps(Cur) clause is not part of the
349 // description of AssignMemLoc() in the original paper. But
350 // there doesn't seem to be any need to evict an inactive
351 // live range that doesn't overlap with the live range
352 // currently being considered. It's especially bad if we
353 // would end up evicting an infinite-weight but
354 // currently-inactive live range. The most common situation
355 // for this would be a scratch register kill set for call
356 // instructions.
357 if (Item.Var->getRegNumTmp() == MinWeightIndex &&
358 Item.overlaps(Cur)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700359 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
360 Str << "Evicting ";
361 Item.dump(Func);
362 Str << "\n";
363 }
364 Item.Var->setRegNumTmp(Variable::NoRegister);
365 Inactive.erase(I);
366 Handled.push_back(Item);
367 }
368 }
369 // Assign the register to Cur.
370 Cur.Var->setRegNumTmp(MinWeightIndex);
371 assert(RegUses[MinWeightIndex] >= 0);
372 ++RegUses[MinWeightIndex];
373 Active.push_back(Cur);
374 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
375 Str << "Allocating ";
376 Cur.dump(Func);
377 Str << "\n";
378 }
379 }
380 }
381 dump(Func);
382 }
383 // Move anything Active or Inactive to Handled for easier handling.
384 for (UnorderedRanges::iterator I = Active.begin(), E = Active.end(); I != E;
385 I = Next) {
386 Next = I;
387 ++Next;
388 Handled.push_back(*I);
389 Active.erase(I);
390 }
391 for (UnorderedRanges::iterator I = Inactive.begin(), E = Inactive.end();
392 I != E; I = Next) {
393 Next = I;
394 ++Next;
395 Handled.push_back(*I);
396 Inactive.erase(I);
397 }
398 dump(Func);
399
400 // Finish up by assigning RegNumTmp->RegNum for each Variable.
401 for (UnorderedRanges::const_iterator I = Handled.begin(), E = Handled.end();
402 I != E; ++I) {
403 LiveRangeWrapper Item = *I;
404 int32_t RegNum = Item.Var->getRegNumTmp();
405 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
406 if (!Item.Var->hasRegTmp()) {
407 Str << "Not assigning ";
408 Item.Var->dump(Func);
409 Str << "\n";
410 } else {
411 Str << (RegNum == Item.Var->getRegNum() ? "Reassigning " : "Assigning ")
412 << Func->getTarget()->getRegName(RegNum, IceType_i32) << "(r"
413 << RegNum << ") to ";
414 Item.Var->dump(Func);
415 Str << "\n";
416 }
417 }
418 Item.Var->setRegNum(Item.Var->getRegNumTmp());
419 }
420
421 // TODO: Consider running register allocation one more time, with
422 // infinite registers, for two reasons. First, evicted live ranges
423 // get a second chance for a register. Second, it allows coalescing
424 // of stack slots. If there is no time budget for the second
425 // register allocation run, each unallocated variable just gets its
426 // own slot.
427 //
428 // Another idea for coalescing stack slots is to initialize the
429 // Unhandled list with just the unallocated variables, saving time
430 // but not offering second-chance opportunities.
431}
432
433// ======================== Dump routines ======================== //
434
435void LiveRangeWrapper::dump(const Cfg *Func) const {
436 Ostream &Str = Func->getContext()->getStrDump();
437 const static size_t BufLen = 30;
438 char buf[BufLen];
439 snprintf(buf, BufLen, "%2d", Var->getRegNumTmp());
440 Str << "R=" << buf << " V=";
441 Var->dump(Func);
442 Str << " Range=" << range();
443}
444
445void LinearScan::dump(Cfg *Func) const {
446 Ostream &Str = Func->getContext()->getStrDump();
447 if (!Func->getContext()->isVerbose(IceV_LinearScan))
448 return;
449 Func->setCurrentNode(NULL);
450 Str << "**** Current regalloc state:\n";
451 Str << "++++++ Handled:\n";
452 for (UnorderedRanges::const_iterator I = Handled.begin(), E = Handled.end();
453 I != E; ++I) {
454 I->dump(Func);
455 Str << "\n";
456 }
457 Str << "++++++ Unhandled:\n";
458 for (OrderedRanges::const_iterator I = Unhandled.begin(), E = Unhandled.end();
459 I != E; ++I) {
460 I->dump(Func);
461 Str << "\n";
462 }
463 Str << "++++++ Active:\n";
464 for (UnorderedRanges::const_iterator I = Active.begin(), E = Active.end();
465 I != E; ++I) {
466 I->dump(Func);
467 Str << "\n";
468 }
469 Str << "++++++ Inactive:\n";
470 for (UnorderedRanges::const_iterator I = Inactive.begin(), E = Inactive.end();
471 I != E; ++I) {
472 I->dump(Func);
473 Str << "\n";
474 }
475}
476
477} // end of namespace Ice