| 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 |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 61 // cases. | 61 // cases. |
| 62 // | 62 // |
| 63 // Requires running Cfg::liveness(Liveness_Intervals) in | 63 // Requires running Cfg::liveness(Liveness_Intervals) in |
| 64 // preparation. Results are assigned to Variable::RegNum for each | 64 // preparation. Results are assigned to Variable::RegNum for each |
| 65 // Variable. | 65 // Variable. |
| 66 void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { | 66 void LinearScan::scan(const llvm::SmallBitVector &RegMaskFull) { |
| 67 static TimerIdT IDscan = GlobalContext::getTimerID("linearScan"); | 67 static TimerIdT IDscan = GlobalContext::getTimerID("linearScan"); |
| 68 TimerMarker T(IDscan, Func->getContext()); | 68 TimerMarker T(IDscan, Func->getContext()); |
| 69 assert(RegMaskFull.any()); // Sanity check | 69 assert(RegMaskFull.any()); // Sanity check |
| 70 Unhandled.clear(); | 70 Unhandled.clear(); |
| 71 UnhandledPrecolored.clear(); |
| 71 Handled.clear(); | 72 Handled.clear(); |
| 72 Inactive.clear(); | 73 Inactive.clear(); |
| 73 Active.clear(); | 74 Active.clear(); |
| 74 Ostream &Str = Func->getContext()->getStrDump(); | 75 Ostream &Str = Func->getContext()->getStrDump(); |
| 75 bool Verbose = Func->getContext()->isVerbose(IceV_LinearScan); | 76 bool Verbose = Func->getContext()->isVerbose(IceV_LinearScan); |
| 76 Func->resetCurrentNode(); | 77 Func->resetCurrentNode(); |
| 77 VariablesMetadata *VMetadata = Func->getVMetadata(); | 78 VariablesMetadata *VMetadata = Func->getVMetadata(); |
| 78 | 79 |
| 79 // Gather the live ranges of all variables and add them to the | 80 // Gather the live ranges of all variables and add them to the |
| 80 // Unhandled set. TODO: Unhandled is a set<> which is based on a | 81 // Unhandled set. TODO: Unhandled is a set<> which is based on a |
| 81 // balanced binary tree, so inserting live ranges for N variables is | 82 // balanced binary tree, so inserting live ranges for N variables is |
| 82 // O(N log N) complexity. N may be proportional to the number of | 83 // O(N log N) complexity. N may be proportional to the number of |
| 83 // instructions, thanks to temporary generation during lowering. As | 84 // instructions, thanks to temporary generation during lowering. As |
| 84 // a result, it may be useful to design a better data structure for | 85 // a result, it may be useful to design a better data structure for |
| 85 // storing Func->getVariables(). | 86 // storing Func->getVariables(). |
| 86 const VarList &Vars = Func->getVariables(); | 87 const VarList &Vars = Func->getVariables(); |
| 87 { | 88 { |
| 88 static TimerIdT IDinitUnhandled = | 89 static TimerIdT IDinitUnhandled = |
| 89 GlobalContext::getTimerID("initUnhandled"); | 90 GlobalContext::getTimerID("initUnhandled"); |
| 90 TimerMarker T(IDinitUnhandled, Func->getContext()); | 91 TimerMarker T(IDinitUnhandled, Func->getContext()); |
| 91 for (Variable *Var : Vars) { | 92 for (Variable *Var : Vars) { |
| 92 // Explicitly don't consider zero-weight variables, which are | 93 // Explicitly don't consider zero-weight variables, which are |
| 93 // meant to be spill slots. | 94 // meant to be spill slots. |
| 94 if (Var->getWeight() == RegWeight::Zero) | 95 if (Var->getWeight() == RegWeight::Zero) |
| 95 continue; | 96 continue; |
| 96 // Don't bother if the variable has a null live range, which means | 97 // Don't bother if the variable has a null live range, which means |
| 97 // it was never referenced. | 98 // it was never referenced. |
| 98 if (Var->getLiveRange().isEmpty()) | 99 if (Var->getLiveRange().isEmpty()) |
| 99 continue; | 100 continue; |
| 100 Unhandled.insert(LiveRangeWrapper(Var)); | 101 LiveRangeWrapper R(Var); |
| 102 Unhandled.insert(R); |
| 101 if (Var->hasReg()) { | 103 if (Var->hasReg()) { |
| 102 Var->setRegNumTmp(Var->getRegNum()); | 104 Var->setRegNumTmp(Var->getRegNum()); |
| 103 Var->setLiveRangeInfiniteWeight(); | 105 Var->setLiveRangeInfiniteWeight(); |
| 106 UnhandledPrecolored.insert(R); |
| 104 } | 107 } |
| 105 } | 108 } |
| 106 } | 109 } |
| 107 | 110 |
| 108 // RegUses[I] is the number of live ranges (variables) that register | 111 // RegUses[I] is the number of live ranges (variables) that register |
| 109 // I is currently assigned to. It can be greater than 1 as a result | 112 // I is currently assigned to. It can be greater than 1 as a result |
| 110 // of AllowOverlap inference below. | 113 // of AllowOverlap inference below. |
| 111 std::vector<int> RegUses(RegMaskFull.size()); | 114 std::vector<int> RegUses(RegMaskFull.size()); |
| 112 // Unhandled is already set to all ranges in increasing order of | 115 // Unhandled is already set to all ranges in increasing order of |
| 113 // start points. | 116 // start points. |
| (...skipping 24 matching lines...) Expand all Loading... |
| 138 // RegNumTmp should have already been set above. | 141 // RegNumTmp should have already been set above. |
| 139 assert(Cur.Var->getRegNumTmp() == RegNum); | 142 assert(Cur.Var->getRegNumTmp() == RegNum); |
| 140 if (Verbose) { | 143 if (Verbose) { |
| 141 Str << "Precoloring "; | 144 Str << "Precoloring "; |
| 142 Cur.dump(Func); | 145 Cur.dump(Func); |
| 143 Str << "\n"; | 146 Str << "\n"; |
| 144 } | 147 } |
| 145 Active.push_back(Cur); | 148 Active.push_back(Cur); |
| 146 assert(RegUses[RegNum] >= 0); | 149 assert(RegUses[RegNum] >= 0); |
| 147 ++RegUses[RegNum]; | 150 ++RegUses[RegNum]; |
| 151 assert(!UnhandledPrecolored.empty()); |
| 152 assert(UnhandledPrecolored.begin()->Var == Cur.Var); |
| 153 UnhandledPrecolored.erase(UnhandledPrecolored.begin()); |
| 148 continue; | 154 continue; |
| 149 } | 155 } |
| 150 | 156 |
| 151 // Check for active ranges that have expired or become inactive. | 157 // Check for active ranges that have expired or become inactive. |
| 152 for (auto I = Active.begin(), E = Active.end(); I != E; I = Next) { | 158 for (auto I = Active.begin(), E = Active.end(); I != E; I = Next) { |
| 153 Next = I; | 159 Next = I; |
| 154 ++Next; | 160 ++Next; |
| 155 LiveRangeWrapper Item = *I; | 161 LiveRangeWrapper Item = *I; |
| 156 bool Moved = false; | 162 bool Moved = false; |
| 157 if (Item.endsBefore(Cur)) { | 163 if (Item.endsBefore(Cur)) { |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 299 // Cur's live range. | 305 // Cur's live range. |
| 300 for (const LiveRangeWrapper &Item : Active) { | 306 for (const LiveRangeWrapper &Item : Active) { |
| 301 int32_t RegNum = Item.Var->getRegNumTmp(); | 307 int32_t RegNum = Item.Var->getRegNumTmp(); |
| 302 if (Item.Var != Prefer && RegNum == PreferReg && | 308 if (Item.Var != Prefer && RegNum == PreferReg && |
| 303 overlapsDefs(Func, Cur, Item.Var)) { | 309 overlapsDefs(Func, Cur, Item.Var)) { |
| 304 AllowOverlap = false; | 310 AllowOverlap = false; |
| 305 dumpDisableOverlap(Func, Item.Var, "Active"); | 311 dumpDisableOverlap(Func, Item.Var, "Active"); |
| 306 } | 312 } |
| 307 } | 313 } |
| 308 | 314 |
| 309 // Remove registers from the Free[] list where an Unhandled range | 315 std::vector<RegWeight> Weights(RegMask.size()); |
| 310 // overlaps with the current range and is precolored. | 316 |
| 311 // Cur.endsBefore(Item) is an early exit check that turns a | 317 // Remove registers from the Free[] list where an Unhandled |
| 312 // guaranteed O(N^2) algorithm into expected linear complexity. | 318 // precolored range overlaps with the current range, and set those |
| 313 llvm::SmallBitVector PrecoloredUnhandled(RegMask.size()); | 319 // registers to infinite weight so that they aren't candidates for |
| 314 // Note: PrecoloredUnhandled is only used for dumping. | 320 // eviction. Cur.endsBefore(Item) is an early exit check that |
| 315 for (const LiveRangeWrapper &Item : Unhandled) { | 321 // turns a guaranteed O(N^2) algorithm into expected linear |
| 322 // complexity. |
| 323 llvm::SmallBitVector PrecoloredUnhandledMask(RegMask.size()); |
| 324 // Note: PrecoloredUnhandledMask is only used for dumping. |
| 325 for (const LiveRangeWrapper &Item : UnhandledPrecolored) { |
| 326 assert(Item.Var->hasReg()); |
| 316 if (Cur.endsBefore(Item)) | 327 if (Cur.endsBefore(Item)) |
| 317 break; | 328 break; |
| 318 if (Item.Var->hasReg() && Item.overlaps(Cur)) { | 329 if (Item.overlaps(Cur)) { |
| 319 int32_t ItemReg = Item.Var->getRegNum(); // Note: not getRegNumTmp() | 330 int32_t ItemReg = Item.Var->getRegNum(); // Note: not getRegNumTmp() |
| 331 Weights[ItemReg].setWeight(RegWeight::Inf); |
| 320 Free[ItemReg] = false; | 332 Free[ItemReg] = false; |
| 321 PrecoloredUnhandled[ItemReg] = true; | 333 PrecoloredUnhandledMask[ItemReg] = true; |
| 322 // Disable AllowOverlap if the preferred register is one of | 334 // Disable AllowOverlap if the preferred register is one of |
| 323 // these precolored unhandled overlapping ranges. | 335 // these precolored unhandled overlapping ranges. |
| 324 if (AllowOverlap && ItemReg == PreferReg) { | 336 if (AllowOverlap && ItemReg == PreferReg) { |
| 325 AllowOverlap = false; | 337 AllowOverlap = false; |
| 326 dumpDisableOverlap(Func, Item.Var, "PrecoloredUnhandled"); | 338 dumpDisableOverlap(Func, Item.Var, "PrecoloredUnhandled"); |
| 327 } | 339 } |
| 328 } | 340 } |
| 329 } | 341 } |
| 330 | 342 |
| 331 // Print info about physical register availability. | 343 // Print info about physical register availability. |
| 332 if (Verbose) { | 344 if (Verbose) { |
| 333 for (SizeT i = 0; i < RegMask.size(); ++i) { | 345 for (SizeT i = 0; i < RegMask.size(); ++i) { |
| 334 if (RegMask[i]) { | 346 if (RegMask[i]) { |
| 335 Str << Func->getTarget()->getRegName(i, IceType_i32) | 347 Str << Func->getTarget()->getRegName(i, IceType_i32) |
| 336 << "(U=" << RegUses[i] << ",F=" << Free[i] | 348 << "(U=" << RegUses[i] << ",F=" << Free[i] |
| 337 << ",P=" << PrecoloredUnhandled[i] << ") "; | 349 << ",P=" << PrecoloredUnhandledMask[i] << ") "; |
| 338 } | 350 } |
| 339 } | 351 } |
| 340 Str << "\n"; | 352 Str << "\n"; |
| 341 } | 353 } |
| 342 | 354 |
| 343 if (Prefer && (AllowOverlap || Free[PreferReg])) { | 355 if (Prefer && (AllowOverlap || Free[PreferReg])) { |
| 344 // First choice: a preferred register that is either free or is | 356 // First choice: a preferred register that is either free or is |
| 345 // allowed to overlap with its linked variable. | 357 // allowed to overlap with its linked variable. |
| 346 Cur.Var->setRegNumTmp(PreferReg); | 358 Cur.Var->setRegNumTmp(PreferReg); |
| 347 if (Verbose) { | 359 if (Verbose) { |
| (...skipping 14 matching lines...) Expand all Loading... |
| 362 Str << "Allocating "; | 374 Str << "Allocating "; |
| 363 Cur.dump(Func); | 375 Cur.dump(Func); |
| 364 Str << "\n"; | 376 Str << "\n"; |
| 365 } | 377 } |
| 366 assert(RegUses[RegNum] >= 0); | 378 assert(RegUses[RegNum] >= 0); |
| 367 ++RegUses[RegNum]; | 379 ++RegUses[RegNum]; |
| 368 Active.push_back(Cur); | 380 Active.push_back(Cur); |
| 369 } else { | 381 } else { |
| 370 // Fallback: there are no free registers, so we look for the | 382 // Fallback: there are no free registers, so we look for the |
| 371 // lowest-weight register and see if Cur has higher weight. | 383 // lowest-weight register and see if Cur has higher weight. |
| 372 std::vector<RegWeight> Weights(RegMask.size()); | |
| 373 // Check Active ranges. | 384 // Check Active ranges. |
| 374 for (const LiveRangeWrapper &Item : Active) { | 385 for (const LiveRangeWrapper &Item : Active) { |
| 375 assert(Item.overlaps(Cur)); | 386 assert(Item.overlaps(Cur)); |
| 376 int32_t RegNum = Item.Var->getRegNumTmp(); | 387 int32_t RegNum = Item.Var->getRegNumTmp(); |
| 377 assert(Item.Var->hasRegTmp()); | 388 assert(Item.Var->hasRegTmp()); |
| 378 Weights[RegNum].addWeight(Item.range().getWeight()); | 389 Weights[RegNum].addWeight(Item.range().getWeight()); |
| 379 } | 390 } |
| 380 // Same as above, but check Inactive ranges instead of Active. | 391 // Same as above, but check Inactive ranges instead of Active. |
| 381 for (const LiveRangeWrapper &Item : Inactive) { | 392 for (const LiveRangeWrapper &Item : Inactive) { |
| 382 int32_t RegNum = Item.Var->getRegNumTmp(); | 393 int32_t RegNum = Item.Var->getRegNumTmp(); |
| 383 assert(Item.Var->hasRegTmp()); | 394 assert(Item.Var->hasRegTmp()); |
| 384 if (Item.overlaps(Cur)) | 395 if (Item.overlaps(Cur)) |
| 385 Weights[RegNum].addWeight(Item.range().getWeight()); | 396 Weights[RegNum].addWeight(Item.range().getWeight()); |
| 386 } | 397 } |
| 387 // Check Unhandled ranges that overlap Cur and are precolored. | |
| 388 // Cur.endsBefore(*I) is an early exit check that turns a | |
| 389 // guaranteed O(N^2) algorithm into expected linear complexity. | |
| 390 for (const LiveRangeWrapper &Item : Unhandled) { | |
| 391 if (Cur.endsBefore(Item)) | |
| 392 break; | |
| 393 int32_t RegNum = Item.Var->getRegNumTmp(); | |
| 394 if (RegNum < 0) | |
| 395 continue; | |
| 396 if (Item.overlaps(Cur)) | |
| 397 Weights[RegNum].setWeight(RegWeight::Inf); | |
| 398 } | |
| 399 | 398 |
| 400 // All the weights are now calculated. Find the register with | 399 // All the weights are now calculated. Find the register with |
| 401 // smallest weight. | 400 // smallest weight. |
| 402 int32_t MinWeightIndex = RegMask.find_first(); | 401 int32_t MinWeightIndex = RegMask.find_first(); |
| 403 // MinWeightIndex must be valid because of the initial | 402 // MinWeightIndex must be valid because of the initial |
| 404 // RegMask.any() test. | 403 // RegMask.any() test. |
| 405 assert(MinWeightIndex >= 0); | 404 assert(MinWeightIndex >= 0); |
| 406 for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) { | 405 for (SizeT i = MinWeightIndex + 1; i < Weights.size(); ++i) { |
| 407 if (RegMask[i] && Weights[i] < Weights[MinWeightIndex]) | 406 if (RegMask[i] && Weights[i] < Weights[MinWeightIndex]) |
| 408 MinWeightIndex = i; | 407 MinWeightIndex = i; |
| (...skipping 142 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 551 Str << "\n"; | 550 Str << "\n"; |
| 552 } | 551 } |
| 553 Str << "++++++ Inactive:\n"; | 552 Str << "++++++ Inactive:\n"; |
| 554 for (const LiveRangeWrapper &Item : Inactive) { | 553 for (const LiveRangeWrapper &Item : Inactive) { |
| 555 Item.dump(Func); | 554 Item.dump(Func); |
| 556 Str << "\n"; | 555 Str << "\n"; |
| 557 } | 556 } |
| 558 } | 557 } |
| 559 | 558 |
| 560 } // end of namespace Ice | 559 } // end of namespace Ice |
| OLD | NEW |