| OLD | NEW |
| 1 // Copyright (c) 2014, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2014, 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 library dart2js.cps_ir.redundant_phi_elimination; | 5 library dart2js.cps_ir.redundant_phi_elimination; |
| 6 | 6 |
| 7 import 'cps_ir_nodes.dart'; | 7 import 'cps_ir_nodes.dart'; |
| 8 import 'optimizers.dart'; | 8 import 'optimizers.dart'; |
| 9 | 9 |
| 10 /// Eliminate redundant phis from the given [FunctionDefinition]. | 10 /// Eliminate redundant phis from the given [FunctionDefinition]. |
| (...skipping 43 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 54 } | 54 } |
| 55 } | 55 } |
| 56 | 56 |
| 57 if (invokes.isEmpty) { | 57 if (invokes.isEmpty) { |
| 58 return; // Continuation is never invoked, can't optimize. | 58 return; // Continuation is never invoked, can't optimize. |
| 59 } | 59 } |
| 60 | 60 |
| 61 /// Returns the unique definition of parameter i if it exists and null | 61 /// Returns the unique definition of parameter i if it exists and null |
| 62 /// otherwise. A definition is unique if it is the only value used to | 62 /// otherwise. A definition is unique if it is the only value used to |
| 63 /// invoke the continuation, excluding feedback. | 63 /// invoke the continuation, excluding feedback. |
| 64 Definition uniqueDefinitionOf(int i) { | 64 Primitive uniqueDefinitionOf(int i) { |
| 65 Definition value = null; | 65 Primitive value = null; |
| 66 for (InvokeContinuation invoke in invokes) { | 66 for (InvokeContinuation invoke in invokes) { |
| 67 Definition def = invoke.arguments[i].definition; | 67 Primitive def = invoke.arguments[i].definition.effectiveDefinition; |
| 68 | 68 |
| 69 if (cont.parameters[i] == def) { | 69 if (cont.parameters[i] == def) { |
| 70 // Invocation param == param in LetCont (i.e. a recursive call). | 70 // Invocation param == param in LetCont (i.e. a recursive call). |
| 71 continue; | 71 continue; |
| 72 } else if (value == null) { | 72 } else if (value == null) { |
| 73 value = def; // Set initial comparison value. | 73 value = def; // Set initial comparison value. |
| 74 } else if (value != def) { | 74 } else if (value != def) { |
| 75 return null; // Differing invocation arguments. | 75 return null; // Differing invocation arguments. |
| 76 } | 76 } |
| 77 } | 77 } |
| (...skipping 19 matching lines...) Expand all Loading... |
| 97 return true; | 97 return true; |
| 98 } | 98 } |
| 99 | 99 |
| 100 // Check if individual parameters are always called with a unique | 100 // Check if individual parameters are always called with a unique |
| 101 // definition, and remove them if that is the case. During each iteration, | 101 // definition, and remove them if that is the case. During each iteration, |
| 102 // we read the current parameter/argument from index `src` and copy it | 102 // we read the current parameter/argument from index `src` and copy it |
| 103 // to index `dst`. | 103 // to index `dst`. |
| 104 int dst = 0; | 104 int dst = 0; |
| 105 for (int src = 0; src < cont.parameters.length; src++) { | 105 for (int src = 0; src < cont.parameters.length; src++) { |
| 106 // Is the current phi redundant? | 106 // Is the current phi redundant? |
| 107 Definition uniqueDefinition = uniqueDefinitionOf(src); | 107 Primitive uniqueDefinition = uniqueDefinitionOf(src); |
| 108 if (uniqueDefinition == null || !safeForHandlers(uniqueDefinition)) { | 108 if (uniqueDefinition == null || !safeForHandlers(uniqueDefinition)) { |
| 109 // Reorganize parameters and arguments in case of deletions. | 109 // Reorganize parameters and arguments in case of deletions. |
| 110 if (src != dst) { | 110 if (src != dst) { |
| 111 cont.parameters[dst] = cont.parameters[src]; | 111 cont.parameters[dst] = cont.parameters[src]; |
| 112 for (InvokeContinuation invoke in invokes) { | 112 for (InvokeContinuation invoke in invokes) { |
| 113 invoke.arguments[dst] = invoke.arguments[src]; | 113 invoke.arguments[dst] = invoke.arguments[src]; |
| 114 } | 114 } |
| 115 } | 115 } |
| 116 dst++; | 116 dst++; |
| 117 continue; | 117 continue; |
| 118 } | 118 } |
| 119 | 119 |
| 120 Definition oldDefinition = cont.parameters[src]; | 120 Primitive oldDefinition = cont.parameters[src]; |
| 121 | 121 |
| 122 // Add continuations of about-to-be modified invokes to worklist since | 122 // Add continuations of about-to-be modified invokes to worklist since |
| 123 // we might introduce new optimization opportunities. | 123 // we might introduce new optimization opportunities. |
| 124 for (Reference ref = oldDefinition.firstRef; | 124 for (Reference ref = oldDefinition.firstRef; |
| 125 ref != null; | 125 ref != null; |
| 126 ref = ref.next) { | 126 ref = ref.next) { |
| 127 Node parent = ref.parent; | 127 Node parent = ref.parent; |
| 128 if (parent is InvokeContinuation) { | 128 if (parent is InvokeContinuation) { |
| 129 Continuation thatCont = parent.continuation.definition; | 129 Continuation thatCont = parent.continuation.definition; |
| 130 if (thatCont != cont) { | 130 if (thatCont != cont) { |
| (...skipping 64 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 195 /// Returns the LetCont that now binds [continuation]. | 195 /// Returns the LetCont that now binds [continuation]. |
| 196 LetCont _makeUniqueBinding(Continuation continuation) { | 196 LetCont _makeUniqueBinding(Continuation continuation) { |
| 197 LetCont letCont = continuation.parent; | 197 LetCont letCont = continuation.parent; |
| 198 if (letCont.continuations.length == 1) return letCont; | 198 if (letCont.continuations.length == 1) return letCont; |
| 199 letCont.continuations.remove(continuation); | 199 letCont.continuations.remove(continuation); |
| 200 LetCont newBinding = new LetCont(continuation, null); | 200 LetCont newBinding = new LetCont(continuation, null); |
| 201 continuation.parent = newBinding; | 201 continuation.parent = newBinding; |
| 202 newBinding.insertBelow(letCont); | 202 newBinding.insertBelow(letCont); |
| 203 return newBinding; | 203 return newBinding; |
| 204 } | 204 } |
| OLD | NEW |