Skip to content

Commit 9f5217d

Browse files
fishythefishCommit Queue
authored andcommitted
[dart2js] Refactor side effects to use EnumSetDomain.
Change-Id: I8233aefd76c1e5b5016571ccaa10b0be4ce25654 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/395063 Reviewed-by: Stephen Adams <[email protected]>
1 parent 0bfa0b0 commit 9f5217d

File tree

5 files changed

+77
-78
lines changed

5 files changed

+77
-78
lines changed

pkg/compiler/lib/src/ssa/optimize.dart

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,9 @@ import '../native/behavior.dart';
3333
import '../options.dart';
3434
import '../universe/call_structure.dart';
3535
import '../universe/selector.dart' show Selector;
36-
import '../universe/side_effects.dart' show SideEffects, SideEffectsFlags;
36+
import '../universe/side_effects.dart' show SideEffects;
3737
import '../universe/use.dart' show StaticUse;
38+
import '../util/bitset.dart';
3839
import '../util/util.dart';
3940
import 'interceptor_simplifier.dart';
4041
import 'interceptor_finalizer.dart';
@@ -3368,8 +3369,8 @@ class SsaGlobalValueNumberer implements OptimizationPhase {
33683369
final String name = "SsaGlobalValueNumberer";
33693370
final Set<int> visited = {};
33703371

3371-
late final List<SideEffectsFlags> blockChangesFlags;
3372-
late final List<SideEffectsFlags> loopChangesFlags;
3372+
late final List<Bitset> blockChangesFlags;
3373+
late final List<Bitset> loopChangesFlags;
33733374

33743375
SsaGlobalValueNumberer(this._abstractValueDomain);
33753376

@@ -3406,8 +3407,8 @@ class SsaGlobalValueNumberer implements OptimizationPhase {
34063407
}
34073408
}
34083409

