Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(253)

Side by Side Diff: lib/compiler/implementation/ssa/types.dart

Issue 10139012: Refactor types in ssa nodes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: More fixes. Created 8 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
2 // for details. All rights reserved. Use of this source code is governed by a 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. 3 // BSD-style license that can be found in the LICENSE file.
4 4
5 class SsaTypePropagator extends HGraphVisitor implements OptimizationPhase { 5 abstract class HType {
6 6 const HType();
7 final Map<int, HInstruction> workmap; 7
8 final List<int> worklist; 8 static final HType CONFLICTING = const HAnalysisType("conflicting");
9 final Compiler compiler; 9 static final HType UNKNOWN = const HAnalysisType("unknown");
10 String get name() => 'type propagator'; 10 static final HType BOOLEAN = const HBooleanType();
11 11 static final HType NUMBER = const HNumberType();
12 SsaTypePropagator(Compiler this.compiler) 12 static final HType INTEGER = const HIntegerType();
13 : workmap = new Map<int, HInstruction>(), 13 static final HType DOUBLE = const HDoubleType();
14 worklist = new List<int>(); 14 static final HType INDEXABLE = const HIndexableType();
15 15 static final HType STRING = const HStringType();
16 16 static final HType READABLE_ARRAY = const HReadableArrayType();
17 HType computeType(HInstruction instruction) { 17 static final HType MUTABLE_ARRAY = const HMutableArrayType();
18 return instruction.computeTypeFromInputTypes(); 18 static final HType EXTENDABLE_ARRAY = const HExtendableArrayType();
19 } 19
20 20 bool isConflicting() => this === CONFLICTING;
21 // Re-compute and update the type of the instruction. Returns 21 bool isUnknown() => this === UNKNOWN;
22 // whether or not the type was changed. 22 bool isBoolean() => false;
23 bool updateType(HInstruction instruction) { 23 bool isNumber() => false;
24 HType oldType = instruction.propagatedType; 24 bool isInteger() => false;
25 HType newType = instruction.hasGuaranteedType() 25 bool isDouble() => false;
26 ? instruction.guaranteedType 26 bool isString() => false;
27 : computeType(instruction); 27 bool isIndexable() => false;
28 // We unconditionally replace the propagated type with the new type. The 28 bool isReadableArray() => false;
29 // computeType must make sure that we eventually reach a stable state. 29 bool isMutableArray() => false;
30 instruction.propagatedType = newType; 30 bool isExtendableArray() => false;
31 return oldType !== newType; 31 bool isPrimitive() => false;
32 } 32 bool isNonPrimitive() => false;
33 33
34 void visitGraph(HGraph graph) { 34 /** A type is useful it is not unknown and not conflicting. */
35 visitDominatorTree(graph); 35 bool isUseful() => !isUnknown() && !isConflicting();
36 processWorklist(); 36 /** Alias for isReadableArray. */
37 } 37 bool isArray() => isReadableArray();
38 38
39 visitBasicBlock(HBasicBlock block) { 39 /**
40 if (block.isLoopHeader()) { 40 * The intersection of two types is the intersection of its values. For
41 block.forEachPhi((HPhi phi) { 41 * example:
42 // Set the initial type for the phi. In theory we would need to mark the 42 * * INTEGER.intersect(NUMBER) => INTEGER.
43 // type of all other incoming edges as "unitialized" and take this into 43 * * DOUBLE.intersect(INTEGER) => CONFLICTING.
44 // account when doing the propagation inside the phis. Just setting 44 * * MUTABLE_ARRAY.intersect(READABLE_ARRAY) => MUTABLE_ARRAY.
45 // the [propagatedType] is however easier. 45 *
46 phi.propagatedType = phi.inputs[0].propagatedType; 46 * When there is no predefined type to represent the intersection returns
47 addToWorkList(phi); 47 * [CONFLICTING].
48 }); 48 *
49 } else { 49 * An intersection with [UNKNOWN] returns the non-UNKNOWN type. An
50 block.forEachPhi((HPhi phi) { 50 * intersection with [CONFLICTING] returns [CONFLICTING].
51 if (updateType(phi)) addDependentInstructionsToWorkList(phi); 51 */
52 }); 52 abstract HType intersection(HType other);
53 } 53
54 54 /**
55 HInstruction instruction = block.first; 55 * The union of two types is the union of its values. For example:
56 while (instruction !== null) { 56 * * INTEGER.union(INTEGER_OR_DOUBLE) => NUMBER.
ngeoffray 2012/04/25 11:41:13 INTEGER_OR_DOUBLE -> NUMBER
floitsch 2012/04/25 19:52:15 Done.
57 if (updateType(instruction)) { 57 * * DOUBLE.union(INTEGER) => DOUBLE.
ngeoffray 2012/04/25 11:41:13 => NUMBER
floitsch 2012/04/25 19:52:15 Done.
58 addDependentInstructionsToWorkList(instruction); 58 * * MUTABLE_ARRAY.union(READABLE_ARRAY) => READABLE_ARRAY.
59 } 59 *
60 instruction = instruction.next; 60 * When there is no predefined type to represent the union returns
61 } 61 * [CONFLICTING].
62 } 62 *
63 63 * A union with [UNKNOWN] returns the non-UNKNOWN type. A union with
64 void processWorklist() { 64 * [CONFLICTING] returns [CONFLICTING].
65 while (!worklist.isEmpty()) { 65 */
66 int id = worklist.removeLast(); 66 abstract HType union(HType other);
ngeoffray 2012/04/25 11:41:13 Could you actually write unit tests for union and
floitsch 2012/04/25 19:52:15 Done.
67 HInstruction instruction = workmap[id]; 67 }
68 assert(instruction !== null); 68
69 workmap.remove(id); 69 /** Used to represent [HType.UNKNOWN] and [HType.CONFLICTING]. */
70 if (updateType(instruction)) { 70 class HAnalysisType extends HType {
71 addDependentInstructionsToWorkList(instruction); 71 final String name;
72 } 72 const HAnalysisType(this.name);
73 } 73 String toString() => name;
74 } 74
75 75 HType combine(HType other) {
76 void addDependentInstructionsToWorkList(HInstruction instruction) { 76 if (isUnknown()) return other;
77 for (int i = 0, length = instruction.usedBy.length; i < length; i++) { 77 if (other.isUnknown()) return this;
78 // The non-speculative type propagator only propagates types forward. We 78 return HType.CONFLICTING;
79 // thus only need to add the users of the [instruction] to the list. 79 }
80 addToWorkList(instruction.usedBy[i]); 80
81 } 81 HType union(HType other) => combine(other);
82 } 82 HType intersection(HType other) => combine(other);
83 83 }
84 void addToWorkList(HInstruction instruction) { 84
85 final int id = instruction.id; 85 abstract class HPrimitiveType extends HType {
86 if (!workmap.containsKey(id)) { 86 const HPrimitiveType();
87 worklist.add(id); 87 bool isPrimitive() => true;
88 workmap[id] = instruction; 88 }
89 } 89
90 } 90 class HBooleanType extends HPrimitiveType {
91 } 91 const HBooleanType();
92 92 bool isBoolean() => true;
93 class SsaSpeculativeTypePropagator extends SsaTypePropagator { 93 String toString() => "boolean";
94 final String name = 'speculative type propagator'; 94
95 SsaSpeculativeTypePropagator(Compiler compiler) : super(compiler); 95 HType combine(HType other) {
96 96 if (other.isBoolean() || other.isUnknown()) return this;
ngeoffray 2012/04/25 11:41:13 Instead of returning [this] in these methods, I wo
floitsch 2012/04/25 19:52:15 Done.
97 void addDependentInstructionsToWorkList(HInstruction instruction) { 97 return HType.CONFLICTING;
98 // The speculative type propagator propagates types forward and backward. 98 }
99 // Not only do we need to add the users of the [instruction] to the list. 99
100 // We also need to add the inputs fo the [instruction], since they might 100 // Since the boolean type is a one-element set the union and intersection are
101 // want to propagate the desired outgoing type. 101 // the same.
102 for (int i = 0, length = instruction.usedBy.length; i < length; i++) { 102 HType union(HType other) => combine(other);
103 addToWorkList(instruction.usedBy[i]); 103 HType intersection(HType other) => combine(other);
104 } 104 }
105 for (int i = 0, length = instruction.inputs.length; i < length; i++) { 105
106 addToWorkList(instruction.inputs[i]); 106 class HNumberType extends HPrimitiveType {
107 } 107 const HNumberType();
108 } 108 bool isNumber() => true;
109 109 String toString() => "number";
110 HType computeDesiredType(HInstruction instruction) { 110
111 HType desiredType = HType.UNKNOWN; 111 HType union(HType other) {
112 for (final user in instruction.usedBy) { 112 if (other.isNumber() || other.isUnknown()) return HType.NUMBER;
113 desiredType = 113 return HType.CONFLICTING;
114 desiredType.combine(user.computeDesiredTypeForInput(instruction)); 114 }
115 // No need to continue if two users disagree on the type. 115
116 if (desiredType.isConflicting()) break; 116 HType intersection(HType other) {
117 } 117 if (other.isUnknown()) return this;
118 return desiredType; 118 if (other.isNumber()) return other;
119 } 119 return HType.CONFLICTING;
120 120 }
121 HType computeType(HInstruction instruction) { 121 }
122 // Once we are in a conflicting state don't update the type anymore. 122
123 HType oldType = instruction.propagatedType; 123 class HIntegerType extends HNumberType {
124 if (oldType.isConflicting()) return oldType; 124 const HIntegerType();
125 125 bool isInteger() => true;
126 HType newType = super.computeType(instruction); 126 String toString() => "integer";
127 // [computeDesiredType] goes to all usedBys and lets them compute their 127
128 // desired type. By setting the [newType] here we give them more context to 128 HType union(HType other) {
129 // work with. 129 if (other.isInteger() || other.isUnknown()) return this;
130 instruction.propagatedType = newType; 130 return super.union(other);
131 HType desiredType = computeDesiredType(instruction); 131 }
132 // If the desired type is conflicting just return the computed type. 132
133 if (desiredType.isConflicting()) return newType; 133 HType intersection(HType other) {
134 // TODO(ngeoffray): Allow speculative optimizations on 134 if (other.isUnknown()) return this;
135 // non-primitive types? 135 if (other.isDouble()) return HType.CONFLICTING;
136 if (desiredType.isNonPrimitive()) return newType; 136 if (other.isNumber()) return this;
137 return newType.combine(desiredType); 137 return HType.CONFLICTING;
138 } 138 }
139 } 139 }
140
141 class HDoubleType extends HNumberType {
142 const HDoubleType();
143 bool isDouble() => true;
144 String toString() => "double";
145
146 HType union(HType other) {
147 if (other.isDouble() || other.isUnknown()) return this;
148 return super.union(other);
ngeoffray 2012/04/25 11:41:13 Personal preference, but I'd prefer if you inlined
floitsch 2012/04/25 19:52:15 Done.
149 }
150
151 HType intersection(HType other) {
152 if (other.isUnknown()) return this;
153 if (other.isInteger()) return HType.CONFLICTING;
154 if (other.isNumber()) return this;
155 return HType.CONFLICTING;
156 }
157 }
158
159 class HIndexableType extends HPrimitiveType {
160 const HIndexableType();
161 bool isIndexable() => true;
162 String toString() => "indexable";
163
164 HType union(HType other) {
165 if (other.isIndexable() || other.isUnknown()) return HType.INDEXABLE;
166 return HType.CONFLICTING;
167 }
168
169 HType intersection(HType other) {
170 if (other.isUnknown()) return this;
171 if (other.isIndexable()) return other;
172 return HType.CONFLICTING;
173 }
174 }
175
176 class HStringType extends HPrimitiveType {
177 const HStringType();
178 bool isString() => true;
179 String toString() => "string";
180
181 HType union(HType other) {
182 if (other.isString() || other.isUnknown()) return this;
183 return HType.CONFLICTING;
184 }
185
186 HType intersection(HType other) {
187 if (other.isString() || other.isUnknown()) return this;
188 if (other.isArray()) return HType.CONFLICTING;
189 if (other.isIndexable()) return this;
190 return HType.CONFLICTING;
191 }
192 }
193
194 class HReadableArrayType extends HIndexableType {
195 const HReadableArrayType();
196 bool isReadableArray() => true;
197 String toString() => "readable array";
198
199 HType union(HType other) {
200 if (other.isReadableArray() || other.isUnknown()) {
201 return HType.READABLE_ARRAY;
202 }
203 return super.union(other);
204 }
205
206 HType intersection(HType other) {
207 if (this === other || other.isUnknown()) return this;
208 if (other.isString()) return HType.CONFLICTING;
209 if (other.isReadableArray()) return other;
210 if (other.isIndexable()) return this;
211 return HType.CONFLICTING;
212 }
213 }
214
215 class HMutableArrayType extends HReadableArrayType {
216 const HMutableArrayType();
217 bool isMutableArray() => true;
218 String toString() => "mutable array";
219
220 HType union(HType other) {
221 if (other.isMutableArray() || other.isUnknown()) {
222 return HType.MUTABLE_ARRAY;
223 }
224 return super.union(other);
225 }
226
227 HType intersection(HType other) {
228 if (this === other || other.isUnknown()) return this;
229 if (other.isString()) return HType.CONFLICTING;
230 if (other.isMutableArray()) return other;
231 if (other.isIndexable()) return this;
232 return HType.CONFLICTING;
233 }
234 }
235
236 class HExtendableArrayType extends HMutableArrayType {
237 const HExtendableArrayType();
238 bool isExtendableArray() => true;
239 String toString() => "extendable array";
240
241 HType union(HType other) {
242 if (other.isExtendableArray() || other.isUnknown()) {
243 return HType.EXTENDABLE_ARRAY;
244 }
245 return super.union(other);
246 }
247
248 HType intersection(HType other) {
249 if (this === other || other.isUnknown()) return this;
250 if (other.isString()) return HType.CONFLICTING;
251 if (other.isIndexable()) return this;
252 return HType.CONFLICTING;
253 }
254 }
255
256
ngeoffray 2012/04/25 11:41:13 extra space
floitsch 2012/04/25 19:52:15 Done.
257 class HNonPrimitiveType extends HType {
258 final Type type;
259
260 const HNonPrimitiveType(Type this.type);
261 bool isNonPrimitive() => true;
262 String toString() => type.toString();
263
264 HType combine(HType other) {
265 if (other.isNonPrimitive()) {
266 HNonPrimitiveType temp = other;
267 if (this.type === temp.type) return this;
268 }
269 if (other.isUnknown()) return this;
270 return HType.CONFLICTING;
271 }
272
273 // As long as we don't keep track of super/sub types for non-primitive types
274 // the intersection and union is the same.
275 HType intersection(HType other) => combine(other);
276 HType union(HType other) => combine(other);
277
278 Element lookupMember(SourceString name) {
279 ClassElement classElement = type.element;
280 return classElement.lookupMember(name);
281 }
282 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698