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