Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(14)

Side by Side Diff: pkg/compiler/lib/src/tree_ir/optimization/loop_rewriter.dart

Issue 1068243002: Overhaul tree IR visitor and rename IR classes. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Add dummy use for RootVisitor and InitializerVisitor without arguments Created 5 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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 part of tree_ir.optimization;
6 6
7 /// Rewrites [WhileTrue] statements with an [If] body into a [WhileCondition], 7 /// Rewrites [WhileTrue] statements with an [If] body into a [WhileCondition],
8 /// in situations where only one of the branches contains a [Continue] to the 8 /// in situations where only one of the branches contains a [Continue] to the
9 /// loop. Schematically: 9 /// loop. Schematically:
10 /// 10 ///
11 /// L: 11 /// L:
12 /// while (true) { 12 /// while (true) {
13 /// if (E) { 13 /// if (E) {
14 /// S1 (has references to L) 14 /// S1 (has references to L)
15 /// } else { 15 /// } else {
16 /// S2 (has no references to L) 16 /// S2 (has no references to L)
17 /// } 17 /// }
18 /// } 18 /// }
19 /// ==> 19 /// ==>
20 /// L: 20 /// L:
21 /// while (E) { 21 /// while (E) {
22 /// S1 22 /// S1
23 /// }; 23 /// };
24 /// S2 24 /// S2
25 /// 25 ///
26 /// A similar transformation is used when S2 occurs in the 'then' position. 26 /// A similar transformation is used when S2 occurs in the 'then' position.
27 /// 27 ///
28 /// Note that the above pattern needs no iteration since nested ifs 28 /// Note that the above pattern needs no iteration since nested ifs
29 /// have been collapsed previously in the [StatementRewriter] phase. 29 /// have been collapsed previously in the [StatementRewriter] phase.
30 class LoopRewriter extends RecursiveVisitor with PassMixin { 30 class LoopRewriter extends RecursiveTransformer
31 implements Pass {
31 String get passName => 'Loop rewriter'; 32 String get passName => 'Loop rewriter';
32 33
33 Set<Label> usedContinueLabels = new Set<Label>(); 34 Set<Label> usedContinueLabels = new Set<Label>();
34 35
35 void rewriteExecutableDefinition(ExecutableDefinition root) { 36 void rewrite(RootNode root) {
36 root.body = visitStatement(root.body); 37 root.replaceEachBody(visitStatement);
37 } 38 }
38 39
39 Statement visitLabeledStatement(LabeledStatement node) { 40 @override
40 node.body = visitStatement(node.body); 41 void visitInnerFunction(FunctionDefinition node) {
41 node.next = visitStatement(node.next); 42 node.body = new LoopRewriter().visitStatement(node.body);
42 return node;
43 }
44
45 Statement visitAssign(Assign node) {
46 visitExpression(node.value);
47 node.next = visitStatement(node.next);
48 return node;
49 }
50
51 Statement visitReturn(Return node) {
52 visitExpression(node.value);
53 return node;
54 }
55
56 Statement visitBreak(Break node) {
57 return node;
58 } 43 }
59 44
60 Statement visitContinue(Continue node) { 45 Statement visitContinue(Continue node) {
61 usedContinueLabels.add(node.target); 46 usedContinueLabels.add(node.target);
62 return node; 47 return node;
63 } 48 }
64 49
65 Statement visitIf(If node) {
66 visitExpression(node.condition);
67 node.thenStatement = visitStatement(node.thenStatement);
68 node.elseStatement = visitStatement(node.elseStatement);
69 return node;
70 }
71
72 Statement visitWhileTrue(WhileTrue node) { 50 Statement visitWhileTrue(WhileTrue node) {
73 assert(!usedContinueLabels.contains(node.label)); 51 assert(!usedContinueLabels.contains(node.label));
74 if (node.body is If) { 52 if (node.body is If) {
75 If body = node.body; 53 If body = node.body;
76 body.thenStatement = visitStatement(body.thenStatement); 54 body.thenStatement = visitStatement(body.thenStatement);
77 bool thenHasContinue = usedContinueLabels.remove(node.label); 55 bool thenHasContinue = usedContinueLabels.remove(node.label);
78 body.elseStatement = visitStatement(body.elseStatement); 56 body.elseStatement = visitStatement(body.elseStatement);
79 bool elseHasContinue = usedContinueLabels.remove(node.label); 57 bool elseHasContinue = usedContinueLabels.remove(node.label);
80 if (thenHasContinue && !elseHasContinue) { 58 if (thenHasContinue && !elseHasContinue) {
81 node.label.binding = null; // Prepare to rebind the label. 59 node.label.binding = null; // Prepare to rebind the label.
82 return new WhileCondition( 60 return new WhileCondition(
83 node.label, 61 node.label,
84 body.condition, 62 body.condition,
85 body.thenStatement, 63 body.thenStatement,
86 body.elseStatement); 64 body.elseStatement);
87 } else if (!thenHasContinue && elseHasContinue) { 65 } else if (!thenHasContinue && elseHasContinue) {
88 node.label.binding = null; 66 node.label.binding = null;
89 return new WhileCondition( 67 return new WhileCondition(
90 node.label, 68 node.label,
91 new Not(body.condition), 69 new Not(body.condition),
92 body.elseStatement, 70 body.elseStatement,
93 body.thenStatement); 71 body.thenStatement);
94 } 72 }
95 } else { 73 } else {
96 node.body = visitStatement(node.body); 74 node.body = visitStatement(node.body);
97 usedContinueLabels.remove(node.label); 75 usedContinueLabels.remove(node.label);
98 } 76 }
99 return node; 77 return node;
100 } 78 }
101
102 Statement visitWhileCondition(WhileCondition node) {
103 // Note: not reachable but the implementation is trivial
104 visitExpression(node.condition);
105 node.body = visitStatement(node.body);
106 node.next = visitStatement(node.next);
107 return node;
108 }
109
110 Statement visitExpressionStatement(ExpressionStatement node) {
111 visitExpression(node.expression);
112 node.next = visitStatement(node.next);
113 return node;
114 }
115
116 Statement visitTry(Try node) {
117 node.tryBody = visitStatement(node.tryBody);
118 node.catchBody = visitStatement(node.catchBody);
119 return node;
120 }
121
122 Statement visitFunctionDeclaration(FunctionDeclaration node) {
123 new LoopRewriter().rewrite(node.definition);
124 node.next = visitStatement(node.next);
125 return node;
126 }
127
128 void visitFunctionExpression(FunctionExpression node) {
129 new LoopRewriter().rewrite(node.definition);
130 }
131
132 Statement visitSetField(SetField node) {
133 visitExpression(node.object);
134 visitExpression(node.value);
135 node.next = visitStatement(node.next);
136 return node;
137 }
138
139 } 79 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698