blob: 1b4d0c249d0d872f2aecb705081dce83e25a04c8 [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
Jim Stichnothad403532014-09-25 12:44:17 -070024namespace {
25
26// Returns true if Var has any definitions within Item's live range.
Jim Stichnoth037fa1d2014-10-07 11:09:33 -070027// TODO(stichnot): Consider trimming the Definitions list similar to
28// how the live ranges are trimmed, since all the overlapsDefs() tests
29// are whether some variable's definitions overlap Cur, and trimming
30// is with respect Cur.start. Initial tests show no measurable
31// performance difference, so we'll keep the code simple for now.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -070032bool overlapsDefs(const Cfg *Func, const Variable *Item, const Variable *Var) {
Jim Stichnoth037fa1d2014-10-07 11:09:33 -070033 const bool UseTrimmed = true;
34 VariablesMetadata *VMetadata = Func->getVMetadata();
Jim Stichnoth877b04e2014-10-15 15:13:06 -070035 if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var))
36 if (Item->getLiveRange().overlapsInst(FirstDef->getNumber(), UseTrimmed))
37 return true;
38 const InstDefList &Defs = VMetadata->getLatterDefinitions(Var);
Jim Stichnothad403532014-09-25 12:44:17 -070039 for (size_t i = 0; i < Defs.size(); ++i) {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -070040 if (Item->getLiveRange().overlapsInst(Defs[i]->getNumber(), UseTrimmed))
Jim Stichnothad403532014-09-25 12:44:17 -070041 return true;
42 }
43 return false;
44}
45
46void dumpDisableOverlap(const Cfg *Func, const Variable *Var,
47 const char *Reason) {
48 if (Func->getContext()->isVerbose(IceV_LinearScan)) {
Jim Stichnoth037fa1d2014-10-07 11:09:33 -070049 VariablesMetadata *VMetadata = Func->getVMetadata();
Jim Stichnothad403532014-09-25 12:44:17 -070050 Ostream &Str = Func->getContext()->getStrDump();
51 Str << "Disabling Overlap due to " << Reason << " " << *Var
52 << " LIVE=" << Var->getLiveRange() << " Defs=";
Jim Stichnoth877b04e2014-10-15 15:13:06 -070053 if (const Inst *FirstDef = VMetadata->getFirstDefinition(Var))
54 Str << FirstDef->getNumber();
55 const InstDefList &Defs = VMetadata->getLatterDefinitions(Var);
Jim Stichnothad403532014-09-25 12:44:17 -070056 for (size_t i = 0; i < Defs.size(); ++i) {
Jim Stichnoth877b04e2014-10-15 15:13:06 -070057 Str << "," << Defs[i]->getNumber();
Jim Stichnothad403532014-09-25 12:44:17 -070058 }
59 Str << "\n";
60 }
61}
62
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -070063void dumpLiveRange(const Variable *Var, const Cfg *Func) {
64 Ostream &Str = Func->getContext()->getStrDump();
65 const static size_t BufLen = 30;
66 char buf[BufLen];
67 snprintf(buf, BufLen, "%2d", Var->getRegNumTmp());
68 Str << "R=" << buf << " V=";
69 Var->dump(Func);
70 Str << " Range=" << Var->getLiveRange();
Jim Stichnothe22f8232014-10-13 16:20:59 -070071}
72
Jim Stichnothad403532014-09-25 12:44:17 -070073} // end of anonymous namespace
74
Jim Stichnothd97c7df2014-06-04 11:57:08 -070075// Implements the linear-scan algorithm. Based on "Linear Scan
76// Register Allocation in the Context of SSA Form and Register
77// Constraints" by Hanspeter Mössenböck and Michael Pfeiffer,
78// ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe02.PDF . This
79// implementation is modified to take affinity into account and allow
80// two interfering variables to share the same register in certain
81// cases.
82//
Jim Stichnoth800dab22014-09-20 12:25:02 -070083// Requires running Cfg::liveness(Liveness_Intervals) in
Jim Stichnothd97c7df2014-06-04 11:57:08 -070084// preparation. Results are assigned to Variable::RegNum for each
85// Variable.
86void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) {
Jim Stichnoth8363a062014-10-07 10:02:38 -070087 TimerMarker T(TimerStack::TT_linearScan, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -070088 assert(RegMaskFull.any()); // Sanity check
89 Unhandled.clear();
Jim Stichnoth541ba662014-10-02 12:58:21 -070090 UnhandledPrecolored.clear();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070091 Handled.clear();
92 Inactive.clear();
93 Active.clear();
94 Ostream &Str = Func->getContext()->getStrDump();
Jim Stichnothc4554d72014-09-30 16:49:38 -070095 bool Verbose = Func->getContext()->isVerbose(IceV_LinearScan);
Jim Stichnoth800dab22014-09-20 12:25:02 -070096 Func->resetCurrentNode();
Jim Stichnothad403532014-09-25 12:44:17 -070097 VariablesMetadata *VMetadata = Func->getVMetadata();
Jim Stichnothd97c7df2014-06-04 11:57:08 -070098
99 // Gather the live ranges of all variables and add them to the
Jim Stichnothe22f8232014-10-13 16:20:59 -0700100 // Unhandled set.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700101 const VarList &Vars = Func->getVariables();
Jim Stichnothc4554d72014-09-30 16:49:38 -0700102 {
Jim Stichnoth8363a062014-10-07 10:02:38 -0700103 TimerMarker T(TimerStack::TT_initUnhandled, Func);
Jim Stichnothe22f8232014-10-13 16:20:59 -0700104 Unhandled.reserve(Vars.size());
Jim Stichnothf44f3712014-10-01 14:05:51 -0700105 for (Variable *Var : Vars) {
Jim Stichnothc4554d72014-09-30 16:49:38 -0700106 // Explicitly don't consider zero-weight variables, which are
107 // meant to be spill slots.
108 if (Var->getWeight() == RegWeight::Zero)
109 continue;
110 // Don't bother if the variable has a null live range, which means
111 // it was never referenced.
112 if (Var->getLiveRange().isEmpty())
113 continue;
Jim Stichnoth037fa1d2014-10-07 11:09:33 -0700114 Var->untrimLiveRange();
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700115 Unhandled.push_back(Var);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700116 if (Var->hasReg()) {
117 Var->setRegNumTmp(Var->getRegNum());
118 Var->setLiveRangeInfiniteWeight();
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700119 UnhandledPrecolored.push_back(Var);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700120 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700121 }
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700122 struct CompareRanges {
123 bool operator()(const Variable *L, const Variable *R) {
124 InstNumberT Lstart = L->getLiveRange().getStart();
125 InstNumberT Rstart = R->getLiveRange().getStart();
126 if (Lstart == Rstart)
127 return L->getIndex() < R->getIndex();
128 return Lstart < Rstart;
129 }
130 };
Jim Stichnothe22f8232014-10-13 16:20:59 -0700131 // Do a reverse sort so that erasing elements (from the end) is fast.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700132 std::sort(Unhandled.rbegin(), Unhandled.rend(), CompareRanges());
Jim Stichnothe22f8232014-10-13 16:20:59 -0700133 std::sort(UnhandledPrecolored.rbegin(), UnhandledPrecolored.rend(),
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700134 CompareRanges());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700135 }
136
137 // RegUses[I] is the number of live ranges (variables) that register
138 // I is currently assigned to. It can be greater than 1 as a result
Jim Stichnothc4554d72014-09-30 16:49:38 -0700139 // of AllowOverlap inference below.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700140 std::vector<int> RegUses(RegMaskFull.size());
141 // Unhandled is already set to all ranges in increasing order of
142 // start points.
143 assert(Active.empty());
144 assert(Inactive.empty());
145 assert(Handled.empty());
146 UnorderedRanges::iterator Next;
147
148 while (!Unhandled.empty()) {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700149 Variable *Cur = Unhandled.back();
Jim Stichnothe22f8232014-10-13 16:20:59 -0700150 Unhandled.pop_back();
Jim Stichnothc4554d72014-09-30 16:49:38 -0700151 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700152 Str << "\nConsidering ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700153 dumpLiveRange(Cur, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700154 Str << "\n";
155 }
156 const llvm::SmallBitVector RegMask =
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700157 RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700158
159 // Check for precolored ranges. If Cur is precolored, it
160 // definitely gets that register. Previously processed live
161 // ranges would have avoided that register due to it being
162 // precolored. Future processed live ranges won't evict that
163 // register because the live range has infinite weight.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700164 if (Cur->hasReg()) {
165 int32_t RegNum = Cur->getRegNum();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700166 // RegNumTmp should have already been set above.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700167 assert(Cur->getRegNumTmp() == RegNum);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700168 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700169 Str << "Precoloring ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700170 dumpLiveRange(Cur, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700171 Str << "\n";
172 }
173 Active.push_back(Cur);
174 assert(RegUses[RegNum] >= 0);
175 ++RegUses[RegNum];
Jim Stichnoth541ba662014-10-02 12:58:21 -0700176 assert(!UnhandledPrecolored.empty());
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700177 assert(UnhandledPrecolored.back() == Cur);
Jim Stichnothe22f8232014-10-13 16:20:59 -0700178 UnhandledPrecolored.pop_back();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700179 continue;
180 }
181
182 // Check for active ranges that have expired or become inactive.
Jim Stichnothf44f3712014-10-01 14:05:51 -0700183 for (auto I = Active.begin(), E = Active.end(); I != E; I = Next) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700184 Next = I;
185 ++Next;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700186 Variable *Item = *I;
187 Item->trimLiveRange(Cur->getLiveRange().getStart());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700188 bool Moved = false;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700189 if (Item->rangeEndsBefore(Cur)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700190 // Move Item from Active to Handled list.
Jim Stichnothc4554d72014-09-30 16:49:38 -0700191 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700192 Str << "Expiring ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700193 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700194 Str << "\n";
195 }
Jim Stichnothe22f8232014-10-13 16:20:59 -0700196 Handled.splice(Handled.end(), Active, I);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700197 Moved = true;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700198 } else if (!Item->rangeOverlapsStart(Cur)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700199 // Move Item from Active to Inactive list.
Jim Stichnothc4554d72014-09-30 16:49:38 -0700200 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700201 Str << "Inactivating ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700202 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700203 Str << "\n";
204 }
Jim Stichnothe22f8232014-10-13 16:20:59 -0700205 Inactive.splice(Inactive.end(), Active, I);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700206 Moved = true;
207 }
208 if (Moved) {
209 // Decrement Item from RegUses[].
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700210 assert(Item->hasRegTmp());
211 int32_t RegNum = Item->getRegNumTmp();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700212 --RegUses[RegNum];
213 assert(RegUses[RegNum] >= 0);
214 }
215 }
216
217 // Check for inactive ranges that have expired or reactivated.
Jim Stichnothf44f3712014-10-01 14:05:51 -0700218 for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700219 Next = I;
220 ++Next;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700221 Variable *Item = *I;
222 Item->trimLiveRange(Cur->getLiveRange().getStart());
Jim Stichnoth037fa1d2014-10-07 11:09:33 -0700223 // As an optimization, don't bother checking pure point-valued
224 // Inactive ranges, because the overlapsStart() test will never
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700225 // succeed, and the rangeEndsBefore() test will generally only
Jim Stichnoth037fa1d2014-10-07 11:09:33 -0700226 // succeed after the last call instruction, which statistically
227 // happens near the end. TODO(stichnot): Consider suppressing
228 // this check every N iterations in case calls are only at the
229 // beginning of the function.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700230 if (!Item->getLiveRange().isNonpoints())
Jim Stichnoth037fa1d2014-10-07 11:09:33 -0700231 continue;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700232 if (Item->rangeEndsBefore(Cur)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700233 // Move Item from Inactive to Handled list.
Jim Stichnothc4554d72014-09-30 16:49:38 -0700234 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700235 Str << "Expiring ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700236 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700237 Str << "\n";
238 }
Jim Stichnothe22f8232014-10-13 16:20:59 -0700239 Handled.splice(Handled.end(), Inactive, I);
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700240 } else if (Item->rangeOverlapsStart(Cur)) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700241 // Move Item from Inactive to Active list.
Jim Stichnothc4554d72014-09-30 16:49:38 -0700242 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700243 Str << "Reactivating ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700244 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700245 Str << "\n";
246 }
Jim Stichnothe22f8232014-10-13 16:20:59 -0700247 Active.splice(Active.end(), Inactive, I);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700248 // Increment Item in RegUses[].
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700249 assert(Item->hasRegTmp());
250 int32_t RegNum = Item->getRegNumTmp();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700251 assert(RegUses[RegNum] >= 0);
252 ++RegUses[RegNum];
253 }
254 }
255
256 // Calculate available registers into Free[].
257 llvm::SmallBitVector Free = RegMask;
258 for (SizeT i = 0; i < RegMask.size(); ++i) {
259 if (RegUses[i] > 0)
260 Free[i] = false;
261 }
262
Jim Stichnothad403532014-09-25 12:44:17 -0700263 // Infer register preference and allowable overlap. Only form a
264 // preference when the current Variable has an unambiguous "first"
265 // definition. The preference is some source Variable of the
266 // defining instruction that either is assigned a register that is
267 // currently free, or that is assigned a register that is not free
268 // but overlap is allowed. Overlap is allowed when the Variable
269 // under consideration is single-definition, and its definition is
270 // a simple assignment - i.e., the register gets copied/aliased
271 // but is never modified. Furthermore, overlap is only allowed
272 // when preferred Variable definition instructions do not appear
273 // within the current Variable's live range.
274 Variable *Prefer = NULL;
275 int32_t PreferReg = Variable::NoRegister;
276 bool AllowOverlap = false;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700277 if (const Inst *DefInst = VMetadata->getFirstDefinition(Cur)) {
278 assert(DefInst->getDest() == Cur);
Jim Stichnothad403532014-09-25 12:44:17 -0700279 bool IsAssign = DefInst->isSimpleAssign();
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700280 bool IsSingleDef = !VMetadata->isMultiDef(Cur);
Jim Stichnothad403532014-09-25 12:44:17 -0700281 for (SizeT i = 0; i < DefInst->getSrcSize(); ++i) {
282 // TODO(stichnot): Iterate through the actual Variables of the
283 // instruction, not just the source operands. This could
284 // capture Load instructions, including address mode
285 // optimization, for Prefer (but not for AllowOverlap).
286 if (Variable *SrcVar = llvm::dyn_cast<Variable>(DefInst->getSrc(i))) {
287 int32_t SrcReg = SrcVar->getRegNumTmp();
288 // Only consider source variables that have (so far) been
289 // assigned a register. That register must be one in the
290 // RegMask set, e.g. don't try to prefer the stack pointer
291 // as a result of the stacksave intrinsic.
292 if (SrcVar->hasRegTmp() && RegMask[SrcReg]) {
293 if (!Free[SrcReg]) {
294 // Don't bother trying to enable AllowOverlap if the
295 // register is already free.
296 AllowOverlap =
297 IsSingleDef && IsAssign && !overlapsDefs(Func, Cur, SrcVar);
298 }
299 if (AllowOverlap || Free[SrcReg]) {
300 Prefer = SrcVar;
301 PreferReg = SrcReg;
302 }
303 }
304 }
305 }
306 }
Jim Stichnothc4554d72014-09-30 16:49:38 -0700307 if (Verbose) {
Jim Stichnothad403532014-09-25 12:44:17 -0700308 if (Prefer) {
309 Str << "Initial Prefer=" << *Prefer << " R=" << PreferReg
310 << " LIVE=" << Prefer->getLiveRange() << " Overlap=" << AllowOverlap
311 << "\n";
312 }
313 }
314
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700315 // Remove registers from the Free[] list where an Inactive range
316 // overlaps with the current range.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700317 for (const Variable *Item : Inactive) {
318 if (Item->rangeOverlaps(Cur)) {
319 int32_t RegNum = Item->getRegNumTmp();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700320 // Don't assert(Free[RegNum]) because in theory (though
321 // probably never in practice) there could be two inactive
Jim Stichnothc4554d72014-09-30 16:49:38 -0700322 // variables that were marked with AllowOverlap.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700323 Free[RegNum] = false;
Jim Stichnothad403532014-09-25 12:44:17 -0700324 // Disable AllowOverlap if an Inactive variable, which is not
325 // Prefer, shares Prefer's register, and has a definition
326 // within Cur's live range.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700327 if (AllowOverlap && Item != Prefer && RegNum == PreferReg &&
328 overlapsDefs(Func, Cur, Item)) {
Jim Stichnothad403532014-09-25 12:44:17 -0700329 AllowOverlap = false;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700330 dumpDisableOverlap(Func, Item, "Inactive");
Jim Stichnothad403532014-09-25 12:44:17 -0700331 }
332 }
333 }
334
335 // Disable AllowOverlap if an Active variable, which is not
336 // Prefer, shares Prefer's register, and has a definition within
337 // Cur's live range.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700338 for (const Variable *Item : Active) {
339 int32_t RegNum = Item->getRegNumTmp();
340 if (Item != Prefer && RegNum == PreferReg &&
341 overlapsDefs(Func, Cur, Item)) {
Jim Stichnothad403532014-09-25 12:44:17 -0700342 AllowOverlap = false;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700343 dumpDisableOverlap(Func, Item, "Active");
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700344 }
345 }
346
Jim Stichnoth541ba662014-10-02 12:58:21 -0700347 std::vector<RegWeight> Weights(RegMask.size());
348
349 // Remove registers from the Free[] list where an Unhandled
350 // precolored range overlaps with the current range, and set those
351 // registers to infinite weight so that they aren't candidates for
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700352 // eviction. Cur->rangeEndsBefore(Item) is an early exit check
353 // that turns a guaranteed O(N^2) algorithm into expected linear
Jim Stichnoth541ba662014-10-02 12:58:21 -0700354 // complexity.
355 llvm::SmallBitVector PrecoloredUnhandledMask(RegMask.size());
356 // Note: PrecoloredUnhandledMask is only used for dumping.
Jim Stichnothe22f8232014-10-13 16:20:59 -0700357 for (auto I = UnhandledPrecolored.rbegin(), E = UnhandledPrecolored.rend();
358 I != E; ++I) {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700359 Variable *Item = *I;
360 assert(Item->hasReg());
361 if (Cur->rangeEndsBefore(Item))
Jim Stichnothf44f3712014-10-01 14:05:51 -0700362 break;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700363 if (Item->rangeOverlaps(Cur)) {
364 int32_t ItemReg = Item->getRegNum(); // Note: not getRegNumTmp()
Jim Stichnoth541ba662014-10-02 12:58:21 -0700365 Weights[ItemReg].setWeight(RegWeight::Inf);
Jim Stichnothad403532014-09-25 12:44:17 -0700366 Free[ItemReg] = false;
Jim Stichnoth541ba662014-10-02 12:58:21 -0700367 PrecoloredUnhandledMask[ItemReg] = true;
Jim Stichnothad403532014-09-25 12:44:17 -0700368 // Disable AllowOverlap if the preferred register is one of
369 // these precolored unhandled overlapping ranges.
370 if (AllowOverlap && ItemReg == PreferReg) {
371 AllowOverlap = false;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700372 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled");
Jim Stichnothad403532014-09-25 12:44:17 -0700373 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700374 }
375 }
376
377 // Print info about physical register availability.
Jim Stichnothc4554d72014-09-30 16:49:38 -0700378 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700379 for (SizeT i = 0; i < RegMask.size(); ++i) {
380 if (RegMask[i]) {
381 Str << Func->getTarget()->getRegName(i, IceType_i32)
Jim Stichnothca662e92014-07-10 15:32:36 -0700382 << "(U=" << RegUses[i] << ",F=" << Free[i]
Jim Stichnoth541ba662014-10-02 12:58:21 -0700383 << ",P=" << PrecoloredUnhandledMask[i] << ") ";
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700384 }
385 }
386 Str << "\n";
387 }
388
Jim Stichnothad403532014-09-25 12:44:17 -0700389 if (Prefer && (AllowOverlap || Free[PreferReg])) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700390 // First choice: a preferred register that is either free or is
391 // allowed to overlap with its linked variable.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700392 Cur->setRegNumTmp(PreferReg);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700393 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700394 Str << "Preferring ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700395 dumpLiveRange(Cur, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700396 Str << "\n";
397 }
398 assert(RegUses[PreferReg] >= 0);
399 ++RegUses[PreferReg];
400 Active.push_back(Cur);
401 } else if (Free.any()) {
402 // Second choice: any free register. TODO: After explicit
403 // affinity is considered, is there a strategy better than just
404 // picking the lowest-numbered available register?
405 int32_t RegNum = Free.find_first();
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700406 Cur->setRegNumTmp(RegNum);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700407 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700408 Str << "Allocating ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700409 dumpLiveRange(Cur, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700410 Str << "\n";
411 }
412 assert(RegUses[RegNum] >= 0);
413 ++RegUses[RegNum];
414 Active.push_back(Cur);
415 } else {
416 // Fallback: there are no free registers, so we look for the
417 // lowest-weight register and see if Cur has higher weight.
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700418 // Check Active ranges.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700419 for (const Variable *Item : Active) {
420 assert(Item->rangeOverlaps(Cur));
421 int32_t RegNum = Item->getRegNumTmp();
422 assert(Item->hasRegTmp());
423 Weights[RegNum].addWeight(Item->getLiveRange().getWeight());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700424 }
425 // Same as above, but check Inactive ranges instead of Active.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700426 for (const Variable *Item : Inactive) {
427 int32_t RegNum = Item->getRegNumTmp();
428 assert(Item->hasRegTmp());
429 if (Item->rangeOverlaps(Cur))
430 Weights[RegNum].addWeight(Item->getLiveRange().getWeight());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700431 }
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700432
433 // All the weights are now calculated. Find the register with
434 // smallest weight.
435 int32_t MinWeightIndex = RegMask.find_first();
436 // MinWeightIndex must be valid because of the initial
437 // RegMask.any() test.
438 assert(MinWeightIndex >= 0);
439 for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) {
440 if (RegMask[i] && Weights[i] < Weights[MinWeightIndex])
441 MinWeightIndex = i;
442 }
443
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700444 if (Cur->getLiveRange().getWeight() <= Weights[MinWeightIndex]) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700445 // Cur doesn't have priority over any other live ranges, so
446 // don't allocate any register to it, and move it to the
447 // Handled state.
448 Handled.push_back(Cur);
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700449 if (Cur->getLiveRange().getWeight().isInf()) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700450 Func->setError("Unable to find a physical register for an "
451 "infinite-weight live range");
452 }
453 } else {
454 // Evict all live ranges in Active that register number
455 // MinWeightIndex is assigned to.
Jim Stichnothf44f3712014-10-01 14:05:51 -0700456 for (auto I = Active.begin(), E = Active.end(); I != E; I = Next) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700457 Next = I;
458 ++Next;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700459 Variable *Item = *I;
460 if (Item->getRegNumTmp() == MinWeightIndex) {
Jim Stichnothc4554d72014-09-30 16:49:38 -0700461 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700462 Str << "Evicting ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700463 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700464 Str << "\n";
465 }
466 --RegUses[MinWeightIndex];
467 assert(RegUses[MinWeightIndex] >= 0);
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700468 Item->setRegNumTmp(Variable::NoRegister);
Jim Stichnothe22f8232014-10-13 16:20:59 -0700469 Handled.splice(Handled.end(), Active, I);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700470 }
471 }
472 // Do the same for Inactive.
Jim Stichnothf44f3712014-10-01 14:05:51 -0700473 for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700474 Next = I;
475 ++Next;
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700476 Variable *Item = *I;
477 // Note: The Item->rangeOverlaps(Cur) clause is not part of the
Jim Stichnoth68e28192014-07-24 08:48:15 -0700478 // description of AssignMemLoc() in the original paper. But
479 // there doesn't seem to be any need to evict an inactive
480 // live range that doesn't overlap with the live range
481 // currently being considered. It's especially bad if we
482 // would end up evicting an infinite-weight but
483 // currently-inactive live range. The most common situation
484 // for this would be a scratch register kill set for call
485 // instructions.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700486 if (Item->getRegNumTmp() == MinWeightIndex &&
487 Item->rangeOverlaps(Cur)) {
Jim Stichnothc4554d72014-09-30 16:49:38 -0700488 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700489 Str << "Evicting ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700490 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700491 Str << "\n";
492 }
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700493 Item->setRegNumTmp(Variable::NoRegister);
Jim Stichnothe22f8232014-10-13 16:20:59 -0700494 Handled.splice(Handled.end(), Inactive, I);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700495 }
496 }
497 // Assign the register to Cur.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700498 Cur->setRegNumTmp(MinWeightIndex);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700499 assert(RegUses[MinWeightIndex] >= 0);
500 ++RegUses[MinWeightIndex];
501 Active.push_back(Cur);
Jim Stichnothc4554d72014-09-30 16:49:38 -0700502 if (Verbose) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700503 Str << "Allocating ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700504 dumpLiveRange(Cur, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700505 Str << "\n";
506 }
507 }
508 }
509 dump(Func);
510 }
511 // Move anything Active or Inactive to Handled for easier handling.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700512 for (Variable *I : Active)
Jim Stichnothf44f3712014-10-01 14:05:51 -0700513 Handled.push_back(I);
514 Active.clear();
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700515 for (Variable *I : Inactive)
Jim Stichnothf44f3712014-10-01 14:05:51 -0700516 Handled.push_back(I);
517 Inactive.clear();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700518 dump(Func);
519
520 // Finish up by assigning RegNumTmp->RegNum for each Variable.
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700521 for (Variable *Item : Handled) {
522 int32_t RegNum = Item->getRegNumTmp();
Jim Stichnothc4554d72014-09-30 16:49:38 -0700523 if (Verbose) {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700524 if (!Item->hasRegTmp()) {
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700525 Str << "Not assigning ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700526 Item->dump(Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700527 Str << "\n";
528 } else {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700529 Str << (RegNum == Item->getRegNum() ? "Reassigning " : "Assigning ")
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700530 << Func->getTarget()->getRegName(RegNum, IceType_i32) << "(r"
531 << RegNum << ") to ";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700532 Item->dump(Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700533 Str << "\n";
534 }
535 }
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700536 Item->setRegNum(Item->getRegNumTmp());
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700537 }
538
539 // TODO: Consider running register allocation one more time, with
540 // infinite registers, for two reasons. First, evicted live ranges
541 // get a second chance for a register. Second, it allows coalescing
542 // of stack slots. If there is no time budget for the second
543 // register allocation run, each unallocated variable just gets its
544 // own slot.
545 //
546 // Another idea for coalescing stack slots is to initialize the
547 // Unhandled list with just the unallocated variables, saving time
548 // but not offering second-chance opportunities.
549}
550
551// ======================== Dump routines ======================== //
552
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700553void LinearScan::dump(Cfg *Func) const {
554 Ostream &Str = Func->getContext()->getStrDump();
555 if (!Func->getContext()->isVerbose(IceV_LinearScan))
556 return;
Jim Stichnoth800dab22014-09-20 12:25:02 -0700557 Func->resetCurrentNode();
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700558 Str << "**** Current regalloc state:\n";
559 Str << "++++++ Handled:\n";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700560 for (const Variable *Item : Handled) {
561 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700562 Str << "\n";
563 }
564 Str << "++++++ Unhandled:\n";
Jim Stichnothe22f8232014-10-13 16:20:59 -0700565 for (auto I = Unhandled.rbegin(), E = Unhandled.rend(); I != E; ++I) {
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700566 dumpLiveRange(*I, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700567 Str << "\n";
568 }
569 Str << "++++++ Active:\n";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700570 for (const Variable *Item : Active) {
571 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700572 Str << "\n";
573 }
574 Str << "++++++ Inactive:\n";
Jim Stichnoth5ce0abb2014-10-15 10:16:54 -0700575 for (const Variable *Item : Inactive) {
576 dumpLiveRange(Item, Func);
Jim Stichnothd97c7df2014-06-04 11:57:08 -0700577 Str << "\n";
578 }
579}
580
581} // end of namespace Ice