Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "inliner.h" |
| 18 | |
| 19 | #include "builder.h" |
| 20 | #include "class_linker.h" |
| 21 | #include "constant_folding.h" |
| 22 | #include "dead_code_elimination.h" |
| 23 | #include "driver/compiler_driver-inl.h" |
| 24 | #include "driver/dex_compilation_unit.h" |
| 25 | #include "instruction_simplifier.h" |
| 26 | #include "mirror/art_method-inl.h" |
| 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/dex_cache.h" |
| 29 | #include "nodes.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 30 | #include "register_allocator.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 31 | #include "ssa_phi_elimination.h" |
| 32 | #include "scoped_thread_state_change.h" |
| 33 | #include "thread.h" |
| 34 | |
| 35 | namespace art { |
| 36 | |
| 37 | static constexpr int kMaxInlineCodeUnits = 100; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 38 | static constexpr int kDepthLimit = 5; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 39 | |
| 40 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 41 | if (graph_->IsDebuggable()) { |
| 42 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 43 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 44 | return; |
| 45 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 46 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
| 47 | for (size_t i = 0; i < blocks.Size(); ++i) { |
| 48 | HBasicBlock* block = blocks.Get(i); |
| 49 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 50 | HInstruction* next = instruction->GetNext(); |
| 51 | HInvokeStaticOrDirect* call = instruction->AsInvokeStaticOrDirect(); |
| 52 | if (call != nullptr) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 53 | // We use the original invoke type to ensure the resolution of the called method |
| 54 | // works properly. |
| 55 | if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 56 | if (kIsDebugBuild) { |
| 57 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 58 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 59 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 60 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 61 | } |
| 62 | } |
| 63 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 64 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 65 | } |
| 66 | } |
| 67 | } |
| 68 | |
| 69 | bool HInliner::TryInline(HInvoke* invoke_instruction, |
| 70 | uint32_t method_index, |
| 71 | InvokeType invoke_type) const { |
| 72 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 73 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 74 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 75 | |
| 76 | StackHandleScope<3> hs(soa.Self()); |
| 77 | Handle<mirror::DexCache> dex_cache( |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 78 | hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file))); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 79 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 80 | soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader()))); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 81 | Handle<mirror::ArtMethod> resolved_method(hs.NewHandle( |
| 82 | compiler_driver_->ResolveMethod( |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 83 | soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type))); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 84 | |
| 85 | if (resolved_method.Get() == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 86 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 87 | return false; |
| 88 | } |
| 89 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 90 | bool can_use_dex_cache = true; |
| 91 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 92 | if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 93 | can_use_dex_cache = false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 97 | |
| 98 | if (code_item == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 99 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 100 | << " is not inlined because it is native"; |
| 101 | return false; |
| 102 | } |
| 103 | |
| 104 | if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 105 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 106 | << " is too big to inline"; |
| 107 | return false; |
| 108 | } |
| 109 | |
| 110 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 111 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 112 | << " is not inlined because of try block"; |
| 113 | return false; |
| 114 | } |
| 115 | |
| 116 | if (!resolved_method->GetDeclaringClass()->IsVerified()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 117 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 118 | << " is not inlined because its class could not be verified"; |
| 119 | return false; |
| 120 | } |
| 121 | |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 122 | if (resolved_method->ShouldNotInline()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 123 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 124 | << " was already flagged as non inlineable"; |
| 125 | return false; |
| 126 | } |
| 127 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 128 | if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, can_use_dex_cache)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 129 | resolved_method->SetShouldNotInline(); |
| 130 | return false; |
| 131 | } |
| 132 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 133 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 134 | MaybeRecordStat(kInlinedInvoke); |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool HInliner::TryBuildAndInline(Handle<mirror::ArtMethod> resolved_method, |
| 139 | HInvoke* invoke_instruction, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 140 | uint32_t method_index, |
| 141 | bool can_use_dex_cache) const { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 142 | ScopedObjectAccess soa(Thread::Current()); |
| 143 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 144 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 145 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 146 | DexCompilationUnit dex_compilation_unit( |
| 147 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 148 | caller_compilation_unit_.GetClassLoader(), |
| 149 | caller_compilation_unit_.GetClassLinker(), |
| 150 | *resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 151 | code_item, |
| 152 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 153 | resolved_method->GetDexMethodIndex(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 154 | resolved_method->GetAccessFlags(), |
| 155 | nullptr); |
| 156 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 157 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
| 158 | graph_->GetArena(), graph_->IsDebuggable(), graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 159 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 160 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 161 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 162 | &dex_compilation_unit, |
| 163 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 164 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 165 | compiler_driver_, |
| 166 | &inline_stats); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 167 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 168 | if (!builder.BuildGraph(*code_item)) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 169 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 170 | << " could not be built, so cannot be inlined"; |
| 171 | return false; |
| 172 | } |
| 173 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 174 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 175 | compiler_driver_->GetInstructionSet())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 176 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 177 | << " cannot be inlined because of the register allocator"; |
| 178 | return false; |
| 179 | } |
| 180 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 181 | if (!callee_graph->TryBuildingSsa()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 182 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 183 | << " could not be transformed to SSA"; |
| 184 | return false; |
| 185 | } |
| 186 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 187 | // Run simple optimizations on the graph. |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 188 | HDeadCodeElimination dce(callee_graph); |
| 189 | HConstantFolding fold(callee_graph); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 190 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 191 | |
| 192 | HOptimization* optimizations[] = { |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 193 | &dce, |
| 194 | &fold, |
| 195 | &simplify, |
| 196 | }; |
| 197 | |
| 198 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 199 | HOptimization* optimization = optimizations[i]; |
| 200 | optimization->Run(); |
| 201 | } |
| 202 | |
| 203 | if (depth_ + 1 < kDepthLimit) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 204 | HInliner inliner(callee_graph, |
| 205 | outer_compilation_unit_, |
| 206 | dex_compilation_unit, |
| 207 | compiler_driver_, |
| 208 | stats_, |
| 209 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 210 | inliner.Run(); |
| 211 | } |
| 212 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 213 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 214 | it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 215 | for (; !it.Done(); it.Advance()) { |
| 216 | HBasicBlock* block = it.Current(); |
| 217 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 218 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 219 | << " could not be inlined because it contains a loop"; |
| 220 | return false; |
| 221 | } |
| 222 | |
| 223 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 224 | !instr_it.Done(); |
| 225 | instr_it.Advance()) { |
| 226 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 227 | if (current->IsSuspendCheck()) { |
| 228 | continue; |
| 229 | } |
| 230 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 231 | if (current->CanThrow()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 232 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 233 | << " could not be inlined because " << current->DebugName() |
| 234 | << " can throw"; |
| 235 | return false; |
| 236 | } |
| 237 | |
| 238 | if (current->NeedsEnvironment()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 239 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 240 | << " could not be inlined because " << current->DebugName() |
| 241 | << " needs an environment"; |
| 242 | return false; |
| 243 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 244 | |
| 245 | if (!can_use_dex_cache && current->NeedsDexCache()) { |
| 246 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 247 | << " could not be inlined because " << current->DebugName() |
| 248 | << " it is in a different dex file and requires access to the dex cache"; |
| 249 | return false; |
| 250 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 251 | } |
| 252 | } |
| 253 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 254 | callee_graph->InlineInto(graph_, invoke_instruction); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 255 | |
Mingyao Yang | e4335eb | 2015-03-02 15:14:13 -0800 | [diff] [blame] | 256 | if (callee_graph->HasArrayAccesses()) { |
| 257 | graph_->SetHasArrayAccesses(true); |
| 258 | } |
| 259 | |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 260 | // Now that we have inlined the callee, we need to update the next |
| 261 | // instruction id of the caller, so that new instructions added |
| 262 | // after optimizations get a unique id. |
| 263 | graph_->SetCurrentInstructionId(callee_graph->GetNextInstructionId()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 264 | return true; |
| 265 | } |
| 266 | |
| 267 | } // namespace art |