Chromium Code Reviews| 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"); |
|
Kevin Millikin (Google)
2012/10/25 14:44:08
Inline ==> Always inline
| |
| 27 DEFINE_FLAG(int, inlining_size_threshold, 20, | |
| 28 "Inline function calls that have up to threshold instruction"); | |
|
Kevin Millikin (Google)
2012/10/25 14:44:08
instruction ==> instructions
| |
| 29 DEFINE_FLAG(int, inlining_in_loop_size_threshold, 60, | |
| 30 "Inline function calls in loops that have up to threshold instructions"); | |
| 31 DEFINE_FLAG(int, inlining_callee_call_sites_threshold, 1, | |
| 32 "Inline function calls containing up to threshold call sites."); | |
|
Kevin Millikin (Google)
2012/10/25 14:44:08
"Always inline functions containing threshold or f
| |
| 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, 40, | |
| 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 DECLARE_FLAG(bool, reject_named_argument_as_positional); | 44 DECLARE_FLAG(bool, reject_named_argument_as_positional); |
| 33 | 45 |
| 34 #define TRACE_INLINING(statement) \ | 46 #define TRACE_INLINING(statement) \ |
| 35 do { \ | 47 do { \ |
| 36 if (FLAG_trace_inlining) statement; \ | 48 if (FLAG_trace_inlining) statement; \ |
| 37 } while (false) | 49 } while (false) |
| (...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 121 // Pair of an argument name and its value. | 133 // Pair of an argument name and its value. |
| 122 struct NamedArgument : ValueObject { | 134 struct NamedArgument : ValueObject { |
| 123 public: | 135 public: |
| 124 String* name; | 136 String* name; |
| 125 Value* value; | 137 Value* value; |
| 126 NamedArgument(String* name, Value* value) | 138 NamedArgument(String* name, Value* value) |
| 127 : name(name), value(value) { } | 139 : name(name), value(value) { } |
| 128 }; | 140 }; |
| 129 | 141 |
| 130 | 142 |
| 143 // Helper to collect information about a callee graph. | |
| 144 class GraphInfoCollector : public ValueObject { | |
| 145 public: | |
| 146 explicit 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 | |
| 131 // A collection of call sites to consider for inlining. | 178 // A collection of call sites to consider for inlining. |
| 132 class CallSites : public FlowGraphVisitor { | 179 class CallSites : public FlowGraphVisitor { |
| 133 public: | 180 public: |
| 134 explicit CallSites(FlowGraph* flow_graph) | 181 explicit CallSites(FlowGraph* flow_graph) |
| 135 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order. | 182 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order. |
| 136 static_calls_(), | 183 static_calls_(), |
| 137 closure_calls_(), | 184 closure_calls_(), |
| 138 instance_calls_() { } | 185 instance_calls_() { } |
| 139 | 186 |
| 140 GrowableArray<StaticCallInstr*>* static_calls() { | 187 GrowableArray<StaticCallInstr*>* static_calls() { |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 200 : caller_graph_(flow_graph), | 247 : caller_graph_(flow_graph), |
| 201 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), | 248 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), |
| 202 inlined_(false), | 249 inlined_(false), |
| 203 initial_size_(flow_graph->InstructionCount()), | 250 initial_size_(flow_graph->InstructionCount()), |
| 204 inlined_size_(0), | 251 inlined_size_(0), |
| 205 inlining_depth_(1), | 252 inlining_depth_(1), |
| 206 collected_call_sites_(NULL), | 253 collected_call_sites_(NULL), |
| 207 inlining_call_sites_(NULL), | 254 inlining_call_sites_(NULL), |
| 208 function_cache_() { } | 255 function_cache_() { } |
| 209 | 256 |
| 257 // Inlining heuristics based on Cooper et al. 2008. | |
| 258 bool ShouldWeInline(intptr_t loop_depth, | |
| 259 intptr_t size, | |
| 260 intptr_t call_sites, | |
| 261 intptr_t constant_args) { | |
| 262 return ((size <= FLAG_inlining_size_threshold) || | |
|
Kevin Millikin (Google)
2012/10/25 14:44:08
This might be easier to read and modify if it were
| |
| 263 (call_sites <= FLAG_inlining_callee_call_sites_threshold) || | |
| 264 ((loop_depth > 0) | |
| 265 && (size <= FLAG_inlining_in_loop_size_threshold)) || | |
| 266 ((constant_args >= FLAG_inlining_constant_arguments_count) | |
| 267 && (size <= FLAG_inlining_constant_arguments_size_threshold))); | |
| 268 } | |
| 269 | |
| 210 void InlineCalls() { | 270 void InlineCalls() { |
| 211 // If inlining depth is less then one abort. | 271 // If inlining depth is less then one abort. |
| 212 if (FLAG_inlining_depth_threshold < 1) return; | 272 if (FLAG_inlining_depth_threshold < 1) return; |
| 213 // Create two call site collections to swap between. | 273 // Create two call site collections to swap between. |
| 214 CallSites sites1(caller_graph_); | 274 CallSites sites1(caller_graph_); |
| 215 CallSites sites2(caller_graph_); | 275 CallSites sites2(caller_graph_); |
| 216 CallSites* call_sites_temp = NULL; | 276 CallSites* call_sites_temp = NULL; |
| 217 collected_call_sites_ = &sites1; | 277 collected_call_sites_ = &sites1; |
| 218 inlining_call_sites_ = &sites2; | 278 inlining_call_sites_ = &sites2; |
| 219 // Collect initial call sites. | 279 // Collect initial call sites. |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 313 } | 373 } |
| 314 | 374 |
| 315 // Load IC data for the callee. | 375 // Load IC data for the callee. |
| 316 if (function.HasCode()) { | 376 if (function.HasCode()) { |
| 317 const Code& unoptimized_code = | 377 const Code& unoptimized_code = |
| 318 Code::Handle(function.unoptimized_code()); | 378 Code::Handle(function.unoptimized_code()); |
| 319 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); | 379 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); |
| 320 } | 380 } |
| 321 | 381 |
| 322 // Build the callee graph. | 382 // Build the callee graph. |
| 383 const intptr_t loop_depth = call->GetBlock()->loop_depth(); | |
| 323 FlowGraphBuilder builder(*parsed_function); | 384 FlowGraphBuilder builder(*parsed_function); |
| 324 builder.SetInitialBlockId(caller_graph_->max_block_id()); | 385 builder.SetInitialBlockId(caller_graph_->max_block_id()); |
| 325 FlowGraph* callee_graph; | 386 FlowGraph* callee_graph; |
| 326 { | 387 { |
| 327 TimerScope timer(FLAG_compiler_stats, | 388 TimerScope timer(FLAG_compiler_stats, |
| 328 &CompilerStats::graphinliner_build_timer, | 389 &CompilerStats::graphinliner_build_timer, |
| 329 isolate); | 390 isolate); |
| 330 callee_graph = builder.BuildGraph(FlowGraphBuilder::kValueContext); | 391 callee_graph = |
| 392 builder.BuildGraph(FlowGraphBuilder::kValueContext, loop_depth); | |
| 331 } | 393 } |
| 332 | 394 |
| 333 // The parameter stubs are a copy of the actual arguments providing | 395 // The parameter stubs are a copy of the actual arguments providing |
| 334 // concrete information about the values, for example constant values, | 396 // concrete information about the values, for example constant values, |
| 335 // without linking between the caller and callee graphs. | 397 // without linking between the caller and callee graphs. |
| 336 // TODO(zerny): Put more information in the stubs, eg, type information. | 398 // TODO(zerny): Put more information in the stubs, eg, type information. |
| 337 GrowableArray<Definition*> param_stubs(function.NumParameters()); | 399 GrowableArray<Definition*> param_stubs(function.NumParameters()); |
| 338 | 400 |
| 339 // Create a parameter stub for each fixed positional parameter. | 401 // Create a parameter stub for each fixed positional parameter. |
| 340 for (intptr_t i = 0; i < function.num_fixed_parameters(); ++i) { | 402 for (intptr_t i = 0; i < function.num_fixed_parameters(); ++i) { |
| (...skipping 44 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 385 callee_graph->ComputeUseLists(); | 447 callee_graph->ComputeUseLists(); |
| 386 } | 448 } |
| 387 | 449 |
| 388 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | 450 if (FLAG_trace_inlining && FLAG_print_flow_graph) { |
| 389 OS::Print("Callee graph for inlining %s\n", | 451 OS::Print("Callee graph for inlining %s\n", |
| 390 function.ToFullyQualifiedCString()); | 452 function.ToFullyQualifiedCString()); |
| 391 FlowGraphPrinter printer(*callee_graph); | 453 FlowGraphPrinter printer(*callee_graph); |
| 392 printer.PrintBlocks(); | 454 printer.PrintBlocks(); |
| 393 } | 455 } |
| 394 | 456 |
| 395 // If result is more than size threshold then abort. | 457 // Collect information about the call site and caller graph. |
| 396 // TODO(zerny): Do this after CP and dead code elimination. | 458 // TODO(zerny): Do this after CP and dead code elimination. |
| 397 intptr_t size = callee_graph->InstructionCount(); | 459 intptr_t constants_count = 0; |
| 398 if (size > FLAG_inlining_size_threshold) { | 460 for (intptr_t i = 0; i < param_stubs.length(); ++i) { |
| 399 function.set_is_inlinable(false); | 461 if (param_stubs[i]->IsConstant()) ++constants_count; |
| 462 } | |
| 463 GraphInfoCollector info; | |
| 464 info.Collect(*callee_graph); | |
| 465 const intptr_t size = info.instruction_count(); | |
| 466 // Use heuristics do decide if this call should be inlined. | |
| 467 if (!ShouldWeInline(loop_depth, | |
| 468 size, | |
| 469 info.call_site_count(), | |
| 470 constants_count)) { | |
| 471 // If size is larger than all thresholds, don't consider it again. | |
| 472 if ((size > FLAG_inlining_size_threshold) && | |
| 473 (size > FLAG_inlining_in_loop_size_threshold) && | |
| 474 (size > FLAG_inlining_callee_call_sites_threshold) && | |
| 475 (size > FLAG_inlining_constant_arguments_size_threshold)) { | |
| 476 function.set_is_inlinable(false); | |
| 477 } | |
| 400 isolate->set_long_jump_base(base); | 478 isolate->set_long_jump_base(base); |
| 401 isolate->set_deopt_id(prev_deopt_id); | 479 isolate->set_deopt_id(prev_deopt_id); |
| 402 isolate->set_ic_data_array(prev_ic_data.raw()); | 480 isolate->set_ic_data_array(prev_ic_data.raw()); |
| 403 TRACE_INLINING(OS::Print(" Bailout: graph size %"Pd"\n", size)); | 481 TRACE_INLINING(OS::Print(" Bailout: heuristics with " |
| 482 "loop depth: %"Pd", " | |
| 483 "code size: %"Pd", " | |
| 484 "call sites: %"Pd", " | |
| 485 "const args: %"Pd"\n", | |
| 486 loop_depth, | |
| 487 size, | |
| 488 info.call_site_count(), | |
| 489 constants_count)); | |
| 404 return false; | 490 return false; |
| 405 } | 491 } |
| 406 | 492 |
| 407 // If depth is less or equal to threshold recursively add call sites. | 493 // If depth is less or equal to threshold recursively add call sites. |
| 408 if (inlining_depth_ < FLAG_inlining_depth_threshold) { | 494 if (inlining_depth_ < FLAG_inlining_depth_threshold) { |
| 409 collected_call_sites_->FindCallSites(callee_graph); | 495 collected_call_sites_->FindCallSites(callee_graph); |
| 410 } | 496 } |
| 411 | 497 |
| 412 { | 498 { |
| 413 TimerScope timer(FLAG_compiler_stats, | 499 TimerScope timer(FLAG_compiler_stats, |
| (...skipping 282 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 696 OS::Print("After Inlining of %s\n", flow_graph_-> | 782 OS::Print("After Inlining of %s\n", flow_graph_-> |
| 697 parsed_function().function().ToFullyQualifiedCString()); | 783 parsed_function().function().ToFullyQualifiedCString()); |
| 698 FlowGraphPrinter printer(*flow_graph_); | 784 FlowGraphPrinter printer(*flow_graph_); |
| 699 printer.PrintBlocks(); | 785 printer.PrintBlocks(); |
| 700 } | 786 } |
| 701 } | 787 } |
| 702 } | 788 } |
| 703 } | 789 } |
| 704 | 790 |
| 705 } // namespace dart | 791 } // namespace dart |
| OLD | NEW |