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