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

Side by Side Diff: runtime/vm/flow_graph_inliner.cc

Issue 11029027: Recursive inlining. (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 8 years, 2 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
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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 17
18 namespace dart { 18 namespace dart {
19 19
20 DEFINE_FLAG(bool, trace_inlining, false, "Trace inlining"); 20 DEFINE_FLAG(bool, trace_inlining, false, "Trace inlining");
21 DEFINE_FLAG(charp, inlining_filter, NULL, "Inline only in named function"); 21 DEFINE_FLAG(charp, inlining_filter, NULL, "Inline only in named function");
22 DEFINE_FLAG(int, inlining_size_threshold, 250, 22 DEFINE_FLAG(int, inlining_size_threshold, 250,
23 "Inline only functions with up to threshold instructions"); 23 "Inline only functions with up to threshold instructions (default 250)");
24 DEFINE_FLAG(int, inlining_depth_threshold, 1,
25 "Inline recursively up to threshold depth (default 1)");
24 DEFINE_FLAG(bool, inline_control_flow, true, 26 DEFINE_FLAG(bool, inline_control_flow, true,
25 "Inline functions with control flow."); 27 "Inline functions with control flow.");
26 DECLARE_FLAG(bool, print_flow_graph); 28 DECLARE_FLAG(bool, print_flow_graph);
27 DECLARE_FLAG(int, deoptimization_counter_threshold); 29 DECLARE_FLAG(int, deoptimization_counter_threshold);
28 DECLARE_FLAG(bool, verify_compiler); 30 DECLARE_FLAG(bool, verify_compiler);
29 31
30 #define TRACE_INLINING(statement) \ 32 #define TRACE_INLINING(statement) \
31 do { \ 33 do { \
32 if (FLAG_trace_inlining) statement; \ 34 if (FLAG_trace_inlining) statement; \
33 } while (false) 35 } while (false)
34 36
35 37
36 // Test if a call is recursive by looking in the deoptimization environment. 38 // Test if a call is recursive by looking in the deoptimization environment.
37 static bool IsCallRecursive(const Function& function, Definition* call) { 39 static bool IsCallRecursive(const Function& function, Definition* call) {
38 Environment* env = call->env(); 40 Environment* env = call->env();
39 while (env != NULL) { 41 while (env != NULL) {
40 if (function.raw() == env->function().raw()) return true; 42 if (function.raw() == env->function().raw()) return true;
41 env = env->outer(); 43 env = env->outer();
42 } 44 }
43 return false; 45 return false;
44 } 46 }
45 47
46 48
47 class CallSiteInliner : public FlowGraphVisitor { 49 // A collection of call sites to consider for inlining.
50 class CallSites : public FlowGraphVisitor {
48 public: 51 public:
49 explicit CallSiteInliner(FlowGraph* flow_graph) 52 explicit CallSites(FlowGraph* flow_graph)
50 : FlowGraphVisitor(flow_graph->postorder()), 53 : FlowGraphVisitor(flow_graph->postorder()), // We don't use this order.
51 caller_graph_(flow_graph),
52 next_ssa_temp_index_(flow_graph->max_virtual_register_number()),
53 inlined_(false),
54 initial_size_(flow_graph->InstructionCount()),
55 inlined_size_(0),
56 static_calls_(), 54 static_calls_(),
57 closure_calls_(), 55 closure_calls_(),
58 instance_calls_() { } 56 instance_calls_() { }
59 57
58 GrowableArray<StaticCallInstr*>* static_calls() {
59 return &static_calls_;
60 }
61
62 GrowableArray<ClosureCallInstr*>* closure_calls() {
63 return &closure_calls_;
64 }
65
66 GrowableArray<PolymorphicInstanceCallInstr*>* instance_calls() {
67 return &instance_calls_;
68 }
69
70 bool HasCalls() const {
71 return !(static_calls_.is_empty() &&
72 closure_calls_.is_empty() &&
73 instance_calls_.is_empty());
74 }
75
76 void Clear() {
77 static_calls_.Clear();
78 closure_calls_.Clear();
79 instance_calls_.Clear();
80 }
81
82 void FindCallSites(FlowGraph* graph) {
83 for (BlockIterator block_it = graph->postorder_iterator();
84 !block_it.Done();
85 block_it.Advance()) {
86 for (ForwardInstructionIterator it(block_it.Current());
87 !it.Done();
88 it.Advance()) {
89 it.Current()->Accept(this);
90 }
91 }
92 }
93
60 void VisitClosureCall(ClosureCallInstr* call) { 94 void VisitClosureCall(ClosureCallInstr* call) {
61 closure_calls_.Add(call); 95 closure_calls_.Add(call);
62 } 96 }
63 97
64 void VisitPolymorphicInstanceCall(PolymorphicInstanceCallInstr* call) { 98 void VisitPolymorphicInstanceCall(PolymorphicInstanceCallInstr* call) {
65 instance_calls_.Add(call); 99 instance_calls_.Add(call);
66 } 100 }
67 101
68 void VisitStaticCall(StaticCallInstr* call) { 102 void VisitStaticCall(StaticCallInstr* call) {
69 if (call->function().is_inlinable()) static_calls_.Add(call); 103 if (call->function().is_inlinable()) static_calls_.Add(call);
70 } 104 }
71 105
72 void FindCallSites() { 106 private:
73 VisitBlocks(); 107 GrowableArray<StaticCallInstr*> static_calls_;
74 } 108 GrowableArray<ClosureCallInstr*> closure_calls_;
109 GrowableArray<PolymorphicInstanceCallInstr*> instance_calls_;
110
111 DISALLOW_COPY_AND_ASSIGN(CallSites);
112 };
113
114
115 class CallSiteInliner : public ValueObject {
116 public:
117 explicit CallSiteInliner(FlowGraph* flow_graph)
118 : caller_graph_(flow_graph),
119 next_ssa_temp_index_(flow_graph->max_virtual_register_number()),
120 inlined_(false),
121 initial_size_(flow_graph->InstructionCount()),
122 inlined_size_(0),
123 inlining_depth_(1),
124 collected_call_sites_(NULL),
125 inlining_call_sites_(NULL) { }
75 126
76 void InlineCalls() { 127 void InlineCalls() {
77 InlineStaticCalls(); 128 // If inlining depth is less then one abort.
78 InlineClosureCalls(); 129 if (FLAG_inlining_depth_threshold < 1) return;
79 InlineInstanceCalls(); 130 // Create two call site collections to swap between.
131 CallSites sites1(caller_graph_);
132 CallSites sites2(caller_graph_);
133 CallSites* call_sites_temp = NULL;
134 collected_call_sites_ = &sites1;
135 inlining_call_sites_ = &sites2;
136 // Collect initial call sites.
137 collected_call_sites_->FindCallSites(caller_graph_);
138 while (collected_call_sites_->HasCalls()) {
139 TRACE_INLINING(OS::Print(" Depth %"Pd" ----------\n", inlining_depth_));
140 // Swap collected and inlining arrays and clear the new collecting array.
141 call_sites_temp = collected_call_sites_;
142 collected_call_sites_ = inlining_call_sites_;
143 inlining_call_sites_ = call_sites_temp;
144 collected_call_sites_->Clear();
145 // Inline call sites at the current depth.
146 InlineStaticCalls();
147 InlineClosureCalls();
148 InlineInstanceCalls();
149 // Increment the inlining depth. Checked before recursive inlining.
150 ++inlining_depth_;
151 }
152 collected_call_sites_ = NULL;
153 inlining_call_sites_ = NULL;
80 } 154 }
81 155
82 bool inlined() const { return inlined_; } 156 bool inlined() const { return inlined_; }
83 157
84 double GrowthFactor() const { 158 double GrowthFactor() const {
85 return static_cast<double>(inlined_size_) / 159 return static_cast<double>(inlined_size_) /
86 static_cast<double>(initial_size_); 160 static_cast<double>(initial_size_);
87 } 161 }
88 162
89 private: 163 private:
90 bool TryInlining(const Function& function, 164 bool TryInlining(const Function& function,
91 GrowableArray<Value*>* arguments, 165 GrowableArray<Value*>* arguments,
92 Definition* call) { 166 Definition* call) {
93 TRACE_INLINING(OS::Print(" => %s\n", function.ToCString())); 167 TRACE_INLINING(OS::Print(" => %s (deopt count %d)\n",
168 function.ToCString(),
169 function.deoptimization_counter()));
94 170
95 // Abort if the inlinable bit on the function is low. 171 // Abort if the inlinable bit on the function is low.
96 if (!function.is_inlinable()) { 172 if (!function.is_inlinable()) {
97 TRACE_INLINING(OS::Print(" Bailout: not inlinable\n")); 173 TRACE_INLINING(OS::Print(" Bailout: not inlinable\n"));
98 return false; 174 return false;
99 } 175 }
100 176
101 // Abort if the callee has optional parameters. 177 // Abort if the callee has optional parameters.
102 if (function.HasOptionalParameters()) { 178 if (function.HasOptionalParameters()) {
103 TRACE_INLINING(OS::Print(" Bailout: optional parameters\n")); 179 TRACE_INLINING(OS::Print(" Bailout: optional parameters\n"));
(...skipping 49 matching lines...) Expand 10 before | Expand all | Expand 10 after
153 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray()); 229 isolate->set_ic_data_array(unoptimized_code.ExtractTypeFeedbackArray());
154 } 230 }
155 231
156 // Build the callee graph. 232 // Build the callee graph.
157 FlowGraphBuilder builder(parsed_function); 233 FlowGraphBuilder builder(parsed_function);
158 builder.SetInitialBlockId(caller_graph_->max_block_id()); 234 builder.SetInitialBlockId(caller_graph_->max_block_id());
159 FlowGraph* callee_graph = 235 FlowGraph* callee_graph =
160 builder.BuildGraph(FlowGraphBuilder::kValueContext); 236 builder.BuildGraph(FlowGraphBuilder::kValueContext);
161 237
162 // Abort if the callee graph contains control flow. 238 // Abort if the callee graph contains control flow.
163 if ((callee_graph->preorder().length() != 2) && 239 if (!FLAG_inline_control_flow &&
164 !FLAG_inline_control_flow) { 240 (callee_graph->preorder().length() != 2)) {
165 function.set_is_inlinable(false); 241 function.set_is_inlinable(false);
166 isolate->set_long_jump_base(base); 242 isolate->set_long_jump_base(base);
167 isolate->set_ic_data_array(prev_ic_data.raw()); 243 isolate->set_ic_data_array(prev_ic_data.raw());
168 TRACE_INLINING(OS::Print(" Bailout: control flow\n")); 244 TRACE_INLINING(OS::Print(" Bailout: control flow\n"));
169 return false; 245 return false;
170 } 246 }
171 247
172 // Compute SSA on the callee graph, catching bailouts. 248 // Compute SSA on the callee graph, catching bailouts.
173 callee_graph->ComputeSSA(next_ssa_temp_index_); 249 callee_graph->ComputeSSA(next_ssa_temp_index_);
174 callee_graph->ComputeUseLists(); 250 callee_graph->ComputeUseLists();
(...skipping 15 matching lines...) Expand all
190 intptr_t size = callee_graph->InstructionCount(); 266 intptr_t size = callee_graph->InstructionCount();
191 if (size > FLAG_inlining_size_threshold) { 267 if (size > FLAG_inlining_size_threshold) {
192 function.set_is_inlinable(false); 268 function.set_is_inlinable(false);
193 isolate->set_long_jump_base(base); 269 isolate->set_long_jump_base(base);
194 isolate->set_deopt_id(prev_deopt_id); 270 isolate->set_deopt_id(prev_deopt_id);
195 isolate->set_ic_data_array(prev_ic_data.raw()); 271 isolate->set_ic_data_array(prev_ic_data.raw());
196 TRACE_INLINING(OS::Print(" Bailout: graph size %"Pd"\n", size)); 272 TRACE_INLINING(OS::Print(" Bailout: graph size %"Pd"\n", size));
197 return false; 273 return false;
198 } 274 }
199 275
200 // TODO(zerny): If effort is less than threshold then inline recursively. 276 // If depth is less or equal to threshold recursively add call sites.
277 if (inlining_depth_ < FLAG_inlining_depth_threshold) {
278 collected_call_sites_->FindCallSites(callee_graph);
279 }
201 280
202 // Plug result in the caller graph. 281 // Plug result in the caller graph.
203 caller_graph_->InlineCall(call, callee_graph); 282 caller_graph_->InlineCall(call, callee_graph);
204 next_ssa_temp_index_ = caller_graph_->max_virtual_register_number(); 283 next_ssa_temp_index_ = caller_graph_->max_virtual_register_number();
205 284
206 // Remove push arguments of the call. 285 // Remove push arguments of the call.
207 for (intptr_t i = 0; i < call->ArgumentCount(); ++i) { 286 for (intptr_t i = 0; i < call->ArgumentCount(); ++i) {
208 PushArgumentInstr* push = call->ArgumentAt(i); 287 PushArgumentInstr* push = call->ArgumentAt(i);
209 push->ReplaceUsesWith(push->value()->definition()); 288 push->ReplaceUsesWith(push->value()->definition());
210 push->RemoveFromGraph(); 289 push->RemoveFromGraph();
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
244 isolate->object_store()->clear_sticky_error(); 323 isolate->object_store()->clear_sticky_error();
245 isolate->set_long_jump_base(base); 324 isolate->set_long_jump_base(base);
246 isolate->set_deopt_id(prev_deopt_id); 325 isolate->set_deopt_id(prev_deopt_id);
247 isolate->set_ic_data_array(prev_ic_data.raw()); 326 isolate->set_ic_data_array(prev_ic_data.raw());
248 TRACE_INLINING(OS::Print(" Bailout: %s\n", error.ToErrorCString())); 327 TRACE_INLINING(OS::Print(" Bailout: %s\n", error.ToErrorCString()));
249 return false; 328 return false;
250 } 329 }
251 } 330 }
252 331
253 void InlineStaticCalls() { 332 void InlineStaticCalls() {
254 TRACE_INLINING(OS::Print(" Static Calls (%d)\n", 333 const GrowableArray<StaticCallInstr*>& calls =
255 static_calls_.length())); 334 *inlining_call_sites_->static_calls();
256 for (intptr_t i = 0; i < static_calls_.length(); ++i) { 335 TRACE_INLINING(OS::Print(" Static Calls (%d)\n", calls.length()));
257 StaticCallInstr* call = static_calls_[i]; 336 for (intptr_t i = 0; i < calls.length(); ++i) {
337 StaticCallInstr* call = calls[i];
258 GrowableArray<Value*> arguments(call->ArgumentCount()); 338 GrowableArray<Value*> arguments(call->ArgumentCount());
259 for (int i = 0; i < call->ArgumentCount(); ++i) { 339 for (int i = 0; i < call->ArgumentCount(); ++i) {
260 arguments.Add(call->ArgumentAt(i)->value()); 340 arguments.Add(call->ArgumentAt(i)->value());
261 } 341 }
262 TryInlining(call->function(), &arguments, call); 342 TryInlining(call->function(), &arguments, call);
263 } 343 }
264 } 344 }
265 345
266 void InlineClosureCalls() { 346 void InlineClosureCalls() {
267 TRACE_INLINING(OS::Print(" Closure Calls (%d)\n", 347 const GrowableArray<ClosureCallInstr*>& calls =
268 closure_calls_.length())); 348 *inlining_call_sites_->closure_calls();
269 for (intptr_t i = 0; i < closure_calls_.length(); ++i) { 349 TRACE_INLINING(OS::Print(" Closure Calls (%d)\n", calls.length()));
270 ClosureCallInstr* call = closure_calls_[i]; 350 for (intptr_t i = 0; i < calls.length(); ++i) {
351 ClosureCallInstr* call = calls[i];
271 // Find the closure of the callee. 352 // Find the closure of the callee.
272 ASSERT(call->ArgumentCount() > 0); 353 ASSERT(call->ArgumentCount() > 0);
273 const CreateClosureInstr* closure = 354 const CreateClosureInstr* closure =
274 call->ArgumentAt(0)->value()->definition()->AsCreateClosure(); 355 call->ArgumentAt(0)->value()->definition()->AsCreateClosure();
275 if (closure == NULL) { 356 if (closure == NULL) {
276 TRACE_INLINING(OS::Print(" Bailout: non-closure operator\n")); 357 TRACE_INLINING(OS::Print(" Bailout: non-closure operator\n"));
277 continue; 358 continue;
278 } 359 }
279 GrowableArray<Value*> arguments(call->ArgumentCount() - 1); 360 GrowableArray<Value*> arguments(call->ArgumentCount() - 1);
280 for (int i = 1; i < call->ArgumentCount(); ++i) { 361 for (int i = 1; i < call->ArgumentCount(); ++i) {
281 arguments.Add(call->ArgumentAt(i)->value()); 362 arguments.Add(call->ArgumentAt(i)->value());
282 } 363 }
283 TryInlining(closure->function(), &arguments, call); 364 TryInlining(closure->function(), &arguments, call);
284 } 365 }
285 } 366 }
286 367
287 void InlineInstanceCalls() { 368 void InlineInstanceCalls() {
369 const GrowableArray<PolymorphicInstanceCallInstr*>& calls =
370 *inlining_call_sites_->instance_calls();
288 TRACE_INLINING(OS::Print(" Polymorphic Instance Calls (%d)\n", 371 TRACE_INLINING(OS::Print(" Polymorphic Instance Calls (%d)\n",
289 instance_calls_.length())); 372 calls.length()));
290 for (intptr_t i = 0; i < instance_calls_.length(); ++i) { 373 for (intptr_t i = 0; i < calls.length(); ++i) {
291 PolymorphicInstanceCallInstr* instr = instance_calls_[i]; 374 PolymorphicInstanceCallInstr* instr = calls[i];
292 const ICData& ic_data = instr->ic_data(); 375 const ICData& ic_data = instr->ic_data();
293 const Function& target = Function::ZoneHandle(ic_data.GetTargetAt(0)); 376 const Function& target = Function::ZoneHandle(ic_data.GetTargetAt(0));
294 if (instr->with_checks()) { 377 if (instr->with_checks()) {
295 TRACE_INLINING(OS::Print(" Bailout: %"Pd" checks target '%s'\n", 378 TRACE_INLINING(OS::Print(" Bailout: %"Pd" checks target '%s'\n",
296 ic_data.NumberOfChecks(), 379 ic_data.NumberOfChecks(),
297 target.ToCString())); 380 target.ToCString()));
298 continue; 381 continue;
299 } 382 }
300 GrowableArray<Value*> arguments(instr->ArgumentCount()); 383 GrowableArray<Value*> arguments(instr->ArgumentCount());
301 for (int i = 0; i < instr->ArgumentCount(); ++i) { 384 for (int i = 0; i < instr->ArgumentCount(); ++i) {
302 arguments.Add(instr->ArgumentAt(i)->value()); 385 arguments.Add(instr->ArgumentAt(i)->value());
303 } 386 }
304 TryInlining(target, &arguments, instr); 387 TryInlining(target, &arguments, instr);
305 } 388 }
306 } 389 }
307 390
308 FlowGraph* caller_graph_; 391 FlowGraph* caller_graph_;
309 intptr_t next_ssa_temp_index_; 392 intptr_t next_ssa_temp_index_;
310 bool inlined_; 393 bool inlined_;
311 intptr_t initial_size_; 394 intptr_t initial_size_;
312 intptr_t inlined_size_; 395 intptr_t inlined_size_;
396 intptr_t inlining_depth_;
397 CallSites* collected_call_sites_;
398 CallSites* inlining_call_sites_;
313 399
314 GrowableArray<StaticCallInstr*> static_calls_; 400 DISALLOW_COPY_AND_ASSIGN(CallSiteInliner);
315 GrowableArray<ClosureCallInstr*> closure_calls_;
316 GrowableArray<PolymorphicInstanceCallInstr*> instance_calls_;
317 }; 401 };
318 402
319 403
320 void FlowGraphInliner::Inline() { 404 void FlowGraphInliner::Inline() {
321 if ((FLAG_inlining_filter != NULL) && 405 if ((FLAG_inlining_filter != NULL) &&
322 (strstr(flow_graph_-> 406 (strstr(flow_graph_->
323 parsed_function().function().ToFullyQualifiedCString(), 407 parsed_function().function().ToFullyQualifiedCString(),
324 FLAG_inlining_filter) == NULL)) { 408 FLAG_inlining_filter) == NULL)) {
325 return; 409 return;
326 } 410 }
327 411
328 TRACE_INLINING(OS::Print( 412 TRACE_INLINING(OS::Print(
329 "Inlining calls in %s\n", 413 "Inlining calls in %s\n",
330 flow_graph_->parsed_function().function().ToCString())); 414 flow_graph_->parsed_function().function().ToCString()));
331 415
332 if (FLAG_trace_inlining && FLAG_print_flow_graph) { 416 if (FLAG_trace_inlining && FLAG_print_flow_graph) {
333 OS::Print("Before Inlining of %s\n", flow_graph_-> 417 OS::Print("Before Inlining of %s\n", flow_graph_->
334 parsed_function().function().ToFullyQualifiedCString()); 418 parsed_function().function().ToFullyQualifiedCString());
335 FlowGraphPrinter printer(*flow_graph_); 419 FlowGraphPrinter printer(*flow_graph_);
336 printer.PrintBlocks(); 420 printer.PrintBlocks();
337 } 421 }
338 422
339 CallSiteInliner inliner(flow_graph_); 423 CallSiteInliner inliner(flow_graph_);
340 inliner.FindCallSites();
341 inliner.InlineCalls(); 424 inliner.InlineCalls();
342 425
343 if (inliner.inlined()) { 426 if (inliner.inlined()) {
344 if (FLAG_trace_inlining) { 427 if (FLAG_trace_inlining) {
345 OS::Print("Inlining growth factor: %f\n", inliner.GrowthFactor()); 428 OS::Print("Inlining growth factor: %f\n", inliner.GrowthFactor());
346 if (FLAG_print_flow_graph) { 429 if (FLAG_print_flow_graph) {
347 OS::Print("After Inlining of %s\n", flow_graph_-> 430 OS::Print("After Inlining of %s\n", flow_graph_->
348 parsed_function().function().ToFullyQualifiedCString()); 431 parsed_function().function().ToFullyQualifiedCString());
349 FlowGraphPrinter printer(*flow_graph_); 432 FlowGraphPrinter printer(*flow_graph_);
350 printer.PrintBlocks(); 433 printer.PrintBlocks();
351 } 434 }
352 } 435 }
353 } 436 }
354 } 437 }
355 438
356 } // namespace dart 439 } // namespace dart
OLDNEW
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698