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; | |
17 | |
18 | |
19 // Collection of live ranges allocated to the same register. | 16 // Collection of live ranges allocated to the same register. |
20 // It supports efficiently finding all conflicts for a given, non-allocated | 17 // It supports efficiently finding all conflicts for a given, non-allocated |
21 // range. See AllocatedInterval. | 18 // range. See AllocatedInterval. |
22 // Allocated live ranges do not intersect. At most, individual use intervals | 19 // Allocated live ranges do not intersect. At most, individual use intervals |
23 // touch. We store, for a live range, an AllocatedInterval corresponding to each | 20 // 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 | 21 // of that range's UseIntervals. We keep the list of AllocatedIntervals sorted |
25 // by starts. Then, given the non-intersecting property, we know that | 22 // by starts. Then, given the non-intersecting property, we know that |
26 // consecutive AllocatedIntervals have the property that the "smaller"'s end is | 23 // consecutive AllocatedIntervals have the property that the "smaller"'s end is |
27 // less or equal to the "larger"'s start. | 24 // less or equal to the "larger"'s start. |
28 // This allows for quick (logarithmic complexity) identification of the first | 25 // This allows for quick (logarithmic complexity) identification of the first |
29 // AllocatedInterval to conflict with a given LiveRange, and then for efficient | 26 // AllocatedInterval to conflict with a given LiveRange, and then for efficient |
30 // traversal of conflicts. | 27 // traversal of conflicts. |
31 class CoalescedLiveRanges : public ZoneObject { | 28 class CoalescedLiveRanges : public ZoneObject { |
32 public: | 29 public: |
33 explicit CoalescedLiveRanges(Zone* zone) : storage_(zone) {} | 30 explicit CoalescedLiveRanges(Zone* zone) : storage_(zone) {} |
34 void clear() { storage_.clear(); } | 31 void clear() { storage_.clear(); } |
35 | 32 |
36 bool empty() const { return storage_.empty(); } | 33 bool empty() const { return storage_.empty(); } |
37 | 34 |
38 // Returns kInvalidWeight if there are no conflicts, or the largest weight of | 35 // Visits each live range conflicting with the provided one. The same live |
39 // a range conflicting with the given range. | 36 // range may be passed to the visitor multiple, but non-consecutive times. |
40 float GetMaximumConflictingWeight(const LiveRange* range) const; | 37 template <typename ConstLiveRangePtrToBool> |
38 void VisitConflicts(const LiveRange* range, ConstLiveRangePtrToBool visitor) { | |
Benedikt Meurer
2015/07/14 05:13:04
Higher order functions which iterate over a data s
Mircea Trofin
2015/07/14 15:21:07
I called this out in the code review description:
Mircea Trofin
2015/07/15 05:18:46
(to capture our discussion) We agreed to move to a
| |
39 auto end = storage().end(); | |
40 for (auto query = range->first_interval(); query != nullptr; | |
41 query = query->next()) { | |
42 auto conflict = GetFirstConflict(query); | |
41 | 43 |
42 // Evicts all conflicts of the given range, and reschedules them with the | 44 if (conflict == end) continue; |
43 // provided scheduler. | 45 while (QueryIntersectsAllocatedInterval(query, conflict)) { |
44 void EvictAndRescheduleConflicts(LiveRange* range, | 46 LiveRange* range = conflict->range; |
45 AllocationScheduler* scheduler); | 47 // Bypass successive intervals belonging to the same range. |
48 while (conflict != end && conflict->range == range) { | |
49 ++conflict; | |
50 } | |
51 bool may_continue = visitor(range); | |
52 if (!may_continue) break; | |
53 } | |
54 } | |
55 } | |
56 | |
57 | |
58 // Evicts all conflicts of the given range, calling the provided function | |
59 // for each found conflict. | |
60 template <typename OnConflictRemoveVisitor> | |
61 void RemoveConflicts(const LiveRange* range, | |
62 OnConflictRemoveVisitor on_conflict_found) { | |
63 VisitConflicts(range, [this, on_conflict_found](LiveRange* conflict) { | |
64 Remove(conflict); | |
65 on_conflict_found(conflict); | |
66 return true; | |
67 }); | |
68 } | |
69 | |
46 | 70 |
47 // Allocates a range with a pre-calculated candidate weight. | 71 // Allocates a range with a pre-calculated candidate weight. |
48 void AllocateRange(LiveRange* range); | 72 void AllocateRange(LiveRange* range); |
49 | 73 |
50 // TODO(mtrofin): remove this in favor of comprehensive unit tests. | 74 // Unit testing API, verifying that allocated intervals do not overlap. |
51 bool VerifyAllocationsAreValid() const; | 75 bool TestOnlyVerifyAllocationsAreValid() const; |
52 | 76 |
53 private: | 77 private: |
54 static const float kAllocatedRangeMultiplier; | 78 static const float kAllocatedRangeMultiplier; |
55 // Storage detail for CoalescedLiveRanges. | 79 // Storage detail for CoalescedLiveRanges. |
56 struct AllocatedInterval { | 80 struct AllocatedInterval { |
57 LifetimePosition start; | 81 LifetimePosition start; |
58 LifetimePosition end; | 82 LifetimePosition end; |
59 LiveRange* range; | 83 LiveRange* range; |
60 bool operator<(const AllocatedInterval& other) const { | 84 bool operator<(const AllocatedInterval& other) const { |
61 return start < other.start; | 85 return start < other.start; |
(...skipping 16 matching lines...) Expand all Loading... | |
78 | 102 |
79 // Intersection utilities. | 103 // Intersection utilities. |
80 static bool Intersects(LifetimePosition a_start, LifetimePosition a_end, | 104 static bool Intersects(LifetimePosition a_start, LifetimePosition a_end, |
81 LifetimePosition b_start, LifetimePosition b_end) { | 105 LifetimePosition b_start, LifetimePosition b_end) { |
82 return a_start < b_end && b_start < a_end; | 106 return a_start < b_end && b_start < a_end; |
83 } | 107 } |
84 static AllocatedInterval AsAllocatedInterval(LifetimePosition pos) { | 108 static AllocatedInterval AsAllocatedInterval(LifetimePosition pos) { |
85 return {pos, LifetimePosition::Invalid(), nullptr}; | 109 return {pos, LifetimePosition::Invalid(), nullptr}; |
86 } | 110 } |
87 | 111 |
112 | |
88 bool QueryIntersectsAllocatedInterval(const UseInterval* query, | 113 bool QueryIntersectsAllocatedInterval(const UseInterval* query, |
89 interval_iterator& pos) const { | 114 interval_iterator& pos) const { |
90 DCHECK(query != nullptr); | 115 DCHECK(query != nullptr); |
91 return pos != storage().end() && | 116 return pos != storage().end() && |
92 Intersects(query->start(), query->end(), pos->start, pos->end); | 117 Intersects(query->start(), query->end(), pos->start, pos->end); |
93 } | 118 } |
94 | 119 |
95 void Remove(LiveRange* range); | 120 void Remove(LiveRange* range); |
96 | 121 |
97 // Get the first interval intersecting query. Since the intervals are sorted, | 122 // Get the first interval intersecting query. Since the intervals are sorted, |
98 // subsequent intervals intersecting query follow. | 123 // subsequent intervals intersecting query follow. |
99 interval_iterator GetFirstConflict(const UseInterval* query) const; | 124 interval_iterator GetFirstConflict(const UseInterval* query) const; |
100 | 125 |
101 IntervalStore storage_; | 126 IntervalStore storage_; |
102 DISALLOW_COPY_AND_ASSIGN(CoalescedLiveRanges); | 127 DISALLOW_COPY_AND_ASSIGN(CoalescedLiveRanges); |
103 }; | 128 }; |
104 | 129 |
105 | 130 |
106 } // namespace compiler | 131 } // namespace compiler |
107 } // namespace internal | 132 } // namespace internal |
108 } // namespace v8 | 133 } // namespace v8 |
109 #endif // V8_COALESCED_LIVE_RANGES_H_ | 134 #endif // V8_COALESCED_LIVE_RANGES_H_ |
OLD | NEW |