| 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"); | |
| 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 // Parse the callee function. | |
| 46 ParsedFunction parsed_function(function); | |
| 47 Parser::ParseFunction(&parsed_function); | |
| 48 FlowGraphBuilder builder(parsed_function); | |
| 49 | |
| 50 // Install bailout jump. | |
| 51 Isolate* isolate = Isolate::Current(); | |
| 52 LongJump* base = isolate->long_jump_base(); | |
| 53 LongJump jump; | |
| 54 isolate->set_long_jump_base(&jump); | |
| 55 if (setjmp(*jump.Set()) == 0) { | |
| 56 // Build the callee graph. | |
| 57 FlowGraph* callee_graph = | |
| 58 builder.BuildGraphForInlining(FlowGraphBuilder::kValueContext); | |
| 59 | |
| 60 // Abort if the callee graph contains control flow. | |
| 61 if (callee_graph->preorder().length() != 2) { | |
| 62 isolate->set_long_jump_base(base); | |
| 63 if (FLAG_trace_inlining) { | |
| 64 OS::Print("Inline aborted %s\nReason: control flow\n", | |
| 65 parsed_function.function().ToFullyQualifiedCString()); | |
| 66 } | |
| 67 return; | |
| 68 } | |
| 69 | |
| 70 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 71 OS::Print("Callee graph before SSA %s\n", | |
| 72 parsed_function.function().ToFullyQualifiedCString()); | |
| 73 FlowGraphPrinter printer(*callee_graph); | |
| 74 printer.PrintBlocks(); | |
| 75 } | |
| 76 | |
| 77 // Compute SSA on the callee graph. (catching bailouts) | |
| 78 callee_graph->ComputeSSA(next_ssa_temp_index_); | |
| 79 | |
| 80 // Build succeeded so we restore the bailout jump. | |
| 81 isolate->set_long_jump_base(base); | |
| 82 | |
| 83 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 84 OS::Print("Callee graph after SSA %s\n", | |
| 85 parsed_function.function().ToFullyQualifiedCString()); | |
| 86 FlowGraphPrinter printer(*callee_graph); | |
| 87 printer.PrintBlocks(); | |
| 88 } | |
| 89 | |
| 90 callee_graph->ComputeUseLists(); | |
| 91 | |
| 92 // TODO(zerny): Do optimization passes on the callee graph. | |
| 93 | |
| 94 // TODO(zerny): If result is more than size threshold then abort. | |
| 95 | |
| 96 // TODO(zerny): If effort is less than threshold then inline recursively. | |
| 97 | |
| 98 // Plug result in the caller graph. | |
| 99 caller_graph_->InlineCall(instr, comp, callee_graph); | |
| 100 next_ssa_temp_index_ = caller_graph_->max_virtual_register_number(); | |
| 101 | |
| 102 // Replace all the formal parameters with the actuals. | |
| 103 for (intptr_t i = 0; i < arguments->length(); ++i) { | |
| 104 Value* val = callee_graph->graph_entry()->start_env()->values()[i]; | |
| 105 ASSERT(val != NULL && val->IsUse()); | |
| 106 ParameterInstr* param = val->AsUse()->definition()->AsParameter(); | |
| 107 ASSERT(param != NULL); | |
| 108 param->ReplaceUsesWith((*arguments)[i]->AsUse()->definition()); | |
| 109 } | |
| 110 | |
| 111 inlined_ = true; | |
| 112 if (FLAG_trace_inlining) { | |
| 113 OS::Print("Inlined %s\n", function.ToFullyQualifiedCString()); | |
| 114 } | |
| 115 } else { | |
| 116 Error& error = Error::Handle(); | |
| 117 error = isolate->object_store()->sticky_error(); | |
| 118 isolate->object_store()->clear_sticky_error(); | |
| 119 isolate->set_long_jump_base(base); | |
| 120 if (FLAG_trace_inlining) { | |
| 121 OS::Print("Inline aborted for %s\nReason: %s\n", | |
| 122 parsed_function.function().ToFullyQualifiedCString(), | |
| 123 error.ToErrorCString()); | |
| 124 } | |
| 125 } | |
| 126 } | |
| 127 | |
| 128 void VisitBind(BindInstr* instr) { | |
| 129 instr->computation()->Accept(this, instr); | |
| 130 } | |
| 131 | |
| 132 void VisitStaticCall(StaticCallComp* comp, BindInstr* instr) { | |
| 133 if (FLAG_trace_inlining) OS::Print("Static call\n"); | |
| 134 GrowableArray<Value*> arguments(comp->ArgumentCount()); | |
| 135 for (int i = 0; i < comp->ArgumentCount(); ++i) { | |
| 136 arguments.Add(comp->ArgumentAt(i)->value()); | |
| 137 } | |
| 138 TryInlining(comp->function(), &arguments, comp, instr); | |
| 139 } | |
| 140 | |
| 141 bool preformed_inlining() const { return inlined_; } | |
| 142 | |
| 143 private: | |
| 144 FlowGraph* caller_graph_; | |
| 145 intptr_t next_ssa_temp_index_; | |
| 146 bool inlined_; | |
| 147 }; | |
| 148 | |
| 149 | |
| 150 void FlowGraphInliner::Inline() { | |
| 151 if ((FLAG_inlining_filter != NULL) && | |
| 152 (strstr(flow_graph_-> | |
| 153 parsed_function().function().ToFullyQualifiedCString(), | |
| 154 FLAG_inlining_filter) == NULL)) { | |
| 155 return; | |
| 156 } | |
| 157 | |
| 158 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 159 OS::Print("Before Inlining of %s\n", flow_graph_-> | |
| 160 parsed_function().function().ToFullyQualifiedCString()); | |
| 161 FlowGraphPrinter printer(*flow_graph_); | |
| 162 printer.PrintBlocks(); | |
| 163 } | |
| 164 | |
| 165 CallSiteInliner inliner(flow_graph_); | |
| 166 inliner.VisitBlocks(); | |
| 167 | |
| 168 if (inliner.preformed_inlining()) { | |
| 169 if (FLAG_trace_inlining && FLAG_print_flow_graph) { | |
| 170 OS::Print("After Inlining of %s\n", flow_graph_-> | |
| 171 parsed_function().function().ToFullyQualifiedCString()); | |
| 172 FlowGraphPrinter printer(*flow_graph_); | |
| 173 printer.PrintBlocks(); | |
| 174 } | |
| 175 } | |
| 176 } | |
| 177 | |
| 178 } // namespace dart | |
| OLD | NEW |