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 /// \file | 10 /// \file |
(...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
63 Str << "," << Defs[i]->getNumber(); | 63 Str << "," << Defs[i]->getNumber(); |
64 } | 64 } |
65 Str << "\n"; | 65 Str << "\n"; |
66 } | 66 } |
67 | 67 |
68 void dumpLiveRange(const Variable *Var, const Cfg *Func) { | 68 void dumpLiveRange(const Variable *Var, const Cfg *Func) { |
69 if (!BuildDefs::dump()) | 69 if (!BuildDefs::dump()) |
70 return; | 70 return; |
71 Ostream &Str = Func->getContext()->getStrDump(); | 71 Ostream &Str = Func->getContext()->getStrDump(); |
72 char buf[30]; | 72 char buf[30]; |
73 snprintf(buf, llvm::array_lengthof(buf), "%2d", Var->getRegNumTmp()); | 73 snprintf(buf, llvm::array_lengthof(buf), "%2u", |
74 unsigned(Var->getRegNumTmp())); | |
74 Str << "R=" << buf << " V="; | 75 Str << "R=" << buf << " V="; |
75 Var->dump(Func); | 76 Var->dump(Func); |
76 Str << " Range=" << Var->getLiveRange(); | 77 Str << " Range=" << Var->getLiveRange(); |
77 } | 78 } |
78 | 79 |
79 int32_t findMinWeightIndex( | 80 int32_t findMinWeightIndex( |
80 const llvm::SmallBitVector &RegMask, | 81 const llvm::SmallBitVector &RegMask, |
81 const llvm::SmallVector<RegWeight, LinearScan::REGS_SIZE> &Weights) { | 82 const llvm::SmallVector<RegWeight, LinearScan::REGS_SIZE> &Weights) { |
82 int32_t MinWeightIndex = RegMask.find_first(); | 83 int MinWeightIndex = RegMask.find_first(); |
83 assert(MinWeightIndex >= 0); | 84 assert(MinWeightIndex >= 0); |
84 for (int32_t i = RegMask.find_next(MinWeightIndex); i != -1; | 85 for (int i = RegMask.find_next(MinWeightIndex); i != -1; |
85 i = RegMask.find_next(i)) { | 86 i = RegMask.find_next(i)) { |
86 if (Weights[i] < Weights[MinWeightIndex]) | 87 if (Weights[i] < Weights[MinWeightIndex]) |
87 MinWeightIndex = i; | 88 MinWeightIndex = i; |
88 } | 89 } |
89 return MinWeightIndex; | 90 return MinWeightIndex; |
90 } | 91 } |
91 | 92 |
92 } // end of anonymous namespace | 93 } // end of anonymous namespace |
93 | 94 |
94 LinearScan::LinearScan(Cfg *Func) | 95 LinearScan::LinearScan(Cfg *Func) |
(...skipping 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
407 if (I->getNumber() == End) | 408 if (I->getNumber() == End) |
408 FillPoint = I; | 409 FillPoint = I; |
409 if (SpillPoint != E) { | 410 if (SpillPoint != E) { |
410 // Remove from RegMask any physical registers referenced during Cur's live | 411 // Remove from RegMask any physical registers referenced during Cur's live |
411 // range. Start looking after SpillPoint gets set, i.e. once Cur's live | 412 // range. Start looking after SpillPoint gets set, i.e. once Cur's live |
412 // range begins. | 413 // range begins. |
413 FOREACH_VAR_IN_INST(Var, *I) { | 414 FOREACH_VAR_IN_INST(Var, *I) { |
414 if (!Var->hasRegTmp()) | 415 if (!Var->hasRegTmp()) |
415 continue; | 416 continue; |
416 const llvm::SmallBitVector &Aliases = *RegAliases[Var->getRegNumTmp()]; | 417 const llvm::SmallBitVector &Aliases = *RegAliases[Var->getRegNumTmp()]; |
417 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 418 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
418 RegAlias = Aliases.find_next(RegAlias)) { | 419 RegAlias = Aliases.find_next(RegAlias)) { |
419 Iter.RegMask[RegAlias] = false; | 420 Iter.RegMask[RegAlias] = false; |
420 } | 421 } |
421 } | 422 } |
422 } | 423 } |
423 } | 424 } |
424 assert(SpillPoint != Insts.end()); | 425 assert(SpillPoint != Insts.end()); |
425 assert(FillPoint != Insts.end()); | 426 assert(FillPoint != Insts.end()); |
426 ++FillPoint; | 427 ++FillPoint; |
427 // TODO(stichnot): Randomize instead of find_first(). | 428 // TODO(stichnot): Randomize instead of find_first(). |
428 int32_t RegNum = Iter.RegMask.find_first(); | 429 int RegNum = Iter.RegMask.find_first(); |
429 assert(RegNum != -1); | 430 assert(RegNum != -1); |
430 Iter.Cur->setRegNumTmp(RegNum); | 431 Iter.Cur->setRegNumTmp(RegNum); |
431 Variable *Preg = Target->getPhysicalRegister(RegNum, Iter.Cur->getType()); | 432 Variable *Preg = Target->getPhysicalRegister(RegNum, Iter.Cur->getType()); |
432 // TODO(stichnot): Add SpillLoc to VariablesMetadata tracking so that SpillLoc | 433 // TODO(stichnot): Add SpillLoc to VariablesMetadata tracking so that SpillLoc |
433 // is correctly identified as !isMultiBlock(), reducing stack frame size. | 434 // is correctly identified as !isMultiBlock(), reducing stack frame size. |
434 Variable *SpillLoc = Func->makeVariable(Iter.Cur->getType()); | 435 Variable *SpillLoc = Func->makeVariable(Iter.Cur->getType()); |
435 // Add "reg=FakeDef;spill=reg" before SpillPoint | 436 // Add "reg=FakeDef;spill=reg" before SpillPoint |
436 Target->lowerInst(Node, SpillPoint, InstFakeDef::create(Func, Preg)); | 437 Target->lowerInst(Node, SpillPoint, InstFakeDef::create(Func, Preg)); |
437 Target->lowerInst(Node, SpillPoint, InstAssign::create(Func, SpillLoc, Preg)); | 438 Target->lowerInst(Node, SpillPoint, InstAssign::create(Func, SpillLoc, Preg)); |
438 // add "reg=spill;FakeUse(reg)" before FillPoint | 439 // add "reg=spill;FakeUse(reg)" before FillPoint |
(...skipping 15 matching lines...) Expand all Loading... | |
454 } else if (!Item->rangeOverlapsStart(Cur)) { | 455 } else if (!Item->rangeOverlapsStart(Cur)) { |
455 // Move Item from Active to Inactive list. | 456 // Move Item from Active to Inactive list. |
456 dumpLiveRangeTrace("Inactivating ", Item); | 457 dumpLiveRangeTrace("Inactivating ", Item); |
457 moveItem(Active, Index, Inactive); | 458 moveItem(Active, Index, Inactive); |
458 Moved = true; | 459 Moved = true; |
459 } | 460 } |
460 if (Moved) { | 461 if (Moved) { |
461 // Decrement Item from RegUses[]. | 462 // Decrement Item from RegUses[]. |
462 assert(Item->hasRegTmp()); | 463 assert(Item->hasRegTmp()); |
463 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; | 464 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; |
464 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 465 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
465 RegAlias = Aliases.find_next(RegAlias)) { | 466 RegAlias = Aliases.find_next(RegAlias)) { |
466 --RegUses[RegAlias]; | 467 --RegUses[RegAlias]; |
467 assert(RegUses[RegAlias] >= 0); | 468 assert(RegUses[RegAlias] >= 0); |
468 } | 469 } |
469 } | 470 } |
470 } | 471 } |
471 } | 472 } |
472 | 473 |
473 void LinearScan::handleInactiveRangeExpiredOrReactivated(const Variable *Cur) { | 474 void LinearScan::handleInactiveRangeExpiredOrReactivated(const Variable *Cur) { |
474 for (SizeT I = Inactive.size(); I > 0; --I) { | 475 for (SizeT I = Inactive.size(); I > 0; --I) { |
475 const SizeT Index = I - 1; | 476 const SizeT Index = I - 1; |
476 Variable *Item = Inactive[Index]; | 477 Variable *Item = Inactive[Index]; |
477 Item->trimLiveRange(Cur->getLiveRange().getStart()); | 478 Item->trimLiveRange(Cur->getLiveRange().getStart()); |
478 if (Item->rangeEndsBefore(Cur)) { | 479 if (Item->rangeEndsBefore(Cur)) { |
479 // Move Item from Inactive to Handled list. | 480 // Move Item from Inactive to Handled list. |
480 dumpLiveRangeTrace("Expiring ", Item); | 481 dumpLiveRangeTrace("Expiring ", Item); |
481 moveItem(Inactive, Index, Handled); | 482 moveItem(Inactive, Index, Handled); |
482 } else if (Item->rangeOverlapsStart(Cur)) { | 483 } else if (Item->rangeOverlapsStart(Cur)) { |
483 // Move Item from Inactive to Active list. | 484 // Move Item from Inactive to Active list. |
484 dumpLiveRangeTrace("Reactivating ", Item); | 485 dumpLiveRangeTrace("Reactivating ", Item); |
485 moveItem(Inactive, Index, Active); | 486 moveItem(Inactive, Index, Active); |
486 // Increment Item in RegUses[]. | 487 // Increment Item in RegUses[]. |
487 assert(Item->hasRegTmp()); | 488 assert(Item->hasRegTmp()); |
488 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; | 489 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; |
489 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 490 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
490 RegAlias = Aliases.find_next(RegAlias)) { | 491 RegAlias = Aliases.find_next(RegAlias)) { |
491 assert(RegUses[RegAlias] >= 0); | 492 assert(RegUses[RegAlias] >= 0); |
492 ++RegUses[RegAlias]; | 493 ++RegUses[RegAlias]; |
493 } | 494 } |
494 } | 495 } |
495 } | 496 } |
496 } | 497 } |
497 | 498 |
498 // Infer register preference and allowable overlap. Only form a preference when | 499 // Infer register preference and allowable overlap. Only form a preference when |
499 // the current Variable has an unambiguous "first" definition. The preference is | 500 // the current Variable has an unambiguous "first" definition. The preference is |
500 // some source Variable of the defining instruction that either is assigned a | 501 // some source Variable of the defining instruction that either is assigned a |
501 // register that is currently free, or that is assigned a register that is not | 502 // register that is currently free, or that is assigned a register that is not |
502 // free but overlap is allowed. Overlap is allowed when the Variable under | 503 // free but overlap is allowed. Overlap is allowed when the Variable under |
503 // consideration is single-definition, and its definition is a simple assignment | 504 // consideration is single-definition, and its definition is a simple assignment |
504 // - i.e., the register gets copied/aliased but is never modified. Furthermore, | 505 // - i.e., the register gets copied/aliased but is never modified. Furthermore, |
505 // overlap is only allowed when preferred Variable definition instructions do | 506 // overlap is only allowed when preferred Variable definition instructions do |
506 // not appear within the current Variable's live range. | 507 // not appear within the current Variable's live range. |
507 void LinearScan::findRegisterPreference(IterationState &Iter) { | 508 void LinearScan::findRegisterPreference(IterationState &Iter) { |
508 Iter.Prefer = nullptr; | 509 Iter.Prefer = nullptr; |
509 Iter.PreferReg = Variable::NoRegister; | 510 Iter.PreferReg = RegNumT::NoRegister; |
510 Iter.AllowOverlap = false; | 511 Iter.AllowOverlap = false; |
511 | 512 |
512 if (!FindPreference) | 513 if (!FindPreference) |
513 return; | 514 return; |
514 | 515 |
515 VariablesMetadata *VMetadata = Func->getVMetadata(); | 516 VariablesMetadata *VMetadata = Func->getVMetadata(); |
516 const Inst *DefInst = VMetadata->getFirstDefinitionSingleBlock(Iter.Cur); | 517 const Inst *DefInst = VMetadata->getFirstDefinitionSingleBlock(Iter.Cur); |
517 if (DefInst == nullptr) | 518 if (DefInst == nullptr) |
518 return; | 519 return; |
519 | 520 |
520 assert(DefInst->getDest() == Iter.Cur); | 521 assert(DefInst->getDest() == Iter.Cur); |
521 const bool IsSingleDefAssign = | 522 const bool IsSingleDefAssign = |
522 DefInst->isVarAssign() && !VMetadata->isMultiDef(Iter.Cur); | 523 DefInst->isVarAssign() && !VMetadata->isMultiDef(Iter.Cur); |
523 FOREACH_VAR_IN_INST(SrcVar, *DefInst) { | 524 FOREACH_VAR_IN_INST(SrcVar, *DefInst) { |
524 // Only consider source variables that have (so far) been assigned a | 525 // Only consider source variables that have (so far) been assigned a |
525 // register. | 526 // register. |
526 if (!SrcVar->hasRegTmp()) | 527 if (!SrcVar->hasRegTmp()) |
527 continue; | 528 continue; |
528 | 529 |
529 // That register must be one in the RegMask set, e.g. don't try to prefer | 530 // That register must be one in the RegMask set, e.g. don't try to prefer |
530 // the stack pointer as a result of the stacksave intrinsic. | 531 // the stack pointer as a result of the stacksave intrinsic. |
531 const llvm::SmallBitVector &Aliases = *RegAliases[SrcVar->getRegNumTmp()]; | 532 const llvm::SmallBitVector &Aliases = *RegAliases[SrcVar->getRegNumTmp()]; |
532 const int32_t SrcReg = (Iter.RegMask & Aliases).find_first(); | 533 const int SrcReg = (Iter.RegMask & Aliases).find_first(); |
533 if (SrcReg == -1) | 534 if (SrcReg == -1) |
534 continue; | 535 continue; |
535 | 536 |
536 if (FindOverlap && IsSingleDefAssign && !Iter.Free[SrcReg]) { | 537 if (FindOverlap && IsSingleDefAssign && !Iter.Free[SrcReg]) { |
537 // Don't bother trying to enable AllowOverlap if the register is already | 538 // Don't bother trying to enable AllowOverlap if the register is already |
538 // free (hence the test on Iter.Free[SrcReg]). | 539 // free (hence the test on Iter.Free[SrcReg]). |
539 Iter.AllowOverlap = !overlapsDefs(Func, Iter.Cur, SrcVar); | 540 Iter.AllowOverlap = !overlapsDefs(Func, Iter.Cur, SrcVar); |
540 } | 541 } |
541 if (Iter.AllowOverlap || Iter.Free[SrcReg]) { | 542 if (Iter.AllowOverlap || Iter.Free[SrcReg]) { |
542 Iter.Prefer = SrcVar; | 543 Iter.Prefer = SrcVar; |
(...skipping 17 matching lines...) Expand all Loading... | |
560 } | 561 } |
561 | 562 |
562 // Remove registers from the Iter.Free[] list where an Inactive range overlaps | 563 // Remove registers from the Iter.Free[] list where an Inactive range overlaps |
563 // with the current range. | 564 // with the current range. |
564 void LinearScan::filterFreeWithInactiveRanges(IterationState &Iter) { | 565 void LinearScan::filterFreeWithInactiveRanges(IterationState &Iter) { |
565 for (const Variable *Item : Inactive) { | 566 for (const Variable *Item : Inactive) { |
566 if (!Item->rangeOverlaps(Iter.Cur)) | 567 if (!Item->rangeOverlaps(Iter.Cur)) |
567 continue; | 568 continue; |
568 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; | 569 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; |
569 // TODO(stichnot): Do this with bitvector ops, not a loop, for efficiency. | 570 // TODO(stichnot): Do this with bitvector ops, not a loop, for efficiency. |
570 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 571 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
571 RegAlias = Aliases.find_next(RegAlias)) { | 572 RegAlias = Aliases.find_next(RegAlias)) { |
572 // Don't assert(Iter.Free[RegNum]) because in theory (though probably | 573 // Don't assert(Iter.Free[RegNum]) because in theory (though probably |
573 // never in practice) there could be two inactive variables that were | 574 // never in practice) there could be two inactive variables that were |
574 // marked with AllowOverlap. | 575 // marked with AllowOverlap. |
575 Iter.Free[RegAlias] = false; | 576 Iter.Free[RegAlias] = false; |
576 Iter.FreeUnfiltered[RegAlias] = false; | 577 Iter.FreeUnfiltered[RegAlias] = false; |
577 // Disable AllowOverlap if an Inactive variable, which is not Prefer, | 578 // Disable AllowOverlap if an Inactive variable, which is not Prefer, |
578 // shares Prefer's register, and has a definition within Cur's live range. | 579 // shares Prefer's register, and has a definition within Cur's live range. |
579 if (Iter.AllowOverlap && Item != Iter.Prefer && | 580 if (Iter.AllowOverlap && Item != Iter.Prefer && |
580 RegAlias == Iter.PreferReg && overlapsDefs(Func, Iter.Cur, Item)) { | 581 RegNumT(RegAlias) == Iter.PreferReg && |
582 overlapsDefs(Func, Iter.Cur, Item)) { | |
581 Iter.AllowOverlap = false; | 583 Iter.AllowOverlap = false; |
582 dumpDisableOverlap(Func, Item, "Inactive"); | 584 dumpDisableOverlap(Func, Item, "Inactive"); |
583 } | 585 } |
584 } | 586 } |
585 } | 587 } |
586 } | 588 } |
587 | 589 |
588 // Remove registers from the Iter.Free[] list where an Unhandled pre-colored | 590 // Remove registers from the Iter.Free[] list where an Unhandled pre-colored |
589 // range overlaps with the current range, and set those registers to infinite | 591 // range overlaps with the current range, and set those registers to infinite |
590 // weight so that they aren't candidates for eviction. | 592 // weight so that they aren't candidates for eviction. |
591 // Cur->rangeEndsBefore(Item) is an early exit check that turns a guaranteed | 593 // Cur->rangeEndsBefore(Item) is an early exit check that turns a guaranteed |
592 // O(N^2) algorithm into expected linear complexity. | 594 // O(N^2) algorithm into expected linear complexity. |
593 void LinearScan::filterFreeWithPrecoloredRanges(IterationState &Iter) { | 595 void LinearScan::filterFreeWithPrecoloredRanges(IterationState &Iter) { |
594 // TODO(stichnot): Partition UnhandledPrecolored according to register class, | 596 // TODO(stichnot): Partition UnhandledPrecolored according to register class, |
595 // to restrict the number of overlap comparisons needed. | 597 // to restrict the number of overlap comparisons needed. |
596 for (Variable *Item : reverse_range(UnhandledPrecolored)) { | 598 for (Variable *Item : reverse_range(UnhandledPrecolored)) { |
597 assert(Item->hasReg()); | 599 assert(Item->hasReg()); |
598 if (Iter.Cur->rangeEndsBefore(Item)) | 600 if (Iter.Cur->rangeEndsBefore(Item)) |
599 break; | 601 break; |
600 if (!Item->rangeOverlaps(Iter.Cur)) | 602 if (!Item->rangeOverlaps(Iter.Cur)) |
601 continue; | 603 continue; |
602 const llvm::SmallBitVector &Aliases = | 604 const llvm::SmallBitVector &Aliases = |
603 *RegAliases[Item->getRegNum()]; // Note: not getRegNumTmp() | 605 *RegAliases[Item->getRegNum()]; // Note: not getRegNumTmp() |
604 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 606 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
605 RegAlias = Aliases.find_next(RegAlias)) { | 607 RegAlias = Aliases.find_next(RegAlias)) { |
606 Iter.Weights[RegAlias].setWeight(RegWeight::Inf); | 608 Iter.Weights[RegAlias].setWeight(RegWeight::Inf); |
607 Iter.Free[RegAlias] = false; | 609 Iter.Free[RegAlias] = false; |
608 Iter.FreeUnfiltered[RegAlias] = false; | 610 Iter.FreeUnfiltered[RegAlias] = false; |
609 Iter.PrecoloredUnhandledMask[RegAlias] = true; | 611 Iter.PrecoloredUnhandledMask[RegAlias] = true; |
610 // Disable Iter.AllowOverlap if the preferred register is one of these | 612 // Disable Iter.AllowOverlap if the preferred register is one of these |
611 // pre-colored unhandled overlapping ranges. | 613 // pre-colored unhandled overlapping ranges. |
612 if (Iter.AllowOverlap && RegAlias == Iter.PreferReg) { | 614 if (Iter.AllowOverlap && RegNumT(RegAlias) == Iter.PreferReg) { |
613 Iter.AllowOverlap = false; | 615 Iter.AllowOverlap = false; |
614 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); | 616 dumpDisableOverlap(Func, Item, "PrecoloredUnhandled"); |
615 } | 617 } |
616 } | 618 } |
617 } | 619 } |
618 } | 620 } |
619 | 621 |
620 void LinearScan::allocatePrecoloredRegister(Variable *Cur) { | 622 void LinearScan::allocatePrecoloredRegister(Variable *Cur) { |
621 int32_t RegNum = Cur->getRegNum(); | 623 RegNumT RegNum = Cur->getRegNum(); |
622 // RegNumTmp should have already been set above. | 624 // RegNumTmp should have already been set above. |
623 assert(Cur->getRegNumTmp() == RegNum); | 625 assert(Cur->getRegNumTmp() == RegNum); |
624 dumpLiveRangeTrace("Precoloring ", Cur); | 626 dumpLiveRangeTrace("Precoloring ", Cur); |
625 Active.push_back(Cur); | 627 Active.push_back(Cur); |
626 const llvm::SmallBitVector &Aliases = *RegAliases[RegNum]; | 628 const llvm::SmallBitVector &Aliases = *RegAliases[RegNum]; |
627 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 629 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
628 RegAlias = Aliases.find_next(RegAlias)) { | 630 RegAlias = Aliases.find_next(RegAlias)) { |
629 assert(RegUses[RegAlias] >= 0); | 631 assert(RegUses[RegAlias] >= 0); |
630 ++RegUses[RegAlias]; | 632 ++RegUses[RegAlias]; |
631 } | 633 } |
632 assert(!UnhandledPrecolored.empty()); | 634 assert(!UnhandledPrecolored.empty()); |
633 assert(UnhandledPrecolored.back() == Cur); | 635 assert(UnhandledPrecolored.back() == Cur); |
634 UnhandledPrecolored.pop_back(); | 636 UnhandledPrecolored.pop_back(); |
635 } | 637 } |
636 | 638 |
637 void LinearScan::allocatePreferredRegister(IterationState &Iter) { | 639 void LinearScan::allocatePreferredRegister(IterationState &Iter) { |
638 Iter.Cur->setRegNumTmp(Iter.PreferReg); | 640 Iter.Cur->setRegNumTmp(Iter.PreferReg); |
639 dumpLiveRangeTrace("Preferring ", Iter.Cur); | 641 dumpLiveRangeTrace("Preferring ", Iter.Cur); |
640 const llvm::SmallBitVector &Aliases = *RegAliases[Iter.PreferReg]; | 642 const llvm::SmallBitVector &Aliases = *RegAliases[Iter.PreferReg]; |
641 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 643 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
642 RegAlias = Aliases.find_next(RegAlias)) { | 644 RegAlias = Aliases.find_next(RegAlias)) { |
643 assert(RegUses[RegAlias] >= 0); | 645 assert(RegUses[RegAlias] >= 0); |
644 ++RegUses[RegAlias]; | 646 ++RegUses[RegAlias]; |
645 } | 647 } |
646 Active.push_back(Iter.Cur); | 648 Active.push_back(Iter.Cur); |
647 } | 649 } |
648 | 650 |
649 void LinearScan::allocateFreeRegister(IterationState &Iter, bool Filtered) { | 651 void LinearScan::allocateFreeRegister(IterationState &Iter, bool Filtered) { |
650 const int32_t RegNum = | 652 const RegNumT RegNum = |
651 Filtered ? Iter.Free.find_first() : Iter.FreeUnfiltered.find_first(); | 653 Filtered ? Iter.Free.find_first() : Iter.FreeUnfiltered.find_first(); |
652 Iter.Cur->setRegNumTmp(RegNum); | 654 Iter.Cur->setRegNumTmp(RegNum); |
653 if (Filtered) | 655 if (Filtered) |
654 dumpLiveRangeTrace("Allocating ", Iter.Cur); | 656 dumpLiveRangeTrace("Allocating ", Iter.Cur); |
655 else | 657 else |
656 dumpLiveRangeTrace("Allocating X ", Iter.Cur); | 658 dumpLiveRangeTrace("Allocating X ", Iter.Cur); |
657 const llvm::SmallBitVector &Aliases = *RegAliases[RegNum]; | 659 const llvm::SmallBitVector &Aliases = *RegAliases[RegNum]; |
658 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 660 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
659 RegAlias = Aliases.find_next(RegAlias)) { | 661 RegAlias = Aliases.find_next(RegAlias)) { |
660 assert(RegUses[RegAlias] >= 0); | 662 assert(RegUses[RegAlias] >= 0); |
661 ++RegUses[RegAlias]; | 663 ++RegUses[RegAlias]; |
662 } | 664 } |
663 Active.push_back(Iter.Cur); | 665 Active.push_back(Iter.Cur); |
664 } | 666 } |
665 | 667 |
666 void LinearScan::handleNoFreeRegisters(IterationState &Iter) { | 668 void LinearScan::handleNoFreeRegisters(IterationState &Iter) { |
667 // Check Active ranges. | 669 // Check Active ranges. |
668 for (const Variable *Item : Active) { | 670 for (const Variable *Item : Active) { |
669 assert(Item->rangeOverlaps(Iter.Cur)); | 671 assert(Item->rangeOverlaps(Iter.Cur)); |
670 assert(Item->hasRegTmp()); | 672 assert(Item->hasRegTmp()); |
671 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; | 673 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; |
672 // We add the Item's weight to each alias/subregister to represent that, | 674 // We add the Item's weight to each alias/subregister to represent that, |
673 // should we decide to pick any of them, then we would incur that many | 675 // should we decide to pick any of them, then we would incur that many |
674 // memory accesses. | 676 // memory accesses. |
675 RegWeight W = Item->getWeight(Func); | 677 RegWeight W = Item->getWeight(Func); |
676 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 678 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
677 RegAlias = Aliases.find_next(RegAlias)) { | 679 RegAlias = Aliases.find_next(RegAlias)) { |
678 Iter.Weights[RegAlias].addWeight(W); | 680 Iter.Weights[RegAlias].addWeight(W); |
679 } | 681 } |
680 } | 682 } |
681 // Same as above, but check Inactive ranges instead of Active. | 683 // Same as above, but check Inactive ranges instead of Active. |
682 for (const Variable *Item : Inactive) { | 684 for (const Variable *Item : Inactive) { |
683 if (!Item->rangeOverlaps(Iter.Cur)) | 685 if (!Item->rangeOverlaps(Iter.Cur)) |
684 continue; | 686 continue; |
685 assert(Item->hasRegTmp()); | 687 assert(Item->hasRegTmp()); |
686 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; | 688 const llvm::SmallBitVector &Aliases = *RegAliases[Item->getRegNumTmp()]; |
687 RegWeight W = Item->getWeight(Func); | 689 RegWeight W = Item->getWeight(Func); |
688 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 690 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
689 RegAlias = Aliases.find_next(RegAlias)) { | 691 RegAlias = Aliases.find_next(RegAlias)) { |
690 Iter.Weights[RegAlias].addWeight(W); | 692 Iter.Weights[RegAlias].addWeight(W); |
691 } | 693 } |
692 } | 694 } |
693 | 695 |
694 // All the weights are now calculated. Find the register with smallest weight. | 696 // All the weights are now calculated. Find the register with smallest weight. |
695 int32_t MinWeightIndex = findMinWeightIndex(Iter.RegMask, Iter.Weights); | 697 int32_t MinWeightIndex = findMinWeightIndex(Iter.RegMask, Iter.Weights); |
696 | 698 |
697 if (Iter.Cur->getWeight(Func) <= Iter.Weights[MinWeightIndex]) { | 699 if (Iter.Cur->getWeight(Func) <= Iter.Weights[MinWeightIndex]) { |
698 if (!Iter.Cur->mustHaveReg()) { | 700 if (!Iter.Cur->mustHaveReg()) { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
735 // At this point, MinWeightIndex points to a valid reserve register to | 737 // At this point, MinWeightIndex points to a valid reserve register to |
736 // reallocate to Iter.Cur, so drop into the eviction code. | 738 // reallocate to Iter.Cur, so drop into the eviction code. |
737 } | 739 } |
738 | 740 |
739 // Evict all live ranges in Active that register number MinWeightIndex is | 741 // Evict all live ranges in Active that register number MinWeightIndex is |
740 // assigned to. | 742 // assigned to. |
741 const llvm::SmallBitVector &Aliases = *RegAliases[MinWeightIndex]; | 743 const llvm::SmallBitVector &Aliases = *RegAliases[MinWeightIndex]; |
742 for (SizeT I = Active.size(); I > 0; --I) { | 744 for (SizeT I = Active.size(); I > 0; --I) { |
743 const SizeT Index = I - 1; | 745 const SizeT Index = I - 1; |
744 Variable *Item = Active[Index]; | 746 Variable *Item = Active[Index]; |
745 int32_t RegNum = Item->getRegNumTmp(); | 747 RegNumT RegNum = Item->getRegNumTmp(); |
746 if (Aliases[RegNum]) { | 748 if (Aliases[RegNum]) { |
747 dumpLiveRangeTrace("Evicting A ", Item); | 749 dumpLiveRangeTrace("Evicting A ", Item); |
748 const llvm::SmallBitVector &Aliases = *RegAliases[RegNum]; | 750 const llvm::SmallBitVector &Aliases = *RegAliases[RegNum]; |
749 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 751 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
750 RegAlias = Aliases.find_next(RegAlias)) { | 752 RegAlias = Aliases.find_next(RegAlias)) { |
751 --RegUses[RegAlias]; | 753 --RegUses[RegAlias]; |
752 assert(RegUses[RegAlias] >= 0); | 754 assert(RegUses[RegAlias] >= 0); |
753 } | 755 } |
754 Item->setRegNumTmp(Variable::NoRegister); | 756 Item->setRegNumTmp(RegNumT::NoRegister); |
755 moveItem(Active, Index, Handled); | 757 moveItem(Active, Index, Handled); |
756 Evicted.push_back(Item); | 758 Evicted.push_back(Item); |
757 } | 759 } |
758 } | 760 } |
759 // Do the same for Inactive. | 761 // Do the same for Inactive. |
760 for (SizeT I = Inactive.size(); I > 0; --I) { | 762 for (SizeT I = Inactive.size(); I > 0; --I) { |
761 const SizeT Index = I - 1; | 763 const SizeT Index = I - 1; |
762 Variable *Item = Inactive[Index]; | 764 Variable *Item = Inactive[Index]; |
763 // Note: The Item->rangeOverlaps(Cur) clause is not part of the description | 765 // Note: The Item->rangeOverlaps(Cur) clause is not part of the description |
764 // of AssignMemLoc() in the original paper. But there doesn't seem to be any | 766 // of AssignMemLoc() in the original paper. But there doesn't seem to be any |
765 // need to evict an inactive live range that doesn't overlap with the live | 767 // need to evict an inactive live range that doesn't overlap with the live |
766 // range currently being considered. It's especially bad if we would end up | 768 // range currently being considered. It's especially bad if we would end up |
767 // evicting an infinite-weight but currently-inactive live range. The most | 769 // evicting an infinite-weight but currently-inactive live range. The most |
768 // common situation for this would be a scratch register kill set for call | 770 // common situation for this would be a scratch register kill set for call |
769 // instructions. | 771 // instructions. |
770 if (Aliases[Item->getRegNumTmp()] && Item->rangeOverlaps(Iter.Cur)) { | 772 if (Aliases[Item->getRegNumTmp()] && Item->rangeOverlaps(Iter.Cur)) { |
771 dumpLiveRangeTrace("Evicting I ", Item); | 773 dumpLiveRangeTrace("Evicting I ", Item); |
772 Item->setRegNumTmp(Variable::NoRegister); | 774 Item->setRegNumTmp(RegNumT::NoRegister); |
773 moveItem(Inactive, Index, Handled); | 775 moveItem(Inactive, Index, Handled); |
774 Evicted.push_back(Item); | 776 Evicted.push_back(Item); |
775 } | 777 } |
776 } | 778 } |
777 // Assign the register to Cur. | 779 // Assign the register to Cur. |
778 Iter.Cur->setRegNumTmp(MinWeightIndex); | 780 Iter.Cur->setRegNumTmp(MinWeightIndex); |
779 for (int32_t RegAlias = Aliases.find_first(); RegAlias >= 0; | 781 for (int RegAlias = Aliases.find_first(); RegAlias >= 0; |
780 RegAlias = Aliases.find_next(RegAlias)) { | 782 RegAlias = Aliases.find_next(RegAlias)) { |
781 assert(RegUses[RegAlias] >= 0); | 783 assert(RegUses[RegAlias] >= 0); |
782 ++RegUses[RegAlias]; | 784 ++RegUses[RegAlias]; |
783 } | 785 } |
784 Active.push_back(Iter.Cur); | 786 Active.push_back(Iter.Cur); |
785 dumpLiveRangeTrace("Allocating ", Iter.Cur); | 787 dumpLiveRangeTrace("Allocating ", Iter.Cur); |
786 } | 788 } |
787 | 789 |
788 void LinearScan::assignFinalRegisters( | 790 void LinearScan::assignFinalRegisters( |
789 const llvm::SmallBitVector &RegMaskFull, | 791 const llvm::SmallBitVector &RegMaskFull, |
790 const llvm::SmallBitVector &PreDefinedRegisters, bool Randomized) { | 792 const llvm::SmallBitVector &PreDefinedRegisters, bool Randomized) { |
791 const size_t NumRegisters = RegMaskFull.size(); | 793 const size_t NumRegisters = RegMaskFull.size(); |
792 llvm::SmallVector<int32_t, REGS_SIZE> Permutation(NumRegisters); | 794 llvm::SmallVector<RegNumT, REGS_SIZE> Permutation(NumRegisters); |
793 if (Randomized) { | 795 if (Randomized) { |
794 // Create a random number generator for regalloc randomization. Merge | 796 // Create a random number generator for regalloc randomization. Merge |
795 // function's sequence and Kind value as the Salt. Because regAlloc() is | 797 // function's sequence and Kind value as the Salt. Because regAlloc() is |
796 // called twice under O2, the second time with RAK_Phi, we check Kind == | 798 // called twice under O2, the second time with RAK_Phi, we check Kind == |
797 // RAK_Phi to determine the lowest-order bit to make sure the Salt is | 799 // RAK_Phi to determine the lowest-order bit to make sure the Salt is |
798 // different. | 800 // different. |
799 uint64_t Salt = | 801 uint64_t Salt = |
800 (Func->getSequenceNumber() << 1) ^ (Kind == RAK_Phi ? 0u : 1u); | 802 (Func->getSequenceNumber() << 1) ^ (Kind == RAK_Phi ? 0u : 1u); |
801 Target->makeRandomRegisterPermutation( | 803 Target->makeRandomRegisterPermutation( |
802 Permutation, PreDefinedRegisters | ~RegMaskFull, Salt); | 804 Permutation, PreDefinedRegisters | ~RegMaskFull, Salt); |
803 } | 805 } |
804 | 806 |
805 // Finish up by setting RegNum = RegNumTmp (or a random permutation thereof) | 807 // Finish up by setting RegNum = RegNumTmp (or a random permutation thereof) |
806 // for each Variable. | 808 // for each Variable. |
807 for (Variable *Item : Handled) { | 809 for (Variable *Item : Handled) { |
808 int32_t RegNum = Item->getRegNumTmp(); | 810 RegNumT RegNum = Item->getRegNumTmp(); |
809 int32_t AssignedRegNum = RegNum; | 811 RegNumT AssignedRegNum = RegNum; |
810 | 812 |
811 if (Randomized && Item->hasRegTmp() && !Item->hasReg()) { | 813 if (Randomized && Item->hasRegTmp() && !Item->hasReg()) { |
812 AssignedRegNum = Permutation[RegNum]; | 814 AssignedRegNum = Permutation[RegNum]; |
813 } | 815 } |
814 if (BuildDefs::dump() && Verbose) { | 816 if (BuildDefs::dump() && Verbose) { |
815 Ostream &Str = Ctx->getStrDump(); | 817 Ostream &Str = Ctx->getStrDump(); |
816 if (!Item->hasRegTmp()) { | 818 if (!Item->hasRegTmp()) { |
817 Str << "Not assigning "; | 819 Str << "Not assigning "; |
818 Item->dump(Func); | 820 Item->dump(Func); |
819 Str << "\n"; | 821 Str << "\n"; |
(...skipping 90 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
910 } | 912 } |
911 | 913 |
912 findRegisterPreference(Iter); | 914 findRegisterPreference(Iter); |
913 filterFreeWithInactiveRanges(Iter); | 915 filterFreeWithInactiveRanges(Iter); |
914 | 916 |
915 // Disable AllowOverlap if an Active variable, which is not Prefer, shares | 917 // Disable AllowOverlap if an Active variable, which is not Prefer, shares |
916 // Prefer's register, and has a definition within Cur's live range. | 918 // Prefer's register, and has a definition within Cur's live range. |
917 if (Iter.AllowOverlap) { | 919 if (Iter.AllowOverlap) { |
918 const llvm::SmallBitVector &Aliases = *RegAliases[Iter.PreferReg]; | 920 const llvm::SmallBitVector &Aliases = *RegAliases[Iter.PreferReg]; |
919 for (const Variable *Item : Active) { | 921 for (const Variable *Item : Active) { |
920 int32_t RegNum = Item->getRegNumTmp(); | 922 RegNumT RegNum = Item->getRegNumTmp(); |
921 if (Item != Iter.Prefer && Aliases[RegNum] && | 923 if (Item != Iter.Prefer && Aliases[RegNum] && |
922 overlapsDefs(Func, Iter.Cur, Item)) { | 924 overlapsDefs(Func, Iter.Cur, Item)) { |
923 Iter.AllowOverlap = false; | 925 Iter.AllowOverlap = false; |
924 dumpDisableOverlap(Func, Item, "Active"); | 926 dumpDisableOverlap(Func, Item, "Active"); |
925 } | 927 } |
926 } | 928 } |
927 } | 929 } |
928 | 930 |
929 Iter.Weights.resize(Iter.RegMask.size()); | 931 Iter.Weights.resize(Iter.RegMask.size()); |
930 std::fill(Iter.Weights.begin(), Iter.Weights.end(), RegWeight()); | 932 std::fill(Iter.Weights.begin(), Iter.Weights.end(), RegWeight()); |
931 | 933 |
932 Iter.PrecoloredUnhandledMask.resize(Iter.RegMask.size()); | 934 Iter.PrecoloredUnhandledMask.resize(Iter.RegMask.size()); |
933 Iter.PrecoloredUnhandledMask.reset(); | 935 Iter.PrecoloredUnhandledMask.reset(); |
934 | 936 |
935 filterFreeWithPrecoloredRanges(Iter); | 937 filterFreeWithPrecoloredRanges(Iter); |
936 | 938 |
937 // Remove scratch registers from the Iter.Free[] list, and mark their | 939 // Remove scratch registers from the Iter.Free[] list, and mark their |
938 // Iter.Weights[] as infinite, if KillsRange overlaps Cur's live range. | 940 // Iter.Weights[] as infinite, if KillsRange overlaps Cur's live range. |
939 constexpr bool UseTrimmed = true; | 941 constexpr bool UseTrimmed = true; |
940 if (Iter.Cur->getLiveRange().overlaps(KillsRange, UseTrimmed)) { | 942 if (Iter.Cur->getLiveRange().overlaps(KillsRange, UseTrimmed)) { |
941 Iter.Free.reset(KillsMask); | 943 Iter.Free.reset(KillsMask); |
942 Iter.FreeUnfiltered.reset(KillsMask); | 944 Iter.FreeUnfiltered.reset(KillsMask); |
943 for (int i = KillsMask.find_first(); i != -1; | 945 for (int i = KillsMask.find_first(); i != -1; |
944 i = KillsMask.find_next(i)) { | 946 i = KillsMask.find_next(i)) { |
945 Iter.Weights[i].setWeight(RegWeight::Inf); | 947 Iter.Weights[i].setWeight(RegWeight::Inf); |
946 if (Iter.PreferReg == i) | 948 if (Iter.PreferReg == RegNumT(i)) |
947 Iter.AllowOverlap = false; | 949 Iter.AllowOverlap = false; |
948 } | 950 } |
949 } | 951 } |
950 | 952 |
951 // Print info about physical register availability. | 953 // Print info about physical register availability. |
952 if (BuildDefs::dump() && Verbose) { | 954 if (BuildDefs::dump() && Verbose) { |
953 Ostream &Str = Ctx->getStrDump(); | 955 Ostream &Str = Ctx->getStrDump(); |
954 for (SizeT i = 0; i < Iter.RegMask.size(); ++i) { | 956 for (int i = Iter.RegMaskUnfiltered.find_first(); i != -1; |
955 if (Iter.RegMaskUnfiltered[i]) { | 957 i = Iter.RegMaskUnfiltered.find_next(i)) { |
Eric Holk
2016/02/08 19:37:09
A lot of the changes in this file seem kind of ind
Jim Stichnoth
2016/02/09 19:33:39
True - while tracking down all the changes, I also
Eric Holk
2016/02/10 01:11:30
Yeah, that's fine with me. It's a pain to split up
| |
956 Str << Target->getRegName(i, Iter.Cur->getType()) | 958 Str << Target->getRegName(i, Iter.Cur->getType()) << "(U=" << RegUses[i] |
957 << "(U=" << RegUses[i] << ",F=" << Iter.Free[i] | 959 << ",F=" << Iter.Free[i] << ",P=" << Iter.PrecoloredUnhandledMask[i] |
958 << ",P=" << Iter.PrecoloredUnhandledMask[i] << ") "; | 960 << ") "; |
959 } | |
960 } | 961 } |
961 Str << "\n"; | 962 Str << "\n"; |
962 } | 963 } |
963 | 964 |
964 if (Iter.Prefer && (Iter.AllowOverlap || Iter.Free[Iter.PreferReg])) { | 965 if (Iter.Prefer && (Iter.AllowOverlap || Iter.Free[Iter.PreferReg])) { |
965 // First choice: a preferred register that is either free or is allowed to | 966 // First choice: a preferred register that is either free or is allowed to |
966 // overlap with its linked variable. | 967 // overlap with its linked variable. |
967 allocatePreferredRegister(Iter); | 968 allocatePreferredRegister(Iter); |
968 } else if (Iter.Free.any()) { | 969 } else if (Iter.Free.any()) { |
969 // Second choice: any free register. | 970 // Second choice: any free register. |
(...skipping 58 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1028 Str << "\n"; | 1029 Str << "\n"; |
1029 } | 1030 } |
1030 Str << "++++++ Inactive:\n"; | 1031 Str << "++++++ Inactive:\n"; |
1031 for (const Variable *Item : Inactive) { | 1032 for (const Variable *Item : Inactive) { |
1032 dumpLiveRange(Item, Func); | 1033 dumpLiveRange(Item, Func); |
1033 Str << "\n"; | 1034 Str << "\n"; |
1034 } | 1035 } |
1035 } | 1036 } |
1036 | 1037 |
1037 } // end of namespace Ice | 1038 } // end of namespace Ice |
OLD | NEW |