| 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.optimizers; | 5 part of dart2js.cps_ir.optimizers; |
| 6 | 6 |
| 7 /** | 7 /** |
| 8 * Propagates constants throughout the IR, and replaces branches with fixed | 8 * Propagates constants throughout the IR, and replaces branches with fixed |
| 9 * jumps as well as side-effect free expressions with known constant results. | 9 * jumps as well as side-effect free expressions with known constant results. |
| 10 * Should be followed by the [ShrinkingReducer] pass. | 10 * Should be followed by the [ShrinkingReducer] pass. |
| 11 * | 11 * |
| 12 * Implemented according to 'Constant Propagation with Conditional Branches' | 12 * Implemented according to 'Constant Propagation with Conditional Branches' |
| 13 * by Wegman, Zadeck. | 13 * by Wegman, Zadeck. |
| 14 */ | 14 */ |
| 15 class ConstantPropagator extends Pass { | 15 class ConstantPropagator extends PassMixin { |
| 16 | 16 |
| 17 // Required for type determination in analysis of TypeOperator expressions. | 17 // Required for type determination in analysis of TypeOperator expressions. |
| 18 final dart2js.Compiler _compiler; | 18 final dart2js.Compiler _compiler; |
| 19 | 19 |
| 20 // The constant system is used for evaluation of expressions with constant | 20 // The constant system is used for evaluation of expressions with constant |
| 21 // arguments. | 21 // arguments. |
| 22 final dart2js.ConstantSystem _constantSystem; | 22 final dart2js.ConstantSystem _constantSystem; |
| 23 | 23 |
| 24 ConstantPropagator(this._compiler, this._constantSystem); | 24 ConstantPropagator(this._compiler, this._constantSystem); |
| 25 | 25 |
| 26 void _rewriteExecutableDefinition(ExecutableDefinition root) { | 26 @override |
| 27 void rewriteExecutableDefinition(ExecutableDefinition root) { |
| 27 // Set all parent pointers. | 28 // Set all parent pointers. |
| 28 new ParentVisitor().visit(root); | 29 new ParentVisitor().visit(root); |
| 29 | 30 |
| 30 // Analyze. In this phase, the entire term is analyzed for reachability | 31 // Analyze. In this phase, the entire term is analyzed for reachability |
| 31 // and the constant status of each expression. | 32 // and the constant status of each expression. |
| 32 | 33 |
| 33 _ConstPropagationVisitor analyzer = | 34 _ConstPropagationVisitor analyzer = |
| 34 new _ConstPropagationVisitor(_compiler, _constantSystem); | 35 new _ConstPropagationVisitor(_compiler, _constantSystem); |
| 35 analyzer.analyze(root); | 36 analyzer.analyze(root); |
| 36 | 37 |
| 37 // Transform. Uses the data acquired in the previous analysis phase to | 38 // Transform. Uses the data acquired in the previous analysis phase to |
| 38 // replace branches with fixed targets and side-effect-free expressions | 39 // replace branches with fixed targets and side-effect-free expressions |
| 39 // with constant results. | 40 // with constant results. |
| 40 | 41 |
| 41 _TransformingVisitor transformer = new _TransformingVisitor( | 42 _TransformingVisitor transformer = new _TransformingVisitor( |
| 42 analyzer.reachableNodes, analyzer.node2value); | 43 analyzer.reachableNodes, analyzer.node2value); |
| 43 transformer.transform(root); | 44 transformer.transform(root); |
| 44 } | 45 } |
| 45 | 46 |
| 46 void rewriteFunctionDefinition(FunctionDefinition root) { | |
| 47 if (root.isAbstract) return; | |
| 48 _rewriteExecutableDefinition(root); | |
| 49 } | |
| 50 | |
| 51 void rewriteFieldDefinition(FieldDefinition root) { | |
| 52 if (!root.hasInitializer) return; | |
| 53 _rewriteExecutableDefinition(root); | |
| 54 } | |
| 55 | |
| 56 } | 47 } |
| 57 | 48 |
| 58 /** | 49 /** |
| 59 * Uses the information from a preceding analysis pass in order to perform the | 50 * Uses the information from a preceding analysis pass in order to perform the |
| 60 * actual transformations on the CPS graph. | 51 * actual transformations on the CPS graph. |
| 61 */ | 52 */ |
| 62 class _TransformingVisitor extends RecursiveVisitor { | 53 class _TransformingVisitor extends RecursiveVisitor { |
| 63 | 54 |
| 64 final Set<Node> reachable; | 55 final Set<Node> reachable; |
| 65 final Map<Node, _ConstnessLattice> node2value; | 56 final Map<Node, _ConstnessLattice> node2value; |
| (...skipping 234 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 300 } | 291 } |
| 301 } | 292 } |
| 302 | 293 |
| 303 // -------------------------- Visitor overrides ------------------------------ | 294 // -------------------------- Visitor overrides ------------------------------ |
| 304 | 295 |
| 305 void visitNode(Node node) { | 296 void visitNode(Node node) { |
| 306 compiler.internalError(NO_LOCATION_SPANNABLE, | 297 compiler.internalError(NO_LOCATION_SPANNABLE, |
| 307 "_ConstPropagationVisitor is stale, add missing visit overrides"); | 298 "_ConstPropagationVisitor is stale, add missing visit overrides"); |
| 308 } | 299 } |
| 309 | 300 |
| 301 void visitRunnableBody(RunnableBody node) { |
| 302 setReachable(node.body); |
| 303 } |
| 304 |
| 310 void visitFunctionDefinition(FunctionDefinition node) { | 305 void visitFunctionDefinition(FunctionDefinition node) { |
| 311 node.parameters.forEach(visit); | 306 node.parameters.forEach(visit); |
| 312 setReachable(node.body); | 307 setReachable(node.body); |
| 313 } | 308 } |
| 314 | 309 |
| 315 void visitFieldDefinition(FieldDefinition node) { | 310 void visitFieldDefinition(FieldDefinition node) { |
| 316 if (node.hasInitializer) { | 311 if (node.hasInitializer) { |
| 317 setReachable(node.body); | 312 setReachable(node.body); |
| 318 } | 313 } |
| 319 } | 314 } |
| (...skipping 376 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 696 return that; | 691 return that; |
| 697 } | 692 } |
| 698 | 693 |
| 699 if (this.constant == that.constant) { | 694 if (this.constant == that.constant) { |
| 700 return this; | 695 return this; |
| 701 } | 696 } |
| 702 | 697 |
| 703 return NonConst; | 698 return NonConst; |
| 704 } | 699 } |
| 705 } | 700 } |
| OLD | NEW |