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" |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 24 | #include "dex/verified_method.h" |
| 25 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 26 | #include "driver/compiler_driver-inl.h" |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 27 | #include "driver/compiler_options.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 28 | #include "driver/dex_compilation_unit.h" |
| 29 | #include "instruction_simplifier.h" |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 30 | #include "intrinsics.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 31 | #include "mirror/class_loader.h" |
| 32 | #include "mirror/dex_cache.h" |
| 33 | #include "nodes.h" |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 34 | #include "optimizing_compiler.h" |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 35 | #include "reference_type_propagation.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 36 | #include "register_allocator.h" |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 37 | #include "quick/inline_method_analyser.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 38 | #include "sharpening.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 39 | #include "ssa_builder.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 40 | #include "ssa_phi_elimination.h" |
| 41 | #include "scoped_thread_state_change.h" |
| 42 | #include "thread.h" |
| 43 | |
| 44 | namespace art { |
| 45 | |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 46 | static constexpr size_t kMaximumNumberOfHInstructions = 32; |
| 47 | |
| 48 | // Limit the number of dex registers that we accumulate while inlining |
| 49 | // to avoid creating large amount of nested environments. |
| 50 | static constexpr size_t kMaximumNumberOfCumulatedDexRegisters = 64; |
| 51 | |
| 52 | // Avoid inlining within a huge method due to memory pressure. |
| 53 | static constexpr size_t kMaximumCodeUnitSize = 4096; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 54 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 55 | void HInliner::Run() { |
Calin Juravle | 8f96df8 | 2015-07-29 15:58:48 +0100 | [diff] [blame] | 56 | const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions(); |
| 57 | if ((compiler_options.GetInlineDepthLimit() == 0) |
| 58 | || (compiler_options.GetInlineMaxCodeUnits() == 0)) { |
| 59 | return; |
| 60 | } |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 61 | if (caller_compilation_unit_.GetCodeItem()->insns_size_in_code_units_ > kMaximumCodeUnitSize) { |
| 62 | return; |
| 63 | } |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 64 | if (graph_->IsDebuggable()) { |
| 65 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 66 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 67 | return; |
| 68 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 69 | const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
| 70 | DCHECK(!blocks.empty()); |
| 71 | HBasicBlock* next_block = blocks[0]; |
| 72 | for (size_t i = 0; i < blocks.size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 73 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 74 | // This avoids doing the inlining work again on the inlined blocks. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 75 | if (blocks[i] != next_block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 76 | continue; |
| 77 | } |
| 78 | HBasicBlock* block = next_block; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 79 | next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1]; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 80 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 81 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 82 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 83 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 84 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 85 | // We use the original invoke type to ensure the resolution of the called method |
| 86 | // works properly. |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 87 | if (!TryInline(call)) { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 88 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 89 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 90 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 91 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 92 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 93 | } |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 94 | } else { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 95 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 96 | std::string callee_name = |
| 97 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
| 98 | bool must_not_inline = callee_name.find("$noinline$") != std::string::npos; |
| 99 | CHECK(!must_not_inline) << "Should not have inlined " << callee_name; |
| 100 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 101 | } |
| 102 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 103 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 104 | } |
| 105 | } |
| 106 | } |
| 107 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 108 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 109 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 110 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 111 | } |
| 112 | |
| 113 | /** |
| 114 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 115 | * the actual runtime target of an interface or virtual call. |
| 116 | * Return nullptr if the runtime target cannot be proven. |
| 117 | */ |
| 118 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 119 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 120 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 121 | // No need to lookup further, the resolved method will be the target. |
| 122 | return resolved_method; |
| 123 | } |
| 124 | |
| 125 | HInstruction* receiver = invoke->InputAt(0); |
| 126 | if (receiver->IsNullCheck()) { |
| 127 | // Due to multiple levels of inlining within the same pass, it might be that |
| 128 | // null check does not have the reference type of the actual receiver. |
| 129 | receiver = receiver->InputAt(0); |
| 130 | } |
| 131 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 132 | DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName(); |
| 133 | if (!info.IsExact()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 134 | // We currently only support inlining with known receivers. |
| 135 | // TODO: Remove this check, we should be able to inline final methods |
| 136 | // on unknown receivers. |
| 137 | return nullptr; |
| 138 | } else if (info.GetTypeHandle()->IsInterface()) { |
| 139 | // Statically knowing that the receiver has an interface type cannot |
| 140 | // help us find what is the target method. |
| 141 | return nullptr; |
| 142 | } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) { |
| 143 | // The method that we're trying to call is not in the receiver's class or super classes. |
| 144 | return nullptr; |
| 145 | } |
| 146 | |
| 147 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 148 | size_t pointer_size = cl->GetImagePointerSize(); |
| 149 | if (invoke->IsInvokeInterface()) { |
| 150 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface( |
| 151 | resolved_method, pointer_size); |
| 152 | } else { |
| 153 | DCHECK(invoke->IsInvokeVirtual()); |
| 154 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual( |
| 155 | resolved_method, pointer_size); |
| 156 | } |
| 157 | |
| 158 | if (resolved_method == nullptr) { |
| 159 | // The information we had on the receiver was not enough to find |
| 160 | // the target method. Since we check above the exact type of the receiver, |
| 161 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 162 | return nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 163 | } else if (!resolved_method->IsInvokable()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 164 | // The information we had on the receiver was not enough to find |
| 165 | // the target method. Since we check above the exact type of the receiver, |
| 166 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 167 | return nullptr; |
| 168 | } else if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 169 | // A final method has to be the target method. |
| 170 | return resolved_method; |
| 171 | } else if (info.IsExact()) { |
| 172 | // If we found a method and the receiver's concrete type is statically |
| 173 | // known, we know for sure the target. |
| 174 | return resolved_method; |
| 175 | } else { |
| 176 | // Even if we did find a method, the receiver type was not enough to |
| 177 | // statically find the runtime target. |
| 178 | return nullptr; |
| 179 | } |
| 180 | } |
| 181 | |
| 182 | static uint32_t FindMethodIndexIn(ArtMethod* method, |
| 183 | const DexFile& dex_file, |
| 184 | uint32_t referrer_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 185 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 186 | if (IsSameDexFile(*method->GetDexFile(), dex_file)) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 187 | return method->GetDexMethodIndex(); |
| 188 | } else { |
| 189 | return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index); |
| 190 | } |
| 191 | } |
| 192 | |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 193 | static uint32_t FindClassIndexIn(mirror::Class* cls, |
| 194 | const DexFile& dex_file, |
| 195 | Handle<mirror::DexCache> dex_cache) |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 196 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 197 | uint32_t index = DexFile::kDexNoIndex; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 198 | if (cls->GetDexCache() == nullptr) { |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 199 | DCHECK(cls->IsArrayClass()) << PrettyClass(cls); |
| 200 | index = cls->FindTypeIndexInOtherDexFile(dex_file); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 201 | } else if (cls->GetDexTypeIndex() == DexFile::kDexNoIndex16) { |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 202 | DCHECK(cls->IsProxyClass()) << PrettyClass(cls); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 203 | // TODO: deal with proxy classes. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 204 | } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) { |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 205 | index = cls->GetDexTypeIndex(); |
| 206 | } else { |
| 207 | index = cls->FindTypeIndexInOtherDexFile(dex_file); |
| 208 | } |
| 209 | |
| 210 | if (index != DexFile::kDexNoIndex) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 211 | // Update the dex cache to ensure the class is in. The generated code will |
| 212 | // consider it is. We make it safe by updating the dex cache, as other |
| 213 | // dex files might also load the class, and there is no guarantee the dex |
| 214 | // cache of the dex file of the class will be updated. |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 215 | if (dex_cache->GetResolvedType(index) == nullptr) { |
| 216 | dex_cache->SetResolvedType(index, cls); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 217 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 218 | } |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 219 | |
| 220 | return index; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 221 | } |
| 222 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 223 | bool HInliner::TryInline(HInvoke* invoke_instruction) { |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 224 | if (invoke_instruction->IsInvokeUnresolved()) { |
| 225 | return false; // Don't bother to move further if we know the method is unresolved. |
| 226 | } |
| 227 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 228 | uint32_t method_index = invoke_instruction->GetDexMethodIndex(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 229 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 230 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 231 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 232 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 233 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 234 | // 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] | 235 | ArtMethod* resolved_method; |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 236 | ArtMethod* actual_method = nullptr; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 237 | if (invoke_instruction->IsInvokeStaticOrDirect()) { |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 238 | if (invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()) { |
| 239 | VLOG(compiler) << "Not inlining a String.<init> method"; |
| 240 | return false; |
| 241 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 242 | MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod(); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 243 | mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file) |
| 244 | ? caller_compilation_unit_.GetDexCache().Get() |
| 245 | : class_linker->FindDexCache(soa.Self(), *ref.dex_file); |
| 246 | resolved_method = dex_cache->GetResolvedMethod( |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 247 | ref.dex_method_index, class_linker->GetImagePointerSize()); |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 248 | // actual_method == resolved_method for direct or static calls. |
| 249 | actual_method = resolved_method; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 250 | } else { |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 251 | resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod( |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 252 | method_index, class_linker->GetImagePointerSize()); |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 253 | if (resolved_method != nullptr) { |
| 254 | // Check if we can statically find the method. |
| 255 | actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
| 256 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 257 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 258 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 259 | if (resolved_method == nullptr) { |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 260 | // TODO: Can this still happen? |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 261 | // 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] | 262 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 263 | return false; |
| 264 | } |
| 265 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 266 | if (actual_method != nullptr) { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 267 | return TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ true); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 268 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 269 | |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 270 | DCHECK(!invoke_instruction->IsInvokeStaticOrDirect()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 271 | |
| 272 | // Check if we can use an inline cache. |
| 273 | ArtMethod* caller = graph_->GetArtMethod(); |
| 274 | size_t pointer_size = class_linker->GetImagePointerSize(); |
| 275 | // Under JIT, we should always know the caller. |
| 276 | DCHECK(!Runtime::Current()->UseJit() || (caller != nullptr)); |
| 277 | if (caller != nullptr && caller->GetProfilingInfo(pointer_size) != nullptr) { |
| 278 | ProfilingInfo* profiling_info = caller->GetProfilingInfo(pointer_size); |
| 279 | const InlineCache& ic = *profiling_info->GetInlineCache(invoke_instruction->GetDexPc()); |
| 280 | if (ic.IsUnitialized()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 281 | VLOG(compiler) << "Interface or virtual call to " |
| 282 | << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 283 | << " is not hit and not inlined"; |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 284 | return false; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 285 | } else if (ic.IsMonomorphic()) { |
| 286 | MaybeRecordStat(kMonomorphicCall); |
| 287 | return TryInlineMonomorphicCall(invoke_instruction, resolved_method, ic); |
| 288 | } else if (ic.IsPolymorphic()) { |
| 289 | MaybeRecordStat(kPolymorphicCall); |
| 290 | return TryInlinePolymorphicCall(invoke_instruction, resolved_method, ic); |
| 291 | } else { |
| 292 | DCHECK(ic.IsMegamorphic()); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 293 | VLOG(compiler) << "Interface or virtual call to " |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 294 | << PrettyMethod(method_index, caller_dex_file) |
| 295 | << " is megamorphic and not inlined"; |
| 296 | MaybeRecordStat(kMegamorphicCall); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 297 | return false; |
| 298 | } |
| 299 | } |
| 300 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 301 | VLOG(compiler) << "Interface or virtual call to " |
| 302 | << PrettyMethod(method_index, caller_dex_file) |
| 303 | << " could not be statically determined"; |
| 304 | return false; |
| 305 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 306 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 307 | HInstanceFieldGet* HInliner::BuildGetReceiverClass(ClassLinker* class_linker, |
| 308 | HInstruction* receiver, |
| 309 | uint32_t dex_pc) const { |
| 310 | ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0); |
| 311 | DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_"); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 312 | HInstanceFieldGet* result = new (graph_->GetArena()) HInstanceFieldGet( |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 313 | receiver, |
| 314 | Primitive::kPrimNot, |
| 315 | field->GetOffset(), |
| 316 | field->IsVolatile(), |
| 317 | field->GetDexFieldIndex(), |
| 318 | field->GetDeclaringClass()->GetDexClassDefIndex(), |
| 319 | *field->GetDexFile(), |
| 320 | handles_->NewHandle(field->GetDexCache()), |
| 321 | dex_pc); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 322 | // The class of a field is effectively final, and does not have any memory dependencies. |
| 323 | result->SetSideEffects(SideEffects::None()); |
| 324 | return result; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 325 | } |
| 326 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 327 | bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction, |
| 328 | ArtMethod* resolved_method, |
| 329 | const InlineCache& ic) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 330 | DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface()) |
| 331 | << invoke_instruction->DebugName(); |
| 332 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 333 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 334 | uint32_t class_index = FindClassIndexIn( |
| 335 | ic.GetMonomorphicType(), caller_dex_file, caller_compilation_unit_.GetDexCache()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 336 | if (class_index == DexFile::kDexNoIndex) { |
| 337 | VLOG(compiler) << "Call to " << PrettyMethod(resolved_method) |
| 338 | << " from inline cache is not inlined because its class is not" |
| 339 | << " accessible to the caller"; |
| 340 | return false; |
| 341 | } |
| 342 | |
| 343 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 344 | size_t pointer_size = class_linker->GetImagePointerSize(); |
| 345 | if (invoke_instruction->IsInvokeInterface()) { |
| 346 | resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForInterface( |
| 347 | resolved_method, pointer_size); |
| 348 | } else { |
| 349 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
| 350 | resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForVirtual( |
| 351 | resolved_method, pointer_size); |
| 352 | } |
| 353 | DCHECK(resolved_method != nullptr); |
| 354 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 355 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 356 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 357 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 358 | if (!TryInlineAndReplace(invoke_instruction, resolved_method, /* do_rtp */ false)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 359 | return false; |
| 360 | } |
| 361 | |
| 362 | // We successfully inlined, now add a guard. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 363 | bool is_referrer = |
| 364 | (ic.GetMonomorphicType() == outermost_graph_->GetArtMethod()->GetDeclaringClass()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 365 | AddTypeGuard(receiver, |
| 366 | cursor, |
| 367 | bb_cursor, |
| 368 | class_index, |
| 369 | is_referrer, |
| 370 | invoke_instruction, |
| 371 | /* with_deoptimization */ true); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 372 | |
| 373 | // Run type propagation to get the guard typed, and eventually propagate the |
| 374 | // type of the receiver. |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 375 | ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 376 | rtp_fixup.Run(); |
| 377 | |
| 378 | MaybeRecordStat(kInlinedMonomorphicCall); |
| 379 | return true; |
| 380 | } |
| 381 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 382 | HInstruction* HInliner::AddTypeGuard(HInstruction* receiver, |
| 383 | HInstruction* cursor, |
| 384 | HBasicBlock* bb_cursor, |
| 385 | uint32_t class_index, |
| 386 | bool is_referrer, |
| 387 | HInstruction* invoke_instruction, |
| 388 | bool with_deoptimization) { |
| 389 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 390 | HInstanceFieldGet* receiver_class = BuildGetReceiverClass( |
| 391 | class_linker, receiver, invoke_instruction->GetDexPc()); |
| 392 | |
| 393 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 394 | // Note that we will just compare the classes, so we don't need Java semantics access checks. |
| 395 | // Also, the caller of `AddTypeGuard` must have guaranteed that the class is in the dex cache. |
| 396 | HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(), |
| 397 | class_index, |
| 398 | caller_dex_file, |
| 399 | is_referrer, |
| 400 | invoke_instruction->GetDexPc(), |
| 401 | /* needs_access_check */ false, |
| 402 | /* is_in_dex_cache */ true); |
| 403 | |
| 404 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, receiver_class); |
| 405 | // TODO: Extend reference type propagation to understand the guard. |
| 406 | if (cursor != nullptr) { |
| 407 | bb_cursor->InsertInstructionAfter(receiver_class, cursor); |
| 408 | } else { |
| 409 | bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction()); |
| 410 | } |
| 411 | bb_cursor->InsertInstructionAfter(load_class, receiver_class); |
| 412 | bb_cursor->InsertInstructionAfter(compare, load_class); |
| 413 | if (with_deoptimization) { |
| 414 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
| 415 | compare, invoke_instruction->GetDexPc()); |
| 416 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 417 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 418 | } |
| 419 | return compare; |
| 420 | } |
| 421 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 422 | bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 423 | ArtMethod* resolved_method, |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 424 | const InlineCache& ic) { |
| 425 | DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface()) |
| 426 | << invoke_instruction->DebugName(); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 427 | |
| 428 | if (TryInlinePolymorphicCallToSameTarget(invoke_instruction, resolved_method, ic)) { |
| 429 | return true; |
| 430 | } |
| 431 | |
| 432 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 433 | size_t pointer_size = class_linker->GetImagePointerSize(); |
| 434 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 435 | |
| 436 | bool all_targets_inlined = true; |
| 437 | bool one_target_inlined = false; |
| 438 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
| 439 | if (ic.GetTypeAt(i) == nullptr) { |
| 440 | break; |
| 441 | } |
| 442 | ArtMethod* method = nullptr; |
| 443 | if (invoke_instruction->IsInvokeInterface()) { |
| 444 | method = ic.GetTypeAt(i)->FindVirtualMethodForInterface( |
| 445 | resolved_method, pointer_size); |
| 446 | } else { |
| 447 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
| 448 | method = ic.GetTypeAt(i)->FindVirtualMethodForVirtual( |
| 449 | resolved_method, pointer_size); |
| 450 | } |
| 451 | |
| 452 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 453 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 454 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 455 | |
Nicolas Geoffray | 1fe26e1 | 2016-02-18 16:55:42 +0000 | [diff] [blame] | 456 | uint32_t class_index = FindClassIndexIn( |
| 457 | ic.GetTypeAt(i), caller_dex_file, caller_compilation_unit_.GetDexCache()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 458 | HInstruction* return_replacement = nullptr; |
| 459 | if (class_index == DexFile::kDexNoIndex || |
| 460 | !TryBuildAndInline(invoke_instruction, method, &return_replacement)) { |
| 461 | all_targets_inlined = false; |
| 462 | } else { |
| 463 | one_target_inlined = true; |
| 464 | bool is_referrer = (ic.GetTypeAt(i) == outermost_graph_->GetArtMethod()->GetDeclaringClass()); |
| 465 | |
| 466 | // If we have inlined all targets before, and this receiver is the last seen, |
| 467 | // we deoptimize instead of keeping the original invoke instruction. |
| 468 | bool deoptimize = all_targets_inlined && |
| 469 | (i != InlineCache::kIndividualCacheSize - 1) && |
| 470 | (ic.GetTypeAt(i + 1) == nullptr); |
| 471 | HInstruction* compare = AddTypeGuard( |
| 472 | receiver, cursor, bb_cursor, class_index, is_referrer, invoke_instruction, deoptimize); |
| 473 | if (deoptimize) { |
| 474 | if (return_replacement != nullptr) { |
| 475 | invoke_instruction->ReplaceWith(return_replacement); |
| 476 | } |
| 477 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
| 478 | // Because the inline cache data can be populated concurrently, we force the end of the |
| 479 | // iteration. Otherhwise, we could see a new receiver type. |
| 480 | break; |
| 481 | } else { |
| 482 | CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction); |
| 483 | } |
| 484 | } |
| 485 | } |
| 486 | |
| 487 | if (!one_target_inlined) { |
| 488 | VLOG(compiler) << "Call to " << PrettyMethod(resolved_method) |
| 489 | << " from inline cache is not inlined because none" |
| 490 | << " of its targets could be inlined"; |
| 491 | return false; |
| 492 | } |
| 493 | MaybeRecordStat(kInlinedPolymorphicCall); |
| 494 | |
| 495 | // Run type propagation to get the guards typed. |
| 496 | ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false); |
| 497 | rtp_fixup.Run(); |
| 498 | return true; |
| 499 | } |
| 500 | |
| 501 | void HInliner::CreateDiamondPatternForPolymorphicInline(HInstruction* compare, |
| 502 | HInstruction* return_replacement, |
| 503 | HInstruction* invoke_instruction) { |
| 504 | uint32_t dex_pc = invoke_instruction->GetDexPc(); |
| 505 | HBasicBlock* cursor_block = compare->GetBlock(); |
| 506 | HBasicBlock* original_invoke_block = invoke_instruction->GetBlock(); |
| 507 | ArenaAllocator* allocator = graph_->GetArena(); |
| 508 | |
| 509 | // Spit the block after the compare: `cursor_block` will now be the start of the diamond, |
| 510 | // and the returned block is the start of the then branch (that could contain multiple blocks). |
| 511 | HBasicBlock* then = cursor_block->SplitAfterForInlining(compare); |
| 512 | |
| 513 | // Split the block containing the invoke before and after the invoke. The returned block |
| 514 | // of the split before will contain the invoke and will be the otherwise branch of |
| 515 | // the diamond. The returned block of the split after will be the merge block |
| 516 | // of the diamond. |
| 517 | HBasicBlock* end_then = invoke_instruction->GetBlock(); |
| 518 | HBasicBlock* otherwise = end_then->SplitBeforeForInlining(invoke_instruction); |
| 519 | HBasicBlock* merge = otherwise->SplitAfterForInlining(invoke_instruction); |
| 520 | |
| 521 | // If the methods we are inlining return a value, we create a phi in the merge block |
| 522 | // that will have the `invoke_instruction and the `return_replacement` as inputs. |
| 523 | if (return_replacement != nullptr) { |
| 524 | HPhi* phi = new (allocator) HPhi( |
| 525 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke_instruction->GetType()), dex_pc); |
| 526 | merge->AddPhi(phi); |
| 527 | invoke_instruction->ReplaceWith(phi); |
| 528 | phi->AddInput(return_replacement); |
| 529 | phi->AddInput(invoke_instruction); |
| 530 | } |
| 531 | |
| 532 | // Add the control flow instructions. |
| 533 | otherwise->AddInstruction(new (allocator) HGoto(dex_pc)); |
| 534 | end_then->AddInstruction(new (allocator) HGoto(dex_pc)); |
| 535 | cursor_block->AddInstruction(new (allocator) HIf(compare, dex_pc)); |
| 536 | |
| 537 | // Add the newly created blocks to the graph. |
| 538 | graph_->AddBlock(then); |
| 539 | graph_->AddBlock(otherwise); |
| 540 | graph_->AddBlock(merge); |
| 541 | |
| 542 | // Set up successor (and implictly predecessor) relations. |
| 543 | cursor_block->AddSuccessor(otherwise); |
| 544 | cursor_block->AddSuccessor(then); |
| 545 | end_then->AddSuccessor(merge); |
| 546 | otherwise->AddSuccessor(merge); |
| 547 | |
| 548 | // Set up dominance information. |
| 549 | then->SetDominator(cursor_block); |
| 550 | cursor_block->AddDominatedBlock(then); |
| 551 | otherwise->SetDominator(cursor_block); |
| 552 | cursor_block->AddDominatedBlock(otherwise); |
| 553 | merge->SetDominator(cursor_block); |
| 554 | cursor_block->AddDominatedBlock(merge); |
| 555 | |
| 556 | // Update the revert post order. |
| 557 | size_t index = IndexOfElement(graph_->reverse_post_order_, cursor_block); |
| 558 | MakeRoomFor(&graph_->reverse_post_order_, 1, index); |
| 559 | graph_->reverse_post_order_[++index] = then; |
| 560 | index = IndexOfElement(graph_->reverse_post_order_, end_then); |
| 561 | MakeRoomFor(&graph_->reverse_post_order_, 2, index); |
| 562 | graph_->reverse_post_order_[++index] = otherwise; |
| 563 | graph_->reverse_post_order_[++index] = merge; |
| 564 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 565 | |
Nicolas Geoffray | a1d8ddf | 2016-02-29 11:46:58 +0000 | [diff] [blame] | 566 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 567 | then, original_invoke_block, /* replace_if_back_edge */ false); |
| 568 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 569 | otherwise, original_invoke_block, /* replace_if_back_edge */ false); |
| 570 | |
| 571 | // In case the original invoke location was a back edge, we need to update |
| 572 | // the loop to now have the merge block as a back edge. |
| 573 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 574 | merge, original_invoke_block, /* replace_if_back_edge */ true); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 575 | } |
| 576 | |
| 577 | bool HInliner::TryInlinePolymorphicCallToSameTarget(HInvoke* invoke_instruction, |
| 578 | ArtMethod* resolved_method, |
| 579 | const InlineCache& ic) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 580 | // This optimization only works under JIT for now. |
| 581 | DCHECK(Runtime::Current()->UseJit()); |
Roland Levillain | 2aba7cd | 2016-02-03 12:27:20 +0000 | [diff] [blame] | 582 | if (graph_->GetInstructionSet() == kMips64) { |
| 583 | // TODO: Support HClassTableGet for mips64. |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 584 | return false; |
| 585 | } |
| 586 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 587 | size_t pointer_size = class_linker->GetImagePointerSize(); |
| 588 | |
| 589 | DCHECK(resolved_method != nullptr); |
| 590 | ArtMethod* actual_method = nullptr; |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 591 | size_t method_index = invoke_instruction->IsInvokeVirtual() |
| 592 | ? invoke_instruction->AsInvokeVirtual()->GetVTableIndex() |
| 593 | : invoke_instruction->AsInvokeInterface()->GetImtIndex(); |
| 594 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 595 | // Check whether we are actually calling the same method among |
| 596 | // the different types seen. |
| 597 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
| 598 | if (ic.GetTypeAt(i) == nullptr) { |
| 599 | break; |
| 600 | } |
| 601 | ArtMethod* new_method = nullptr; |
| 602 | if (invoke_instruction->IsInvokeInterface()) { |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 603 | new_method = ic.GetTypeAt(i)->GetEmbeddedImTableEntry( |
| 604 | method_index % mirror::Class::kImtSize, pointer_size); |
| 605 | if (new_method->IsRuntimeMethod()) { |
| 606 | // Bail out as soon as we see a conflict trampoline in one of the target's |
| 607 | // interface table. |
| 608 | return false; |
| 609 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 610 | } else { |
| 611 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 612 | new_method = ic.GetTypeAt(i)->GetEmbeddedVTableEntry(method_index, pointer_size); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 613 | } |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 614 | DCHECK(new_method != nullptr); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 615 | if (actual_method == nullptr) { |
| 616 | actual_method = new_method; |
| 617 | } else if (actual_method != new_method) { |
| 618 | // Different methods, bailout. |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 619 | VLOG(compiler) << "Call to " << PrettyMethod(resolved_method) |
| 620 | << " from inline cache is not inlined because it resolves" |
| 621 | << " to different methods"; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 622 | return false; |
| 623 | } |
| 624 | } |
| 625 | |
| 626 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 627 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 628 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 629 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 630 | if (!TryInlineAndReplace(invoke_instruction, actual_method, /* do_rtp */ false)) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 631 | return false; |
| 632 | } |
| 633 | |
| 634 | // We successfully inlined, now add a guard. |
| 635 | HInstanceFieldGet* receiver_class = BuildGetReceiverClass( |
| 636 | class_linker, receiver, invoke_instruction->GetDexPc()); |
| 637 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 638 | Primitive::Type type = Is64BitInstructionSet(graph_->GetInstructionSet()) |
| 639 | ? Primitive::kPrimLong |
| 640 | : Primitive::kPrimInt; |
| 641 | HClassTableGet* class_table_get = new (graph_->GetArena()) HClassTableGet( |
| 642 | receiver_class, |
| 643 | type, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 644 | invoke_instruction->IsInvokeVirtual() ? HClassTableGet::TableKind::kVTable |
| 645 | : HClassTableGet::TableKind::kIMTable, |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 646 | method_index, |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 647 | invoke_instruction->GetDexPc()); |
| 648 | |
| 649 | HConstant* constant; |
| 650 | if (type == Primitive::kPrimLong) { |
| 651 | constant = graph_->GetLongConstant( |
| 652 | reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc()); |
| 653 | } else { |
| 654 | constant = graph_->GetIntConstant( |
| 655 | reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc()); |
| 656 | } |
| 657 | |
| 658 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(class_table_get, constant); |
| 659 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
| 660 | compare, invoke_instruction->GetDexPc()); |
| 661 | // TODO: Extend reference type propagation to understand the guard. |
| 662 | if (cursor != nullptr) { |
| 663 | bb_cursor->InsertInstructionAfter(receiver_class, cursor); |
| 664 | } else { |
| 665 | bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction()); |
| 666 | } |
| 667 | bb_cursor->InsertInstructionAfter(class_table_get, receiver_class); |
| 668 | bb_cursor->InsertInstructionAfter(compare, class_table_get); |
| 669 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 670 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 671 | |
| 672 | // Run type propagation to get the guard typed. |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 673 | ReferenceTypePropagation rtp_fixup(graph_, handles_, /* is_first_run */ false); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 674 | rtp_fixup.Run(); |
| 675 | |
| 676 | MaybeRecordStat(kInlinedPolymorphicCall); |
| 677 | |
| 678 | return true; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 679 | } |
| 680 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 681 | bool HInliner::TryInlineAndReplace(HInvoke* invoke_instruction, ArtMethod* method, bool do_rtp) { |
| 682 | HInstruction* return_replacement = nullptr; |
| 683 | if (!TryBuildAndInline(invoke_instruction, method, &return_replacement)) { |
| 684 | return false; |
| 685 | } |
| 686 | if (return_replacement != nullptr) { |
| 687 | invoke_instruction->ReplaceWith(return_replacement); |
| 688 | } |
| 689 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
| 690 | FixUpReturnReferenceType(invoke_instruction, method, return_replacement, do_rtp); |
| 691 | return true; |
| 692 | } |
| 693 | |
| 694 | bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction, |
| 695 | ArtMethod* method, |
| 696 | HInstruction** return_replacement) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 697 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 698 | |
| 699 | // Check whether we're allowed to inline. The outermost compilation unit is the relevant |
| 700 | // dex file here (though the transitivity of an inline chain would allow checking the calller). |
| 701 | if (!compiler_driver_->MayInline(method->GetDexFile(), |
| 702 | outer_compilation_unit_.GetDexFile())) { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 703 | if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 704 | VLOG(compiler) << "Successfully replaced pattern of invoke " << PrettyMethod(method); |
| 705 | MaybeRecordStat(kReplacedInvokeWithSimplePattern); |
| 706 | return true; |
| 707 | } |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 708 | VLOG(compiler) << "Won't inline " << PrettyMethod(method) << " in " |
| 709 | << outer_compilation_unit_.GetDexFile()->GetLocation() << " (" |
| 710 | << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from " |
| 711 | << method->GetDexFile()->GetLocation(); |
| 712 | return false; |
| 713 | } |
| 714 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 715 | uint32_t method_index = FindMethodIndexIn( |
| 716 | method, caller_dex_file, invoke_instruction->GetDexMethodIndex()); |
| 717 | if (method_index == DexFile::kDexNoIndex) { |
| 718 | VLOG(compiler) << "Call to " |
| 719 | << PrettyMethod(method) |
| 720 | << " cannot be inlined because unaccessible to caller"; |
| 721 | return false; |
| 722 | } |
| 723 | |
| 724 | bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile()); |
| 725 | |
| 726 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 727 | |
| 728 | if (code_item == nullptr) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 729 | VLOG(compiler) << "Method " << PrettyMethod(method) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 730 | << " is not inlined because it is native"; |
| 731 | return false; |
| 732 | } |
| 733 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 734 | size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits(); |
| 735 | if (code_item->insns_size_in_code_units_ > inline_max_code_units) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 736 | VLOG(compiler) << "Method " << PrettyMethod(method) |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 737 | << " is too big to inline: " |
| 738 | << code_item->insns_size_in_code_units_ |
| 739 | << " > " |
| 740 | << inline_max_code_units; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 741 | return false; |
| 742 | } |
| 743 | |
| 744 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 745 | VLOG(compiler) << "Method " << PrettyMethod(method) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 746 | << " is not inlined because of try block"; |
| 747 | return false; |
| 748 | } |
| 749 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 750 | if (!method->GetDeclaringClass()->IsVerified()) { |
| 751 | uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex(); |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 752 | if (Runtime::Current()->UseJit() || |
| 753 | !compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 754 | method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) { |
Nicolas Geoffray | ccc6197 | 2015-10-01 14:34:20 +0100 | [diff] [blame] | 755 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 756 | << " couldn't be verified, so it cannot be inlined"; |
| 757 | return false; |
| 758 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 759 | } |
| 760 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 761 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 762 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 763 | // Case of a static method that cannot be inlined because it implicitly |
| 764 | // requires an initialization check of its declaring class. |
| 765 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 766 | << " is not inlined because it is static and requires a clinit" |
| 767 | << " check that cannot be emitted due to Dex cache limitations"; |
| 768 | return false; |
| 769 | } |
| 770 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 771 | if (!TryBuildAndInlineHelper(invoke_instruction, method, same_dex_file, return_replacement)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 772 | return false; |
| 773 | } |
| 774 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 775 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 776 | MaybeRecordStat(kInlinedInvoke); |
| 777 | return true; |
| 778 | } |
| 779 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 780 | static HInstruction* GetInvokeInputForArgVRegIndex(HInvoke* invoke_instruction, |
| 781 | size_t arg_vreg_index) |
| 782 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 783 | size_t input_index = 0; |
| 784 | for (size_t i = 0; i < arg_vreg_index; ++i, ++input_index) { |
| 785 | DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments()); |
| 786 | if (Primitive::Is64BitType(invoke_instruction->InputAt(input_index)->GetType())) { |
| 787 | ++i; |
| 788 | DCHECK_NE(i, arg_vreg_index); |
| 789 | } |
| 790 | } |
| 791 | DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments()); |
| 792 | return invoke_instruction->InputAt(input_index); |
| 793 | } |
| 794 | |
| 795 | // Try to recognize known simple patterns and replace invoke call with appropriate instructions. |
| 796 | bool HInliner::TryPatternSubstitution(HInvoke* invoke_instruction, |
| 797 | ArtMethod* resolved_method, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 798 | HInstruction** return_replacement) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 799 | InlineMethod inline_method; |
| 800 | if (!InlineMethodAnalyser::AnalyseMethodCode(resolved_method, &inline_method)) { |
| 801 | return false; |
| 802 | } |
| 803 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 804 | switch (inline_method.opcode) { |
| 805 | case kInlineOpNop: |
| 806 | DCHECK_EQ(invoke_instruction->GetType(), Primitive::kPrimVoid); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 807 | *return_replacement = nullptr; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 808 | break; |
| 809 | case kInlineOpReturnArg: |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 810 | *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, |
| 811 | inline_method.d.return_data.arg); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 812 | break; |
| 813 | case kInlineOpNonWideConst: |
| 814 | if (resolved_method->GetShorty()[0] == 'L') { |
| 815 | DCHECK_EQ(inline_method.d.data, 0u); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 816 | *return_replacement = graph_->GetNullConstant(); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 817 | } else { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 818 | *return_replacement = graph_->GetIntConstant(static_cast<int32_t>(inline_method.d.data)); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 819 | } |
| 820 | break; |
| 821 | case kInlineOpIGet: { |
| 822 | const InlineIGetIPutData& data = inline_method.d.ifield_data; |
| 823 | if (data.method_is_static || data.object_arg != 0u) { |
| 824 | // TODO: Needs null check. |
| 825 | return false; |
| 826 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 827 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 828 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 829 | HInstanceFieldGet* iget = CreateInstanceFieldGet(dex_cache, data.field_idx, obj); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 830 | DCHECK_EQ(iget->GetFieldOffset().Uint32Value(), data.field_offset); |
| 831 | DCHECK_EQ(iget->IsVolatile() ? 1u : 0u, data.is_volatile); |
| 832 | invoke_instruction->GetBlock()->InsertInstructionBefore(iget, invoke_instruction); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 833 | *return_replacement = iget; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 834 | break; |
| 835 | } |
| 836 | case kInlineOpIPut: { |
| 837 | const InlineIGetIPutData& data = inline_method.d.ifield_data; |
| 838 | if (data.method_is_static || data.object_arg != 0u) { |
| 839 | // TODO: Needs null check. |
| 840 | return false; |
| 841 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 842 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 843 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg); |
| 844 | HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, data.src_arg); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 845 | HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, data.field_idx, obj, value); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 846 | DCHECK_EQ(iput->GetFieldOffset().Uint32Value(), data.field_offset); |
| 847 | DCHECK_EQ(iput->IsVolatile() ? 1u : 0u, data.is_volatile); |
| 848 | invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction); |
| 849 | if (data.return_arg_plus1 != 0u) { |
| 850 | size_t return_arg = data.return_arg_plus1 - 1u; |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 851 | *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, return_arg); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 852 | } |
| 853 | break; |
| 854 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 855 | case kInlineOpConstructor: { |
| 856 | const InlineConstructorData& data = inline_method.d.constructor_data; |
| 857 | // Get the indexes to arrays for easier processing. |
| 858 | uint16_t iput_field_indexes[] = { |
| 859 | data.iput0_field_index, data.iput1_field_index, data.iput2_field_index |
| 860 | }; |
| 861 | uint16_t iput_args[] = { data.iput0_arg, data.iput1_arg, data.iput2_arg }; |
| 862 | static_assert(arraysize(iput_args) == arraysize(iput_field_indexes), "Size mismatch"); |
| 863 | // Count valid field indexes. |
| 864 | size_t number_of_iputs = 0u; |
| 865 | while (number_of_iputs != arraysize(iput_field_indexes) && |
| 866 | iput_field_indexes[number_of_iputs] != DexFile::kDexNoIndex16) { |
| 867 | // Check that there are no duplicate valid field indexes. |
| 868 | DCHECK_EQ(0, std::count(iput_field_indexes + number_of_iputs + 1, |
| 869 | iput_field_indexes + arraysize(iput_field_indexes), |
| 870 | iput_field_indexes[number_of_iputs])); |
| 871 | ++number_of_iputs; |
| 872 | } |
| 873 | // Check that there are no valid field indexes in the rest of the array. |
| 874 | DCHECK_EQ(0, std::count_if(iput_field_indexes + number_of_iputs, |
| 875 | iput_field_indexes + arraysize(iput_field_indexes), |
| 876 | [](uint16_t index) { return index != DexFile::kDexNoIndex16; })); |
| 877 | |
| 878 | // Create HInstanceFieldSet for each IPUT that stores non-zero data. |
| 879 | Handle<mirror::DexCache> dex_cache; |
| 880 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, /* this */ 0u); |
| 881 | bool needs_constructor_barrier = false; |
| 882 | for (size_t i = 0; i != number_of_iputs; ++i) { |
| 883 | HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, iput_args[i]); |
| 884 | if (!value->IsConstant() || |
| 885 | (!value->AsConstant()->IsZero() && !value->IsNullConstant())) { |
| 886 | if (dex_cache.GetReference() == nullptr) { |
| 887 | dex_cache = handles_->NewHandle(resolved_method->GetDexCache()); |
| 888 | } |
| 889 | uint16_t field_index = iput_field_indexes[i]; |
| 890 | HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, field_index, obj, value); |
| 891 | invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction); |
| 892 | |
| 893 | // Check whether the field is final. If it is, we need to add a barrier. |
| 894 | size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet()); |
| 895 | ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size); |
| 896 | DCHECK(resolved_field != nullptr); |
| 897 | if (resolved_field->IsFinal()) { |
| 898 | needs_constructor_barrier = true; |
| 899 | } |
| 900 | } |
| 901 | } |
| 902 | if (needs_constructor_barrier) { |
| 903 | HMemoryBarrier* barrier = new (graph_->GetArena()) HMemoryBarrier(kStoreStore, kNoDexPc); |
| 904 | invoke_instruction->GetBlock()->InsertInstructionBefore(barrier, invoke_instruction); |
| 905 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 906 | *return_replacement = nullptr; |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 907 | break; |
| 908 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 909 | default: |
| 910 | LOG(FATAL) << "UNREACHABLE"; |
| 911 | UNREACHABLE(); |
| 912 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 913 | return true; |
| 914 | } |
| 915 | |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 916 | HInstanceFieldGet* HInliner::CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 917 | uint32_t field_index, |
| 918 | HInstruction* obj) |
| 919 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 920 | size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet()); |
| 921 | ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size); |
| 922 | DCHECK(resolved_field != nullptr); |
| 923 | HInstanceFieldGet* iget = new (graph_->GetArena()) HInstanceFieldGet( |
| 924 | obj, |
| 925 | resolved_field->GetTypeAsPrimitiveType(), |
| 926 | resolved_field->GetOffset(), |
| 927 | resolved_field->IsVolatile(), |
| 928 | field_index, |
| 929 | resolved_field->GetDeclaringClass()->GetDexClassDefIndex(), |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 930 | *dex_cache->GetDexFile(), |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 931 | dex_cache, |
Vladimir Marko | adda435 | 2016-01-29 10:24:41 +0000 | [diff] [blame] | 932 | // Read barrier generates a runtime call in slow path and we need a valid |
| 933 | // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537. |
| 934 | /* dex_pc */ 0); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 935 | if (iget->GetType() == Primitive::kPrimNot) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 936 | ReferenceTypePropagation rtp(graph_, handles_, /* is_first_run */ false); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 937 | rtp.Visit(iget); |
| 938 | } |
| 939 | return iget; |
| 940 | } |
| 941 | |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 942 | HInstanceFieldSet* HInliner::CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 943 | uint32_t field_index, |
| 944 | HInstruction* obj, |
| 945 | HInstruction* value) |
| 946 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 947 | size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet()); |
| 948 | ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size); |
| 949 | DCHECK(resolved_field != nullptr); |
| 950 | HInstanceFieldSet* iput = new (graph_->GetArena()) HInstanceFieldSet( |
| 951 | obj, |
| 952 | value, |
| 953 | resolved_field->GetTypeAsPrimitiveType(), |
| 954 | resolved_field->GetOffset(), |
| 955 | resolved_field->IsVolatile(), |
| 956 | field_index, |
| 957 | resolved_field->GetDeclaringClass()->GetDexClassDefIndex(), |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 958 | *dex_cache->GetDexFile(), |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 959 | dex_cache, |
Vladimir Marko | adda435 | 2016-01-29 10:24:41 +0000 | [diff] [blame] | 960 | // Read barrier generates a runtime call in slow path and we need a valid |
| 961 | // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537. |
| 962 | /* dex_pc */ 0); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 963 | return iput; |
| 964 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 965 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 966 | bool HInliner::TryBuildAndInlineHelper(HInvoke* invoke_instruction, |
| 967 | ArtMethod* resolved_method, |
| 968 | bool same_dex_file, |
| 969 | HInstruction** return_replacement) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 970 | ScopedObjectAccess soa(Thread::Current()); |
| 971 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 972 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 973 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 974 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 975 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 976 | DexCompilationUnit dex_compilation_unit( |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 977 | nullptr, |
| 978 | caller_compilation_unit_.GetClassLoader(), |
| 979 | class_linker, |
| 980 | callee_dex_file, |
| 981 | code_item, |
| 982 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
| 983 | method_index, |
| 984 | resolved_method->GetAccessFlags(), |
| 985 | /* verified_method */ nullptr, |
| 986 | dex_cache); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 987 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 988 | bool requires_ctor_barrier = false; |
| 989 | |
| 990 | if (dex_compilation_unit.IsConstructor()) { |
| 991 | // If it's a super invocation and we already generate a barrier there's no need |
| 992 | // to generate another one. |
| 993 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 994 | // same as the local "this" pointer then we must have a super invocation. |
| 995 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 996 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 997 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 998 | requires_ctor_barrier = false; |
| 999 | } else { |
| 1000 | Thread* self = Thread::Current(); |
| 1001 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 1002 | dex_compilation_unit.GetDexFile(), |
| 1003 | dex_compilation_unit.GetClassDefIndex()); |
| 1004 | } |
| 1005 | } |
| 1006 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 1007 | InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType(); |
| 1008 | if (invoke_type == kInterface) { |
| 1009 | // We have statically resolved the dispatch. To please the class linker |
| 1010 | // at runtime, we change this call as if it was a virtual call. |
| 1011 | invoke_type = kVirtual; |
| 1012 | } |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1013 | |
| 1014 | const int32_t caller_instruction_counter = graph_->GetCurrentInstructionId(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 1015 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1016 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1017 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1018 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 1019 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1020 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 1021 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1022 | graph_->IsDebuggable(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1023 | /* osr */ false, |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1024 | caller_instruction_counter); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1025 | callee_graph->SetArtMethod(resolved_method); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 1026 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1027 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 1028 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1029 | &dex_compilation_unit, |
| 1030 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1031 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1032 | compiler_driver_, |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 1033 | &inline_stats, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 1034 | resolved_method->GetQuickenedInfo(), |
| 1035 | dex_cache); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1036 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1037 | if (builder.BuildGraph(*code_item, handles_) != kAnalysisSuccess) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1038 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1039 | << " could not be built, so cannot be inlined"; |
| 1040 | return false; |
| 1041 | } |
| 1042 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 1043 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 1044 | compiler_driver_->GetInstructionSet())) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1045 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 1046 | << " cannot be inlined because of the register allocator"; |
| 1047 | return false; |
| 1048 | } |
| 1049 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1050 | size_t parameter_index = 0; |
| 1051 | for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions()); |
| 1052 | !instructions.Done(); |
| 1053 | instructions.Advance()) { |
| 1054 | HInstruction* current = instructions.Current(); |
| 1055 | if (current->IsParameterValue()) { |
| 1056 | HInstruction* argument = invoke_instruction->InputAt(parameter_index++); |
| 1057 | if (argument->IsNullConstant()) { |
| 1058 | current->ReplaceWith(callee_graph->GetNullConstant()); |
| 1059 | } else if (argument->IsIntConstant()) { |
| 1060 | current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue())); |
| 1061 | } else if (argument->IsLongConstant()) { |
| 1062 | current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue())); |
| 1063 | } else if (argument->IsFloatConstant()) { |
| 1064 | current->ReplaceWith( |
| 1065 | callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue())); |
| 1066 | } else if (argument->IsDoubleConstant()) { |
| 1067 | current->ReplaceWith( |
| 1068 | callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue())); |
| 1069 | } else if (argument->GetType() == Primitive::kPrimNot) { |
| 1070 | current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo()); |
| 1071 | current->AsParameterValue()->SetCanBeNull(argument->CanBeNull()); |
| 1072 | } |
| 1073 | } |
| 1074 | } |
| 1075 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1076 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 1077 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 1078 | HConstantFolding fold(callee_graph); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1079 | HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 1080 | InstructionSimplifier simplify(callee_graph, stats_); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 1081 | IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_, stats_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1082 | |
| 1083 | HOptimization* optimizations[] = { |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 1084 | &intrinsics, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1085 | &sharpening, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1086 | &simplify, |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1087 | &fold, |
Vladimir Marko | 9e23df5 | 2015-11-10 17:14:35 +0000 | [diff] [blame] | 1088 | &dce, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1089 | }; |
| 1090 | |
| 1091 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 1092 | HOptimization* optimization = optimizations[i]; |
| 1093 | optimization->Run(); |
| 1094 | } |
| 1095 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1096 | size_t number_of_instructions_budget = kMaximumNumberOfHInstructions; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 1097 | if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1098 | HInliner inliner(callee_graph, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1099 | outermost_graph_, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1100 | codegen_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1101 | outer_compilation_unit_, |
| 1102 | dex_compilation_unit, |
| 1103 | compiler_driver_, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 1104 | handles_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1105 | stats_, |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1106 | total_number_of_dex_registers_ + code_item->registers_size_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1107 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1108 | inliner.Run(); |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1109 | number_of_instructions_budget += inliner.number_of_inlined_instructions_; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1110 | } |
| 1111 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1112 | // TODO: We should abort only if all predecessors throw. However, |
| 1113 | // HGraph::InlineInto currently does not handle an exit block with |
| 1114 | // a throw predecessor. |
| 1115 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 1116 | if (exit_block == nullptr) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1117 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1118 | << " could not be inlined because it has an infinite loop"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1119 | return false; |
| 1120 | } |
| 1121 | |
| 1122 | bool has_throw_predecessor = false; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1123 | for (HBasicBlock* predecessor : exit_block->GetPredecessors()) { |
| 1124 | if (predecessor->GetLastInstruction()->IsThrow()) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1125 | has_throw_predecessor = true; |
| 1126 | break; |
| 1127 | } |
| 1128 | } |
| 1129 | if (has_throw_predecessor) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1130 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1131 | << " could not be inlined because one branch always throws"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1132 | return false; |
| 1133 | } |
| 1134 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1135 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1136 | 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] | 1137 | size_t number_of_instructions = 0; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1138 | |
| 1139 | bool can_inline_environment = |
| 1140 | total_number_of_dex_registers_ < kMaximumNumberOfCumulatedDexRegisters; |
| 1141 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1142 | for (; !it.Done(); it.Advance()) { |
| 1143 | HBasicBlock* block = it.Current(); |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 1144 | |
| 1145 | if (block->IsLoopHeader() && block->GetLoopInformation()->IsIrreducible()) { |
| 1146 | // Don't inline methods with irreducible loops, they could prevent some |
| 1147 | // optimizations to run. |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1148 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 1149 | << " could not be inlined because it contains an irreducible loop"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1150 | return false; |
| 1151 | } |
| 1152 | |
| 1153 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 1154 | !instr_it.Done(); |
| 1155 | instr_it.Advance()) { |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1156 | if (number_of_instructions++ == number_of_instructions_budget) { |
| 1157 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1158 | << " is not inlined because its caller has reached" |
| 1159 | << " its instruction budget limit."; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1160 | return false; |
| 1161 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1162 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1163 | if (!can_inline_environment && current->NeedsEnvironment()) { |
| 1164 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
| 1165 | << " is not inlined because its caller has reached" |
| 1166 | << " its environment budget limit."; |
| 1167 | return false; |
| 1168 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1169 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1170 | if (current->IsInvokeInterface()) { |
| 1171 | // Disable inlining of interface calls. The cost in case of entering the |
| 1172 | // resolution conflict is currently too high. |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1173 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1174 | << " could not be inlined because it has an interface call."; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1175 | return false; |
| 1176 | } |
| 1177 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1178 | if (!same_dex_file && current->NeedsEnvironment()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1179 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1180 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1181 | << " needs an environment and is in a different dex file"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1182 | return false; |
| 1183 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1184 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1185 | if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1186 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1187 | << " could not be inlined because " << current->DebugName() |
| 1188 | << " it is in a different dex file and requires access to the dex cache"; |
| 1189 | return false; |
| 1190 | } |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1191 | |
| 1192 | if (current->IsNewInstance() && |
| 1193 | (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1194 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
| 1195 | << " could not be inlined because it is using an entrypoint" |
| 1196 | << " with access checks"; |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1197 | // Allocation entrypoint does not handle inlined frames. |
| 1198 | return false; |
| 1199 | } |
| 1200 | |
| 1201 | if (current->IsNewArray() && |
| 1202 | (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1203 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
| 1204 | << " could not be inlined because it is using an entrypoint" |
| 1205 | << " with access checks"; |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1206 | // Allocation entrypoint does not handle inlined frames. |
| 1207 | return false; |
| 1208 | } |
| 1209 | |
| 1210 | if (current->IsUnresolvedStaticFieldGet() || |
| 1211 | current->IsUnresolvedInstanceFieldGet() || |
| 1212 | current->IsUnresolvedStaticFieldSet() || |
| 1213 | current->IsUnresolvedInstanceFieldSet()) { |
| 1214 | // Entrypoint for unresolved fields does not handle inlined frames. |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1215 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
| 1216 | << " could not be inlined because it is using an unresolved" |
| 1217 | << " entrypoint"; |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1218 | return false; |
| 1219 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1220 | } |
| 1221 | } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1222 | number_of_inlined_instructions_ += number_of_instructions; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1223 | |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1224 | DCHECK_EQ(caller_instruction_counter, graph_->GetCurrentInstructionId()) |
| 1225 | << "No instructions can be added to the outer graph while inner graph is being built"; |
| 1226 | |
| 1227 | const int32_t callee_instruction_counter = callee_graph->GetCurrentInstructionId(); |
| 1228 | graph_->SetCurrentInstructionId(callee_instruction_counter); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1229 | *return_replacement = callee_graph->InlineInto(graph_, invoke_instruction); |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1230 | |
| 1231 | DCHECK_EQ(callee_instruction_counter, callee_graph->GetCurrentInstructionId()) |
| 1232 | << "No instructions can be added to the inner graph during inlining into the outer graph"; |
| 1233 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1234 | return true; |
| 1235 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1236 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1237 | void HInliner::FixUpReturnReferenceType(HInvoke* invoke_instruction, |
| 1238 | ArtMethod* resolved_method, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1239 | HInstruction* return_replacement, |
| 1240 | bool do_rtp) { |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 1241 | // Check the integrity of reference types and run another type propagation if needed. |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 1242 | if (return_replacement != nullptr) { |
| 1243 | if (return_replacement->GetType() == Primitive::kPrimNot) { |
| 1244 | if (!return_replacement->GetReferenceTypeInfo().IsValid()) { |
| 1245 | // Make sure that we have a valid type for the return. We may get an invalid one when |
| 1246 | // we inline invokes with multiple branches and create a Phi for the result. |
| 1247 | // TODO: we could be more precise by merging the phi inputs but that requires |
| 1248 | // some functionality from the reference type propagation. |
| 1249 | DCHECK(return_replacement->IsPhi()); |
| 1250 | size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| 1251 | ReferenceTypeInfo::TypeHandle return_handle = |
| 1252 | handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size)); |
| 1253 | return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create( |
| 1254 | return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */)); |
| 1255 | } |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 1256 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 1257 | if (do_rtp) { |
| 1258 | // If the return type is a refinement of the declared type run the type propagation again. |
| 1259 | ReferenceTypeInfo return_rti = return_replacement->GetReferenceTypeInfo(); |
| 1260 | ReferenceTypeInfo invoke_rti = invoke_instruction->GetReferenceTypeInfo(); |
| 1261 | if (invoke_rti.IsStrictSupertypeOf(return_rti) |
| 1262 | || (return_rti.IsExact() && !invoke_rti.IsExact()) |
| 1263 | || !return_replacement->CanBeNull()) { |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1264 | ReferenceTypePropagation(graph_, handles_, /* is_first_run */ false).Run(); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 1265 | } |
| 1266 | } |
| 1267 | } else if (return_replacement->IsInstanceOf()) { |
| 1268 | if (do_rtp) { |
| 1269 | // Inlining InstanceOf into an If may put a tighter bound on reference types. |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1270 | ReferenceTypePropagation(graph_, handles_, /* is_first_run */ false).Run(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1271 | } |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 1272 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1273 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1274 | } |
| 1275 | |
| 1276 | } // namespace art |