| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2016, the Dart project authors. Please see the AUTHORS file |
| 2 // for details. All rights reserved. Use of this source code is governed by a |
| 3 // BSD-style license that can be found in the LICENSE file. |
| 4 library dart2js.cps_ir.effects; |
| 5 |
| 6 import 'dart:typed_data'; |
| 7 import '../universe/side_effects.dart' show SideEffects; |
| 8 |
| 9 /// Bitmasks for tracking non-local side effects and dependencies. |
| 10 /// |
| 11 /// All effects must be attributed to an effect area. The `other` area is a |
| 12 /// catch-all for any effect that does not belong to any of the specific areas; |
| 13 /// this includes external effects such as printing to the console. |
| 14 class Effects { |
| 15 // Effect areas. |
| 16 // These are bit positions, not bit masks. They are private because it is |
| 17 // otherwise easy to mistake them for bitmasks. |
| 18 static const int _staticField = 0; |
| 19 static const int _instanceField = 1; |
| 20 static const int _indexableContent = 2; |
| 21 static const int _indexableLength = 3; |
| 22 static const int _other = 4; |
| 23 static const int numberOfEffectAreas = 5; |
| 24 |
| 25 // Bitmasks for computation that modifies state in a given area. |
| 26 static const int _changes = 1; |
| 27 static const int changesStaticField = _changes << _staticField; |
| 28 static const int changesInstanceField = _changes << _instanceField; |
| 29 static const int changesIndexableContent = _changes << _indexableContent; |
| 30 static const int changesIndexableLength = _changes << _indexableLength; |
| 31 static const int changesOther = _changes << _other; |
| 32 |
| 33 static const int changesAll = |
| 34 changesStaticField | |
| 35 changesInstanceField | |
| 36 changesIndexableContent | |
| 37 changesIndexableLength | |
| 38 changesOther; |
| 39 |
| 40 // Bitmasks for computation that depends on state in a given area. |
| 41 static const int _depends = 1 << numberOfEffectAreas; |
| 42 static const int dependsOnStaticField = _depends << _staticField; |
| 43 static const int dependsOnInstanceField = _depends << _instanceField; |
| 44 static const int dependsOnIndexableContent = _depends << _indexableContent; |
| 45 static const int dependsOnIndexableLength = _depends << _indexableLength; |
| 46 static const int dependsOnOther = _depends << _other; |
| 47 |
| 48 static const int dependsOnAll = |
| 49 dependsOnStaticField | |
| 50 dependsOnInstanceField | |
| 51 dependsOnIndexableContent | |
| 52 dependsOnIndexableLength | |
| 53 dependsOnOther; |
| 54 |
| 55 static const int all = changesAll | dependsOnAll; |
| 56 static const int none = 0; |
| 57 |
| 58 static int _changesArea(int effectArea) => _changes << effectArea; |
| 59 |
| 60 static int _dependsOnArea(int effectArea) => _depends << effectArea; |
| 61 |
| 62 static int changesToDepends(int changesFlags) { |
| 63 return (changesFlags & changesAll) << numberOfEffectAreas; |
| 64 } |
| 65 |
| 66 static int dependsToChanges(int dependsFlags) { |
| 67 return (dependsFlags & dependsOnAll) >> numberOfEffectAreas; |
| 68 } |
| 69 |
| 70 /// Converts [SideEffects] from a JS annotation or type inference to the |
| 71 /// more fine-grained flag set. |
| 72 // |
| 73 // TODO(asgerf): Once we finalize the set of flags to use, unify the two |
| 74 // systems. |
| 75 static int from(SideEffects fx) { |
| 76 int result = 0; |
| 77 if (fx.changesInstanceProperty()) { |
| 78 result |= changesInstanceField; |
| 79 } |
| 80 if (fx.changesStaticProperty()) { |
| 81 result |= changesStaticField; |
| 82 } |
| 83 if (fx.changesIndex()) { |
| 84 result |= changesIndexableContent | changesIndexableLength; |
| 85 } |
| 86 if (fx.hasSideEffects()) { |
| 87 result |= changesOther; |
| 88 } |
| 89 if (fx.dependsOnInstancePropertyStore()) { |
| 90 result |= dependsOnInstanceField; |
| 91 } |
| 92 if (fx.dependsOnStaticPropertyStore()) { |
| 93 result |= dependsOnStaticField; |
| 94 } |
| 95 if (fx.dependsOnIndexStore()) { |
| 96 result |= dependsOnIndexableContent | dependsOnIndexableLength; |
| 97 } |
| 98 if (fx.dependsOnSomething()) { |
| 99 result |= dependsOnOther; |
| 100 } |
| 101 return result; |
| 102 } |
| 103 } |
| 104 |
| 105 /// Creates fresh IDs to ensure effect numbers do not clash with each other. |
| 106 class EffectNumberer { |
| 107 int _id = 0; |
| 108 int next() => ++_id; |
| 109 |
| 110 /// Special effect number that can be used in place for an effect area that |
| 111 /// is irrelevant for a computation. |
| 112 /// |
| 113 /// This value is never returned by [next]. |
| 114 static const int none = 0; |
| 115 } |
| 116 |
| 117 /// A mutable vector of effect numbers, one for each effect area. |
| 118 /// |
| 119 /// Effect numbers are used to identify regions of code wherein the given |
| 120 /// effect area is unmodified. |
| 121 class EffectNumbers { |
| 122 final Int32List _effectNumbers = new Int32List(Effects.numberOfEffectAreas); |
| 123 |
| 124 EffectNumbers._zero(); |
| 125 |
| 126 EffectNumbers.fresh(EffectNumberer numberer) { |
| 127 reset(numberer); |
| 128 } |
| 129 |
| 130 EffectNumbers copy() { |
| 131 return new EffectNumbers._zero().._effectNumbers.setAll(0, _effectNumbers); |
| 132 } |
| 133 |
| 134 int operator[](int i) => _effectNumbers[i]; |
| 135 |
| 136 void operator[]=(int i, int value) { |
| 137 _effectNumbers[i] = value; |
| 138 } |
| 139 |
| 140 int get staticField => _effectNumbers[Effects._staticField]; |
| 141 int get instanceField => _effectNumbers[Effects._instanceField]; |
| 142 int get indexableContent => _effectNumbers[Effects._indexableContent]; |
| 143 int get indexableLength => _effectNumbers[Effects._indexableLength]; |
| 144 int get other => _effectNumbers[Effects._other]; |
| 145 |
| 146 void set staticField(int n) { |
| 147 _effectNumbers[Effects._staticField] = n; |
| 148 } |
| 149 void set instanceField(int n) { |
| 150 _effectNumbers[Effects._instanceField] = n; |
| 151 } |
| 152 void set indexableContent(int n) { |
| 153 _effectNumbers[Effects._indexableContent] = n; |
| 154 } |
| 155 void set indexableLength(int n) { |
| 156 _effectNumbers[Effects._indexableLength] = n; |
| 157 } |
| 158 void set other(int n) { |
| 159 _effectNumbers[Effects._other] = n; |
| 160 } |
| 161 |
| 162 void reset(EffectNumberer numberer) { |
| 163 for (int i = 0; i < Effects.numberOfEffectAreas; ++i) { |
| 164 _effectNumbers[i] = numberer.next(); |
| 165 } |
| 166 } |
| 167 |
| 168 void join(EffectNumberer numberer, EffectNumbers other) { |
| 169 for (int i = 0; i < Effects.numberOfEffectAreas; ++i) { |
| 170 if (_effectNumbers[i] != other._effectNumbers[i]) { |
| 171 _effectNumbers[i] = numberer.next(); |
| 172 } |
| 173 } |
| 174 } |
| 175 |
| 176 void change(EffectNumberer numberer, int changeFlags) { |
| 177 for (int i = 0; i < Effects.numberOfEffectAreas; ++i) { |
| 178 if (changeFlags & Effects._changesArea(i) != 0) { |
| 179 _effectNumbers[i] = numberer.next(); |
| 180 } |
| 181 } |
| 182 } |
| 183 |
| 184 /// Builds a vector where all entries that are not depended on are replaced |
| 185 /// by [EffectNumberer.none]. |
| 186 // |
| 187 // TODO(asgerf): Use this in GVN to simplify the dispatching code. |
| 188 List<int> getDependencies(int dependsFlags) { |
| 189 Int32List copy = new Int32List.fromList(_effectNumbers); |
| 190 for (int i = 0; i < Effects.numberOfEffectAreas; ++i) { |
| 191 if (dependsFlags & Effects._dependsOnArea(i) == 0) { |
| 192 copy[i] = EffectNumberer.none; |
| 193 } |
| 194 } |
| 195 return copy; |
| 196 } |
| 197 } |
| OLD | NEW |