Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "vm/flow_graph_inliner.h" | |
| 6 | |
| 7 #include "vm/flags.h" | |
| 8 #include "vm/flow_graph.h" | |
| 9 #include "vm/flow_graph_builder.h" | |
| 10 #include "vm/il_printer.h" | |
| 11 #include "vm/longjump.h" | |
| 12 #include "vm/object.h" | |
| 13 #include "vm/object_store.h" | |
| 14 | |
| 15 namespace dart { | |
| 16 | |
| 17 DEFINE_FLAG(bool, trace_inlining, false, "Trace inlining"); | |
|
srdjan
2012/09/05 17:40:05
I would like to trace only inlined functions. Mayb
zerny-google
2012/09/07 14:08:43
Could you elaborate. I'm not sure I understand?
| |
| 18 DEFINE_FLAG(charp, inlining_filter, NULL, "Inline only in named function"); | |
| 19 DECLARE_FLAG(bool, print_flow_graph); | |
| 20 | |
| 21 class CallSiteInliner : public FlowGraphVisitor { | |
| 22 public: | |
| 23 explicit CallSiteInliner(FlowGraph* flow_graph) | |
| 24 : FlowGraphVisitor(flow_graph->postorder()), | |
| 25 caller_graph_(flow_graph), | |
| 26 next_ssa_temp_index_(flow_graph->max_virtual_register_number()), | |
| 27 inlined_(false) { } | |
| 28 | |
| 29 void TryInlining(const Function& function, | |
| 30 GrowableArray<Value*>* arguments, | |
| 31 StaticCallComp* comp, // TODO(zerny): Generalize to calls. | |
| 32 BindInstr* instr) { | |
| 33 // Abort if the callee has named parameters. | |
| 34 if (function.num_optional_parameters() > 0) { | |
| 35 if (FLAG_trace_inlining) { | |
| 36 OS::Print("Inline aborted %s\nReason: optional parameters\n", | |
| 37 function.ToFullyQualifiedCString()); | |
| 38 } | |
| 39 return; | |
| 40 } | |
| 41 | |
| 42 // Assuming no optional parameters the actual/formal count should match. | |
| 43 ASSERT(arguments->length() == function.num_fixed_parameters()); | |
| 44 | |
| 45 Isolate* isolate = Isolate::Current(); | |
| 46 // Save and clear IC data. | |
| 47 const Array& old_ic_data = Array::Handle(isolate->ic_data_array()); | |
| 48 isolate->set_ic_data_array(Array::null()); | |
| 49 // Install bailout jump. | |
| 50 LongJump* base = isolate->long_jump_base(); | |
| 51 LongJump jump; | |
| 52 isolate->set_long_jump_base(&jump); | |
| 53 if (setjmp(*jump.Set()) == 0) { | |
| 54 // Parse the callee function. | |
| 55 ParsedFunction parsed_function(function); | |
| 56 Parser::ParseFunction(&parsed_function); | |
| 57 FlowGraphBuilder builder(parsed_function); | |
| 58 | |
| 59 // Build the callee graph. | |
| 60 FlowGraph* callee_graph = | |
| 61 builder.BuildGraphForInlining(FlowGraphBuilder::kValueContext); | |
| 62 | |
| 63 // Abort if the callee graph contains control flow. | |
| 64 if (callee_graph->preorder().length() != 2) { | |
| 65 isolate->set_long_jump_base(base); | |
| 66 isolate->set_ic_data_array(old_ic_data.raw()); | |
| 67 if (FLAG_trace_inlining) { | |
| 68 OS::Print("Inline aborted %s\nReason: control flow\n", | |
| 69 parsed_function.function().ToFullyQualifiedCString()); | |
| 70 } | |
| 71 return; | |
| 72 } | |
| 73 | |
| 74 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 75 OS::Print("Callee graph before SSA %s\n", | |
| 76 parsed_function.function().ToFullyQualifiedCString()); | |
| 77 FlowGraphPrinter printer(*callee_graph); | |
| 78 printer.PrintBlocks(); | |
| 79 } | |
| 80 | |
| 81 // Compute SSA on the callee graph. (catching bailouts) | |
| 82 callee_graph->ComputeSSA(next_ssa_temp_index_); | |
| 83 | |
| 84 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 85 OS::Print("Callee graph after SSA %s\n", | |
| 86 parsed_function.function().ToFullyQualifiedCString()); | |
| 87 FlowGraphPrinter printer(*callee_graph); | |
| 88 printer.PrintBlocks(); | |
| 89 } | |
| 90 | |
| 91 callee_graph->ComputeUseLists(); | |
| 92 | |
| 93 // TODO(zerny): Do optimization passes on the callee graph. | |
| 94 | |
| 95 // TODO(zerny): If result is more than size threshold then abort. | |
| 96 | |
| 97 // TODO(zerny): If effort is less than threshold then inline recursively. | |
| 98 | |
| 99 // Plug result in the caller graph. | |
| 100 caller_graph_->InlineCall(instr, comp, callee_graph); | |
| 101 next_ssa_temp_index_ = caller_graph_->max_virtual_register_number(); | |
| 102 | |
| 103 // Replace all the formal parameters with the actuals. | |
| 104 for (intptr_t i = 0; i < arguments->length(); ++i) { | |
| 105 Value* val = callee_graph->graph_entry()->start_env()->values()[i]; | |
| 106 ASSERT(val != NULL && val->IsUse()); | |
| 107 ParameterInstr* param = val->AsUse()->definition()->AsParameter(); | |
| 108 ASSERT(param != NULL); | |
| 109 param->ReplaceUsesWith((*arguments)[i]->AsUse()->definition()); | |
| 110 } | |
| 111 | |
| 112 if (FLAG_trace_inlining) { | |
| 113 OS::Print("Inlined %s\n", function.ToFullyQualifiedCString()); | |
| 114 } | |
| 115 | |
| 116 // Build succeeded so we restore the bailout jump. | |
| 117 inlined_ = true; | |
| 118 isolate->set_long_jump_base(base); | |
| 119 isolate->set_ic_data_array(old_ic_data.raw()); | |
| 120 } else { | |
| 121 Error& error = Error::Handle(); | |
| 122 error = isolate->object_store()->sticky_error(); | |
| 123 isolate->object_store()->clear_sticky_error(); | |
| 124 isolate->set_long_jump_base(base); | |
| 125 isolate->set_ic_data_array(old_ic_data.raw()); | |
| 126 if (FLAG_trace_inlining) { | |
| 127 OS::Print("Inline aborted for %s\nReason: %s\n", | |
| 128 function.ToFullyQualifiedCString(), | |
| 129 error.ToErrorCString()); | |
| 130 } | |
| 131 } | |
| 132 } | |
| 133 | |
| 134 void VisitBind(BindInstr* instr) { | |
| 135 instr->computation()->Accept(this, instr); | |
| 136 } | |
| 137 | |
| 138 void VisitStaticCall(StaticCallComp* comp, BindInstr* instr) { | |
| 139 if (FLAG_trace_inlining) OS::Print("Static call\n"); | |
| 140 GrowableArray<Value*> arguments(comp->ArgumentCount()); | |
| 141 for (int i = 0; i < comp->ArgumentCount(); ++i) { | |
| 142 arguments.Add(comp->ArgumentAt(i)->value()); | |
| 143 } | |
| 144 TryInlining(comp->function(), &arguments, comp, instr); | |
| 145 } | |
| 146 | |
| 147 bool preformed_inlining() const { return inlined_; } | |
|
srdjan
2012/09/05 17:40:05
What does preformed inlining mean?
zerny-google
2012/09/07 14:08:43
Typo there. I meant to write performed, but have c
| |
| 148 | |
| 149 private: | |
| 150 FlowGraph* caller_graph_; | |
| 151 intptr_t next_ssa_temp_index_; | |
| 152 bool inlined_; | |
| 153 }; | |
| 154 | |
| 155 | |
| 156 void FlowGraphInliner::Inline() { | |
| 157 if ((FLAG_inlining_filter != NULL) && | |
| 158 (strstr(flow_graph_-> | |
| 159 parsed_function().function().ToFullyQualifiedCString(), | |
| 160 FLAG_inlining_filter) == NULL)) { | |
| 161 return; | |
| 162 } | |
| 163 | |
| 164 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 165 OS::Print("Before Inlining of %s\n", flow_graph_-> | |
| 166 parsed_function().function().ToFullyQualifiedCString()); | |
| 167 FlowGraphPrinter printer(*flow_graph_); | |
| 168 printer.PrintBlocks(); | |
| 169 } | |
| 170 | |
| 171 CallSiteInliner inliner(flow_graph_); | |
| 172 inliner.VisitBlocks(); | |
| 173 | |
| 174 if (inliner.preformed_inlining()) { | |
| 175 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 176 OS::Print("After Inlining of %s\n", flow_graph_-> | |
| 177 parsed_function().function().ToFullyQualifiedCString()); | |
| 178 FlowGraphPrinter printer(*flow_graph_); | |
| 179 printer.PrintBlocks(); | |
| 180 } | |
| 181 } | |
| 182 } | |
| 183 | |
| 184 } // namespace dart | |
| OLD | NEW |