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 | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 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 | 454a481 | 2015-06-09 10:37: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. |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 66 | if (!TryInline(call, call->GetDexMethodIndex())) { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 67 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 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 { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 74 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 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 | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 87 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 88 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 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) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 98 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 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(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 111 | DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName(); |
| 112 | if (!info.IsExact()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 113 | // We currently only support inlining with known receivers. |
| 114 | // TODO: Remove this check, we should be able to inline final methods |
| 115 | // on unknown receivers. |
| 116 | return nullptr; |
| 117 | } else if (info.GetTypeHandle()->IsInterface()) { |
| 118 | // Statically knowing that the receiver has an interface type cannot |
| 119 | // help us find what is the target method. |
| 120 | return nullptr; |
| 121 | } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) { |
| 122 | // The method that we're trying to call is not in the receiver's class or super classes. |
| 123 | return nullptr; |
| 124 | } |
| 125 | |
| 126 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 127 | size_t pointer_size = cl->GetImagePointerSize(); |
| 128 | if (invoke->IsInvokeInterface()) { |
| 129 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface( |
| 130 | resolved_method, pointer_size); |
| 131 | } else { |
| 132 | DCHECK(invoke->IsInvokeVirtual()); |
| 133 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual( |
| 134 | resolved_method, pointer_size); |
| 135 | } |
| 136 | |
| 137 | if (resolved_method == nullptr) { |
| 138 | // The information we had on the receiver was not enough to find |
| 139 | // the target method. Since we check above the exact type of the receiver, |
| 140 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 141 | return nullptr; |
| 142 | } else if (resolved_method->IsAbstract()) { |
| 143 | // The information we had on the receiver was not enough to find |
| 144 | // the target method. Since we check above the exact type of the receiver, |
| 145 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 146 | return nullptr; |
| 147 | } else if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 148 | // A final method has to be the target method. |
| 149 | return resolved_method; |
| 150 | } else if (info.IsExact()) { |
| 151 | // If we found a method and the receiver's concrete type is statically |
| 152 | // known, we know for sure the target. |
| 153 | return resolved_method; |
| 154 | } else { |
| 155 | // Even if we did find a method, the receiver type was not enough to |
| 156 | // statically find the runtime target. |
| 157 | return nullptr; |
| 158 | } |
| 159 | } |
| 160 | |
| 161 | static uint32_t FindMethodIndexIn(ArtMethod* method, |
| 162 | const DexFile& dex_file, |
| 163 | uint32_t referrer_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 164 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 165 | if (method->GetDexFile()->GetLocation().compare(dex_file.GetLocation()) == 0) { |
| 166 | return method->GetDexMethodIndex(); |
| 167 | } else { |
| 168 | return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index); |
| 169 | } |
| 170 | } |
| 171 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 172 | bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 173 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 174 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 175 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 176 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 177 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 178 | // We can query the dex cache directly. The verifier has populated it already. |
| 179 | ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod( |
| 180 | method_index, class_linker->GetImagePointerSize()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 181 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 182 | if (resolved_method == nullptr) { |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 183 | // 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] | 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 | 454a481 | 2015-06-09 10:37: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 | // We have found a method, but we need to find where that method is for the caller's |
| 197 | // dex file. |
| 198 | method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index); |
| 199 | if (method_index == DexFile::kDexNoIndex) { |
| 200 | VLOG(compiler) << "Interface or virtual call to " |
| 201 | << PrettyMethod(resolved_method) |
| 202 | << " cannot be inlined because unaccessible to caller"; |
| 203 | return false; |
| 204 | } |
| 205 | } |
| 206 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 207 | bool same_dex_file = true; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 208 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 209 | if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 210 | same_dex_file = false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 211 | } |
| 212 | |
| 213 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 214 | |
| 215 | if (code_item == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 216 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 217 | << " is not inlined because it is native"; |
| 218 | return false; |
| 219 | } |
| 220 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 221 | size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits(); |
| 222 | if (code_item->insns_size_in_code_units_ > inline_max_code_units) { |
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 too big to inline"; |
| 225 | return false; |
| 226 | } |
| 227 | |
| 228 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 229 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 230 | << " is not inlined because of try block"; |
| 231 | return false; |
| 232 | } |
| 233 | |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 234 | uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex(); |
| 235 | if (!compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 236 | resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 237 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 238 | << " couldn't be verified, so it cannot be inlined"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 239 | return false; |
| 240 | } |
| 241 | |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 242 | if (resolved_method->ShouldNotInline()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 243 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 244 | << " was already flagged as non inlineable"; |
| 245 | return false; |
| 246 | } |
| 247 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 248 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 249 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 250 | // Case of a static method that cannot be inlined because it implicitly |
| 251 | // requires an initialization check of its declaring class. |
| 252 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 253 | << " is not inlined because it is static and requires a clinit" |
| 254 | << " check that cannot be emitted due to Dex cache limitations"; |
| 255 | return false; |
| 256 | } |
| 257 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 258 | if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 259 | return false; |
| 260 | } |
| 261 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 262 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 263 | MaybeRecordStat(kInlinedInvoke); |
| 264 | return true; |
| 265 | } |
| 266 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 267 | bool HInliner::TryBuildAndInline(ArtMethod* resolved_method, |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 268 | HInvoke* invoke_instruction, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 269 | bool same_dex_file) const { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 270 | ScopedObjectAccess soa(Thread::Current()); |
| 271 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 272 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 273 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 274 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 275 | DexCompilationUnit dex_compilation_unit( |
| 276 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 277 | caller_compilation_unit_.GetClassLoader(), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 278 | class_linker, |
Nicolas Geoffray | f25b661 | 2015-08-07 15:31:10 +0000 | [diff] [blame^] | 279 | *resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 280 | code_item, |
| 281 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | f25b661 | 2015-08-07 15:31:10 +0000 | [diff] [blame^] | 282 | resolved_method->GetDexMethodIndex(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 283 | resolved_method->GetAccessFlags(), |
Nicolas Geoffray | f25b661 | 2015-08-07 15:31:10 +0000 | [diff] [blame^] | 284 | nullptr); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 285 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 286 | bool requires_ctor_barrier = false; |
| 287 | |
| 288 | if (dex_compilation_unit.IsConstructor()) { |
| 289 | // If it's a super invocation and we already generate a barrier there's no need |
| 290 | // to generate another one. |
| 291 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 292 | // same as the local "this" pointer then we must have a super invocation. |
| 293 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 294 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 295 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 296 | requires_ctor_barrier = false; |
| 297 | } else { |
| 298 | Thread* self = Thread::Current(); |
| 299 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 300 | dex_compilation_unit.GetDexFile(), |
| 301 | dex_compilation_unit.GetClassDefIndex()); |
| 302 | } |
| 303 | } |
| 304 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 305 | InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType(); |
| 306 | if (invoke_type == kInterface) { |
| 307 | // We have statically resolved the dispatch. To please the class linker |
| 308 | // at runtime, we change this call as if it was a virtual call. |
| 309 | invoke_type = kVirtual; |
| 310 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 311 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 312 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 313 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 314 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 315 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 316 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 317 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 318 | graph_->IsDebuggable(), |
| 319 | graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 320 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 321 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 322 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 323 | &dex_compilation_unit, |
| 324 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 325 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 326 | compiler_driver_, |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 327 | &inline_stats, |
| 328 | resolved_method->GetQuickenedInfo()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 329 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 330 | if (!builder.BuildGraph(*code_item)) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 331 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 332 | << " could not be built, so cannot be inlined"; |
Nicolas Geoffray | 5ae1325 | 2015-05-27 12:53:36 +0100 | [diff] [blame] | 333 | // There could be multiple reasons why the graph could not be built, including |
| 334 | // unaccessible methods/fields due to using a different dex cache. We do not mark |
| 335 | // 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] | 336 | return false; |
| 337 | } |
| 338 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 339 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 340 | compiler_driver_->GetInstructionSet())) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 341 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 342 | << " cannot be inlined because of the register allocator"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 343 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 344 | return false; |
| 345 | } |
| 346 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 347 | if (!callee_graph->TryBuildingSsa()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 348 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 349 | << " could not be transformed to SSA"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 350 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 351 | return false; |
| 352 | } |
| 353 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 354 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 355 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 356 | HConstantFolding fold(callee_graph); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 357 | ReferenceTypePropagation type_propagation(callee_graph, handles_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 358 | InstructionSimplifier simplify(callee_graph, stats_); |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 359 | IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 360 | |
| 361 | HOptimization* optimizations[] = { |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 362 | &intrinsics, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 363 | &dce, |
| 364 | &fold, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 365 | &type_propagation, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 366 | &simplify, |
| 367 | }; |
| 368 | |
| 369 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 370 | HOptimization* optimization = optimizations[i]; |
| 371 | optimization->Run(); |
| 372 | } |
| 373 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 374 | if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 375 | HInliner inliner(callee_graph, |
| 376 | outer_compilation_unit_, |
| 377 | dex_compilation_unit, |
| 378 | compiler_driver_, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 379 | handles_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 380 | stats_, |
| 381 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 382 | inliner.Run(); |
| 383 | } |
| 384 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 385 | // TODO: We should abort only if all predecessors throw. However, |
| 386 | // HGraph::InlineInto currently does not handle an exit block with |
| 387 | // a throw predecessor. |
| 388 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 389 | if (exit_block == nullptr) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 390 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 391 | << " could not be inlined because it has an infinite loop"; |
| 392 | resolved_method->SetShouldNotInline(); |
| 393 | return false; |
| 394 | } |
| 395 | |
| 396 | bool has_throw_predecessor = false; |
| 397 | for (size_t i = 0, e = exit_block->GetPredecessors().Size(); i < e; ++i) { |
| 398 | if (exit_block->GetPredecessors().Get(i)->GetLastInstruction()->IsThrow()) { |
| 399 | has_throw_predecessor = true; |
| 400 | break; |
| 401 | } |
| 402 | } |
| 403 | if (has_throw_predecessor) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 404 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 405 | << " could not be inlined because one branch always throws"; |
| 406 | resolved_method->SetShouldNotInline(); |
| 407 | return false; |
| 408 | } |
| 409 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 410 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 411 | 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] | 412 | for (; !it.Done(); it.Advance()) { |
| 413 | HBasicBlock* block = it.Current(); |
| 414 | if (block->IsLoopHeader()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 415 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 416 | << " could not be inlined because it contains a loop"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 417 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 418 | return false; |
| 419 | } |
| 420 | |
| 421 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 422 | !instr_it.Done(); |
| 423 | instr_it.Advance()) { |
| 424 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 425 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 426 | if (current->IsInvokeInterface()) { |
| 427 | // Disable inlining of interface calls. The cost in case of entering the |
| 428 | // resolution conflict is currently too high. |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 429 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 430 | << " could not be inlined because it has an interface call."; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 431 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 432 | return false; |
| 433 | } |
| 434 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 435 | if (!same_dex_file && current->NeedsEnvironment()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 436 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 437 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 438 | << " needs an environment and is in a different dex file"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 439 | return false; |
| 440 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 441 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 442 | if (!same_dex_file && current->NeedsDexCache()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 443 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 444 | << " could not be inlined because " << current->DebugName() |
| 445 | << " 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] | 446 | // Do not flag the method as not-inlineable. A caller within the same |
| 447 | // dex file could still successfully inline it. |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 448 | return false; |
| 449 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 453 | HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction); |
| 454 | |
| 455 | // When merging the graph we might create a new NullConstant in the caller graph which does |
| 456 | // not have the chance to be typed. We assign the correct type here so that we can keep the |
| 457 | // assertion that every reference has a valid type. This also simplifies checks along the way. |
| 458 | HNullConstant* null_constant = graph_->GetNullConstant(); |
| 459 | if (!null_constant->GetReferenceTypeInfo().IsValid()) { |
| 460 | ReferenceTypeInfo::TypeHandle obj_handle = |
| 461 | handles_->NewHandle(class_linker->GetClassRoot(ClassLinker::kJavaLangObject)); |
| 462 | null_constant->SetReferenceTypeInfo( |
| 463 | ReferenceTypeInfo::Create(obj_handle, false /* is_exact */)); |
| 464 | } |
| 465 | |
| 466 | if ((return_replacement != nullptr) |
| 467 | && (return_replacement->GetType() == Primitive::kPrimNot)) { |
| 468 | if (!return_replacement->GetReferenceTypeInfo().IsValid()) { |
| 469 | // Make sure that we have a valid type for the return. We may get an invalid one when |
| 470 | // we inline invokes with multiple branches and create a Phi for the result. |
| 471 | // TODO: we could be more precise by merging the phi inputs but that requires |
| 472 | // some functionality from the reference type propagation. |
| 473 | DCHECK(return_replacement->IsPhi()); |
| 474 | ReferenceTypeInfo::TypeHandle return_handle = |
| 475 | handles_->NewHandle(resolved_method->GetReturnType()); |
| 476 | return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create( |
| 477 | return_handle, return_handle->IsFinal() /* is_exact */)); |
| 478 | } |
| 479 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 480 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 481 | return true; |
| 482 | } |
| 483 | |
| 484 | } // namespace art |