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