| 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 tree_ir.optimization.logical_rewriter; | 5 library tree_ir.optimization.logical_rewriter; |
| 6 | 6 |
| 7 import '../tree_ir_nodes.dart'; | 7 import '../tree_ir_nodes.dart'; |
| 8 import 'optimization.dart' show Pass; | 8 import 'optimization.dart' show Pass; |
| 9 import '../../constants/values.dart' as values; | 9 import '../../constants/values.dart' as values; |
| 10 | 10 |
| (...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 56 implements Pass { | 56 implements Pass { |
| 57 String get passName => 'Logical rewriter'; | 57 String get passName => 'Logical rewriter'; |
| 58 | 58 |
| 59 @override | 59 @override |
| 60 void rewrite(FunctionDefinition node) { | 60 void rewrite(FunctionDefinition node) { |
| 61 node.body = visitStatement(node.body); | 61 node.body = visitStatement(node.body); |
| 62 } | 62 } |
| 63 | 63 |
| 64 final FallthroughStack fallthrough = new FallthroughStack(); | 64 final FallthroughStack fallthrough = new FallthroughStack(); |
| 65 | 65 |
| 66 @override | |
| 67 void visitInnerFunction(FunctionDefinition node) { | |
| 68 new LogicalRewriter().rewrite(node); | |
| 69 } | |
| 70 | |
| 71 /// True if the given statement is equivalent to its fallthrough semantics. | 66 /// True if the given statement is equivalent to its fallthrough semantics. |
| 72 /// | 67 /// |
| 73 /// This means it will ultimately translate to an empty statement. | 68 /// This means it will ultimately translate to an empty statement. |
| 74 bool isFallthrough(Statement node) { | 69 bool isFallthrough(Statement node) { |
| 75 return node is Break && isFallthroughBreak(node) || | 70 return node is Break && isFallthroughBreak(node) || |
| 76 node is Continue && isFallthroughContinue(node) || | 71 node is Continue && isFallthroughContinue(node) || |
| 77 node is Return && isFallthroughReturn(node); | 72 node is Return && isFallthroughReturn(node); |
| 78 } | 73 } |
| 79 | 74 |
| 80 bool isFallthroughBreak(Break node) { | 75 bool isFallthroughBreak(Break node) { |
| (...skipping 455 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 536 } | 531 } |
| 537 | 532 |
| 538 Expression makeOr(Expression e1, Expression e2, {bool liftNots: true}) { | 533 Expression makeOr(Expression e1, Expression e2, {bool liftNots: true}) { |
| 539 if (e1 is Not && e2 is Not && liftNots) { | 534 if (e1 is Not && e2 is Not && liftNots) { |
| 540 return new Not(new LogicalOperator.and(e1.operand, e2.operand)); | 535 return new Not(new LogicalOperator.and(e1.operand, e2.operand)); |
| 541 } else { | 536 } else { |
| 542 return new LogicalOperator.or(e1, e2); | 537 return new LogicalOperator.or(e1, e2); |
| 543 } | 538 } |
| 544 } | 539 } |
| 545 | 540 |
| 546 /// True if [e2] is known to return the same value as [e1] | 541 /// True if [e2] is known to return the same value as [e1] |
| 547 /// (with no additional side effects) if evaluated immediately after [e1]. | 542 /// (with no additional side effects) if evaluated immediately after [e1]. |
| 548 /// | 543 /// |
| 549 /// Concretely, this is true if [e1] and [e2] are uses of the same variable, | 544 /// Concretely, this is true if [e1] and [e2] are uses of the same variable, |
| 550 /// or if [e2] is a use of a variable assigned by [e1]. | 545 /// or if [e2] is a use of a variable assigned by [e1]. |
| 551 bool isSameVariable(Expression e1, Expression e2) { | 546 bool isSameVariable(Expression e1, Expression e2) { |
| 552 if (e1 is VariableUse) { | 547 if (e1 is VariableUse) { |
| 553 return e2 is VariableUse && e1.variable == e2.variable; | 548 return e2 is VariableUse && e1.variable == e2.variable; |
| 554 } else if (e1 is Assign) { | 549 } else if (e1 is Assign) { |
| 555 return e2 is VariableUse && e1.variable == e2.variable; | 550 return e2 is VariableUse && e1.variable == e2.variable; |
| 556 } | 551 } |
| 557 return false; | 552 return false; |
| 558 } | 553 } |
| 559 | 554 |
| 560 void destroyVariableUse(VariableUse node) { | 555 void destroyVariableUse(VariableUse node) { |
| 561 --node.variable.readCount; | 556 --node.variable.readCount; |
| 562 } | 557 } |
| 563 } | 558 } |
| OLD | NEW |