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 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 20 | #include "builder.h" |
| 21 | #include "class_linker.h" |
| 22 | #include "constant_folding.h" |
| 23 | #include "dead_code_elimination.h" |
| 24 | #include "driver/compiler_driver-inl.h" |
| 25 | #include "driver/dex_compilation_unit.h" |
| 26 | #include "instruction_simplifier.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/dex_cache.h" |
| 29 | #include "nodes.h" |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 30 | #include "reference_type_propagation.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 31 | #include "register_allocator.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 32 | #include "ssa_phi_elimination.h" |
| 33 | #include "scoped_thread_state_change.h" |
| 34 | #include "thread.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 35 | #include "dex/verified_method.h" |
| 36 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 37 | |
| 38 | namespace art { |
| 39 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 40 | static constexpr int kMaxInlineCodeUnits = 18; |
| 41 | static constexpr int kDepthLimit = 3; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 42 | |
| 43 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 44 | if (graph_->IsDebuggable()) { |
| 45 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 46 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 47 | return; |
| 48 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 49 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 50 | HBasicBlock* next_block = blocks.Get(0); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 51 | for (size_t i = 0; i < blocks.Size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 52 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 53 | // This avoids doing the inlining work again on the inlined blocks. |
| 54 | if (blocks.Get(i) != next_block) { |
| 55 | continue; |
| 56 | } |
| 57 | HBasicBlock* block = next_block; |
| 58 | next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 59 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 60 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 61 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 62 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 63 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 64 | // We use the original invoke type to ensure the resolution of the called method |
| 65 | // works properly. |
| 66 | if (!TryInline(call, call->GetDexMethodIndex(), call->GetOriginalInvokeType())) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 67 | if (kIsDebugBuild) { |
| 68 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 69 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 70 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 71 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 72 | } |
| 73 | } |
| 74 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 75 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 76 | } |
| 77 | } |
| 78 | } |
| 79 | |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 80 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
| 81 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 82 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 83 | } |
| 84 | |
| 85 | /** |
| 86 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 87 | * the actual runtime target of an interface or virtual call. |
| 88 | * Return nullptr if the runtime target cannot be proven. |
| 89 | */ |
| 90 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
| 91 | SHARED_LOCKS_REQUIRED(Locks::mutator_lock_) { |
| 92 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 93 | // No need to lookup further, the resolved method will be the target. |
| 94 | return resolved_method; |
| 95 | } |
| 96 | |
| 97 | HInstruction* receiver = invoke->InputAt(0); |
| 98 | if (receiver->IsNullCheck()) { |
| 99 | // Due to multiple levels of inlining within the same pass, it might be that |
| 100 | // null check does not have the reference type of the actual receiver. |
| 101 | receiver = receiver->InputAt(0); |
| 102 | } |
| 103 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
| 104 | if (info.IsTop()) { |
| 105 | // We have no information on the receiver. |
| 106 | return nullptr; |
| 107 | } else if (!info.IsExact()) { |
| 108 | // We currently only support inlining with known receivers. |
| 109 | // TODO: Remove this check, we should be able to inline final methods |
| 110 | // on unknown receivers. |
| 111 | return nullptr; |
| 112 | } else if (info.GetTypeHandle()->IsInterface()) { |
| 113 | // Statically knowing that the receiver has an interface type cannot |
| 114 | // help us find what is the target method. |
| 115 | return nullptr; |
| 116 | } |
| 117 | |
| 118 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 119 | size_t pointer_size = cl->GetImagePointerSize(); |
| 120 | if (invoke->IsInvokeInterface()) { |
| 121 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface( |
| 122 | resolved_method, pointer_size); |
| 123 | } else { |
| 124 | DCHECK(invoke->IsInvokeVirtual()); |
| 125 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual( |
| 126 | resolved_method, pointer_size); |
| 127 | } |
| 128 | |
| 129 | if (resolved_method == nullptr) { |
| 130 | // The information we had on the receiver was not enough to find |
| 131 | // the target method. Since we check above the exact type of the receiver, |
| 132 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 133 | return nullptr; |
| 134 | } else if (resolved_method->IsAbstract()) { |
| 135 | // The information we had on the receiver was not enough to find |
| 136 | // the target method. Since we check above the exact type of the receiver, |
| 137 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 138 | return nullptr; |
| 139 | } else if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 140 | // A final method has to be the target method. |
| 141 | return resolved_method; |
| 142 | } else if (info.IsExact()) { |
| 143 | // If we found a method and the receiver's concrete type is statically |
| 144 | // known, we know for sure the target. |
| 145 | return resolved_method; |
| 146 | } else { |
| 147 | // Even if we did find a method, the receiver type was not enough to |
| 148 | // statically find the runtime target. |
| 149 | return nullptr; |
| 150 | } |
| 151 | } |
| 152 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 153 | bool HInliner::TryInline(HInvoke* invoke_instruction, |
| 154 | uint32_t method_index, |
| 155 | InvokeType invoke_type) const { |
| 156 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 157 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 158 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 159 | |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 160 | ArtMethod* resolved_method = nullptr; |
| 161 | { |
| 162 | // Don't keep this handle scope on stack, otherwise we cannot do a reference type |
| 163 | // propagation while inlining. |
| 164 | StackHandleScope<2> hs(soa.Self()); |
| 165 | Handle<mirror::DexCache> dex_cache( |
| 166 | hs.NewHandle(caller_compilation_unit_.GetClassLinker()->FindDexCache(caller_dex_file))); |
| 167 | Handle<mirror::ClassLoader> class_loader(hs.NewHandle( |
| 168 | soa.Decode<mirror::ClassLoader*>(caller_compilation_unit_.GetClassLoader()))); |
| 169 | resolved_method = compiler_driver_->ResolveMethod( |
| 170 | soa, dex_cache, class_loader, &caller_compilation_unit_, method_index, invoke_type); |
| 171 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 172 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 173 | if (resolved_method == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 174 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 175 | return false; |
| 176 | } |
| 177 | |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 178 | if (!invoke_instruction->IsInvokeStaticOrDirect()) { |
| 179 | resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
| 180 | if (resolved_method == nullptr) { |
| 181 | VLOG(compiler) << "Interface or virtual call to " |
| 182 | << PrettyMethod(method_index, caller_dex_file) |
| 183 | << "could not be statically determined"; |
| 184 | return false; |
| 185 | } |
| 186 | } |
| 187 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 188 | bool same_dex_file = true; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 189 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 190 | if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 191 | same_dex_file = false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 195 | |
| 196 | if (code_item == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 197 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 198 | << " is not inlined because it is native"; |
| 199 | return false; |
| 200 | } |
| 201 | |
| 202 | if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 203 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 204 | << " is too big to inline"; |
| 205 | return false; |
| 206 | } |
| 207 | |
| 208 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 209 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 210 | << " is not inlined because of try block"; |
| 211 | return false; |
| 212 | } |
| 213 | |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 214 | uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex(); |
| 215 | if (!compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 216 | resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 217 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 218 | << " couldn't be verified, so it cannot be inlined"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 219 | return false; |
| 220 | } |
| 221 | |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 222 | if (resolved_method->ShouldNotInline()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 223 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 224 | << " was already flagged as non inlineable"; |
| 225 | return false; |
| 226 | } |
| 227 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 228 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 229 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 230 | // Case of a static method that cannot be inlined because it implicitly |
| 231 | // requires an initialization check of its declaring class. |
| 232 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 233 | << " is not inlined because it is static and requires a clinit" |
| 234 | << " check that cannot be emitted due to Dex cache limitations"; |
| 235 | return false; |
| 236 | } |
| 237 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 238 | if (!TryBuildAndInline(resolved_method, invoke_instruction, method_index, same_dex_file)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 242 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 243 | MaybeRecordStat(kInlinedInvoke); |
| 244 | return true; |
| 245 | } |
| 246 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 247 | bool HInliner::TryBuildAndInline(ArtMethod* resolved_method, |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 248 | HInvoke* invoke_instruction, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 249 | uint32_t method_index, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 250 | bool same_dex_file) const { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 251 | ScopedObjectAccess soa(Thread::Current()); |
| 252 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 253 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 254 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 255 | DexCompilationUnit dex_compilation_unit( |
| 256 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 257 | caller_compilation_unit_.GetClassLoader(), |
| 258 | caller_compilation_unit_.GetClassLinker(), |
| 259 | *resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 260 | code_item, |
| 261 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 262 | resolved_method->GetDexMethodIndex(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 263 | resolved_method->GetAccessFlags(), |
| 264 | nullptr); |
| 265 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 266 | bool requires_ctor_barrier = false; |
| 267 | |
| 268 | if (dex_compilation_unit.IsConstructor()) { |
| 269 | // If it's a super invocation and we already generate a barrier there's no need |
| 270 | // to generate another one. |
| 271 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 272 | // same as the local "this" pointer then we must have a super invocation. |
| 273 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 274 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 275 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 276 | requires_ctor_barrier = false; |
| 277 | } else { |
| 278 | Thread* self = Thread::Current(); |
| 279 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 280 | dex_compilation_unit.GetDexFile(), |
| 281 | dex_compilation_unit.GetClassDefIndex()); |
| 282 | } |
| 283 | } |
| 284 | |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 285 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 286 | graph_->GetArena(), |
| 287 | caller_dex_file, |
| 288 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 289 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 290 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | b176d7c | 2015-05-20 18:48:31 +0100 | [diff] [blame] | 291 | invoke_instruction->GetOriginalInvokeType(), |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 292 | graph_->IsDebuggable(), |
| 293 | graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 294 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 295 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 296 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 297 | &dex_compilation_unit, |
| 298 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 299 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 300 | compiler_driver_, |
| 301 | &inline_stats); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 302 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 303 | if (!builder.BuildGraph(*code_item)) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 304 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 305 | << " could not be built, so cannot be inlined"; |
Nicolas Geoffray | 5ae1325 | 2015-05-27 12:53:36 +0100 | [diff] [blame] | 306 | // There could be multiple reasons why the graph could not be built, including |
| 307 | // unaccessible methods/fields due to using a different dex cache. We do not mark |
| 308 | // the method as non-inlineable so that other callers can still try to inline it. |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 309 | return false; |
| 310 | } |
| 311 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 312 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 313 | compiler_driver_->GetInstructionSet())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 314 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 315 | << " cannot be inlined because of the register allocator"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 316 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 320 | if (!callee_graph->TryBuildingSsa()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 321 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 322 | << " could not be transformed to SSA"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 323 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 324 | return false; |
| 325 | } |
| 326 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 327 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 328 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 329 | HConstantFolding fold(callee_graph); |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 330 | ReferenceTypePropagation type_propagation(callee_graph, handles_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 331 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 332 | |
| 333 | HOptimization* optimizations[] = { |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 334 | &dce, |
| 335 | &fold, |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 336 | &type_propagation, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 337 | &simplify, |
| 338 | }; |
| 339 | |
| 340 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 341 | HOptimization* optimization = optimizations[i]; |
| 342 | optimization->Run(); |
| 343 | } |
| 344 | |
| 345 | if (depth_ + 1 < kDepthLimit) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 346 | HInliner inliner(callee_graph, |
| 347 | outer_compilation_unit_, |
| 348 | dex_compilation_unit, |
| 349 | compiler_driver_, |
Nicolas Geoffray | 1d5006c | 2015-06-03 15:04:32 +0100 | [diff] [blame^] | 350 | handles_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 351 | stats_, |
| 352 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 353 | inliner.Run(); |
| 354 | } |
| 355 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 356 | // TODO: We should abort only if all predecessors throw. However, |
| 357 | // HGraph::InlineInto currently does not handle an exit block with |
| 358 | // a throw predecessor. |
| 359 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 360 | if (exit_block == nullptr) { |
| 361 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 362 | << " could not be inlined because it has an infinite loop"; |
| 363 | resolved_method->SetShouldNotInline(); |
| 364 | return false; |
| 365 | } |
| 366 | |
| 367 | bool has_throw_predecessor = false; |
| 368 | for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) { |
| 369 | if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) { |
| 370 | has_throw_predecessor = true; |
| 371 | break; |
| 372 | } |
| 373 | } |
| 374 | if (has_throw_predecessor) { |
| 375 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 376 | << " could not be inlined because one branch always throws"; |
| 377 | resolved_method->SetShouldNotInline(); |
| 378 | return false; |
| 379 | } |
| 380 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 381 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 382 | 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] | 383 | for (; !it.Done(); it.Advance()) { |
| 384 | HBasicBlock* block = it.Current(); |
| 385 | if (block->IsLoopHeader()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 386 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 387 | << " could not be inlined because it contains a loop"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 388 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 389 | return false; |
| 390 | } |
| 391 | |
| 392 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 393 | !instr_it.Done(); |
| 394 | instr_it.Advance()) { |
| 395 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 396 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 397 | if (current->IsInvokeInterface()) { |
| 398 | // Disable inlining of interface calls. The cost in case of entering the |
| 399 | // resolution conflict is currently too high. |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 400 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 401 | << " could not be inlined because it has an interface call."; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 402 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 403 | return false; |
| 404 | } |
| 405 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 406 | if (!same_dex_file && current->NeedsEnvironment()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 407 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 408 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 409 | << " needs an environment and is in a different dex file"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 410 | return false; |
| 411 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 412 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 413 | if (!same_dex_file && current->NeedsDexCache()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 414 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 415 | << " could not be inlined because " << current->DebugName() |
| 416 | << " it is in a different dex file and requires access to the dex cache"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 417 | // Do not flag the method as not-inlineable. A caller within the same |
| 418 | // dex file could still successfully inline it. |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 419 | return false; |
| 420 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 424 | callee_graph->InlineInto(graph_, invoke_instruction); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 425 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 426 | return true; |
| 427 | } |
| 428 | |
| 429 | } // namespace art |