Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "inliner.h" |
| 18 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 20 | #include "builder.h" |
| 21 | #include "class_linker.h" |
| 22 | #include "constant_folding.h" |
| 23 | #include "dead_code_elimination.h" |
| 24 | #include "driver/compiler_driver-inl.h" |
| 25 | #include "driver/dex_compilation_unit.h" |
| 26 | #include "instruction_simplifier.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
| 28 | #include "mirror/dex_cache.h" |
| 29 | #include "nodes.h" |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 30 | #include "optimizing_compiler.h" |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 31 | #include "reference_type_propagation.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 32 | #include "register_allocator.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 33 | #include "ssa_phi_elimination.h" |
| 34 | #include "scoped_thread_state_change.h" |
| 35 | #include "thread.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 36 | #include "dex/verified_method.h" |
| 37 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 38 | |
| 39 | namespace art { |
| 40 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 41 | static constexpr int kMaxInlineCodeUnits = 18; |
| 42 | static constexpr int kDepthLimit = 3; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 43 | |
| 44 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 45 | if (graph_->IsDebuggable()) { |
| 46 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 47 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 48 | return; |
| 49 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 50 | const GrowableArray<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 51 | HBasicBlock* next_block = blocks.Get(0); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 52 | for (size_t i = 0; i < blocks.Size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 53 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 54 | // This avoids doing the inlining work again on the inlined blocks. |
| 55 | if (blocks.Get(i) != next_block) { |
| 56 | continue; |
| 57 | } |
| 58 | HBasicBlock* block = next_block; |
| 59 | next_block = (i == blocks.Size() - 1) ? nullptr : blocks.Get(i + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 60 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 61 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 62 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 63 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 64 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 65 | // We use the original invoke type to ensure the resolution of the called method |
| 66 | // works properly. |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 67 | if (!TryInline(call, call->GetDexMethodIndex())) { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 68 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 69 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 70 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 71 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 72 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 73 | } |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 74 | } else { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 75 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 76 | std::string callee_name = |
| 77 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
| 78 | bool must_not_inline = callee_name.find("$noinline$") != std::string::npos; |
| 79 | CHECK(!must_not_inline) << "Should not have inlined " << callee_name; |
| 80 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 81 | } |
| 82 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 83 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 84 | } |
| 85 | } |
| 86 | } |
| 87 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 88 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 89 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 90 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 91 | } |
| 92 | |
| 93 | /** |
| 94 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 95 | * the actual runtime target of an interface or virtual call. |
| 96 | * Return nullptr if the runtime target cannot be proven. |
| 97 | */ |
| 98 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 99 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 100 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 101 | // No need to lookup further, the resolved method will be the target. |
| 102 | return resolved_method; |
| 103 | } |
| 104 | |
| 105 | HInstruction* receiver = invoke->InputAt(0); |
| 106 | if (receiver->IsNullCheck()) { |
| 107 | // Due to multiple levels of inlining within the same pass, it might be that |
| 108 | // null check does not have the reference type of the actual receiver. |
| 109 | receiver = receiver->InputAt(0); |
| 110 | } |
| 111 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
Calin Juravle | b734808 | 2015-07-28 11:52:02 +0000 | [diff] [blame] | 112 | if (info.IsTop()) { |
| 113 | // We have no information on the receiver. |
| 114 | return nullptr; |
| 115 | } else 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 | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 175 | bool HInliner::TryInline(HInvoke* invoke_instruction, uint32_t method_index) const { |
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. |
| 182 | ArtMethod* resolved_method = class_linker->FindDexCache(caller_dex_file)->GetResolvedMethod( |
| 183 | method_index, class_linker->GetImagePointerSize()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 184 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 185 | if (resolved_method == nullptr) { |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 186 | // 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] | 187 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 188 | return false; |
| 189 | } |
| 190 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 191 | if (!invoke_instruction->IsInvokeStaticOrDirect()) { |
| 192 | resolved_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
| 193 | if (resolved_method == nullptr) { |
| 194 | VLOG(compiler) << "Interface or virtual call to " |
| 195 | << PrettyMethod(method_index, caller_dex_file) |
| 196 | << " could not be statically determined"; |
| 197 | return false; |
| 198 | } |
| 199 | // We have found a method, but we need to find where that method is for the caller's |
| 200 | // dex file. |
| 201 | method_index = FindMethodIndexIn(resolved_method, caller_dex_file, method_index); |
| 202 | if (method_index == DexFile::kDexNoIndex) { |
| 203 | VLOG(compiler) << "Interface or virtual call to " |
| 204 | << PrettyMethod(resolved_method) |
| 205 | << " cannot be inlined because unaccessible to caller"; |
| 206 | return false; |
| 207 | } |
| 208 | } |
| 209 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 210 | bool same_dex_file = true; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 211 | const DexFile& outer_dex_file = *outer_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 212 | if (resolved_method->GetDexFile()->GetLocation().compare(outer_dex_file.GetLocation()) != 0) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 213 | same_dex_file = false; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 214 | } |
| 215 | |
| 216 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
| 217 | |
| 218 | if (code_item == nullptr) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 219 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 220 | << " is not inlined because it is native"; |
| 221 | return false; |
| 222 | } |
| 223 | |
| 224 | if (code_item->insns_size_in_code_units_ > kMaxInlineCodeUnits) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 225 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 226 | << " is too big to inline"; |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 231 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 232 | << " is not inlined because of try block"; |
| 233 | return false; |
| 234 | } |
| 235 | |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 236 | uint16_t class_def_idx = resolved_method->GetDeclaringClass()->GetDexClassDefIndex(); |
| 237 | if (!compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 238 | resolved_method->GetDexMethodIndex(), class_def_idx, *resolved_method->GetDexFile())) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 239 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 240 | << " couldn't be verified, so it cannot be inlined"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 241 | return false; |
| 242 | } |
| 243 | |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 244 | if (resolved_method->ShouldNotInline()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 245 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 246 | << " was already flagged as non inlineable"; |
| 247 | return false; |
| 248 | } |
| 249 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 250 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 251 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 252 | // Case of a static method that cannot be inlined because it implicitly |
| 253 | // requires an initialization check of its declaring class. |
| 254 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 255 | << " is not inlined because it is static and requires a clinit" |
| 256 | << " check that cannot be emitted due to Dex cache limitations"; |
| 257 | return false; |
| 258 | } |
| 259 | |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 260 | if (!TryBuildAndInline(resolved_method, invoke_instruction, same_dex_file)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 261 | return false; |
| 262 | } |
| 263 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 264 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 265 | MaybeRecordStat(kInlinedInvoke); |
| 266 | return true; |
| 267 | } |
| 268 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 269 | bool HInliner::TryBuildAndInline(ArtMethod* resolved_method, |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 270 | HInvoke* invoke_instruction, |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 271 | bool same_dex_file) const { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 272 | ScopedObjectAccess soa(Thread::Current()); |
| 273 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 274 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 275 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 276 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 277 | DexCompilationUnit dex_compilation_unit( |
| 278 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 279 | caller_compilation_unit_.GetClassLoader(), |
| 280 | caller_compilation_unit_.GetClassLinker(), |
| 281 | *resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 282 | code_item, |
| 283 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 284 | resolved_method->GetDexMethodIndex(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 285 | resolved_method->GetAccessFlags(), |
| 286 | nullptr); |
| 287 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 288 | bool requires_ctor_barrier = false; |
| 289 | |
| 290 | if (dex_compilation_unit.IsConstructor()) { |
| 291 | // If it's a super invocation and we already generate a barrier there's no need |
| 292 | // to generate another one. |
| 293 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 294 | // same as the local "this" pointer then we must have a super invocation. |
| 295 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 296 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 297 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 298 | requires_ctor_barrier = false; |
| 299 | } else { |
| 300 | Thread* self = Thread::Current(); |
| 301 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 302 | dex_compilation_unit.GetDexFile(), |
| 303 | dex_compilation_unit.GetClassDefIndex()); |
| 304 | } |
| 305 | } |
| 306 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 307 | InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType(); |
| 308 | if (invoke_type == kInterface) { |
| 309 | // We have statically resolved the dispatch. To please the class linker |
| 310 | // at runtime, we change this call as if it was a virtual call. |
| 311 | invoke_type = kVirtual; |
| 312 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 313 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 314 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 315 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 316 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 317 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 318 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 319 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 320 | graph_->IsDebuggable(), |
| 321 | graph_->GetCurrentInstructionId()); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 322 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 323 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 324 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 325 | &dex_compilation_unit, |
| 326 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 327 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 328 | compiler_driver_, |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 329 | &inline_stats, |
| 330 | resolved_method->GetQuickenedInfo()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 331 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 332 | if (!builder.BuildGraph(*code_item)) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 333 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 334 | << " could not be built, so cannot be inlined"; |
Nicolas Geoffray | 5ae1325 | 2015-05-27 12:53:36 +0100 | [diff] [blame] | 335 | // There could be multiple reasons why the graph could not be built, including |
| 336 | // unaccessible methods/fields due to using a different dex cache. We do not mark |
| 337 | // 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] | 338 | return false; |
| 339 | } |
| 340 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 341 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 342 | compiler_driver_->GetInstructionSet())) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 343 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 344 | << " cannot be inlined because of the register allocator"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 345 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 346 | return false; |
| 347 | } |
| 348 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 349 | if (!callee_graph->TryBuildingSsa()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 350 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 351 | << " could not be transformed to SSA"; |
Nicolas Geoffray | d026143 | 2015-05-26 14:35:06 +0100 | [diff] [blame] | 352 | resolved_method->SetShouldNotInline(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 353 | return false; |
| 354 | } |
| 355 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 356 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 357 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 358 | HConstantFolding fold(callee_graph); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 359 | ReferenceTypePropagation type_propagation(callee_graph, handles_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 360 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 361 | |
| 362 | HOptimization* optimizations[] = { |
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 | |
| 374 | if (depth_ + 1 < kDepthLimit) { |
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 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 453 | callee_graph->InlineInto(graph_, invoke_instruction); |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 454 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 455 | return true; |
| 456 | } |
| 457 | |
| 458 | } // namespace art |