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

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: Address comments. 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_PRIMITIVE = const HIndexablePrimitiveType();
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 isIndexablePrimitive() => 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(NUMBER) => NUMBER.
57 if (updateType(instruction)) { 57 * * DOUBLE.union(INTEGER) => NUMBER.
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);
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 HType.BOOLEAN;
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 HType.NUMBER;
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 HType.INTEGER;
130 instruction.propagatedType = newType; 130 if (other.isNumber()) return HType.NUMBER;
131 HType desiredType = computeDesiredType(instruction); 131 return HType.CONFLICTING;
132 // If the desired type is conflicting just return the computed type. 132 }
133 if (desiredType.isConflicting()) return newType; 133
134 // TODO(ngeoffray): Allow speculative optimizations on 134 HType intersection(HType other) {
135 // non-primitive types? 135 if (other.isUnknown()) return HType.INTEGER;
136 if (desiredType.isNonPrimitive()) return newType; 136 if (other.isDouble()) return HType.CONFLICTING;
137 return newType.combine(desiredType); 137 if (other.isNumber()) return this;
138 } 138 return HType.CONFLICTING;
139 } 139 }
140 }
141
142 class HDoubleType extends HNumberType {
143 const HDoubleType();
144 bool isDouble() => true;
145 String toString() => "double";
146
147 HType union(HType other) {
148 if (other.isDouble() || other.isUnknown()) return HType.DOUBLE;
149 if (other.isNumber()) return HType.NUMBER;
150 return HType.CONFLICTING;
151 }
152
153 HType intersection(HType other) {
154 if (other.isUnknown()) return HType.DOUBLE;
155 if (other.isInteger()) return HType.CONFLICTING;
156 if (other.isNumber()) return this;
157 return HType.CONFLICTING;
158 }
159 }
160
161 class HIndexablePrimitiveType extends HPrimitiveType {
162 const HIndexablePrimitiveType();
163 bool isIndexablePrimitive() => true;
164 String toString() => "indexable";
165
166 HType union(HType other) {
167 if (other.isIndexablePrimitive() || other.isUnknown()) {
168 return HType.INDEXABLE_PRIMITIVE;
169 }
170 return HType.CONFLICTING;
171 }
172
173 HType intersection(HType other) {
174 if (other.isUnknown()) return HType.INDEXABLE_PRIMITIVE;
175 if (other.isIndexablePrimitive()) return other;
176 return HType.CONFLICTING;
177 }
178 }
179
180 class HStringType extends HIndexablePrimitiveType {
181 const HStringType();
182 bool isString() => true;
183 String toString() => "string";
184
185 HType union(HType other) {
186 if (other.isString() || other.isUnknown()) return HType.STRING;
187 if (other.isIndexablePrimitive()) return HType.INDEXABLE_PRIMITIVE;
188 return HType.CONFLICTING;
189 }
190
191 HType intersection(HType other) {
192 if (other.isString() || other.isUnknown()) return HType.STRING;
193 if (other.isArray()) return HType.CONFLICTING;
194 if (other.isIndexablePrimitive()) return HType.STRING;
195 return HType.CONFLICTING;
196 }
197 }
198
199 class HReadableArrayType extends HIndexablePrimitiveType {
200 const HReadableArrayType();
201 bool isReadableArray() => true;
202 String toString() => "readable array";
203
204 HType union(HType other) {
205 if (other.isReadableArray() || other.isUnknown()) {
206 return HType.READABLE_ARRAY;
207 }
208 if (other.isIndexablePrimitive()) return HType.INDEXABLE_PRIMITIVE;
209 return HType.CONFLICTING;
210 }
211
212 HType intersection(HType other) {
213 if (this === other || other.isUnknown()) return HType.READABLE_ARRAY;
214 if (other.isString()) return HType.CONFLICTING;
215 if (other.isReadableArray()) return other;
216 if (other.isIndexablePrimitive()) return this;
217 return HType.CONFLICTING;
218 }
219 }
220
221 class HMutableArrayType extends HReadableArrayType {
222 const HMutableArrayType();
223 bool isMutableArray() => true;
224 String toString() => "mutable array";
225
226 HType union(HType other) {
227 if (other.isMutableArray() || other.isUnknown()) {
228 return HType.MUTABLE_ARRAY;
229 }
230 if (other.isReadableArray()) return HType.READABLE_ARRAY;
231 if (other.isIndexablePrimitive()) return HType.INDEXABLE_PRIMITIVE;
232 return HType.CONFLICTING;
233 }
234
235 HType intersection(HType other) {
236 if (this === other || other.isUnknown()) return HType.MUTABLE_ARRAY;
237 if (other.isString()) return HType.CONFLICTING;
238 if (other.isMutableArray()) return other;
239 if (other.isIndexablePrimitive()) return HType.MUTABLE_ARRAY;
240 return HType.CONFLICTING;
241 }
242 }
243
244 class HExtendableArrayType extends HMutableArrayType {
245 const HExtendableArrayType();
246 bool isExtendableArray() => true;
247 String toString() => "extendable array";
248
249 HType union(HType other) {
250 if (other.isExtendableArray() || other.isUnknown()) {
251 return HType.EXTENDABLE_ARRAY;
252 }
253 if (other.isMutableArray()) return HType.MUTABLE_ARRAY;
254 if (other.isReadableArray()) return HType.READABLE_ARRAY;
255 if (other.isIndexablePrimitive()) return HType.INDEXABLE_PRIMITIVE;
256 return HType.CONFLICTING;
257 }
258
259 HType intersection(HType other) {
260 if (this === other || other.isUnknown()) return HType.EXTENDABLE_ARRAY;
261 if (other.isString()) return HType.CONFLICTING;
262 if (other.isIndexablePrimitive()) return HType.EXTENDABLE_ARRAY;
263 return HType.CONFLICTING;
264 }
265 }
266
267 class HNonPrimitiveType extends HType {
268 final Type type;
269
270 const HNonPrimitiveType(Type this.type);
271 bool isNonPrimitive() => true;
272 String toString() => type.toString();
273
274 HType combine(HType other) {
275 if (other.isNonPrimitive()) {
276 HNonPrimitiveType temp = other;
277 if (this.type === temp.type) return this;
278 }
279 if (other.isUnknown()) return this;
280 return HType.CONFLICTING;
281 }
282
283 // As long as we don't keep track of super/sub types for non-primitive types
284 // the intersection and union is the same.
285 HType intersection(HType other) => combine(other);
286 HType union(HType other) => combine(other);
287
288 Element lookupMember(SourceString name) {
289 ClassElement classElement = type.element;
290 return classElement.lookupMember(name);
291 }
292 }
OLDNEW
« no previous file with comments | « lib/compiler/implementation/ssa/tracer.dart ('k') | lib/compiler/implementation/ssa/types_propagation.dart » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698