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 tree_ir.optimization; | 5 library tree_ir.optimization.logical_rewriter; |
| 6 |
| 7 import '../tree_ir_nodes.dart'; |
| 8 import 'optimization.dart' show Pass; |
| 9 import '../../constants/values.dart' as values; |
| 10 |
| 11 // TODO(asgerf): Update this class to use JS semantics for && and ||. |
6 | 12 |
7 /// Rewrites logical expressions to be more compact in the Tree IR. | 13 /// Rewrites logical expressions to be more compact in the Tree IR. |
8 /// | 14 /// |
9 /// In this class an expression is said to occur in "boolean context" if | 15 /// In this class an expression is said to occur in "boolean context" if |
10 /// its result is immediately applied to boolean conversion. | 16 /// its result is immediately applied to boolean conversion. |
11 /// | 17 /// |
12 /// IF STATEMENTS: | 18 /// IF STATEMENTS: |
13 /// | 19 /// |
14 /// We apply the following two rules to [If] statements (see [visitIf]). | 20 /// We apply the following two rules to [If] statements (see [visitIf]). |
15 /// | 21 /// |
(...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
52 /// x ? (y ? true : false) : false | 58 /// x ? (y ? true : false) : false |
53 /// x ? !!y : false (double negation introduced by [toBoolean]) | 59 /// x ? !!y : false (double negation introduced by [toBoolean]) |
54 /// x && !!y (!!y validated by [isBooleanValued]) | 60 /// x && !!y (!!y validated by [isBooleanValued]) |
55 /// x && y (double negation removed by [putInBooleanContext]) | 61 /// x && y (double negation removed by [putInBooleanContext]) |
56 /// | 62 /// |
57 class LogicalRewriter extends RecursiveTransformer | 63 class LogicalRewriter extends RecursiveTransformer |
58 implements Pass { | 64 implements Pass { |
59 String get passName => 'Logical rewriter'; | 65 String get passName => 'Logical rewriter'; |
60 | 66 |
61 @override | 67 @override |
62 void rewrite(RootNode node) { | 68 void rewrite(FunctionDefinition node) { |
63 node.replaceEachBody(visitStatement); | 69 node.body = visitStatement(node.body); |
64 } | 70 } |
65 | 71 |
66 /// Statement to be executed next by natural fallthrough. Although fallthrough | 72 /// Statement to be executed next by natural fallthrough. Although fallthrough |
67 /// is not introduced in this phase, we need to reason about fallthrough when | 73 /// is not introduced in this phase, we need to reason about fallthrough when |
68 /// evaluating the benefit of swapping the branches of an [If]. | 74 /// evaluating the benefit of swapping the branches of an [If]. |
69 Statement fallthrough; | 75 Statement fallthrough; |
70 | 76 |
71 @override | 77 @override |
72 void visitInnerFunction(FunctionDefinition node) { | 78 void visitInnerFunction(FunctionDefinition node) { |
73 new LogicalRewriter().rewrite(node); | 79 new LogicalRewriter().rewrite(node); |
(...skipping 264 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
338 | 344 |
339 Expression makeOr(Expression e1, Expression e2, {bool liftNots: true}) { | 345 Expression makeOr(Expression e1, Expression e2, {bool liftNots: true}) { |
340 if (e1 is Not && e2 is Not && liftNots) { | 346 if (e1 is Not && e2 is Not && liftNots) { |
341 return new Not(new LogicalOperator.and(e1.operand, e2.operand)); | 347 return new Not(new LogicalOperator.and(e1.operand, e2.operand)); |
342 } else { | 348 } else { |
343 return new LogicalOperator.or(e1, e2); | 349 return new LogicalOperator.or(e1, e2); |
344 } | 350 } |
345 } | 351 } |
346 } | 352 } |
347 | 353 |
OLD | NEW |