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.type = guard.guarded.type; | 57 guard.propagatedType = guard.guarded.propagatedType; |
58 guard.guarded.type = HType.UNKNOWN; | 58 guard.guarded.propagatedType = 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.type.isKnown()) { | 102 if (!replacement.propagatedType.isUseful()) { |
103 replacement.type = instruction.type; | 103 replacement.propagatedType = instruction.propagatedType; |
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.type.isKnown()) { | 120 if (input.propagatedType.isUseful()) { |
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 return (value.type.combine(node.type) == value.type) ? value : node; | 189 HType combinedType = value.propagatedType.combine(node.propagatedType); |
| 190 return (combinedType == value.propagatedType) ? value : node; |
190 } | 191 } |
191 | 192 |
192 HInstruction visitIntegerCheck(HIntegerCheck node) { | 193 HInstruction visitIntegerCheck(HIntegerCheck node) { |
193 HInstruction value = node.value; | 194 HInstruction value = node.value; |
194 return value.isInteger() ? value : node; | 195 return value.isInteger() ? value : node; |
195 } | 196 } |
196 | 197 |
197 HInstruction visitIs(HIs node) { | 198 HInstruction visitIs(HIs node) { |
198 Element element = node.typeExpression; | 199 Element element = node.typeExpression; |
199 if (element.kind === ElementKind.TYPE_VARIABLE) { | 200 if (element.kind === ElementKind.TYPE_VARIABLE) { |
200 compiler.unimplemented("visitIs for type variables"); | 201 compiler.unimplemented("visitIs for type variables"); |
201 } | 202 } |
202 | 203 |
203 HType expressionType = node.expression.type; | 204 HType expressionType = node.expression.propagatedType; |
204 if (element === compiler.objectClass | 205 if (element === compiler.objectClass |
205 || element === compiler.dynamicClass) { | 206 || element === compiler.dynamicClass) { |
206 return graph.addConstantBool(true); | 207 return graph.addConstantBool(true); |
207 } else if (expressionType.isInteger()) { | 208 } else if (expressionType.isInteger()) { |
208 if (element === compiler.intClass || element === compiler.numClass) { | 209 if (element === compiler.intClass || element === compiler.numClass) { |
209 return graph.addConstantBool(true); | 210 return graph.addConstantBool(true); |
210 } else if (element === compiler.doubleClass) { | 211 } else if (element === compiler.doubleClass) { |
211 // We let the JS semantics decide for that check. Currently | 212 // We let the JS semantics decide for that check. Currently |
212 // the code we emit will always return true. | 213 // the code we emit will always return true. |
213 return node; | 214 return node; |
(...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
276 HBoundsCheck insertBoundsCheck(HInstruction node, | 277 HBoundsCheck insertBoundsCheck(HInstruction node, |
277 HInstruction receiver, | 278 HInstruction receiver, |
278 HInstruction index) { | 279 HInstruction index) { |
279 HStatic interceptor = new HStatic(lengthInterceptor); | 280 HStatic interceptor = new HStatic(lengthInterceptor); |
280 node.block.addBefore(node, interceptor); | 281 node.block.addBefore(node, interceptor); |
281 HInvokeInterceptor length = new HInvokeInterceptor( | 282 HInvokeInterceptor length = new HInvokeInterceptor( |
282 Selector.INVOCATION_0, | 283 Selector.INVOCATION_0, |
283 const SourceString("length"), | 284 const SourceString("length"), |
284 true, | 285 true, |
285 <HInstruction>[interceptor, receiver]); | 286 <HInstruction>[interceptor, receiver]); |
286 length.type = HType.NUMBER; | 287 length.propagatedType = HType.INTEGER; |
287 node.block.addBefore(node, length); | 288 node.block.addBefore(node, length); |
288 | 289 |
289 HBoundsCheck check = new HBoundsCheck(length, index); | 290 HBoundsCheck check = new HBoundsCheck(length, index); |
290 node.block.addBefore(node, check); | 291 node.block.addBefore(node, check); |
291 return check; | 292 return check; |
292 } | 293 } |
293 | 294 |
294 HIntegerCheck insertIntegerCheck(HInstruction node, HInstruction value) { | 295 HIntegerCheck insertIntegerCheck(HInstruction node, HInstruction value) { |
295 HIntegerCheck check = new HIntegerCheck(value); | 296 HIntegerCheck check = new HIntegerCheck(value); |
296 node.block.addBefore(node, check); | 297 node.block.addBefore(node, check); |
297 return check; | 298 return check; |
298 } | 299 } |
299 | 300 |
300 void visitIndex(HIndex node) { | 301 void visitIndex(HIndex node) { |
301 if (!node.builtin) return; | 302 if (!node.receiver.isStringOrArray()) return; |
302 if (node.index is HBoundsCheck) return; | 303 HInstruction index = node.index; |
303 HInstruction index = insertIntegerCheck(node, node.index); | 304 if (index is HBoundsCheck) return; |
| 305 if (!node.index.isInteger()) { |
| 306 index = insertIntegerCheck(node, index); |
| 307 } |
304 index = insertBoundsCheck(node, node.receiver, index); | 308 index = insertBoundsCheck(node, node.receiver, index); |
305 HIndex newInstruction = new HIndex(node.target, node.receiver, index); | 309 HIndex newInstruction = new HIndex(node.target, node.receiver, index); |
306 node.block.addBefore(node, newInstruction); | 310 node.block.addBefore(node, newInstruction); |
307 node.block.rewrite(node, newInstruction); | 311 node.block.rewrite(node, newInstruction); |
308 node.block.remove(node); | 312 node.block.remove(node); |
309 } | 313 } |
310 | 314 |
311 void visitIndexAssign(HIndexAssign node) { | 315 void visitIndexAssign(HIndexAssign node) { |
312 if (!node.builtin) return; | 316 if (!node.receiver.isMutableArray()) return; |
313 if (node.index is HBoundsCheck) return; | 317 HInstruction index = node.index; |
314 HInstruction index = insertIntegerCheck(node, node.index); | 318 if (index is HBoundsCheck) return; |
| 319 if (!node.index.isInteger()) { |
| 320 index = insertIntegerCheck(node, index); |
| 321 } |
315 index = insertBoundsCheck(node, node.receiver, index); | 322 index = insertBoundsCheck(node, node.receiver, index); |
316 HIndexAssign newInstruction = | 323 HIndexAssign newInstruction = |
317 new HIndexAssign(node.target, node.receiver, index, node.value); | 324 new HIndexAssign(node.target, node.receiver, index, node.value); |
318 node.block.addBefore(node, newInstruction); | 325 node.block.addBefore(node, newInstruction); |
319 node.block.rewrite(node, newInstruction); | 326 node.block.rewrite(node, newInstruction); |
320 node.block.remove(node); | 327 node.block.remove(node); |
321 } | 328 } |
322 } | 329 } |
323 | 330 |
324 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { | 331 class SsaDeadCodeEliminator extends HGraphVisitor implements OptimizationPhase { |
(...skipping 379 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
704 } | 711 } |
705 } | 712 } |
706 if (!canBeMoved) continue; | 713 if (!canBeMoved) continue; |
707 | 714 |
708 // This is safe because we are running after GVN. | 715 // This is safe because we are running after GVN. |
709 // TODO(ngeoffray): ensure GVN has been run. | 716 // TODO(ngeoffray): ensure GVN has been run. |
710 set_.add(current); | 717 set_.add(current); |
711 } | 718 } |
712 } | 719 } |
713 } | 720 } |
OLD | NEW |