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