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" |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 25 | #include "driver/compiler_options.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 26 | #include "driver/dex_compilation_unit.h" |
| 27 | #include "instruction_simplifier.h" |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 28 | #include "intrinsics.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 29 | #include "mirror/class_loader.h" |
| 30 | #include "mirror/dex_cache.h" |
| 31 | #include "nodes.h" |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 32 | #include "optimizing_compiler.h" |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 33 | #include "reference_type_propagation.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 34 | #include "register_allocator.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 35 | #include "ssa_phi_elimination.h" |
| 36 | #include "scoped_thread_state_change.h" |
| 37 | #include "thread.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 38 | #include "dex/verified_method.h" |
| 39 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 40 | |
| 41 | namespace art { |
| 42 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 43 | static constexpr size_t kMaximumNumberOfHInstructions = 12; |
| 44 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 45 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 46 | if (graph_->IsDebuggable()) { |
| 47 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 48 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 49 | return; |
| 50 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 51 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 52 | HBasicBlock* next_block = blocks.Get(0); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 53 | for (size_t i = 0; i < blocks.Size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 54 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 55 | // This avoids doing the inlining work again on the inlined blocks. |
| 56 | if (blocks.Get(i) != next_block) { |
| 57 | continue; |
| 58 | } |
| 59 | HBasicBlock* block = next_block; |
| 60 | next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 61 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 62 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 63 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 64 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 65 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 66 | // We use the original invoke type to ensure the resolution of the called method |
| 67 | // works properly. |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 68 | if (!TryInline(call)) { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 69 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 70 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 71 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 72 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 73 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 74 | } |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 75 | } else { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 76 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 77 | std::string callee_name = |
| 78 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
| 79 | bool must_not_inline = callee_name.find("$noinline$") != std::string::npos; |
| 80 | CHECK(!must_not_inline) << "Should not have inlined " << callee_name; |
| 81 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 82 | } |
| 83 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 84 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 85 | } |
| 86 | } |
| 87 | } |
| 88 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 89 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 90 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 91 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 92 | } |
| 93 | |
| 94 | /** |
| 95 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 96 | * the actual runtime target of an interface or virtual call. |
| 97 | * Return nullptr if the runtime target cannot be proven. |
| 98 | */ |
| 99 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 100 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 101 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 102 | // No need to lookup further, the resolved method will be the target. |
| 103 | return resolved_method; |
| 104 | } |
| 105 | |
| 106 | HInstruction* receiver = invoke->InputAt(0); |
| 107 | if (receiver->IsNullCheck()) { |
| 108 | // Due to multiple levels of inlining within the same pass, it might be that |
| 109 | // null check does not have the reference type of the actual receiver. |
| 110 | receiver = receiver->InputAt(0); |
| 111 | } |
| 112 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 113 | DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName(); |
| 114 | if (!info.IsExact()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 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; |
| 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; |
| 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 | |
| 163 | static uint32_t FindMethodIndexIn(ArtMethod* method, |
| 164 | const DexFile& dex_file, |
| 165 | uint32_t referrer_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 166 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 167 | if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) { |
| 168 | return method->GetDexMethodIndex(); |
| 169 | } else { |
| 170 | return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index); |
| 171 | } |
| 172 | } |
| 173 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 174 | bool HInliner::TryInline(HInvoke* invoke_instruction) { |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 175 | uint32_t method_index = invoke_instruction->GetDexMethodIndex(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 176 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 177 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 178 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 179 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 180 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 181 | // We can query the dex cache directly. The verifier has populated it already. |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 182 | ArtMethod* resolved_method; |
| 183 | if (invoke_instruction->IsInvokeStaticOrDirect()) { |
| 184 | MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod(); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 185 | resolved_method = class_linker->FindDexCache(soa.Self(), *ref.dex_file)->GetResolvedMethod( |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 186 | ref.dex_method_index, class_linker->GetImagePointerSize()); |
| 187 | } else { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 188 | resolved_method = class_linker->FindDexCache(soa.Self(), caller_dex_file)->GetResolvedMethod( |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 189 | method_index, class_linker->GetImagePointerSize()); |
| 190 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 191 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 192 | if (resolved_method == nullptr) { |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 193 | // Method cannot be resolved if it is in another dex file we do not have access to. |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 194 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 195 | return false; |
| 196 | } |
| 197 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 198 | if (!invoke_instruction->IsInvokeStaticOrDirect()) { |
| 199 | resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
| 200 | if (resolved_method == nullptr) { |
| 201 | VLOG(compiler) << "Interface or virtual call to " |
| 202 | << PrettyMethod(method_index, caller_dex_file) |
| 203 | << " could not be statically determined"; |
| 204 | return false; |
| 205 | } |
| 206 | // We have found a method, but we need to find where that method is for the caller's |
| 207 | // dex file. |
| 208 | method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index); |
| 209 | if (method_index == DexFile::kDexNoIndex) { |
| 210 | VLOG(compiler) << "Interface or virtual call to " |
| 211 | << PrettyMethod(resolved_method) |
| 212 | << " cannot be inlined because unaccessible to caller"; |
| 213 | return false; |
| 214 | } |
| 215 | } |
| 216 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 217 | bool same_dex_file = |
| 218 | IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *resolved_method->GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 219 | |
| 220 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 221 | |
| 222 | if (code_item == nullptr) { |
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 | << " is not inlined because it is native"; |
| 225 | return false; |
| 226 | } |
| 227 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 228 | size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits(); |
| 229 | if (code_item->insns_size_in_code_units_ > inline_max_code_units) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 230 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 231 | << " is too big to inline"; |
| 232 | return false; |
| 233 | } |
| 234 | |
| 235 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 236 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 237 | << " is not inlined because of try block"; |
| 238 | return false; |
| 239 | } |
| 240 | |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 241 | uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex(); |
| 242 | if (!compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 243 | resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 244 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 245 | << " couldn't be verified, so it cannot be inlined"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 249 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 250 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 251 | // Case of a static method that cannot be inlined because it implicitly |
| 252 | // requires an initialization check of its declaring class. |
| 253 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 254 | << " is not inlined because it is static and requires a clinit" |
| 255 | << " check that cannot be emitted due to Dex cache limitations"; |
| 256 | return false; |
| 257 | } |
| 258 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 259 | if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 260 | return false; |
| 261 | } |
| 262 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 263 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 264 | MaybeRecordStat(kInlinedInvoke); |
| 265 | return true; |
| 266 | } |
| 267 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 268 | bool HInliner::TryBuildAndInline(ArtMethod* resolved_method, |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 269 | HInvoke* invoke_instruction, |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 270 | bool same_dex_file) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 271 | ScopedObjectAccess soa(Thread::Current()); |
| 272 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 273 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 274 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 275 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 276 | DexCompilationUnit dex_compilation_unit( |
| 277 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 278 | caller_compilation_unit_.GetClassLoader(), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 279 | class_linker, |
Nicolas Geoffray | 8dbf0cf | 2015-08-11 02:14:38 +0000 | [diff] [blame] | 280 | callee_dex_file, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 281 | code_item, |
| 282 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 8dbf0cf | 2015-08-11 02:14:38 +0000 | [diff] [blame] | 283 | method_index, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 284 | resolved_method->GetAccessFlags(), |
Nicolas Geoffray | 8dbf0cf | 2015-08-11 02:14:38 +0000 | [diff] [blame] | 285 | compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index)); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 286 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 287 | bool requires_ctor_barrier = false; |
| 288 | |
| 289 | if (dex_compilation_unit.IsConstructor()) { |
| 290 | // If it's a super invocation and we already generate a barrier there's no need |
| 291 | // to generate another one. |
| 292 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 293 | // same as the local "this" pointer then we must have a super invocation. |
| 294 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 295 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 296 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 297 | requires_ctor_barrier = false; |
| 298 | } else { |
| 299 | Thread* self = Thread::Current(); |
| 300 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 301 | dex_compilation_unit.GetDexFile(), |
| 302 | dex_compilation_unit.GetClassDefIndex()); |
| 303 | } |
| 304 | } |
| 305 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 306 | InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType(); |
| 307 | if (invoke_type == kInterface) { |
| 308 | // We have statically resolved the dispatch. To please the class linker |
| 309 | // at runtime, we change this call as if it was a virtual call. |
| 310 | invoke_type = kVirtual; |
| 311 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 312 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 313 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 314 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 315 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 316 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 317 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 318 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 319 | graph_->IsDebuggable(), |
| 320 | graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 321 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 322 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 323 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 324 | &dex_compilation_unit, |
| 325 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 326 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 327 | compiler_driver_, |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 328 | &inline_stats, |
| 329 | resolved_method->GetQuickenedInfo()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 330 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 331 | if (!builder.BuildGraph(*code_item)) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 332 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 333 | << " could not be built, so cannot be inlined"; |
| 334 | return false; |
| 335 | } |
| 336 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 337 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 338 | compiler_driver_->GetInstructionSet())) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 339 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 340 | << " cannot be inlined because of the register allocator"; |
| 341 | return false; |
| 342 | } |
| 343 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 344 | if (!callee_graph->TryBuildingSsa()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 345 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 346 | << " could not be transformed to SSA"; |
| 347 | return false; |
| 348 | } |
| 349 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 350 | size_t parameter_index = 0; |
| 351 | for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions()); |
| 352 | !instructions.Done(); |
| 353 | instructions.Advance()) { |
| 354 | HInstruction* current = instructions.Current(); |
| 355 | if (current->IsParameterValue()) { |
| 356 | HInstruction* argument = invoke_instruction->InputAt(parameter_index++); |
| 357 | if (argument->IsNullConstant()) { |
| 358 | current->ReplaceWith(callee_graph->GetNullConstant()); |
| 359 | } else if (argument->IsIntConstant()) { |
| 360 | current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue())); |
| 361 | } else if (argument->IsLongConstant()) { |
| 362 | current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue())); |
| 363 | } else if (argument->IsFloatConstant()) { |
| 364 | current->ReplaceWith( |
| 365 | callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue())); |
| 366 | } else if (argument->IsDoubleConstant()) { |
| 367 | current->ReplaceWith( |
| 368 | callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue())); |
| 369 | } else if (argument->GetType() == Primitive::kPrimNot) { |
| 370 | current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo()); |
| 371 | current->AsParameterValue()->SetCanBeNull(argument->CanBeNull()); |
| 372 | } |
| 373 | } |
| 374 | } |
| 375 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 376 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 377 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 378 | HConstantFolding fold(callee_graph); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 379 | ReferenceTypePropagation type_propagation(callee_graph, handles_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 380 | InstructionSimplifier simplify(callee_graph, stats_); |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 381 | IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 382 | |
| 383 | HOptimization* optimizations[] = { |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 384 | &intrinsics, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 385 | &type_propagation, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 386 | &simplify, |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 387 | &dce, |
| 388 | &fold, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 389 | }; |
| 390 | |
| 391 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 392 | HOptimization* optimization = optimizations[i]; |
| 393 | optimization->Run(); |
| 394 | } |
| 395 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 396 | size_t number_of_instructions_budget = kMaximumNumberOfHInstructions; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 397 | if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 398 | HInliner inliner(callee_graph, |
| 399 | outer_compilation_unit_, |
| 400 | dex_compilation_unit, |
| 401 | compiler_driver_, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 402 | handles_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 403 | stats_, |
| 404 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 405 | inliner.Run(); |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 406 | number_of_instructions_budget += inliner.number_of_inlined_instructions_; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 407 | } |
| 408 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 409 | // TODO: We should abort only if all predecessors throw. However, |
| 410 | // HGraph::InlineInto currently does not handle an exit block with |
| 411 | // a throw predecessor. |
| 412 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 413 | if (exit_block == nullptr) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 414 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 415 | << " could not be inlined because it has an infinite loop"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 416 | return false; |
| 417 | } |
| 418 | |
| 419 | bool has_throw_predecessor = false; |
| 420 | for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) { |
| 421 | if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) { |
| 422 | has_throw_predecessor = true; |
| 423 | break; |
| 424 | } |
| 425 | } |
| 426 | if (has_throw_predecessor) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 427 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 428 | << " could not be inlined because one branch always throws"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 429 | return false; |
| 430 | } |
| 431 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 432 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 433 | it.Advance(); // Past the entry block, it does not contain instructions that prevent inlining. |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 434 | size_t number_of_instructions = 0; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 435 | for (; !it.Done(); it.Advance()) { |
| 436 | HBasicBlock* block = it.Current(); |
| 437 | if (block->IsLoopHeader()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 438 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 439 | << " could not be inlined because it contains a loop"; |
| 440 | return false; |
| 441 | } |
| 442 | |
| 443 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 444 | !instr_it.Done(); |
| 445 | instr_it.Advance()) { |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 446 | if (number_of_instructions++ == number_of_instructions_budget) { |
| 447 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
| 448 | << " could not be inlined because it is too big."; |
| 449 | return false; |
| 450 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 451 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 452 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 453 | if (current->IsInvokeInterface()) { |
| 454 | // Disable inlining of interface calls. The cost in case of entering the |
| 455 | // resolution conflict is currently too high. |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 456 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 457 | << " could not be inlined because it has an interface call."; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 458 | return false; |
| 459 | } |
| 460 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 461 | if (!same_dex_file && current->NeedsEnvironment()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 462 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 463 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 464 | << " needs an environment and is in a different dex file"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 465 | return false; |
| 466 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 467 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 468 | if (!same_dex_file && current->NeedsDexCache()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 469 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 470 | << " could not be inlined because " << current->DebugName() |
| 471 | << " it is in a different dex file and requires access to the dex cache"; |
| 472 | return false; |
| 473 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 474 | } |
| 475 | } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 476 | number_of_inlined_instructions_ += number_of_instructions; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 477 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 478 | HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction); |
| 479 | |
| 480 | // When merging the graph we might create a new NullConstant in the caller graph which does |
| 481 | // not have the chance to be typed. We assign the correct type here so that we can keep the |
| 482 | // assertion that every reference has a valid type. This also simplifies checks along the way. |
| 483 | HNullConstant* null_constant = graph_->GetNullConstant(); |
| 484 | if (!null_constant->GetReferenceTypeInfo().IsValid()) { |
| 485 | ReferenceTypeInfo::TypeHandle obj_handle = |
| 486 | handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject)); |
| 487 | null_constant->SetReferenceTypeInfo( |
| 488 | ReferenceTypeInfo::Create(obj_handle, false /* is_exact */)); |
| 489 | } |
| 490 | |
| 491 | if ((return_replacement != nullptr) |
| 492 | && (return_replacement->GetType() == Primitive::kPrimNot)) { |
| 493 | if (!return_replacement->GetReferenceTypeInfo().IsValid()) { |
| 494 | // Make sure that we have a valid type for the return. We may get an invalid one when |
| 495 | // we inline invokes with multiple branches and create a Phi for the result. |
| 496 | // TODO: we could be more precise by merging the phi inputs but that requires |
| 497 | // some functionality from the reference type propagation. |
| 498 | DCHECK(return_replacement->IsPhi()); |
| 499 | ReferenceTypeInfo::TypeHandle return_handle = |
| 500 | handles_->NewHandle(resolved_method->GetReturnType()); |
| 501 | return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create( |
| 502 | return_handle, return_handle->IsFinal() /* is_exact */)); |
| 503 | } |
| 504 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 505 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 506 | return true; |
| 507 | } |
| 508 | |
| 509 | } // namespace art |