Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2015, 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 dart2js.cps_ir.share_interceptors; | 5 library dart2js.cps_ir.optimize_interceptors; |
| 6 | 6 |
| 7 import 'optimizers.dart'; | 7 import 'optimizers.dart'; |
| 8 import 'cps_ir_nodes.dart'; | 8 import 'cps_ir_nodes.dart'; |
| 9 import 'loop_hierarchy.dart'; | 9 import 'loop_hierarchy.dart'; |
| 10 import 'cps_fragment.dart'; | 10 import 'cps_fragment.dart'; |
| 11 import '../constants/values.dart'; | 11 import '../constants/values.dart'; |
| 12 import '../elements/elements.dart'; | 12 import '../elements/elements.dart'; |
| 13 import '../js_backend/backend_helpers.dart' show BackendHelpers; | 13 import '../js_backend/backend_helpers.dart' show BackendHelpers; |
| 14 import '../js_backend/js_backend.dart' show JavaScriptBackend; | 14 import '../js_backend/js_backend.dart' show JavaScriptBackend; |
| 15 import '../types/types.dart' show TypeMask; | 15 import '../types/types.dart' show TypeMask; |
| 16 import '../io/source_information.dart' show SourceInformation; | 16 import '../io/source_information.dart' show SourceInformation; |
| 17 | 17 |
| 18 /// Removes redundant `getInterceptor` calls. | 18 /// Replaces `getInterceptor` calls with interceptor constants when possible, |
| 19 /// | 19 /// or with "almost constant" expressions like "x && CONST" when the input |
| 20 /// The pass performs three optimizations for interceptors: | 20 /// is either null or has a known interceptor. |
| 21 ///- pull interceptors out of loops | 21 // |
| 22 ///- replace interceptors with constants | 22 // TODO(asgerf): Compute intercepted classes in this pass. |
| 23 ///- share interceptors when one is in scope of the other | 23 class OptimizeInterceptors extends TrampolineRecursiveVisitor implements Pass { |
| 24 class ShareInterceptors extends TrampolineRecursiveVisitor implements Pass { | 24 String get passName => 'Optimize interceptors'; |
| 25 String get passName => 'Share interceptors'; | |
| 26 | |
| 27 /// The innermost loop containing a given primitive. | |
| 28 final Map<Primitive, Continuation> loopHeaderFor = | |
| 29 <Primitive, Continuation>{}; | |
| 30 | |
| 31 /// An interceptor currently in scope for a given primitive. | |
| 32 final Map<Primitive, Interceptor> interceptorFor = <Primitive, Interceptor>{}; | |
| 33 | |
| 34 /// Interceptors that have been hoisted out of a given loop. | |
| 35 final Map<Continuation, List<Interceptor>> loopHoistedInterceptors = | |
| 36 <Continuation, List<Interceptor>>{}; | |
| 37 | 25 |
| 38 JavaScriptBackend backend; | 26 JavaScriptBackend backend; |
| 39 LoopHierarchy loopHierarchy; | 27 LoopHierarchy loopHierarchy; |
| 40 Continuation currentLoopHeader; | 28 Continuation currentLoopHeader; |
| 41 | 29 |
| 42 ShareInterceptors(this.backend); | 30 OptimizeInterceptors(this.backend); |
| 43 | 31 |
| 44 BackendHelpers get helpers => backend.helpers; | 32 BackendHelpers get helpers => backend.helpers; |
| 45 | 33 |
| 46 void rewrite(FunctionDefinition node) { | 34 void rewrite(FunctionDefinition node) { |
| 35 // TODO(asgerf): Computing the LoopHierarchy here may be overkill when all | |
| 36 // we want is to hoist constants out of loops. | |
| 47 loopHierarchy = new LoopHierarchy(node); | 37 loopHierarchy = new LoopHierarchy(node); |
| 48 visit(node.body); | 38 visit(node.body); |
| 49 new ShareConstants().visit(node); | 39 new ShareConstants().visit(node); |
| 50 } | 40 } |
| 51 | 41 |
| 52 @override | 42 @override |
| 53 Expression traverseContinuation(Continuation cont) { | 43 Expression traverseContinuation(Continuation cont) { |
| 54 Continuation oldLoopHeader = currentLoopHeader; | 44 Continuation oldLoopHeader = currentLoopHeader; |
| 55 currentLoopHeader = loopHierarchy.getLoopHeader(cont); | 45 currentLoopHeader = loopHierarchy.getLoopHeader(cont); |
| 56 for (Parameter param in cont.parameters) { | |
| 57 loopHeaderFor[param] = currentLoopHeader; | |
| 58 } | |
| 59 if (cont.isRecursive) { | |
| 60 pushAction(() { | |
| 61 // After the loop body has been processed, all interceptors hoisted | |
| 62 // to this loop fall out of scope and should be removed from the | |
| 63 // environment. | |
| 64 List<Interceptor> hoisted = loopHoistedInterceptors[cont]; | |
| 65 if (hoisted != null) { | |
| 66 for (Interceptor interceptor in hoisted) { | |
| 67 Primitive input = interceptor.input.definition; | |
| 68 assert(interceptorFor[input] == interceptor); | |
| 69 interceptorFor.remove(input); | |
| 70 constifyInterceptor(interceptor); | |
| 71 } | |
| 72 } | |
| 73 }); | |
| 74 } | |
| 75 pushAction(() { | 46 pushAction(() { |
| 76 currentLoopHeader = oldLoopHeader; | 47 currentLoopHeader = oldLoopHeader; |
| 77 }); | 48 }); |
| 78 return cont.body; | 49 return cont.body; |
| 79 } | 50 } |
| 80 | 51 |
| 81 /// If only one method table can be returned by the given interceptor, | 52 /// If only one method table can be returned by the given interceptor, |
| 82 /// returns a constant for that method table. | 53 /// returns a constant for that method table. |
| 83 InterceptorConstantValue getInterceptorConstant(Interceptor node) { | 54 InterceptorConstantValue getInterceptorConstant(Interceptor node) { |
| 84 if (node.interceptedClasses.length == 1 && | 55 if (node.interceptedClasses.length == 1 && |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 168 cps.context = cont; | 139 cps.context = cont; |
| 169 cps.insertAbove(let); | 140 cps.insertAbove(let); |
| 170 param.substituteFor(interceptor); | 141 param.substituteFor(interceptor); |
| 171 interceptor.destroy(); | 142 interceptor.destroy(); |
| 172 let.remove(); | 143 let.remove(); |
| 173 } | 144 } |
| 174 } | 145 } |
| 175 | 146 |
| 176 @override | 147 @override |
| 177 Expression traverseLetPrim(LetPrim node) { | 148 Expression traverseLetPrim(LetPrim node) { |
| 178 loopHeaderFor[node.primitive] = currentLoopHeader; | |
| 179 Expression next = node.body; | 149 Expression next = node.body; |
| 180 if (node.primitive is! Interceptor) { | 150 if (node.primitive is Interceptor) { |
| 181 return next; | 151 constifyInterceptor(node.primitive); |
| 182 } | 152 } |
| 183 Interceptor interceptor = node.primitive; | |
| 184 Primitive input = interceptor.input.definition; | |
| 185 | |
| 186 // Try to reuse an existing interceptor for the same input. | |
| 187 Interceptor existing = interceptorFor[input]; | |
| 188 if (existing != null) { | |
| 189 existing.interceptedClasses.addAll(interceptor.interceptedClasses); | |
| 190 existing.flags |= interceptor.flags; | |
| 191 existing.substituteFor(interceptor); | |
| 192 interceptor.destroy(); | |
| 193 node.remove(); | |
| 194 return next; | |
| 195 } | |
| 196 | |
| 197 // Put this interceptor in the environment. | |
| 198 interceptorFor[input] = interceptor; | |
| 199 | |
| 200 // Determine how far the interceptor can be lifted. The outermost loop | |
| 201 // that contains the input binding should also contain the interceptor | |
| 202 // binding. | |
| 203 Continuation referencedLoop = | |
| 204 lowestCommonAncestor(loopHeaderFor[input], currentLoopHeader); | |
| 205 if (referencedLoop != currentLoopHeader) { | |
| 206 Continuation hoistTarget = getCurrentOuterLoop(scope: referencedLoop); | |
| 207 LetCont loopBinding = hoistTarget.parent; | |
| 208 node.remove(); | |
| 209 node.insertAbove(loopBinding); | |
| 210 // Remove the interceptor from the environment after processing the loop. | |
| 211 loopHoistedInterceptors | |
| 212 .putIfAbsent(hoistTarget, () => <Interceptor>[]) | |
| 213 .add(interceptor); | |
| 214 } else { | |
| 215 // Remove the interceptor from the environment when it falls out of scope. | |
| 216 pushAction(() { | |
| 217 assert(interceptorFor[input] == interceptor); | |
| 218 interceptorFor.remove(input); | |
| 219 | |
| 220 // Now that the final set of intercepted classes has been seen, try to | |
| 221 // replace it with a constant. | |
| 222 constifyInterceptor(interceptor); | |
| 223 }); | |
| 224 } | |
| 225 | |
| 226 return next; | 153 return next; |
| 227 } | 154 } |
| 228 | |
| 229 /// Returns the the innermost loop that effectively encloses both | |
| 230 /// c1 and c2 (or `null` if there is no such loop). | |
| 231 Continuation lowestCommonAncestor(Continuation c1, Continuation c2) { | |
| 232 int d1 = getDepth(c1), d2 = getDepth(c2); | |
| 233 while (c1 != c2) { | |
| 234 if (d1 <= d2) { | |
| 235 c2 = loopHierarchy.getEnclosingLoop(c2); | |
| 236 d2 = getDepth(c2); | |
| 237 } else { | |
| 238 c1 = loopHierarchy.getEnclosingLoop(c1); | |
| 239 d1 = getDepth(c1); | |
| 240 } | |
| 241 } | |
| 242 return c1; | |
| 243 } | |
| 244 | |
| 245 int getDepth(Continuation loop) { | |
| 246 if (loop == null) return -1; | |
| 247 return loopHierarchy.loopDepth[loop]; | |
| 248 } | |
| 249 } | 155 } |
| 250 | 156 |
| 157 /// Shares interceptor constants when one is in scope of another. | |
| 158 /// | |
| 159 /// Interceptor optimization runs after GVN, hence this clean-up step is needed. | |
| 160 /// | |
| 161 /// TODO(asgerf): Handle in seperate constant optimization pass? With some other | |
|
sra1
2015/11/17 05:41:14
separate
asgerf
2015/11/17 12:43:43
Done.
| |
| 162 /// constant-related optimizations, like cloning small constants at use-site. | |
| 251 class ShareConstants extends TrampolineRecursiveVisitor { | 163 class ShareConstants extends TrampolineRecursiveVisitor { |
| 252 Map<ConstantValue, Constant> sharedConstantFor = <ConstantValue, Constant>{}; | 164 Map<ConstantValue, Constant> sharedConstantFor = <ConstantValue, Constant>{}; |
| 253 | 165 |
| 254 Expression traverseLetPrim(LetPrim node) { | 166 Expression traverseLetPrim(LetPrim node) { |
| 255 Expression next = node.body; | 167 Expression next = node.body; |
| 256 if (node.primitive is Constant && shouldShareConstant(node.primitive)) { | 168 if (node.primitive is Constant && shouldShareConstant(node.primitive)) { |
| 257 Constant prim = node.primitive; | 169 Constant prim = node.primitive; |
| 258 Constant existing = sharedConstantFor[prim.value]; | 170 Constant existing = sharedConstantFor[prim.value]; |
| 259 if (existing != null) { | 171 if (existing != null) { |
| 260 existing.substituteFor(prim); | 172 existing.substituteFor(prim); |
| 261 existing.useElementAsHint(prim.hint); | 173 existing.useElementAsHint(prim.hint); |
| 262 prim.destroy(); | 174 prim.destroy(); |
| 263 node.remove(); | 175 node.remove(); |
| 264 return next; | 176 return next; |
| 265 } | 177 } |
| 266 sharedConstantFor[prim.value] = prim; | 178 sharedConstantFor[prim.value] = prim; |
| 267 pushAction(() { | 179 pushAction(() { |
| 268 assert(sharedConstantFor[prim.value] == prim); | 180 assert(sharedConstantFor[prim.value] == prim); |
| 269 sharedConstantFor.remove(prim.value); | 181 sharedConstantFor.remove(prim.value); |
| 270 }); | 182 }); |
| 271 } | 183 } |
| 272 return next; | 184 return next; |
| 273 } | 185 } |
| 274 | 186 |
| 275 bool shouldShareConstant(Constant constant) { | 187 bool shouldShareConstant(Constant constant) { |
| 276 return constant.value.isInterceptor; | 188 return constant.value.isInterceptor; |
| 277 } | 189 } |
| 278 } | 190 } |
| OLD | NEW |