OLD | NEW |
1 // Copyright (c) 2011, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2011, 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 interface OptimizationPhase { | 5 interface OptimizationPhase { |
6 String get name(); | 6 String get name(); |
7 void visitGraph(HGraph graph); | 7 void visitGraph(HGraph graph); |
8 } | 8 } |
9 | 9 |
10 class SsaOptimizerTask extends CompilerTask { | 10 class SsaOptimizerTask extends CompilerTask { |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
47 return !work.guards.isEmpty(); | 47 return !work.guards.isEmpty(); |
48 }); | 48 }); |
49 } | 49 } |
50 | 50 |
51 void prepareForSpeculativeOptimizations(WorkItem work, HGraph graph) { | 51 void prepareForSpeculativeOptimizations(WorkItem work, HGraph graph) { |
52 measure(() { | 52 measure(() { |
53 // In order to generate correct code for the bailout version, we did not | 53 // In order to generate correct code for the bailout version, we did not |
54 // propagate types from the instruction to the type guard. We do it | 54 // propagate types from the instruction to the type guard. We do it |
55 // now to be able to optimize further. | 55 // now to be able to optimize further. |
56 work.guards.forEach((HTypeGuard guard) { | 56 work.guards.forEach((HTypeGuard guard) { |
57 guard.propagatedType = guard.guarded.propagatedType; | 57 guard.type = guard.guarded.type; |
58 guard.guarded.propagatedType = HType.UNKNOWN; | 58 guard.guarded.type = HType.UNKNOWN; |
59 }); | 59 }); |
60 // We also need to insert range and integer checks for the type guards, | 60 // We also need to insert range and integer checks for the type guards, |
61 // now that they know their type. We did not need to do that | 61 // now that they know their type. We did not need to do that |
62 // before because instructions that reference a guard would | 62 // before because instructions that reference a guard would |
63 // have not tried to use, e.g. native array access, since the | 63 // have not tried to use, e.g. native array access, since the |
64 // guard was not typed. | 64 // guard was not typed. |
65 runPhases(graph, <OptimizationPhase>[new SsaCheckInserter(compiler)]); | 65 runPhases(graph, <OptimizationPhase>[new SsaCheckInserter(compiler)]); |
66 }); | 66 }); |
67 } | 67 } |
68 } | 68 } |
(...skipping 23 matching lines...) Expand all Loading... |
92 if (!replacement.isInBasicBlock()) { | 92 if (!replacement.isInBasicBlock()) { |
93 // The constant folding can return an instruction that is already | 93 // The constant folding can return an instruction that is already |
94 // part of the graph (like an input), so we only add the replacement | 94 // part of the graph (like an input), so we only add the replacement |
95 // if necessary. | 95 // if necessary. |
96 block.addAfter(instruction, replacement); | 96 block.addAfter(instruction, replacement); |
97 } | 97 } |
98 block.rewrite(instruction, replacement); | 98 block.rewrite(instruction, replacement); |
99 block.remove(instruction); | 99 block.remove(instruction); |
100 // If the replacement instruction does not know its type yet, | 100 // If the replacement instruction does not know its type yet, |
101 // use the type of the instruction. | 101 // use the type of the instruction. |
102 if (!replacement.propagatedType.isUseful()) { | 102 if (!replacement.type.isKnown()) { |
103 replacement.propagatedType = instruction.propagatedType; | 103 replacement.type = instruction.type; |
104 } | 104 } |
105 } | 105 } |
106 instruction = next; | 106 instruction = next; |
107 } | 107 } |
108 } | 108 } |
109 | 109 |
110 HInstruction visitInstruction(HInstruction node) { | 110 HInstruction visitInstruction(HInstruction node) { |
111 return node; | 111 return node; |
112 } | 112 } |
113 | 113 |
114 HInstruction visitBoolify(HBoolify node) { | 114 HInstruction visitBoolify(HBoolify node) { |
115 List<HInstruction> inputs = node.inputs; | 115 List<HInstruction> inputs = node.inputs; |
116 assert(inputs.length == 1); | 116 assert(inputs.length == 1); |
117 HInstruction input = inputs[0]; | 117 HInstruction input = inputs[0]; |
118 if (input.isBoolean()) return input; | 118 if (input.isBoolean()) return input; |
119 // All values !== true are boolified to false. | 119 // All values !== true are boolified to false. |
120 if (input.propagatedType.isUseful()) { | 120 if (input.type.isKnown()) { |
121 return graph.addConstantBool(false); | 121 return graph.addConstantBool(false); |
122 } | 122 } |
123 return node; | 123 return node; |
124 } | 124 } |
125 | 125 |
126 HInstruction visitNot(HNot node) { | 126 HInstruction visitNot(HNot node) { |
127 List<HInstruction> inputs = node.inputs; | 127 List<HInstruction> inputs = node.inputs; |
128 assert(inputs.length == 1); | 128 assert(inputs.length == 1); |
129 HInstruction input = inputs[0]; | 129 HInstruction input = inputs[0]; |
130 if (input is HConstant) { | 130 if (input is HConstant) { |
(...skipping 48 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
179 compiler.builder.interceptors.getEqualsNullInterceptor()); | 179 compiler.builder.interceptors.getEqualsNullInterceptor()); |
180 node.block.addBefore(node,target); | 180 node.block.addBefore(node,target); |
181 return new HEquals(target, node.left, node.right); | 181 return new HEquals(target, node.left, node.right); |
182 } | 182 } |
183 // All other cases are dealt with by the [visitInvokeBinary]. | 183 // All other cases are dealt with by the [visitInvokeBinary]. |
184 return visitInvokeBinary(node); | 184 return visitInvokeBinary(node); |
185 } | 185 } |
186 | 186 |
187 HInstruction visitTypeGuard(HTypeGuard node) { | 187 HInstruction visitTypeGuard(HTypeGuard node) { |
188 HInstruction value = node.guarded; | 188 HInstruction value = node.guarded; |
189 HType combinedType = value.propagatedType.combine(node.propagatedType); | 189 return (value.type.combine(node.type) == value.type) ? value : node; |
190 return (combinedType == value.propagatedType) ? value : node; | |
191 } | 190 } |
192 | 191 |
193 HInstruction visitIntegerCheck(HIntegerCheck node) { | 192 HInstruction visitIntegerCheck(HIntegerCheck node) { |
194 HInstruction value = node.value; | 193 HInstruction value = node.value; |
195 return value.isInteger() ? value : node; | 194 return value.isInteger() ? value : node; |
196 } | 195 } |
197 | 196 |
198 HInstruction visitIs(HIs node) { | 197 HInstruction visitIs(HIs node) { |
199 Type type = node.typeName; | 198 Type type = node.typeName; |
200 Element element = type.element; | 199 Element element = type.element; |
201 if (element.kind === ElementKind.TYPE_VARIABLE) { | 200 if (element.kind === ElementKind.TYPE_VARIABLE) { |
202 compiler.unimplemented("visitIs for type variables"); | 201 compiler.unimplemented("visitIs for type variables"); |
203 } | 202 } |
204 | 203 |
205 HType expressionType = node.expression.propagatedType; | 204 HType expressionType = node.expression.type; |
206 if (element === compiler.objectClass | 205 if (element === compiler.objectClass |
207 || element === compiler.dynamicClass) { | 206 || element === compiler.dynamicClass) { |
208 return graph.addConstantBool(true); | 207 return graph.addConstantBool(true); |
209 } else if (expressionType.isInteger()) { | 208 } else if (expressionType.isInteger()) { |
210 if (element === compiler.intClass || element === compiler.numClass) { | 209 if (element === compiler.intClass || element === compiler.numClass) { |
211 return graph.addConstantBool(true); | 210 return graph.addConstantBool(true); |
212 } else if (element === compiler.doubleClass) { | 211 } else if (element === compiler.doubleClass) { |
213 // We let the JS semantics decide for that check. Currently | 212 // We let the JS semantics decide for that check. Currently |
214 // the code we emit will always return true. | 213 // the code we emit will always return true. |
215 return node; | 214 return node; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
278 HBoundsCheck insertBoundsCheck(HInstruction node, | 277 HBoundsCheck insertBoundsCheck(HInstruction node, |
279 HInstruction receiver, | 278 HInstruction receiver, |
280 HInstruction index) { | 279 HInstruction index) { |
281 HStatic interceptor = new HStatic(lengthInterceptor); | 280 HStatic interceptor = new HStatic(lengthInterceptor); |
282 node.block.addBefore(node, interceptor); | 281 node.block.addBefore(node, interceptor); |
283 HInvokeInterceptor length = new HInvokeInterceptor( | 282 HInvokeInterceptor length = new HInvokeInterceptor( |
284 Selector.INVOCATION_0, | 283 Selector.INVOCATION_0, |
285 const SourceString("length"), | 284 const SourceString("length"), |
286 true, | 285 true, |
287 <HInstruction>[interceptor, receiver]); | 286 <HInstruction>[interceptor, receiver]); |
288 length.propagatedType = HType.INTEGER; | 287 length.type = HType.NUMBER; |
289 node.block.addBefore(node, length); | 288 node.block.addBefore(node, length); |
290 | 289 |
291 HBoundsCheck check = new HBoundsCheck(length, index); | 290 HBoundsCheck check = new HBoundsCheck(length, index); |
292 node.block.addBefore(node, check); | 291 node.block.addBefore(node, check); |
293 return check; | 292 return check; |
294 } | 293 } |
295 | 294 |
296 HIntegerCheck insertIntegerCheck(HInstruction node, HInstruction value) { | 295 HIntegerCheck insertIntegerCheck(HInstruction node, HInstruction value) { |
297 HIntegerCheck check = new HIntegerCheck(value); | 296 HIntegerCheck check = new HIntegerCheck(value); |
298 node.block.addBefore(node, check); | 297 node.block.addBefore(node, check); |
299 return check; | 298 return check; |
300 } | 299 } |
301 | 300 |
302 void visitIndex(HIndex node) { | 301 void visitIndex(HIndex node) { |
303 if (!node.receiver.isStringOrArray()) return; | 302 if (!node.builtin) return; |
304 HInstruction index = node.index; | 303 if (node.index is HBoundsCheck) return; |
305 if (index is HBoundsCheck) return; | 304 HInstruction index = insertIntegerCheck(node, node.index); |
306 if (!node.index.isInteger()) { | |
307 index = insertIntegerCheck(node, index); | |
308 } | |
309 index = insertBoundsCheck(node, node.receiver, index); | 305 index = insertBoundsCheck(node, node.receiver, index); |
310 HIndex newInstruction = new HIndex(node.target, node.receiver, index); | 306 HIndex newInstruction = new HIndex(node.target, node.receiver, index); |
311 node.block.addBefore(node, newInstruction); | 307 node.block.addBefore(node, newInstruction); |
312 node.block.rewrite(node, newInstruction); | 308 node.block.rewrite(node, newInstruction); |
313 node.block.remove(node); | 309 node.block.remove(node); |
314 } | 310 } |
315 | 311 |
316 void visitIndexAssign(HIndexAssign node) { | 312 void visitIndexAssign(HIndexAssign node) { |
317 if (!node.receiver.isMutableArray()) return; | 313 if (!node.builtin) return; |
318 HInstruction index = node.index; | 314 if (node.index is HBoundsCheck) return; |
319 if (index is HBoundsCheck) return; | 315 HInstruction index = insertIntegerCheck(node, node.index); |
320 if (!node.index.isInteger()) { | |
321 index = insertIntegerCheck(node, index); | |
322 } | |
323 index = insertBoundsCheck(node, node.receiver, index); | 316 index = insertBoundsCheck(node, node.receiver, index); |
324 HIndexAssign newInstruction = | 317 HIndexAssign newInstruction = |
325 new HIndexAssign(node.target, node.receiver, index, node.value); | 318 new HIndexAssign(node.target, node.receiver, index, node.value); |
326 node.block.addBefore(node, newInstruction); | 319 node.block.addBefore(node, newInstruction); |
327 node.block.rewrite(node, newInstruction); | 320 node.block.rewrite(node, newInstruction); |
328 node.block.remove(node); | 321 node.block.remove(node); |
329 } | 322 } |
330 } | 323 } |
331 | 324 |
332 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { | 325 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
712 } | 705 } |
713 } | 706 } |
714 if (!canBeMoved) continue; | 707 if (!canBeMoved) continue; |
715 | 708 |
716 // This is safe because we are running after GVN. | 709 // This is safe because we are running after GVN. |
717 // TODO(ngeoffray): ensure GVN has been run. | 710 // TODO(ngeoffray): ensure GVN has been run. |
718 set_.add(current); | 711 set_.add(current); |
719 } | 712 } |
720 } | 713 } |
721 } | 714 } |
OLD | NEW |