Chromium Code Reviews| 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; |
| 147 | 168 |
| 148 while (!Unhandled.empty()) { | 169 while (!Unhandled.empty()) { |
| 149 Variable *Cur = Unhandled.back(); | 170 Variable *Cur = Unhandled.back(); |
| 150 Unhandled.pop_back(); | 171 Unhandled.pop_back(); |
| 151 if (Verbose) { | 172 if (Verbose) { |
| 152 Str << "\nConsidering "; | 173 Str << "\nConsidering "; |
| 153 dumpLiveRange(Cur, Func); | 174 dumpLiveRange(Cur, Func); |
| 154 Str << "\n"; | 175 Str << "\n"; |
| 155 } | 176 } |
| 156 const llvm::SmallBitVector RegMask = | 177 const llvm::SmallBitVector RegMask = |
| 157 RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType()); | 178 RegMaskFull & Func->getTarget()->getRegisterSetForType(Cur->getType()); |
| 179 KillsRange.trim(Cur->getLiveRange().getStart()); | |
| 180 llvm::SmallBitVector KillsMask = Func->getTarget()->getRegisterSet( | |
|
jvoung (off chromium)
2014/11/14 16:47:23
Can the kills mask be lifted out of the while loop
Jim Stichnoth
2014/11/14 18:27:03
Good point, done.
| |
| 181 TargetLowering::RegSet_CallerSave, TargetLowering::RegSet_None); | |
| 158 | 182 |
| 159 // Check for precolored ranges. If Cur is precolored, it | 183 // Check for precolored ranges. If Cur is precolored, it |
| 160 // definitely gets that register. Previously processed live | 184 // definitely gets that register. Previously processed live |
| 161 // ranges would have avoided that register due to it being | 185 // ranges would have avoided that register due to it being |
| 162 // precolored. Future processed live ranges won't evict that | 186 // precolored. Future processed live ranges won't evict that |
| 163 // register because the live range has infinite weight. | 187 // register because the live range has infinite weight. |
| 164 if (Cur->hasReg()) { | 188 if (Cur->hasReg()) { |
| 165 int32_t RegNum = Cur->getRegNum(); | 189 int32_t RegNum = Cur->getRegNum(); |
| 166 // RegNumTmp should have already been set above. | 190 // RegNumTmp should have already been set above. |
| 167 assert(Cur->getRegNumTmp() == RegNum); | 191 assert(Cur->getRegNumTmp() == RegNum); |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 213 assert(RegUses[RegNum] >= 0); | 237 assert(RegUses[RegNum] >= 0); |
| 214 } | 238 } |
| 215 } | 239 } |
| 216 | 240 |
| 217 // Check for inactive ranges that have expired or reactivated. | 241 // Check for inactive ranges that have expired or reactivated. |
| 218 for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) { | 242 for (auto I = Inactive.begin(), E = Inactive.end(); I != E; I = Next) { |
| 219 Next = I; | 243 Next = I; |
| 220 ++Next; | 244 ++Next; |
| 221 Variable *Item = *I; | 245 Variable *Item = *I; |
| 222 Item->trimLiveRange(Cur->getLiveRange().getStart()); | 246 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)) { | 247 if (Item->rangeEndsBefore(Cur)) { |
| 233 // Move Item from Inactive to Handled list. | 248 // Move Item from Inactive to Handled list. |
| 234 if (Verbose) { | 249 if (Verbose) { |
| 235 Str << "Expiring "; | 250 Str << "Expiring "; |
| 236 dumpLiveRange(Item, Func); | 251 dumpLiveRange(Item, Func); |
| 237 Str << "\n"; | 252 Str << "\n"; |
| 238 } | 253 } |
| 239 Handled.splice(Handled.end(), Inactive, I); | 254 Handled.splice(Handled.end(), Inactive, I); |
| 240 } else if (Item->rangeOverlapsStart(Cur)) { | 255 } else if (Item->rangeOverlapsStart(Cur)) { |
| 241 // Move Item from Inactive to Active list. | 256 // Move Item from Inactive to Active list. |
| (...skipping 76 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 318 if (Item->rangeOverlaps(Cur)) { | 333 if (Item->rangeOverlaps(Cur)) { |
| 319 int32_t RegNum = Item->getRegNumTmp(); | 334 int32_t RegNum = Item->getRegNumTmp(); |
| 320 // Don't assert(Free[RegNum]) because in theory (though | 335 // Don't assert(Free[RegNum]) because in theory (though |
| 321 // probably never in practice) there could be two inactive | 336 // probably never in practice) there could be two inactive |
| 322 // variables that were marked with AllowOverlap. | 337 // variables that were marked with AllowOverlap. |
| 323 Free[RegNum] = false; | 338 Free[RegNum] = false; |
| 324 // Disable AllowOverlap if an Inactive variable, which is not | 339 // Disable AllowOverlap if an Inactive variable, which is not |
| 325 // Prefer, shares Prefer's register, and has a definition | 340 // Prefer, shares Prefer's register, and has a definition |
| 326 // within Cur's live range. | 341 // within Cur's live range. |
| 327 if (AllowOverlap && Item != Prefer && RegNum == PreferReg && | 342 if (AllowOverlap && Item != Prefer && RegNum == PreferReg && |
| 328 overlapsDefs(Func, Cur, Item)) { | 343 overlapsDefs(Func, Cur, Item)) { |
|
Jim Stichnoth
2014/11/14 00:51:13
It turns out this code had a bug, because a pre-co
| |
| 329 AllowOverlap = false; | 344 AllowOverlap = false; |
| 330 dumpDisableOverlap(Func, Item, "Inactive"); | 345 dumpDisableOverlap(Func, Item, "Inactive"); |
| 331 } | 346 } |
| 332 } | 347 } |
| 333 } | 348 } |
| 334 | 349 |
| 335 // Disable AllowOverlap if an Active variable, which is not | 350 // Disable AllowOverlap if an Active variable, which is not |
| 336 // Prefer, shares Prefer's register, and has a definition within | 351 // Prefer, shares Prefer's register, and has a definition within |
| 337 // Cur's live range. | 352 // Cur's live range. |
| 338 for (const Variable *Item : Active) { | 353 for (const Variable *Item : Active) { |
| (...skipping 28 matching lines...) Expand all Loading... | |
| 367 PrecoloredUnhandledMask[ItemReg] = true; | 382 PrecoloredUnhandledMask[ItemReg] = true; |
| 368 // Disable AllowOverlap if the preferred register is one of | 383 // Disable AllowOverlap if the preferred register is one of |
| 369 // these precolored unhandled overlapping ranges. | 384 // these precolored unhandled overlapping ranges. |
| 370 if (AllowOverlap && ItemReg == PreferReg) { | 385 if (AllowOverlap && ItemReg == PreferReg) { |
| 371 AllowOverlap = false; | 386 AllowOverlap = false; |
| 372 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); | 387 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); |
| 373 } | 388 } |
| 374 } | 389 } |
| 375 } | 390 } |
| 376 | 391 |
| 392 // Remove scratch registers from the Free[] list, and mark their | |
| 393 // Weights[] as infinite, if KillsRange overlaps Cur's live range. | |
| 394 const bool UseTrimmed = true; | |
| 395 if (Cur->getLiveRange().overlaps(KillsRange, UseTrimmed)) { | |
| 396 Free.reset(KillsMask); | |
| 397 for (int i = KillsMask.find_first(); i != -1; | |
| 398 i = KillsMask.find_next(i)) { | |
| 399 Weights[i].setWeight(RegWeight::Inf); | |
| 400 if (PreferReg == i) | |
| 401 AllowOverlap = false; | |
| 402 } | |
| 403 } | |
| 404 | |
| 377 // Print info about physical register availability. | 405 // Print info about physical register availability. |
| 378 if (Verbose) { | 406 if (Verbose) { |
| 379 for (SizeT i = 0; i < RegMask.size(); ++i) { | 407 for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 380 if (RegMask[i]) { | 408 if (RegMask[i]) { |
| 381 Str << Func->getTarget()->getRegName(i, IceType_i32) | 409 Str << Func->getTarget()->getRegName(i, IceType_i32) |
| 382 << "(U=" << RegUses[i] << ",F=" << Free[i] | 410 << "(U=" << RegUses[i] << ",F=" << Free[i] |
| 383 << ",P=" << PrecoloredUnhandledMask[i] << ") "; | 411 << ",P=" << PrecoloredUnhandledMask[i] << ") "; |
| 384 } | 412 } |
| 385 } | 413 } |
| 386 Str << "\n"; | 414 Str << "\n"; |
| (...skipping 185 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 572 Str << "\n"; | 600 Str << "\n"; |
| 573 } | 601 } |
| 574 Str << "++++++ Inactive:\n"; | 602 Str << "++++++ Inactive:\n"; |
| 575 for (const Variable *Item : Inactive) { | 603 for (const Variable *Item : Inactive) { |
| 576 dumpLiveRange(Item, Func); | 604 dumpLiveRange(Item, Func); |
| 577 Str << "\n"; | 605 Str << "\n"; |
| 578 } | 606 } |
| 579 } | 607 } |
| 580 | 608 |
| 581 } // end of namespace Ice | 609 } // end of namespace Ice |
| OLD | NEW |