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