| OLD | NEW |
| 1 //===- subzero/src/IceRegAlloc.cpp - Linear-scan implementation -----------===// | 1 //===- subzero/src/IceRegAlloc.cpp - Linear-scan implementation -----------===// |
| 2 // | 2 // |
| 3 // The Subzero Code Generator | 3 // The Subzero Code Generator |
| 4 // | 4 // |
| 5 // This file is distributed under the University of Illinois Open Source | 5 // This file is distributed under the University of Illinois Open Source |
| 6 // License. See LICENSE.TXT for details. | 6 // License. See LICENSE.TXT for details. |
| 7 // | 7 // |
| 8 //===----------------------------------------------------------------------===// | 8 //===----------------------------------------------------------------------===// |
| 9 // | 9 // |
| 10 // This file implements the LinearScan class, which performs the | 10 // This file implements the LinearScan class, which performs the |
| 11 // linear-scan register allocation after liveness analysis has been | 11 // linear-scan register allocation after liveness analysis has been |
| 12 // performed. | 12 // performed. |
| 13 // | 13 // |
| 14 //===----------------------------------------------------------------------===// | 14 //===----------------------------------------------------------------------===// |
| 15 | 15 |
| 16 #include "IceCfg.h" | 16 #include "IceCfg.h" |
| 17 #include "IceCfgNode.h" |
| 17 #include "IceInst.h" | 18 #include "IceInst.h" |
| 18 #include "IceOperand.h" | 19 #include "IceOperand.h" |
| 19 #include "IceRegAlloc.h" | 20 #include "IceRegAlloc.h" |
| 20 #include "IceTargetLowering.h" | 21 #include "IceTargetLowering.h" |
| 21 | 22 |
| 22 namespace Ice { | 23 namespace Ice { |
| 23 | 24 |
| 24 namespace { | 25 namespace { |
| 25 | 26 |
| 26 // Returns true if Var has any definitions within Item's live range. | 27 // Returns true if Var has any definitions within Item's live range. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 65 const static size_t BufLen = 30; | 66 const static size_t BufLen = 30; |
| 66 char buf[BufLen]; | 67 char buf[BufLen]; |
| 67 snprintf(buf, BufLen, "%2d", Var->getRegNumTmp()); | 68 snprintf(buf, BufLen, "%2d", Var->getRegNumTmp()); |
| 68 Str << "R=" << buf << " V="; | 69 Str << "R=" << buf << " V="; |
| 69 Var->dump(Func); | 70 Var->dump(Func); |
| 70 Str << " Range=" << Var->getLiveRange(); | 71 Str << " Range=" << Var->getLiveRange(); |
| 71 } | 72 } |
| 72 | 73 |
| 73 } // end of anonymous namespace | 74 } // end of anonymous namespace |
| 74 | 75 |
| 76 void LinearScan::initForGlobalAlloc() { |
| 77 TimerMarker T(TimerStack::TT_initUnhandled, Func); |
| 78 Unhandled.clear(); |
| 79 UnhandledPrecolored.clear(); |
| 80 Handled.clear(); |
| 81 Inactive.clear(); |
| 82 Active.clear(); |
| 83 // Gather the live ranges of all variables and add them to the |
| 84 // Unhandled set. |
| 85 const VarList &Vars = Func->getVariables(); |
| 86 Unhandled.reserve(Vars.size()); |
| 87 for (Variable *Var : Vars) { |
| 88 // Explicitly don't consider zero-weight variables, which are |
| 89 // meant to be spill slots. |
| 90 if (Var->getWeight() == RegWeight::Zero) |
| 91 continue; |
| 92 // Don't bother if the variable has a null live range, which means |
| 93 // it was never referenced. |
| 94 if (Var->getLiveRange().isEmpty()) |
| 95 continue; |
| 96 Var->untrimLiveRange(); |
| 97 Unhandled.push_back(Var); |
| 98 if (Var->hasReg()) { |
| 99 Var->setRegNumTmp(Var->getRegNum()); |
| 100 Var->setLiveRangeInfiniteWeight(); |
| 101 UnhandledPrecolored.push_back(Var); |
| 102 } |
| 103 } |
| 104 struct CompareRanges { |
| 105 bool operator()(const Variable *L, const Variable *R) { |
| 106 InstNumberT Lstart = L->getLiveRange().getStart(); |
| 107 InstNumberT Rstart = R->getLiveRange().getStart(); |
| 108 if (Lstart == Rstart) |
| 109 return L->getIndex() < R->getIndex(); |
| 110 return Lstart < Rstart; |
| 111 } |
| 112 }; |
| 113 // Do a reverse sort so that erasing elements (from the end) is fast. |
| 114 std::sort(Unhandled.rbegin(), Unhandled.rend(), CompareRanges()); |
| 115 std::sort(UnhandledPrecolored.rbegin(), UnhandledPrecolored.rend(), |
| 116 CompareRanges()); |
| 117 |
| 118 // Build the (ordered) list of FakeKill instruction numbers. |
| 119 Kills.clear(); |
| 120 for (CfgNode *Node : Func->getNodes()) { |
| 121 for (auto I = Node->getInsts().begin(), E = Node->getInsts().end(); I != E; |
| 122 ++I) { |
| 123 if (I->isDeleted()) |
| 124 continue; |
| 125 if (auto Kill = llvm::dyn_cast<InstFakeKill>(I)) { |
| 126 if (!Kill->getLinked()->isDeleted()) |
| 127 Kills.push_back(I->getNumber()); |
| 128 } |
| 129 } |
| 130 } |
| 131 } |
| 132 |
| 75 // Implements the linear-scan algorithm. Based on "Linear Scan | 133 // Implements the linear-scan algorithm. Based on "Linear Scan |
| 76 // Register Allocation in the Context of SSA Form and Register | 134 // Register Allocation in the Context of SSA Form and Register |
| 77 // Constraints" by Hanspeter Mössenböck and Michael Pfeiffer, | 135 // Constraints" by Hanspeter Mössenböck and Michael Pfeiffer, |
| 78 // ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe02.PDF . This | 136 // ftp://ftp.ssw.uni-linz.ac.at/pub/Papers/Moe02.PDF . This |
| 79 // implementation is modified to take affinity into account and allow | 137 // implementation is modified to take affinity into account and allow |
| 80 // two interfering variables to share the same register in certain | 138 // two interfering variables to share the same register in certain |
| 81 // cases. | 139 // cases. |
| 82 // | 140 // |
| 83 // Requires running Cfg::liveness(Liveness_Intervals) in | 141 // Requires running Cfg::liveness(Liveness_Intervals) in |
| 84 // preparation. Results are assigned to Variable::RegNum for each | 142 // preparation. Results are assigned to Variable::RegNum for each |
| 85 // Variable. | 143 // Variable. |
| 86 void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { | 144 void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { |
| 87 TimerMarker T(TimerStack::TT_linearScan, Func); | 145 TimerMarker T(TimerStack::TT_linearScan, Func); |
| 88 assert(RegMaskFull.any()); // Sanity check | 146 assert(RegMaskFull.any()); // Sanity check |
| 89 Unhandled.clear(); | |
| 90 UnhandledPrecolored.clear(); | |
| 91 Handled.clear(); | |
| 92 Inactive.clear(); | |
| 93 Active.clear(); | |
| 94 Ostream &Str = Func->getContext()->getStrDump(); | 147 Ostream &Str = Func->getContext()->getStrDump(); |
| 95 bool Verbose = Func->getContext()->isVerbose(IceV_LinearScan); | 148 bool Verbose = Func->getContext()->isVerbose(IceV_LinearScan); |
| 96 Func->resetCurrentNode(); | 149 Func->resetCurrentNode(); |
| 97 VariablesMetadata *VMetadata = Func->getVMetadata(); | 150 VariablesMetadata *VMetadata = Func->getVMetadata(); |
| 98 | 151 |
| 99 // Gather the live ranges of all variables and add them to the | 152 // Build a LiveRange representing the Kills list. |
| 100 // Unhandled set. | 153 LiveRange KillsRange; |
| 101 const VarList &Vars = Func->getVariables(); | 154 for (InstNumberT I : Kills) |
| 102 { | 155 KillsRange.addSegment(I, I); |
| 103 TimerMarker T(TimerStack::TT_initUnhandled, Func); | 156 KillsRange.untrim(); |
| 104 Unhandled.reserve(Vars.size()); | |
| 105 for (Variable *Var : Vars) { | |
| 106 // 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; | |
| 114 Var->untrimLiveRange(); | |
| 115 Unhandled.push_back(Var); | |
| 116 if (Var->hasReg()) { | |
| 117 Var->setRegNumTmp(Var->getRegNum()); | |
| 118 Var->setLiveRangeInfiniteWeight(); | |
| 119 UnhandledPrecolored.push_back(Var); | |
| 120 } | |
| 121 } | |
| 122 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 }; | |
| 131 // Do a reverse sort so that erasing elements (from the end) is fast. | |
| 132 std::sort(Unhandled.rbegin(), Unhandled.rend(), CompareRanges()); | |
| 133 std::sort(UnhandledPrecolored.rbegin(), UnhandledPrecolored.rend(), | |
| 134 CompareRanges()); | |
| 135 } | |
| 136 | 157 |
| 137 // RegUses[I] is the number of live ranges (variables) that register | 158 // 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 | 159 // I is currently assigned to. It can be greater than 1 as a result |
| 139 // of AllowOverlap inference below. | 160 // of AllowOverlap inference below. |
| 140 std::vector<int> RegUses(RegMaskFull.size()); | 161 std::vector<int> RegUses(RegMaskFull.size()); |
| 141 // Unhandled is already set to all ranges in increasing order of | 162 // Unhandled is already set to all ranges in increasing order of |
| 142 // start points. | 163 // start points. |
| 143 assert(Active.empty()); | 164 assert(Active.empty()); |
| 144 assert(Inactive.empty()); | 165 assert(Inactive.empty()); |
| 145 assert(Handled.empty()); | 166 assert(Handled.empty()); |
| 146 UnorderedRanges::iterator Next; | 167 UnorderedRanges::iterator Next; |
| 168 const TargetLowering::RegSetMask RegsInclude = |
| 169 TargetLowering::RegSet_CallerSave; |
| 170 const TargetLowering::RegSetMask RegsExclude = TargetLowering::RegSet_None; |
| 171 const llvm::SmallBitVector KillsMask = |
| 172 Func->getTarget()->getRegisterSet(RegsInclude, RegsExclude); |
| 147 | 173 |
| 148 while (!Unhandled.empty()) { | 174 while (!Unhandled.empty()) { |
| 149 Variable *Cur = Unhandled.back(); | 175 Variable *Cur = Unhandled.back(); |
| 150 Unhandled.pop_back(); | 176 Unhandled.pop_back(); |
| 151 if (Verbose) { | 177 if (Verbose) { |
| 152 Str << "\nConsidering "; | 178 Str << "\nConsidering "; |
| 153 dumpLiveRange(Cur, Func); | 179 dumpLiveRange(Cur, Func); |
| 154 Str << "\n"; | 180 Str << "\n"; |
| 155 } | 181 } |
| 156 const llvm::SmallBitVector RegMask = | 182 const llvm::SmallBitVector RegMask = |
| 157 RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType()); | 183 RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType()); |
| 184 KillsRange.trim(Cur->getLiveRange().getStart()); |
| 158 | 185 |
| 159 // Check for precolored ranges. If Cur is precolored, it | 186 // Check for precolored ranges. If Cur is precolored, it |
| 160 // definitely gets that register. Previously processed live | 187 // definitely gets that register. Previously processed live |
| 161 // ranges would have avoided that register due to it being | 188 // ranges would have avoided that register due to it being |
| 162 // precolored. Future processed live ranges won't evict that | 189 // precolored. Future processed live ranges won't evict that |
| 163 // register because the live range has infinite weight. | 190 // register because the live range has infinite weight. |
| 164 if (Cur->hasReg()) { | 191 if (Cur->hasReg()) { |
| 165 int32_t RegNum = Cur->getRegNum(); | 192 int32_t RegNum = Cur->getRegNum(); |
| 166 // RegNumTmp should have already been set above. | 193 // RegNumTmp should have already been set above. |
| 167 assert(Cur->getRegNumTmp() == RegNum); | 194 assert(Cur->getRegNumTmp() == RegNum); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 213 assert(RegUses[RegNum] >= 0); | 240 assert(RegUses[RegNum] >= 0); |
| 214 } | 241 } |
| 215 } | 242 } |
| 216 | 243 |
| 217 // Check for inactive ranges that have expired or reactivated. | 244 // Check for inactive ranges that have expired or reactivated. |
| 218 for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) { | 245 for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) { |
| 219 Next = I; | 246 Next = I; |
| 220 ++Next; | 247 ++Next; |
| 221 Variable *Item = *I; | 248 Variable *Item = *I; |
| 222 Item->trimLiveRange(Cur->getLiveRange().getStart()); | 249 Item->trimLiveRange(Cur->getLiveRange().getStart()); |
| 223 // As an optimization, don't bother checking pure point-valued | |
| 224 // Inactive ranges, because the overlapsStart() test will never | |
| 225 // succeed, and the rangeEndsBefore() test will generally only | |
| 226 // 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. | |
| 230 if (!Item->getLiveRange().isNonpoints()) | |
| 231 continue; | |
| 232 if (Item->rangeEndsBefore(Cur)) { | 250 if (Item->rangeEndsBefore(Cur)) { |
| 233 // Move Item from Inactive to Handled list. | 251 // Move Item from Inactive to Handled list. |
| 234 if (Verbose) { | 252 if (Verbose) { |
| 235 Str << "Expiring "; | 253 Str << "Expiring "; |
| 236 dumpLiveRange(Item, Func); | 254 dumpLiveRange(Item, Func); |
| 237 Str << "\n"; | 255 Str << "\n"; |
| 238 } | 256 } |
| 239 Handled.splice(Handled.end(), Inactive, I); | 257 Handled.splice(Handled.end(), Inactive, I); |
| 240 } else if (Item->rangeOverlapsStart(Cur)) { | 258 } else if (Item->rangeOverlapsStart(Cur)) { |
| 241 // Move Item from Inactive to Active list. | 259 // Move Item from Inactive to Active list. |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 367 PrecoloredUnhandledMask[ItemReg] = true; | 385 PrecoloredUnhandledMask[ItemReg] = true; |
| 368 // Disable AllowOverlap if the preferred register is one of | 386 // Disable AllowOverlap if the preferred register is one of |
| 369 // these precolored unhandled overlapping ranges. | 387 // these precolored unhandled overlapping ranges. |
| 370 if (AllowOverlap && ItemReg == PreferReg) { | 388 if (AllowOverlap && ItemReg == PreferReg) { |
| 371 AllowOverlap = false; | 389 AllowOverlap = false; |
| 372 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); | 390 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); |
| 373 } | 391 } |
| 374 } | 392 } |
| 375 } | 393 } |
| 376 | 394 |
| 395 // Remove scratch registers from the Free[] list, and mark their |
| 396 // Weights[] as infinite, if KillsRange overlaps Cur's live range. |
| 397 const bool UseTrimmed = true; |
| 398 if (Cur->getLiveRange().overlaps(KillsRange, UseTrimmed)) { |
| 399 Free.reset(KillsMask); |
| 400 for (int i = KillsMask.find_first(); i != -1; |
| 401 i = KillsMask.find_next(i)) { |
| 402 Weights[i].setWeight(RegWeight::Inf); |
| 403 if (PreferReg == i) |
| 404 AllowOverlap = false; |
| 405 } |
| 406 } |
| 407 |
| 377 // Print info about physical register availability. | 408 // Print info about physical register availability. |
| 378 if (Verbose) { | 409 if (Verbose) { |
| 379 for (SizeT i = 0; i < RegMask.size(); ++i) { | 410 for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 380 if (RegMask[i]) { | 411 if (RegMask[i]) { |
| 381 Str << Func->getTarget()->getRegName(i, IceType_i32) | 412 Str << Func->getTarget()->getRegName(i, IceType_i32) |
| 382 << "(U=" << RegUses[i] << ",F=" << Free[i] | 413 << "(U=" << RegUses[i] << ",F=" << Free[i] |
| 383 << ",P=" << PrecoloredUnhandledMask[i] << ") "; | 414 << ",P=" << PrecoloredUnhandledMask[i] << ") "; |
| 384 } | 415 } |
| 385 } | 416 } |
| 386 Str << "\n"; | 417 Str << "\n"; |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 572 Str << "\n"; | 603 Str << "\n"; |
| 573 } | 604 } |
| 574 Str << "++++++ Inactive:\n"; | 605 Str << "++++++ Inactive:\n"; |
| 575 for (const Variable *Item : Inactive) { | 606 for (const Variable *Item : Inactive) { |
| 576 dumpLiveRange(Item, Func); | 607 dumpLiveRange(Item, Func); |
| 577 Str << "\n"; | 608 Str << "\n"; |
| 578 } | 609 } |
| 579 } | 610 } |
| 580 | 611 |
| 581 } // end of namespace Ice | 612 } // end of namespace Ice |
| OLD | NEW |