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