Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 the V8 project authors. All rights reserved. | 1 // Copyright 2014 the V8 project authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "src/base/adapters.h" | 5 #include "src/base/adapters.h" |
| 6 #include "src/compiler/linkage.h" | 6 #include "src/compiler/linkage.h" |
| 7 #include "src/compiler/register-allocator.h" | 7 #include "src/compiler/register-allocator.h" |
| 8 #include "src/string-stream.h" | 8 #include "src/string-stream.h" |
| 9 | 9 |
| 10 namespace v8 { | 10 namespace v8 { |
| (...skipping 29 matching lines...) Expand all Loading... | |
| 40 | 40 |
| 41 | 41 |
| 42 const InstructionBlock* GetContainingLoop(const InstructionSequence* sequence, | 42 const InstructionBlock* GetContainingLoop(const InstructionSequence* sequence, |
| 43 const InstructionBlock* block) { | 43 const InstructionBlock* block) { |
| 44 auto index = block->loop_header(); | 44 auto index = block->loop_header(); |
| 45 if (!index.IsValid()) return nullptr; | 45 if (!index.IsValid()) return nullptr; |
| 46 return sequence->InstructionBlockAt(index); | 46 return sequence->InstructionBlockAt(index); |
| 47 } | 47 } |
| 48 | 48 |
| 49 | 49 |
| 50 unsigned GetContainingLoopCount(const InstructionSequence* sequence, | |
| 51 const InstructionBlock* block) { | |
| 52 unsigned ret = 0; | |
| 53 for (auto cursor = GetContainingLoop(sequence, block); cursor != nullptr; | |
| 54 cursor = GetContainingLoop(sequence, cursor)) { | |
| 55 ++ret; | |
| 56 } | |
| 57 return ret; | |
| 58 } | |
| 59 | |
| 60 | |
| 50 const InstructionBlock* GetInstructionBlock(const InstructionSequence* code, | 61 const InstructionBlock* GetInstructionBlock(const InstructionSequence* code, |
| 51 LifetimePosition pos) { | 62 LifetimePosition pos) { |
| 52 return code->GetInstructionBlock(pos.ToInstructionIndex()); | 63 return code->GetInstructionBlock(pos.ToInstructionIndex()); |
| 53 } | 64 } |
| 54 | 65 |
| 55 | 66 |
| 56 bool IsBlockBoundary(const InstructionSequence* code, LifetimePosition pos) { | 67 bool IsBlockBoundary(const InstructionSequence* code, LifetimePosition pos) { |
| 57 return pos.IsFullStart() && | 68 return pos.IsFullStart() && |
| 58 code->GetInstructionBlock(pos.ToInstructionIndex())->code_start() == | 69 code->GetInstructionBlock(pos.ToInstructionIndex())->code_start() == |
| 59 pos.ToInstructionIndex(); | 70 pos.ToInstructionIndex(); |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 103 case kRepFloat32: | 114 case kRepFloat32: |
| 104 case kRepWord64: | 115 case kRepWord64: |
| 105 case kRepFloat64: | 116 case kRepFloat64: |
| 106 return 8; | 117 return 8; |
| 107 default: | 118 default: |
| 108 UNREACHABLE(); | 119 UNREACHABLE(); |
| 109 return 0; | 120 return 0; |
| 110 } | 121 } |
| 111 } | 122 } |
| 112 | 123 |
| 124 | |
| 125 int GetHintedRegister(LiveRange* range) { | |
| 126 int reg; | |
| 127 auto hint = range->FirstHintPosition(®); | |
| 128 if (hint != nullptr) { | |
| 129 if (hint->HasOperand()) { | |
| 130 if (hint->operand()->IsDoubleStackSlot() || | |
| 131 hint->operand()->IsStackSlot()) { | |
| 132 return -1; | |
| 133 } | |
| 134 } | |
| 135 return reg; | |
| 136 } | |
| 137 return -1; | |
| 138 } | |
| 139 | |
| 113 } // namespace | 140 } // namespace |
| 114 | 141 |
| 115 | 142 |
| 116 UsePosition::UsePosition(LifetimePosition pos, InstructionOperand* operand, | 143 UsePosition::UsePosition(LifetimePosition pos, InstructionOperand* operand, |
| 117 void* hint, UsePositionHintType hint_type) | 144 void* hint, UsePositionHintType hint_type) |
| 118 : operand_(operand), hint_(hint), next_(nullptr), pos_(pos), flags_(0) { | 145 : operand_(operand), hint_(hint), next_(nullptr), pos_(pos), flags_(0) { |
| 119 DCHECK_IMPLIES(hint == nullptr, hint_type == UsePositionHintType::kNone); | 146 DCHECK_IMPLIES(hint == nullptr, hint_type == UsePositionHintType::kNone); |
| 120 bool register_beneficial = true; | 147 bool register_beneficial = true; |
| 121 UsePositionType type = UsePositionType::kAny; | 148 UsePositionType type = UsePositionType::kAny; |
| 122 if (operand_ != nullptr && operand_->IsUnallocated()) { | 149 if (operand_ != nullptr && operand_->IsUnallocated()) { |
| (...skipping 13 matching lines...) Expand all Loading... | |
| 136 DCHECK(pos_.IsValid()); | 163 DCHECK(pos_.IsValid()); |
| 137 } | 164 } |
| 138 | 165 |
| 139 | 166 |
| 140 bool UsePosition::HasHint() const { | 167 bool UsePosition::HasHint() const { |
| 141 int hint_register; | 168 int hint_register; |
| 142 return HintRegister(&hint_register); | 169 return HintRegister(&hint_register); |
| 143 } | 170 } |
| 144 | 171 |
| 145 | 172 |
| 173 void UsePosition::dump_hint(std::ostream& os, | |
| 174 const RegisterConfiguration* config) { | |
| 175 if (hint_ == nullptr) { | |
| 176 os << "H:nil"; | |
| 177 return; | |
| 178 } | |
| 179 switch (HintTypeField::decode(flags_)) { | |
| 180 case UsePositionHintType::kNone: | |
| 181 os << "H:N"; | |
| 182 return; | |
| 183 case UsePositionHintType::kUnresolved: | |
| 184 os << "H:U"; | |
| 185 return; | |
| 186 case UsePositionHintType::kUsePos: { | |
| 187 auto use_pos = reinterpret_cast<UsePosition*>(hint_); | |
| 188 int assigned_register = AssignedRegisterField::decode(use_pos->flags_); | |
| 189 if (assigned_register == kUnassignedRegister) { | |
| 190 os << "H:Use(R ?"; | |
| 191 } else { | |
| 192 os << "H:Use(R " << assigned_register; | |
| 193 } | |
| 194 if (use_pos->HasOperand()) { | |
| 195 PrintableInstructionOperand pio{config, *use_pos->operand()}; | |
| 196 os << " | " << pio; | |
| 197 } | |
| 198 os << ")"; | |
| 199 return; | |
| 200 } | |
| 201 case UsePositionHintType::kOperand: { | |
| 202 auto operand = reinterpret_cast<InstructionOperand*>(hint_); | |
| 203 int assigned_register = AllocatedOperand::cast(operand)->index(); | |
| 204 PrintableInstructionOperand pio{config, *operand}; | |
| 205 os << "H:Op(R" << assigned_register << " | " << pio << ")"; | |
| 206 return; | |
| 207 } | |
| 208 case UsePositionHintType::kPhi: { | |
| 209 auto phi = reinterpret_cast<RegisterAllocationData::PhiMapValue*>(hint_); | |
| 210 int assigned_register = phi->assigned_register(); | |
| 211 PrintableInstructionOperand pio{config, phi->phi()->output()}; | |
| 212 if (assigned_register == kUnassignedRegister) { | |
| 213 os << "H:Phi(R x"; | |
| 214 | |
| 215 } else { | |
| 216 os << "H:Phi(R" << assigned_register; | |
| 217 } | |
| 218 os << " | " << pio; | |
| 219 os << ")"; | |
| 220 return; | |
| 221 } | |
| 222 } | |
| 223 UNREACHABLE(); | |
| 224 } | |
| 225 | |
| 226 | |
| 146 bool UsePosition::HintRegister(int* register_index) const { | 227 bool UsePosition::HintRegister(int* register_index) const { |
| 147 if (hint_ == nullptr) return false; | 228 if (hint_ == nullptr) return false; |
| 148 switch (HintTypeField::decode(flags_)) { | 229 switch (HintTypeField::decode(flags_)) { |
| 149 case UsePositionHintType::kNone: | 230 case UsePositionHintType::kNone: |
| 150 case UsePositionHintType::kUnresolved: | 231 case UsePositionHintType::kUnresolved: |
| 151 return false; | 232 return false; |
| 152 case UsePositionHintType::kUsePos: { | 233 case UsePositionHintType::kUsePos: { |
| 153 auto use_pos = reinterpret_cast<UsePosition*>(hint_); | 234 auto use_pos = reinterpret_cast<UsePosition*>(hint_); |
| 154 int assigned_register = AssignedRegisterField::decode(use_pos->flags_); | 235 int assigned_register = AssignedRegisterField::decode(use_pos->flags_); |
| 155 if (assigned_register == kUnassignedRegister) return false; | 236 if (assigned_register == kUnassignedRegister) return false; |
| (...skipping 101 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 257 bits_(0), | 338 bits_(0), |
| 258 last_interval_(nullptr), | 339 last_interval_(nullptr), |
| 259 first_interval_(nullptr), | 340 first_interval_(nullptr), |
| 260 first_pos_(nullptr), | 341 first_pos_(nullptr), |
| 261 parent_(nullptr), | 342 parent_(nullptr), |
| 262 next_(nullptr), | 343 next_(nullptr), |
| 263 spill_operand_(nullptr), | 344 spill_operand_(nullptr), |
| 264 spills_at_definition_(nullptr), | 345 spills_at_definition_(nullptr), |
| 265 current_interval_(nullptr), | 346 current_interval_(nullptr), |
| 266 last_processed_use_(nullptr), | 347 last_processed_use_(nullptr), |
| 267 current_hint_position_(nullptr) { | 348 current_hint_position_(nullptr), |
| 349 group_(nullptr) { | |
| 268 DCHECK(AllocatedOperand::IsSupportedMachineType(machine_type)); | 350 DCHECK(AllocatedOperand::IsSupportedMachineType(machine_type)); |
| 269 bits_ = SpillTypeField::encode(SpillType::kNoSpillType) | | 351 bits_ = SpillTypeField::encode(SpillType::kNoSpillType) | |
| 270 AssignedRegisterField::encode(kUnassignedRegister) | | 352 AssignedRegisterField::encode(kUnassignedRegister) | |
| 271 MachineTypeField::encode(machine_type); | 353 MachineTypeField::encode(machine_type); |
| 354 InvalidateWeightAndSize(); | |
| 272 } | 355 } |
| 273 | 356 |
| 274 | 357 |
| 275 void LiveRange::Verify() const { | 358 void LiveRange::Verify() const { |
| 276 // Walk the positions, verifying that each is in an interval. | 359 // Walk the positions, verifying that each is in an interval. |
| 277 auto interval = first_interval_; | 360 auto interval = first_interval_; |
| 278 for (auto pos = first_pos_; pos != nullptr; pos = pos->next()) { | 361 for (auto pos = first_pos_; pos != nullptr; pos = pos->next()) { |
| 279 CHECK(Start() <= pos->pos()); | 362 CHECK(Start() <= pos->pos()); |
| 280 CHECK(pos->pos() <= End()); | 363 CHECK(pos->pos() <= End()); |
| 281 CHECK(interval != nullptr); | 364 CHECK(interval != nullptr); |
| (...skipping 135 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 417 | 500 |
| 418 UsePosition* LiveRange::NextRegisterPosition(LifetimePosition start) const { | 501 UsePosition* LiveRange::NextRegisterPosition(LifetimePosition start) const { |
| 419 UsePosition* pos = NextUsePosition(start); | 502 UsePosition* pos = NextUsePosition(start); |
| 420 while (pos != nullptr && pos->type() != UsePositionType::kRequiresRegister) { | 503 while (pos != nullptr && pos->type() != UsePositionType::kRequiresRegister) { |
| 421 pos = pos->next(); | 504 pos = pos->next(); |
| 422 } | 505 } |
| 423 return pos; | 506 return pos; |
| 424 } | 507 } |
| 425 | 508 |
| 426 | 509 |
| 427 bool LiveRange::CanBeSpilled(LifetimePosition pos) const { | 510 bool LiveRange::CanBeSplit(LifetimePosition pos) const { |
| 428 // We cannot spill a live range that has a use requiring a register | 511 // We cannot split a live range that has a use requiring a register |
| 429 // at the current or the immediate next position. | 512 // at the current or the immediate next position, because there would be |
| 513 // no gap where to insert a parallel move. | |
| 430 auto use_pos = NextRegisterPosition(pos); | 514 auto use_pos = NextRegisterPosition(pos); |
| 431 if (use_pos == nullptr) return true; | 515 if (use_pos == nullptr) return true; |
| 432 return use_pos->pos() > pos.NextStart().End(); | 516 return use_pos->pos() > pos.NextStart().End(); |
| 433 } | 517 } |
| 434 | 518 |
| 435 | 519 |
| 436 InstructionOperand LiveRange::GetAssignedOperand() const { | 520 InstructionOperand LiveRange::GetAssignedOperand() const { |
| 437 if (HasRegisterAssigned()) { | 521 if (HasRegisterAssigned()) { |
| 438 DCHECK(!spilled()); | 522 DCHECK(!spilled()); |
| 439 switch (kind()) { | 523 switch (kind()) { |
| (...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 565 // Discard cached iteration state. It might be pointing | 649 // Discard cached iteration state. It might be pointing |
| 566 // to the use that no longer belongs to this live range. | 650 // to the use that no longer belongs to this live range. |
| 567 last_processed_use_ = nullptr; | 651 last_processed_use_ = nullptr; |
| 568 current_interval_ = nullptr; | 652 current_interval_ = nullptr; |
| 569 | 653 |
| 570 // Link the new live range in the chain before any of the other | 654 // Link the new live range in the chain before any of the other |
| 571 // ranges linked from the range before the split. | 655 // ranges linked from the range before the split. |
| 572 result->parent_ = (parent_ == nullptr) ? this : parent_; | 656 result->parent_ = (parent_ == nullptr) ? this : parent_; |
| 573 result->next_ = next_; | 657 result->next_ = next_; |
| 574 next_ = result; | 658 next_ = result; |
| 659 InvalidateWeightAndSize(); | |
| 575 | 660 |
| 576 #ifdef DEBUG | 661 #ifdef DEBUG |
| 577 Verify(); | 662 Verify(); |
| 578 result->Verify(); | 663 result->Verify(); |
| 579 #endif | 664 #endif |
| 580 } | 665 } |
| 581 | 666 |
| 582 | 667 |
| 583 // This implements an ordering on live ranges so that they are ordered by their | 668 // This implements an ordering on live ranges so that they are ordered by their |
| 584 // start positions. This is needed for the correctness of the register | 669 // start positions. This is needed for the correctness of the register |
| (...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 756 if (a == nullptr || a->start() > other->End()) break; | 841 if (a == nullptr || a->start() > other->End()) break; |
| 757 AdvanceLastProcessedMarker(a, advance_last_processed_up_to); | 842 AdvanceLastProcessedMarker(a, advance_last_processed_up_to); |
| 758 } else { | 843 } else { |
| 759 b = b->next(); | 844 b = b->next(); |
| 760 } | 845 } |
| 761 } | 846 } |
| 762 return LifetimePosition::Invalid(); | 847 return LifetimePosition::Invalid(); |
| 763 } | 848 } |
| 764 | 849 |
| 765 | 850 |
| 851 LifetimePosition LiveRange::GetFirstSplittablePosition() { | |
| 852 for (auto c = first_pos(); c != nullptr; c = c->next()) { | |
| 853 LifetimePosition pos; | |
| 854 if (CanBeSpilled(c->pos())) { | |
| 855 pos = c->pos(); | |
| 856 } else { | |
| 857 auto after = c->pos().NextStart(); | |
| 858 if (Covers(after) && CanBeSpilled(after)) { | |
| 859 pos = after; | |
| 860 } | |
| 861 } | |
| 862 if (pos.IsValid() && Start() < pos && pos < End()) { | |
| 863 return pos; | |
| 864 } | |
| 865 } | |
| 866 return LifetimePosition::Invalid(); | |
| 867 } | |
| 868 | |
| 869 | |
| 870 void LiveRange::RecalculateSize() { | |
| 871 size_ = 0; | |
| 872 for (auto cursor = first_interval(); cursor != nullptr; | |
| 873 cursor = cursor->next()) { | |
| 874 size_ += cursor->end().value() - cursor->start().value(); | |
| 875 } | |
| 876 | |
| 877 DCHECK_NE(0U, static_cast<unsigned>(size_)); | |
| 878 } | |
| 879 | |
| 880 | |
| 881 const float LiveRange::kMaxWeight = FLT_MAX; | |
| 882 const float LiveRange::kUseInLoopWeightMultiplier = 10.0f; | |
| 883 const float LiveRange::kPreferredRegisterWeightMultiplier = 10.0f; | |
| 884 const float LiveRange::kInvalidWeight = -1.0f; | |
| 885 | |
| 886 | |
| 887 void LiveRange::RecalculateWeight(InstructionSequence* code) { | |
| 888 auto start = Start(); | |
| 889 | |
| 890 CHECK(!spilled()); | |
| 891 | |
| 892 if (IsFixed()) { | |
| 893 weight_ = kMaxWeight; | |
| 894 return; | |
| 895 } | |
| 896 | |
| 897 if (first_interval()->next() == nullptr) { | |
| 898 bool can_be_split_or_spilled = | |
| 899 CanBeSpilled(start) || GetFirstSplittablePosition().IsValid(); | |
| 900 if (!can_be_split_or_spilled) { | |
| 901 weight_ = kMaxWeight; | |
| 902 return; | |
| 903 } | |
| 904 } | |
| 905 | |
| 906 float use_count = 0.0; | |
| 907 auto* pos = first_pos(); | |
| 908 | |
| 909 for (; pos != nullptr; pos = pos->next()) { | |
| 910 auto loop_count = GetContainingLoopCount( | |
| 911 code, code->GetInstructionBlock(pos->pos().ToInstructionIndex())); | |
| 912 use_count += | |
| 913 std::pow(kUseInLoopWeightMultiplier, static_cast<float>(loop_count)); | |
| 914 } | |
| 915 | |
| 916 | |
| 917 if (GetHintedRegister(this) >= 0 && | |
| 918 GetHintedRegister(this) == assigned_register()) { | |
| 919 use_count *= kPreferredRegisterWeightMultiplier; | |
| 920 } | |
| 921 | |
| 922 weight_ = use_count / static_cast<float>(size()); | |
| 923 } | |
| 924 | |
| 925 | |
| 766 static bool AreUseIntervalsIntersecting(UseInterval* interval1, | 926 static bool AreUseIntervalsIntersecting(UseInterval* interval1, |
| 767 UseInterval* interval2) { | 927 UseInterval* interval2) { |
| 768 while (interval1 != nullptr && interval2 != nullptr) { | 928 while (interval1 != nullptr && interval2 != nullptr) { |
| 769 if (interval1->start() < interval2->start()) { | 929 if (interval1->start() < interval2->start()) { |
| 770 if (interval1->end() > interval2->start()) { | 930 if (interval1->end() > interval2->start()) { |
| 771 return true; | 931 return true; |
| 772 } | 932 } |
| 773 interval1 = interval1->next(); | 933 interval1 = interval1->next(); |
| 774 } else { | 934 } else { |
| 775 if (interval2->end() > interval1->start()) { | 935 if (interval2->end() > interval1->start()) { |
| 776 return true; | 936 return true; |
| 777 } | 937 } |
| 778 interval2 = interval2->next(); | 938 interval2 = interval2->next(); |
| 779 } | 939 } |
| 780 } | 940 } |
| 781 return false; | 941 return false; |
| 782 } | 942 } |
| 783 | 943 |
| 784 | 944 |
| 785 std::ostream& operator<<(std::ostream& os, | 945 void PrintIntervals(std::ostream& os, UseInterval* interval) { |
| 786 const PrintableLiveRange& printable_range) { | |
| 787 const LiveRange* range = printable_range.range_; | |
| 788 os << "Range: " << range->id() << " "; | |
| 789 if (range->is_phi()) os << "phi "; | |
| 790 if (range->is_non_loop_phi()) os << "nlphi "; | |
| 791 | |
| 792 os << "{" << std::endl; | |
| 793 auto interval = range->first_interval(); | |
| 794 auto use_pos = range->first_pos(); | |
| 795 PrintableInstructionOperand pio; | |
| 796 pio.register_configuration_ = printable_range.register_configuration_; | |
| 797 while (use_pos != nullptr) { | |
| 798 pio.op_ = *use_pos->operand(); | |
| 799 os << pio << use_pos->pos() << " "; | |
| 800 use_pos = use_pos->next(); | |
| 801 } | |
| 802 os << std::endl; | |
| 803 | |
| 804 while (interval != nullptr) { | 946 while (interval != nullptr) { |
| 805 os << '[' << interval->start() << ", " << interval->end() << ')' | 947 os << '[' << interval->start() << ", " << interval->end() << ')' |
| 806 << std::endl; | 948 << std::endl; |
| 807 interval = interval->next(); | 949 interval = interval->next(); |
| 808 } | 950 } |
| 951 } | |
| 952 | |
| 953 std::ostream& operator<<(std::ostream& os, | |
| 954 const PrintableLiveRange& printable_range) { | |
| 955 PrintableInstructionOperand pio; | |
| 956 pio.register_configuration_ = printable_range.register_configuration_; | |
| 957 | |
| 958 const LiveRange* range = printable_range.range_; | |
| 959 os << "Range: " << range->id() << " "; | |
| 960 if (range->is_phi()) os << "phi "; | |
| 961 if (range->is_non_loop_phi()) os << "nlphi "; | |
| 962 if (range->HasRegisterAssigned()) | |
| 963 os << "R: " << range->assigned_register() << " "; | |
| 964 | |
| 965 if (range->HasSpillOperand()) { | |
| 966 pio.op_ = *(range->GetSpillOperand()); | |
| 967 os << "SOp: " << pio << " "; | |
| 968 } | |
| 969 if (range->HasSpillRange()) { | |
| 970 os << "SR: "; | |
| 971 if (range->GetSpillRange()->IsSlotAssigned()) { | |
| 972 os << range->GetSpillRange()->assigned_slot() << " "; | |
| 973 } else { | |
| 974 os << "x "; | |
| 975 } | |
| 976 } | |
| 977 os << "{" << std::endl; | |
| 978 auto interval = range->first_interval(); | |
| 979 auto use_pos = range->first_pos(); | |
| 980 while (use_pos != nullptr) { | |
| 981 os << "["; | |
| 982 if (use_pos->HasOperand()) { | |
| 983 pio.op_ = *use_pos->operand(); | |
| 984 os << pio << use_pos->pos() << " "; | |
| 985 } else { | |
| 986 os << "<no_op> "; | |
| 987 } | |
| 988 use_pos->dump_hint(os, printable_range.register_configuration_); | |
| 989 os << "] "; | |
| 990 use_pos = use_pos->next(); | |
| 991 } | |
| 992 os << std::endl; | |
| 993 | |
| 994 PrintIntervals(os, interval); | |
| 809 os << "}"; | 995 os << "}"; |
| 810 return os; | 996 return os; |
| 811 } | 997 } |
| 812 | 998 |
| 813 | 999 |
| 1000 std::ostream& operator<<(std::ostream& os, SpillRange* range) { | |
| 1001 if (range->IsSlotAssigned()) { | |
| 1002 os << "Slot: " << range->assigned_slot(); | |
| 1003 } else { | |
| 1004 os << "Unassigned Slot."; | |
| 1005 } | |
| 1006 os << std::endl; | |
| 1007 os << "{"; | |
| 1008 PrintIntervals(os, range->interval()); | |
| 1009 os << "}" << std::endl; | |
| 1010 return os; | |
| 1011 } | |
| 1012 | |
| 1013 | |
| 814 SpillRange::SpillRange(LiveRange* parent, Zone* zone) | 1014 SpillRange::SpillRange(LiveRange* parent, Zone* zone) |
| 815 : live_ranges_(zone), assigned_slot_(kUnassignedSlot) { | 1015 : live_ranges_(zone), assigned_slot_(kUnassignedSlot) { |
| 816 DCHECK(!parent->IsChild()); | 1016 DCHECK(!parent->IsChild()); |
| 817 UseInterval* result = nullptr; | 1017 UseInterval* result = nullptr; |
| 818 UseInterval* node = nullptr; | 1018 UseInterval* node = nullptr; |
| 819 // Copy the intervals for all ranges. | 1019 // Copy the intervals for all ranges. |
| 820 for (auto range = parent; range != nullptr; range = range->next()) { | 1020 for (auto range = parent; range != nullptr; range = range->next()) { |
| 821 auto src = range->first_interval(); | 1021 auto src = range->first_interval(); |
| 822 while (src != nullptr) { | 1022 while (src != nullptr) { |
| 823 auto new_node = new (zone) UseInterval(src->start(), src->end()); | 1023 auto new_node = new (zone) UseInterval(src->start(), src->end()); |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1006 } | 1206 } |
| 1007 auto child = new (allocation_zone()) LiveRange(vreg, range->machine_type()); | 1207 auto child = new (allocation_zone()) LiveRange(vreg, range->machine_type()); |
| 1008 DCHECK_NULL(live_ranges()[vreg]); | 1208 DCHECK_NULL(live_ranges()[vreg]); |
| 1009 live_ranges()[vreg] = child; | 1209 live_ranges()[vreg] = child; |
| 1010 return child; | 1210 return child; |
| 1011 } | 1211 } |
| 1012 | 1212 |
| 1013 | 1213 |
| 1014 RegisterAllocationData::PhiMapValue* RegisterAllocationData::InitializePhiMap( | 1214 RegisterAllocationData::PhiMapValue* RegisterAllocationData::InitializePhiMap( |
| 1015 const InstructionBlock* block, PhiInstruction* phi) { | 1215 const InstructionBlock* block, PhiInstruction* phi) { |
| 1016 auto map_value = new (allocation_zone()) | 1216 auto map_value = |
| 1017 RegisterAllocationData::PhiMapValue(phi, block, allocation_zone()); | 1217 new (allocation_zone()) PhiMapValue(phi, block, allocation_zone()); |
| 1018 auto res = | 1218 auto res = |
| 1019 phi_map_.insert(std::make_pair(phi->virtual_register(), map_value)); | 1219 phi_map_.insert(std::make_pair(phi->virtual_register(), map_value)); |
| 1020 DCHECK(res.second); | 1220 DCHECK(res.second); |
| 1021 USE(res); | 1221 USE(res); |
| 1022 return map_value; | 1222 return map_value; |
| 1023 } | 1223 } |
| 1024 | 1224 |
| 1025 | 1225 |
| 1026 RegisterAllocationData::PhiMapValue* RegisterAllocationData::GetPhiMapValueFor( | 1226 RegisterAllocationData::PhiMapValue* RegisterAllocationData::GetPhiMapValueFor( |
| 1027 int virtual_register) { | 1227 int virtual_register) { |
| (...skipping 809 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1837 // Try hoisting out to an outer loop. | 2037 // Try hoisting out to an outer loop. |
| 1838 loop_header = GetContainingLoop(code(), loop_header); | 2038 loop_header = GetContainingLoop(code(), loop_header); |
| 1839 } | 2039 } |
| 1840 | 2040 |
| 1841 return pos; | 2041 return pos; |
| 1842 } | 2042 } |
| 1843 | 2043 |
| 1844 | 2044 |
| 1845 void RegisterAllocator::Spill(LiveRange* range) { | 2045 void RegisterAllocator::Spill(LiveRange* range) { |
| 1846 DCHECK(!range->spilled()); | 2046 DCHECK(!range->spilled()); |
| 2047 stats_.spills++; | |
| 2048 | |
| 1847 TRACE("Spilling live range %d\n", range->id()); | 2049 TRACE("Spilling live range %d\n", range->id()); |
| 1848 auto first = range->TopLevel(); | 2050 auto first = range->TopLevel(); |
| 1849 if (first->HasNoSpillType()) { | 2051 if (first->HasNoSpillType()) { |
| 1850 data()->AssignSpillRangeToLiveRange(first); | 2052 data()->AssignSpillRangeToLiveRange(first); |
| 1851 } | 2053 } |
| 1852 range->Spill(); | 2054 range->Spill(); |
| 1853 } | 2055 } |
| 1854 | 2056 |
| 1855 | 2057 |
| 1856 LinearScanAllocator::LinearScanAllocator(RegisterAllocationData* data, | 2058 LinearScanAllocator::LinearScanAllocator(RegisterAllocationData* data, |
| 1857 RegisterKind kind, Zone* local_zone) | 2059 RegisterKind kind, Zone* local_zone) |
| 1858 : RegisterAllocator(data, kind), | 2060 : RegisterAllocator(data, kind), |
| 1859 unhandled_live_ranges_(local_zone), | 2061 unhandled_live_ranges_(local_zone), |
| 1860 active_live_ranges_(local_zone), | 2062 active_live_ranges_(local_zone), |
| 1861 inactive_live_ranges_(local_zone) { | 2063 inactive_live_ranges_(local_zone) { |
| 1862 unhandled_live_ranges().reserve( | 2064 unhandled_live_ranges().reserve( |
| 1863 static_cast<size_t>(code()->VirtualRegisterCount() * 2)); | 2065 static_cast<size_t>(code()->VirtualRegisterCount() * 2)); |
| 1864 active_live_ranges().reserve(8); | 2066 active_live_ranges().reserve(8); |
| 1865 inactive_live_ranges().reserve(8); | 2067 inactive_live_ranges().reserve(8); |
| 1866 // TryAllocateFreeReg and AllocateBlockedReg assume this | 2068 // TryAllocateFreeReg and AllocateBlockedReg assume this |
| 1867 // when allocating local arrays. | 2069 // when allocating local arrays. |
| 1868 DCHECK(RegisterConfiguration::kMaxDoubleRegisters >= | 2070 DCHECK(RegisterConfiguration::kMaxDoubleRegisters >= |
| 1869 this->data()->config()->num_general_registers()); | 2071 this->data()->config()->num_general_registers()); |
| 1870 } | 2072 } |
| 1871 | 2073 |
| 1872 | 2074 |
| 1873 void LinearScanAllocator::AllocateRegisters() { | 2075 void LinearScanAllocator::AllocateRegisters() { |
| 2076 stats_.reset(); | |
| 1874 DCHECK(unhandled_live_ranges().empty()); | 2077 DCHECK(unhandled_live_ranges().empty()); |
| 1875 DCHECK(active_live_ranges().empty()); | 2078 DCHECK(active_live_ranges().empty()); |
| 1876 DCHECK(inactive_live_ranges().empty()); | 2079 DCHECK(inactive_live_ranges().empty()); |
| 1877 | 2080 TRACE("Begin allocating function %s with the Linear Allocator\n", |
| 2081 data()->debug_name()); | |
| 1878 for (auto range : data()->live_ranges()) { | 2082 for (auto range : data()->live_ranges()) { |
| 1879 if (range == nullptr) continue; | 2083 if (range == nullptr) continue; |
| 1880 if (range->kind() == mode()) { | 2084 if (range->kind() == mode()) { |
| 1881 AddToUnhandledUnsorted(range); | 2085 AddToUnhandledUnsorted(range); |
| 1882 } | 2086 } |
| 1883 } | 2087 } |
| 1884 SortUnhandled(); | 2088 SortUnhandled(); |
| 1885 DCHECK(UnhandledIsSorted()); | 2089 DCHECK(UnhandledIsSorted()); |
| 1886 | 2090 |
| 1887 auto& fixed_ranges = GetFixedRegisters(data(), mode()); | 2091 auto& fixed_ranges = GetFixedRegisters(data(), mode()); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1944 --i; // Live range was removed from the list of inactive live ranges. | 2148 --i; // Live range was removed from the list of inactive live ranges. |
| 1945 } else if (cur_inactive->Covers(position)) { | 2149 } else if (cur_inactive->Covers(position)) { |
| 1946 InactiveToActive(cur_inactive); | 2150 InactiveToActive(cur_inactive); |
| 1947 --i; // Live range was removed from the list of inactive live ranges. | 2151 --i; // Live range was removed from the list of inactive live ranges. |
| 1948 } | 2152 } |
| 1949 } | 2153 } |
| 1950 | 2154 |
| 1951 DCHECK(!current->HasRegisterAssigned() && !current->spilled()); | 2155 DCHECK(!current->HasRegisterAssigned() && !current->spilled()); |
| 1952 | 2156 |
| 1953 bool result = TryAllocateFreeReg(current); | 2157 bool result = TryAllocateFreeReg(current); |
| 1954 if (!result) AllocateBlockedReg(current); | 2158 if (!result) { |
| 2159 TRACE("Failed to allocate a free reg for %d\n", current->id()); | |
| 2160 AllocateBlockedReg(current); | |
| 2161 } | |
| 1955 if (current->HasRegisterAssigned()) { | 2162 if (current->HasRegisterAssigned()) { |
| 1956 AddToActive(current); | 2163 AddToActive(current); |
| 2164 } else { | |
| 2165 TRACE("Failed to assign register to %d\n", current->id()); | |
| 1957 } | 2166 } |
| 1958 } | 2167 } |
| 2168 TRACE("End allocating function %s with the Linear Allocator\n", | |
| 2169 data()->debug_name()); | |
| 1959 } | 2170 } |
| 1960 | 2171 |
| 1961 | 2172 |
| 1962 const char* LinearScanAllocator::RegisterName(int allocation_index) const { | 2173 const char* RegisterAllocator::RegisterName(int allocation_index) const { |
| 1963 if (mode() == GENERAL_REGISTERS) { | 2174 if (mode() == GENERAL_REGISTERS) { |
| 1964 return data()->config()->general_register_name(allocation_index); | 2175 return data()->config()->general_register_name(allocation_index); |
| 1965 } else { | 2176 } else { |
| 1966 return data()->config()->double_register_name(allocation_index); | 2177 return data()->config()->double_register_name(allocation_index); |
| 1967 } | 2178 } |
| 1968 } | 2179 } |
| 1969 | 2180 |
| 1970 | 2181 |
| 1971 void LinearScanAllocator::SetLiveRangeAssignedRegister(LiveRange* range, | 2182 void LinearScanAllocator::SetLiveRangeAssignedRegister(LiveRange* range, |
| 1972 int reg) { | 2183 int reg) { |
| (...skipping 115 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2088 | 2299 |
| 2089 for (auto cur_inactive : inactive_live_ranges()) { | 2300 for (auto cur_inactive : inactive_live_ranges()) { |
| 2090 DCHECK(cur_inactive->End() > current->Start()); | 2301 DCHECK(cur_inactive->End() > current->Start()); |
| 2091 auto next_intersection = cur_inactive->FirstIntersection(current); | 2302 auto next_intersection = cur_inactive->FirstIntersection(current); |
| 2092 if (!next_intersection.IsValid()) continue; | 2303 if (!next_intersection.IsValid()) continue; |
| 2093 int cur_reg = cur_inactive->assigned_register(); | 2304 int cur_reg = cur_inactive->assigned_register(); |
| 2094 free_until_pos[cur_reg] = Min(free_until_pos[cur_reg], next_intersection); | 2305 free_until_pos[cur_reg] = Min(free_until_pos[cur_reg], next_intersection); |
| 2095 } | 2306 } |
| 2096 | 2307 |
| 2097 int hint_register; | 2308 int hint_register; |
| 2098 if (current->FirstHintPosition(&hint_register) != nullptr) { | 2309 UsePosition* hint = current->FirstHintPosition(&hint_register); |
| 2310 if (hint != nullptr) { | |
| 2099 TRACE("Found reg hint %s (free until [%d) for live range %d (end %d[).\n", | 2311 TRACE("Found reg hint %s (free until [%d) for live range %d (end %d[).\n", |
| 2100 RegisterName(hint_register), free_until_pos[hint_register].value(), | 2312 RegisterName(hint_register), free_until_pos[hint_register].value(), |
| 2101 current->id(), current->End().value()); | 2313 current->id(), current->End().value()); |
| 2102 | 2314 |
| 2103 // The desired register is free until the end of the current live range. | 2315 // The desired register is free until the end of the current live range. |
| 2104 if (free_until_pos[hint_register] >= current->End()) { | 2316 if (free_until_pos[hint_register] >= current->End()) { |
| 2105 TRACE("Assigning preferred reg %s to live range %d\n", | 2317 TRACE("Assigning preferred reg %s to live range %d\n", |
| 2106 RegisterName(hint_register), current->id()); | 2318 RegisterName(hint_register), current->id()); |
| 2107 SetLiveRangeAssignedRegister(current, hint_register); | 2319 SetLiveRangeAssignedRegister(current, hint_register); |
| 2108 return true; | 2320 return true; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2120 auto pos = free_until_pos[reg]; | 2332 auto pos = free_until_pos[reg]; |
| 2121 | 2333 |
| 2122 if (pos <= current->Start()) { | 2334 if (pos <= current->Start()) { |
| 2123 // All registers are blocked. | 2335 // All registers are blocked. |
| 2124 return false; | 2336 return false; |
| 2125 } | 2337 } |
| 2126 | 2338 |
| 2127 if (pos < current->End()) { | 2339 if (pos < current->End()) { |
| 2128 // Register reg is available at the range start but becomes blocked before | 2340 // Register reg is available at the range start but becomes blocked before |
| 2129 // the range end. Split current at position where it becomes blocked. | 2341 // the range end. Split current at position where it becomes blocked. |
| 2342 TRACE( | |
| 2343 "Register %d is available at the range start but becomes blocked " | |
| 2344 "before range %d end\n", | |
| 2345 reg, current->id()); | |
| 2130 auto tail = SplitRangeAt(current, pos); | 2346 auto tail = SplitRangeAt(current, pos); |
| 2131 AddToUnhandledSorted(tail); | 2347 AddToUnhandledSorted(tail); |
| 2132 } | 2348 } |
| 2133 | 2349 |
| 2134 // Register reg is available at the range start and is free until | 2350 // Register reg is available at the range start and is free until |
| 2135 // the range end. | 2351 // the range end. |
| 2136 DCHECK(pos >= current->End()); | 2352 DCHECK(pos >= current->End()); |
| 2137 TRACE("Assigning free reg %s to live range %d\n", RegisterName(reg), | 2353 TRACE("Assigning free reg %s to live range %d\n", RegisterName(reg), |
| 2138 current->id()); | 2354 current->id()); |
| 2139 SetLiveRangeAssignedRegister(current, reg); | 2355 SetLiveRangeAssignedRegister(current, reg); |
| (...skipping 52 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2192 if (use_pos[i] > use_pos[reg]) { | 2408 if (use_pos[i] > use_pos[reg]) { |
| 2193 reg = i; | 2409 reg = i; |
| 2194 } | 2410 } |
| 2195 } | 2411 } |
| 2196 | 2412 |
| 2197 auto pos = use_pos[reg]; | 2413 auto pos = use_pos[reg]; |
| 2198 | 2414 |
| 2199 if (pos < register_use->pos()) { | 2415 if (pos < register_use->pos()) { |
| 2200 // All registers are blocked before the first use that requires a register. | 2416 // All registers are blocked before the first use that requires a register. |
| 2201 // Spill starting part of live range up to that use. | 2417 // Spill starting part of live range up to that use. |
| 2418 TRACE("All registers are blocked before the first use for %d\n", | |
| 2419 current->id()); | |
| 2202 SpillBetween(current, current->Start(), register_use->pos()); | 2420 SpillBetween(current, current->Start(), register_use->pos()); |
| 2203 return; | 2421 return; |
| 2204 } | 2422 } |
| 2205 | 2423 |
| 2206 if (block_pos[reg] < current->End()) { | 2424 if (block_pos[reg] < current->End()) { |
| 2207 // Register becomes blocked before the current range end. Split before that | 2425 // Register becomes blocked before the current range end. Split before that |
| 2208 // position. | 2426 // position. |
| 2427 TRACE("Register %d becomes blocked before end of range %d\n", reg, | |
| 2428 current->id()); | |
| 2209 LiveRange* tail = | 2429 LiveRange* tail = |
| 2210 SplitBetween(current, current->Start(), block_pos[reg].Start()); | 2430 SplitBetween(current, current->Start(), block_pos[reg].Start()); |
| 2211 AddToUnhandledSorted(tail); | 2431 AddToUnhandledSorted(tail); |
| 2212 } | 2432 } |
| 2213 | 2433 |
| 2214 // Register reg is not blocked for the whole range. | 2434 // Register reg is not blocked for the whole range. |
| 2215 DCHECK(block_pos[reg] >= current->End()); | 2435 DCHECK(block_pos[reg] >= current->End()); |
| 2216 TRACE("Assigning blocked reg %s to live range %d\n", RegisterName(reg), | 2436 TRACE("Assigning blocked reg %s to live range %d\n", RegisterName(reg), |
| 2217 current->id()); | 2437 current->id()); |
| 2218 SetLiveRangeAssignedRegister(current, reg); | 2438 SetLiveRangeAssignedRegister(current, reg); |
| 2219 | 2439 |
| 2220 // This register was not free. Thus we need to find and spill | 2440 // This register was not free. Thus we need to find and spill |
| 2221 // parts of active and inactive live regions that use the same register | 2441 // parts of active and inactive live regions that use the same register |
| 2222 // at the same lifetime positions as current. | 2442 // at the same lifetime positions as current. |
| 2223 SplitAndSpillIntersecting(current); | 2443 SplitAndSpillIntersecting(current); |
| 2224 } | 2444 } |
| 2225 | 2445 |
| 2226 | 2446 |
| 2227 void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) { | 2447 void LinearScanAllocator::SplitAndSpillIntersecting(LiveRange* current) { |
| 2228 DCHECK(current->HasRegisterAssigned()); | 2448 DCHECK(current->HasRegisterAssigned()); |
| 2229 int reg = current->assigned_register(); | 2449 int reg = current->assigned_register(); |
| 2230 auto split_pos = current->Start(); | 2450 auto split_pos = current->Start(); |
| 2231 for (size_t i = 0; i < active_live_ranges().size(); ++i) { | 2451 for (size_t i = 0; i < active_live_ranges().size(); ++i) { |
| 2232 auto range = active_live_ranges()[i]; | 2452 auto range = active_live_ranges()[i]; |
| 2233 if (range->assigned_register() == reg) { | 2453 if (range->assigned_register() == reg) { |
| 2234 auto next_pos = range->NextRegisterPosition(current->Start()); | 2454 auto next_pos = range->NextRegisterPosition(current->Start()); |
| 2235 auto spill_pos = FindOptimalSpillingPos(range, split_pos); | 2455 auto spill_pos = FindOptimalSpillingPos(range, split_pos); |
| 2236 if (next_pos == nullptr) { | 2456 if (next_pos == nullptr) { |
| 2457 TRACE("SplitAndSpillIntersecting (1). Range %d, for %d\n", range->id(), | |
| 2458 current->id()); | |
| 2237 SpillAfter(range, spill_pos); | 2459 SpillAfter(range, spill_pos); |
| 2238 } else { | 2460 } else { |
| 2239 // When spilling between spill_pos and next_pos ensure that the range | 2461 // When spilling between spill_pos and next_pos ensure that the range |
| 2240 // remains spilled at least until the start of the current live range. | 2462 // remains spilled at least until the start of the current live range. |
| 2241 // This guarantees that we will not introduce new unhandled ranges that | 2463 // This guarantees that we will not introduce new unhandled ranges that |
| 2242 // start before the current range as this violates allocation invariant | 2464 // start before the current range as this violates allocation invariant |
| 2243 // and will lead to an inconsistent state of active and inactive | 2465 // and will lead to an inconsistent state of active and inactive |
| 2244 // live-ranges: ranges are allocated in order of their start positions, | 2466 // live-ranges: ranges are allocated in order of their start positions, |
| 2245 // ranges are retired from active/inactive when the start of the | 2467 // ranges are retired from active/inactive when the start of the |
| 2246 // current live-range is larger than their end. | 2468 // current live-range is larger than their end. |
| 2469 TRACE("SplitAndSpillIntersecting (2). Range %d, for %d\n", range->id(), | |
| 2470 current->id()); | |
| 2247 SpillBetweenUntil(range, spill_pos, current->Start(), next_pos->pos()); | 2471 SpillBetweenUntil(range, spill_pos, current->Start(), next_pos->pos()); |
| 2248 } | 2472 } |
| 2249 ActiveToHandled(range); | 2473 ActiveToHandled(range); |
| 2250 --i; | 2474 --i; |
| 2251 } | 2475 } |
| 2252 } | 2476 } |
| 2253 | 2477 |
| 2254 for (size_t i = 0; i < inactive_live_ranges().size(); ++i) { | 2478 for (size_t i = 0; i < inactive_live_ranges().size(); ++i) { |
| 2255 auto range = inactive_live_ranges()[i]; | 2479 auto range = inactive_live_ranges()[i]; |
| 2256 DCHECK(range->End() > current->Start()); | 2480 DCHECK(range->End() > current->Start()); |
| 2257 if (range->assigned_register() == reg && !range->IsFixed()) { | 2481 if (range->assigned_register() == reg && !range->IsFixed()) { |
| 2258 LifetimePosition next_intersection = range->FirstIntersection(current); | 2482 LifetimePosition next_intersection = range->FirstIntersection(current); |
| 2259 if (next_intersection.IsValid()) { | 2483 if (next_intersection.IsValid()) { |
| 2260 UsePosition* next_pos = range->NextRegisterPosition(current->Start()); | 2484 UsePosition* next_pos = range->NextRegisterPosition(current->Start()); |
| 2261 if (next_pos == nullptr) { | 2485 if (next_pos == nullptr) { |
| 2486 TRACE("SplitAndSpillIntersecting (3). Range %d, for %d\n", | |
| 2487 range->id(), current->id()); | |
| 2262 SpillAfter(range, split_pos); | 2488 SpillAfter(range, split_pos); |
| 2263 } else { | 2489 } else { |
| 2264 next_intersection = Min(next_intersection, next_pos->pos()); | 2490 next_intersection = Min(next_intersection, next_pos->pos()); |
| 2491 TRACE("SplitAndSpillIntersecting (4). Range %d, for %d\n", | |
| 2492 range->id(), current->id()); | |
| 2265 SpillBetween(range, split_pos, next_intersection); | 2493 SpillBetween(range, split_pos, next_intersection); |
| 2266 } | 2494 } |
| 2267 InactiveToHandled(range); | 2495 InactiveToHandled(range); |
| 2268 --i; | 2496 --i; |
| 2269 } | 2497 } |
| 2270 } | 2498 } |
| 2271 } | 2499 } |
| 2272 } | 2500 } |
| 2273 | 2501 |
| 2274 | 2502 |
| (...skipping 116 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2391 Spill(second_part); | 2619 Spill(second_part); |
| 2392 AddToUnhandledSorted(third_part); | 2620 AddToUnhandledSorted(third_part); |
| 2393 } else { | 2621 } else { |
| 2394 // The split result does not intersect with [start, end[. | 2622 // The split result does not intersect with [start, end[. |
| 2395 // Nothing to spill. Just put it to unhandled as whole. | 2623 // Nothing to spill. Just put it to unhandled as whole. |
| 2396 AddToUnhandledSorted(second_part); | 2624 AddToUnhandledSorted(second_part); |
| 2397 } | 2625 } |
| 2398 } | 2626 } |
| 2399 | 2627 |
| 2400 | 2628 |
| 2629 conflict_iterator& conflict_iterator::operator++() { | |
| 2630 if (pos_ == storage_->end()) { | |
|
Jarin
2015/06/09 15:00:41
Overall, this gymnastics of MoveToEnd seem to be q
Mircea Trofin
2015/06/10 05:37:10
I'll take the suggestion with storage_, some compl
| |
| 2631 MoveToEnd(); | |
| 2632 return *this; | |
| 2633 } | |
| 2634 ++pos_; | |
| 2635 if (pos_ == storage_->end()) { | |
| 2636 MoveToEnd(); | |
| 2637 return *this; | |
| 2638 } | |
| 2639 if (!Intersects(query_->start(), query_->end(), pos_->start, pos_->end)) { | |
| 2640 query_ = query_->next(); | |
| 2641 if (query_ == nullptr) { | |
| 2642 MoveToEnd(); | |
| 2643 return *this; | |
| 2644 } | |
| 2645 // We may discover it is better to linearly skip over non-conflicting | |
| 2646 // use intervals rather than re-do the seeking in InitializeForNewQuery. | |
| 2647 // No profiling data yet on this one. | |
| 2648 InitializeForNewQuery(); | |
| 2649 } | |
| 2650 return *this; | |
| 2651 } | |
| 2652 | |
| 2653 | |
| 2654 bool operator==(const conflict_iterator& first, | |
| 2655 const conflict_iterator& second) { | |
| 2656 bool same_storage_and_query = | |
| 2657 first.storage_ == second.storage_ && first.query_ == second.query_; | |
|
Jarin
2015/06/09 15:00:41
Does it even make sense to compare iterators with
Mircea Trofin
2015/06/10 05:37:10
That makes sense, thanks!
| |
| 2658 if (same_storage_and_query && first.IsValid()) | |
| 2659 return first.pos_ == second.pos_; | |
| 2660 return same_storage_and_query; | |
| 2661 } | |
| 2662 | |
| 2663 | |
| 2664 bool operator!=(const conflict_iterator& first, | |
| 2665 const conflict_iterator& second) { | |
| 2666 return !(first == second); | |
| 2667 } | |
| 2668 | |
| 2669 | |
| 2670 void conflict_iterator::InitializeForNewQuery() { | |
| 2671 if (storage_->empty()) { | |
| 2672 MoveToEnd(); | |
| 2673 return; | |
| 2674 } | |
| 2675 | |
| 2676 // If the last element starts before the query, we have no conflicts. | |
| 2677 if (storage_->rbegin()->end <= query_->start()) { | |
| 2678 MoveToEnd(); | |
| 2679 return; | |
| 2680 } | |
| 2681 | |
| 2682 for (; query_ != nullptr; query_ = query_->next()) { | |
| 2683 auto q_start = query_->start(); | |
| 2684 auto q_end = query_->end(); | |
| 2685 if (storage_->begin()->start >= q_end) { | |
| 2686 // Skip over queried use intervals that end before the first stored | |
| 2687 // element. | |
| 2688 continue; | |
| 2689 } | |
| 2690 | |
| 2691 // Seek the first stored element that contains q_start. We do so by first | |
| 2692 // finding the last stored interval that starts before q_start. Either there | |
| 2693 // is a stored interval that starts at or after, meaning the one before is | |
| 2694 // "it", or we position at the end of storage. We then check that the thus | |
| 2695 // found stored interval actually intersects (or overlaps) with the current | |
| 2696 // query. | |
| 2697 pos_ = storage_->lower_bound(AsAllocatedInterval(q_start)); | |
|
Jarin
2015/06/09 15:00:42
Would not the upper_bound be easier to handle here
Mircea Trofin
2015/06/10 05:37:11
upper_bound would never return a pos_ starting at
Jarin
2015/06/12 04:09:10
Actually, you did what I had in mind (and fixed th
| |
| 2698 if (pos_ != storage_->end()) { | |
| 2699 if (pos_->start == q_start) return; | |
| 2700 if (pos_->start > q_start && pos_ != storage_->begin()) { | |
| 2701 // If pos_ starts after the query, then --pos_ starts strictly before. | |
| 2702 // See if that position still includes q_start | |
| 2703 --pos_; | |
| 2704 if (pos_->end <= q_start) { | |
| 2705 ++pos_; | |
| 2706 } | |
| 2707 } | |
| 2708 } else if (pos_ != storage_->begin()) { | |
| 2709 // No stored interval starts at or after q_start. So maybe the last one | |
| 2710 // ends after q_start. Position at the last valid element. | |
| 2711 --pos_; | |
| 2712 } | |
| 2713 if (!Intersects(q_start, q_end, pos_->start, pos_->end)) { | |
| 2714 continue; | |
| 2715 } | |
| 2716 break; | |
|
Jarin
2015/06/09 15:00:42
Why can't you just say "if (Intersects(...)) break
Mircea Trofin
2015/06/10 05:37:10
No reason - good catch.
| |
| 2717 } | |
| 2718 | |
| 2719 // If we got here because we couldn't find an intersection and got to the last | |
| 2720 // use interval query, we have no conflicts. | |
| 2721 if (query_ == nullptr) { | |
| 2722 MoveToEnd(); | |
| 2723 return; | |
| 2724 } | |
| 2725 | |
| 2726 // If we found a conflict, advance query_ past the last use interval that | |
| 2727 // still intersects/overlaps with the current conflict, so that operator++ can | |
| 2728 // pick up from there. | |
| 2729 for (; query_->next() != nullptr; query_ = query_->next()) { | |
| 2730 if (!Intersects(query_->next()->start(), query_->next()->end(), pos_->start, | |
| 2731 pos_->end)) { | |
| 2732 break; | |
| 2733 } | |
| 2734 } | |
| 2735 } | |
| 2736 | |
| 2737 | |
| 2738 // Collection of live ranges allocated to the same register. | |
| 2739 // It supports efficiently finding all conflicts for a given, non-allocated | |
| 2740 // range. See AllocatedInterval. | |
| 2741 // Allocated live ranges do not intersect. At most, individual use intervals | |
| 2742 // touch. We store, for a live range, an AllocatedInterval corresponding to each | |
| 2743 // of that range's UseIntervals. We keep the list of AllocatedIntervals sorted | |
| 2744 // by starts. Then, given the non-intersecting property, we know that | |
| 2745 // consecutive AllocatedIntervals have the property that the "smaller"'s end is | |
| 2746 // less or equal to the "larger"'s start. | |
| 2747 // This allows for quick (logarithmic complexity) identification of the first | |
| 2748 // AllocatedInterval to conflict with a given LiveRange, and then for efficient | |
| 2749 // traversal of conflicts. See also conflict_iterator. | |
| 2401 class CoalescedLiveRanges : public ZoneObject { | 2750 class CoalescedLiveRanges : public ZoneObject { |
| 2402 public: | 2751 public: |
| 2403 explicit CoalescedLiveRanges(Zone* zone) : storage_(zone) {} | 2752 explicit CoalescedLiveRanges(Zone* zone) : storage_(zone) {} |
| 2404 | 2753 |
| 2405 LiveRange* Find(UseInterval* query) { | 2754 void clear() { storage_.clear(); } |
| 2406 ZoneSplayTree<Config>::Locator locator; | |
| 2407 | 2755 |
| 2408 if (storage_.Find(GetKey(query), &locator)) { | 2756 |
| 2409 return locator.value(); | 2757 conflict_iterator first_conflict(LiveRange* query) { |
| 2410 } | 2758 return first_conflict(query->first_interval()); |
| 2411 return nullptr; | |
| 2412 } | 2759 } |
| 2413 | 2760 |
| 2414 // TODO(mtrofin): Change to void returning if we do not care if the interval | 2761 conflict_iterator conflict_end() { return {}; } |
| 2415 // was previously added. | 2762 |
| 2416 bool Insert(LiveRange* range) { | 2763 // We assume it was determined that this range does not conflict with |
| 2417 auto* interval = range->first_interval(); | 2764 // contained ranges. |
| 2418 while (interval != nullptr) { | 2765 void insert(LiveRange* range) { |
|
Jarin
2015/06/09 15:00:42
Style nit: why did you change the name to lower ca
Mircea Trofin
2015/06/10 05:37:10
to emulate the collections' casing - but happy to
| |
| 2419 if (!Insert(interval, range)) return false; | 2766 for (auto interval = range->first_interval(); interval != nullptr; |
| 2420 interval = interval->next(); | 2767 interval = interval->next()) { |
| 2768 storage_.insert({interval->start(), interval->end(), range}); | |
| 2769 } | |
| 2770 } | |
| 2771 | |
| 2772 void remove(LiveRange* range) { | |
| 2773 for (auto interval = range->first_interval(); interval != nullptr; | |
| 2774 interval = interval->next()) { | |
| 2775 storage_.erase({interval->start(), interval->end(), range}); | |
| 2776 } | |
| 2777 } | |
| 2778 | |
| 2779 bool empty() const { return storage_.empty(); } | |
| 2780 | |
| 2781 bool IsValid() { | |
| 2782 LifetimePosition last_end = LifetimePosition::GapFromInstructionIndex(0); | |
| 2783 for (auto i : storage_) { | |
| 2784 if (i.start < last_end) { | |
| 2785 return false; | |
| 2786 } | |
| 2787 last_end = i.end; | |
| 2421 } | 2788 } |
| 2422 return true; | 2789 return true; |
| 2423 } | 2790 } |
| 2424 | 2791 |
| 2425 bool Remove(LiveRange* range) { | 2792 private: |
| 2426 bool ret = false; | 2793 conflict_iterator first_conflict(UseInterval* query) { |
| 2427 auto* segment = range->first_interval(); | 2794 return conflict_iterator(query, &storage_); |
| 2428 while (segment != nullptr) { | |
| 2429 ret |= Remove(segment); | |
| 2430 segment = segment->next(); | |
| 2431 } | |
| 2432 return ret; | |
| 2433 } | 2795 } |
| 2434 | 2796 |
| 2435 bool IsEmpty() { return storage_.is_empty(); } | 2797 ZoneSet<AllocatedInterval, AllocatedInterval::Comparer> storage_; |
| 2436 | |
| 2437 private: | |
| 2438 struct Config { | |
| 2439 typedef std::pair<int, int> Key; | |
| 2440 typedef LiveRange* Value; | |
| 2441 static const Key kNoKey; | |
| 2442 static Value NoValue() { return nullptr; } | |
| 2443 static int Compare(const Key& a, const Key& b) { | |
| 2444 if (a.second <= b.first) return -1; | |
| 2445 if (a.first >= b.second) return 1; | |
| 2446 return 0; | |
| 2447 } | |
| 2448 }; | |
| 2449 | |
| 2450 Config::Key GetKey(UseInterval* interval) { | |
| 2451 if (interval == nullptr) return std::make_pair(0, 0); | |
| 2452 return std::make_pair(interval->start().value(), interval->end().value()); | |
| 2453 } | |
| 2454 | |
| 2455 // TODO(mtrofin): Change to void returning if we do not care if the interval | |
| 2456 // was previously added. | |
| 2457 bool Insert(UseInterval* interval, LiveRange* range) { | |
| 2458 ZoneSplayTree<Config>::Locator locator; | |
| 2459 bool ret = storage_.Insert(GetKey(interval), &locator); | |
| 2460 if (ret) locator.set_value(range); | |
| 2461 return ret; | |
| 2462 } | |
| 2463 | |
| 2464 bool Remove(UseInterval* key) { return storage_.Remove(GetKey(key)); } | |
| 2465 | |
| 2466 ZoneSplayTree<Config> storage_; | |
| 2467 DISALLOW_COPY_AND_ASSIGN(CoalescedLiveRanges); | 2798 DISALLOW_COPY_AND_ASSIGN(CoalescedLiveRanges); |
| 2468 }; | 2799 }; |
| 2469 | 2800 |
| 2470 | 2801 |
| 2471 const std::pair<int, int> CoalescedLiveRanges::Config::kNoKey = {0, 0}; | 2802 std::ostream& operator<<(std::ostream& os, const AllocatorStats& stats) { |
| 2803 os << "losses after eviction: " << stats.losses_after_eviction << std::endl; | |
| 2804 os << "losses, no eviction: " << stats.losses_no_eviction << std::endl; | |
| 2805 os << "spills: " << stats.spills << std::endl; | |
| 2806 os << "wins: " << stats.wins << std::endl; | |
| 2807 os << "split attempts: " << stats.good_split_attempts << std::endl; | |
| 2808 os << "split successes: " << stats.good_split_successes << std::endl; | |
| 2809 os << "splits above: " << stats.good_split_above << std::endl; | |
| 2810 os << "splits below: " << stats.good_split_below << std::endl; | |
| 2811 os << "smart splits above: " << stats.good_split_smart_above << std::endl; | |
| 2812 os << "smart splits below: " << stats.good_split_smart_below << std::endl; | |
| 2813 os << "groups allocated: " << stats.groups_allocated << std::endl; | |
| 2814 os << "groups attempted: " << stats.groups_attempted << std::endl; | |
| 2815 os << "groups succeeded: " << stats.groups_succeeded << std::endl; | |
| 2816 return os; | |
| 2817 } | |
| 2818 | |
| 2472 | 2819 |
| 2473 GreedyAllocator::GreedyAllocator(RegisterAllocationData* data, | 2820 GreedyAllocator::GreedyAllocator(RegisterAllocationData* data, |
| 2474 RegisterKind kind, Zone* local_zone) | 2821 RegisterKind kind, Zone* local_zone) |
| 2475 : RegisterAllocator(data, kind), | 2822 : RegisterAllocator(data, kind), |
| 2476 local_zone_(local_zone), | 2823 local_zone_(local_zone), |
| 2477 allocations_(local_zone), | 2824 allocations_(local_zone), |
| 2478 queue_(local_zone) {} | 2825 queue_(local_zone) {} |
| 2479 | 2826 |
| 2480 | 2827 |
| 2481 unsigned GreedyAllocator::GetLiveRangeSize(LiveRange* range) { | |
| 2482 auto interval = range->first_interval(); | |
| 2483 if (interval == nullptr) return 0; | |
| 2484 | |
| 2485 unsigned size = 0; | |
| 2486 while (interval != nullptr) { | |
| 2487 size += (interval->end().value() - interval->start().value()); | |
| 2488 interval = interval->next(); | |
| 2489 } | |
| 2490 | |
| 2491 return size; | |
| 2492 } | |
| 2493 | |
| 2494 | 2828 |
| 2495 void GreedyAllocator::AssignRangeToRegister(int reg_id, LiveRange* range) { | 2829 void GreedyAllocator::AssignRangeToRegister(int reg_id, LiveRange* range) { |
| 2496 allocations_[reg_id]->Insert(range); | 2830 allocations_[reg_id]->insert(range); |
| 2497 if (range->HasRegisterAssigned()) { | 2831 if (range->HasRegisterAssigned()) { |
| 2498 DCHECK_EQ(reg_id, range->assigned_register()); | 2832 DCHECK_EQ(reg_id, range->assigned_register()); |
| 2499 return; | 2833 return; |
| 2500 } | 2834 } |
| 2835 TRACE("Assigning %s to range %d\n", RegisterName(reg_id), range->id()); | |
| 2501 range->set_assigned_register(reg_id); | 2836 range->set_assigned_register(reg_id); |
| 2502 range->SetUseHints(reg_id); | 2837 range->SetUseHints(reg_id); |
| 2503 if (range->is_phi()) { | 2838 if (range->is_phi()) { |
| 2504 data()->GetPhiMapValueFor(range->id())->set_assigned_register(reg_id); | 2839 data()->GetPhiMapValueFor(range->id())->set_assigned_register(reg_id); |
| 2505 } | 2840 } |
| 2506 } | 2841 range->RecalculateWeight(code()); |
| 2507 | 2842 DCHECK(allocations_[reg_id]->IsValid()); |
| 2508 | |
| 2509 float GreedyAllocator::CalculateSpillWeight(LiveRange* range) { | |
| 2510 InstructionOperand* first_hint = nullptr; | |
| 2511 if (range->FirstHintPosition() != nullptr) { | |
| 2512 first_hint = range->FirstHintPosition()->operand(); | |
| 2513 } | |
| 2514 | |
| 2515 if (range->IsFixed()) return std::numeric_limits<float>::max(); | |
| 2516 bool spill; | |
| 2517 if (!FindProgressingSplitPosition(range, &spill).IsValid()) | |
| 2518 return std::numeric_limits<float>::max(); | |
| 2519 | |
| 2520 float multiplier = 1.0; | |
| 2521 if (first_hint != nullptr && first_hint->IsRegister()) { | |
| 2522 multiplier = 3.0; | |
| 2523 } | |
| 2524 | |
| 2525 unsigned use_count = 0; | |
| 2526 auto* pos = range->first_pos(); | |
| 2527 while (pos != nullptr) { | |
| 2528 use_count++; | |
| 2529 pos = pos->next(); | |
| 2530 } | |
| 2531 | |
| 2532 unsigned range_size = GetLiveRangeSize(range); | |
| 2533 DCHECK_NE(0U, range_size); | |
| 2534 | |
| 2535 return multiplier * static_cast<float>(use_count) / | |
| 2536 static_cast<float>(range_size); | |
| 2537 } | |
| 2538 | |
| 2539 | |
| 2540 float GreedyAllocator::CalculateMaxSpillWeight( | |
| 2541 const ZoneSet<LiveRange*>& ranges) { | |
| 2542 float max = 0.0; | |
| 2543 for (auto* r : ranges) { | |
| 2544 max = std::max(max, CalculateSpillWeight(r)); | |
| 2545 } | |
| 2546 return max; | |
| 2547 } | 2843 } |
| 2548 | 2844 |
| 2549 | 2845 |
| 2550 void GreedyAllocator::Evict(LiveRange* range) { | 2846 void GreedyAllocator::Evict(LiveRange* range) { |
| 2551 bool removed = allocations_[range->assigned_register()]->Remove(range); | 2847 TRACE("Evicting range %d from register %s\n", range->id(), |
| 2552 CHECK(removed); | 2848 RegisterName(range->assigned_register())); |
| 2553 range->UnsetUseHints(); | 2849 range->UnsetUseHints(); |
| 2554 range->UnsetAssignedRegister(); | 2850 range->UnsetAssignedRegister(); |
| 2555 if (range->is_phi()) { | 2851 if (range->is_phi()) { |
| 2556 data()->GetPhiMapValueFor(range->id())->UnsetAssignedRegister(); | 2852 data()->GetPhiMapValueFor(range->id())->UnsetAssignedRegister(); |
| 2557 } | 2853 } |
| 2558 } | 2854 } |
| 2559 | 2855 |
| 2560 | 2856 |
| 2561 bool GreedyAllocator::TryAllocatePhysicalRegister( | |
| 2562 unsigned reg_id, LiveRange* range, ZoneSet<LiveRange*>* conflicting) { | |
| 2563 auto* segment = range->first_interval(); | |
| 2564 | |
| 2565 auto* alloc_info = allocations_[reg_id]; | |
| 2566 while (segment != nullptr) { | |
| 2567 if (auto* existing = alloc_info->Find(segment)) { | |
| 2568 DCHECK(existing->HasRegisterAssigned()); | |
| 2569 conflicting->insert(existing); | |
| 2570 } | |
| 2571 segment = segment->next(); | |
| 2572 } | |
| 2573 if (!conflicting->empty()) return false; | |
| 2574 // No conflicts means we can safely allocate this register to this range. | |
| 2575 AssignRangeToRegister(reg_id, range); | |
| 2576 return true; | |
| 2577 } | |
| 2578 | |
| 2579 | |
| 2580 int GreedyAllocator::GetHintedRegister(LiveRange* range) { | |
| 2581 int reg; | |
| 2582 if (range->FirstHintPosition(®) != nullptr) { | |
| 2583 return reg; | |
| 2584 } | |
| 2585 return -1; | |
| 2586 } | |
| 2587 | |
| 2588 | |
| 2589 bool GreedyAllocator::TryAllocate(LiveRange* current, | |
| 2590 ZoneSet<LiveRange*>* conflicting) { | |
| 2591 if (current->IsFixed()) { | |
| 2592 return TryAllocatePhysicalRegister(current->assigned_register(), current, | |
| 2593 conflicting); | |
| 2594 } | |
| 2595 | |
| 2596 int hinted_reg_id = GetHintedRegister(current); | |
| 2597 if (hinted_reg_id >= 0) { | |
| 2598 if (TryAllocatePhysicalRegister(hinted_reg_id, current, conflicting)) { | |
| 2599 return true; | |
| 2600 } | |
| 2601 } | |
| 2602 | |
| 2603 ZoneSet<LiveRange*> local_conflicts(local_zone()); | |
| 2604 for (unsigned candidate_reg = 0; candidate_reg < allocations_.size(); | |
| 2605 candidate_reg++) { | |
| 2606 if (hinted_reg_id >= 0 && | |
| 2607 candidate_reg == static_cast<size_t>(hinted_reg_id)) | |
| 2608 continue; | |
| 2609 local_conflicts.clear(); | |
| 2610 if (TryAllocatePhysicalRegister(candidate_reg, current, &local_conflicts)) { | |
| 2611 conflicting->clear(); | |
| 2612 return true; | |
| 2613 } else { | |
| 2614 conflicting->insert(local_conflicts.begin(), local_conflicts.end()); | |
| 2615 } | |
| 2616 } | |
| 2617 return false; | |
| 2618 } | |
| 2619 | |
| 2620 | |
| 2621 LiveRange* GreedyAllocator::SpillBetweenUntil(LiveRange* range, | 2857 LiveRange* GreedyAllocator::SpillBetweenUntil(LiveRange* range, |
| 2622 LifetimePosition start, | 2858 LifetimePosition start, |
| 2623 LifetimePosition until, | 2859 LifetimePosition until, |
| 2624 LifetimePosition end) { | 2860 LifetimePosition end) { |
| 2625 CHECK(start < end); | 2861 CHECK(start < end); |
| 2626 auto second_part = SplitRangeAt(range, start); | 2862 auto second_part = SplitRangeAt(range, start); |
| 2627 | 2863 |
| 2628 if (second_part->Start() < end) { | 2864 if (second_part->Start() < end) { |
| 2629 // The split result intersects with [start, end[. | 2865 // The split result intersects with [start, end[. |
| 2630 // Split it at position between ]start+1, end[, spill the middle part | 2866 // Split it at position between ]start+1, end[, spill the middle part |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 2642 return third_part; | 2878 return third_part; |
| 2643 } else { | 2879 } else { |
| 2644 // The split result does not intersect with [start, end[. | 2880 // The split result does not intersect with [start, end[. |
| 2645 // Nothing to spill. Just return it for re-processing. | 2881 // Nothing to spill. Just return it for re-processing. |
| 2646 return second_part; | 2882 return second_part; |
| 2647 } | 2883 } |
| 2648 } | 2884 } |
| 2649 | 2885 |
| 2650 | 2886 |
| 2651 void GreedyAllocator::Enqueue(LiveRange* range) { | 2887 void GreedyAllocator::Enqueue(LiveRange* range) { |
| 2652 if (range == nullptr || range->IsEmpty()) return; | 2888 unsigned size = range->size(); |
| 2653 unsigned size = GetLiveRangeSize(range); | 2889 range->RecalculateWeight(code()); |
| 2654 TRACE("Enqueuing range %d\n", range->id()); | 2890 TRACE("Enqueuing range %d size %d\n", range->id(), size); |
| 2655 queue_.push(std::make_pair(size, range)); | 2891 DCHECK(size > 0); |
| 2892 queue_.push({size, PQueueElem(range)}); | |
| 2656 } | 2893 } |
| 2657 | 2894 |
| 2658 | 2895 |
| 2659 bool GreedyAllocator::HandleSpillOperands(LiveRange* range) { | 2896 // We treat groups of ranges as one, so that we try first to allocate |
| 2897 // them all to the same register. If that fails, they get processed as | |
| 2898 // individual ranges. | |
| 2899 void GreedyAllocator::Enqueue(LiveRangeGroup* group) { | |
| 2900 unsigned size = 0; | |
| 2901 for (auto r : group->ranges()) { | |
| 2902 size += r->size(); | |
| 2903 r->RecalculateWeight(code()); | |
| 2904 } | |
| 2905 | |
| 2906 DCHECK(size > 0); | |
| 2907 TRACE("Enqueuing group of size %d\n", size); | |
| 2908 queue_.push({size, PQueueElem(group)}); | |
| 2909 } | |
| 2910 | |
| 2911 | |
| 2912 bool GreedyAllocator::HandleSpillOperands(LiveRange* range, | |
| 2913 LiveRange** remaining) { | |
| 2660 auto position = range->Start(); | 2914 auto position = range->Start(); |
| 2661 TRACE("Processing interval %d start=%d\n", range->id(), position.value()); | 2915 TRACE("Processing interval %d start=%d\n", range->id(), position.value()); |
| 2662 | 2916 |
| 2663 if (!range->HasNoSpillType()) { | 2917 if (!range->HasNoSpillType()) { |
| 2664 TRACE("Live range %d already has a spill operand\n", range->id()); | 2918 TRACE("Live range %d already has a spill operand\n", range->id()); |
| 2665 auto next_pos = position; | 2919 auto next_pos = position; |
| 2666 if (next_pos.IsGapPosition()) { | 2920 if (next_pos.IsGapPosition()) { |
| 2667 next_pos = next_pos.NextStart(); | 2921 next_pos = next_pos.NextStart(); |
| 2668 } | 2922 } |
| 2669 auto pos = range->NextUsePositionRegisterIsBeneficial(next_pos); | 2923 auto pos = range->NextUsePositionRegisterIsBeneficial(next_pos); |
| 2670 // If the range already has a spill operand and it doesn't need a | 2924 // If the range already has a spill operand and it doesn't need a |
| 2671 // register immediately, split it and spill the first part of the range. | 2925 // register immediately, split it and spill the first part of the range. |
| 2672 if (pos == nullptr) { | 2926 if (pos == nullptr) { |
| 2673 Spill(range); | 2927 Spill(range); |
| 2674 return true; | 2928 return true; |
| 2675 } else if (pos->pos() > range->Start().NextStart()) { | 2929 } else if (pos->pos() > range->Start().NextStart()) { |
| 2676 // Do not spill live range eagerly if use position that can benefit from | 2930 // Do not spill live range eagerly if use position that can benefit from |
| 2677 // the register is too close to the start of live range. | 2931 // the register is too close to the start of live range. |
| 2678 auto* reminder = SpillBetweenUntil(range, position, position, pos->pos()); | 2932 *remaining = SpillBetweenUntil(range, position, position, pos->pos()); |
| 2679 Enqueue(reminder); | |
| 2680 return true; | 2933 return true; |
| 2681 } | 2934 } |
| 2682 } | 2935 } |
| 2683 return TryReuseSpillForPhi(range); | 2936 return false; |
| 2937 } | |
| 2938 | |
| 2939 | |
| 2940 // TODO(mtrofin): consider using CoalescedLiveRanges for grouping. | |
| 2941 bool CanAddToGroup(LiveRange* r, LiveRangeGroup* grp) { | |
|
Jarin
2015/06/09 15:00:42
Style nit: move to an anonymous name space (or mak
Mircea Trofin
2015/06/10 05:37:11
Done.
| |
| 2942 bool ret = true; | |
| 2943 for (auto member : grp->ranges()) { | |
| 2944 if (member->FirstIntersection(r).IsValid()) { | |
| 2945 ret = false; | |
|
Jarin
2015/06/09 15:00:41
How about "return false" here and get rid of the "
Mircea Trofin
2015/06/10 05:37:10
Done.
| |
| 2946 break; | |
| 2947 } | |
| 2948 } | |
| 2949 return ret; | |
| 2950 } | |
| 2951 | |
| 2952 | |
| 2953 void TryMergeGroups(LiveRange* r1, LiveRange* r2) { | |
|
Jarin
2015/06/09 15:00:41
It is a bit funny that its name is TryMergeGroups
Mircea Trofin
2015/06/10 05:37:10
Done.
| |
| 2954 DCHECK(r1->group() != nullptr); | |
| 2955 DCHECK(r2->group() != nullptr); | |
| 2956 | |
| 2957 bool can_merge = true; | |
| 2958 for (auto r : r1->group()->ranges()) { | |
| 2959 if (!CanAddToGroup(r, r2->group())) { | |
| 2960 can_merge = false; | |
| 2961 break; | |
| 2962 } | |
| 2963 } | |
| 2964 if (can_merge) { | |
| 2965 for (auto r : r1->group()->ranges()) { | |
| 2966 r2->group()->ranges().insert(r); | |
| 2967 r->SetGroup(r2->group()); | |
| 2968 } | |
| 2969 r1->SetGroup(r2->group()); | |
|
Jarin
2015/06/09 15:00:42
Is this necessary? r1 should be part of r1->group(
Mircea Trofin
2015/06/10 05:37:11
Done.
| |
| 2970 } | |
| 2971 } | |
| 2972 | |
| 2973 | |
| 2974 void TryGroup(LiveRange* r1, LiveRange* r2, Zone* local_zone) { | |
| 2975 if (r1->group() == nullptr) { | |
| 2976 if (r2->group() == nullptr) { | |
| 2977 if (!r1->FirstIntersection(r2).IsValid()) { | |
| 2978 LiveRangeGroup* grp = new (local_zone) LiveRangeGroup(local_zone); | |
| 2979 grp->ranges().insert(r1); | |
| 2980 grp->ranges().insert(r2); | |
| 2981 r1->SetGroup(grp); | |
| 2982 r2->SetGroup(grp); | |
| 2983 } | |
| 2984 return; | |
| 2985 } | |
| 2986 return TryGroup(r2, r1, local_zone); | |
| 2987 } | |
| 2988 DCHECK(r1->group() != nullptr); | |
| 2989 if (r2->group() == nullptr) { | |
| 2990 if (CanAddToGroup(r2, r1->group())) { | |
| 2991 r1->group()->ranges().insert(r2); | |
| 2992 r2->SetGroup(r1->group()); | |
| 2993 } | |
| 2994 return; | |
| 2995 } | |
| 2996 TryMergeGroups(r1, r2); | |
| 2997 } | |
| 2998 | |
| 2999 | |
| 3000 void GreedyAllocator::GroupAndEnqueue() { | |
| 3001 // Group phi inputs to the output. Ideally, they get all allocated to the same | |
| 3002 // register, avoiding moves. | |
| 3003 for (auto r : data()->live_ranges()) { | |
| 3004 if (r == nullptr || r->IsEmpty() || r->kind() != mode()) continue; | |
|
Jarin
2015/06/09 15:00:42
Are all these cases really possible? It seems to m
Mircea Trofin
2015/06/10 05:37:10
A similar test happens in LinearScanAllocator::All
| |
| 3005 if (r->is_phi()) { | |
| 3006 auto phi_map = data()->GetPhiMapValueFor(r->id()); | |
| 3007 auto phi = phi_map->phi(); | |
| 3008 auto inputs = phi->operands(); | |
| 3009 for (auto i : inputs) { | |
| 3010 LiveRange* in_range = data()->live_ranges()[i]; | |
| 3011 TryGroup(r, in_range, local_zone()); | |
| 3012 } | |
| 3013 } | |
| 3014 } | |
| 3015 | |
| 3016 ZoneSet<LiveRangeGroup*> seen_groups(local_zone()); | |
| 3017 for (auto range : data()->live_ranges()) { | |
| 3018 if (range == nullptr || range->IsEmpty() || range->spilled() || | |
| 3019 range->kind() != mode()) | |
| 3020 continue; | |
| 3021 | |
| 3022 if (range->group() != nullptr) { | |
| 3023 auto grp = range->group(); | |
| 3024 if (seen_groups.count(grp) > 0) continue; | |
| 3025 seen_groups.insert(grp); | |
| 3026 Enqueue(grp); | |
| 3027 if (FLAG_trace_alloc) { | |
| 3028 OFStream os(stdout); | |
| 3029 os << "group: " << std::endl; | |
| 3030 PrintableLiveRange plr; | |
| 3031 plr.register_configuration_ = data()->config(); | |
| 3032 for (auto r : grp->ranges()) { | |
| 3033 plr.range_ = r; | |
| 3034 os << plr; | |
| 3035 } | |
| 3036 os << std::endl; | |
| 3037 } | |
| 3038 } else { | |
| 3039 Enqueue(range); | |
| 3040 } | |
| 3041 } | |
| 3042 } | |
| 3043 | |
| 3044 | |
| 3045 void GreedyAllocator::EvictAll(int reg, | |
| 3046 const conflict_iterator& first_conflict) { | |
| 3047 for (conflict_iterator c = first_conflict; c.IsValid();) { | |
| 3048 auto range = *c; | |
| 3049 while (c.IsValid() && *c == range) ++c; | |
| 3050 | |
| 3051 DCHECK(range->HasRegisterAssigned()); | |
| 3052 CHECK(!range->IsFixed()); | |
| 3053 allocations_[reg]->remove(range); | |
| 3054 Evict(range); | |
| 3055 Enqueue(range); | |
| 3056 } | |
| 3057 } | |
| 3058 | |
| 3059 | |
| 3060 void GreedyAllocator::AllocateRange(LiveRange* current) { | |
| 3061 TRACE("Processing interval %d of size %d\n", current->id(), current->size()); | |
| 3062 | |
| 3063 LiveRange* remaining = nullptr; | |
| 3064 if (HandleSpillOperands(current, &remaining)) { | |
| 3065 if (remaining != nullptr) Enqueue(remaining); | |
| 3066 return; | |
| 3067 } | |
| 3068 | |
| 3069 // TODO(mtrofin): Does the linear algo's hinting mechanism even matter, | |
| 3070 // now that we have groups? | |
|
Jarin
2015/06/09 15:00:42
I think some hinting is important for both allocat
Mircea Trofin
2015/06/10 05:37:10
I wonder if more elaborate grouping will help ther
| |
| 3071 int hint_reg = GetHintedRegister(current); | |
| 3072 float my_weight = current->weight(); | |
| 3073 if (hint_reg >= 0) { | |
| 3074 auto first_conflict = allocations_[hint_reg]->first_conflict(current); | |
| 3075 if (!first_conflict.IsValid()) { | |
| 3076 AssignRangeToRegister(hint_reg, current); | |
| 3077 return; | |
| 3078 } | |
| 3079 float max_weight = CalculateMaxSpillWeight( | |
| 3080 first_conflict, allocations_[hint_reg]->conflict_end()); | |
| 3081 if (max_weight < my_weight) { | |
| 3082 EvictAll(hint_reg, first_conflict); | |
| 3083 AssignRangeToRegister(hint_reg, current); | |
| 3084 return; | |
| 3085 } | |
| 3086 } | |
| 3087 | |
| 3088 int free_reg = -1; | |
| 3089 conflict_iterator all_conflicts[RegisterConfiguration::kMaxDoubleRegisters]; | |
| 3090 for (int i = 0; i < num_registers(); i++) { | |
| 3091 auto conflict = allocations_[i]->first_conflict(current); | |
| 3092 if (!conflict.IsValid()) { | |
| 3093 free_reg = i; | |
| 3094 break; | |
| 3095 } | |
| 3096 all_conflicts[i] = conflict; | |
| 3097 } | |
| 3098 | |
| 3099 if (free_reg >= 0) { | |
| 3100 AssignRangeToRegister(free_reg, current); | |
| 3101 return; | |
| 3102 } | |
| 3103 free_reg = FindRegisterToEvictForRange(all_conflicts, my_weight); | |
| 3104 if (free_reg >= 0) { | |
| 3105 EvictAll(free_reg, all_conflicts[free_reg]); | |
| 3106 AssignRangeToRegister(free_reg, current); | |
| 3107 return; | |
| 3108 } | |
| 3109 HandleBlockedRange(current); | |
| 3110 } | |
| 3111 | |
| 3112 template <typename TIter> | |
| 3113 float GreedyAllocator::CalculateMaxSpillWeight(const TIter& begin, | |
| 3114 const TIter& end) { | |
| 3115 float ret = 0.0; | |
| 3116 for (auto s = begin; s != end; ++s) { | |
| 3117 ret = Max(ret, (*s)->weight()); | |
| 3118 if (ret == LiveRange::kMaxWeight) return ret; | |
| 3119 } | |
| 3120 return ret; | |
| 3121 } | |
| 3122 | |
| 3123 | |
| 3124 bool GreedyAllocator::TryAllocateGroup(LiveRangeGroup* grp) { | |
| 3125 for (int i = 0; i < num_registers(); i++) { | |
| 3126 if (TryAllocateGroupAtRegister(i, grp)) { | |
| 3127 return true; | |
| 3128 } | |
| 3129 } | |
| 3130 return false; | |
| 3131 } | |
| 3132 | |
| 3133 | |
| 3134 bool GreedyAllocator::TryAllocateGroupAtRegister(unsigned reg, | |
| 3135 LiveRangeGroup* grp) { | |
| 3136 auto ranges = grp->ranges(); | |
| 3137 for (auto r : ranges) { | |
| 3138 auto first_conflict = allocations_[reg]->first_conflict(r); | |
| 3139 if (first_conflict.IsValid()) { | |
| 3140 return false; | |
| 3141 } | |
| 3142 } | |
| 3143 for (auto r : ranges) { | |
| 3144 AssignRangeToRegister(reg, r); | |
| 3145 } | |
| 3146 return true; | |
| 3147 } | |
| 3148 | |
| 3149 | |
| 3150 int GreedyAllocator::FindRegisterToEvictForRange( | |
| 3151 conflict_iterator all_conflicts[RegisterConfiguration::kMaxDoubleRegisters], | |
| 3152 float competing) { | |
| 3153 int ret = -1; | |
| 3154 float smallest_weight = LiveRange::kMaxWeight; | |
| 3155 for (int i = 0; i < num_registers(); ++i) { | |
| 3156 float w = CalculateMaxSpillWeight(all_conflicts[i], conflict_iterator()); | |
| 3157 if (w >= competing) continue; | |
| 3158 if (w < smallest_weight) { | |
| 3159 smallest_weight = w; | |
| 3160 ret = i; | |
| 3161 } | |
| 3162 } | |
| 3163 return ret; | |
| 3164 } | |
| 3165 | |
| 3166 | |
| 3167 int GreedyAllocator::FindRegisterToEvictForGroup(LiveRangeGroup* grp, | |
| 3168 float competing) { | |
| 3169 int ret = -1; | |
| 3170 auto ranges = grp->ranges(); | |
| 3171 float smallest_weight = LiveRange::kMaxWeight; | |
| 3172 for (int i = 0; i < num_registers(); ++i) { | |
| 3173 float grp_counter_weight = 0.0; | |
| 3174 for (auto r : ranges) { | |
| 3175 auto first_conflict = allocations_[i]->first_conflict(r); | |
| 3176 if (!first_conflict.IsValid()) continue; | |
| 3177 auto w = CalculateMaxSpillWeight(first_conflict, conflict_iterator()); | |
| 3178 grp_counter_weight = Max(grp_counter_weight, w); | |
| 3179 if (grp_counter_weight >= competing) break; | |
| 3180 } | |
| 3181 if (grp_counter_weight >= competing) continue; | |
| 3182 if (grp_counter_weight < smallest_weight) { | |
| 3183 smallest_weight = grp_counter_weight; | |
| 3184 ret = i; | |
| 3185 } | |
| 3186 } | |
| 3187 return ret; | |
| 3188 } | |
| 3189 | |
| 3190 | |
| 3191 // Utility for improved readability in AllocateGroup | |
| 3192 enum Attempt { | |
| 3193 Error = -1, | |
| 3194 BeforeEviction = 0, | |
| 3195 AfterEviction = 1, | |
| 3196 AllocateIndividuals = 2 | |
| 3197 }; | |
| 3198 | |
| 3199 | |
| 3200 static Attempt Next(Attempt a) { | |
| 3201 int i = static_cast<int>(a); | |
|
Jarin
2015/06/09 15:00:41
This code is both brittle and hard to read. Please
Mircea Trofin
2015/06/10 05:37:10
Done.
| |
| 3202 if (i < 0) return Error; | |
| 3203 if (i > 2) return Error; | |
| 3204 return static_cast<Attempt>(i + 1); | |
| 3205 } | |
| 3206 | |
| 3207 | |
| 3208 // TODO(mtrofin): improved code reuse with AllocateRange? | |
| 3209 void GreedyAllocator::AllocateGroup(LiveRangeGroup* grp) { | |
| 3210 // Modify the group ranges content after handling spill operands | |
| 3211 for (auto iter = grp->ranges().begin(), end = grp->ranges().end(); | |
| 3212 iter != end;) { | |
| 3213 auto iter_backup = iter; | |
| 3214 auto range = *iter++; | |
| 3215 LiveRange* reminder = nullptr; | |
| 3216 if (HandleSpillOperands(range, &reminder)) { | |
|
Jarin
2015/06/09 15:00:41
reminder -> remainder
Mircea Trofin
2015/06/10 05:37:10
Done.
| |
| 3217 grp->ranges().erase(iter_backup); | |
| 3218 if (reminder != nullptr) { | |
| 3219 grp->ranges().insert(reminder); | |
| 3220 reminder->RecalculateWeight(code()); | |
| 3221 } | |
| 3222 } | |
| 3223 } | |
| 3224 | |
| 3225 float grp_weight = -1.0; | |
| 3226 for (Attempt state = BeforeEviction; state != AllocateIndividuals; | |
| 3227 Next(state)) { | |
|
Jarin
2015/06/09 15:00:42
You mean "state = Next(state)"?
I am puzzled why
Mircea Trofin
2015/06/10 05:37:11
Ya, there's a bug, it should be state = Next(state
Jarin
2015/06/10 05:59:07
Actually, there is a return at the end of the loop
| |
| 3228 if (TryAllocateGroup(grp)) { | |
| 3229 stats_.groups_allocated++; | |
| 3230 return; | |
| 3231 } | |
| 3232 | |
| 3233 if (grp_weight < 0.0) | |
| 3234 grp_weight = | |
| 3235 CalculateMaxSpillWeight(grp->ranges().begin(), grp->ranges().end()); | |
| 3236 int reg_to_free = FindRegisterToEvictForGroup(grp, grp_weight); | |
| 3237 if (reg_to_free < 0) break; | |
| 3238 for (auto r : grp->ranges()) { | |
| 3239 EvictAll(reg_to_free, allocations_[reg_to_free]->first_conflict(r)); | |
| 3240 AssignRangeToRegister(reg_to_free, r); | |
| 3241 } | |
| 3242 return; | |
| 3243 } | |
| 3244 | |
| 3245 for (auto r : grp->ranges()) { | |
| 3246 Enqueue(r); | |
| 3247 } | |
| 2684 } | 3248 } |
| 2685 | 3249 |
| 2686 | 3250 |
| 2687 void GreedyAllocator::AllocateRegisters() { | 3251 void GreedyAllocator::AllocateRegisters() { |
| 2688 for (auto range : data()->live_ranges()) { | 3252 stats_.reset(); |
| 2689 if (range == nullptr) continue; | 3253 CHECK_EQ(0, queue_.size()); |
| 2690 if (range->kind() == mode()) { | 3254 CHECK_EQ(0, allocations_.size()); |
|
Jarin
2015/06/09 15:00:41
CHECK(queue_.empty());
CHECK(allocations_.empty())
Mircea Trofin
2015/06/10 05:37:11
Done.
| |
| 2691 DCHECK(!range->HasRegisterAssigned() && !range->spilled()); | 3255 |
| 2692 TRACE("Enqueueing live range %d to priority queue \n", range->id()); | 3256 TRACE("Begin allocating function %s with the Greedy Allocator\n", |
| 2693 Enqueue(range); | 3257 data()->debug_name()); |
| 2694 } | |
| 2695 } | |
| 2696 | 3258 |
| 2697 allocations_.resize(num_registers()); | 3259 allocations_.resize(num_registers()); |
| 2698 for (int i = 0; i < num_registers(); i++) { | 3260 for (int i = 0; i < num_registers(); i++) { |
| 2699 allocations_[i] = new (local_zone()) CoalescedLiveRanges(local_zone()); | 3261 allocations_[i] = new (local_zone()) CoalescedLiveRanges(local_zone()); |
| 2700 } | 3262 } |
| 2701 | 3263 |
| 2702 for (auto* current : GetFixedRegisters(data(), mode())) { | 3264 for (auto* current : GetFixedRegisters(data(), mode())) { |
| 2703 if (current != nullptr) { | 3265 if (current != nullptr) { |
| 2704 DCHECK_EQ(mode(), current->kind()); | 3266 DCHECK_EQ(mode(), current->kind()); |
| 2705 int reg_nr = current->assigned_register(); | 3267 int reg_nr = current->assigned_register(); |
| 2706 bool inserted = allocations_[reg_nr]->Insert(current); | 3268 allocations_[reg_nr]->insert(current); |
| 2707 CHECK(inserted); | 3269 current->RecalculateWeight(code()); |
| 2708 } | 3270 } |
| 2709 } | 3271 } |
| 3272 | |
| 3273 GroupAndEnqueue(); | |
| 2710 | 3274 |
| 2711 while (!queue_.empty()) { | 3275 while (!queue_.empty()) { |
| 2712 auto current_pair = queue_.top(); | 3276 auto current_pair = queue_.top(); |
|
Jarin
2015/06/09 15:00:42
Could you replace the "auto" with a real type here
Mircea Trofin
2015/06/10 05:37:10
Done.
I'll keep an eye for other instances of thi
| |
| 2713 queue_.pop(); | 3277 queue_.pop(); |
| 2714 auto current = current_pair.second; | 3278 auto element = current_pair.second; |
| 2715 if (HandleSpillOperands(current)) { | 3279 if (element.IsGroup()) { |
| 2716 continue; | 3280 AllocateGroup(element.get_group()); |
| 2717 } | 3281 } else { |
| 2718 bool spill = false; | 3282 auto current = element.get_range(); |
| 2719 ZoneSet<LiveRange*> conflicting(local_zone()); | 3283 AllocateRange(current); |
| 2720 if (!TryAllocate(current, &conflicting)) { | |
| 2721 DCHECK(!conflicting.empty()); | |
| 2722 float this_weight = std::numeric_limits<float>::max(); | |
| 2723 LifetimePosition split_pos = | |
| 2724 FindProgressingSplitPosition(current, &spill); | |
| 2725 if (split_pos.IsValid()) { | |
| 2726 this_weight = CalculateSpillWeight(current); | |
| 2727 } | |
| 2728 | |
| 2729 bool evicted = false; | |
| 2730 for (auto* conflict : conflicting) { | |
| 2731 if (CalculateSpillWeight(conflict) < this_weight) { | |
| 2732 Evict(conflict); | |
| 2733 Enqueue(conflict); | |
| 2734 evicted = true; | |
| 2735 } | |
| 2736 } | |
| 2737 if (evicted) { | |
| 2738 conflicting.clear(); | |
| 2739 TryAllocate(current, &conflicting); | |
| 2740 } | |
| 2741 if (!conflicting.empty()) { | |
| 2742 DCHECK(!current->IsFixed() || current->CanBeSpilled(current->Start())); | |
| 2743 DCHECK(split_pos.IsValid()); | |
| 2744 AllocateBlockedRange(current, split_pos, spill); | |
| 2745 } | |
| 2746 } | 3284 } |
| 2747 } | 3285 } |
| 2748 | 3286 |
| 2749 for (size_t i = 0; i < allocations_.size(); ++i) { | 3287 for (size_t i = 0; i < allocations_.size(); ++i) { |
| 2750 if (!allocations_[i]->IsEmpty()) { | 3288 if (!allocations_[i]->empty()) { |
| 2751 data()->MarkAllocated(mode(), static_cast<int>(i)); | 3289 data()->MarkAllocated(mode(), static_cast<int>(i)); |
| 2752 } | 3290 } |
| 2753 } | 3291 } |
| 2754 } | 3292 allocations_.clear(); |
| 2755 | 3293 |
| 2756 | 3294 for (auto r : data()->live_ranges()) { |
| 2757 LifetimePosition GreedyAllocator::GetSplittablePos(LifetimePosition pos) { | 3295 if (r == nullptr || r->IsEmpty() || r->kind() != mode()) continue; |
| 2758 auto ret = pos.PrevStart().End(); | 3296 if (!r->spilled()) continue; |
| 2759 if (IsBlockBoundary(code(), pos.Start())) { | 3297 auto top = r->TopLevel(); |
| 2760 ret = pos.Start(); | 3298 if (top->group() != nullptr) { |
| 2761 } | 3299 if (!top->HasSpillRange()) continue; |
| 2762 DCHECK(ret <= pos); | 3300 auto top_sp_range = top->GetSpillRange(); |
| 2763 return ret; | 3301 CHECK(top_sp_range != nullptr); |
|
Jarin
2015/06/12 04:09:10
CHECK -> DCHECK
top_sp_range -> top_spill_range
W
| |
| 2764 } | 3302 for (auto m : top->group()->ranges()) { |
| 2765 | 3303 if (!m->HasSpillRange()) continue; |
| 2766 LifetimePosition GreedyAllocator::FindProgressingSplitPosition( | 3304 auto m_sp_range = m->TopLevel()->GetSpillRange(); |
| 2767 LiveRange* range, bool* is_spill_pos) { | 3305 if (m_sp_range == top_sp_range) continue; |
| 3306 bool merged = top_sp_range->TryMerge(m_sp_range); | |
| 3307 CHECK(merged); | |
| 3308 } | |
| 3309 } | |
|
Jarin
2015/06/09 15:00:41
I am a bit confused about what this is doing. It l
Mircea Trofin
2015/06/10 05:37:11
It merges the spill ranges of live ranges belongin
| |
| 3310 } | |
| 3311 TRACE("End allocating function %s with the Greedy Allocator\n", | |
| 3312 data()->debug_name()); | |
| 3313 } | |
| 3314 | |
| 3315 | |
| 3316 void GreedyAllocator::HandleBlockedRange(LiveRange* current) { | |
| 3317 // Make the best possible decision for splitting this range. The resulting | |
| 3318 // chunks may have a better chance at allocation, or, if not, will eventually | |
| 3319 // be unsplittable and "fit". | |
| 3320 | |
| 3321 // TODO(mtrofin): more tuning. Is the ordering the one we want? | |
| 3322 auto start = current->Start(); | |
| 3323 auto end = current->End(); | |
| 3324 | |
| 3325 UsePosition* next_reg_use = | |
| 3326 current->NextUsePositionRegisterIsBeneficial(start); | |
| 3327 | |
| 3328 if (current->is_phi()) { | |
| 3329 CHECK(next_reg_use != nullptr && next_reg_use->pos() == start); | |
| 3330 // If the range does not need register soon, spill it to the merged | |
| 3331 // spill range. | |
| 3332 auto next_pos = start; | |
| 3333 if (next_pos.IsGapPosition()) next_pos = next_pos.NextStart(); | |
| 3334 auto pos = current->NextUsePositionRegisterIsBeneficial(next_pos); | |
| 3335 if (pos == nullptr) { | |
| 3336 Spill(current); | |
| 3337 return; | |
| 3338 } else if (pos->pos() > start.NextStart()) { | |
| 3339 Enqueue(SpillBetweenUntil(current, start, start, pos->pos())); | |
| 3340 return; | |
| 3341 } | |
| 3342 } | |
| 3343 | |
| 3344 if (next_reg_use == nullptr) { | |
| 3345 auto pos = FindOptimalSpillingPos(current, start); | |
| 3346 DCHECK(pos.IsValid()); | |
| 3347 auto tail = SplitRangeAt(current, pos); | |
| 3348 Spill(tail); | |
| 3349 if (tail != current) { | |
| 3350 Enqueue(current); | |
| 3351 } | |
| 3352 return; | |
| 3353 } | |
| 3354 | |
| 3355 if (TrySplitAroundCalls(current)) return; | |
| 3356 if (TrySplitBeforeLoops(current)) return; | |
| 3357 if (TrySplitAfterLoops(current)) return; | |
| 3358 | |
| 3359 | |
| 3360 if (current->CanBeSpilled(start)) { | |
| 3361 UsePosition* next_mandatory_use = nullptr; | |
| 3362 for (next_mandatory_use = current->first_pos(); | |
| 3363 next_mandatory_use != nullptr; | |
| 3364 next_mandatory_use = next_mandatory_use->next()) { | |
| 3365 if (next_mandatory_use->type() == UsePositionType::kRequiresRegister) | |
| 3366 break; | |
| 3367 } | |
| 3368 if (next_mandatory_use == nullptr) | |
|
Jarin
2015/06/09 15:00:41
Please, put the then- and else- clauses into brace
Mircea Trofin
2015/06/10 05:37:11
Ugh... yes... llvm-ism
| |
| 3369 Spill(current); | |
| 3370 else | |
| 3371 Enqueue( | |
| 3372 SpillBetweenUntil(current, start, start, next_mandatory_use->pos())); | |
| 3373 return; | |
| 3374 } | |
| 3375 | |
| 3376 if (current->first_interval()->next() != nullptr) { | |
| 3377 auto tail = SplitRangeAt(current, current->first_interval()->end()); | |
| 3378 DCHECK(tail != current); | |
| 3379 Enqueue(tail); | |
| 3380 Enqueue(current); | |
| 3381 return; | |
| 3382 } | |
| 3383 | |
| 3384 auto pos_to_split = current->GetFirstSplittablePosition(); | |
| 3385 CHECK(pos_to_split.IsValid() && start < pos_to_split && pos_to_split < end); | |
| 3386 auto tail = SplitRangeAt(current, pos_to_split); | |
| 3387 CHECK(tail != current); | |
| 3388 Enqueue(tail); | |
| 3389 Enqueue(current); | |
| 3390 } | |
| 3391 | |
| 3392 | |
| 3393 bool GreedyAllocator::TrySplitAroundCalls(LiveRange* range) { | |
| 3394 // TODO(mtrofin): should we just split around all calls? | |
| 2768 auto start = range->Start(); | 3395 auto start = range->Start(); |
| 2769 auto end = range->End(); | 3396 auto end = range->End(); |
| 2770 | 3397 for (auto i = range->first_interval(); i != nullptr; i = i->next()) { |
| 2771 UsePosition* next_reg_use = range->first_pos(); | 3398 for (int instr_pos = i->start().ToInstructionIndex(); |
| 2772 while (next_reg_use != nullptr && | 3399 instr_pos < i->end().ToInstructionIndex(); instr_pos++) { |
| 2773 next_reg_use->type() != UsePositionType::kRequiresRegister) { | 3400 auto instr = code()->InstructionAt(instr_pos); |
| 2774 next_reg_use = next_reg_use->next(); | 3401 if (instr->IsCall()) { |
| 2775 } | 3402 auto pos = LifetimePosition::GapFromInstructionIndex(instr_pos); |
| 2776 | 3403 if (start >= pos || pos >= end) continue; |
| 2777 if (next_reg_use == nullptr) { | 3404 auto tail = SplitRangeAt(range, pos); |
| 2778 *is_spill_pos = true; | 3405 DCHECK(tail != range); |
| 2779 auto ret = FindOptimalSpillingPos(range, start); | 3406 Enqueue(tail); |
| 2780 DCHECK(ret.IsValid()); | 3407 Enqueue(range); |
| 2781 return ret; | 3408 return true; |
| 2782 } | 3409 } |
| 2783 | 3410 } |
| 2784 *is_spill_pos = false; | 3411 } |
| 2785 auto reg_pos = next_reg_use->pos(); | 3412 return false; |
| 2786 auto correct_pos = GetSplittablePos(reg_pos); | 3413 } |
| 2787 if (start < correct_pos && correct_pos < end) { | 3414 |
| 2788 return correct_pos; | 3415 |
| 2789 } | 3416 bool GreedyAllocator::TrySplitBeforeLoops(LiveRange* range) { |
| 2790 | 3417 auto start = range->Start(); |
| 2791 if (correct_pos >= end) { | 3418 auto end = range->End(); |
| 2792 return LifetimePosition::Invalid(); | 3419 for (auto pos = range->first_pos(); pos != nullptr; pos = pos->next()) { |
| 2793 } | 3420 if (!pos->RegisterIsBeneficial()) continue; |
| 2794 | 3421 const InstructionBlock* block = |
| 2795 // Correct_pos must be at or before start. Find the next use position. | 3422 code()->GetInstructionBlock(pos->pos().ToInstructionIndex()); |
| 2796 next_reg_use = next_reg_use->next(); | 3423 if (block->IsLoopHeader() || block->loop_header().IsValid()) { |
| 2797 auto reference = reg_pos; | 3424 block = block->IsLoopHeader() ? block : GetContainingLoop(code(), block); |
| 2798 while (next_reg_use != nullptr) { | 3425 auto split_pos = start; |
| 2799 auto pos = next_reg_use->pos(); | 3426 while (block != nullptr) { |
| 2800 // Skip over tight successive uses. | 3427 auto before_loop_start = LifetimePosition::GapFromInstructionIndex( |
| 2801 if (reference.NextStart() < pos) { | 3428 block->first_instruction_index() - 1); |
| 2802 break; | 3429 |
| 2803 } | 3430 if (range->Covers(before_loop_start)) { |
| 2804 reference = pos; | 3431 split_pos = before_loop_start; |
| 2805 next_reg_use = next_reg_use->next(); | 3432 } |
| 2806 } | 3433 |
| 2807 | 3434 // Try hoisting out to an outer loop. |
| 2808 if (next_reg_use == nullptr) { | 3435 block = GetContainingLoop(code(), block); |
| 2809 // While there may not be another use, we may still have space in the range | 3436 } |
| 2810 // to clip off. | 3437 if (start < split_pos && split_pos < end) { |
| 2811 correct_pos = reference.NextStart(); | 3438 auto tail = SplitRangeAt(range, split_pos); |
| 2812 if (start < correct_pos && correct_pos < end) { | 3439 Enqueue(tail); |
| 2813 return correct_pos; | 3440 Enqueue(range); |
| 2814 } | 3441 return true; |
| 2815 return LifetimePosition::Invalid(); | 3442 } |
| 2816 } | 3443 } |
| 2817 | 3444 } |
| 2818 correct_pos = GetSplittablePos(next_reg_use->pos()); | 3445 return false; |
| 2819 if (start < correct_pos && correct_pos < end) { | 3446 } |
| 2820 DCHECK(reference < correct_pos); | 3447 |
| 2821 return correct_pos; | 3448 |
| 2822 } | 3449 bool GreedyAllocator::TrySplitAfterLoops(LiveRange* range) { |
| 2823 return LifetimePosition::Invalid(); | 3450 auto start = range->Start(); |
| 2824 } | 3451 auto end = range->End(); |
| 2825 | 3452 for (auto pos = range->first_pos(); pos != nullptr; pos = pos->next()) { |
| 2826 | 3453 if (!pos->RegisterIsBeneficial()) continue; |
| 2827 void GreedyAllocator::AllocateBlockedRange(LiveRange* current, | 3454 const InstructionBlock* block = |
| 2828 LifetimePosition pos, bool spill) { | 3455 code()->GetInstructionBlock(pos->pos().ToInstructionIndex()); |
| 2829 auto tail = SplitRangeAt(current, pos); | 3456 if (block->IsLoopHeader() || block->loop_header().IsValid()) { |
| 2830 if (spill) { | 3457 auto header = block->IsLoopHeader() |
| 2831 Spill(tail); | 3458 ? block |
| 2832 } else { | 3459 : code()->InstructionBlockAt(block->loop_header()); |
| 2833 Enqueue(tail); | 3460 |
| 2834 } | 3461 auto split_pos = start; |
| 2835 if (tail != current) { | 3462 while (header != nullptr) { |
| 2836 Enqueue(current); | 3463 if (header->loop_end().ToSize() >= |
| 2837 } | 3464 static_cast<size_t>(code()->InstructionBlockCount())) |
| 2838 } | 3465 break; |
| 2839 | 3466 auto loop_end = code()->InstructionBlockAt(header->loop_end()); |
| 2840 | 3467 auto after_loop_start = LifetimePosition::GapFromInstructionIndex( |
| 3468 loop_end->last_instruction_index() + 1); | |
| 3469 | |
| 3470 if (range->Covers(after_loop_start)) { | |
| 3471 split_pos = after_loop_start; | |
| 3472 } | |
| 3473 | |
| 3474 // Try hoisting out to an outer loop. | |
| 3475 header = GetContainingLoop(code(), header); | |
| 3476 } | |
| 3477 if (start < split_pos && split_pos < end) { | |
| 3478 auto tail = SplitRangeAt(range, split_pos); | |
| 3479 Enqueue(tail); | |
| 3480 Enqueue(range); | |
| 3481 return true; | |
| 3482 } | |
| 3483 } | |
| 3484 } | |
| 3485 return false; | |
| 3486 } | |
| 3487 | |
| 3488 | |
| 2841 SpillSlotLocator::SpillSlotLocator(RegisterAllocationData* data) | 3489 SpillSlotLocator::SpillSlotLocator(RegisterAllocationData* data) |
| 2842 : data_(data) {} | 3490 : data_(data) {} |
| 2843 | 3491 |
| 2844 | 3492 |
| 2845 void SpillSlotLocator::LocateSpillSlots() { | 3493 void SpillSlotLocator::LocateSpillSlots() { |
| 2846 auto code = data()->code(); | 3494 auto code = data()->code(); |
| 2847 for (auto range : data()->live_ranges()) { | 3495 for (auto range : data()->live_ranges()) { |
| 2848 if (range == nullptr || range->IsEmpty() || range->IsChild()) continue; | 3496 if (range == nullptr || range->IsEmpty() || range->IsChild()) continue; |
| 2849 // We care only about ranges which spill in the frame. | 3497 // We care only about ranges which spill in the frame. |
| 2850 if (!range->HasSpillRange()) continue; | 3498 if (!range->HasSpillRange()) continue; |
| 2851 auto spills = range->spills_at_definition(); | 3499 auto spills = range->spills_at_definition(); |
| 2852 DCHECK_NOT_NULL(spills); | 3500 DCHECK_NOT_NULL(spills); |
| 2853 for (; spills != nullptr; spills = spills->next) { | 3501 for (; spills != nullptr; spills = spills->next) { |
| 2854 code->GetInstructionBlock(spills->gap_index)->mark_needs_frame(); | 3502 code->GetInstructionBlock(spills->gap_index)->mark_needs_frame(); |
| 2855 } | 3503 } |
| 2856 } | 3504 } |
| 2857 } | 3505 } |
| 2858 | 3506 |
| 2859 | 3507 |
| 2860 bool GreedyAllocator::TryReuseSpillForPhi(LiveRange* range) { | |
| 2861 if (range->IsChild() || !range->is_phi()) return false; | |
| 2862 DCHECK(!range->HasSpillOperand()); | |
| 2863 | |
| 2864 auto phi_map_value = data()->GetPhiMapValueFor(range->id()); | |
| 2865 auto phi = phi_map_value->phi(); | |
| 2866 auto block = phi_map_value->block(); | |
| 2867 // Count the number of spilled operands. | |
| 2868 size_t spilled_count = 0; | |
| 2869 LiveRange* first_op = nullptr; | |
| 2870 for (size_t i = 0; i < phi->operands().size(); i++) { | |
| 2871 int op = phi->operands()[i]; | |
| 2872 LiveRange* op_range = LiveRangeFor(op); | |
| 2873 if (!op_range->HasSpillRange()) continue; | |
| 2874 auto pred = code()->InstructionBlockAt(block->predecessors()[i]); | |
| 2875 auto pred_end = LifetimePosition::InstructionFromInstructionIndex( | |
| 2876 pred->last_instruction_index()); | |
| 2877 while (op_range != nullptr && !op_range->CanCover(pred_end)) { | |
| 2878 op_range = op_range->next(); | |
| 2879 } | |
| 2880 if (op_range != nullptr && op_range->spilled()) { | |
| 2881 spilled_count++; | |
| 2882 if (first_op == nullptr) { | |
| 2883 first_op = op_range->TopLevel(); | |
| 2884 } | |
| 2885 } | |
| 2886 } | |
| 2887 | |
| 2888 // Only continue if more than half of the operands are spilled. | |
| 2889 if (spilled_count * 2 <= phi->operands().size()) { | |
| 2890 return false; | |
| 2891 } | |
| 2892 | |
| 2893 // Try to merge the spilled operands and count the number of merged spilled | |
| 2894 // operands. | |
| 2895 DCHECK(first_op != nullptr); | |
| 2896 auto first_op_spill = first_op->GetSpillRange(); | |
| 2897 size_t num_merged = 1; | |
| 2898 for (size_t i = 1; i < phi->operands().size(); i++) { | |
| 2899 int op = phi->operands()[i]; | |
| 2900 auto op_range = LiveRangeFor(op); | |
| 2901 if (!op_range->HasSpillRange()) continue; | |
| 2902 auto op_spill = op_range->GetSpillRange(); | |
| 2903 if (op_spill == first_op_spill || first_op_spill->TryMerge(op_spill)) { | |
| 2904 num_merged++; | |
| 2905 } | |
| 2906 } | |
| 2907 | |
| 2908 // Only continue if enough operands could be merged to the | |
| 2909 // same spill slot. | |
| 2910 if (num_merged * 2 <= phi->operands().size() || | |
| 2911 AreUseIntervalsIntersecting(first_op_spill->interval(), | |
| 2912 range->first_interval())) { | |
| 2913 return false; | |
| 2914 } | |
| 2915 | |
| 2916 // If the range does not need register soon, spill it to the merged | |
| 2917 // spill range. | |
| 2918 auto next_pos = range->Start(); | |
| 2919 if (next_pos.IsGapPosition()) next_pos = next_pos.NextStart(); | |
| 2920 auto pos = range->NextUsePositionRegisterIsBeneficial(next_pos); | |
| 2921 if (pos == nullptr) { | |
| 2922 auto spill_range = | |
| 2923 range->TopLevel()->HasSpillRange() | |
| 2924 ? range->TopLevel()->GetSpillRange() | |
| 2925 : data()->AssignSpillRangeToLiveRange(range->TopLevel()); | |
| 2926 bool merged = first_op_spill->TryMerge(spill_range); | |
| 2927 CHECK(merged); | |
| 2928 Spill(range); | |
| 2929 return true; | |
| 2930 } else if (pos->pos() > range->Start().NextStart()) { | |
| 2931 auto spill_range = | |
| 2932 range->TopLevel()->HasSpillRange() | |
| 2933 ? range->TopLevel()->GetSpillRange() | |
| 2934 : data()->AssignSpillRangeToLiveRange(range->TopLevel()); | |
| 2935 bool merged = first_op_spill->TryMerge(spill_range); | |
| 2936 CHECK(merged); | |
| 2937 Enqueue( | |
| 2938 SpillBetweenUntil(range, range->Start(), range->Start(), pos->pos())); | |
| 2939 return true; | |
| 2940 } | |
| 2941 return false; | |
| 2942 } | |
| 2943 | |
| 2944 | |
| 2945 OperandAssigner::OperandAssigner(RegisterAllocationData* data) : data_(data) {} | 3508 OperandAssigner::OperandAssigner(RegisterAllocationData* data) : data_(data) {} |
| 2946 | 3509 |
| 2947 | 3510 |
| 2948 void OperandAssigner::AssignSpillSlots() { | 3511 void OperandAssigner::AssignSpillSlots() { |
| 2949 auto& spill_ranges = data()->spill_ranges(); | 3512 auto& spill_ranges = data()->spill_ranges(); |
| 2950 // Merge disjoint spill ranges | 3513 // Merge disjoint spill ranges |
| 2951 for (size_t i = 0; i < spill_ranges.size(); i++) { | 3514 for (size_t i = 0; i < spill_ranges.size(); i++) { |
| 2952 auto range = spill_ranges[i]; | 3515 auto range = spill_ranges[i]; |
| 2953 if (range->IsEmpty()) continue; | 3516 if (range->IsEmpty()) continue; |
| 2954 for (size_t j = i + 1; j < spill_ranges.size(); j++) { | 3517 for (size_t j = i + 1; j < spill_ranges.size(); j++) { |
| (...skipping 415 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 3370 bool done = it == delayed_insertion_map.end(); | 3933 bool done = it == delayed_insertion_map.end(); |
| 3371 if (done || it->first.first != moves) { | 3934 if (done || it->first.first != moves) { |
| 3372 // Commit the MoveOperands for current ParallelMove. | 3935 // Commit the MoveOperands for current ParallelMove. |
| 3373 for (auto move : to_eliminate) { | 3936 for (auto move : to_eliminate) { |
| 3374 move->Eliminate(); | 3937 move->Eliminate(); |
| 3375 } | 3938 } |
| 3376 for (auto move : to_insert) { | 3939 for (auto move : to_insert) { |
| 3377 moves->push_back(move); | 3940 moves->push_back(move); |
| 3378 } | 3941 } |
| 3379 if (done) break; | 3942 if (done) break; |
| 3380 // Reset state. | |
| 3381 to_eliminate.clear(); | 3943 to_eliminate.clear(); |
| 3382 to_insert.clear(); | 3944 to_insert.clear(); |
| 3383 moves = it->first.first; | 3945 moves = it->first.first; |
| 3384 } | 3946 } |
| 3385 // Gather all MoveOperands for a single ParallelMove. | 3947 // Gather all MoveOperands for a single ParallelMove. |
| 3386 auto move = new (code_zone()) MoveOperands(it->first.second, it->second); | 3948 auto move = new (code_zone()) MoveOperands(it->first.second, it->second); |
| 3387 auto eliminate = moves->PrepareInsertAfter(move); | 3949 auto eliminate = moves->PrepareInsertAfter(move); |
| 3388 to_insert.push_back(move); | 3950 to_insert.push_back(move); |
| 3389 if (eliminate != nullptr) to_eliminate.push_back(eliminate); | 3951 if (eliminate != nullptr) to_eliminate.push_back(eliminate); |
| 3390 } | 3952 } |
| 3391 } | 3953 } |
| 3392 | 3954 |
| 3393 | 3955 |
| 3394 } // namespace compiler | 3956 } // namespace compiler |
| 3395 } // namespace internal | 3957 } // namespace internal |
| 3396 } // namespace v8 | 3958 } // namespace v8 |
| OLD | NEW |