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" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 21 | #include "builder.h" |
| 22 | #include "class_linker.h" |
| 23 | #include "constant_folding.h" |
| 24 | #include "dead_code_elimination.h" |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 25 | #include "dex/verified_method.h" |
| 26 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 27 | #include "driver/compiler_driver-inl.h" |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 28 | #include "driver/compiler_options.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 29 | #include "driver/dex_compilation_unit.h" |
| 30 | #include "instruction_simplifier.h" |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 31 | #include "intrinsics.h" |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 32 | #include "jit/jit.h" |
| 33 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 34 | #include "mirror/class_loader.h" |
| 35 | #include "mirror/dex_cache.h" |
| 36 | #include "nodes.h" |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 37 | #include "optimizing_compiler.h" |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 38 | #include "reference_type_propagation.h" |
Matthew Gharrity | e928885 | 2016-07-14 14:08:16 -0700 | [diff] [blame] | 39 | #include "register_allocator_linear_scan.h" |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 40 | #include "quick/inline_method_analyser.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 41 | #include "sharpening.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 42 | #include "ssa_builder.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 43 | #include "ssa_phi_elimination.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 44 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 45 | #include "thread.h" |
| 46 | |
| 47 | namespace art { |
| 48 | |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 49 | static constexpr size_t kMaximumNumberOfHInstructions = 32; |
| 50 | |
| 51 | // Limit the number of dex registers that we accumulate while inlining |
| 52 | // to avoid creating large amount of nested environments. |
| 53 | static constexpr size_t kMaximumNumberOfCumulatedDexRegisters = 64; |
| 54 | |
| 55 | // Avoid inlining within a huge method due to memory pressure. |
| 56 | static constexpr size_t kMaximumCodeUnitSize = 4096; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 57 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 58 | void HInliner::Run() { |
Calin Juravle | 8f96df8 | 2015-07-29 15:58:48 +0100 | [diff] [blame] | 59 | const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions(); |
| 60 | if ((compiler_options.GetInlineDepthLimit() == 0) |
| 61 | || (compiler_options.GetInlineMaxCodeUnits() == 0)) { |
| 62 | return; |
| 63 | } |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 64 | if (caller_compilation_unit_.GetCodeItem()->insns_size_in_code_units_ > kMaximumCodeUnitSize) { |
| 65 | return; |
| 66 | } |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 67 | if (graph_->IsDebuggable()) { |
| 68 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 69 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 70 | return; |
| 71 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 72 | const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
| 73 | DCHECK(!blocks.empty()); |
| 74 | HBasicBlock* next_block = blocks[0]; |
| 75 | for (size_t i = 0; i < blocks.size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 76 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 77 | // This avoids doing the inlining work again on the inlined blocks. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 78 | if (blocks[i] != next_block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 79 | continue; |
| 80 | } |
| 81 | HBasicBlock* block = next_block; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 82 | next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1]; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 83 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 84 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 85 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 86 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 87 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | b703d18 | 2017-02-14 18:05:28 +0000 | [diff] [blame^] | 88 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
| 89 | // Debugging case: directives in method names control or assert on inlining. |
| 90 | std::string callee_name = outer_compilation_unit_.GetDexFile()->PrettyMethod( |
| 91 | call->GetDexMethodIndex(), /* with_signature */ false); |
| 92 | // Tests prevent inlining by having $noinline$ in their method names. |
| 93 | if (callee_name.find("$noinline$") == std::string::npos) { |
| 94 | if (!TryInline(call)) { |
| 95 | bool should_have_inlined = (callee_name.find("$inline$") != std::string::npos); |
| 96 | CHECK(!should_have_inlined) << "Could not inline " << callee_name; |
| 97 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 98 | } |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 99 | } else { |
Nicolas Geoffray | b703d18 | 2017-02-14 18:05:28 +0000 | [diff] [blame^] | 100 | // Normal case: try to inline. |
| 101 | TryInline(call); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 102 | } |
| 103 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 104 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 105 | } |
| 106 | } |
| 107 | } |
| 108 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 109 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 110 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 111 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 112 | } |
| 113 | |
| 114 | /** |
| 115 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 116 | * the actual runtime target of an interface or virtual call. |
| 117 | * Return nullptr if the runtime target cannot be proven. |
| 118 | */ |
| 119 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 120 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 121 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 122 | // No need to lookup further, the resolved method will be the target. |
| 123 | return resolved_method; |
| 124 | } |
| 125 | |
| 126 | HInstruction* receiver = invoke->InputAt(0); |
| 127 | if (receiver->IsNullCheck()) { |
| 128 | // Due to multiple levels of inlining within the same pass, it might be that |
| 129 | // null check does not have the reference type of the actual receiver. |
| 130 | receiver = receiver->InputAt(0); |
| 131 | } |
| 132 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 133 | DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName(); |
| 134 | if (!info.IsExact()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 135 | // We currently only support inlining with known receivers. |
| 136 | // TODO: Remove this check, we should be able to inline final methods |
| 137 | // on unknown receivers. |
| 138 | return nullptr; |
| 139 | } else if (info.GetTypeHandle()->IsInterface()) { |
| 140 | // Statically knowing that the receiver has an interface type cannot |
| 141 | // help us find what is the target method. |
| 142 | return nullptr; |
| 143 | } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) { |
| 144 | // The method that we're trying to call is not in the receiver's class or super classes. |
| 145 | return nullptr; |
Nicolas Geoffray | ab5327d | 2016-03-18 11:36:20 +0000 | [diff] [blame] | 146 | } else if (info.GetTypeHandle()->IsErroneous()) { |
| 147 | // If the type is erroneous, do not go further, as we are going to query the vtable or |
| 148 | // imt table, that we can only safely do on non-erroneous classes. |
| 149 | return nullptr; |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 150 | } |
| 151 | |
| 152 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 153 | PointerSize pointer_size = cl->GetImagePointerSize(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 154 | if (invoke->IsInvokeInterface()) { |
| 155 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface( |
| 156 | resolved_method, pointer_size); |
| 157 | } else { |
| 158 | DCHECK(invoke->IsInvokeVirtual()); |
| 159 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual( |
| 160 | resolved_method, pointer_size); |
| 161 | } |
| 162 | |
| 163 | if (resolved_method == nullptr) { |
| 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; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 168 | } else if (!resolved_method->IsInvokable()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 169 | // The information we had on the receiver was not enough to find |
| 170 | // the target method. Since we check above the exact type of the receiver, |
| 171 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 172 | return nullptr; |
| 173 | } else if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 174 | // A final method has to be the target method. |
| 175 | return resolved_method; |
| 176 | } else if (info.IsExact()) { |
| 177 | // If we found a method and the receiver's concrete type is statically |
| 178 | // known, we know for sure the target. |
| 179 | return resolved_method; |
| 180 | } else { |
| 181 | // Even if we did find a method, the receiver type was not enough to |
| 182 | // statically find the runtime target. |
| 183 | return nullptr; |
| 184 | } |
| 185 | } |
| 186 | |
| 187 | static uint32_t FindMethodIndexIn(ArtMethod* method, |
| 188 | const DexFile& dex_file, |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 189 | uint32_t name_and_signature_index) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 190 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 191 | if (IsSameDexFile(*method->GetDexFile(), dex_file)) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 192 | return method->GetDexMethodIndex(); |
| 193 | } else { |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 194 | return method->FindDexMethodIndexInOtherDexFile(dex_file, name_and_signature_index); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 198 | static dex::TypeIndex FindClassIndexIn(mirror::Class* cls, |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 199 | const DexFile& dex_file, |
| 200 | Handle<mirror::DexCache> dex_cache) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 201 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 202 | dex::TypeIndex index; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 203 | if (cls->GetDexCache() == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 204 | DCHECK(cls->IsArrayClass()) << cls->PrettyClass(); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 205 | index = cls->FindTypeIndexInOtherDexFile(dex_file); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 206 | } else if (!cls->GetDexTypeIndex().IsValid()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 207 | DCHECK(cls->IsProxyClass()) << cls->PrettyClass(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 208 | // TODO: deal with proxy classes. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 209 | } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) { |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 210 | DCHECK_EQ(cls->GetDexCache(), dex_cache.Get()); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 211 | index = cls->GetDexTypeIndex(); |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 212 | // Update the dex cache to ensure the class is in. The generated code will |
| 213 | // consider it is. We make it safe by updating the dex cache, as other |
| 214 | // dex files might also load the class, and there is no guarantee the dex |
| 215 | // cache of the dex file of the class will be updated. |
| 216 | if (dex_cache->GetResolvedType(index) == nullptr) { |
| 217 | dex_cache->SetResolvedType(index, cls); |
| 218 | } |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 219 | } else { |
| 220 | index = cls->FindTypeIndexInOtherDexFile(dex_file); |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 221 | // We cannot guarantee the entry in the dex cache will resolve to the same class, |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 222 | // as there may be different class loaders. So only return the index if it's |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 223 | // the right class in the dex cache already. |
| 224 | if (index.IsValid() && dex_cache->GetResolvedType(index) != cls) { |
| 225 | index = dex::TypeIndex::Invalid(); |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 226 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 227 | } |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 228 | |
| 229 | return index; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 230 | } |
| 231 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 232 | class ScopedProfilingInfoInlineUse { |
| 233 | public: |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 234 | explicit ScopedProfilingInfoInlineUse(ArtMethod* method, Thread* self) |
| 235 | : method_(method), |
| 236 | self_(self), |
| 237 | // Fetch the profiling info ahead of using it. If it's null when fetching, |
| 238 | // we should not call JitCodeCache::DoneInlining. |
| 239 | profiling_info_( |
| 240 | Runtime::Current()->GetJit()->GetCodeCache()->NotifyCompilerUse(method, self)) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 241 | } |
| 242 | |
| 243 | ~ScopedProfilingInfoInlineUse() { |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 244 | if (profiling_info_ != nullptr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 245 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 246 | DCHECK_EQ(profiling_info_, method_->GetProfilingInfo(pointer_size)); |
| 247 | Runtime::Current()->GetJit()->GetCodeCache()->DoneCompilerUse(method_, self_); |
| 248 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 249 | } |
| 250 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 251 | ProfilingInfo* GetProfilingInfo() const { return profiling_info_; } |
| 252 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 253 | private: |
| 254 | ArtMethod* const method_; |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 255 | Thread* const self_; |
| 256 | ProfilingInfo* const profiling_info_; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 257 | }; |
| 258 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 259 | static bool IsMonomorphic(Handle<mirror::ObjectArray<mirror::Class>> classes) |
| 260 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 261 | DCHECK_GE(InlineCache::kIndividualCacheSize, 2); |
| 262 | return classes->Get(0) != nullptr && classes->Get(1) == nullptr; |
| 263 | } |
| 264 | |
| 265 | static bool IsMegamorphic(Handle<mirror::ObjectArray<mirror::Class>> classes) |
| 266 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 267 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
| 268 | if (classes->Get(i) == nullptr) { |
| 269 | return false; |
| 270 | } |
| 271 | } |
| 272 | return true; |
| 273 | } |
| 274 | |
| 275 | static mirror::Class* GetMonomorphicType(Handle<mirror::ObjectArray<mirror::Class>> classes) |
| 276 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 277 | DCHECK(classes->Get(0) != nullptr); |
| 278 | return classes->Get(0); |
| 279 | } |
| 280 | |
| 281 | static bool IsUninitialized(Handle<mirror::ObjectArray<mirror::Class>> classes) |
| 282 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 283 | return classes->Get(0) == nullptr; |
| 284 | } |
| 285 | |
| 286 | static bool IsPolymorphic(Handle<mirror::ObjectArray<mirror::Class>> classes) |
| 287 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 288 | DCHECK_GE(InlineCache::kIndividualCacheSize, 3); |
| 289 | return classes->Get(1) != nullptr && |
| 290 | classes->Get(InlineCache::kIndividualCacheSize - 1) == nullptr; |
| 291 | } |
| 292 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 293 | ArtMethod* HInliner::TryCHADevirtualization(ArtMethod* resolved_method) { |
| 294 | if (!resolved_method->HasSingleImplementation()) { |
| 295 | return nullptr; |
| 296 | } |
| 297 | if (Runtime::Current()->IsAotCompiler()) { |
| 298 | // No CHA-based devirtulization for AOT compiler (yet). |
| 299 | return nullptr; |
| 300 | } |
| 301 | if (outermost_graph_->IsCompilingOsr()) { |
| 302 | // We do not support HDeoptimize in OSR methods. |
| 303 | return nullptr; |
| 304 | } |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 305 | PointerSize pointer_size = caller_compilation_unit_.GetClassLinker()->GetImagePointerSize(); |
| 306 | return resolved_method->GetSingleImplementation(pointer_size); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 307 | } |
| 308 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 309 | bool HInliner::TryInline(HInvoke* invoke_instruction) { |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 310 | if (invoke_instruction->IsInvokeUnresolved() || |
| 311 | invoke_instruction->IsInvokePolymorphic()) { |
| 312 | return false; // Don't bother to move further if we know the method is unresolved or an |
| 313 | // invoke-polymorphic. |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 314 | } |
| 315 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 316 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 317 | uint32_t method_index = invoke_instruction->GetDexMethodIndex(); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 318 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 319 | VLOG(compiler) << "Try inlining " << caller_dex_file.PrettyMethod(method_index); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 320 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 321 | // We can query the dex cache directly. The verifier has populated it already. |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 322 | ArtMethod* resolved_method = invoke_instruction->GetResolvedMethod(); |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 323 | ArtMethod* actual_method = nullptr; |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 324 | if (resolved_method == nullptr) { |
| 325 | DCHECK(invoke_instruction->IsInvokeStaticOrDirect()); |
| 326 | DCHECK(invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()); |
| 327 | VLOG(compiler) << "Not inlining a String.<init> method"; |
| 328 | return false; |
| 329 | } else if (invoke_instruction->IsInvokeStaticOrDirect()) { |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 330 | actual_method = resolved_method; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 331 | } else { |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 332 | // Check if we can statically find the method. |
| 333 | actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 334 | } |
| 335 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 336 | bool cha_devirtualize = false; |
| 337 | if (actual_method == nullptr) { |
| 338 | ArtMethod* method = TryCHADevirtualization(resolved_method); |
| 339 | if (method != nullptr) { |
| 340 | cha_devirtualize = true; |
| 341 | actual_method = method; |
| 342 | } |
| 343 | } |
| 344 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 345 | if (actual_method != nullptr) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 346 | bool result = TryInlineAndReplace(invoke_instruction, |
| 347 | actual_method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 348 | ReferenceTypeInfo::CreateInvalid(), |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 349 | /* do_rtp */ true, |
| 350 | cha_devirtualize); |
Calin Juravle | 6915898 | 2016-03-16 11:53:41 +0000 | [diff] [blame] | 351 | if (result && !invoke_instruction->IsInvokeStaticOrDirect()) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 352 | if (cha_devirtualize) { |
| 353 | // Add dependency due to devirtulization. We've assumed resolved_method |
| 354 | // has single implementation. |
| 355 | outermost_graph_->AddCHASingleImplementationDependency(resolved_method); |
| 356 | MaybeRecordStat(kCHAInline); |
| 357 | } else { |
| 358 | MaybeRecordStat(kInlinedInvokeVirtualOrInterface); |
| 359 | } |
Calin Juravle | 6915898 | 2016-03-16 11:53:41 +0000 | [diff] [blame] | 360 | } |
| 361 | return result; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 362 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 363 | |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 364 | DCHECK(!invoke_instruction->IsInvokeStaticOrDirect()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 365 | |
| 366 | // Check if we can use an inline cache. |
| 367 | ArtMethod* caller = graph_->GetArtMethod(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 368 | if (Runtime::Current()->UseJitCompilation()) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 369 | // Under JIT, we should always know the caller. |
| 370 | DCHECK(caller != nullptr); |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 371 | ScopedProfilingInfoInlineUse spiis(caller, soa.Self()); |
| 372 | ProfilingInfo* profiling_info = spiis.GetProfilingInfo(); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 373 | if (profiling_info != nullptr) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 374 | StackHandleScope<1> hs(soa.Self()); |
| 375 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 376 | Handle<mirror::ObjectArray<mirror::Class>> inline_cache = hs.NewHandle( |
| 377 | mirror::ObjectArray<mirror::Class>::Alloc( |
| 378 | soa.Self(), |
| 379 | class_linker->GetClassRoot(ClassLinker::kClassArrayClass), |
| 380 | InlineCache::kIndividualCacheSize)); |
| 381 | if (inline_cache.Get() == nullptr) { |
| 382 | // We got an OOME. Just clear the exception, and don't inline. |
| 383 | DCHECK(soa.Self()->IsExceptionPending()); |
| 384 | soa.Self()->ClearException(); |
| 385 | VLOG(compiler) << "Out of memory in the compiler when trying to inline"; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 386 | return false; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 387 | } else { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 388 | Runtime::Current()->GetJit()->GetCodeCache()->CopyInlineCacheInto( |
| 389 | *profiling_info->GetInlineCache(invoke_instruction->GetDexPc()), |
| 390 | inline_cache); |
| 391 | if (IsUninitialized(inline_cache)) { |
| 392 | VLOG(compiler) << "Interface or virtual call to " |
| 393 | << caller_dex_file.PrettyMethod(method_index) |
| 394 | << " is not hit and not inlined"; |
| 395 | return false; |
| 396 | } else if (IsMonomorphic(inline_cache)) { |
| 397 | MaybeRecordStat(kMonomorphicCall); |
| 398 | if (outermost_graph_->IsCompilingOsr()) { |
| 399 | // If we are compiling OSR, we pretend this call is polymorphic, as we may come from the |
| 400 | // interpreter and it may have seen different receiver types. |
| 401 | return TryInlinePolymorphicCall(invoke_instruction, resolved_method, inline_cache); |
| 402 | } else { |
| 403 | return TryInlineMonomorphicCall(invoke_instruction, resolved_method, inline_cache); |
| 404 | } |
| 405 | } else if (IsPolymorphic(inline_cache)) { |
| 406 | MaybeRecordStat(kPolymorphicCall); |
| 407 | return TryInlinePolymorphicCall(invoke_instruction, resolved_method, inline_cache); |
| 408 | } else { |
| 409 | DCHECK(IsMegamorphic(inline_cache)); |
| 410 | VLOG(compiler) << "Interface or virtual call to " |
| 411 | << caller_dex_file.PrettyMethod(method_index) |
| 412 | << " is megamorphic and not inlined"; |
| 413 | MaybeRecordStat(kMegamorphicCall); |
| 414 | return false; |
| 415 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 416 | } |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 420 | VLOG(compiler) << "Interface or virtual call to " |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 421 | << caller_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 422 | << " could not be statically determined"; |
| 423 | return false; |
| 424 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 425 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 426 | HInstanceFieldGet* HInliner::BuildGetReceiverClass(ClassLinker* class_linker, |
| 427 | HInstruction* receiver, |
| 428 | uint32_t dex_pc) const { |
| 429 | ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0); |
| 430 | DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_"); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 431 | HInstanceFieldGet* result = new (graph_->GetArena()) HInstanceFieldGet( |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 432 | receiver, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 433 | field, |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 434 | Primitive::kPrimNot, |
| 435 | field->GetOffset(), |
| 436 | field->IsVolatile(), |
| 437 | field->GetDexFieldIndex(), |
| 438 | field->GetDeclaringClass()->GetDexClassDefIndex(), |
| 439 | *field->GetDexFile(), |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 440 | dex_pc); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 441 | // The class of a field is effectively final, and does not have any memory dependencies. |
| 442 | result->SetSideEffects(SideEffects::None()); |
| 443 | return result; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 444 | } |
| 445 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 446 | bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction, |
| 447 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 448 | Handle<mirror::ObjectArray<mirror::Class>> classes) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 449 | DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface()) |
| 450 | << invoke_instruction->DebugName(); |
| 451 | |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 452 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 453 | dex::TypeIndex class_index = FindClassIndexIn( |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 454 | GetMonomorphicType(classes), caller_dex_file, caller_compilation_unit_.GetDexCache()); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 455 | if (!class_index.IsValid()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 456 | VLOG(compiler) << "Call to " << ArtMethod::PrettyMethod(resolved_method) |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 457 | << " from inline cache is not inlined because its class is not" |
| 458 | << " accessible to the caller"; |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 463 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 464 | if (invoke_instruction->IsInvokeInterface()) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 465 | resolved_method = GetMonomorphicType(classes)->FindVirtualMethodForInterface( |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 466 | resolved_method, pointer_size); |
| 467 | } else { |
| 468 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 469 | resolved_method = GetMonomorphicType(classes)->FindVirtualMethodForVirtual( |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 470 | resolved_method, pointer_size); |
| 471 | } |
| 472 | DCHECK(resolved_method != nullptr); |
| 473 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 474 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 475 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 476 | Handle<mirror::Class> monomorphic_type = handles_->NewHandle(GetMonomorphicType(classes)); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 477 | if (!TryInlineAndReplace(invoke_instruction, |
| 478 | resolved_method, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 479 | ReferenceTypeInfo::Create(monomorphic_type, /* is_exact */ true), |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 480 | /* do_rtp */ false, |
| 481 | /* cha_devirtualize */ false)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 482 | return false; |
| 483 | } |
| 484 | |
| 485 | // We successfully inlined, now add a guard. |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 486 | AddTypeGuard(receiver, |
| 487 | cursor, |
| 488 | bb_cursor, |
| 489 | class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 490 | monomorphic_type, |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 491 | invoke_instruction, |
| 492 | /* with_deoptimization */ true); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 493 | |
| 494 | // Run type propagation to get the guard typed, and eventually propagate the |
| 495 | // type of the receiver. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 496 | ReferenceTypePropagation rtp_fixup(graph_, |
| 497 | outer_compilation_unit_.GetDexCache(), |
| 498 | handles_, |
| 499 | /* is_first_run */ false); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 500 | rtp_fixup.Run(); |
| 501 | |
| 502 | MaybeRecordStat(kInlinedMonomorphicCall); |
| 503 | return true; |
| 504 | } |
| 505 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 506 | void HInliner::AddCHAGuard(HInstruction* invoke_instruction, |
| 507 | uint32_t dex_pc, |
| 508 | HInstruction* cursor, |
| 509 | HBasicBlock* bb_cursor) { |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 510 | HShouldDeoptimizeFlag* deopt_flag = new (graph_->GetArena()) |
| 511 | HShouldDeoptimizeFlag(graph_->GetArena(), dex_pc); |
| 512 | HInstruction* compare = new (graph_->GetArena()) HNotEqual( |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 513 | deopt_flag, graph_->GetIntConstant(0, dex_pc)); |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 514 | HInstruction* deopt = new (graph_->GetArena()) HDeoptimize(compare, dex_pc); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 515 | |
| 516 | if (cursor != nullptr) { |
| 517 | bb_cursor->InsertInstructionAfter(deopt_flag, cursor); |
| 518 | } else { |
| 519 | bb_cursor->InsertInstructionBefore(deopt_flag, bb_cursor->GetFirstInstruction()); |
| 520 | } |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 521 | bb_cursor->InsertInstructionAfter(compare, deopt_flag); |
| 522 | bb_cursor->InsertInstructionAfter(deopt, compare); |
| 523 | |
| 524 | // Add receiver as input to aid CHA guard optimization later. |
| 525 | deopt_flag->AddInput(invoke_instruction->InputAt(0)); |
| 526 | DCHECK_EQ(deopt_flag->InputCount(), 1u); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 527 | deopt->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 528 | outermost_graph_->IncrementNumberOfCHAGuards(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 529 | } |
| 530 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 531 | HInstruction* HInliner::AddTypeGuard(HInstruction* receiver, |
| 532 | HInstruction* cursor, |
| 533 | HBasicBlock* bb_cursor, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 534 | dex::TypeIndex class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 535 | Handle<mirror::Class> klass, |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 536 | HInstruction* invoke_instruction, |
| 537 | bool with_deoptimization) { |
| 538 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 539 | HInstanceFieldGet* receiver_class = BuildGetReceiverClass( |
| 540 | class_linker, receiver, invoke_instruction->GetDexPc()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 541 | if (cursor != nullptr) { |
| 542 | bb_cursor->InsertInstructionAfter(receiver_class, cursor); |
| 543 | } else { |
| 544 | bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction()); |
| 545 | } |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 546 | |
| 547 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 548 | bool is_referrer = (klass.Get() == outermost_graph_->GetArtMethod()->GetDeclaringClass()); |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 549 | // Note that we will just compare the classes, so we don't need Java semantics access checks. |
| 550 | // Note that the type index and the dex file are relative to the method this type guard is |
| 551 | // inlined into. |
| 552 | HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(), |
| 553 | class_index, |
| 554 | caller_dex_file, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 555 | klass, |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 556 | is_referrer, |
| 557 | invoke_instruction->GetDexPc(), |
| 558 | /* needs_access_check */ false); |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 559 | HLoadClass::LoadKind kind = HSharpening::SharpenClass( |
| 560 | load_class, codegen_, compiler_driver_, caller_compilation_unit_); |
| 561 | DCHECK(kind != HLoadClass::LoadKind::kInvalid) |
| 562 | << "We should always be able to reference a class for inline caches"; |
| 563 | // Insert before setting the kind, as setting the kind affects the inputs. |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 564 | bb_cursor->InsertInstructionAfter(load_class, receiver_class); |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 565 | load_class->SetLoadKind(kind); |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 566 | |
| 567 | // TODO: Extend reference type propagation to understand the guard. |
| 568 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, receiver_class); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 569 | bb_cursor->InsertInstructionAfter(compare, load_class); |
| 570 | if (with_deoptimization) { |
| 571 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
| 572 | compare, invoke_instruction->GetDexPc()); |
| 573 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 574 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 575 | } |
| 576 | return compare; |
| 577 | } |
| 578 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 579 | bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 580 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 581 | Handle<mirror::ObjectArray<mirror::Class>> classes) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 582 | DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface()) |
| 583 | << invoke_instruction->DebugName(); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 584 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 585 | if (TryInlinePolymorphicCallToSameTarget(invoke_instruction, resolved_method, classes)) { |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 586 | return true; |
| 587 | } |
| 588 | |
| 589 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 590 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 591 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 592 | |
| 593 | bool all_targets_inlined = true; |
| 594 | bool one_target_inlined = false; |
| 595 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 596 | if (classes->Get(i) == nullptr) { |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 597 | break; |
| 598 | } |
| 599 | ArtMethod* method = nullptr; |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 600 | |
| 601 | Handle<mirror::Class> handle = handles_->NewHandle(classes->Get(i)); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 602 | if (invoke_instruction->IsInvokeInterface()) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 603 | method = handle->FindVirtualMethodForInterface(resolved_method, pointer_size); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 604 | } else { |
| 605 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 606 | method = handle->FindVirtualMethodForVirtual(resolved_method, pointer_size); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 607 | } |
| 608 | |
| 609 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 610 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 611 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 612 | |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 613 | dex::TypeIndex class_index = FindClassIndexIn( |
| 614 | handle.Get(), caller_dex_file, caller_compilation_unit_.GetDexCache()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 615 | HInstruction* return_replacement = nullptr; |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 616 | if (!class_index.IsValid() || |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 617 | !TryBuildAndInline(invoke_instruction, |
| 618 | method, |
| 619 | ReferenceTypeInfo::Create(handle, /* is_exact */ true), |
| 620 | &return_replacement)) { |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 621 | all_targets_inlined = false; |
| 622 | } else { |
| 623 | one_target_inlined = true; |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 624 | |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 625 | VLOG(compiler) << "Polymorphic call to " << ArtMethod::PrettyMethod(resolved_method) |
| 626 | << " has inlined " << ArtMethod::PrettyMethod(method); |
| 627 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 628 | // If we have inlined all targets before, and this receiver is the last seen, |
| 629 | // we deoptimize instead of keeping the original invoke instruction. |
| 630 | bool deoptimize = all_targets_inlined && |
| 631 | (i != InlineCache::kIndividualCacheSize - 1) && |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 632 | (classes->Get(i + 1) == nullptr); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 633 | |
| 634 | if (outermost_graph_->IsCompilingOsr()) { |
| 635 | // We do not support HDeoptimize in OSR methods. |
| 636 | deoptimize = false; |
| 637 | } |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 638 | HInstruction* compare = AddTypeGuard(receiver, |
| 639 | cursor, |
| 640 | bb_cursor, |
| 641 | class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 642 | handle, |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 643 | invoke_instruction, |
| 644 | deoptimize); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 645 | if (deoptimize) { |
| 646 | if (return_replacement != nullptr) { |
| 647 | invoke_instruction->ReplaceWith(return_replacement); |
| 648 | } |
| 649 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
| 650 | // Because the inline cache data can be populated concurrently, we force the end of the |
| 651 | // iteration. Otherhwise, we could see a new receiver type. |
| 652 | break; |
| 653 | } else { |
| 654 | CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction); |
| 655 | } |
| 656 | } |
| 657 | } |
| 658 | |
| 659 | if (!one_target_inlined) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 660 | VLOG(compiler) << "Call to " << ArtMethod::PrettyMethod(resolved_method) |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 661 | << " from inline cache is not inlined because none" |
| 662 | << " of its targets could be inlined"; |
| 663 | return false; |
| 664 | } |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 665 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 666 | MaybeRecordStat(kInlinedPolymorphicCall); |
| 667 | |
| 668 | // Run type propagation to get the guards typed. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 669 | ReferenceTypePropagation rtp_fixup(graph_, |
| 670 | outer_compilation_unit_.GetDexCache(), |
| 671 | handles_, |
| 672 | /* is_first_run */ false); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 673 | rtp_fixup.Run(); |
| 674 | return true; |
| 675 | } |
| 676 | |
| 677 | void HInliner::CreateDiamondPatternForPolymorphicInline(HInstruction* compare, |
| 678 | HInstruction* return_replacement, |
| 679 | HInstruction* invoke_instruction) { |
| 680 | uint32_t dex_pc = invoke_instruction->GetDexPc(); |
| 681 | HBasicBlock* cursor_block = compare->GetBlock(); |
| 682 | HBasicBlock* original_invoke_block = invoke_instruction->GetBlock(); |
| 683 | ArenaAllocator* allocator = graph_->GetArena(); |
| 684 | |
| 685 | // Spit the block after the compare: `cursor_block` will now be the start of the diamond, |
| 686 | // and the returned block is the start of the then branch (that could contain multiple blocks). |
| 687 | HBasicBlock* then = cursor_block->SplitAfterForInlining(compare); |
| 688 | |
| 689 | // Split the block containing the invoke before and after the invoke. The returned block |
| 690 | // of the split before will contain the invoke and will be the otherwise branch of |
| 691 | // the diamond. The returned block of the split after will be the merge block |
| 692 | // of the diamond. |
| 693 | HBasicBlock* end_then = invoke_instruction->GetBlock(); |
| 694 | HBasicBlock* otherwise = end_then->SplitBeforeForInlining(invoke_instruction); |
| 695 | HBasicBlock* merge = otherwise->SplitAfterForInlining(invoke_instruction); |
| 696 | |
| 697 | // If the methods we are inlining return a value, we create a phi in the merge block |
| 698 | // that will have the `invoke_instruction and the `return_replacement` as inputs. |
| 699 | if (return_replacement != nullptr) { |
| 700 | HPhi* phi = new (allocator) HPhi( |
| 701 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke_instruction->GetType()), dex_pc); |
| 702 | merge->AddPhi(phi); |
| 703 | invoke_instruction->ReplaceWith(phi); |
| 704 | phi->AddInput(return_replacement); |
| 705 | phi->AddInput(invoke_instruction); |
| 706 | } |
| 707 | |
| 708 | // Add the control flow instructions. |
| 709 | otherwise->AddInstruction(new (allocator) HGoto(dex_pc)); |
| 710 | end_then->AddInstruction(new (allocator) HGoto(dex_pc)); |
| 711 | cursor_block->AddInstruction(new (allocator) HIf(compare, dex_pc)); |
| 712 | |
| 713 | // Add the newly created blocks to the graph. |
| 714 | graph_->AddBlock(then); |
| 715 | graph_->AddBlock(otherwise); |
| 716 | graph_->AddBlock(merge); |
| 717 | |
| 718 | // Set up successor (and implictly predecessor) relations. |
| 719 | cursor_block->AddSuccessor(otherwise); |
| 720 | cursor_block->AddSuccessor(then); |
| 721 | end_then->AddSuccessor(merge); |
| 722 | otherwise->AddSuccessor(merge); |
| 723 | |
| 724 | // Set up dominance information. |
| 725 | then->SetDominator(cursor_block); |
| 726 | cursor_block->AddDominatedBlock(then); |
| 727 | otherwise->SetDominator(cursor_block); |
| 728 | cursor_block->AddDominatedBlock(otherwise); |
| 729 | merge->SetDominator(cursor_block); |
| 730 | cursor_block->AddDominatedBlock(merge); |
| 731 | |
| 732 | // Update the revert post order. |
| 733 | size_t index = IndexOfElement(graph_->reverse_post_order_, cursor_block); |
| 734 | MakeRoomFor(&graph_->reverse_post_order_, 1, index); |
| 735 | graph_->reverse_post_order_[++index] = then; |
| 736 | index = IndexOfElement(graph_->reverse_post_order_, end_then); |
| 737 | MakeRoomFor(&graph_->reverse_post_order_, 2, index); |
| 738 | graph_->reverse_post_order_[++index] = otherwise; |
| 739 | graph_->reverse_post_order_[++index] = merge; |
| 740 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 741 | |
Nicolas Geoffray | a1d8ddf | 2016-02-29 11:46:58 +0000 | [diff] [blame] | 742 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 743 | then, original_invoke_block, /* replace_if_back_edge */ false); |
| 744 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 745 | otherwise, original_invoke_block, /* replace_if_back_edge */ false); |
| 746 | |
| 747 | // In case the original invoke location was a back edge, we need to update |
| 748 | // the loop to now have the merge block as a back edge. |
| 749 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 750 | merge, original_invoke_block, /* replace_if_back_edge */ true); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 751 | } |
| 752 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 753 | bool HInliner::TryInlinePolymorphicCallToSameTarget( |
| 754 | HInvoke* invoke_instruction, |
| 755 | ArtMethod* resolved_method, |
| 756 | Handle<mirror::ObjectArray<mirror::Class>> classes) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 757 | // This optimization only works under JIT for now. |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 758 | DCHECK(Runtime::Current()->UseJitCompilation()); |
Roland Levillain | 2aba7cd | 2016-02-03 12:27:20 +0000 | [diff] [blame] | 759 | if (graph_->GetInstructionSet() == kMips64) { |
| 760 | // TODO: Support HClassTableGet for mips64. |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 761 | return false; |
| 762 | } |
| 763 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 764 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 765 | |
| 766 | DCHECK(resolved_method != nullptr); |
| 767 | ArtMethod* actual_method = nullptr; |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 768 | size_t method_index = invoke_instruction->IsInvokeVirtual() |
| 769 | ? invoke_instruction->AsInvokeVirtual()->GetVTableIndex() |
| 770 | : invoke_instruction->AsInvokeInterface()->GetImtIndex(); |
| 771 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 772 | // Check whether we are actually calling the same method among |
| 773 | // the different types seen. |
| 774 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 775 | if (classes->Get(i) == nullptr) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 776 | break; |
| 777 | } |
| 778 | ArtMethod* new_method = nullptr; |
| 779 | if (invoke_instruction->IsInvokeInterface()) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 780 | new_method = classes->Get(i)->GetImt(pointer_size)->Get( |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 781 | method_index, pointer_size); |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 782 | if (new_method->IsRuntimeMethod()) { |
| 783 | // Bail out as soon as we see a conflict trampoline in one of the target's |
| 784 | // interface table. |
| 785 | return false; |
| 786 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 787 | } else { |
| 788 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 789 | new_method = classes->Get(i)->GetEmbeddedVTableEntry(method_index, pointer_size); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 790 | } |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 791 | DCHECK(new_method != nullptr); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 792 | if (actual_method == nullptr) { |
| 793 | actual_method = new_method; |
| 794 | } else if (actual_method != new_method) { |
| 795 | // Different methods, bailout. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 796 | VLOG(compiler) << "Call to " << ArtMethod::PrettyMethod(resolved_method) |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 797 | << " from inline cache is not inlined because it resolves" |
| 798 | << " to different methods"; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 799 | return false; |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 804 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 805 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 806 | |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 807 | HInstruction* return_replacement = nullptr; |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 808 | if (!TryBuildAndInline(invoke_instruction, |
| 809 | actual_method, |
| 810 | ReferenceTypeInfo::CreateInvalid(), |
| 811 | &return_replacement)) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 812 | return false; |
| 813 | } |
| 814 | |
| 815 | // We successfully inlined, now add a guard. |
| 816 | HInstanceFieldGet* receiver_class = BuildGetReceiverClass( |
| 817 | class_linker, receiver, invoke_instruction->GetDexPc()); |
| 818 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 819 | Primitive::Type type = Is64BitInstructionSet(graph_->GetInstructionSet()) |
| 820 | ? Primitive::kPrimLong |
| 821 | : Primitive::kPrimInt; |
| 822 | HClassTableGet* class_table_get = new (graph_->GetArena()) HClassTableGet( |
| 823 | receiver_class, |
| 824 | type, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 825 | invoke_instruction->IsInvokeVirtual() ? HClassTableGet::TableKind::kVTable |
| 826 | : HClassTableGet::TableKind::kIMTable, |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 827 | method_index, |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 828 | invoke_instruction->GetDexPc()); |
| 829 | |
| 830 | HConstant* constant; |
| 831 | if (type == Primitive::kPrimLong) { |
| 832 | constant = graph_->GetLongConstant( |
| 833 | reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc()); |
| 834 | } else { |
| 835 | constant = graph_->GetIntConstant( |
| 836 | reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc()); |
| 837 | } |
| 838 | |
| 839 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(class_table_get, constant); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 840 | if (cursor != nullptr) { |
| 841 | bb_cursor->InsertInstructionAfter(receiver_class, cursor); |
| 842 | } else { |
| 843 | bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction()); |
| 844 | } |
| 845 | bb_cursor->InsertInstructionAfter(class_table_get, receiver_class); |
| 846 | bb_cursor->InsertInstructionAfter(compare, class_table_get); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 847 | |
| 848 | if (outermost_graph_->IsCompilingOsr()) { |
| 849 | CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction); |
| 850 | } else { |
| 851 | // TODO: Extend reference type propagation to understand the guard. |
| 852 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
| 853 | compare, invoke_instruction->GetDexPc()); |
| 854 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 855 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 856 | if (return_replacement != nullptr) { |
| 857 | invoke_instruction->ReplaceWith(return_replacement); |
| 858 | } |
Nicolas Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 859 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 860 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 861 | |
| 862 | // Run type propagation to get the guard typed. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 863 | ReferenceTypePropagation rtp_fixup(graph_, |
| 864 | outer_compilation_unit_.GetDexCache(), |
| 865 | handles_, |
| 866 | /* is_first_run */ false); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 867 | rtp_fixup.Run(); |
| 868 | |
| 869 | MaybeRecordStat(kInlinedPolymorphicCall); |
| 870 | |
| 871 | return true; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 872 | } |
| 873 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 874 | bool HInliner::TryInlineAndReplace(HInvoke* invoke_instruction, |
| 875 | ArtMethod* method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 876 | ReferenceTypeInfo receiver_type, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 877 | bool do_rtp, |
| 878 | bool cha_devirtualize) { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 879 | HInstruction* return_replacement = nullptr; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 880 | uint32_t dex_pc = invoke_instruction->GetDexPc(); |
| 881 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 882 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 883 | if (!TryBuildAndInline(invoke_instruction, method, receiver_type, &return_replacement)) { |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 884 | if (invoke_instruction->IsInvokeInterface()) { |
| 885 | // Turn an invoke-interface into an invoke-virtual. An invoke-virtual is always |
| 886 | // better than an invoke-interface because: |
| 887 | // 1) In the best case, the interface call has one more indirection (to fetch the IMT). |
| 888 | // 2) We will not go to the conflict trampoline with an invoke-virtual. |
| 889 | // TODO: Consider sharpening once it is not dependent on the compiler driver. |
| 890 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 891 | uint32_t dex_method_index = FindMethodIndexIn( |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 892 | method, caller_dex_file, invoke_instruction->GetDexMethodIndex()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 893 | if (dex_method_index == DexFile::kDexNoIndex) { |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 894 | return false; |
| 895 | } |
| 896 | HInvokeVirtual* new_invoke = new (graph_->GetArena()) HInvokeVirtual( |
| 897 | graph_->GetArena(), |
| 898 | invoke_instruction->GetNumberOfArguments(), |
| 899 | invoke_instruction->GetType(), |
| 900 | invoke_instruction->GetDexPc(), |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 901 | dex_method_index, |
| 902 | method, |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 903 | method->GetMethodIndex()); |
| 904 | HInputsRef inputs = invoke_instruction->GetInputs(); |
| 905 | for (size_t index = 0; index != inputs.size(); ++index) { |
| 906 | new_invoke->SetArgumentAt(index, inputs[index]); |
| 907 | } |
| 908 | invoke_instruction->GetBlock()->InsertInstructionBefore(new_invoke, invoke_instruction); |
| 909 | new_invoke->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 910 | if (invoke_instruction->GetType() == Primitive::kPrimNot) { |
| 911 | new_invoke->SetReferenceTypeInfo(invoke_instruction->GetReferenceTypeInfo()); |
| 912 | } |
| 913 | return_replacement = new_invoke; |
| 914 | } else { |
| 915 | // TODO: Consider sharpening an invoke virtual once it is not dependent on the |
| 916 | // compiler driver. |
| 917 | return false; |
| 918 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 919 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 920 | if (cha_devirtualize) { |
| 921 | AddCHAGuard(invoke_instruction, dex_pc, cursor, bb_cursor); |
| 922 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 923 | if (return_replacement != nullptr) { |
| 924 | invoke_instruction->ReplaceWith(return_replacement); |
| 925 | } |
| 926 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 927 | FixUpReturnReferenceType(method, return_replacement); |
| 928 | if (do_rtp && ReturnTypeMoreSpecific(invoke_instruction, return_replacement)) { |
| 929 | // Actual return value has a more specific type than the method's declared |
| 930 | // return type. Run RTP again on the outer graph to propagate it. |
| 931 | ReferenceTypePropagation(graph_, |
| 932 | outer_compilation_unit_.GetDexCache(), |
| 933 | handles_, |
| 934 | /* is_first_run */ false).Run(); |
| 935 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 936 | return true; |
| 937 | } |
| 938 | |
| 939 | bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction, |
| 940 | ArtMethod* method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 941 | ReferenceTypeInfo receiver_type, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 942 | HInstruction** return_replacement) { |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 943 | if (method->IsProxyMethod()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 944 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 945 | << " is not inlined because of unimplemented inline support for proxy methods."; |
| 946 | return false; |
| 947 | } |
| 948 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 949 | // Check whether we're allowed to inline. The outermost compilation unit is the relevant |
| 950 | // dex file here (though the transitivity of an inline chain would allow checking the calller). |
| 951 | if (!compiler_driver_->MayInline(method->GetDexFile(), |
| 952 | outer_compilation_unit_.GetDexFile())) { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 953 | if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 954 | VLOG(compiler) << "Successfully replaced pattern of invoke " |
| 955 | << method->PrettyMethod(); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 956 | MaybeRecordStat(kReplacedInvokeWithSimplePattern); |
| 957 | return true; |
| 958 | } |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 959 | VLOG(compiler) << "Won't inline " << method->PrettyMethod() << " in " |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 960 | << outer_compilation_unit_.GetDexFile()->GetLocation() << " (" |
| 961 | << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from " |
| 962 | << method->GetDexFile()->GetLocation(); |
| 963 | return false; |
| 964 | } |
| 965 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 966 | bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile()); |
| 967 | |
| 968 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 969 | |
| 970 | if (code_item == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 971 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 972 | << " is not inlined because it is native"; |
| 973 | return false; |
| 974 | } |
| 975 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 976 | size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits(); |
| 977 | if (code_item->insns_size_in_code_units_ > inline_max_code_units) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 978 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 979 | << " is too big to inline: " |
| 980 | << code_item->insns_size_in_code_units_ |
| 981 | << " > " |
| 982 | << inline_max_code_units; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 983 | return false; |
| 984 | } |
| 985 | |
| 986 | if (code_item->tries_size_ != 0) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 987 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 988 | << " is not inlined because of try block"; |
| 989 | return false; |
| 990 | } |
| 991 | |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 992 | if (!method->IsCompilable()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 993 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 994 | << " has soft failures un-handled by the compiler, so it cannot be inlined"; |
| 995 | } |
| 996 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 997 | if (!method->GetDeclaringClass()->IsVerified()) { |
| 998 | uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 999 | if (Runtime::Current()->UseJitCompilation() || |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 1000 | !compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 1001 | method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1002 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Nicolas Geoffray | ccc6197 | 2015-10-01 14:34:20 +0100 | [diff] [blame] | 1003 | << " couldn't be verified, so it cannot be inlined"; |
| 1004 | return false; |
| 1005 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1006 | } |
| 1007 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1008 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 1009 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 1010 | // Case of a static method that cannot be inlined because it implicitly |
| 1011 | // requires an initialization check of its declaring class. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1012 | VLOG(compiler) << "Method " << method->PrettyMethod() |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1013 | << " is not inlined because it is static and requires a clinit" |
| 1014 | << " check that cannot be emitted due to Dex cache limitations"; |
| 1015 | return false; |
| 1016 | } |
| 1017 | |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1018 | if (!TryBuildAndInlineHelper( |
| 1019 | invoke_instruction, method, receiver_type, same_dex_file, return_replacement)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 1020 | return false; |
| 1021 | } |
| 1022 | |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1023 | VLOG(compiler) << "Successfully inlined " << method->PrettyMethod(); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 1024 | MaybeRecordStat(kInlinedInvoke); |
| 1025 | return true; |
| 1026 | } |
| 1027 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1028 | static HInstruction* GetInvokeInputForArgVRegIndex(HInvoke* invoke_instruction, |
| 1029 | size_t arg_vreg_index) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1030 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1031 | size_t input_index = 0; |
| 1032 | for (size_t i = 0; i < arg_vreg_index; ++i, ++input_index) { |
| 1033 | DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments()); |
| 1034 | if (Primitive::Is64BitType(invoke_instruction->InputAt(input_index)->GetType())) { |
| 1035 | ++i; |
| 1036 | DCHECK_NE(i, arg_vreg_index); |
| 1037 | } |
| 1038 | } |
| 1039 | DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments()); |
| 1040 | return invoke_instruction->InputAt(input_index); |
| 1041 | } |
| 1042 | |
| 1043 | // Try to recognize known simple patterns and replace invoke call with appropriate instructions. |
| 1044 | bool HInliner::TryPatternSubstitution(HInvoke* invoke_instruction, |
| 1045 | ArtMethod* resolved_method, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1046 | HInstruction** return_replacement) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1047 | InlineMethod inline_method; |
| 1048 | if (!InlineMethodAnalyser::AnalyseMethodCode(resolved_method, &inline_method)) { |
| 1049 | return false; |
| 1050 | } |
| 1051 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1052 | switch (inline_method.opcode) { |
| 1053 | case kInlineOpNop: |
| 1054 | DCHECK_EQ(invoke_instruction->GetType(), Primitive::kPrimVoid); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1055 | *return_replacement = nullptr; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1056 | break; |
| 1057 | case kInlineOpReturnArg: |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1058 | *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, |
| 1059 | inline_method.d.return_data.arg); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1060 | break; |
| 1061 | case kInlineOpNonWideConst: |
| 1062 | if (resolved_method->GetShorty()[0] == 'L') { |
| 1063 | DCHECK_EQ(inline_method.d.data, 0u); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1064 | *return_replacement = graph_->GetNullConstant(); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1065 | } else { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1066 | *return_replacement = graph_->GetIntConstant(static_cast<int32_t>(inline_method.d.data)); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1067 | } |
| 1068 | break; |
| 1069 | case kInlineOpIGet: { |
| 1070 | const InlineIGetIPutData& data = inline_method.d.ifield_data; |
| 1071 | if (data.method_is_static || data.object_arg != 0u) { |
| 1072 | // TODO: Needs null check. |
| 1073 | return false; |
| 1074 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1075 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1076 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1077 | HInstanceFieldGet* iget = CreateInstanceFieldGet(dex_cache, data.field_idx, obj); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1078 | DCHECK_EQ(iget->GetFieldOffset().Uint32Value(), data.field_offset); |
| 1079 | DCHECK_EQ(iget->IsVolatile() ? 1u : 0u, data.is_volatile); |
| 1080 | invoke_instruction->GetBlock()->InsertInstructionBefore(iget, invoke_instruction); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1081 | *return_replacement = iget; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1082 | break; |
| 1083 | } |
| 1084 | case kInlineOpIPut: { |
| 1085 | const InlineIGetIPutData& data = inline_method.d.ifield_data; |
| 1086 | if (data.method_is_static || data.object_arg != 0u) { |
| 1087 | // TODO: Needs null check. |
| 1088 | return false; |
| 1089 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1090 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1091 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg); |
| 1092 | HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, data.src_arg); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1093 | HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, data.field_idx, obj, value); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1094 | DCHECK_EQ(iput->GetFieldOffset().Uint32Value(), data.field_offset); |
| 1095 | DCHECK_EQ(iput->IsVolatile() ? 1u : 0u, data.is_volatile); |
| 1096 | invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction); |
| 1097 | if (data.return_arg_plus1 != 0u) { |
| 1098 | size_t return_arg = data.return_arg_plus1 - 1u; |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1099 | *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, return_arg); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1100 | } |
| 1101 | break; |
| 1102 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1103 | case kInlineOpConstructor: { |
| 1104 | const InlineConstructorData& data = inline_method.d.constructor_data; |
| 1105 | // Get the indexes to arrays for easier processing. |
| 1106 | uint16_t iput_field_indexes[] = { |
| 1107 | data.iput0_field_index, data.iput1_field_index, data.iput2_field_index |
| 1108 | }; |
| 1109 | uint16_t iput_args[] = { data.iput0_arg, data.iput1_arg, data.iput2_arg }; |
| 1110 | static_assert(arraysize(iput_args) == arraysize(iput_field_indexes), "Size mismatch"); |
| 1111 | // Count valid field indexes. |
| 1112 | size_t number_of_iputs = 0u; |
| 1113 | while (number_of_iputs != arraysize(iput_field_indexes) && |
| 1114 | iput_field_indexes[number_of_iputs] != DexFile::kDexNoIndex16) { |
| 1115 | // Check that there are no duplicate valid field indexes. |
| 1116 | DCHECK_EQ(0, std::count(iput_field_indexes + number_of_iputs + 1, |
| 1117 | iput_field_indexes + arraysize(iput_field_indexes), |
| 1118 | iput_field_indexes[number_of_iputs])); |
| 1119 | ++number_of_iputs; |
| 1120 | } |
| 1121 | // Check that there are no valid field indexes in the rest of the array. |
| 1122 | DCHECK_EQ(0, std::count_if(iput_field_indexes + number_of_iputs, |
| 1123 | iput_field_indexes + arraysize(iput_field_indexes), |
| 1124 | [](uint16_t index) { return index != DexFile::kDexNoIndex16; })); |
| 1125 | |
| 1126 | // Create HInstanceFieldSet for each IPUT that stores non-zero data. |
| 1127 | Handle<mirror::DexCache> dex_cache; |
| 1128 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, /* this */ 0u); |
| 1129 | bool needs_constructor_barrier = false; |
| 1130 | for (size_t i = 0; i != number_of_iputs; ++i) { |
| 1131 | HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, iput_args[i]); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1132 | if (!value->IsConstant() || !value->AsConstant()->IsZeroBitPattern()) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1133 | if (dex_cache.GetReference() == nullptr) { |
| 1134 | dex_cache = handles_->NewHandle(resolved_method->GetDexCache()); |
| 1135 | } |
| 1136 | uint16_t field_index = iput_field_indexes[i]; |
| 1137 | HInstanceFieldSet* iput = CreateInstanceFieldSet(dex_cache, field_index, obj, value); |
| 1138 | invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction); |
| 1139 | |
| 1140 | // Check whether the field is final. If it is, we need to add a barrier. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1141 | PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet()); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1142 | ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size); |
| 1143 | DCHECK(resolved_field != nullptr); |
| 1144 | if (resolved_field->IsFinal()) { |
| 1145 | needs_constructor_barrier = true; |
| 1146 | } |
| 1147 | } |
| 1148 | } |
| 1149 | if (needs_constructor_barrier) { |
| 1150 | HMemoryBarrier* barrier = new (graph_->GetArena()) HMemoryBarrier(kStoreStore, kNoDexPc); |
| 1151 | invoke_instruction->GetBlock()->InsertInstructionBefore(barrier, invoke_instruction); |
| 1152 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1153 | *return_replacement = nullptr; |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1154 | break; |
| 1155 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1156 | default: |
| 1157 | LOG(FATAL) << "UNREACHABLE"; |
| 1158 | UNREACHABLE(); |
| 1159 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1160 | return true; |
| 1161 | } |
| 1162 | |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1163 | HInstanceFieldGet* HInliner::CreateInstanceFieldGet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1164 | uint32_t field_index, |
| 1165 | HInstruction* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1166 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1167 | PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet()); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1168 | ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size); |
| 1169 | DCHECK(resolved_field != nullptr); |
| 1170 | HInstanceFieldGet* iget = new (graph_->GetArena()) HInstanceFieldGet( |
| 1171 | obj, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1172 | resolved_field, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1173 | resolved_field->GetTypeAsPrimitiveType(), |
| 1174 | resolved_field->GetOffset(), |
| 1175 | resolved_field->IsVolatile(), |
| 1176 | field_index, |
| 1177 | resolved_field->GetDeclaringClass()->GetDexClassDefIndex(), |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1178 | *dex_cache->GetDexFile(), |
Vladimir Marko | adda435 | 2016-01-29 10:24:41 +0000 | [diff] [blame] | 1179 | // Read barrier generates a runtime call in slow path and we need a valid |
| 1180 | // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537. |
| 1181 | /* dex_pc */ 0); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1182 | if (iget->GetType() == Primitive::kPrimNot) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 1183 | // Use the same dex_cache that we used for field lookup as the hint_dex_cache. |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 1184 | ReferenceTypePropagation rtp(graph_, dex_cache, handles_, /* is_first_run */ false); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1185 | rtp.Visit(iget); |
| 1186 | } |
| 1187 | return iget; |
| 1188 | } |
| 1189 | |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1190 | HInstanceFieldSet* HInliner::CreateInstanceFieldSet(Handle<mirror::DexCache> dex_cache, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1191 | uint32_t field_index, |
| 1192 | HInstruction* obj, |
| 1193 | HInstruction* value) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1194 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1195 | PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet()); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1196 | ArtField* resolved_field = dex_cache->GetResolvedField(field_index, pointer_size); |
| 1197 | DCHECK(resolved_field != nullptr); |
| 1198 | HInstanceFieldSet* iput = new (graph_->GetArena()) HInstanceFieldSet( |
| 1199 | obj, |
| 1200 | value, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1201 | resolved_field, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1202 | resolved_field->GetTypeAsPrimitiveType(), |
| 1203 | resolved_field->GetOffset(), |
| 1204 | resolved_field->IsVolatile(), |
| 1205 | field_index, |
| 1206 | resolved_field->GetDeclaringClass()->GetDexClassDefIndex(), |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1207 | *dex_cache->GetDexFile(), |
Vladimir Marko | adda435 | 2016-01-29 10:24:41 +0000 | [diff] [blame] | 1208 | // Read barrier generates a runtime call in slow path and we need a valid |
| 1209 | // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537. |
| 1210 | /* dex_pc */ 0); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1211 | return iput; |
| 1212 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1213 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1214 | bool HInliner::TryBuildAndInlineHelper(HInvoke* invoke_instruction, |
| 1215 | ArtMethod* resolved_method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1216 | ReferenceTypeInfo receiver_type, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1217 | bool same_dex_file, |
| 1218 | HInstruction** return_replacement) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1219 | DCHECK(!(resolved_method->IsStatic() && receiver_type.IsValid())); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 1220 | ScopedObjectAccess soa(Thread::Current()); |
| 1221 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1222 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 1223 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1224 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 1225 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Nicolas Geoffray | f1aedb1 | 2016-07-28 03:49:14 +0100 | [diff] [blame] | 1226 | Handle<mirror::ClassLoader> class_loader(handles_->NewHandle( |
| 1227 | resolved_method->GetDeclaringClass()->GetClassLoader())); |
| 1228 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1229 | DexCompilationUnit dex_compilation_unit( |
Mathieu Chartier | 5812e20 | 2017-02-13 18:32:04 -0800 | [diff] [blame] | 1230 | class_loader.ToJObject(), |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 1231 | class_linker, |
| 1232 | callee_dex_file, |
| 1233 | code_item, |
| 1234 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
| 1235 | method_index, |
| 1236 | resolved_method->GetAccessFlags(), |
| 1237 | /* verified_method */ nullptr, |
| 1238 | dex_cache); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1239 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 1240 | bool requires_ctor_barrier = false; |
| 1241 | |
| 1242 | if (dex_compilation_unit.IsConstructor()) { |
| 1243 | // If it's a super invocation and we already generate a barrier there's no need |
| 1244 | // to generate another one. |
| 1245 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 1246 | // same as the local "this" pointer then we must have a super invocation. |
| 1247 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 1248 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 1249 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 1250 | requires_ctor_barrier = false; |
| 1251 | } else { |
| 1252 | Thread* self = Thread::Current(); |
| 1253 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 1254 | dex_compilation_unit.GetDexFile(), |
| 1255 | dex_compilation_unit.GetClassDefIndex()); |
| 1256 | } |
| 1257 | } |
| 1258 | |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 1259 | InvokeType invoke_type = invoke_instruction->GetInvokeType(); |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 1260 | if (invoke_type == kInterface) { |
| 1261 | // We have statically resolved the dispatch. To please the class linker |
| 1262 | // at runtime, we change this call as if it was a virtual call. |
| 1263 | invoke_type = kVirtual; |
| 1264 | } |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1265 | |
| 1266 | const int32_t caller_instruction_counter = graph_->GetCurrentInstructionId(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 1267 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1268 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1269 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1270 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 1271 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1272 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 1273 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1274 | graph_->IsDebuggable(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1275 | /* osr */ false, |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1276 | caller_instruction_counter); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1277 | callee_graph->SetArtMethod(resolved_method); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 1278 | |
Roland Levillain | a8013fd | 2016-04-04 15:34:31 +0100 | [diff] [blame] | 1279 | // When they are needed, allocate `inline_stats` on the heap instead |
| 1280 | // of on the stack, as Clang might produce a stack frame too large |
| 1281 | // for this function, that would not fit the requirements of the |
| 1282 | // `-Wframe-larger-than` option. |
| 1283 | std::unique_ptr<OptimizingCompilerStats> inline_stats = |
| 1284 | (stats_ == nullptr) ? nullptr : MakeUnique<OptimizingCompilerStats>(); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 1285 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1286 | &dex_compilation_unit, |
| 1287 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1288 | resolved_method->GetDexFile(), |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 1289 | *code_item, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1290 | compiler_driver_, |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 1291 | codegen_, |
Roland Levillain | a8013fd | 2016-04-04 15:34:31 +0100 | [diff] [blame] | 1292 | inline_stats.get(), |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1293 | resolved_method->GetQuickenedInfo(class_linker->GetImagePointerSize()), |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 1294 | dex_cache, |
| 1295 | handles_); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1296 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 1297 | if (builder.BuildGraph() != kAnalysisSuccess) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1298 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1299 | << " could not be built, so cannot be inlined"; |
| 1300 | return false; |
| 1301 | } |
| 1302 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 1303 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 1304 | compiler_driver_->GetInstructionSet())) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1305 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 1306 | << " cannot be inlined because of the register allocator"; |
| 1307 | return false; |
| 1308 | } |
| 1309 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1310 | size_t parameter_index = 0; |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1311 | bool run_rtp = false; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1312 | for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions()); |
| 1313 | !instructions.Done(); |
| 1314 | instructions.Advance()) { |
| 1315 | HInstruction* current = instructions.Current(); |
| 1316 | if (current->IsParameterValue()) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1317 | HInstruction* argument = invoke_instruction->InputAt(parameter_index); |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1318 | if (argument->IsNullConstant()) { |
| 1319 | current->ReplaceWith(callee_graph->GetNullConstant()); |
| 1320 | } else if (argument->IsIntConstant()) { |
| 1321 | current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue())); |
| 1322 | } else if (argument->IsLongConstant()) { |
| 1323 | current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue())); |
| 1324 | } else if (argument->IsFloatConstant()) { |
| 1325 | current->ReplaceWith( |
| 1326 | callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue())); |
| 1327 | } else if (argument->IsDoubleConstant()) { |
| 1328 | current->ReplaceWith( |
| 1329 | callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue())); |
| 1330 | } else if (argument->GetType() == Primitive::kPrimNot) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1331 | if (!resolved_method->IsStatic() && parameter_index == 0 && receiver_type.IsValid()) { |
| 1332 | run_rtp = true; |
| 1333 | current->SetReferenceTypeInfo(receiver_type); |
| 1334 | } else { |
| 1335 | current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo()); |
| 1336 | } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1337 | current->AsParameterValue()->SetCanBeNull(argument->CanBeNull()); |
| 1338 | } |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1339 | ++parameter_index; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1340 | } |
| 1341 | } |
| 1342 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1343 | // We have replaced formal arguments with actual arguments. If actual types |
| 1344 | // are more specific than the declared ones, run RTP again on the inner graph. |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1345 | if (run_rtp || ArgumentTypesMoreSpecific(invoke_instruction, resolved_method)) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1346 | ReferenceTypePropagation(callee_graph, |
| 1347 | dex_compilation_unit.GetDexCache(), |
| 1348 | handles_, |
| 1349 | /* is_first_run */ false).Run(); |
| 1350 | } |
| 1351 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1352 | size_t number_of_instructions_budget = kMaximumNumberOfHInstructions; |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1353 | size_t number_of_inlined_instructions = |
| 1354 | RunOptimizations(callee_graph, code_item, dex_compilation_unit); |
| 1355 | number_of_instructions_budget += number_of_inlined_instructions; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1356 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1357 | // TODO: We should abort only if all predecessors throw. However, |
| 1358 | // HGraph::InlineInto currently does not handle an exit block with |
| 1359 | // a throw predecessor. |
| 1360 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 1361 | if (exit_block == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1362 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1363 | << " could not be inlined because it has an infinite loop"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1364 | return false; |
| 1365 | } |
| 1366 | |
| 1367 | bool has_throw_predecessor = false; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1368 | for (HBasicBlock* predecessor : exit_block->GetPredecessors()) { |
| 1369 | if (predecessor->GetLastInstruction()->IsThrow()) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1370 | has_throw_predecessor = true; |
| 1371 | break; |
| 1372 | } |
| 1373 | } |
| 1374 | if (has_throw_predecessor) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1375 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1376 | << " could not be inlined because one branch always throws"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1377 | return false; |
| 1378 | } |
| 1379 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1380 | size_t number_of_instructions = 0; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1381 | |
| 1382 | bool can_inline_environment = |
| 1383 | total_number_of_dex_registers_ < kMaximumNumberOfCumulatedDexRegisters; |
| 1384 | |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 1385 | // Skip the entry block, it does not contain instructions that prevent inlining. |
| 1386 | for (HBasicBlock* block : callee_graph->GetReversePostOrderSkipEntryBlock()) { |
David Sehr | c757dec | 2016-11-04 15:48:34 -0700 | [diff] [blame] | 1387 | if (block->IsLoopHeader()) { |
| 1388 | if (block->GetLoopInformation()->IsIrreducible()) { |
| 1389 | // Don't inline methods with irreducible loops, they could prevent some |
| 1390 | // optimizations to run. |
| 1391 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1392 | << " could not be inlined because it contains an irreducible loop"; |
| 1393 | return false; |
| 1394 | } |
| 1395 | if (!block->GetLoopInformation()->HasExitEdge()) { |
| 1396 | // Don't inline methods with loops without exit, since they cause the |
| 1397 | // loop information to be computed incorrectly when updating after |
| 1398 | // inlining. |
| 1399 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1400 | << " could not be inlined because it contains a loop with no exit"; |
| 1401 | return false; |
| 1402 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1403 | } |
| 1404 | |
| 1405 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 1406 | !instr_it.Done(); |
| 1407 | instr_it.Advance()) { |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1408 | if (number_of_instructions++ == number_of_instructions_budget) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1409 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1410 | << " is not inlined because its caller has reached" |
| 1411 | << " its instruction budget limit."; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1412 | return false; |
| 1413 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1414 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1415 | if (!can_inline_environment && current->NeedsEnvironment()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1416 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1417 | << " is not inlined because its caller has reached" |
| 1418 | << " its environment budget limit."; |
| 1419 | return false; |
| 1420 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1421 | |
Nicolas Geoffray | fbdfa6d | 2017-02-03 10:43:13 +0000 | [diff] [blame] | 1422 | if (current->NeedsEnvironment() && |
| 1423 | !CanEncodeInlinedMethodInStackMap(*caller_compilation_unit_.GetDexFile(), |
| 1424 | resolved_method)) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1425 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1426 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | fbdfa6d | 2017-02-03 10:43:13 +0000 | [diff] [blame] | 1427 | << " needs an environment, is in a different dex file" |
| 1428 | << ", and cannot be encoded in the stack maps."; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1429 | return false; |
| 1430 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1431 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1432 | if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1433 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1434 | << " could not be inlined because " << current->DebugName() |
| 1435 | << " it is in a different dex file and requires access to the dex cache"; |
| 1436 | return false; |
| 1437 | } |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1438 | |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1439 | if (current->IsUnresolvedStaticFieldGet() || |
| 1440 | current->IsUnresolvedInstanceFieldGet() || |
| 1441 | current->IsUnresolvedStaticFieldSet() || |
| 1442 | current->IsUnresolvedInstanceFieldSet()) { |
| 1443 | // Entrypoint for unresolved fields does not handle inlined frames. |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 1444 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1445 | << " could not be inlined because it is using an unresolved" |
| 1446 | << " entrypoint"; |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1447 | return false; |
| 1448 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1449 | } |
| 1450 | } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1451 | number_of_inlined_instructions_ += number_of_instructions; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1452 | |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1453 | DCHECK_EQ(caller_instruction_counter, graph_->GetCurrentInstructionId()) |
| 1454 | << "No instructions can be added to the outer graph while inner graph is being built"; |
| 1455 | |
| 1456 | const int32_t callee_instruction_counter = callee_graph->GetCurrentInstructionId(); |
| 1457 | graph_->SetCurrentInstructionId(callee_instruction_counter); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1458 | *return_replacement = callee_graph->InlineInto(graph_, invoke_instruction); |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1459 | |
| 1460 | DCHECK_EQ(callee_instruction_counter, callee_graph->GetCurrentInstructionId()) |
| 1461 | << "No instructions can be added to the inner graph during inlining into the outer graph"; |
| 1462 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1463 | return true; |
| 1464 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1465 | |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1466 | size_t HInliner::RunOptimizations(HGraph* callee_graph, |
| 1467 | const DexFile::CodeItem* code_item, |
| 1468 | const DexCompilationUnit& dex_compilation_unit) { |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1469 | // Note: if the outermost_graph_ is being compiled OSR, we should not run any |
| 1470 | // optimization that could lead to a HDeoptimize. The following optimizations do not. |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 1471 | HDeadCodeElimination dce(callee_graph, stats_, "dead_code_elimination$inliner"); |
| 1472 | HConstantFolding fold(callee_graph, "constant_folding$inliner"); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1473 | HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_, handles_); |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1474 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | 762869d | 2016-07-15 15:28:35 +0100 | [diff] [blame] | 1475 | IntrinsicsRecognizer intrinsics(callee_graph, stats_); |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1476 | |
| 1477 | HOptimization* optimizations[] = { |
| 1478 | &intrinsics, |
| 1479 | &sharpening, |
| 1480 | &simplify, |
| 1481 | &fold, |
| 1482 | &dce, |
| 1483 | }; |
| 1484 | |
| 1485 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 1486 | HOptimization* optimization = optimizations[i]; |
| 1487 | optimization->Run(); |
| 1488 | } |
| 1489 | |
| 1490 | size_t number_of_inlined_instructions = 0u; |
| 1491 | if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) { |
| 1492 | HInliner inliner(callee_graph, |
| 1493 | outermost_graph_, |
| 1494 | codegen_, |
| 1495 | outer_compilation_unit_, |
| 1496 | dex_compilation_unit, |
| 1497 | compiler_driver_, |
| 1498 | handles_, |
| 1499 | stats_, |
| 1500 | total_number_of_dex_registers_ + code_item->registers_size_, |
| 1501 | depth_ + 1); |
| 1502 | inliner.Run(); |
| 1503 | number_of_inlined_instructions += inliner.number_of_inlined_instructions_; |
| 1504 | } |
| 1505 | |
| 1506 | return number_of_inlined_instructions; |
| 1507 | } |
| 1508 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1509 | static bool IsReferenceTypeRefinement(ReferenceTypeInfo declared_rti, |
| 1510 | bool declared_can_be_null, |
| 1511 | HInstruction* actual_obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1512 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1513 | if (declared_can_be_null && !actual_obj->CanBeNull()) { |
| 1514 | return true; |
| 1515 | } |
| 1516 | |
| 1517 | ReferenceTypeInfo actual_rti = actual_obj->GetReferenceTypeInfo(); |
| 1518 | return (actual_rti.IsExact() && !declared_rti.IsExact()) || |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1519 | declared_rti.IsStrictSupertypeOf(actual_rti); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1520 | } |
| 1521 | |
| 1522 | ReferenceTypeInfo HInliner::GetClassRTI(mirror::Class* klass) { |
| 1523 | return ReferenceTypePropagation::IsAdmissible(klass) |
| 1524 | ? ReferenceTypeInfo::Create(handles_->NewHandle(klass)) |
| 1525 | : graph_->GetInexactObjectRti(); |
| 1526 | } |
| 1527 | |
| 1528 | bool HInliner::ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method) { |
| 1529 | // If this is an instance call, test whether the type of the `this` argument |
| 1530 | // is more specific than the class which declares the method. |
| 1531 | if (!resolved_method->IsStatic()) { |
| 1532 | if (IsReferenceTypeRefinement(GetClassRTI(resolved_method->GetDeclaringClass()), |
| 1533 | /* declared_can_be_null */ false, |
| 1534 | invoke_instruction->InputAt(0u))) { |
| 1535 | return true; |
| 1536 | } |
| 1537 | } |
| 1538 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1539 | // Iterate over the list of parameter types and test whether any of the |
| 1540 | // actual inputs has a more specific reference type than the type declared in |
| 1541 | // the signature. |
| 1542 | const DexFile::TypeList* param_list = resolved_method->GetParameterTypeList(); |
| 1543 | for (size_t param_idx = 0, |
| 1544 | input_idx = resolved_method->IsStatic() ? 0 : 1, |
| 1545 | e = (param_list == nullptr ? 0 : param_list->Size()); |
| 1546 | param_idx < e; |
| 1547 | ++param_idx, ++input_idx) { |
| 1548 | HInstruction* input = invoke_instruction->InputAt(input_idx); |
| 1549 | if (input->GetType() == Primitive::kPrimNot) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1550 | mirror::Class* param_cls = resolved_method->GetClassFromTypeIndex( |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1551 | param_list->GetTypeItem(param_idx).type_idx_, |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1552 | /* resolve */ false); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1553 | if (IsReferenceTypeRefinement(GetClassRTI(param_cls), |
| 1554 | /* declared_can_be_null */ true, |
| 1555 | input)) { |
| 1556 | return true; |
| 1557 | } |
| 1558 | } |
| 1559 | } |
| 1560 | |
| 1561 | return false; |
| 1562 | } |
| 1563 | |
| 1564 | bool HInliner::ReturnTypeMoreSpecific(HInvoke* invoke_instruction, |
| 1565 | HInstruction* return_replacement) { |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 1566 | // 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] | 1567 | if (return_replacement != nullptr) { |
| 1568 | if (return_replacement->GetType() == Primitive::kPrimNot) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1569 | // Test if the return type is a refinement of the declared return type. |
| 1570 | if (IsReferenceTypeRefinement(invoke_instruction->GetReferenceTypeInfo(), |
| 1571 | /* declared_can_be_null */ true, |
| 1572 | return_replacement)) { |
| 1573 | return true; |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1574 | } else if (return_replacement->IsInstanceFieldGet()) { |
| 1575 | HInstanceFieldGet* field_get = return_replacement->AsInstanceFieldGet(); |
| 1576 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1577 | if (field_get->GetFieldInfo().GetField() == |
| 1578 | class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0)) { |
| 1579 | return true; |
| 1580 | } |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1581 | } |
| 1582 | } else if (return_replacement->IsInstanceOf()) { |
| 1583 | // Inlining InstanceOf into an If may put a tighter bound on reference types. |
| 1584 | return true; |
| 1585 | } |
| 1586 | } |
| 1587 | |
| 1588 | return false; |
| 1589 | } |
| 1590 | |
| 1591 | void HInliner::FixUpReturnReferenceType(ArtMethod* resolved_method, |
| 1592 | HInstruction* return_replacement) { |
| 1593 | if (return_replacement != nullptr) { |
| 1594 | if (return_replacement->GetType() == Primitive::kPrimNot) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 1595 | if (!return_replacement->GetReferenceTypeInfo().IsValid()) { |
| 1596 | // Make sure that we have a valid type for the return. We may get an invalid one when |
| 1597 | // we inline invokes with multiple branches and create a Phi for the result. |
| 1598 | // TODO: we could be more precise by merging the phi inputs but that requires |
| 1599 | // some functionality from the reference type propagation. |
| 1600 | DCHECK(return_replacement->IsPhi()); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1601 | mirror::Class* cls = resolved_method->GetReturnType(false /* resolve */); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1602 | return_replacement->SetReferenceTypeInfo(GetClassRTI(cls)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1603 | } |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 1604 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1605 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1606 | } |
| 1607 | |
| 1608 | } // namespace art |