blob: ce08c6a83c23cf9a9a5e1485a8c60e8591b16040 [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 &&
Jim Stichnothdd7b8462014-09-04 10:37:49 -0700236 RegMask[PreferReg] &&
Jim Stichnothca662e92014-07-10 15:32:36 -0700237 !PrecoloredUnhandled[PreferReg];
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700238 if (PreferReg != Variable::NoRegister &&
Jim Stichnothca662e92014-07-10 15:32:36 -0700239 (AllowedToOverlap || Free[PreferReg])) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700240 // First choice: a preferred register that is either free or is
241 // allowed to overlap with its linked variable.
242 Cur.Var->setRegNumTmp(PreferReg);
243 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
244 Str << "Preferring ";
245 Cur.dump(Func);
246 Str << "\n";
247 }
248 assert(RegUses[PreferReg] >= 0);
249 ++RegUses[PreferReg];
250 Active.push_back(Cur);
251 } else if (Free.any()) {
252 // Second choice: any free register. TODO: After explicit
253 // affinity is considered, is there a strategy better than just
254 // picking the lowest-numbered available register?
255 int32_t RegNum = Free.find_first();
256 Cur.Var->setRegNumTmp(RegNum);
257 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
258 Str << "Allocating ";
259 Cur.dump(Func);
260 Str << "\n";
261 }
262 assert(RegUses[RegNum] >= 0);
263 ++RegUses[RegNum];
264 Active.push_back(Cur);
265 } else {
266 // Fallback: there are no free registers, so we look for the
267 // lowest-weight register and see if Cur has higher weight.
268 std::vector<RegWeight> Weights(RegMask.size());
269 // Check Active ranges.
270 for (UnorderedRanges::const_iterator I = Active.begin(), E = Active.end();
271 I != E; ++I) {
272 LiveRangeWrapper Item = *I;
273 assert(Item.overlaps(Cur));
274 int32_t RegNum = Item.Var->getRegNumTmp();
275 assert(Item.Var->hasRegTmp());
276 Weights[RegNum].addWeight(Item.range().getWeight());
277 }
278 // Same as above, but check Inactive ranges instead of Active.
279 for (UnorderedRanges::const_iterator I = Inactive.begin(),
280 E = Inactive.end();
281 I != E; ++I) {
282 LiveRangeWrapper Item = *I;
283 int32_t RegNum = Item.Var->getRegNumTmp();
284 assert(Item.Var->hasRegTmp());
285 if (Item.overlaps(Cur))
286 Weights[RegNum].addWeight(Item.range().getWeight());
287 }
288 // Check Unhandled ranges that overlap Cur and are precolored.
289 // Cur.endsBefore(*I) is an early exit check that turns a
290 // guaranteed O(N^2) algorithm into expected linear complexity.
291 for (OrderedRanges::const_iterator I = Unhandled.begin(),
292 E = Unhandled.end();
293 I != E && !Cur.endsBefore(*I); ++I) {
294 LiveRangeWrapper Item = *I;
295 int32_t RegNum = Item.Var->getRegNumTmp();
296 if (RegNum < 0)
297 continue;
298 if (Item.overlaps(Cur))
299 Weights[RegNum].setWeight(RegWeight::Inf);
300 }
301
302 // All the weights are now calculated. Find the register with
303 // smallest weight.
304 int32_t MinWeightIndex = RegMask.find_first();
305 // MinWeightIndex must be valid because of the initial
306 // RegMask.any() test.
307 assert(MinWeightIndex >= 0);
308 for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) {
309 if (RegMask[i] && Weights[i] < Weights[MinWeightIndex])
310 MinWeightIndex = i;
311 }
312
313 if (Cur.range().getWeight() <= Weights[MinWeightIndex]) {
314 // Cur doesn't have priority over any other live ranges, so
315 // don't allocate any register to it, and move it to the
316 // Handled state.
317 Handled.push_back(Cur);
318 if (Cur.range().getWeight().isInf()) {
319 Func->setError("Unable to find a physical register for an "
320 "infinite-weight live range");
321 }
322 } else {
323 // Evict all live ranges in Active that register number
324 // MinWeightIndex is assigned to.
325 for (UnorderedRanges::iterator I = Active.begin(), E = Active.end();
326 I != E; I = Next) {
327 Next = I;
328 ++Next;
329 LiveRangeWrapper Item = *I;
330 if (Item.Var->getRegNumTmp() == MinWeightIndex) {
331 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
332 Str << "Evicting ";
333 Item.dump(Func);
334 Str << "\n";
335 }
336 --RegUses[MinWeightIndex];
337 assert(RegUses[MinWeightIndex] >= 0);
338 Item.Var->setRegNumTmp(Variable::NoRegister);
339 Active.erase(I);
340 Handled.push_back(Item);
341 }
342 }
343 // Do the same for Inactive.
344 for (UnorderedRanges::iterator I = Inactive.begin(), E = Inactive.end();
345 I != E; I = Next) {
346 Next = I;
347 ++Next;
348 LiveRangeWrapper Item = *I;
Jim Stichnoth68e28192014-07-24 08:48:15 -0700349 // Note: The Item.overlaps(Cur) clause is not part of the
350 // description of AssignMemLoc() in the original paper. But
351 // there doesn't seem to be any need to evict an inactive
352 // live range that doesn't overlap with the live range
353 // currently being considered. It's especially bad if we
354 // would end up evicting an infinite-weight but
355 // currently-inactive live range. The most common situation
356 // for this would be a scratch register kill set for call
357 // instructions.
358 if (Item.Var->getRegNumTmp() == MinWeightIndex &&
359 Item.overlaps(Cur)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700360 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
361 Str << "Evicting ";
362 Item.dump(Func);
363 Str << "\n";
364 }
365 Item.Var->setRegNumTmp(Variable::NoRegister);
366 Inactive.erase(I);
367 Handled.push_back(Item);
368 }
369 }
370 // Assign the register to Cur.
371 Cur.Var->setRegNumTmp(MinWeightIndex);
372 assert(RegUses[MinWeightIndex] >= 0);
373 ++RegUses[MinWeightIndex];
374 Active.push_back(Cur);
375 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
376 Str << "Allocating ";
377 Cur.dump(Func);
378 Str << "\n";
379 }
380 }
381 }
382 dump(Func);
383 }
384 // Move anything Active or Inactive to Handled for easier handling.
385 for (UnorderedRanges::iterator I = Active.begin(), E = Active.end(); I != E;
386 I = Next) {
387 Next = I;
388 ++Next;
389 Handled.push_back(*I);
390 Active.erase(I);
391 }
392 for (UnorderedRanges::iterator I = Inactive.begin(), E = Inactive.end();
393 I != E; I = Next) {
394 Next = I;
395 ++Next;
396 Handled.push_back(*I);
397 Inactive.erase(I);
398 }
399 dump(Func);
400
401 // Finish up by assigning RegNumTmp->RegNum for each Variable.
402 for (UnorderedRanges::const_iterator I = Handled.begin(), E = Handled.end();
403 I != E; ++I) {
404 LiveRangeWrapper Item = *I;
405 int32_t RegNum = Item.Var->getRegNumTmp();
406 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
407 if (!Item.Var->hasRegTmp()) {
408 Str << "Not assigning ";
409 Item.Var->dump(Func);
410 Str << "\n";
411 } else {
412 Str << (RegNum == Item.Var->getRegNum() ? "Reassigning " : "Assigning ")
413 << Func->getTarget()->getRegName(RegNum, IceType_i32) << "(r"
414 << RegNum << ") to ";
415 Item.Var->dump(Func);
416 Str << "\n";
417 }
418 }
419 Item.Var->setRegNum(Item.Var->getRegNumTmp());
420 }
421
422 // TODO: Consider running register allocation one more time, with
423 // infinite registers, for two reasons. First, evicted live ranges
424 // get a second chance for a register. Second, it allows coalescing
425 // of stack slots. If there is no time budget for the second
426 // register allocation run, each unallocated variable just gets its
427 // own slot.
428 //
429 // Another idea for coalescing stack slots is to initialize the
430 // Unhandled list with just the unallocated variables, saving time
431 // but not offering second-chance opportunities.
432}
433
434// ======================== Dump routines ======================== //
435
436void LiveRangeWrapper::dump(const Cfg *Func) const {
437 Ostream &Str = Func->getContext()->getStrDump();
438 const static size_t BufLen = 30;
439 char buf[BufLen];
440 snprintf(buf, BufLen, "%2d", Var->getRegNumTmp());
441 Str << "R=" << buf << " V=";
442 Var->dump(Func);
443 Str << " Range=" << range();
444}
445
446void LinearScan::dump(Cfg *Func) const {
447 Ostream &Str = Func->getContext()->getStrDump();
448 if (!Func->getContext()->isVerbose(IceV_LinearScan))
449 return;
450 Func->setCurrentNode(NULL);
451 Str << "**** Current regalloc state:\n";
452 Str << "++++++ Handled:\n";
453 for (UnorderedRanges::const_iterator I = Handled.begin(), E = Handled.end();
454 I != E; ++I) {
455 I->dump(Func);
456 Str << "\n";
457 }
458 Str << "++++++ Unhandled:\n";
459 for (OrderedRanges::const_iterator I = Unhandled.begin(), E = Unhandled.end();
460 I != E; ++I) {
461 I->dump(Func);
462 Str << "\n";
463 }
464 Str << "++++++ Active:\n";
465 for (UnorderedRanges::const_iterator I = Active.begin(), E = Active.end();
466 I != E; ++I) {
467 I->dump(Func);
468 Str << "\n";
469 }
470 Str << "++++++ Inactive:\n";
471 for (UnorderedRanges::const_iterator I = Inactive.begin(), E = Inactive.end();
472 I != E; ++I) {
473 I->dump(Func);
474 Str << "\n";
475 }
476}
477
478} // end of namespace Ice