3409-
void moveLoopInvariantCodeFromBlock(HBasicBlock block, HBasicBlock loopHeader,
3410-
SideEffectsFlags changesFlags) {
3410+
void moveLoopInvariantCodeFromBlock(
3411+
HBasicBlock block, HBasicBlock loopHeader, Bitset changesFlags) {
34113412
assert(block.parentLoopHeader == loopHeader || block == loopHeader);
34123413
HBasicBlock preheader = loopHeader.predecessors[0];
34133414
var dependsFlags = SideEffects.computeDependsOnFlags(changesFlags);
@@ -3506,7 +3507,7 @@ class SsaGlobalValueNumberer implements OptimizationPhase {
35063507
if (!successorValues.isEmpty && block.id + 1 < dominated.id) {
35073508
visited.clear();
35083509
List<HBasicBlock> workQueue = <HBasicBlock>[dominated];
3509-
var changesFlags = SideEffectsFlags.empty();
3510+
var changesFlags = Bitset.empty();
35103511
do {
35113512
HBasicBlock current = workQueue.removeLast();
35123513
changesFlags = changesFlags.union(
@@ -3523,10 +3524,8 @@ class SsaGlobalValueNumberer implements OptimizationPhase {
35233524
// loop changes flags list to zero so we can use bitwise-or when
35243525
// propagating loop changes upwards.
35253526
final int length = graph.blocks.length;
3526-
blockChangesFlags =
3527-
List<SideEffectsFlags>.filled(length, SideEffects.allChanges);
3528-
loopChangesFlags =
3529-
List<SideEffectsFlags>.filled(length, SideEffectsFlags.empty());
3527+
blockChangesFlags = List<Bitset>.filled(length, SideEffects.allChanges);
3528+
loopChangesFlags = List<Bitset>.filled(length, Bitset.empty());
35303529

35313530
// Run through all the basic blocks in the graph and fill in the
35323531
// changes flags lists.
@@ -3535,7 +3534,7 @@ class SsaGlobalValueNumberer implements OptimizationPhase {
35353534
final int id = block.id;
35363535

35373536
// Compute block changes flags for the block.
3538-
var changesFlags = SideEffectsFlags.empty();
3537+
var changesFlags = Bitset.empty();
35393538
HInstruction? instruction = block.first;
35403539
while (instruction != null) {
35413540
changesFlags =
@@ -3561,9 +3560,9 @@ class SsaGlobalValueNumberer implements OptimizationPhase {
35613560
}
35623561
}
35633562

3564-
SideEffectsFlags getChangesFlagsForDominatedBlock(HBasicBlock dominator,
3563+
Bitset getChangesFlagsForDominatedBlock(HBasicBlock dominator,
35653564
HBasicBlock dominated, List<HBasicBlock> workQueue) {
3566-
var changesFlags = SideEffectsFlags.empty();
3565+
var changesFlags = Bitset.empty();
35673566
List<HBasicBlock> predecessors = dominated.predecessors;
35683567
for (int i = 0, length = predecessors.length; i < length; i++) {
35693568
HBasicBlock block = predecessors[i];
@@ -3666,7 +3665,7 @@ class SsaCodeMotion extends HBaseVisitor<void> implements OptimizationPhase {
36663665
// which instructions can be moved to a dominator block.
36673666
ValueSet set_ = values[block.id];
36683667
HInstruction? instruction = block.first;
3669-
var flags = SideEffectsFlags.empty();
3668+
var flags = Bitset.empty();
36703669
while (instruction != null) {
36713670
final dependsFlags = SideEffects.computeDependsOnFlags(flags);
36723671
flags = flags.union(instruction.sideEffects.getChangesFlags());

pkg/compiler/lib/src/ssa/value_set.dart

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
44

5-
import '../universe/side_effects.dart' show SideEffects, SideEffectsFlag;
6-
import '../util/enumset.dart';
5+
import '../universe/side_effects.dart' show SideEffects;
6+
import '../util/bitset.dart';
77
import 'nodes.dart';
88

99
class ValueSet {
@@ -50,8 +50,8 @@ class ValueSet {
5050
return null;
5151
}
5252

53-
void kill(EnumSet<SideEffectsFlag> flags) {
54-
if (flags == 0) return;
53+
void kill(Bitset flags) {
54+
if (flags.isEmpty) return;
5555
final depends = SideEffects.computeDependsOnFlags(flags);
5656
// Kill in the hash table.
5757
for (int index = 0, length = table.length; index < length; index++) {

pkg/compiler/lib/src/universe/side_effects.dart

Lines changed: 56 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -6,39 +6,28 @@ library universe.side_effects;
66

77
import '../elements/entities.dart';
88
import '../serialization/serialization.dart';
9+
import '../util/bitset.dart';
910
import '../util/enumset.dart';
1011

11-
typedef SideEffectsFlags = EnumSet<SideEffectsFlag>;
12-
1312
enum SideEffectsFlag {
14-
// Changes flags.
15-
changesIndex,
16-
changesInstanceProperty,
17-
changesStaticProperty,
18-
19-
// Depends flags (one for each changes flag).
20-
dependsOnIndexStore,
21-
dependsOnInstancePropertyStore,
22-
dependsOnStaticPropertyStore,
23-
;
24-
25-
static final int _changesCount = dependsOnIndexStore.index;
26-
static final int _dependsCount = values.length - _changesCount;
13+
index_,
14+
instanceProperty,
15+
staticProperty,
2716
}
2817

18+
final _changes = EnumSetDomain<SideEffectsFlag>(0);
19+
final _depends = EnumSetDomain<SideEffectsFlag>(SideEffectsFlag.values.length);
20+
2921
class SideEffects {
3022
/// Tag used for identifying serialized [SideEffects] objects in a debugging
3123
/// data stream.
3224
static const String tag = 'side-effects';
3325

34-
EnumSet<SideEffectsFlag> _flags = EnumSet.empty();
26+
Bitset _flags = Bitset.empty();
3527

36-
static final EnumSet<SideEffectsFlag> allChanges =
37-
EnumSet.fromRawBits((1 << SideEffectsFlag._changesCount) - 1);
28+
static final Bitset allChanges = _changes.allValues(SideEffectsFlag.values);
3829

39-
static final EnumSet<SideEffectsFlag> allDepends = EnumSet.fromRawBits(
40-
((1 << SideEffectsFlag._dependsCount) - 1) <<
41-
SideEffectsFlag._changesCount);
30+
static final Bitset allDepends = _depends.allValues(SideEffectsFlag.values);
4231

4332
SideEffects() {
4433
setAllSideEffects();
@@ -50,20 +39,20 @@ class SideEffects {
5039
clearAllSideEffects();
5140
}
5241

53-
SideEffects.fromFlags(this._flags);
42+
SideEffects._fromBits(int bits) : _flags = Bitset(bits);
5443

5544
/// Deserializes a [SideEffects] object from [source].
5645
factory SideEffects.readFromDataSource(DataSourceReader source) {
5746
source.begin(tag);
58-
int flags = source.readInt();
47+
int bits = source.readInt();
5948
source.end(tag);
60-
return SideEffects.fromFlags(EnumSet.fromRawBits(flags));
49+
return SideEffects._fromBits(bits);
6150
}
6251

6352
/// Serializes this [SideEffects] to [sink].
6453
void writeToDataSink(DataSinkWriter sink) {
6554
sink.begin(tag);
66-
sink.writeInt(_flags.mask.bits);
55+
sink.writeInt(_flags.bits);
6756
sink.end(tag);
6857
}
6958

@@ -74,24 +63,37 @@ class SideEffects {
7463
@override
7564
int get hashCode => throw UnsupportedError('SideEffects.hashCode');
7665

77-
bool _getFlag(SideEffectsFlag flag) => _flags.contains(flag);
66+
bool _getChangesFlag(SideEffectsFlag flag) => _changes.contains(_flags, flag);
67+
68+
bool _getDependsFlag(SideEffectsFlag flag) => _depends.contains(_flags, flag);
69+
70+
bool _setChangesFlag(SideEffectsFlag flag) {
71+
final before = _flags;
72+
_flags = _changes.add(_flags, flag);
73+
return before != _flags;
74+
}
75+
76+
bool _setDependsFlag(SideEffectsFlag flag) {
77+
final before = _flags;
78+
_flags = _depends.add(_flags, flag);
79+
return before != _flags;
80+
}
7881

79-
bool _setFlag(SideEffectsFlag flag) {
82+
bool _clearChangesFlag(SideEffectsFlag flag) {
8083
final before = _flags;
81-
_flags += flag;
84+
_flags = _changes.remove(_flags, flag);
8285
return before != _flags;
8386
}
8487

85-
bool _clearFlag(SideEffectsFlag flag) {
88+
bool _clearDependsFlag(SideEffectsFlag flag) {
8689
final before = _flags;
87-
_flags -= flag;
90+
_flags = _depends.remove(_flags, flag);
8891
return before != _flags;
8992
}
9093

91-
EnumSet<SideEffectsFlag> getChangesFlags() => _flags.intersection(allChanges);
94+
Bitset getChangesFlags() => _flags.intersection(allChanges);
9295

93-
EnumSet<SideEffectsFlag> getDependsOnFlags() =>
94-
_flags.intersection(allDepends);
96+
Bitset getDependsOnFlags() => _flags.intersection(allDepends);
9597

9698
bool hasSideEffects() => getChangesFlags() != 0;
9799
bool dependsOnSomething() => getDependsOnFlags() != 0;
@@ -121,61 +123,57 @@ class SideEffects {
121123
}
122124

123125
bool dependsOnStaticPropertyStore() =>
124-
_getFlag(SideEffectsFlag.dependsOnStaticPropertyStore);
126+
_getDependsFlag(SideEffectsFlag.staticProperty);
125127

126128
bool setDependsOnStaticPropertyStore() =>
127-
_setFlag(SideEffectsFlag.dependsOnStaticPropertyStore);
129+
_setDependsFlag(SideEffectsFlag.staticProperty);
128130

129131
bool clearDependsOnStaticPropertyStore() =>
130-
_clearFlag(SideEffectsFlag.dependsOnStaticPropertyStore);
132+
_clearDependsFlag(SideEffectsFlag.staticProperty);
131133

132134
bool setChangesStaticProperty() =>
133-
_setFlag(SideEffectsFlag.changesStaticProperty);
135+
_setChangesFlag(SideEffectsFlag.staticProperty);
134136

135137
bool clearChangesStaticProperty() =>
136-
_clearFlag(SideEffectsFlag.changesStaticProperty);
138+
_clearChangesFlag(SideEffectsFlag.staticProperty);
137139

138140
bool changesStaticProperty() =>
139-
_getFlag(SideEffectsFlag.changesStaticProperty);
141+
_getChangesFlag(SideEffectsFlag.staticProperty);
140142

141-
bool dependsOnIndexStore() => _getFlag(SideEffectsFlag.dependsOnIndexStore);
143+
bool dependsOnIndexStore() => _getDependsFlag(SideEffectsFlag.index_);
142144

143-
bool setDependsOnIndexStore() =>
144-
_setFlag(SideEffectsFlag.dependsOnIndexStore);
145+
bool setDependsOnIndexStore() => _setDependsFlag(SideEffectsFlag.index_);
145146

146-
bool clearDependsOnIndexStore() =>
147-
_clearFlag(SideEffectsFlag.dependsOnIndexStore);
147+
bool clearDependsOnIndexStore() => _clearDependsFlag(SideEffectsFlag.index_);
148148

149-
bool setChangesIndex() => _setFlag(SideEffectsFlag.changesIndex);
149+
bool setChangesIndex() => _setChangesFlag(SideEffectsFlag.index_);
150150

151-
bool clearChangesIndex() => _clearFlag(SideEffectsFlag.changesIndex);
151+
bool clearChangesIndex() => _clearChangesFlag(SideEffectsFlag.index_);
152152

153-
bool changesIndex() => _getFlag(SideEffectsFlag.changesIndex);
153+
bool changesIndex() => _getChangesFlag(SideEffectsFlag.index_);
154154

155155
bool dependsOnInstancePropertyStore() =>
156-
_getFlag(SideEffectsFlag.dependsOnInstancePropertyStore);
156+
_getDependsFlag(SideEffectsFlag.instanceProperty);
157157

158158
bool setDependsOnInstancePropertyStore() =>
159-
_setFlag(SideEffectsFlag.dependsOnInstancePropertyStore);
159+
_setDependsFlag(SideEffectsFlag.instanceProperty);
160160

161161
bool clearDependsOnInstancePropertyStore() =>
162-
_setFlag(SideEffectsFlag.dependsOnInstancePropertyStore);
162+
_setDependsFlag(SideEffectsFlag.instanceProperty);
163163

164164
bool setChangesInstanceProperty() =>
165-
_setFlag(SideEffectsFlag.changesInstanceProperty);
165+
_setChangesFlag(SideEffectsFlag.instanceProperty);
166166

167167
bool clearChangesInstanceProperty() =>
168-
_clearFlag(SideEffectsFlag.changesInstanceProperty);
168+
_clearChangesFlag(SideEffectsFlag.instanceProperty);
169169

170170
bool changesInstanceProperty() =>
171-
_getFlag(SideEffectsFlag.changesInstanceProperty);
171+
_getChangesFlag(SideEffectsFlag.instanceProperty);
172172

173-
static EnumSet<SideEffectsFlag> computeDependsOnFlags(
174-
EnumSet<SideEffectsFlag> flags) =>
175-
EnumSet.fromRawBits(flags.mask.bits << SideEffectsFlag._changesCount);
173+
static Bitset computeDependsOnFlags(Bitset flags) =>
174+
Bitset(flags.bits << SideEffectsFlag.values.length);
176175

177-
bool dependsOn(EnumSet<SideEffectsFlag> dependsFlags) =>
178-
_flags.intersects(dependsFlags);
176+
bool dependsOn(Bitset dependsFlags) => _flags.intersects(dependsFlags);
179177

180178
bool add(SideEffects other) {
181179
final before = _flags;

pkg/compiler/lib/src/util/bitset.dart

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
import 'package:meta/meta.dart';
66

77
extension type const Bitset(int bits) {
8+
const Bitset.empty() : this(0);
9+
810
@useResult
911
Bitset intersection(Bitset other) => Bitset(bits & other.bits);
1012

pkg/compiler/lib/src/util/enumset.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ extension type const EnumSet<E extends Enum>(Bitset mask) {
3535

3636
/// Creates a set containing [values].
3737
EnumSet.fromValues(Iterable<E> values)
38-
: this(values.fold(Bitset(0), (acc, e) => acc.union(e.mask(0))));
38+
: this(values.fold(Bitset.empty(), (acc, e) => acc.union(e.mask(0))));
3939

4040
/// Returns a set containing all enum values in [this] as well as [enumValue].
4141
@useResult
@@ -149,7 +149,7 @@ class EnumSetDomain<E extends Enum> {
149149

150150
/// Returns a bitset containing [values].
151151
Bitset fromValues(Iterable<E> values) =>
152-
values.fold(Bitset(0), (acc, e) => acc.union(e.mask(offset)));
152+
values.fold(Bitset.empty(), (acc, e) => acc.union(e.mask(offset)));
153153

154154
/// Returns a bitset containing all enum values in [bits] as well as
155155
/// [enumValue].

0 commit comments

Comments
 (0)