| 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 /** | 7 /** |
| 8 * [ShrinkingReducer] applies shrinking reductions to CPS terms as described | 8 * [ShrinkingReducer] applies shrinking reductions to CPS terms as described |
| 9 * in 'Compiling with Continuations, Continued' by Andrew Kennedy. | 9 * in 'Compiling with Continuations, Continued' by Andrew Kennedy. |
| 10 */ | 10 */ |
| (...skipping 182 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 193 // let cont k0(v0) = /* v0 is not used */ in | 193 // let cont k0(v0) = /* v0 is not used */ in |
| 194 // call foo () k0 | 194 // call foo () k0 |
| 195 // | 195 // |
| 196 // Where the dead parameter reduction is no longer valid because we do not | 196 // Where the dead parameter reduction is no longer valid because we do not |
| 197 // allow removing the paramter of call continuations. We disallow such eta | 197 // allow removing the paramter of call continuations. We disallow such eta |
| 198 // reductions in [_isEtaCont]. | 198 // reductions in [_isEtaCont]. |
| 199 assert(_isDeadParameter(task.node)); | 199 assert(_isDeadParameter(task.node)); |
| 200 | 200 |
| 201 Parameter parameter = task.node; | 201 Parameter parameter = task.node; |
| 202 Continuation continuation = parameter.parent; | 202 Continuation continuation = parameter.parent; |
| 203 int index = parameter.parent_index; | 203 int index = parameter.parentIndex; |
| 204 | 204 |
| 205 // Remove the index'th argument from each invocation. | 205 // Remove the index'th argument from each invocation. |
| 206 Reference<Continuation> current = continuation.firstRef; | 206 Reference<Continuation> current = continuation.firstRef; |
| 207 while (current != null) { | 207 while (current != null) { |
| 208 InvokeContinuation invoke = current.parent; | 208 InvokeContinuation invoke = current.parent; |
| 209 Reference<Primitive> argument = invoke.arguments[index]; | 209 Reference<Primitive> argument = invoke.arguments[index]; |
| 210 argument.unlink(); | 210 argument.unlink(); |
| 211 // Removing an argument can create a dead parameter or dead value redex. | 211 // Removing an argument can create a dead parameter or dead value redex. |
| 212 if (argument.definition is Parameter) { | 212 if (argument.definition is Parameter) { |
| 213 if (_isDeadParameter(argument.definition)) { | 213 if (_isDeadParameter(argument.definition)) { |
| 214 _worklist.add(new _ReductionTask(_ReductionKind.DEAD_PARAMETER, | 214 _worklist.add(new _ReductionTask(_ReductionKind.DEAD_PARAMETER, |
| 215 argument.definition)); | 215 argument.definition)); |
| 216 } | 216 } |
| 217 } else { | 217 } else { |
| 218 Node parent = argument.definition.parent; | 218 Node parent = argument.definition.parent; |
| 219 if (parent is LetPrim) { | 219 if (parent is LetPrim) { |
| 220 if (_isDeadVal(parent)) { | 220 if (_isDeadVal(parent)) { |
| 221 _worklist.add(new _ReductionTask(_ReductionKind.DEAD_VAL, parent)); | 221 _worklist.add(new _ReductionTask(_ReductionKind.DEAD_VAL, parent)); |
| 222 } | 222 } |
| 223 } | 223 } |
| 224 } | 224 } |
| 225 invoke.arguments.removeAt(index); | 225 invoke.arguments.removeAt(index); |
| 226 current = current.next; | 226 current = current.next; |
| 227 } | 227 } |
| 228 // Copy the parameters above index down. | 228 // Copy the parameters above index down. |
| 229 List<Parameter> parameters = continuation.parameters; | 229 List<Parameter> parameters = continuation.parameters; |
| 230 for (int i = index; i < parameters.length - 1; ++i) { | 230 for (int i = index; i < parameters.length - 1; ++i) { |
| 231 Parameter p = parameters[i + 1]; | 231 Parameter p = parameters[i + 1]; |
| 232 parameters[i] = p; | 232 parameters[i] = p; |
| 233 p.parent_index = i; | 233 p.parentIndex = i; |
| 234 } | 234 } |
| 235 parameters.removeLast(); | 235 parameters.removeLast(); |
| 236 | 236 |
| 237 // Removing an unused parameter can create an eta-redex. | 237 // Removing an unused parameter can create an eta-redex. |
| 238 if (_isEtaCont(continuation)) { | 238 if (_isEtaCont(continuation)) { |
| 239 _worklist.add(new _ReductionTask(_ReductionKind.ETA_CONT, continuation)); | 239 _worklist.add(new _ReductionTask(_ReductionKind.ETA_CONT, continuation)); |
| 240 } | 240 } |
| 241 } | 241 } |
| 242 } | 242 } |
| 243 | 243 |
| (...skipping 217 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 461 } | 461 } |
| 462 } | 462 } |
| 463 | 463 |
| 464 /// Traverses the CPS term and sets node.parent for each visited node. | 464 /// Traverses the CPS term and sets node.parent for each visited node. |
| 465 class ParentVisitor extends RecursiveVisitor { | 465 class ParentVisitor extends RecursiveVisitor { |
| 466 processFunctionDefinition(FunctionDefinition node) { | 466 processFunctionDefinition(FunctionDefinition node) { |
| 467 node.body.parent = node; | 467 node.body.parent = node; |
| 468 int index = 0; | 468 int index = 0; |
| 469 node.parameters.forEach((Definition parameter) { | 469 node.parameters.forEach((Definition parameter) { |
| 470 parameter.parent = node; | 470 parameter.parent = node; |
| 471 if (parameter is Parameter) parameter.parent_index = index++; | 471 if (parameter is Parameter) parameter.parentIndex = index++; |
| 472 }); | 472 }); |
| 473 } | 473 } |
| 474 | 474 |
| 475 processRunnableBody(RunnableBody node) { | 475 processRunnableBody(RunnableBody node) { |
| 476 node.returnContinuation.parent = node; | 476 node.returnContinuation.parent = node; |
| 477 node.body.parent = node; | 477 node.body.parent = node; |
| 478 } | 478 } |
| 479 | 479 |
| 480 processConstructorDefinition(ConstructorDefinition node) { | 480 processConstructorDefinition(ConstructorDefinition node) { |
| 481 node.body.parent = node; | 481 node.body.parent = node; |
| 482 int index = 0; | 482 int index = 0; |
| 483 node.parameters.forEach((Definition parameter) { | 483 node.parameters.forEach((Definition parameter) { |
| 484 parameter.parent = node; | 484 parameter.parent = node; |
| 485 if (parameter is Parameter) parameter.parent_index = index++; | 485 if (parameter is Parameter) parameter.parentIndex = index++; |
| 486 }); | 486 }); |
| 487 node.initializers.forEach((Initializer i) => i.parent = node); | 487 node.initializers.forEach((Initializer i) => i.parent = node); |
| 488 } | 488 } |
| 489 | 489 |
| 490 // Expressions. | 490 // Expressions. |
| 491 | 491 |
| 492 processFieldInitializer(FieldInitializer node) { | 492 processFieldInitializer(FieldInitializer node) { |
| 493 node.body.body.parent = node; | 493 node.body.body.parent = node; |
| 494 } | 494 } |
| 495 | 495 |
| (...skipping 84 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 580 | 580 |
| 581 processCreateFunction(CreateFunction node) { | 581 processCreateFunction(CreateFunction node) { |
| 582 node.definition.parent = node; | 582 node.definition.parent = node; |
| 583 } | 583 } |
| 584 | 584 |
| 585 processContinuation(Continuation node) { | 585 processContinuation(Continuation node) { |
| 586 if (node.body != null) node.body.parent = node; | 586 if (node.body != null) node.body.parent = node; |
| 587 int index = 0; | 587 int index = 0; |
| 588 node.parameters.forEach((Parameter parameter) { | 588 node.parameters.forEach((Parameter parameter) { |
| 589 parameter.parent = node; | 589 parameter.parent = node; |
| 590 parameter.parent_index = index++; | 590 parameter.parentIndex = index++; |
| 591 }); | 591 }); |
| 592 } | 592 } |
| 593 | 593 |
| 594 // Conditions. | 594 // Conditions. |
| 595 | 595 |
| 596 processIsTrue(IsTrue node) { | 596 processIsTrue(IsTrue node) { |
| 597 node.value.parent = node; | 597 node.value.parent = node; |
| 598 } | 598 } |
| 599 | 599 |
| 600 // JavaScript specific nodes. | 600 // JavaScript specific nodes. |
| (...skipping 62 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 663 } | 663 } |
| 664 | 664 |
| 665 String toString() => "$kind: $node"; | 665 String toString() => "$kind: $node"; |
| 666 } | 666 } |
| 667 | 667 |
| 668 /// A dummy class used solely to mark nodes as deleted once they are removed | 668 /// A dummy class used solely to mark nodes as deleted once they are removed |
| 669 /// from a term. | 669 /// from a term. |
| 670 class _DeletedNode extends Node { | 670 class _DeletedNode extends Node { |
| 671 accept(_) => null; | 671 accept(_) => null; |
| 672 } | 672 } |
| OLD | NEW |