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