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" |
| 24 | #include "driver/compiler_driver-inl.h" |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 25 | #include "driver/compiler_options.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 26 | #include "driver/dex_compilation_unit.h" |
| 27 | #include "instruction_simplifier.h" |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 28 | #include "intrinsics.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 29 | #include "mirror/class_loader.h" |
| 30 | #include "mirror/dex_cache.h" |
| 31 | #include "nodes.h" |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 32 | #include "optimizing_compiler.h" |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 33 | #include "reference_type_propagation.h" |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 34 | #include "register_allocator.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 35 | #include "sharpening.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 36 | #include "ssa_builder.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 37 | #include "ssa_phi_elimination.h" |
| 38 | #include "scoped_thread_state_change.h" |
| 39 | #include "thread.h" |
Calin Juravle | f1c6d9e | 2015-04-13 18:42:21 +0100 | [diff] [blame] | 40 | #include "dex/verified_method.h" |
| 41 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 42 | |
| 43 | namespace art { |
| 44 | |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 45 | static constexpr size_t kMaximumNumberOfHInstructions = 32; |
| 46 | |
| 47 | // Limit the number of dex registers that we accumulate while inlining |
| 48 | // to avoid creating large amount of nested environments. |
| 49 | static constexpr size_t kMaximumNumberOfCumulatedDexRegisters = 64; |
| 50 | |
| 51 | // Avoid inlining within a huge method due to memory pressure. |
| 52 | static constexpr size_t kMaximumCodeUnitSize = 4096; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 53 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 54 | void HInliner::Run() { |
Calin Juravle | 8f96df8 | 2015-07-29 15:58:48 +0100 | [diff] [blame] | 55 | const CompilerOptions& compiler_options = compiler_driver_->GetCompilerOptions(); |
| 56 | if ((compiler_options.GetInlineDepthLimit() == 0) |
| 57 | || (compiler_options.GetInlineMaxCodeUnits() == 0)) { |
| 58 | return; |
| 59 | } |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 60 | if (caller_compilation_unit_.GetCodeItem()->insns_size_in_code_units_ > kMaximumCodeUnitSize) { |
| 61 | return; |
| 62 | } |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 63 | if (graph_->IsDebuggable()) { |
| 64 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 65 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 66 | return; |
| 67 | } |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 68 | const ArenaVector<HBasicBlock*>& blocks = graph_->GetReversePostOrder(); |
| 69 | DCHECK(!blocks.empty()); |
| 70 | HBasicBlock* next_block = blocks[0]; |
| 71 | for (size_t i = 0; i < blocks.size(); ++i) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 72 | // Because we are changing the graph when inlining, we need to remember the next block. |
| 73 | // This avoids doing the inlining work again on the inlined blocks. |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 74 | if (blocks[i] != next_block) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 75 | continue; |
| 76 | } |
| 77 | HBasicBlock* block = next_block; |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 78 | next_block = (i == blocks.size() - 1) ? nullptr : blocks[i + 1]; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 79 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 80 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 81 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 82 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 83 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Nicolas Geoffray | 7904129 | 2015-03-26 10:05:54 +0000 | [diff] [blame] | 84 | // We use the original invoke type to ensure the resolution of the called method |
| 85 | // works properly. |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 86 | if (!TryInline(call)) { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 87 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 88 | std::string callee_name = |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 89 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 90 | bool should_inline = callee_name.find("$inline$") != std::string::npos; |
| 91 | CHECK(!should_inline) << "Could not inline " << callee_name; |
| 92 | } |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 93 | } else { |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 94 | if (kIsDebugBuild && IsCompilingWithCoreImage()) { |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 95 | std::string callee_name = |
| 96 | PrettyMethod(call->GetDexMethodIndex(), *outer_compilation_unit_.GetDexFile()); |
| 97 | bool must_not_inline = callee_name.find("$noinline$") != std::string::npos; |
| 98 | CHECK(!must_not_inline) << "Should not have inlined " << callee_name; |
| 99 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 100 | } |
| 101 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 102 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 103 | } |
| 104 | } |
| 105 | } |
| 106 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 107 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 108 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 109 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 114 | * the actual runtime target of an interface or virtual call. |
| 115 | * Return nullptr if the runtime target cannot be proven. |
| 116 | */ |
| 117 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 118 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 119 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 120 | // No need to lookup further, the resolved method will be the target. |
| 121 | return resolved_method; |
| 122 | } |
| 123 | |
| 124 | HInstruction* receiver = invoke->InputAt(0); |
| 125 | if (receiver->IsNullCheck()) { |
| 126 | // Due to multiple levels of inlining within the same pass, it might be that |
| 127 | // null check does not have the reference type of the actual receiver. |
| 128 | receiver = receiver->InputAt(0); |
| 129 | } |
| 130 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 131 | DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName(); |
| 132 | if (!info.IsExact()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 133 | // We currently only support inlining with known receivers. |
| 134 | // TODO: Remove this check, we should be able to inline final methods |
| 135 | // on unknown receivers. |
| 136 | return nullptr; |
| 137 | } else if (info.GetTypeHandle()->IsInterface()) { |
| 138 | // Statically knowing that the receiver has an interface type cannot |
| 139 | // help us find what is the target method. |
| 140 | return nullptr; |
| 141 | } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) { |
| 142 | // The method that we're trying to call is not in the receiver's class or super classes. |
| 143 | return nullptr; |
| 144 | } |
| 145 | |
| 146 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
| 147 | size_t pointer_size = cl->GetImagePointerSize(); |
| 148 | if (invoke->IsInvokeInterface()) { |
| 149 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface( |
| 150 | resolved_method, pointer_size); |
| 151 | } else { |
| 152 | DCHECK(invoke->IsInvokeVirtual()); |
| 153 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual( |
| 154 | resolved_method, pointer_size); |
| 155 | } |
| 156 | |
| 157 | if (resolved_method == nullptr) { |
| 158 | // The information we had on the receiver was not enough to find |
| 159 | // the target method. Since we check above the exact type of the receiver, |
| 160 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 161 | return nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 162 | } else if (!resolved_method->IsInvokable()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 163 | // The information we had on the receiver was not enough to find |
| 164 | // the target method. Since we check above the exact type of the receiver, |
| 165 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 166 | return nullptr; |
| 167 | } else if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 168 | // A final method has to be the target method. |
| 169 | return resolved_method; |
| 170 | } else if (info.IsExact()) { |
| 171 | // If we found a method and the receiver's concrete type is statically |
| 172 | // known, we know for sure the target. |
| 173 | return resolved_method; |
| 174 | } else { |
| 175 | // Even if we did find a method, the receiver type was not enough to |
| 176 | // statically find the runtime target. |
| 177 | return nullptr; |
| 178 | } |
| 179 | } |
| 180 | |
| 181 | static uint32_t FindMethodIndexIn(ArtMethod* method, |
| 182 | const DexFile& dex_file, |
| 183 | uint32_t referrer_index) |
Mathieu Chartier | 9044347 | 2015-07-16 20:32:27 -0700 | [diff] [blame] | 184 | SHARED_REQUIRES(Locks::mutator_lock_) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 185 | if (IsSameDexFile(*method->GetDexFile(), dex_file)) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 186 | return method->GetDexMethodIndex(); |
| 187 | } else { |
| 188 | return method->FindDexMethodIndexInOtherDexFile(dex_file, referrer_index); |
| 189 | } |
| 190 | } |
| 191 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 192 | static uint32_t FindClassIndexIn(mirror::Class* cls, const DexFile& dex_file) |
| 193 | SHARED_REQUIRES(Locks::mutator_lock_) { |
| 194 | if (cls->GetDexCache() == nullptr) { |
| 195 | DCHECK(cls->IsArrayClass()); |
| 196 | // TODO: find the class in `dex_file`. |
| 197 | return DexFile::kDexNoIndex; |
| 198 | } else if (cls->GetDexTypeIndex() == DexFile::kDexNoIndex16) { |
| 199 | // TODO: deal with proxy classes. |
| 200 | return DexFile::kDexNoIndex; |
| 201 | } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) { |
| 202 | // Update the dex cache to ensure the class is in. The generated code will |
| 203 | // consider it is. We make it safe by updating the dex cache, as other |
| 204 | // dex files might also load the class, and there is no guarantee the dex |
| 205 | // cache of the dex file of the class will be updated. |
| 206 | if (cls->GetDexCache()->GetResolvedType(cls->GetDexTypeIndex()) == nullptr) { |
| 207 | cls->GetDexCache()->SetResolvedType(cls->GetDexTypeIndex(), cls); |
| 208 | } |
| 209 | return cls->GetDexTypeIndex(); |
| 210 | } else { |
| 211 | // TODO: find the class in `dex_file`. |
| 212 | return DexFile::kDexNoIndex; |
| 213 | } |
| 214 | } |
| 215 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 216 | bool HInliner::TryInline(HInvoke* invoke_instruction) { |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 217 | if (invoke_instruction->IsInvokeUnresolved()) { |
| 218 | return false; // Don't bother to move further if we know the method is unresolved. |
| 219 | } |
| 220 | |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 221 | uint32_t method_index = invoke_instruction->GetDexMethodIndex(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 222 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 223 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 224 | VLOG(compiler) << "Try inlining " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 225 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 226 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 227 | // 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] | 228 | ArtMethod* resolved_method; |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 229 | ArtMethod* actual_method = nullptr; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 230 | if (invoke_instruction->IsInvokeStaticOrDirect()) { |
Nicolas Geoffray | e523423 | 2015-12-02 09:06:11 +0000 | [diff] [blame] | 231 | if (invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()) { |
| 232 | VLOG(compiler) << "Not inlining a String.<init> method"; |
| 233 | return false; |
| 234 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 235 | MethodReference ref = invoke_instruction->AsInvokeStaticOrDirect()->GetTargetMethod(); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 236 | mirror::DexCache* const dex_cache = (&caller_dex_file == ref.dex_file) |
| 237 | ? caller_compilation_unit_.GetDexCache().Get() |
| 238 | : class_linker->FindDexCache(soa.Self(), *ref.dex_file); |
| 239 | resolved_method = dex_cache->GetResolvedMethod( |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 240 | ref.dex_method_index, class_linker->GetImagePointerSize()); |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 241 | // actual_method == resolved_method for direct or static calls. |
| 242 | actual_method = resolved_method; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 243 | } else { |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 244 | resolved_method = caller_compilation_unit_.GetDexCache().Get()->GetResolvedMethod( |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 245 | method_index, class_linker->GetImagePointerSize()); |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 246 | if (resolved_method != nullptr) { |
| 247 | // Check if we can statically find the method. |
| 248 | actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
| 249 | } |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 250 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 251 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 252 | if (resolved_method == nullptr) { |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 253 | // TODO: Can this still happen? |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 254 | // 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] | 255 | VLOG(compiler) << "Method cannot be resolved " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 256 | return false; |
| 257 | } |
| 258 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 259 | if (actual_method != nullptr) { |
| 260 | return TryInline(invoke_instruction, actual_method); |
| 261 | } |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 262 | DCHECK(!invoke_instruction->IsInvokeStaticOrDirect()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 263 | |
| 264 | // Check if we can use an inline cache. |
| 265 | ArtMethod* caller = graph_->GetArtMethod(); |
| 266 | size_t pointer_size = class_linker->GetImagePointerSize(); |
| 267 | // Under JIT, we should always know the caller. |
| 268 | DCHECK(!Runtime::Current()->UseJit() || (caller != nullptr)); |
| 269 | if (caller != nullptr && caller->GetProfilingInfo(pointer_size) != nullptr) { |
| 270 | ProfilingInfo* profiling_info = caller->GetProfilingInfo(pointer_size); |
| 271 | const InlineCache& ic = *profiling_info->GetInlineCache(invoke_instruction->GetDexPc()); |
| 272 | if (ic.IsUnitialized()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 273 | VLOG(compiler) << "Interface or virtual call to " |
| 274 | << PrettyMethod(method_index, caller_dex_file) |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 275 | << " is not hit and not inlined"; |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 276 | return false; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 277 | } else if (ic.IsMonomorphic()) { |
| 278 | MaybeRecordStat(kMonomorphicCall); |
| 279 | return TryInlineMonomorphicCall(invoke_instruction, resolved_method, ic); |
| 280 | } else if (ic.IsPolymorphic()) { |
| 281 | MaybeRecordStat(kPolymorphicCall); |
| 282 | return TryInlinePolymorphicCall(invoke_instruction, resolved_method, ic); |
| 283 | } else { |
| 284 | DCHECK(ic.IsMegamorphic()); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 285 | VLOG(compiler) << "Interface or virtual call to " |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 286 | << PrettyMethod(method_index, caller_dex_file) |
| 287 | << " is megamorphic and not inlined"; |
| 288 | MaybeRecordStat(kMegamorphicCall); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 289 | return false; |
| 290 | } |
| 291 | } |
| 292 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 293 | VLOG(compiler) << "Interface or virtual call to " |
| 294 | << PrettyMethod(method_index, caller_dex_file) |
| 295 | << " could not be statically determined"; |
| 296 | return false; |
| 297 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 298 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 299 | bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction, |
| 300 | ArtMethod* resolved_method, |
| 301 | const InlineCache& ic) { |
| 302 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
| 303 | uint32_t class_index = FindClassIndexIn(ic.GetMonomorphicType(), caller_dex_file); |
| 304 | if (class_index == DexFile::kDexNoIndex) { |
| 305 | VLOG(compiler) << "Call to " << PrettyMethod(resolved_method) |
| 306 | << " from inline cache is not inlined because its class is not" |
| 307 | << " accessible to the caller"; |
| 308 | return false; |
| 309 | } |
| 310 | |
| 311 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 312 | size_t pointer_size = class_linker->GetImagePointerSize(); |
| 313 | if (invoke_instruction->IsInvokeInterface()) { |
| 314 | resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForInterface( |
| 315 | resolved_method, pointer_size); |
| 316 | } else { |
| 317 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
| 318 | resolved_method = ic.GetMonomorphicType()->FindVirtualMethodForVirtual( |
| 319 | resolved_method, pointer_size); |
| 320 | } |
| 321 | DCHECK(resolved_method != nullptr); |
| 322 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 323 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 324 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 325 | |
| 326 | if (!TryInline(invoke_instruction, resolved_method, /* do_rtp */ false)) { |
| 327 | return false; |
| 328 | } |
| 329 | |
| 330 | // We successfully inlined, now add a guard. |
| 331 | ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0); |
| 332 | DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_"); |
| 333 | HInstanceFieldGet* field_get = new (graph_->GetArena()) HInstanceFieldGet( |
| 334 | receiver, |
| 335 | Primitive::kPrimNot, |
| 336 | field->GetOffset(), |
| 337 | field->IsVolatile(), |
| 338 | field->GetDexFieldIndex(), |
| 339 | field->GetDeclaringClass()->GetDexClassDefIndex(), |
| 340 | *field->GetDexFile(), |
| 341 | handles_->NewHandle(field->GetDexCache()), |
| 342 | invoke_instruction->GetDexPc()); |
| 343 | |
| 344 | bool is_referrer = |
| 345 | (ic.GetMonomorphicType() == outermost_graph_->GetArtMethod()->GetDeclaringClass()); |
| 346 | HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(), |
| 347 | class_index, |
| 348 | caller_dex_file, |
| 349 | is_referrer, |
| 350 | invoke_instruction->GetDexPc(), |
| 351 | /* needs_access_check */ false, |
| 352 | /* is_in_dex_cache */ true); |
| 353 | |
| 354 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, field_get); |
| 355 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
| 356 | compare, invoke_instruction->GetDexPc()); |
| 357 | // TODO: Extend reference type propagation to understand the guard. |
| 358 | if (cursor != nullptr) { |
Nicolas Geoffray | 7c0f2e5 | 2016-01-18 15:24:53 +0000 | [diff] [blame] | 359 | bb_cursor->InsertInstructionAfter(field_get, cursor); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 360 | } else { |
Nicolas Geoffray | 7c0f2e5 | 2016-01-18 15:24:53 +0000 | [diff] [blame] | 361 | bb_cursor->InsertInstructionBefore(field_get, bb_cursor->GetFirstInstruction()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 362 | } |
Nicolas Geoffray | 7c0f2e5 | 2016-01-18 15:24:53 +0000 | [diff] [blame] | 363 | bb_cursor->InsertInstructionAfter(load_class, field_get); |
| 364 | bb_cursor->InsertInstructionAfter(compare, load_class); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 365 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 366 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 367 | |
| 368 | // Run type propagation to get the guard typed, and eventually propagate the |
| 369 | // type of the receiver. |
| 370 | ReferenceTypePropagation rtp_fixup(graph_, handles_); |
| 371 | rtp_fixup.Run(); |
| 372 | |
| 373 | MaybeRecordStat(kInlinedMonomorphicCall); |
| 374 | return true; |
| 375 | } |
| 376 | |
| 377 | bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction ATTRIBUTE_UNUSED, |
| 378 | ArtMethod* resolved_method, |
| 379 | const InlineCache& ic ATTRIBUTE_UNUSED) { |
| 380 | // TODO |
| 381 | VLOG(compiler) << "Unimplemented polymorphic inlining for " |
| 382 | << PrettyMethod(resolved_method); |
| 383 | return false; |
| 384 | } |
| 385 | |
| 386 | bool HInliner::TryInline(HInvoke* invoke_instruction, ArtMethod* method, bool do_rtp) { |
| 387 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 388 | |
| 389 | // Check whether we're allowed to inline. The outermost compilation unit is the relevant |
| 390 | // dex file here (though the transitivity of an inline chain would allow checking the calller). |
| 391 | if (!compiler_driver_->MayInline(method->GetDexFile(), |
| 392 | outer_compilation_unit_.GetDexFile())) { |
| 393 | VLOG(compiler) << "Won't inline " << PrettyMethod(method) << " in " |
| 394 | << outer_compilation_unit_.GetDexFile()->GetLocation() << " (" |
| 395 | << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from " |
| 396 | << method->GetDexFile()->GetLocation(); |
| 397 | return false; |
| 398 | } |
| 399 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 400 | uint32_t method_index = FindMethodIndexIn( |
| 401 | method, caller_dex_file, invoke_instruction->GetDexMethodIndex()); |
| 402 | if (method_index == DexFile::kDexNoIndex) { |
| 403 | VLOG(compiler) << "Call to " |
| 404 | << PrettyMethod(method) |
| 405 | << " cannot be inlined because unaccessible to caller"; |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile()); |
| 410 | |
| 411 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 412 | |
| 413 | if (code_item == nullptr) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 414 | VLOG(compiler) << "Method " << PrettyMethod(method) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 415 | << " is not inlined because it is native"; |
| 416 | return false; |
| 417 | } |
| 418 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 419 | size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits(); |
| 420 | if (code_item->insns_size_in_code_units_ > inline_max_code_units) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 421 | VLOG(compiler) << "Method " << PrettyMethod(method) |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 422 | << " is too big to inline: " |
| 423 | << code_item->insns_size_in_code_units_ |
| 424 | << " > " |
| 425 | << inline_max_code_units; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 426 | return false; |
| 427 | } |
| 428 | |
| 429 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 430 | VLOG(compiler) << "Method " << PrettyMethod(method) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 431 | << " is not inlined because of try block"; |
| 432 | return false; |
| 433 | } |
| 434 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 435 | if (!method->GetDeclaringClass()->IsVerified()) { |
| 436 | uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex(); |
Nicolas Geoffray | ccc6197 | 2015-10-01 14:34:20 +0100 | [diff] [blame] | 437 | if (!compiler_driver_->IsMethodVerifiedWithoutFailures( |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 438 | method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) { |
Nicolas Geoffray | ccc6197 | 2015-10-01 14:34:20 +0100 | [diff] [blame] | 439 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 440 | << " couldn't be verified, so it cannot be inlined"; |
| 441 | return false; |
| 442 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 445 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 446 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 447 | // Case of a static method that cannot be inlined because it implicitly |
| 448 | // requires an initialization check of its declaring class. |
| 449 | VLOG(compiler) << "Method " << PrettyMethod(method_index, caller_dex_file) |
| 450 | << " is not inlined because it is static and requires a clinit" |
| 451 | << " check that cannot be emitted due to Dex cache limitations"; |
| 452 | return false; |
| 453 | } |
| 454 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 455 | if (!TryBuildAndInline(method, invoke_instruction, same_dex_file, do_rtp)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 456 | return false; |
| 457 | } |
| 458 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 459 | VLOG(compiler) << "Successfully inlined " << PrettyMethod(method_index, caller_dex_file); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 460 | MaybeRecordStat(kInlinedInvoke); |
| 461 | return true; |
| 462 | } |
| 463 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 464 | bool HInliner::TryBuildAndInline(ArtMethod* resolved_method, |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 465 | HInvoke* invoke_instruction, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 466 | bool same_dex_file, |
| 467 | bool do_rtp) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 468 | ScopedObjectAccess soa(Thread::Current()); |
| 469 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 470 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 471 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 472 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 473 | Handle<mirror::DexCache> dex_cache(handles_->NewHandle(resolved_method->GetDexCache())); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 474 | DexCompilationUnit dex_compilation_unit( |
| 475 | nullptr, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 476 | caller_compilation_unit_.GetClassLoader(), |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 477 | class_linker, |
Nicolas Geoffray | 8dbf0cf | 2015-08-11 02:14:38 +0000 | [diff] [blame] | 478 | callee_dex_file, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 479 | code_item, |
| 480 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
Nicolas Geoffray | 8dbf0cf | 2015-08-11 02:14:38 +0000 | [diff] [blame] | 481 | method_index, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 482 | resolved_method->GetAccessFlags(), |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 483 | compiler_driver_->GetVerifiedMethod(&callee_dex_file, method_index), |
| 484 | dex_cache); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 485 | |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 486 | bool requires_ctor_barrier = false; |
| 487 | |
| 488 | if (dex_compilation_unit.IsConstructor()) { |
| 489 | // If it's a super invocation and we already generate a barrier there's no need |
| 490 | // to generate another one. |
| 491 | // We identify super calls by looking at the "this" pointer. If its value is the |
| 492 | // same as the local "this" pointer then we must have a super invocation. |
| 493 | bool is_super_invocation = invoke_instruction->InputAt(0)->IsParameterValue() |
| 494 | && invoke_instruction->InputAt(0)->AsParameterValue()->IsThis(); |
| 495 | if (is_super_invocation && graph_->ShouldGenerateConstructorBarrier()) { |
| 496 | requires_ctor_barrier = false; |
| 497 | } else { |
| 498 | Thread* self = Thread::Current(); |
| 499 | requires_ctor_barrier = compiler_driver_->RequiresConstructorBarrier(self, |
| 500 | dex_compilation_unit.GetDexFile(), |
| 501 | dex_compilation_unit.GetClassDefIndex()); |
| 502 | } |
| 503 | } |
| 504 | |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 505 | InvokeType invoke_type = invoke_instruction->GetOriginalInvokeType(); |
| 506 | if (invoke_type == kInterface) { |
| 507 | // We have statically resolved the dispatch. To please the class linker |
| 508 | // at runtime, we change this call as if it was a virtual call. |
| 509 | invoke_type = kVirtual; |
| 510 | } |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 511 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 512 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 513 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 514 | method_index, |
Calin Juravle | 3cd4fc8 | 2015-05-14 15:15:42 +0100 | [diff] [blame] | 515 | requires_ctor_barrier, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 516 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 517 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 518 | graph_->IsDebuggable(), |
| 519 | graph_->GetCurrentInstructionId()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 520 | callee_graph->SetArtMethod(resolved_method); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 521 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 522 | OptimizingCompilerStats inline_stats; |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 523 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 524 | &dex_compilation_unit, |
| 525 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 526 | resolved_method->GetDexFile(), |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 527 | compiler_driver_, |
Nicolas Geoffray | 9523a3e | 2015-07-17 11:51:28 +0000 | [diff] [blame] | 528 | &inline_stats, |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 529 | resolved_method->GetQuickenedInfo(), |
| 530 | dex_cache); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 531 | |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 532 | if (!builder.BuildGraph(*code_item)) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 533 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 534 | << " could not be built, so cannot be inlined"; |
| 535 | return false; |
| 536 | } |
| 537 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 538 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 539 | compiler_driver_->GetInstructionSet())) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 540 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 541 | << " cannot be inlined because of the register allocator"; |
| 542 | return false; |
| 543 | } |
| 544 | |
Nicolas Geoffray | 15bd228 | 2016-01-05 15:55:41 +0000 | [diff] [blame] | 545 | if (callee_graph->TryBuildingSsa(handles_) != kAnalysisSuccess) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 546 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 547 | << " could not be transformed to SSA"; |
| 548 | return false; |
| 549 | } |
| 550 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 551 | size_t parameter_index = 0; |
| 552 | for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions()); |
| 553 | !instructions.Done(); |
| 554 | instructions.Advance()) { |
| 555 | HInstruction* current = instructions.Current(); |
| 556 | if (current->IsParameterValue()) { |
| 557 | HInstruction* argument = invoke_instruction->InputAt(parameter_index++); |
| 558 | if (argument->IsNullConstant()) { |
| 559 | current->ReplaceWith(callee_graph->GetNullConstant()); |
| 560 | } else if (argument->IsIntConstant()) { |
| 561 | current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue())); |
| 562 | } else if (argument->IsLongConstant()) { |
| 563 | current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue())); |
| 564 | } else if (argument->IsFloatConstant()) { |
| 565 | current->ReplaceWith( |
| 566 | callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue())); |
| 567 | } else if (argument->IsDoubleConstant()) { |
| 568 | current->ReplaceWith( |
| 569 | callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue())); |
| 570 | } else if (argument->GetType() == Primitive::kPrimNot) { |
| 571 | current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo()); |
| 572 | current->AsParameterValue()->SetCanBeNull(argument->CanBeNull()); |
| 573 | } |
| 574 | } |
| 575 | } |
| 576 | |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 577 | // Run simple optimizations on the graph. |
Calin Juravle | 7a9c885 | 2015-04-21 14:07:50 +0100 | [diff] [blame] | 578 | HDeadCodeElimination dce(callee_graph, stats_); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 579 | HConstantFolding fold(callee_graph); |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 580 | HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_); |
Calin Juravle | acf735c | 2015-02-12 15:25:22 +0000 | [diff] [blame] | 581 | InstructionSimplifier simplify(callee_graph, stats_); |
Nicolas Geoffray | e34648d | 2015-11-23 08:59:07 +0000 | [diff] [blame] | 582 | IntrinsicsRecognizer intrinsics(callee_graph, compiler_driver_); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 583 | |
| 584 | HOptimization* optimizations[] = { |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 585 | &intrinsics, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 586 | &sharpening, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 587 | &simplify, |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 588 | &fold, |
Vladimir Marko | 9e23df5 | 2015-11-10 17:14:35 +0000 | [diff] [blame] | 589 | &dce, |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 590 | }; |
| 591 | |
| 592 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 593 | HOptimization* optimization = optimizations[i]; |
| 594 | optimization->Run(); |
| 595 | } |
| 596 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 597 | size_t number_of_instructions_budget = kMaximumNumberOfHInstructions; |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 598 | if (depth_ + 1 < compiler_driver_->GetCompilerOptions().GetInlineDepthLimit()) { |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 599 | HInliner inliner(callee_graph, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 600 | outermost_graph_, |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 601 | codegen_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 602 | outer_compilation_unit_, |
| 603 | dex_compilation_unit, |
| 604 | compiler_driver_, |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 605 | handles_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 606 | stats_, |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 607 | total_number_of_dex_registers_ + code_item->registers_size_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 608 | depth_ + 1); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 609 | inliner.Run(); |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 610 | number_of_instructions_budget += inliner.number_of_inlined_instructions_; |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 611 | } |
| 612 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 613 | // TODO: We should abort only if all predecessors throw. However, |
| 614 | // HGraph::InlineInto currently does not handle an exit block with |
| 615 | // a throw predecessor. |
| 616 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 617 | if (exit_block == nullptr) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 618 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 619 | << " could not be inlined because it has an infinite loop"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 620 | return false; |
| 621 | } |
| 622 | |
| 623 | bool has_throw_predecessor = false; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 624 | for (HBasicBlock* predecessor : exit_block->GetPredecessors()) { |
| 625 | if (predecessor->GetLastInstruction()->IsThrow()) { |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 626 | has_throw_predecessor = true; |
| 627 | break; |
| 628 | } |
| 629 | } |
| 630 | if (has_throw_predecessor) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 631 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 632 | << " could not be inlined because one branch always throws"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 633 | return false; |
| 634 | } |
| 635 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 636 | HReversePostOrderIterator it(*callee_graph); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 637 | 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] | 638 | size_t number_of_instructions = 0; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 639 | |
| 640 | bool can_inline_environment = |
| 641 | total_number_of_dex_registers_ < kMaximumNumberOfCumulatedDexRegisters; |
| 642 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 643 | for (; !it.Done(); it.Advance()) { |
| 644 | HBasicBlock* block = it.Current(); |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 645 | |
| 646 | if (block->IsLoopHeader() && block->GetLoopInformation()->IsIrreducible()) { |
| 647 | // Don't inline methods with irreducible loops, they could prevent some |
| 648 | // optimizations to run. |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 649 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 788f2f0 | 2016-01-22 12:41:38 +0000 | [diff] [blame] | 650 | << " could not be inlined because it contains an irreducible loop"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 651 | return false; |
| 652 | } |
| 653 | |
| 654 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 655 | !instr_it.Done(); |
| 656 | instr_it.Advance()) { |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 657 | if (number_of_instructions++ == number_of_instructions_budget) { |
| 658 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 659 | << " is not inlined because its caller has reached" |
| 660 | << " its instruction budget limit."; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 661 | return false; |
| 662 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 663 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 664 | if (!can_inline_environment && current->NeedsEnvironment()) { |
| 665 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
| 666 | << " is not inlined because its caller has reached" |
| 667 | << " its environment budget limit."; |
| 668 | return false; |
| 669 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 670 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 671 | if (current->IsInvokeInterface()) { |
| 672 | // Disable inlining of interface calls. The cost in case of entering the |
| 673 | // resolution conflict is currently too high. |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 674 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 675 | << " could not be inlined because it has an interface call."; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 676 | return false; |
| 677 | } |
| 678 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 679 | if (!same_dex_file && current->NeedsEnvironment()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 680 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 681 | << " could not be inlined because " << current->DebugName() |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 682 | << " needs an environment and is in a different dex file"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 683 | return false; |
| 684 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 685 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 686 | if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) { |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 687 | VLOG(compiler) << "Method " << PrettyMethod(method_index, callee_dex_file) |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 688 | << " could not be inlined because " << current->DebugName() |
| 689 | << " it is in a different dex file and requires access to the dex cache"; |
| 690 | return false; |
| 691 | } |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 692 | |
| 693 | if (current->IsNewInstance() && |
| 694 | (current->AsNewInstance()->GetEntrypoint() == kQuickAllocObjectWithAccessCheck)) { |
| 695 | // Allocation entrypoint does not handle inlined frames. |
| 696 | return false; |
| 697 | } |
| 698 | |
| 699 | if (current->IsNewArray() && |
| 700 | (current->AsNewArray()->GetEntrypoint() == kQuickAllocArrayWithAccessCheck)) { |
| 701 | // Allocation entrypoint does not handle inlined frames. |
| 702 | return false; |
| 703 | } |
| 704 | |
| 705 | if (current->IsUnresolvedStaticFieldGet() || |
| 706 | current->IsUnresolvedInstanceFieldGet() || |
| 707 | current->IsUnresolvedStaticFieldSet() || |
| 708 | current->IsUnresolvedInstanceFieldSet()) { |
| 709 | // Entrypoint for unresolved fields does not handle inlined frames. |
| 710 | return false; |
| 711 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 712 | } |
| 713 | } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 714 | number_of_inlined_instructions_ += number_of_instructions; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 715 | |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 716 | HInstruction* return_replacement = callee_graph->InlineInto(graph_, invoke_instruction); |
Calin Juravle | 214bbcd | 2015-10-20 14:54:07 +0100 | [diff] [blame] | 717 | if (return_replacement != nullptr) { |
| 718 | DCHECK_EQ(graph_, return_replacement->GetBlock()->GetGraph()); |
| 719 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 720 | |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 721 | // 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] | 722 | if (return_replacement != nullptr) { |
| 723 | if (return_replacement->GetType() == Primitive::kPrimNot) { |
| 724 | if (!return_replacement->GetReferenceTypeInfo().IsValid()) { |
| 725 | // Make sure that we have a valid type for the return. We may get an invalid one when |
| 726 | // we inline invokes with multiple branches and create a Phi for the result. |
| 727 | // TODO: we could be more precise by merging the phi inputs but that requires |
| 728 | // some functionality from the reference type propagation. |
| 729 | DCHECK(return_replacement->IsPhi()); |
| 730 | size_t pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
| 731 | ReferenceTypeInfo::TypeHandle return_handle = |
| 732 | handles_->NewHandle(resolved_method->GetReturnType(true /* resolve */, pointer_size)); |
| 733 | return_replacement->SetReferenceTypeInfo(ReferenceTypeInfo::Create( |
| 734 | return_handle, return_handle->CannotBeAssignedFromOtherTypes() /* is_exact */)); |
| 735 | } |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 736 | |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 737 | if (do_rtp) { |
| 738 | // If the return type is a refinement of the declared type run the type propagation again. |
| 739 | ReferenceTypeInfo return_rti = return_replacement->GetReferenceTypeInfo(); |
| 740 | ReferenceTypeInfo invoke_rti = invoke_instruction->GetReferenceTypeInfo(); |
| 741 | if (invoke_rti.IsStrictSupertypeOf(return_rti) |
| 742 | || (return_rti.IsExact() && !invoke_rti.IsExact()) |
| 743 | || !return_replacement->CanBeNull()) { |
| 744 | ReferenceTypePropagation(graph_, handles_).Run(); |
| 745 | } |
| 746 | } |
| 747 | } else if (return_replacement->IsInstanceOf()) { |
| 748 | if (do_rtp) { |
| 749 | // Inlining InstanceOf into an If may put a tighter bound on reference types. |
| 750 | ReferenceTypePropagation(graph_, handles_).Run(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 751 | } |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 752 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 753 | } |
Nicolas Geoffray | 7c5367b | 2014-12-17 10:13:46 +0000 | [diff] [blame] | 754 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 755 | return true; |
| 756 | } |
| 757 | |
| 758 | } // namespace art |