| 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 part of dart2js.cps_ir.optimizers; | 5 part of dart2js.cps_ir.optimizers; |
| 6 | 6 |
| 7 /// Eliminate redundant phis from the given [FunctionDefinition]. | 7 /// Eliminate redundant phis from the given [FunctionDefinition]. |
| 8 /// | 8 /// |
| 9 /// Phis in this case are [Continuations] together with corresponding | 9 /// Phis in this case are [Continuations] together with corresponding |
| 10 /// [InvokeContinuation]s. A [Continuation] parameter at position i is redundant | 10 /// [InvokeContinuation]s. A [Continuation] parameter at position i is redundant |
| (...skipping 60 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 71 } else if (value == null) { | 71 } else if (value == null) { |
| 72 value = def; // Set initial comparison value. | 72 value = def; // Set initial comparison value. |
| 73 } else if (value != def) { | 73 } else if (value != def) { |
| 74 return null; // Differing invocation arguments. | 74 return null; // Differing invocation arguments. |
| 75 } | 75 } |
| 76 } | 76 } |
| 77 | 77 |
| 78 return value; | 78 return value; |
| 79 } | 79 } |
| 80 | 80 |
| 81 // If uniqueDefinition is in the body of the LetCont binding the |
| 82 // continuation, then we will drop the continuation binding to just inside |
| 83 // the binding of uniqueDefiniton. This is not safe if we drop the |
| 84 // continuation binding inside a LetHandler exception handler binding. |
| 85 LetCont letCont = cont.parent; |
| 86 bool safeForHandlers(Definition uniqueDefinition) { |
| 87 bool seenHandler = false; |
| 88 Node current = uniqueDefinition.parent; |
| 89 while (current != null) { |
| 90 if (current == letCont) return !seenHandler; |
| 91 seenHandler = seenHandler || current is LetHandler; |
| 92 current = current.parent; |
| 93 } |
| 94 // When uniqueDefinition is not in the body of the LetCont binding the |
| 95 // continuation, we will not move any code, so that is safe. |
| 96 return true; |
| 97 } |
| 98 |
| 81 // Check if individual parameters are always called with a unique | 99 // Check if individual parameters are always called with a unique |
| 82 // definition, and remove them if that is the case. During each iteration, | 100 // definition, and remove them if that is the case. During each iteration, |
| 83 // we read the current parameter/argument from index `src` and copy it | 101 // we read the current parameter/argument from index `src` and copy it |
| 84 // to index `dst`. | 102 // to index `dst`. |
| 85 int dst = 0; | 103 int dst = 0; |
| 86 for (int src = 0; src < cont.parameters.length; src++) { | 104 for (int src = 0; src < cont.parameters.length; src++) { |
| 87 // Is the current phi redundant? | 105 // Is the current phi redundant? |
| 88 Definition uniqueDefinition = uniqueDefinitionOf(src); | 106 Definition uniqueDefinition = uniqueDefinitionOf(src); |
| 89 if (uniqueDefinition == null) { | 107 if (uniqueDefinition == null || !safeForHandlers(uniqueDefinition)) { |
| 90 // Reorganize parameters and arguments in case of deletions. | 108 // Reorganize parameters and arguments in case of deletions. |
| 91 cont.parameters[dst] = cont.parameters[src]; | 109 if (src != dst) { |
| 92 for (InvokeContinuation invoke in invokes) { | 110 cont.parameters[dst] = cont.parameters[src]; |
| 111 for (InvokeContinuation invoke in invokes) { |
| 93 invoke.arguments[dst] = invoke.arguments[src]; | 112 invoke.arguments[dst] = invoke.arguments[src]; |
| 113 } |
| 94 } | 114 } |
| 95 | |
| 96 dst++; | 115 dst++; |
| 97 continue; | 116 continue; |
| 98 } | 117 } |
| 99 | 118 |
| 100 Definition oldDefinition = cont.parameters[src]; | 119 Definition oldDefinition = cont.parameters[src]; |
| 101 | 120 |
| 102 // Add continuations of about-to-be modified invokes to worklist since | 121 // Add continuations of about-to-be modified invokes to worklist since |
| 103 // we might introduce new optimization opportunities. | 122 // we might introduce new optimization opportunities. |
| 104 for (Reference ref = oldDefinition.firstRef; ref != null; | 123 for (Reference ref = oldDefinition.firstRef; |
| 124 ref != null; |
| 105 ref = ref.next) { | 125 ref = ref.next) { |
| 106 Node parent = ref.parent; | 126 Node parent = ref.parent; |
| 107 if (parent is InvokeContinuation) { | 127 if (parent is InvokeContinuation) { |
| 108 Continuation thatCont = parent.continuation.definition; | 128 Continuation thatCont = parent.continuation.definition; |
| 109 if (thatCont != cont) { | 129 if (thatCont != cont) { |
| 110 workSet.add(thatCont); | 130 workSet.add(thatCont); |
| 111 } | 131 } |
| 112 } | 132 } |
| 113 } | 133 } |
| 114 | 134 |
| 115 // Replace individual parameters: | 135 // Replace individual parameters: |
| 116 // * In the continuation body, replace occurrence of param with value, | 136 // * In the continuation body, replace occurrence of param with value, |
| 117 // * and implicitly remove param from continuation signature and | 137 // * and implicitly remove param from continuation signature and |
| 118 // invocations by not incrementing `dst`. References of removed | 138 // invocations by not incrementing `dst`. References of removed |
| 119 // arguments are unlinked to keep definition usages up to date. | 139 // arguments are unlinked to keep definition usages up to date. |
| 120 uniqueDefinition.substituteFor(oldDefinition); | 140 uniqueDefinition.substituteFor(oldDefinition); |
| 121 for (InvokeContinuation invoke in invokes) { | 141 for (InvokeContinuation invoke in invokes) { |
| 122 invoke.arguments[src].unlink(); | 142 invoke.arguments[src].unlink(); |
| 123 } | 143 } |
| 124 | 144 |
| 125 // Finally, if the substituted definition is not in scope of the affected | 145 // Finally, if the substituted definition is not in scope of the affected |
| 126 // continuation, move the continuation binding. This is safe to do since | 146 // continuation, move the continuation binding. This is safe to do since |
| 127 // the continuation is referenced only as the target in continuation | 147 // the continuation is referenced only as the target in continuation |
| 128 // invokes, and all such invokes must be within the scope of | 148 // invokes, and all such invokes must be within the scope of |
| 129 // [uniqueDefinition]. Note that this is linear in the depth of | 149 // [uniqueDefinition]. Note that this is linear in the depth of |
| 130 // the binding of [uniqueDefinition]. | 150 // the binding of [uniqueDefinition]. |
| 131 LetCont letCont = cont.parent; | |
| 132 assert(letCont != null); | 151 assert(letCont != null); |
| 133 _moveIntoScopeOf(letCont, uniqueDefinition); | 152 _moveIntoScopeOf(letCont, uniqueDefinition); |
| 134 } | 153 } |
| 135 | 154 |
| 136 // Remove trailing items from parameter and argument lists. | 155 // Remove trailing items from parameter and argument lists. |
| 137 cont.parameters.length = dst; | 156 cont.parameters.length = dst; |
| 138 for (InvokeContinuation invoke in invokes) { | 157 for (InvokeContinuation invoke in invokes) { |
| 139 invoke.arguments.length = dst; | 158 invoke.arguments.length = dst; |
| 140 } | 159 } |
| 141 } | 160 } |
| (...skipping 29 matching lines...) Expand all Loading... |
| 171 | 190 |
| 172 // Insert it just below the binding of definition. | 191 // Insert it just below the binding of definition. |
| 173 InteriorNode binding = definition.parent; | 192 InteriorNode binding = definition.parent; |
| 174 | 193 |
| 175 letCont.body = binding.body; | 194 letCont.body = binding.body; |
| 176 binding.body.parent = letCont; | 195 binding.body.parent = letCont; |
| 177 | 196 |
| 178 binding.body = letCont; | 197 binding.body = letCont; |
| 179 letCont.parent = binding; | 198 letCont.parent = binding; |
| 180 } | 199 } |
| OLD | NEW |