| 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" |
| 11 #include "vm/flow_graph_optimizer.h" | 11 #include "vm/flow_graph_optimizer.h" |
| 12 #include "vm/il_printer.h" | 12 #include "vm/il_printer.h" |
| 13 #include "vm/intrinsifier.h" | 13 #include "vm/intrinsifier.h" |
| 14 #include "vm/longjump.h" | 14 #include "vm/longjump.h" |
| 15 #include "vm/object.h" | 15 #include "vm/object.h" |
| 16 #include "vm/object_store.h" | 16 #include "vm/object_store.h" |
| 17 #include "vm/timer.h" | 17 #include "vm/timer.h" |
| 18 | 18 |
| 19 namespace dart { | 19 namespace dart { |
| 20 | 20 |
| 21 DEFINE_FLAG(bool, trace_inlining, false, "Trace inlining"); | 21 DEFINE_FLAG(bool, trace_inlining, false, "Trace inlining"); |
| 22 DEFINE_FLAG(charp, inlining_filter, NULL, "Inline only in named function"); | 22 DEFINE_FLAG(charp, inlining_filter, NULL, "Inline only in named function"); |
| 23 DEFINE_FLAG(int, inlining_size_threshold, 50, | 23 |
| 24 "Inline only functions with up to threshold instructions (default 50)"); | 24 // Flags for inlining heuristics. |
| 25 // TODO(srdjan): set to 3 once crash in apidoc.dart is resolved. | |
| 26 DEFINE_FLAG(int, inlining_depth_threshold, 3, | 25 DEFINE_FLAG(int, inlining_depth_threshold, 3, |
| 27 "Inline recursively up to threshold depth (default 3)"); | 26 "Inline function calls up to threshold nesting depth"); |
| 27 DEFINE_FLAG(int, inlining_size_threshold, 20, |
| 28 "Always inline functions that have threshold or fewer instructions"); |
| 29 DEFINE_FLAG(int, inlining_in_loop_size_threshold, 80, |
| 30 "Inline functions in loops that have threshold or fewer instructions"); |
| 31 DEFINE_FLAG(int, inlining_callee_call_sites_threshold, 1, |
| 32 "Always inline functions containing threshold or fewer calls."); |
| 33 DEFINE_FLAG(int, inlining_constant_arguments_count, 1, |
| 34 "Inline function calls with sufficient constant arguments " |
| 35 "and up to the increased threshold on instructions"); |
| 36 DEFINE_FLAG(int, inlining_constant_arguments_size_threshold, 60, |
| 37 "Inline function calls with sufficient constant arguments " |
| 38 "and up to the increased threshold on instructions"); |
| 39 |
| 28 DECLARE_FLAG(bool, print_flow_graph); | 40 DECLARE_FLAG(bool, print_flow_graph); |
| 29 DECLARE_FLAG(int, deoptimization_counter_threshold); | 41 DECLARE_FLAG(int, deoptimization_counter_threshold); |
| 30 DECLARE_FLAG(bool, verify_compiler); | 42 DECLARE_FLAG(bool, verify_compiler); |
| 31 DECLARE_FLAG(bool, compiler_stats); | 43 DECLARE_FLAG(bool, compiler_stats); |
| 32 | 44 |
| 33 #define TRACE_INLINING(statement) \ | 45 #define TRACE_INLINING(statement) \ |
| 34 do { \ | 46 do { \ |
| 35 if (FLAG_trace_inlining) statement; \ | 47 if (FLAG_trace_inlining) statement; \ |
| 36 } while (false) | 48 } while (false) |
| 37 | 49 |
| (...skipping 82 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 120 // Pair of an argument name and its value. | 132 // Pair of an argument name and its value. |
| 121 struct NamedArgument : ValueObject { | 133 struct NamedArgument : ValueObject { |
| 122 public: | 134 public: |
| 123 String* name; | 135 String* name; |
| 124 Value* value; | 136 Value* value; |
| 125 NamedArgument(String* name, Value* value) | 137 NamedArgument(String* name, Value* value) |
| 126 : name(name), value(value) { } | 138 : name(name), value(value) { } |
| 127 }; | 139 }; |
| 128 | 140 |
| 129 | 141 |
| 142 // Helper to collect information about a callee graph when considering it for |
| 143 // inlining. |
| 144 class GraphInfoCollector : public ValueObject { |
| 145 public: |
| 146 GraphInfoCollector() |
| 147 : call_site_count_(0), |
| 148 instruction_count_(0) { } |
| 149 |
| 150 void Collect(const FlowGraph& graph) { |
| 151 call_site_count_ = 0; |
| 152 instruction_count_ = 0; |
| 153 for (BlockIterator block_it = graph.postorder_iterator(); |
| 154 !block_it.Done(); |
| 155 block_it.Advance()) { |
| 156 for (ForwardInstructionIterator it(block_it.Current()); |
| 157 !it.Done(); |
| 158 it.Advance()) { |
| 159 ++instruction_count_; |
| 160 if (it.Current()->IsStaticCall() || |
| 161 it.Current()->IsClosureCall() || |
| 162 it.Current()->IsPolymorphicInstanceCall()) { |
| 163 ++call_site_count_; |
| 164 } |
| 165 } |
| 166 } |
| 167 } |
| 168 |
| 169 intptr_t call_site_count() const { return call_site_count_; } |
| 170 intptr_t instruction_count() const { return instruction_count_; } |
| 171 |
| 172 private: |
| 173 intptr_t call_site_count_; |
| 174 intptr_t instruction_count_; |
| 175 }; |
| 176 |
| 177 |
| 130 // A collection of call sites to consider for inlining. | 178 // A collection of call sites to consider for inlining. |
| 131 class CallSites : public FlowGraphVisitor { | 179 class CallSites : public FlowGraphVisitor { |
| 132 public: | 180 public: |
| 133 explicit CallSites(FlowGraph* flow_graph) | 181 explicit CallSites(FlowGraph* flow_graph) |
| 134 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order. | 182 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order. |
| 135 static_calls_(), | 183 static_calls_(), |
| 136 closure_calls_(), | 184 closure_calls_(), |
| 137 instance_calls_() { } | 185 instance_calls_() { } |
| 138 | 186 |
| 139 GrowableArray<StaticCallInstr*>* static_calls() { | 187 GrowableArray<StaticCallInstr*>* static_calls() { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 199 : caller_graph_(flow_graph), | 247 : caller_graph_(flow_graph), |
| 200 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), | 248 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), |
| 201 inlined_(false), | 249 inlined_(false), |
| 202 initial_size_(flow_graph->InstructionCount()), | 250 initial_size_(flow_graph->InstructionCount()), |
| 203 inlined_size_(0), | 251 inlined_size_(0), |
| 204 inlining_depth_(1), | 252 inlining_depth_(1), |
| 205 collected_call_sites_(NULL), | 253 collected_call_sites_(NULL), |
| 206 inlining_call_sites_(NULL), | 254 inlining_call_sites_(NULL), |
| 207 function_cache_() { } | 255 function_cache_() { } |
| 208 | 256 |
| 257 // Inlining heuristics based on Cooper et al. 2008. |
| 258 bool ShouldWeInline(intptr_t loop_depth, |
| 259 intptr_t instr_count, |
| 260 intptr_t call_site_count, |
| 261 intptr_t const_arg_count) { |
| 262 if (instr_count <= FLAG_inlining_size_threshold) { |
| 263 return true; |
| 264 } |
| 265 if (call_site_count <= FLAG_inlining_callee_call_sites_threshold) { |
| 266 return true; |
| 267 } |
| 268 if ((loop_depth > 0) && |
| 269 (instr_count <= FLAG_inlining_in_loop_size_threshold)) { |
| 270 return true; |
| 271 } |
| 272 if ((const_arg_count >= FLAG_inlining_constant_arguments_count) && |
| 273 (instr_count <= FLAG_inlining_constant_arguments_size_threshold)) { |
| 274 return true; |
| 275 } |
| 276 return false; |
| 277 } |
| 278 |
| 209 void InlineCalls() { | 279 void InlineCalls() { |
| 210 // If inlining depth is less then one abort. | 280 // If inlining depth is less then one abort. |
| 211 if (FLAG_inlining_depth_threshold < 1) return; | 281 if (FLAG_inlining_depth_threshold < 1) return; |
| 212 // Create two call site collections to swap between. | 282 // Create two call site collections to swap between. |
| 213 CallSites sites1(caller_graph_); | 283 CallSites sites1(caller_graph_); |
| 214 CallSites sites2(caller_graph_); | 284 CallSites sites2(caller_graph_); |
| 215 CallSites* call_sites_temp = NULL; | 285 CallSites* call_sites_temp = NULL; |
| 216 collected_call_sites_ = &sites1; | 286 collected_call_sites_ = &sites1; |
| 217 inlining_call_sites_ = &sites2; | 287 inlining_call_sites_ = &sites2; |
| 218 // Collect initial call sites. | 288 // Collect initial call sites. |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 302 } | 372 } |
| 303 | 373 |
| 304 // Load IC data for the callee. | 374 // Load IC data for the callee. |
| 305 if (function.HasCode()) { | 375 if (function.HasCode()) { |
| 306 const Code& unoptimized_code = | 376 const Code& unoptimized_code = |
| 307 Code::Handle(function.unoptimized_code()); | 377 Code::Handle(function.unoptimized_code()); |
| 308 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); | 378 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); |
| 309 } | 379 } |
| 310 | 380 |
| 311 // Build the callee graph. | 381 // Build the callee graph. |
| 382 const intptr_t loop_depth = call->GetBlock()->loop_depth(); |
| 312 FlowGraphBuilder builder(*parsed_function); | 383 FlowGraphBuilder builder(*parsed_function); |
| 313 builder.SetInitialBlockId(caller_graph_->max_block_id()); | 384 builder.SetInitialBlockId(caller_graph_->max_block_id()); |
| 314 FlowGraph* callee_graph; | 385 FlowGraph* callee_graph; |
| 315 { | 386 { |
| 316 TimerScope timer(FLAG_compiler_stats, | 387 TimerScope timer(FLAG_compiler_stats, |
| 317 &CompilerStats::graphinliner_build_timer, | 388 &CompilerStats::graphinliner_build_timer, |
| 318 isolate); | 389 isolate); |
| 319 callee_graph = builder.BuildGraph(FlowGraphBuilder::kValueContext); | 390 callee_graph = |
| 391 builder.BuildGraph(FlowGraphBuilder::kValueContext, loop_depth); |
| 320 } | 392 } |
| 321 | 393 |
| 322 // The parameter stubs are a copy of the actual arguments providing | 394 // The parameter stubs are a copy of the actual arguments providing |
| 323 // concrete information about the values, for example constant values, | 395 // concrete information about the values, for example constant values, |
| 324 // without linking between the caller and callee graphs. | 396 // without linking between the caller and callee graphs. |
| 325 // TODO(zerny): Put more information in the stubs, eg, type information. | 397 // TODO(zerny): Put more information in the stubs, eg, type information. |
| 326 GrowableArray<Definition*> param_stubs(function.NumParameters()); | 398 GrowableArray<Definition*> param_stubs(function.NumParameters()); |
| 327 | 399 |
| 328 // Create a parameter stub for each fixed positional parameter. | 400 // Create a parameter stub for each fixed positional parameter. |
| 329 for (intptr_t i = 0; i < function.num_fixed_parameters(); ++i) { | 401 for (intptr_t i = 0; i < function.num_fixed_parameters(); ++i) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 374 callee_graph->ComputeUseLists(); | 446 callee_graph->ComputeUseLists(); |
| 375 } | 447 } |
| 376 | 448 |
| 377 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | 449 if (FLAG_trace_inlining && FLAG_print_flow_graph) { |
| 378 OS::Print("Callee graph for inlining %s\n", | 450 OS::Print("Callee graph for inlining %s\n", |
| 379 function.ToFullyQualifiedCString()); | 451 function.ToFullyQualifiedCString()); |
| 380 FlowGraphPrinter printer(*callee_graph); | 452 FlowGraphPrinter printer(*callee_graph); |
| 381 printer.PrintBlocks(); | 453 printer.PrintBlocks(); |
| 382 } | 454 } |
| 383 | 455 |
| 384 // If result is more than size threshold then abort. | 456 // Collect information about the call site and caller graph. |
| 385 // TODO(zerny): Do this after CP and dead code elimination. | 457 // TODO(zerny): Do this after CP and dead code elimination. |
| 386 intptr_t size = callee_graph->InstructionCount(); | 458 intptr_t constants_count = 0; |
| 387 if (size > FLAG_inlining_size_threshold) { | 459 for (intptr_t i = 0; i < param_stubs.length(); ++i) { |
| 388 function.set_is_inlinable(false); | 460 if (param_stubs[i]->IsConstant()) ++constants_count; |
| 461 } |
| 462 GraphInfoCollector info; |
| 463 info.Collect(*callee_graph); |
| 464 const intptr_t size = info.instruction_count(); |
| 465 // Use heuristics do decide if this call should be inlined. |
| 466 if (!ShouldWeInline(loop_depth, |
| 467 size, |
| 468 info.call_site_count(), |
| 469 constants_count)) { |
| 470 // If size is larger than all thresholds, don't consider it again. |
| 471 if ((size > FLAG_inlining_size_threshold) && |
| 472 (size > FLAG_inlining_in_loop_size_threshold) && |
| 473 (size > FLAG_inlining_callee_call_sites_threshold) && |
| 474 (size > FLAG_inlining_constant_arguments_size_threshold)) { |
| 475 function.set_is_inlinable(false); |
| 476 } |
| 389 isolate->set_long_jump_base(base); | 477 isolate->set_long_jump_base(base); |
| 390 isolate->set_deopt_id(prev_deopt_id); | 478 isolate->set_deopt_id(prev_deopt_id); |
| 391 isolate->set_ic_data_array(prev_ic_data.raw()); | 479 isolate->set_ic_data_array(prev_ic_data.raw()); |
| 392 TRACE_INLINING(OS::Print(" Bailout: graph size %"Pd"\n", size)); | 480 TRACE_INLINING(OS::Print(" Bailout: heuristics with " |
| 481 "loop depth: %"Pd", " |
| 482 "code size: %"Pd", " |
| 483 "call sites: %"Pd", " |
| 484 "const args: %"Pd"\n", |
| 485 loop_depth, |
| 486 size, |
| 487 info.call_site_count(), |
| 488 constants_count)); |
| 393 return false; | 489 return false; |
| 394 } | 490 } |
| 395 | 491 |
| 396 // If depth is less or equal to threshold recursively add call sites. | 492 // If depth is less or equal to threshold recursively add call sites. |
| 397 if (inlining_depth_ < FLAG_inlining_depth_threshold) { | 493 if (inlining_depth_ < FLAG_inlining_depth_threshold) { |
| 398 collected_call_sites_->FindCallSites(callee_graph); | 494 collected_call_sites_->FindCallSites(callee_graph); |
| 399 } | 495 } |
| 400 | 496 |
| 401 { | 497 { |
| 402 TimerScope timer(FLAG_compiler_stats, | 498 TimerScope timer(FLAG_compiler_stats, |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 685 OS::Print("After Inlining of %s\n", flow_graph_-> | 781 OS::Print("After Inlining of %s\n", flow_graph_-> |
| 686 parsed_function().function().ToFullyQualifiedCString()); | 782 parsed_function().function().ToFullyQualifiedCString()); |
| 687 FlowGraphPrinter printer(*flow_graph_); | 783 FlowGraphPrinter printer(*flow_graph_); |
| 688 printer.PrintBlocks(); | 784 printer.PrintBlocks(); |
| 689 } | 785 } |
| 690 } | 786 } |
| 691 } | 787 } |
| 692 } | 788 } |
| 693 | 789 |
| 694 } // namespace dart | 790 } // namespace dart |
| OLD | NEW |