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