Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2015 the V8 project authors. All rights reserved. | 1 // Copyright 2015 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 #ifndef V8_COALESCED_LIVE_RANGES_H_ | 5 #ifndef V8_COALESCED_LIVE_RANGES_H_ |
| 6 #define V8_COALESCED_LIVE_RANGES_H_ | 6 #define V8_COALESCED_LIVE_RANGES_H_ |
| 7 | 7 |
| 8 #include "src/compiler/register-allocator.h" | 8 #include "src/compiler/register-allocator.h" |
| 9 #include "src/zone-containers.h" | 9 #include "src/zone-containers.h" |
| 10 | 10 |
| 11 namespace v8 { | 11 namespace v8 { |
| 12 namespace internal { | 12 namespace internal { |
| 13 namespace compiler { | 13 namespace compiler { |
| 14 | 14 |
| 15 | 15 |
| 16 class AllocationScheduler; | 16 // Storage detail for CoalescedLiveRanges. |
| 17 struct AllocatedInterval { | |
| 18 LifetimePosition start; | |
| 19 LifetimePosition end; | |
| 20 LiveRange* range; | |
| 21 bool operator<(const AllocatedInterval& other) const { | |
| 22 return start < other.start; | |
| 23 } | |
| 24 bool operator>(const AllocatedInterval& other) const { | |
| 25 return start > other.start; | |
| 26 } | |
| 27 }; | |
| 28 typedef ZoneSet<AllocatedInterval> IntervalStore; | |
| 17 | 29 |
| 18 | 30 |
| 31 // An iterator over conflicts of a live range, obtained from CoalescedLiveRanges | |
| 32 // The design supports two main scenarios (see GreedyAllocator): | |
| 33 // (1) observing each conflicting range, without mutating the allocations, and | |
| 34 // (2) observing each conflicting range, and then moving to the next, after | |
| 35 // removing the current conflict. | |
| 36 class LiveRangeConflictIterator { | |
| 37 public: | |
| 38 // Current conflict. nullptr if no conflicts, or if we reached the end of | |
| 39 // conflicts. | |
| 40 LiveRange* Current() const; | |
| 41 | |
| 42 // Get the next conflict. Caller should handle non-consecutive repetitions of | |
| 43 // the same range. | |
| 44 LiveRange* GetNext() { return InternalGetNext(false); } | |
| 45 | |
| 46 // Get the next conflict, after evicting the current one. Caller may expect | |
| 47 // to never observe the same live range more than once. | |
| 48 LiveRange* ClearCurrentAndGetNext() { return InternalGetNext(true); } | |
|
Jarin
2015/07/21 08:51:18
ClearCurrent seems ambiguous to me, it sounds like
Mircea Trofin
2015/07/21 14:51:01
Done.
| |
| 49 | |
| 50 private: | |
| 51 friend class CoalescedLiveRanges; | |
| 52 | |
| 53 typedef IntervalStore::const_iterator interval_iterator; | |
| 54 LiveRangeConflictIterator(const LiveRange* range, IntervalStore* store); | |
| 55 | |
| 56 // Move the store iterator to first interval intersecting query. Since the | |
| 57 // intervals are sorted, subsequent intervals intersecting query follow. May | |
| 58 // leave the store iterator at "end", meaning that the current query does not | |
| 59 // have an intersection. | |
| 60 void MovePosToFirstConflictForQuery(); | |
| 61 | |
| 62 // Move both query and store iterator to the first intersection, if any. If | |
| 63 // none, then it invalidates the iterator (IsFinished() == true) | |
| 64 void MovePosAndQueryToFirstConflict(); | |
| 65 | |
| 66 // Increment pos and skip over intervals belonging to the same range we | |
| 67 // started with (i.e. Current() before the call). It is possible that range | |
| 68 // will be seen again, but not consecutively. | |
| 69 void IncrementPosAndSkipOverRepetitions(); | |
| 70 | |
| 71 // Common implementation used by both GetNext as well as | |
| 72 // ClearCurrentAndGetNext. | |
| 73 LiveRange* InternalGetNext(bool clean_behind); | |
| 74 | |
| 75 bool IsFinished() const { return query_ == nullptr; } | |
| 76 | |
| 77 static AllocatedInterval AsAllocatedInterval(LifetimePosition pos) { | |
| 78 return {pos, LifetimePosition::Invalid(), nullptr}; | |
| 79 } | |
| 80 | |
| 81 // Intersection utilities. | |
| 82 static bool Intersects(LifetimePosition a_start, LifetimePosition a_end, | |
| 83 LifetimePosition b_start, LifetimePosition b_end) { | |
| 84 return a_start < b_end && b_start < a_end; | |
| 85 } | |
| 86 | |
| 87 bool QueryIntersectsAllocatedInterval() const { | |
| 88 DCHECK(query_ != nullptr); | |
| 89 return pos_ != storage_->end() && | |
| 90 Intersects(query_->start(), query_->end(), pos_->start, pos_->end); | |
| 91 } | |
| 92 | |
| 93 void Invalidate() { | |
| 94 query_ = nullptr; | |
| 95 pos_ = storage_->end(); | |
| 96 } | |
| 97 | |
| 98 const UseInterval* query_; | |
| 99 interval_iterator pos_; | |
| 100 IntervalStore* storage_; | |
|
Jarin
2015/07/21 08:51:18
Could we please move away from the 'storage_' name
Mircea Trofin
2015/07/21 14:51:01
Done.
| |
| 101 }; | |
| 102 | |
| 19 // Collection of live ranges allocated to the same register. | 103 // Collection of live ranges allocated to the same register. |
| 20 // It supports efficiently finding all conflicts for a given, non-allocated | 104 // It supports efficiently finding all conflicts for a given, non-allocated |
| 21 // range. See AllocatedInterval. | 105 // range. See AllocatedInterval. |
| 22 // Allocated live ranges do not intersect. At most, individual use intervals | 106 // Allocated live ranges do not intersect. At most, individual use intervals |
| 23 // touch. We store, for a live range, an AllocatedInterval corresponding to each | 107 // touch. We store, for a live range, an AllocatedInterval corresponding to each |
| 24 // of that range's UseIntervals. We keep the list of AllocatedIntervals sorted | 108 // of that range's UseIntervals. We keep the list of AllocatedIntervals sorted |
| 25 // by starts. Then, given the non-intersecting property, we know that | 109 // by starts. Then, given the non-intersecting property, we know that |
| 26 // consecutive AllocatedIntervals have the property that the "smaller"'s end is | 110 // consecutive AllocatedIntervals have the property that the "smaller"'s end is |
| 27 // less or equal to the "larger"'s start. | 111 // less or equal to the "larger"'s start. |
| 28 // This allows for quick (logarithmic complexity) identification of the first | 112 // This allows for quick (logarithmic complexity) identification of the first |
| 29 // AllocatedInterval to conflict with a given LiveRange, and then for efficient | 113 // AllocatedInterval to conflict with a given LiveRange, and then for efficient |
| 30 // traversal of conflicts. | 114 // traversal of conflicts. |
| 31 class CoalescedLiveRanges : public ZoneObject { | 115 class CoalescedLiveRanges : public ZoneObject { |
| 32 public: | 116 public: |
| 33 explicit CoalescedLiveRanges(Zone* zone) : storage_(zone) {} | 117 explicit CoalescedLiveRanges(Zone* zone) : storage_(zone) {} |
| 34 void clear() { storage_.clear(); } | 118 void clear() { storage_.clear(); } |
| 35 | 119 |
| 36 bool empty() const { return storage_.empty(); } | 120 bool empty() const { return storage_.empty(); } |
| 37 | 121 |
| 38 // Returns kInvalidWeight if there are no conflicts, or the largest weight of | 122 // Iterate over each live range conflicting with the provided one. |
| 39 // a range conflicting with the given range. | 123 // The same live range may be observed multiple, but non-consecutive times. |
| 40 float GetMaximumConflictingWeight(const LiveRange* range) const; | 124 LiveRangeConflictIterator GetConflicts(const LiveRange* range); |
| 41 | 125 |
| 42 // Evicts all conflicts of the given range, and reschedules them with the | |
| 43 // provided scheduler. | |
| 44 void EvictAndRescheduleConflicts(LiveRange* range, | |
| 45 AllocationScheduler* scheduler); | |
| 46 | 126 |
| 47 // Allocates a range with a pre-calculated candidate weight. | 127 // Allocates a range with a pre-calculated candidate weight. |
| 48 void AllocateRange(LiveRange* range); | 128 void AllocateRange(LiveRange* range); |
| 49 | 129 |
| 50 // TODO(mtrofin): remove this in favor of comprehensive unit tests. | 130 // Unit testing API, verifying that allocated intervals do not overlap. |
| 51 bool VerifyAllocationsAreValid() const; | 131 bool TestOnlyVerifyAllocationsAreValid() const; |
|
Jarin
2015/07/21 08:51:18
TestOnlyVerifyAllocationsAreValid -> VerifyAllocat
Mircea Trofin
2015/07/21 14:51:01
Ah, it was a suffix. Done.
| |
| 52 | 132 |
| 53 private: | 133 private: |
| 54 static const float kAllocatedRangeMultiplier; | 134 static const float kAllocatedRangeMultiplier; |
| 55 // Storage detail for CoalescedLiveRanges. | |
| 56 struct AllocatedInterval { | |
| 57 LifetimePosition start; | |
| 58 LifetimePosition end; | |
| 59 LiveRange* range; | |
| 60 bool operator<(const AllocatedInterval& other) const { | |
| 61 return start < other.start; | |
| 62 } | |
| 63 bool operator>(const AllocatedInterval& other) const { | |
| 64 return start > other.start; | |
| 65 } | |
| 66 }; | |
| 67 typedef ZoneSet<AllocatedInterval> IntervalStore; | |
| 68 typedef IntervalStore::const_iterator interval_iterator; | |
| 69 | 135 |
| 70 IntervalStore& storage() { return storage_; } | 136 IntervalStore& storage() { return storage_; } |
|
Jarin
2015/07/21 08:51:18
Again, could we rename storage -> intervals.
Mircea Trofin
2015/07/21 14:51:01
Done.
| |
| 71 const IntervalStore& storage() const { return storage_; } | 137 const IntervalStore& storage() const { return storage_; } |
| 72 | 138 |
| 73 // Augment the weight of a range that is about to be allocated. | 139 // Augment the weight of a range that is about to be allocated. |
| 74 static void UpdateWeightAtAllocation(LiveRange* range); | 140 static void UpdateWeightAtAllocation(LiveRange* range); |
| 75 | 141 |
| 76 // Reduce the weight of a range that has lost allocation. | 142 // Reduce the weight of a range that has lost allocation. |
| 77 static void UpdateWeightAtEviction(LiveRange* range); | 143 static void UpdateWeightAtEviction(LiveRange* range); |
| 78 | 144 |
| 79 // Intersection utilities. | |
| 80 static bool Intersects(LifetimePosition a_start, LifetimePosition a_end, | |
| 81 LifetimePosition b_start, LifetimePosition b_end) { | |
| 82 return a_start < b_end && b_start < a_end; | |
| 83 } | |
| 84 static AllocatedInterval AsAllocatedInterval(LifetimePosition pos) { | |
| 85 return {pos, LifetimePosition::Invalid(), nullptr}; | |
| 86 } | |
| 87 | |
| 88 bool QueryIntersectsAllocatedInterval(const UseInterval* query, | |
| 89 interval_iterator& pos) const { | |
| 90 DCHECK(query != nullptr); | |
| 91 return pos != storage().end() && | |
| 92 Intersects(query->start(), query->end(), pos->start, pos->end); | |
| 93 } | |
| 94 | |
| 95 void Remove(LiveRange* range); | |
| 96 | |
| 97 // Get the first interval intersecting query. Since the intervals are sorted, | |
| 98 // subsequent intervals intersecting query follow. | |
| 99 interval_iterator GetFirstConflict(const UseInterval* query) const; | |
| 100 | 145 |
| 101 IntervalStore storage_; | 146 IntervalStore storage_; |
| 102 DISALLOW_COPY_AND_ASSIGN(CoalescedLiveRanges); | 147 DISALLOW_COPY_AND_ASSIGN(CoalescedLiveRanges); |
| 103 }; | 148 }; |
| 104 | 149 |
| 105 | 150 |
| 106 } // namespace compiler | 151 } // namespace compiler |
| 107 } // namespace internal | 152 } // namespace internal |
| 108 } // namespace v8 | 153 } // namespace v8 |
| 109 #endif // V8_COALESCED_LIVE_RANGES_H_ | 154 #endif // V8_COALESCED_LIVE_RANGES_H_ |
| OLD | NEW |