OLD | NEW |
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, 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 #include "vm/flow_graph_inliner.h" | 5 #include "vm/flow_graph_inliner.h" |
6 | 6 |
7 #include "vm/compiler.h" | 7 #include "vm/compiler.h" |
8 #include "vm/flags.h" | 8 #include "vm/flags.h" |
9 #include "vm/flow_graph.h" | 9 #include "vm/flow_graph.h" |
10 #include "vm/flow_graph_builder.h" | 10 #include "vm/flow_graph_builder.h" |
(...skipping 29 matching lines...) Expand all Loading... |
40 static bool IsCallRecursive(const Function& function, Definition* call) { | 40 static bool IsCallRecursive(const Function& function, Definition* call) { |
41 Environment* env = call->env(); | 41 Environment* env = call->env(); |
42 while (env != NULL) { | 42 while (env != NULL) { |
43 if (function.raw() == env->function().raw()) return true; | 43 if (function.raw() == env->function().raw()) return true; |
44 env = env->outer(); | 44 env = env->outer(); |
45 } | 45 } |
46 return false; | 46 return false; |
47 } | 47 } |
48 | 48 |
49 | 49 |
| 50 // TODO(zerny): Remove the following classes once we have moved the label/join |
| 51 // map for control flow out of the AST an into the flow graph builder. |
| 52 |
| 53 // Default visitor to traverse child nodes. |
| 54 class ChildrenVisitor : public AstNodeVisitor { |
| 55 public: |
| 56 ChildrenVisitor() { } |
| 57 #define DEFINE_VISIT(type, name) \ |
| 58 virtual void Visit##type(type* node) { node->VisitChildren(this); } |
| 59 NODE_LIST(DEFINE_VISIT); |
| 60 #undef DEFINE_VISIT |
| 61 }; |
| 62 |
| 63 |
| 64 // Visitor to clear each AST node containing source labels. |
| 65 class SourceLabelResetter : public ChildrenVisitor { |
| 66 public: |
| 67 SourceLabelResetter() { } |
| 68 virtual void VisitSequenceNode(SequenceNode* node) { |
| 69 Reset(node, node->label()); |
| 70 } |
| 71 virtual void VisitCaseNode(CaseNode* node) { |
| 72 Reset(node, node->label()); |
| 73 } |
| 74 virtual void VisitSwitchNode(SwitchNode* node) { |
| 75 Reset(node, node->label()); |
| 76 } |
| 77 virtual void VisitWhileNode(WhileNode* node) { |
| 78 Reset(node, node->label()); |
| 79 } |
| 80 virtual void VisitDoWhileNode(DoWhileNode* node) { |
| 81 Reset(node, node->label()); |
| 82 } |
| 83 virtual void VisitForNode(ForNode* node) { |
| 84 Reset(node, node->label()); |
| 85 } |
| 86 virtual void VisitJumpNode(JumpNode* node) { |
| 87 Reset(node, node->label()); |
| 88 } |
| 89 void Reset(AstNode* node, SourceLabel* lbl) { |
| 90 node->VisitChildren(this); |
| 91 if (lbl == NULL) return; |
| 92 lbl->join_for_break_ = NULL; |
| 93 lbl->join_for_continue_ = NULL; |
| 94 } |
| 95 }; |
| 96 |
| 97 |
50 // A collection of call sites to consider for inlining. | 98 // A collection of call sites to consider for inlining. |
51 class CallSites : public FlowGraphVisitor { | 99 class CallSites : public FlowGraphVisitor { |
52 public: | 100 public: |
53 explicit CallSites(FlowGraph* flow_graph) | 101 explicit CallSites(FlowGraph* flow_graph) |
54 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order. | 102 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order. |
55 static_calls_(), | 103 static_calls_(), |
56 closure_calls_(), | 104 closure_calls_(), |
57 instance_calls_() { } | 105 instance_calls_() { } |
58 | 106 |
59 GrowableArray<StaticCallInstr*>* static_calls() { | 107 GrowableArray<StaticCallInstr*>* static_calls() { |
(...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
116 class CallSiteInliner : public ValueObject { | 164 class CallSiteInliner : public ValueObject { |
117 public: | 165 public: |
118 explicit CallSiteInliner(FlowGraph* flow_graph) | 166 explicit CallSiteInliner(FlowGraph* flow_graph) |
119 : caller_graph_(flow_graph), | 167 : caller_graph_(flow_graph), |
120 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), | 168 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), |
121 inlined_(false), | 169 inlined_(false), |
122 initial_size_(flow_graph->InstructionCount()), | 170 initial_size_(flow_graph->InstructionCount()), |
123 inlined_size_(0), | 171 inlined_size_(0), |
124 inlining_depth_(1), | 172 inlining_depth_(1), |
125 collected_call_sites_(NULL), | 173 collected_call_sites_(NULL), |
126 inlining_call_sites_(NULL) { } | 174 inlining_call_sites_(NULL), |
| 175 function_cache() { } |
127 | 176 |
128 void InlineCalls() { | 177 void InlineCalls() { |
129 // If inlining depth is less then one abort. | 178 // If inlining depth is less then one abort. |
130 if (FLAG_inlining_depth_threshold < 1) return; | 179 if (FLAG_inlining_depth_threshold < 1) return; |
131 // Create two call site collections to swap between. | 180 // Create two call site collections to swap between. |
132 CallSites sites1(caller_graph_); | 181 CallSites sites1(caller_graph_); |
133 CallSites sites2(caller_graph_); | 182 CallSites sites2(caller_graph_); |
134 CallSites* call_sites_temp = NULL; | 183 CallSites* call_sites_temp = NULL; |
135 collected_call_sites_ = &sites1; | 184 collected_call_sites_ = &sites1; |
136 inlining_call_sites_ = &sites2; | 185 inlining_call_sites_ = &sites2; |
(...skipping 75 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
212 isolate->set_ic_data_array(Array::null()); | 261 isolate->set_ic_data_array(Array::null()); |
213 // Save and clear deopt id. | 262 // Save and clear deopt id. |
214 const intptr_t prev_deopt_id = isolate->deopt_id(); | 263 const intptr_t prev_deopt_id = isolate->deopt_id(); |
215 isolate->set_deopt_id(0); | 264 isolate->set_deopt_id(0); |
216 // Install bailout jump. | 265 // Install bailout jump. |
217 LongJump* base = isolate->long_jump_base(); | 266 LongJump* base = isolate->long_jump_base(); |
218 LongJump jump; | 267 LongJump jump; |
219 isolate->set_long_jump_base(&jump); | 268 isolate->set_long_jump_base(&jump); |
220 if (setjmp(*jump.Set()) == 0) { | 269 if (setjmp(*jump.Set()) == 0) { |
221 // Parse the callee function. | 270 // Parse the callee function. |
222 ParsedFunction parsed_function(function); | 271 bool in_cache; |
223 Parser::ParseFunction(&parsed_function); | 272 ParsedFunction* parsed_function = ParseFunction(function, &in_cache); |
224 parsed_function.AllocateVariables(); | |
225 | 273 |
226 // Load IC data for the callee. | 274 // Load IC data for the callee. |
227 if (function.HasCode()) { | 275 if (function.HasCode()) { |
228 const Code& unoptimized_code = | 276 const Code& unoptimized_code = |
229 Code::Handle(function.unoptimized_code()); | 277 Code::Handle(function.unoptimized_code()); |
230 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); | 278 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); |
231 } | 279 } |
232 | 280 |
233 // Build the callee graph. | 281 // Build the callee graph. |
234 FlowGraphBuilder builder(parsed_function); | 282 FlowGraphBuilder builder(*parsed_function); |
235 builder.SetInitialBlockId(caller_graph_->max_block_id()); | 283 builder.SetInitialBlockId(caller_graph_->max_block_id()); |
236 FlowGraph* callee_graph = | 284 FlowGraph* callee_graph = |
237 builder.BuildGraph(FlowGraphBuilder::kValueContext); | 285 builder.BuildGraph(FlowGraphBuilder::kValueContext); |
238 | 286 |
239 // Abort if the callee graph contains control flow. | 287 // Abort if the callee graph contains control flow. |
240 if (!FLAG_inline_control_flow && | 288 if (!FLAG_inline_control_flow && |
241 (callee_graph->preorder().length() != 2)) { | 289 (callee_graph->preorder().length() != 2)) { |
242 function.set_is_inlinable(false); | 290 function.set_is_inlinable(false); |
243 isolate->set_long_jump_base(base); | 291 isolate->set_long_jump_base(base); |
244 isolate->set_ic_data_array(prev_ic_data.raw()); | 292 isolate->set_ic_data_array(prev_ic_data.raw()); |
245 TRACE_INLINING(OS::Print(" Bailout: control flow\n")); | 293 TRACE_INLINING(OS::Print(" Bailout: control flow\n")); |
246 return false; | 294 return false; |
247 } | 295 } |
248 | 296 |
249 // Compute SSA on the callee graph, catching bailouts. | 297 // Compute SSA on the callee graph, catching bailouts. |
250 callee_graph->ComputeSSA(next_ssa_temp_index_); | 298 callee_graph->ComputeSSA(next_ssa_temp_index_); |
251 callee_graph->ComputeUseLists(); | 299 callee_graph->ComputeUseLists(); |
252 | 300 |
253 // TODO(zerny): Do more optimization passes on the callee graph. | 301 // TODO(zerny): Do more optimization passes on the callee graph. |
254 FlowGraphOptimizer optimizer(callee_graph); | 302 FlowGraphOptimizer optimizer(callee_graph); |
255 optimizer.ApplyICData(); | 303 optimizer.ApplyICData(); |
256 callee_graph->ComputeUseLists(); | 304 callee_graph->ComputeUseLists(); |
257 | 305 |
258 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | 306 if (FLAG_trace_inlining && FLAG_print_flow_graph) { |
259 OS::Print("Callee graph for inlining %s\n", | 307 OS::Print("Callee graph for inlining %s\n", |
260 parsed_function.function().ToFullyQualifiedCString()); | 308 function.ToFullyQualifiedCString()); |
261 FlowGraphPrinter printer(*callee_graph); | 309 FlowGraphPrinter printer(*callee_graph); |
262 printer.PrintBlocks(); | 310 printer.PrintBlocks(); |
263 } | 311 } |
264 | 312 |
265 // If result is more than size threshold then abort. | 313 // If result is more than size threshold then abort. |
266 // TODO(zerny): Do this after CP and dead code elimination. | 314 // TODO(zerny): Do this after CP and dead code elimination. |
267 intptr_t size = callee_graph->InstructionCount(); | 315 intptr_t size = callee_graph->InstructionCount(); |
268 if (size > FLAG_inlining_size_threshold) { | 316 if (size > FLAG_inlining_size_threshold) { |
269 function.set_is_inlinable(false); | 317 function.set_is_inlinable(false); |
270 isolate->set_long_jump_base(base); | 318 isolate->set_long_jump_base(base); |
(...skipping 30 matching lines...) Expand all Loading... |
301 } | 349 } |
302 } | 350 } |
303 ASSERT(arg_index == arguments->length()); | 351 ASSERT(arg_index == arguments->length()); |
304 | 352 |
305 // Replace callee's null constant with caller's null constant. | 353 // Replace callee's null constant with caller's null constant. |
306 callee_graph->graph_entry()->constant_null()->ReplaceUsesWith( | 354 callee_graph->graph_entry()->constant_null()->ReplaceUsesWith( |
307 caller_graph_->graph_entry()->constant_null()); | 355 caller_graph_->graph_entry()->constant_null()); |
308 | 356 |
309 TRACE_INLINING(OS::Print(" Success\n")); | 357 TRACE_INLINING(OS::Print(" Success\n")); |
310 | 358 |
| 359 // Add the function to the cache. |
| 360 if (!in_cache) function_cache.Add(parsed_function); |
| 361 |
311 // Check that inlining maintains use lists. | 362 // Check that inlining maintains use lists. |
312 DEBUG_ASSERT(!FLAG_verify_compiler || caller_graph_->ValidateUseLists()); | 363 DEBUG_ASSERT(!FLAG_verify_compiler || caller_graph_->ValidateUseLists()); |
313 | 364 |
314 // Build succeeded so we restore the bailout jump. | 365 // Build succeeded so we restore the bailout jump. |
315 inlined_ = true; | 366 inlined_ = true; |
316 inlined_size_ += size; | 367 inlined_size_ += size; |
317 isolate->set_long_jump_base(base); | 368 isolate->set_long_jump_base(base); |
318 isolate->set_deopt_id(prev_deopt_id); | 369 isolate->set_deopt_id(prev_deopt_id); |
319 isolate->set_ic_data_array(prev_ic_data.raw()); | 370 isolate->set_ic_data_array(prev_ic_data.raw()); |
320 return true; | 371 return true; |
321 } else { | 372 } else { |
322 Error& error = Error::Handle(); | 373 Error& error = Error::Handle(); |
323 error = isolate->object_store()->sticky_error(); | 374 error = isolate->object_store()->sticky_error(); |
324 isolate->object_store()->clear_sticky_error(); | 375 isolate->object_store()->clear_sticky_error(); |
325 isolate->set_long_jump_base(base); | 376 isolate->set_long_jump_base(base); |
326 isolate->set_deopt_id(prev_deopt_id); | 377 isolate->set_deopt_id(prev_deopt_id); |
327 isolate->set_ic_data_array(prev_ic_data.raw()); | 378 isolate->set_ic_data_array(prev_ic_data.raw()); |
328 TRACE_INLINING(OS::Print(" Bailout: %s\n", error.ToErrorCString())); | 379 TRACE_INLINING(OS::Print(" Bailout: %s\n", error.ToErrorCString())); |
329 return false; | 380 return false; |
330 } | 381 } |
331 } | 382 } |
332 | 383 |
| 384 // Parse a function reusing the cache if possible. Returns true if the |
| 385 // function was in the cache. |
| 386 ParsedFunction* ParseFunction(const Function& function, bool* in_cache) { |
| 387 // TODO(zerny): Use a hash map for the cache. |
| 388 for (intptr_t i = 0; i < function_cache.length(); ++i) { |
| 389 ParsedFunction* parsed_function = function_cache[i]; |
| 390 if (parsed_function->function().raw() == function.raw()) { |
| 391 *in_cache = true; |
| 392 SourceLabelResetter reset; |
| 393 parsed_function->node_sequence()->Visit(&reset); |
| 394 return parsed_function; |
| 395 } |
| 396 } |
| 397 *in_cache = false; |
| 398 ParsedFunction* parsed_function = new ParsedFunction(function); |
| 399 Parser::ParseFunction(parsed_function); |
| 400 parsed_function->AllocateVariables(); |
| 401 return parsed_function; |
| 402 } |
| 403 |
333 void InlineStaticCalls() { | 404 void InlineStaticCalls() { |
334 const GrowableArray<StaticCallInstr*>& calls = | 405 const GrowableArray<StaticCallInstr*>& calls = |
335 *inlining_call_sites_->static_calls(); | 406 *inlining_call_sites_->static_calls(); |
336 TRACE_INLINING(OS::Print(" Static Calls (%d)\n", calls.length())); | 407 TRACE_INLINING(OS::Print(" Static Calls (%d)\n", calls.length())); |
337 for (intptr_t i = 0; i < calls.length(); ++i) { | 408 for (intptr_t i = 0; i < calls.length(); ++i) { |
338 StaticCallInstr* call = calls[i]; | 409 StaticCallInstr* call = calls[i]; |
339 GrowableArray<Value*> arguments(call->ArgumentCount()); | 410 GrowableArray<Value*> arguments(call->ArgumentCount()); |
340 for (int i = 0; i < call->ArgumentCount(); ++i) { | 411 for (int i = 0; i < call->ArgumentCount(); ++i) { |
341 arguments.Add(call->ArgumentAt(i)->value()); | 412 arguments.Add(call->ArgumentAt(i)->value()); |
342 } | 413 } |
(...skipping 26 matching lines...) Expand all Loading... |
369 void InlineInstanceCalls() { | 440 void InlineInstanceCalls() { |
370 const GrowableArray<PolymorphicInstanceCallInstr*>& calls = | 441 const GrowableArray<PolymorphicInstanceCallInstr*>& calls = |
371 *inlining_call_sites_->instance_calls(); | 442 *inlining_call_sites_->instance_calls(); |
372 TRACE_INLINING(OS::Print(" Polymorphic Instance Calls (%d)\n", | 443 TRACE_INLINING(OS::Print(" Polymorphic Instance Calls (%d)\n", |
373 calls.length())); | 444 calls.length())); |
374 for (intptr_t i = 0; i < calls.length(); ++i) { | 445 for (intptr_t i = 0; i < calls.length(); ++i) { |
375 PolymorphicInstanceCallInstr* instr = calls[i]; | 446 PolymorphicInstanceCallInstr* instr = calls[i]; |
376 const ICData& ic_data = instr->ic_data(); | 447 const ICData& ic_data = instr->ic_data(); |
377 const Function& target = Function::ZoneHandle(ic_data.GetTargetAt(0)); | 448 const Function& target = Function::ZoneHandle(ic_data.GetTargetAt(0)); |
378 if (instr->with_checks()) { | 449 if (instr->with_checks()) { |
379 TRACE_INLINING(OS::Print(" Bailout: %"Pd" checks target '%s'\n", | 450 TRACE_INLINING(OS::Print( |
380 ic_data.NumberOfChecks(), | 451 " => %s (deopt count %d)\n Bailout: %"Pd" checks\n", |
381 target.ToCString())); | 452 target.ToCString(), |
| 453 target.deoptimization_counter(), |
| 454 ic_data.NumberOfChecks())); |
382 continue; | 455 continue; |
383 } | 456 } |
384 GrowableArray<Value*> arguments(instr->ArgumentCount()); | 457 GrowableArray<Value*> arguments(instr->ArgumentCount()); |
385 for (int i = 0; i < instr->ArgumentCount(); ++i) { | 458 for (int i = 0; i < instr->ArgumentCount(); ++i) { |
386 arguments.Add(instr->ArgumentAt(i)->value()); | 459 arguments.Add(instr->ArgumentAt(i)->value()); |
387 } | 460 } |
388 TryInlining(target, &arguments, instr); | 461 TryInlining(target, &arguments, instr); |
389 } | 462 } |
390 } | 463 } |
391 | 464 |
392 FlowGraph* caller_graph_; | 465 FlowGraph* caller_graph_; |
393 intptr_t next_ssa_temp_index_; | 466 intptr_t next_ssa_temp_index_; |
394 bool inlined_; | 467 bool inlined_; |
395 intptr_t initial_size_; | 468 intptr_t initial_size_; |
396 intptr_t inlined_size_; | 469 intptr_t inlined_size_; |
397 intptr_t inlining_depth_; | 470 intptr_t inlining_depth_; |
398 CallSites* collected_call_sites_; | 471 CallSites* collected_call_sites_; |
399 CallSites* inlining_call_sites_; | 472 CallSites* inlining_call_sites_; |
| 473 GrowableArray<ParsedFunction*> function_cache; |
400 | 474 |
401 DISALLOW_COPY_AND_ASSIGN(CallSiteInliner); | 475 DISALLOW_COPY_AND_ASSIGN(CallSiteInliner); |
402 }; | 476 }; |
403 | 477 |
404 | 478 |
405 void FlowGraphInliner::Inline() { | 479 void FlowGraphInliner::Inline() { |
406 if ((FLAG_inlining_filter != NULL) && | 480 if ((FLAG_inlining_filter != NULL) && |
407 (strstr(flow_graph_-> | 481 (strstr(flow_graph_-> |
408 parsed_function().function().ToFullyQualifiedCString(), | 482 parsed_function().function().ToFullyQualifiedCString(), |
409 FLAG_inlining_filter) == NULL)) { | 483 FLAG_inlining_filter) == NULL)) { |
(...skipping 21 matching lines...) Expand all Loading... |
431 OS::Print("After Inlining of %s\n", flow_graph_-> | 505 OS::Print("After Inlining of %s\n", flow_graph_-> |
432 parsed_function().function().ToFullyQualifiedCString()); | 506 parsed_function().function().ToFullyQualifiedCString()); |
433 FlowGraphPrinter printer(*flow_graph_); | 507 FlowGraphPrinter printer(*flow_graph_); |
434 printer.PrintBlocks(); | 508 printer.PrintBlocks(); |
435 } | 509 } |
436 } | 510 } |
437 } | 511 } |
438 } | 512 } |
439 | 513 |
440 } // namespace dart | 514 } // namespace dart |
OLD | NEW |