Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "inliner.h" |
| 18 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 19 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 20 | #include "base/enums.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 21 | #include "builder.h" |
| 22 | #include "class_linker.h" |
| 23 | #include "constant_folding.h" |
| 24 | #include "dead_code_elimination.h" |
Andreas Gampe | b95c74b | 2017-04-20 19:43:21 -0700 | [diff] [blame] | 25 | #include "dex/inline_method_analyser.h" |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 26 | #include "dex/verified_method.h" |
| 27 | #include "dex/verification_results.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 28 | #include "driver/compiler_driver-inl.h" |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 29 | #include "driver/compiler_options.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 30 | #include "driver/dex_compilation_unit.h" |
| 31 | #include "instruction_simplifier.h" |
Scott Wakeling | d60a1af | 2015-07-22 14:32:44 +0100 | [diff] [blame] | 32 | #include "intrinsics.h" |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 33 | #include "jit/jit.h" |
| 34 | #include "jit/jit_code_cache.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 35 | #include "mirror/class_loader.h" |
| 36 | #include "mirror/dex_cache.h" |
| 37 | #include "nodes.h" |
Nicolas Geoffray | 335005e | 2015-06-25 10:01:47 +0100 | [diff] [blame] | 38 | #include "optimizing_compiler.h" |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 39 | #include "reference_type_propagation.h" |
Matthew Gharrity | e928885 | 2016-07-14 14:08:16 -0700 | [diff] [blame] | 40 | #include "register_allocator_linear_scan.h" |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 41 | #include "sharpening.h" |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 42 | #include "ssa_builder.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 43 | #include "ssa_phi_elimination.h" |
Mathieu Chartier | 0795f23 | 2016-09-27 18:43:30 -0700 | [diff] [blame] | 44 | #include "scoped_thread_state_change-inl.h" |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 45 | #include "thread.h" |
| 46 | |
| 47 | namespace art { |
| 48 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 49 | // Instruction limit to control memory. |
| 50 | static constexpr size_t kMaximumNumberOfTotalInstructions = 1024; |
| 51 | |
| 52 | // Maximum number of instructions for considering a method small, |
| 53 | // which we will always try to inline if the other non-instruction limits |
| 54 | // are not reached. |
| 55 | static constexpr size_t kMaximumNumberOfInstructionsForSmallMethod = 3; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 56 | |
| 57 | // Limit the number of dex registers that we accumulate while inlining |
| 58 | // to avoid creating large amount of nested environments. |
| 59 | static constexpr size_t kMaximumNumberOfCumulatedDexRegisters = 64; |
| 60 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 61 | // Limit recursive call inlining, which do not benefit from too |
| 62 | // much inlining compared to code locality. |
| 63 | static constexpr size_t kMaximumNumberOfRecursiveCalls = 4; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 64 | |
Calin Juravle | e2492d4 | 2017-03-20 11:42:13 -0700 | [diff] [blame] | 65 | // Controls the use of inline caches in AOT mode. |
Calin Juravle | 8af7089 | 2017-03-28 15:31:44 -0700 | [diff] [blame] | 66 | static constexpr bool kUseAOTInlineCaches = true; |
Calin Juravle | e2492d4 | 2017-03-20 11:42:13 -0700 | [diff] [blame] | 67 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 68 | // We check for line numbers to make sure the DepthString implementation |
| 69 | // aligns the output nicely. |
| 70 | #define LOG_INTERNAL(msg) \ |
| 71 | static_assert(__LINE__ > 10, "Unhandled line number"); \ |
| 72 | static_assert(__LINE__ < 10000, "Unhandled line number"); \ |
| 73 | VLOG(compiler) << DepthString(__LINE__) << msg |
| 74 | |
| 75 | #define LOG_TRY() LOG_INTERNAL("Try inlinining call: ") |
| 76 | #define LOG_NOTE() LOG_INTERNAL("Note: ") |
| 77 | #define LOG_SUCCESS() LOG_INTERNAL("Success: ") |
| 78 | #define LOG_FAIL(stat) MaybeRecordStat(stat); LOG_INTERNAL("Fail: ") |
| 79 | #define LOG_FAIL_NO_STAT() LOG_INTERNAL("Fail: ") |
| 80 | |
| 81 | std::string HInliner::DepthString(int line) const { |
| 82 | std::string value; |
| 83 | // Indent according to the inlining depth. |
| 84 | size_t count = depth_; |
| 85 | // Line numbers get printed in the log, so add a space if the log's line is less |
| 86 | // than 1000, and two if less than 100. 10 cannot be reached as it's the copyright. |
| 87 | if (!kIsTargetBuild) { |
| 88 | if (line < 100) { |
| 89 | value += " "; |
| 90 | } |
| 91 | if (line < 1000) { |
| 92 | value += " "; |
| 93 | } |
| 94 | // Safeguard if this file reaches more than 10000 lines. |
| 95 | DCHECK_LT(line, 10000); |
| 96 | } |
| 97 | for (size_t i = 0; i < count; ++i) { |
| 98 | value += " "; |
| 99 | } |
| 100 | return value; |
| 101 | } |
| 102 | |
| 103 | static size_t CountNumberOfInstructions(HGraph* graph) { |
| 104 | size_t number_of_instructions = 0; |
| 105 | for (HBasicBlock* block : graph->GetReversePostOrderSkipEntryBlock()) { |
| 106 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 107 | !instr_it.Done(); |
| 108 | instr_it.Advance()) { |
| 109 | ++number_of_instructions; |
| 110 | } |
| 111 | } |
| 112 | return number_of_instructions; |
| 113 | } |
| 114 | |
| 115 | void HInliner::UpdateInliningBudget() { |
| 116 | if (total_number_of_instructions_ >= kMaximumNumberOfTotalInstructions) { |
| 117 | // Always try to inline small methods. |
| 118 | inlining_budget_ = kMaximumNumberOfInstructionsForSmallMethod; |
| 119 | } else { |
| 120 | inlining_budget_ = std::max( |
| 121 | kMaximumNumberOfInstructionsForSmallMethod, |
| 122 | kMaximumNumberOfTotalInstructions - total_number_of_instructions_); |
| 123 | } |
| 124 | } |
| 125 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 126 | void HInliner::Run() { |
Nicolas Geoffray | e50b8d2 | 2015-03-13 08:57:42 +0000 | [diff] [blame] | 127 | if (graph_->IsDebuggable()) { |
| 128 | // For simplicity, we currently never inline when the graph is debuggable. This avoids |
| 129 | // doing some logic in the runtime to discover if a method could have been inlined. |
| 130 | return; |
| 131 | } |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 132 | |
| 133 | // Initialize the number of instructions for the method being compiled. Recursive calls |
| 134 | // to HInliner::Run have already updated the instruction count. |
| 135 | if (outermost_graph_ == graph_) { |
| 136 | total_number_of_instructions_ = CountNumberOfInstructions(graph_); |
| 137 | } |
| 138 | |
| 139 | UpdateInliningBudget(); |
| 140 | DCHECK_NE(total_number_of_instructions_, 0u); |
| 141 | DCHECK_NE(inlining_budget_, 0u); |
| 142 | |
Roland Levillain | 6c3af16 | 2017-04-27 11:18:56 +0100 | [diff] [blame] | 143 | // If we're compiling with a core image (which is only used for |
| 144 | // test purposes), honor inlining directives in method names: |
| 145 | // - if a method's name contains the substring "$inline$", ensure |
| 146 | // that this method is actually inlined; |
| 147 | // - if a method's name contains the substring "$noinline$", do not |
| 148 | // inline that method. |
| 149 | const bool honor_inlining_directives = IsCompilingWithCoreImage(); |
| 150 | |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 151 | // Keep a copy of all blocks when starting the visit. |
| 152 | ArenaVector<HBasicBlock*> blocks = graph_->GetReversePostOrder(); |
Vladimir Marko | fa6b93c | 2015-09-15 10:15:55 +0100 | [diff] [blame] | 153 | DCHECK(!blocks.empty()); |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 154 | // Because we are changing the graph when inlining, |
| 155 | // we just iterate over the blocks of the outer method. |
| 156 | // This avoids doing the inlining work again on the inlined blocks. |
| 157 | for (HBasicBlock* block : blocks) { |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 158 | for (HInstruction* instruction = block->GetFirstInstruction(); instruction != nullptr;) { |
| 159 | HInstruction* next = instruction->GetNext(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 160 | HInvoke* call = instruction->AsInvoke(); |
Razvan A Lupusoru | 3e90a96 | 2015-03-27 13:44:44 -0700 | [diff] [blame] | 161 | // As long as the call is not intrinsified, it is worth trying to inline. |
| 162 | if (call != nullptr && call->GetIntrinsic() == Intrinsics::kNone) { |
Roland Levillain | 6c3af16 | 2017-04-27 11:18:56 +0100 | [diff] [blame] | 163 | if (honor_inlining_directives) { |
Nicolas Geoffray | b703d18 | 2017-02-14 18:05:28 +0000 | [diff] [blame] | 164 | // Debugging case: directives in method names control or assert on inlining. |
| 165 | std::string callee_name = outer_compilation_unit_.GetDexFile()->PrettyMethod( |
| 166 | call->GetDexMethodIndex(), /* with_signature */ false); |
| 167 | // Tests prevent inlining by having $noinline$ in their method names. |
| 168 | if (callee_name.find("$noinline$") == std::string::npos) { |
| 169 | if (!TryInline(call)) { |
| 170 | bool should_have_inlined = (callee_name.find("$inline$") != std::string::npos); |
| 171 | CHECK(!should_have_inlined) << "Could not inline " << callee_name; |
| 172 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 173 | } |
Guillaume "Vermeille" Sanchez | e918d38 | 2015-06-03 15:32:41 +0100 | [diff] [blame] | 174 | } else { |
Nicolas Geoffray | b703d18 | 2017-02-14 18:05:28 +0000 | [diff] [blame] | 175 | // Normal case: try to inline. |
| 176 | TryInline(call); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 177 | } |
| 178 | } |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 179 | instruction = next; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 180 | } |
| 181 | } |
| 182 | } |
| 183 | |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 184 | static bool IsMethodOrDeclaringClassFinal(ArtMethod* method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 185 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 186 | return method->IsFinal() || method->GetDeclaringClass()->IsFinal(); |
| 187 | } |
| 188 | |
| 189 | /** |
| 190 | * Given the `resolved_method` looked up in the dex cache, try to find |
| 191 | * the actual runtime target of an interface or virtual call. |
| 192 | * Return nullptr if the runtime target cannot be proven. |
| 193 | */ |
| 194 | static ArtMethod* FindVirtualOrInterfaceTarget(HInvoke* invoke, ArtMethod* resolved_method) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 195 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 196 | if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 197 | // No need to lookup further, the resolved method will be the target. |
| 198 | return resolved_method; |
| 199 | } |
| 200 | |
| 201 | HInstruction* receiver = invoke->InputAt(0); |
| 202 | if (receiver->IsNullCheck()) { |
| 203 | // Due to multiple levels of inlining within the same pass, it might be that |
| 204 | // null check does not have the reference type of the actual receiver. |
| 205 | receiver = receiver->InputAt(0); |
| 206 | } |
| 207 | ReferenceTypeInfo info = receiver->GetReferenceTypeInfo(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 208 | DCHECK(info.IsValid()) << "Invalid RTI for " << receiver->DebugName(); |
| 209 | if (!info.IsExact()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 210 | // We currently only support inlining with known receivers. |
| 211 | // TODO: Remove this check, we should be able to inline final methods |
| 212 | // on unknown receivers. |
| 213 | return nullptr; |
| 214 | } else if (info.GetTypeHandle()->IsInterface()) { |
| 215 | // Statically knowing that the receiver has an interface type cannot |
| 216 | // help us find what is the target method. |
| 217 | return nullptr; |
| 218 | } else if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(info.GetTypeHandle().Get())) { |
| 219 | // The method that we're trying to call is not in the receiver's class or super classes. |
| 220 | return nullptr; |
Nicolas Geoffray | ab5327d | 2016-03-18 11:36:20 +0000 | [diff] [blame] | 221 | } else if (info.GetTypeHandle()->IsErroneous()) { |
| 222 | // If the type is erroneous, do not go further, as we are going to query the vtable or |
| 223 | // imt table, that we can only safely do on non-erroneous classes. |
| 224 | return nullptr; |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 225 | } |
| 226 | |
| 227 | ClassLinker* cl = Runtime::Current()->GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 228 | PointerSize pointer_size = cl->GetImagePointerSize(); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 229 | if (invoke->IsInvokeInterface()) { |
| 230 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForInterface( |
| 231 | resolved_method, pointer_size); |
| 232 | } else { |
| 233 | DCHECK(invoke->IsInvokeVirtual()); |
| 234 | resolved_method = info.GetTypeHandle()->FindVirtualMethodForVirtual( |
| 235 | resolved_method, pointer_size); |
| 236 | } |
| 237 | |
| 238 | if (resolved_method == nullptr) { |
| 239 | // The information we had on the receiver was not enough to find |
| 240 | // the target method. Since we check above the exact type of the receiver, |
| 241 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 242 | return nullptr; |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 243 | } else if (!resolved_method->IsInvokable()) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 244 | // The information we had on the receiver was not enough to find |
| 245 | // the target method. Since we check above the exact type of the receiver, |
| 246 | // the only reason this can happen is an IncompatibleClassChangeError. |
| 247 | return nullptr; |
| 248 | } else if (IsMethodOrDeclaringClassFinal(resolved_method)) { |
| 249 | // A final method has to be the target method. |
| 250 | return resolved_method; |
| 251 | } else if (info.IsExact()) { |
| 252 | // If we found a method and the receiver's concrete type is statically |
| 253 | // known, we know for sure the target. |
| 254 | return resolved_method; |
| 255 | } else { |
| 256 | // Even if we did find a method, the receiver type was not enough to |
| 257 | // statically find the runtime target. |
| 258 | return nullptr; |
| 259 | } |
| 260 | } |
| 261 | |
| 262 | static uint32_t FindMethodIndexIn(ArtMethod* method, |
| 263 | const DexFile& dex_file, |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 264 | uint32_t name_and_signature_index) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 265 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 266 | if (IsSameDexFile(*method->GetDexFile(), dex_file)) { |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 267 | return method->GetDexMethodIndex(); |
| 268 | } else { |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 269 | return method->FindDexMethodIndexInOtherDexFile(dex_file, name_and_signature_index); |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 270 | } |
| 271 | } |
| 272 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 273 | static dex::TypeIndex FindClassIndexIn(mirror::Class* cls, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 274 | const DexCompilationUnit& compilation_unit) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 275 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 276 | const DexFile& dex_file = *compilation_unit.GetDexFile(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 277 | dex::TypeIndex index; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 278 | if (cls->GetDexCache() == nullptr) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 279 | DCHECK(cls->IsArrayClass()) << cls->PrettyClass(); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 280 | index = cls->FindTypeIndexInOtherDexFile(dex_file); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 281 | } else if (!cls->GetDexTypeIndex().IsValid()) { |
David Sehr | 709b070 | 2016-10-13 09:12:37 -0700 | [diff] [blame] | 282 | DCHECK(cls->IsProxyClass()) << cls->PrettyClass(); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 283 | // TODO: deal with proxy classes. |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 284 | } else if (IsSameDexFile(cls->GetDexFile(), dex_file)) { |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 285 | DCHECK_EQ(cls->GetDexCache(), compilation_unit.GetDexCache().Get()); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 286 | index = cls->GetDexTypeIndex(); |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 287 | } else { |
| 288 | index = cls->FindTypeIndexInOtherDexFile(dex_file); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 289 | // We cannot guarantee the entry will resolve to the same class, |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 290 | // as there may be different class loaders. So only return the index if it's |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 291 | // the right class already resolved with the class loader. |
| 292 | if (index.IsValid()) { |
| 293 | ObjPtr<mirror::Class> resolved = ClassLinker::LookupResolvedType( |
| 294 | index, compilation_unit.GetDexCache().Get(), compilation_unit.GetClassLoader().Get()); |
| 295 | if (resolved != cls) { |
| 296 | index = dex::TypeIndex::Invalid(); |
| 297 | } |
Nicolas Geoffray | 491617a | 2016-07-19 17:06:23 +0100 | [diff] [blame] | 298 | } |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 299 | } |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 300 | |
| 301 | return index; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 302 | } |
| 303 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 304 | class ScopedProfilingInfoInlineUse { |
| 305 | public: |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 306 | explicit ScopedProfilingInfoInlineUse(ArtMethod* method, Thread* self) |
| 307 | : method_(method), |
| 308 | self_(self), |
| 309 | // Fetch the profiling info ahead of using it. If it's null when fetching, |
| 310 | // we should not call JitCodeCache::DoneInlining. |
| 311 | profiling_info_( |
| 312 | Runtime::Current()->GetJit()->GetCodeCache()->NotifyCompilerUse(method, self)) { |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | ~ScopedProfilingInfoInlineUse() { |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 316 | if (profiling_info_ != nullptr) { |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 317 | PointerSize pointer_size = Runtime::Current()->GetClassLinker()->GetImagePointerSize(); |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 318 | DCHECK_EQ(profiling_info_, method_->GetProfilingInfo(pointer_size)); |
| 319 | Runtime::Current()->GetJit()->GetCodeCache()->DoneCompilerUse(method_, self_); |
| 320 | } |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 321 | } |
| 322 | |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 323 | ProfilingInfo* GetProfilingInfo() const { return profiling_info_; } |
| 324 | |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 325 | private: |
| 326 | ArtMethod* const method_; |
Nicolas Geoffray | 07e3ca9 | 2016-03-11 09:57:57 +0000 | [diff] [blame] | 327 | Thread* const self_; |
| 328 | ProfilingInfo* const profiling_info_; |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 329 | }; |
| 330 | |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 331 | HInliner::InlineCacheType HInliner::GetInlineCacheType( |
| 332 | const Handle<mirror::ObjectArray<mirror::Class>>& classes) |
| 333 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 334 | uint8_t number_of_types = 0; |
| 335 | for (; number_of_types < InlineCache::kIndividualCacheSize; ++number_of_types) { |
| 336 | if (classes->Get(number_of_types) == nullptr) { |
| 337 | break; |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 338 | } |
| 339 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 340 | |
| 341 | if (number_of_types == 0) { |
| 342 | return kInlineCacheUninitialized; |
| 343 | } else if (number_of_types == 1) { |
| 344 | return kInlineCacheMonomorphic; |
| 345 | } else if (number_of_types == InlineCache::kIndividualCacheSize) { |
| 346 | return kInlineCacheMegamorphic; |
| 347 | } else { |
| 348 | return kInlineCachePolymorphic; |
| 349 | } |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | static mirror::Class* GetMonomorphicType(Handle<mirror::ObjectArray<mirror::Class>> classes) |
| 353 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 354 | DCHECK(classes->Get(0) != nullptr); |
| 355 | return classes->Get(0); |
| 356 | } |
| 357 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 358 | ArtMethod* HInliner::TryCHADevirtualization(ArtMethod* resolved_method) { |
| 359 | if (!resolved_method->HasSingleImplementation()) { |
| 360 | return nullptr; |
| 361 | } |
| 362 | if (Runtime::Current()->IsAotCompiler()) { |
| 363 | // No CHA-based devirtulization for AOT compiler (yet). |
| 364 | return nullptr; |
| 365 | } |
| 366 | if (outermost_graph_->IsCompilingOsr()) { |
| 367 | // We do not support HDeoptimize in OSR methods. |
| 368 | return nullptr; |
| 369 | } |
Mingyao Yang | e8fcd01 | 2017-01-20 10:43:30 -0800 | [diff] [blame] | 370 | PointerSize pointer_size = caller_compilation_unit_.GetClassLinker()->GetImagePointerSize(); |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 371 | ArtMethod* single_impl = resolved_method->GetSingleImplementation(pointer_size); |
| 372 | if (single_impl == nullptr) { |
| 373 | return nullptr; |
| 374 | } |
| 375 | if (single_impl->IsProxyMethod()) { |
| 376 | // Proxy method is a generic invoker that's not worth |
| 377 | // devirtualizing/inlining. It also causes issues when the proxy |
| 378 | // method is in another dex file if we try to rewrite invoke-interface to |
| 379 | // invoke-virtual because a proxy method doesn't have a real dex file. |
| 380 | return nullptr; |
| 381 | } |
Nicolas Geoffray | 8e33e84 | 2017-04-03 16:55:16 +0100 | [diff] [blame] | 382 | if (!single_impl->GetDeclaringClass()->IsResolved()) { |
| 383 | // There's a race with the class loading, which updates the CHA info |
| 384 | // before setting the class to resolved. So we just bail for this |
| 385 | // rare occurence. |
| 386 | return nullptr; |
| 387 | } |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 388 | return single_impl; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 389 | } |
| 390 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 391 | bool HInliner::TryInline(HInvoke* invoke_instruction) { |
Orion Hodson | ac14139 | 2017-01-13 11:53:47 +0000 | [diff] [blame] | 392 | if (invoke_instruction->IsInvokeUnresolved() || |
| 393 | invoke_instruction->IsInvokePolymorphic()) { |
| 394 | return false; // Don't bother to move further if we know the method is unresolved or an |
| 395 | // invoke-polymorphic. |
Calin Juravle | 175dc73 | 2015-08-25 15:42:32 +0100 | [diff] [blame] | 396 | } |
| 397 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 398 | ScopedObjectAccess soa(Thread::Current()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 399 | uint32_t method_index = invoke_instruction->GetDexMethodIndex(); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 400 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 401 | LOG_TRY() << caller_dex_file.PrettyMethod(method_index); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 402 | |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 403 | ArtMethod* resolved_method = invoke_instruction->GetResolvedMethod(); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 404 | if (resolved_method == nullptr) { |
| 405 | DCHECK(invoke_instruction->IsInvokeStaticOrDirect()); |
| 406 | DCHECK(invoke_instruction->AsInvokeStaticOrDirect()->IsStringInit()); |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 407 | LOG_FAIL_NO_STAT() << "Not inlining a String.<init> method"; |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 408 | return false; |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 409 | } |
| 410 | ArtMethod* actual_method = nullptr; |
| 411 | |
| 412 | if (invoke_instruction->IsInvokeStaticOrDirect()) { |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 413 | actual_method = resolved_method; |
Vladimir Marko | 5815501 | 2015-08-19 12:49:41 +0000 | [diff] [blame] | 414 | } else { |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 415 | // Check if we can statically find the method. |
| 416 | actual_method = FindVirtualOrInterfaceTarget(invoke_instruction, resolved_method); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 417 | } |
| 418 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 419 | bool cha_devirtualize = false; |
| 420 | if (actual_method == nullptr) { |
| 421 | ArtMethod* method = TryCHADevirtualization(resolved_method); |
| 422 | if (method != nullptr) { |
| 423 | cha_devirtualize = true; |
| 424 | actual_method = method; |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 425 | LOG_NOTE() << "Try CHA-based inlining of " << actual_method->PrettyMethod(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 426 | } |
| 427 | } |
| 428 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 429 | if (actual_method != nullptr) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 430 | bool result = TryInlineAndReplace(invoke_instruction, |
| 431 | actual_method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 432 | ReferenceTypeInfo::CreateInvalid(), |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 433 | /* do_rtp */ true, |
| 434 | cha_devirtualize); |
Calin Juravle | 6915898 | 2016-03-16 11:53:41 +0000 | [diff] [blame] | 435 | if (result && !invoke_instruction->IsInvokeStaticOrDirect()) { |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 436 | if (cha_devirtualize) { |
| 437 | // Add dependency due to devirtulization. We've assumed resolved_method |
| 438 | // has single implementation. |
| 439 | outermost_graph_->AddCHASingleImplementationDependency(resolved_method); |
| 440 | MaybeRecordStat(kCHAInline); |
| 441 | } else { |
| 442 | MaybeRecordStat(kInlinedInvokeVirtualOrInterface); |
| 443 | } |
Calin Juravle | 6915898 | 2016-03-16 11:53:41 +0000 | [diff] [blame] | 444 | } |
| 445 | return result; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 446 | } |
Andreas Gampe | fd2140f | 2015-12-23 16:30:44 -0800 | [diff] [blame] | 447 | DCHECK(!invoke_instruction->IsInvokeStaticOrDirect()); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 448 | |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 449 | // Try using inline caches. |
| 450 | return TryInlineFromInlineCache(caller_dex_file, invoke_instruction, resolved_method); |
| 451 | } |
| 452 | |
| 453 | static Handle<mirror::ObjectArray<mirror::Class>> AllocateInlineCacheHolder( |
| 454 | const DexCompilationUnit& compilation_unit, |
| 455 | StackHandleScope<1>* hs) |
| 456 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 457 | Thread* self = Thread::Current(); |
| 458 | ClassLinker* class_linker = compilation_unit.GetClassLinker(); |
| 459 | Handle<mirror::ObjectArray<mirror::Class>> inline_cache = hs->NewHandle( |
| 460 | mirror::ObjectArray<mirror::Class>::Alloc( |
| 461 | self, |
| 462 | class_linker->GetClassRoot(ClassLinker::kClassArrayClass), |
| 463 | InlineCache::kIndividualCacheSize)); |
| 464 | if (inline_cache == nullptr) { |
| 465 | // We got an OOME. Just clear the exception, and don't inline. |
| 466 | DCHECK(self->IsExceptionPending()); |
| 467 | self->ClearException(); |
| 468 | VLOG(compiler) << "Out of memory in the compiler when trying to inline"; |
| 469 | } |
| 470 | return inline_cache; |
| 471 | } |
| 472 | |
Calin Juravle | af44e6c | 2017-05-23 14:24:55 -0700 | [diff] [blame] | 473 | bool HInliner::UseOnlyPolymorphicInliningWithNoDeopt() { |
| 474 | // If we are compiling AOT or OSR, pretend the call using inline caches is polymorphic and |
| 475 | // do not generate a deopt. |
| 476 | // |
| 477 | // For AOT: |
| 478 | // Generating a deopt does not ensure that we will actually capture the new types; |
| 479 | // and the danger is that we could be stuck in a loop with "forever" deoptimizations. |
| 480 | // Take for example the following scenario: |
| 481 | // - we capture the inline cache in one run |
| 482 | // - the next run, we deoptimize because we miss a type check, but the method |
| 483 | // never becomes hot again |
| 484 | // In this case, the inline cache will not be updated in the profile and the AOT code |
| 485 | // will keep deoptimizing. |
| 486 | // Another scenario is if we use profile compilation for a process which is not allowed |
| 487 | // to JIT (e.g. system server). If we deoptimize we will run interpreted code for the |
| 488 | // rest of the lifetime. |
| 489 | // TODO(calin): |
| 490 | // This is a compromise because we will most likely never update the inline cache |
| 491 | // in the profile (unless there's another reason to deopt). So we might be stuck with |
| 492 | // a sub-optimal inline cache. |
| 493 | // We could be smarter when capturing inline caches to mitigate this. |
| 494 | // (e.g. by having different thresholds for new and old methods). |
| 495 | // |
| 496 | // For OSR: |
| 497 | // We may come from the interpreter and it may have seen different receiver types. |
| 498 | return Runtime::Current()->IsAotCompiler() || outermost_graph_->IsCompilingOsr(); |
| 499 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 500 | bool HInliner::TryInlineFromInlineCache(const DexFile& caller_dex_file, |
| 501 | HInvoke* invoke_instruction, |
| 502 | ArtMethod* resolved_method) |
| 503 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Calin Juravle | e2492d4 | 2017-03-20 11:42:13 -0700 | [diff] [blame] | 504 | if (Runtime::Current()->IsAotCompiler() && !kUseAOTInlineCaches) { |
| 505 | return false; |
| 506 | } |
| 507 | |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 508 | StackHandleScope<1> hs(Thread::Current()); |
| 509 | Handle<mirror::ObjectArray<mirror::Class>> inline_cache; |
| 510 | InlineCacheType inline_cache_type = Runtime::Current()->IsAotCompiler() |
| 511 | ? GetInlineCacheAOT(caller_dex_file, invoke_instruction, &hs, &inline_cache) |
| 512 | : GetInlineCacheJIT(invoke_instruction, &hs, &inline_cache); |
| 513 | |
| 514 | switch (inline_cache_type) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 515 | case kInlineCacheNoData: { |
| 516 | LOG_FAIL_NO_STAT() |
| 517 | << "Interface or virtual call to " |
| 518 | << caller_dex_file.PrettyMethod(invoke_instruction->GetDexMethodIndex()) |
| 519 | << " could not be statically determined"; |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 520 | return false; |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 521 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 522 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 523 | case kInlineCacheUninitialized: { |
| 524 | LOG_FAIL_NO_STAT() |
| 525 | << "Interface or virtual call to " |
| 526 | << caller_dex_file.PrettyMethod(invoke_instruction->GetDexMethodIndex()) |
| 527 | << " is not hit and not inlined"; |
| 528 | return false; |
| 529 | } |
| 530 | |
| 531 | case kInlineCacheMonomorphic: { |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 532 | MaybeRecordStat(kMonomorphicCall); |
Calin Juravle | af44e6c | 2017-05-23 14:24:55 -0700 | [diff] [blame] | 533 | if (UseOnlyPolymorphicInliningWithNoDeopt()) { |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 534 | return TryInlinePolymorphicCall(invoke_instruction, resolved_method, inline_cache); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 535 | } else { |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 536 | return TryInlineMonomorphicCall(invoke_instruction, resolved_method, inline_cache); |
Nicolas Geoffray | b6e20ae | 2016-03-07 14:29:04 +0000 | [diff] [blame] | 537 | } |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 538 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 539 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 540 | case kInlineCachePolymorphic: { |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 541 | MaybeRecordStat(kPolymorphicCall); |
| 542 | return TryInlinePolymorphicCall(invoke_instruction, resolved_method, inline_cache); |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 543 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 544 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 545 | case kInlineCacheMegamorphic: { |
| 546 | LOG_FAIL_NO_STAT() |
| 547 | << "Interface or virtual call to " |
| 548 | << caller_dex_file.PrettyMethod(invoke_instruction->GetDexMethodIndex()) |
| 549 | << " is megamorphic and not inlined"; |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 550 | MaybeRecordStat(kMegamorphicCall); |
| 551 | return false; |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 552 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 553 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 554 | case kInlineCacheMissingTypes: { |
| 555 | LOG_FAIL_NO_STAT() |
| 556 | << "Interface or virtual call to " |
| 557 | << caller_dex_file.PrettyMethod(invoke_instruction->GetDexMethodIndex()) |
| 558 | << " is missing types and not inlined"; |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 559 | return false; |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 560 | } |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 561 | } |
| 562 | UNREACHABLE(); |
| 563 | } |
| 564 | |
| 565 | HInliner::InlineCacheType HInliner::GetInlineCacheJIT( |
| 566 | HInvoke* invoke_instruction, |
| 567 | StackHandleScope<1>* hs, |
| 568 | /*out*/Handle<mirror::ObjectArray<mirror::Class>>* inline_cache) |
| 569 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 570 | DCHECK(Runtime::Current()->UseJitCompilation()); |
| 571 | |
| 572 | ArtMethod* caller = graph_->GetArtMethod(); |
| 573 | // Under JIT, we should always know the caller. |
| 574 | DCHECK(caller != nullptr); |
| 575 | ScopedProfilingInfoInlineUse spiis(caller, Thread::Current()); |
| 576 | ProfilingInfo* profiling_info = spiis.GetProfilingInfo(); |
| 577 | |
| 578 | if (profiling_info == nullptr) { |
| 579 | return kInlineCacheNoData; |
| 580 | } |
| 581 | |
| 582 | *inline_cache = AllocateInlineCacheHolder(caller_compilation_unit_, hs); |
| 583 | if (inline_cache->Get() == nullptr) { |
| 584 | // We can't extract any data if we failed to allocate; |
| 585 | return kInlineCacheNoData; |
| 586 | } else { |
| 587 | Runtime::Current()->GetJit()->GetCodeCache()->CopyInlineCacheInto( |
| 588 | *profiling_info->GetInlineCache(invoke_instruction->GetDexPc()), |
| 589 | *inline_cache); |
| 590 | return GetInlineCacheType(*inline_cache); |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | HInliner::InlineCacheType HInliner::GetInlineCacheAOT( |
| 595 | const DexFile& caller_dex_file, |
| 596 | HInvoke* invoke_instruction, |
| 597 | StackHandleScope<1>* hs, |
| 598 | /*out*/Handle<mirror::ObjectArray<mirror::Class>>* inline_cache) |
| 599 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 600 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 601 | const ProfileCompilationInfo* pci = compiler_driver_->GetProfileCompilationInfo(); |
| 602 | if (pci == nullptr) { |
| 603 | return kInlineCacheNoData; |
| 604 | } |
| 605 | |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 606 | std::unique_ptr<ProfileCompilationInfo::OfflineProfileMethodInfo> offline_profile = |
| 607 | pci->GetMethod(caller_dex_file.GetLocation(), |
| 608 | caller_dex_file.GetLocationChecksum(), |
| 609 | caller_compilation_unit_.GetDexMethodIndex()); |
| 610 | if (offline_profile == nullptr) { |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 611 | return kInlineCacheNoData; // no profile information for this invocation. |
| 612 | } |
| 613 | |
| 614 | *inline_cache = AllocateInlineCacheHolder(caller_compilation_unit_, hs); |
| 615 | if (inline_cache == nullptr) { |
| 616 | // We can't extract any data if we failed to allocate; |
| 617 | return kInlineCacheNoData; |
| 618 | } else { |
| 619 | return ExtractClassesFromOfflineProfile(invoke_instruction, |
Calin Juravle | cc3171a | 2017-05-19 16:47:53 -0700 | [diff] [blame] | 620 | *(offline_profile.get()), |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 621 | *inline_cache); |
| 622 | } |
| 623 | } |
| 624 | |
| 625 | HInliner::InlineCacheType HInliner::ExtractClassesFromOfflineProfile( |
| 626 | const HInvoke* invoke_instruction, |
| 627 | const ProfileCompilationInfo::OfflineProfileMethodInfo& offline_profile, |
| 628 | /*out*/Handle<mirror::ObjectArray<mirror::Class>> inline_cache) |
| 629 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Calin Juravle | e6f87cc | 2017-05-24 17:41:05 -0700 | [diff] [blame] | 630 | const auto it = offline_profile.inline_caches->find(invoke_instruction->GetDexPc()); |
| 631 | if (it == offline_profile.inline_caches->end()) { |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 632 | return kInlineCacheUninitialized; |
| 633 | } |
| 634 | |
| 635 | const ProfileCompilationInfo::DexPcData& dex_pc_data = it->second; |
| 636 | |
| 637 | if (dex_pc_data.is_missing_types) { |
| 638 | return kInlineCacheMissingTypes; |
| 639 | } |
| 640 | if (dex_pc_data.is_megamorphic) { |
| 641 | return kInlineCacheMegamorphic; |
| 642 | } |
| 643 | |
| 644 | DCHECK_LE(dex_pc_data.classes.size(), InlineCache::kIndividualCacheSize); |
| 645 | Thread* self = Thread::Current(); |
| 646 | // We need to resolve the class relative to the containing dex file. |
| 647 | // So first, build a mapping from the index of dex file in the profile to |
| 648 | // its dex cache. This will avoid repeating the lookup when walking over |
| 649 | // the inline cache types. |
| 650 | std::vector<ObjPtr<mirror::DexCache>> dex_profile_index_to_dex_cache( |
| 651 | offline_profile.dex_references.size()); |
| 652 | for (size_t i = 0; i < offline_profile.dex_references.size(); i++) { |
| 653 | bool found = false; |
| 654 | for (const DexFile* dex_file : compiler_driver_->GetDexFilesForOatFile()) { |
| 655 | if (offline_profile.dex_references[i].MatchesDex(dex_file)) { |
| 656 | dex_profile_index_to_dex_cache[i] = |
| 657 | caller_compilation_unit_.GetClassLinker()->FindDexCache(self, *dex_file); |
| 658 | found = true; |
| 659 | } |
| 660 | } |
| 661 | if (!found) { |
| 662 | VLOG(compiler) << "Could not find profiled dex file: " |
| 663 | << offline_profile.dex_references[i].dex_location; |
| 664 | return kInlineCacheMissingTypes; |
Nicolas Geoffray | 454a481 | 2015-06-09 10:37:32 +0100 | [diff] [blame] | 665 | } |
| 666 | } |
| 667 | |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 668 | // Walk over the classes and resolve them. If we cannot find a type we return |
| 669 | // kInlineCacheMissingTypes. |
| 670 | int ic_index = 0; |
| 671 | for (const ProfileCompilationInfo::ClassReference& class_ref : dex_pc_data.classes) { |
| 672 | ObjPtr<mirror::DexCache> dex_cache = |
| 673 | dex_profile_index_to_dex_cache[class_ref.dex_profile_index]; |
| 674 | DCHECK(dex_cache != nullptr); |
Calin Juravle | 0855688 | 2017-05-26 16:40:45 -0700 | [diff] [blame^] | 675 | |
| 676 | if (!dex_cache->GetDexFile()->IsTypeIndexValid(class_ref.type_index)) { |
| 677 | VLOG(compiler) << "Profile data corrupt: type index " << class_ref.type_index |
| 678 | << "is invalid in location" << dex_cache->GetDexFile()->GetLocation(); |
| 679 | return kInlineCacheNoData; |
| 680 | } |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 681 | ObjPtr<mirror::Class> clazz = ClassLinker::LookupResolvedType( |
| 682 | class_ref.type_index, |
| 683 | dex_cache, |
| 684 | caller_compilation_unit_.GetClassLoader().Get()); |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 685 | if (clazz != nullptr) { |
| 686 | inline_cache->Set(ic_index++, clazz); |
| 687 | } else { |
| 688 | VLOG(compiler) << "Could not resolve class from inline cache in AOT mode " |
| 689 | << caller_compilation_unit_.GetDexFile()->PrettyMethod( |
| 690 | invoke_instruction->GetDexMethodIndex()) << " : " |
| 691 | << caller_compilation_unit_ |
| 692 | .GetDexFile()->StringByTypeIdx(class_ref.type_index); |
| 693 | return kInlineCacheMissingTypes; |
| 694 | } |
| 695 | } |
| 696 | return GetInlineCacheType(inline_cache); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 697 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 698 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 699 | HInstanceFieldGet* HInliner::BuildGetReceiverClass(ClassLinker* class_linker, |
| 700 | HInstruction* receiver, |
| 701 | uint32_t dex_pc) const { |
| 702 | ArtField* field = class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0); |
| 703 | DCHECK_EQ(std::string(field->GetName()), "shadow$_klass_"); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 704 | HInstanceFieldGet* result = new (graph_->GetArena()) HInstanceFieldGet( |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 705 | receiver, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 706 | field, |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 707 | Primitive::kPrimNot, |
| 708 | field->GetOffset(), |
| 709 | field->IsVolatile(), |
| 710 | field->GetDexFieldIndex(), |
| 711 | field->GetDeclaringClass()->GetDexClassDefIndex(), |
| 712 | *field->GetDexFile(), |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 713 | dex_pc); |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 714 | // The class of a field is effectively final, and does not have any memory dependencies. |
| 715 | result->SetSideEffects(SideEffects::None()); |
| 716 | return result; |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 717 | } |
| 718 | |
Nicolas Geoffray | 4c0b4bc | 2017-03-17 13:08:26 +0000 | [diff] [blame] | 719 | static ArtMethod* ResolveMethodFromInlineCache(Handle<mirror::Class> klass, |
| 720 | ArtMethod* resolved_method, |
| 721 | HInstruction* invoke_instruction, |
| 722 | PointerSize pointer_size) |
| 723 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 724 | if (Runtime::Current()->IsAotCompiler()) { |
| 725 | // We can get unrelated types when working with profiles (corruption, |
| 726 | // systme updates, or anyone can write to it). So first check if the class |
| 727 | // actually implements the declaring class of the method that is being |
| 728 | // called in bytecode. |
| 729 | // Note: the lookup methods used below require to have assignable types. |
| 730 | if (!resolved_method->GetDeclaringClass()->IsAssignableFrom(klass.Get())) { |
| 731 | return nullptr; |
| 732 | } |
| 733 | } |
| 734 | |
| 735 | if (invoke_instruction->IsInvokeInterface()) { |
| 736 | resolved_method = klass->FindVirtualMethodForInterface(resolved_method, pointer_size); |
| 737 | } else { |
| 738 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
| 739 | resolved_method = klass->FindVirtualMethodForVirtual(resolved_method, pointer_size); |
| 740 | } |
| 741 | DCHECK(resolved_method != nullptr); |
| 742 | return resolved_method; |
| 743 | } |
| 744 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 745 | bool HInliner::TryInlineMonomorphicCall(HInvoke* invoke_instruction, |
| 746 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 747 | Handle<mirror::ObjectArray<mirror::Class>> classes) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 748 | DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface()) |
| 749 | << invoke_instruction->DebugName(); |
| 750 | |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 751 | dex::TypeIndex class_index = FindClassIndexIn( |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 752 | GetMonomorphicType(classes), caller_compilation_unit_); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 753 | if (!class_index.IsValid()) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 754 | LOG_FAIL(kNotInlinedDexCache) |
| 755 | << "Call to " << ArtMethod::PrettyMethod(resolved_method) |
| 756 | << " from inline cache is not inlined because its class is not" |
| 757 | << " accessible to the caller"; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 758 | return false; |
| 759 | } |
| 760 | |
| 761 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 762 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Nicolas Geoffray | 4c0b4bc | 2017-03-17 13:08:26 +0000 | [diff] [blame] | 763 | Handle<mirror::Class> monomorphic_type = handles_->NewHandle(GetMonomorphicType(classes)); |
| 764 | resolved_method = ResolveMethodFromInlineCache( |
| 765 | monomorphic_type, resolved_method, invoke_instruction, pointer_size); |
| 766 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 767 | LOG_NOTE() << "Try inline monomorphic call to " << resolved_method->PrettyMethod(); |
Nicolas Geoffray | 4c0b4bc | 2017-03-17 13:08:26 +0000 | [diff] [blame] | 768 | if (resolved_method == nullptr) { |
| 769 | // Bogus AOT profile, bail. |
| 770 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 771 | return false; |
| 772 | } |
| 773 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 774 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 775 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 776 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 777 | if (!TryInlineAndReplace(invoke_instruction, |
| 778 | resolved_method, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 779 | ReferenceTypeInfo::Create(monomorphic_type, /* is_exact */ true), |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 780 | /* do_rtp */ false, |
| 781 | /* cha_devirtualize */ false)) { |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 782 | return false; |
| 783 | } |
| 784 | |
| 785 | // We successfully inlined, now add a guard. |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 786 | AddTypeGuard(receiver, |
| 787 | cursor, |
| 788 | bb_cursor, |
| 789 | class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 790 | monomorphic_type, |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 791 | invoke_instruction, |
| 792 | /* with_deoptimization */ true); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 793 | |
| 794 | // Run type propagation to get the guard typed, and eventually propagate the |
| 795 | // type of the receiver. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 796 | ReferenceTypePropagation rtp_fixup(graph_, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 797 | outer_compilation_unit_.GetClassLoader(), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 798 | outer_compilation_unit_.GetDexCache(), |
| 799 | handles_, |
| 800 | /* is_first_run */ false); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 801 | rtp_fixup.Run(); |
| 802 | |
| 803 | MaybeRecordStat(kInlinedMonomorphicCall); |
| 804 | return true; |
| 805 | } |
| 806 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 807 | void HInliner::AddCHAGuard(HInstruction* invoke_instruction, |
| 808 | uint32_t dex_pc, |
| 809 | HInstruction* cursor, |
| 810 | HBasicBlock* bb_cursor) { |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 811 | HShouldDeoptimizeFlag* deopt_flag = new (graph_->GetArena()) |
| 812 | HShouldDeoptimizeFlag(graph_->GetArena(), dex_pc); |
| 813 | HInstruction* compare = new (graph_->GetArena()) HNotEqual( |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 814 | deopt_flag, graph_->GetIntConstant(0, dex_pc)); |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 815 | HInstruction* deopt = new (graph_->GetArena()) HDeoptimize( |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 816 | graph_->GetArena(), compare, DeoptimizationKind::kCHA, dex_pc); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 817 | |
| 818 | if (cursor != nullptr) { |
| 819 | bb_cursor->InsertInstructionAfter(deopt_flag, cursor); |
| 820 | } else { |
| 821 | bb_cursor->InsertInstructionBefore(deopt_flag, bb_cursor->GetFirstInstruction()); |
| 822 | } |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 823 | bb_cursor->InsertInstructionAfter(compare, deopt_flag); |
| 824 | bb_cursor->InsertInstructionAfter(deopt, compare); |
| 825 | |
| 826 | // Add receiver as input to aid CHA guard optimization later. |
| 827 | deopt_flag->AddInput(invoke_instruction->InputAt(0)); |
| 828 | DCHECK_EQ(deopt_flag->InputCount(), 1u); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 829 | deopt->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
Mingyao Yang | b0b051a | 2016-11-17 09:04:53 -0800 | [diff] [blame] | 830 | outermost_graph_->IncrementNumberOfCHAGuards(); |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 831 | } |
| 832 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 833 | HInstruction* HInliner::AddTypeGuard(HInstruction* receiver, |
| 834 | HInstruction* cursor, |
| 835 | HBasicBlock* bb_cursor, |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 836 | dex::TypeIndex class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 837 | Handle<mirror::Class> klass, |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 838 | HInstruction* invoke_instruction, |
| 839 | bool with_deoptimization) { |
| 840 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
| 841 | HInstanceFieldGet* receiver_class = BuildGetReceiverClass( |
| 842 | class_linker, receiver, invoke_instruction->GetDexPc()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 843 | if (cursor != nullptr) { |
| 844 | bb_cursor->InsertInstructionAfter(receiver_class, cursor); |
| 845 | } else { |
| 846 | bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction()); |
| 847 | } |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 848 | |
| 849 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Calin Juravle | 07f01df | 2017-04-28 19:58:01 -0700 | [diff] [blame] | 850 | bool is_referrer; |
| 851 | ArtMethod* outermost_art_method = outermost_graph_->GetArtMethod(); |
| 852 | if (outermost_art_method == nullptr) { |
| 853 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 854 | // We are in AOT mode and we don't have an ART method to determine |
| 855 | // if the inlined method belongs to the referrer. Assume it doesn't. |
| 856 | is_referrer = false; |
| 857 | } else { |
| 858 | is_referrer = klass.Get() == outermost_art_method->GetDeclaringClass(); |
| 859 | } |
| 860 | |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 861 | // Note that we will just compare the classes, so we don't need Java semantics access checks. |
| 862 | // Note that the type index and the dex file are relative to the method this type guard is |
| 863 | // inlined into. |
| 864 | HLoadClass* load_class = new (graph_->GetArena()) HLoadClass(graph_->GetCurrentMethod(), |
| 865 | class_index, |
| 866 | caller_dex_file, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 867 | klass, |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 868 | is_referrer, |
| 869 | invoke_instruction->GetDexPc(), |
| 870 | /* needs_access_check */ false); |
Nicolas Geoffray | c4aa82c | 2017-03-06 14:38:52 +0000 | [diff] [blame] | 871 | HLoadClass::LoadKind kind = HSharpening::ComputeLoadClassKind( |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 872 | load_class, codegen_, compiler_driver_, caller_compilation_unit_); |
| 873 | DCHECK(kind != HLoadClass::LoadKind::kInvalid) |
| 874 | << "We should always be able to reference a class for inline caches"; |
| 875 | // Insert before setting the kind, as setting the kind affects the inputs. |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 876 | bb_cursor->InsertInstructionAfter(load_class, receiver_class); |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 877 | load_class->SetLoadKind(kind); |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 878 | // In AOT mode, we will most likely load the class from BSS, which will involve a call |
| 879 | // to the runtime. In this case, the load instruction will need an environment so copy |
| 880 | // it from the invoke instruction. |
| 881 | if (load_class->NeedsEnvironment()) { |
| 882 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 883 | load_class->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 884 | } |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 885 | |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 886 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(load_class, receiver_class); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 887 | bb_cursor->InsertInstructionAfter(compare, load_class); |
| 888 | if (with_deoptimization) { |
| 889 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 890 | graph_->GetArena(), |
| 891 | compare, |
| 892 | receiver, |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 893 | Runtime::Current()->IsAotCompiler() |
| 894 | ? DeoptimizationKind::kAotInlineCache |
| 895 | : DeoptimizationKind::kJitInlineCache, |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 896 | invoke_instruction->GetDexPc()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 897 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 898 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 899 | DCHECK_EQ(invoke_instruction->InputAt(0), receiver); |
| 900 | receiver->ReplaceUsesDominatedBy(deoptimize, deoptimize); |
| 901 | deoptimize->SetReferenceTypeInfo(receiver->GetReferenceTypeInfo()); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 902 | } |
| 903 | return compare; |
| 904 | } |
| 905 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 906 | bool HInliner::TryInlinePolymorphicCall(HInvoke* invoke_instruction, |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 907 | ArtMethod* resolved_method, |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 908 | Handle<mirror::ObjectArray<mirror::Class>> classes) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 909 | DCHECK(invoke_instruction->IsInvokeVirtual() || invoke_instruction->IsInvokeInterface()) |
| 910 | << invoke_instruction->DebugName(); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 911 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 912 | if (TryInlinePolymorphicCallToSameTarget(invoke_instruction, resolved_method, classes)) { |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 913 | return true; |
| 914 | } |
| 915 | |
| 916 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 917 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 918 | |
| 919 | bool all_targets_inlined = true; |
| 920 | bool one_target_inlined = false; |
| 921 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 922 | if (classes->Get(i) == nullptr) { |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 923 | break; |
| 924 | } |
| 925 | ArtMethod* method = nullptr; |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 926 | |
| 927 | Handle<mirror::Class> handle = handles_->NewHandle(classes->Get(i)); |
Nicolas Geoffray | 4c0b4bc | 2017-03-17 13:08:26 +0000 | [diff] [blame] | 928 | method = ResolveMethodFromInlineCache( |
| 929 | handle, resolved_method, invoke_instruction, pointer_size); |
| 930 | if (method == nullptr) { |
| 931 | DCHECK(Runtime::Current()->IsAotCompiler()); |
| 932 | // AOT profile is bogus. This loop expects to iterate over all entries, |
| 933 | // so just just continue. |
| 934 | all_targets_inlined = false; |
| 935 | continue; |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 936 | } |
| 937 | |
| 938 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 939 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 940 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 941 | |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 942 | dex::TypeIndex class_index = FindClassIndexIn(handle.Get(), caller_compilation_unit_); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 943 | HInstruction* return_replacement = nullptr; |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 944 | LOG_NOTE() << "Try inline polymorphic call to " << method->PrettyMethod(); |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 945 | if (!class_index.IsValid() || |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 946 | !TryBuildAndInline(invoke_instruction, |
| 947 | method, |
| 948 | ReferenceTypeInfo::Create(handle, /* is_exact */ true), |
| 949 | &return_replacement)) { |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 950 | all_targets_inlined = false; |
| 951 | } else { |
| 952 | one_target_inlined = true; |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 953 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 954 | LOG_SUCCESS() << "Polymorphic call to " << ArtMethod::PrettyMethod(resolved_method) |
| 955 | << " has inlined " << ArtMethod::PrettyMethod(method); |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 956 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 957 | // If we have inlined all targets before, and this receiver is the last seen, |
| 958 | // we deoptimize instead of keeping the original invoke instruction. |
Calin Juravle | af44e6c | 2017-05-23 14:24:55 -0700 | [diff] [blame] | 959 | bool deoptimize = !UseOnlyPolymorphicInliningWithNoDeopt() && |
| 960 | all_targets_inlined && |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 961 | (i != InlineCache::kIndividualCacheSize - 1) && |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 962 | (classes->Get(i + 1) == nullptr); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 963 | |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 964 | HInstruction* compare = AddTypeGuard(receiver, |
| 965 | cursor, |
| 966 | bb_cursor, |
| 967 | class_index, |
Nicolas Geoffray | 5247c08 | 2017-01-13 14:17:29 +0000 | [diff] [blame] | 968 | handle, |
Nicolas Geoffray | 5687634 | 2016-12-16 16:09:08 +0000 | [diff] [blame] | 969 | invoke_instruction, |
| 970 | deoptimize); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 971 | if (deoptimize) { |
| 972 | if (return_replacement != nullptr) { |
| 973 | invoke_instruction->ReplaceWith(return_replacement); |
| 974 | } |
| 975 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
| 976 | // Because the inline cache data can be populated concurrently, we force the end of the |
Nicolas Geoffray | 4c0b4bc | 2017-03-17 13:08:26 +0000 | [diff] [blame] | 977 | // iteration. Otherwise, we could see a new receiver type. |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 978 | break; |
| 979 | } else { |
| 980 | CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction); |
| 981 | } |
| 982 | } |
| 983 | } |
| 984 | |
| 985 | if (!one_target_inlined) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 986 | LOG_FAIL_NO_STAT() |
| 987 | << "Call to " << ArtMethod::PrettyMethod(resolved_method) |
| 988 | << " from inline cache is not inlined because none" |
| 989 | << " of its targets could be inlined"; |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 990 | return false; |
| 991 | } |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 992 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 993 | MaybeRecordStat(kInlinedPolymorphicCall); |
| 994 | |
| 995 | // Run type propagation to get the guards typed. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 996 | ReferenceTypePropagation rtp_fixup(graph_, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 997 | outer_compilation_unit_.GetClassLoader(), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 998 | outer_compilation_unit_.GetDexCache(), |
| 999 | handles_, |
| 1000 | /* is_first_run */ false); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 1001 | rtp_fixup.Run(); |
| 1002 | return true; |
| 1003 | } |
| 1004 | |
| 1005 | void HInliner::CreateDiamondPatternForPolymorphicInline(HInstruction* compare, |
| 1006 | HInstruction* return_replacement, |
| 1007 | HInstruction* invoke_instruction) { |
| 1008 | uint32_t dex_pc = invoke_instruction->GetDexPc(); |
| 1009 | HBasicBlock* cursor_block = compare->GetBlock(); |
| 1010 | HBasicBlock* original_invoke_block = invoke_instruction->GetBlock(); |
| 1011 | ArenaAllocator* allocator = graph_->GetArena(); |
| 1012 | |
| 1013 | // Spit the block after the compare: `cursor_block` will now be the start of the diamond, |
| 1014 | // and the returned block is the start of the then branch (that could contain multiple blocks). |
| 1015 | HBasicBlock* then = cursor_block->SplitAfterForInlining(compare); |
| 1016 | |
| 1017 | // Split the block containing the invoke before and after the invoke. The returned block |
| 1018 | // of the split before will contain the invoke and will be the otherwise branch of |
| 1019 | // the diamond. The returned block of the split after will be the merge block |
| 1020 | // of the diamond. |
| 1021 | HBasicBlock* end_then = invoke_instruction->GetBlock(); |
| 1022 | HBasicBlock* otherwise = end_then->SplitBeforeForInlining(invoke_instruction); |
| 1023 | HBasicBlock* merge = otherwise->SplitAfterForInlining(invoke_instruction); |
| 1024 | |
| 1025 | // If the methods we are inlining return a value, we create a phi in the merge block |
| 1026 | // that will have the `invoke_instruction and the `return_replacement` as inputs. |
| 1027 | if (return_replacement != nullptr) { |
| 1028 | HPhi* phi = new (allocator) HPhi( |
| 1029 | allocator, kNoRegNumber, 0, HPhi::ToPhiType(invoke_instruction->GetType()), dex_pc); |
| 1030 | merge->AddPhi(phi); |
| 1031 | invoke_instruction->ReplaceWith(phi); |
| 1032 | phi->AddInput(return_replacement); |
| 1033 | phi->AddInput(invoke_instruction); |
| 1034 | } |
| 1035 | |
| 1036 | // Add the control flow instructions. |
| 1037 | otherwise->AddInstruction(new (allocator) HGoto(dex_pc)); |
| 1038 | end_then->AddInstruction(new (allocator) HGoto(dex_pc)); |
| 1039 | cursor_block->AddInstruction(new (allocator) HIf(compare, dex_pc)); |
| 1040 | |
| 1041 | // Add the newly created blocks to the graph. |
| 1042 | graph_->AddBlock(then); |
| 1043 | graph_->AddBlock(otherwise); |
| 1044 | graph_->AddBlock(merge); |
| 1045 | |
| 1046 | // Set up successor (and implictly predecessor) relations. |
| 1047 | cursor_block->AddSuccessor(otherwise); |
| 1048 | cursor_block->AddSuccessor(then); |
| 1049 | end_then->AddSuccessor(merge); |
| 1050 | otherwise->AddSuccessor(merge); |
| 1051 | |
| 1052 | // Set up dominance information. |
| 1053 | then->SetDominator(cursor_block); |
| 1054 | cursor_block->AddDominatedBlock(then); |
| 1055 | otherwise->SetDominator(cursor_block); |
| 1056 | cursor_block->AddDominatedBlock(otherwise); |
| 1057 | merge->SetDominator(cursor_block); |
| 1058 | cursor_block->AddDominatedBlock(merge); |
| 1059 | |
| 1060 | // Update the revert post order. |
| 1061 | size_t index = IndexOfElement(graph_->reverse_post_order_, cursor_block); |
| 1062 | MakeRoomFor(&graph_->reverse_post_order_, 1, index); |
| 1063 | graph_->reverse_post_order_[++index] = then; |
| 1064 | index = IndexOfElement(graph_->reverse_post_order_, end_then); |
| 1065 | MakeRoomFor(&graph_->reverse_post_order_, 2, index); |
| 1066 | graph_->reverse_post_order_[++index] = otherwise; |
| 1067 | graph_->reverse_post_order_[++index] = merge; |
| 1068 | |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 1069 | |
Nicolas Geoffray | a1d8ddf | 2016-02-29 11:46:58 +0000 | [diff] [blame] | 1070 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 1071 | then, original_invoke_block, /* replace_if_back_edge */ false); |
| 1072 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 1073 | otherwise, original_invoke_block, /* replace_if_back_edge */ false); |
| 1074 | |
| 1075 | // In case the original invoke location was a back edge, we need to update |
| 1076 | // the loop to now have the merge block as a back edge. |
| 1077 | graph_->UpdateLoopAndTryInformationOfNewBlock( |
| 1078 | merge, original_invoke_block, /* replace_if_back_edge */ true); |
Nicolas Geoffray | 916cc1d | 2016-02-18 11:12:31 +0000 | [diff] [blame] | 1079 | } |
| 1080 | |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 1081 | bool HInliner::TryInlinePolymorphicCallToSameTarget( |
| 1082 | HInvoke* invoke_instruction, |
| 1083 | ArtMethod* resolved_method, |
| 1084 | Handle<mirror::ObjectArray<mirror::Class>> classes) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1085 | // This optimization only works under JIT for now. |
Calin Juravle | 13439f0 | 2017-02-21 01:17:21 -0800 | [diff] [blame] | 1086 | if (!Runtime::Current()->UseJitCompilation()) { |
| 1087 | return false; |
| 1088 | } |
| 1089 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1090 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 1091 | PointerSize pointer_size = class_linker->GetImagePointerSize(); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1092 | |
| 1093 | DCHECK(resolved_method != nullptr); |
| 1094 | ArtMethod* actual_method = nullptr; |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 1095 | size_t method_index = invoke_instruction->IsInvokeVirtual() |
| 1096 | ? invoke_instruction->AsInvokeVirtual()->GetVTableIndex() |
| 1097 | : invoke_instruction->AsInvokeInterface()->GetImtIndex(); |
| 1098 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1099 | // Check whether we are actually calling the same method among |
| 1100 | // the different types seen. |
| 1101 | for (size_t i = 0; i < InlineCache::kIndividualCacheSize; ++i) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 1102 | if (classes->Get(i) == nullptr) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1103 | break; |
| 1104 | } |
| 1105 | ArtMethod* new_method = nullptr; |
| 1106 | if (invoke_instruction->IsInvokeInterface()) { |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 1107 | new_method = classes->Get(i)->GetImt(pointer_size)->Get( |
Matthew Gharrity | 465ecc8 | 2016-07-19 21:32:52 +0000 | [diff] [blame] | 1108 | method_index, pointer_size); |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 1109 | if (new_method->IsRuntimeMethod()) { |
| 1110 | // Bail out as soon as we see a conflict trampoline in one of the target's |
| 1111 | // interface table. |
| 1112 | return false; |
| 1113 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1114 | } else { |
| 1115 | DCHECK(invoke_instruction->IsInvokeVirtual()); |
Nicolas Geoffray | e51ca8b | 2016-11-22 14:49:31 +0000 | [diff] [blame] | 1116 | new_method = classes->Get(i)->GetEmbeddedVTableEntry(method_index, pointer_size); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1117 | } |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 1118 | DCHECK(new_method != nullptr); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1119 | if (actual_method == nullptr) { |
| 1120 | actual_method = new_method; |
| 1121 | } else if (actual_method != new_method) { |
| 1122 | // Different methods, bailout. |
| 1123 | return false; |
| 1124 | } |
| 1125 | } |
| 1126 | |
| 1127 | HInstruction* receiver = invoke_instruction->InputAt(0); |
| 1128 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 1129 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
| 1130 | |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1131 | HInstruction* return_replacement = nullptr; |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1132 | if (!TryBuildAndInline(invoke_instruction, |
| 1133 | actual_method, |
| 1134 | ReferenceTypeInfo::CreateInvalid(), |
| 1135 | &return_replacement)) { |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1136 | return false; |
| 1137 | } |
| 1138 | |
| 1139 | // We successfully inlined, now add a guard. |
| 1140 | HInstanceFieldGet* receiver_class = BuildGetReceiverClass( |
| 1141 | class_linker, receiver, invoke_instruction->GetDexPc()); |
| 1142 | |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1143 | Primitive::Type type = Is64BitInstructionSet(graph_->GetInstructionSet()) |
| 1144 | ? Primitive::kPrimLong |
| 1145 | : Primitive::kPrimInt; |
| 1146 | HClassTableGet* class_table_get = new (graph_->GetArena()) HClassTableGet( |
| 1147 | receiver_class, |
| 1148 | type, |
Vladimir Marko | a1de918 | 2016-02-25 11:37:38 +0000 | [diff] [blame] | 1149 | invoke_instruction->IsInvokeVirtual() ? HClassTableGet::TableKind::kVTable |
| 1150 | : HClassTableGet::TableKind::kIMTable, |
Nicolas Geoffray | 4f97a21 | 2016-02-25 16:17:54 +0000 | [diff] [blame] | 1151 | method_index, |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1152 | invoke_instruction->GetDexPc()); |
| 1153 | |
| 1154 | HConstant* constant; |
| 1155 | if (type == Primitive::kPrimLong) { |
| 1156 | constant = graph_->GetLongConstant( |
| 1157 | reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc()); |
| 1158 | } else { |
| 1159 | constant = graph_->GetIntConstant( |
| 1160 | reinterpret_cast<intptr_t>(actual_method), invoke_instruction->GetDexPc()); |
| 1161 | } |
| 1162 | |
| 1163 | HNotEqual* compare = new (graph_->GetArena()) HNotEqual(class_table_get, constant); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1164 | if (cursor != nullptr) { |
| 1165 | bb_cursor->InsertInstructionAfter(receiver_class, cursor); |
| 1166 | } else { |
| 1167 | bb_cursor->InsertInstructionBefore(receiver_class, bb_cursor->GetFirstInstruction()); |
| 1168 | } |
| 1169 | bb_cursor->InsertInstructionAfter(class_table_get, receiver_class); |
| 1170 | bb_cursor->InsertInstructionAfter(compare, class_table_get); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1171 | |
| 1172 | if (outermost_graph_->IsCompilingOsr()) { |
| 1173 | CreateDiamondPatternForPolymorphicInline(compare, return_replacement, invoke_instruction); |
| 1174 | } else { |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1175 | HDeoptimize* deoptimize = new (graph_->GetArena()) HDeoptimize( |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 1176 | graph_->GetArena(), |
| 1177 | compare, |
| 1178 | receiver, |
Nicolas Geoffray | 4e92c3c | 2017-05-08 09:34:26 +0100 | [diff] [blame] | 1179 | DeoptimizationKind::kJitSameTarget, |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 1180 | invoke_instruction->GetDexPc()); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1181 | bb_cursor->InsertInstructionAfter(deoptimize, compare); |
| 1182 | deoptimize->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 1183 | if (return_replacement != nullptr) { |
| 1184 | invoke_instruction->ReplaceWith(return_replacement); |
| 1185 | } |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 1186 | receiver->ReplaceUsesDominatedBy(deoptimize, deoptimize); |
Nicolas Geoffray | 1be7cbd | 2016-04-29 13:56:01 +0100 | [diff] [blame] | 1187 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
Nicolas Geoffray | 6f8e2c9 | 2017-03-23 14:37:26 +0000 | [diff] [blame] | 1188 | deoptimize->SetReferenceTypeInfo(receiver->GetReferenceTypeInfo()); |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1189 | } |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1190 | |
| 1191 | // Run type propagation to get the guard typed. |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 1192 | ReferenceTypePropagation rtp_fixup(graph_, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1193 | outer_compilation_unit_.GetClassLoader(), |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 1194 | outer_compilation_unit_.GetDexCache(), |
| 1195 | handles_, |
| 1196 | /* is_first_run */ false); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1197 | rtp_fixup.Run(); |
| 1198 | |
| 1199 | MaybeRecordStat(kInlinedPolymorphicCall); |
| 1200 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1201 | LOG_SUCCESS() << "Inlined same polymorphic target " << actual_method->PrettyMethod(); |
Nicolas Geoffray | a42363f | 2015-12-17 14:57:09 +0000 | [diff] [blame] | 1202 | return true; |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1203 | } |
| 1204 | |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1205 | bool HInliner::TryInlineAndReplace(HInvoke* invoke_instruction, |
| 1206 | ArtMethod* method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1207 | ReferenceTypeInfo receiver_type, |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1208 | bool do_rtp, |
| 1209 | bool cha_devirtualize) { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1210 | HInstruction* return_replacement = nullptr; |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1211 | uint32_t dex_pc = invoke_instruction->GetDexPc(); |
| 1212 | HInstruction* cursor = invoke_instruction->GetPrevious(); |
| 1213 | HBasicBlock* bb_cursor = invoke_instruction->GetBlock(); |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1214 | if (!TryBuildAndInline(invoke_instruction, method, receiver_type, &return_replacement)) { |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 1215 | if (invoke_instruction->IsInvokeInterface()) { |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 1216 | DCHECK(!method->IsProxyMethod()); |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 1217 | // Turn an invoke-interface into an invoke-virtual. An invoke-virtual is always |
| 1218 | // better than an invoke-interface because: |
| 1219 | // 1) In the best case, the interface call has one more indirection (to fetch the IMT). |
| 1220 | // 2) We will not go to the conflict trampoline with an invoke-virtual. |
| 1221 | // TODO: Consider sharpening once it is not dependent on the compiler driver. |
Nicolas Geoffray | 18ea1c9 | 2017-03-27 08:00:18 +0000 | [diff] [blame] | 1222 | |
| 1223 | if (method->IsDefault() && !method->IsCopied()) { |
| 1224 | // Changing to invoke-virtual cannot be done on an original default method |
| 1225 | // since it's not in any vtable. Devirtualization by exact type/inline-cache |
| 1226 | // always uses a method in the iftable which is never an original default |
| 1227 | // method. |
| 1228 | // On the other hand, inlining an original default method by CHA is fine. |
| 1229 | DCHECK(cha_devirtualize); |
| 1230 | return false; |
| 1231 | } |
| 1232 | |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 1233 | const DexFile& caller_dex_file = *caller_compilation_unit_.GetDexFile(); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 1234 | uint32_t dex_method_index = FindMethodIndexIn( |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 1235 | method, caller_dex_file, invoke_instruction->GetDexMethodIndex()); |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 1236 | if (dex_method_index == DexFile::kDexNoIndex) { |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 1237 | return false; |
| 1238 | } |
| 1239 | HInvokeVirtual* new_invoke = new (graph_->GetArena()) HInvokeVirtual( |
| 1240 | graph_->GetArena(), |
| 1241 | invoke_instruction->GetNumberOfArguments(), |
| 1242 | invoke_instruction->GetType(), |
| 1243 | invoke_instruction->GetDexPc(), |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 1244 | dex_method_index, |
| 1245 | method, |
Nicolas Geoffray | 5bf7bac | 2016-07-06 14:18:23 +0000 | [diff] [blame] | 1246 | method->GetMethodIndex()); |
| 1247 | HInputsRef inputs = invoke_instruction->GetInputs(); |
| 1248 | for (size_t index = 0; index != inputs.size(); ++index) { |
| 1249 | new_invoke->SetArgumentAt(index, inputs[index]); |
| 1250 | } |
| 1251 | invoke_instruction->GetBlock()->InsertInstructionBefore(new_invoke, invoke_instruction); |
| 1252 | new_invoke->CopyEnvironmentFrom(invoke_instruction->GetEnvironment()); |
| 1253 | if (invoke_instruction->GetType() == Primitive::kPrimNot) { |
| 1254 | new_invoke->SetReferenceTypeInfo(invoke_instruction->GetReferenceTypeInfo()); |
| 1255 | } |
| 1256 | return_replacement = new_invoke; |
| 1257 | } else { |
| 1258 | // TODO: Consider sharpening an invoke virtual once it is not dependent on the |
| 1259 | // compiler driver. |
| 1260 | return false; |
| 1261 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1262 | } |
Mingyao Yang | 063fc77 | 2016-08-02 11:02:54 -0700 | [diff] [blame] | 1263 | if (cha_devirtualize) { |
| 1264 | AddCHAGuard(invoke_instruction, dex_pc, cursor, bb_cursor); |
| 1265 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1266 | if (return_replacement != nullptr) { |
| 1267 | invoke_instruction->ReplaceWith(return_replacement); |
| 1268 | } |
| 1269 | invoke_instruction->GetBlock()->RemoveInstruction(invoke_instruction); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1270 | FixUpReturnReferenceType(method, return_replacement); |
| 1271 | if (do_rtp && ReturnTypeMoreSpecific(invoke_instruction, return_replacement)) { |
| 1272 | // Actual return value has a more specific type than the method's declared |
| 1273 | // return type. Run RTP again on the outer graph to propagate it. |
| 1274 | ReferenceTypePropagation(graph_, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1275 | outer_compilation_unit_.GetClassLoader(), |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1276 | outer_compilation_unit_.GetDexCache(), |
| 1277 | handles_, |
| 1278 | /* is_first_run */ false).Run(); |
| 1279 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1280 | return true; |
| 1281 | } |
| 1282 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1283 | size_t HInliner::CountRecursiveCallsOf(ArtMethod* method) const { |
| 1284 | const HInliner* current = this; |
| 1285 | size_t count = 0; |
| 1286 | do { |
| 1287 | if (current->graph_->GetArtMethod() == method) { |
| 1288 | ++count; |
| 1289 | } |
| 1290 | current = current->parent_; |
| 1291 | } while (current != nullptr); |
| 1292 | return count; |
| 1293 | } |
| 1294 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1295 | bool HInliner::TryBuildAndInline(HInvoke* invoke_instruction, |
| 1296 | ArtMethod* method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1297 | ReferenceTypeInfo receiver_type, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1298 | HInstruction** return_replacement) { |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1299 | if (method->IsProxyMethod()) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1300 | LOG_FAIL(kNotInlinedProxy) |
| 1301 | << "Method " << method->PrettyMethod() |
| 1302 | << " is not inlined because of unimplemented inline support for proxy methods."; |
| 1303 | return false; |
| 1304 | } |
| 1305 | |
| 1306 | if (CountRecursiveCallsOf(method) > kMaximumNumberOfRecursiveCalls) { |
| 1307 | LOG_FAIL(kNotInlinedRecursiveBudget) |
| 1308 | << "Method " |
| 1309 | << method->PrettyMethod() |
| 1310 | << " is not inlined because it has reached its recursive call budget."; |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1311 | return false; |
| 1312 | } |
| 1313 | |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1314 | // Check whether we're allowed to inline. The outermost compilation unit is the relevant |
| 1315 | // dex file here (though the transitivity of an inline chain would allow checking the calller). |
| 1316 | if (!compiler_driver_->MayInline(method->GetDexFile(), |
| 1317 | outer_compilation_unit_.GetDexFile())) { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1318 | if (TryPatternSubstitution(invoke_instruction, method, return_replacement)) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1319 | LOG_SUCCESS() << "Successfully replaced pattern of invoke " |
| 1320 | << method->PrettyMethod(); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1321 | MaybeRecordStat(kReplacedInvokeWithSimplePattern); |
| 1322 | return true; |
| 1323 | } |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1324 | LOG_FAIL(kNotInlinedWont) |
| 1325 | << "Won't inline " << method->PrettyMethod() << " in " |
| 1326 | << outer_compilation_unit_.GetDexFile()->GetLocation() << " (" |
| 1327 | << caller_compilation_unit_.GetDexFile()->GetLocation() << ") from " |
| 1328 | << method->GetDexFile()->GetLocation(); |
Jeff Hao | dcdc85b | 2015-12-04 14:06:18 -0800 | [diff] [blame] | 1329 | return false; |
| 1330 | } |
| 1331 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1332 | bool same_dex_file = IsSameDexFile(*outer_compilation_unit_.GetDexFile(), *method->GetDexFile()); |
| 1333 | |
| 1334 | const DexFile::CodeItem* code_item = method->GetCodeItem(); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1335 | |
| 1336 | if (code_item == nullptr) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1337 | LOG_FAIL_NO_STAT() |
| 1338 | << "Method " << method->PrettyMethod() << " is not inlined because it is native"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1339 | return false; |
| 1340 | } |
| 1341 | |
Calin Juravle | ec74835 | 2015-07-29 13:52:12 +0100 | [diff] [blame] | 1342 | size_t inline_max_code_units = compiler_driver_->GetCompilerOptions().GetInlineMaxCodeUnits(); |
| 1343 | if (code_item->insns_size_in_code_units_ > inline_max_code_units) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1344 | LOG_FAIL(kNotInlinedCodeItem) |
| 1345 | << "Method " << method->PrettyMethod() |
| 1346 | << " is not inlined because its code item is too big: " |
| 1347 | << code_item->insns_size_in_code_units_ |
| 1348 | << " > " |
| 1349 | << inline_max_code_units; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1350 | return false; |
| 1351 | } |
| 1352 | |
| 1353 | if (code_item->tries_size_ != 0) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1354 | LOG_FAIL(kNotInlinedTryCatch) |
| 1355 | << "Method " << method->PrettyMethod() << " is not inlined because of try block"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1356 | return false; |
| 1357 | } |
| 1358 | |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 1359 | if (!method->IsCompilable()) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1360 | LOG_FAIL(kNotInlinedNotVerified) |
| 1361 | << "Method " << method->PrettyMethod() |
| 1362 | << " has soft failures un-handled by the compiler, so it cannot be inlined"; |
Nicolas Geoffray | 250a378 | 2016-04-20 16:27:53 +0100 | [diff] [blame] | 1363 | } |
| 1364 | |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1365 | if (!method->GetDeclaringClass()->IsVerified()) { |
| 1366 | uint16_t class_def_idx = method->GetDeclaringClass()->GetDexClassDefIndex(); |
Calin Juravle | ffc8707 | 2016-04-20 14:22:09 +0100 | [diff] [blame] | 1367 | if (Runtime::Current()->UseJitCompilation() || |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 1368 | !compiler_driver_->IsMethodVerifiedWithoutFailures( |
| 1369 | method->GetDexMethodIndex(), class_def_idx, *method->GetDexFile())) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1370 | LOG_FAIL(kNotInlinedNotVerified) |
| 1371 | << "Method " << method->PrettyMethod() |
| 1372 | << " couldn't be verified, so it cannot be inlined"; |
Nicolas Geoffray | ccc6197 | 2015-10-01 14:34:20 +0100 | [diff] [blame] | 1373 | return false; |
| 1374 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1375 | } |
| 1376 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1377 | if (invoke_instruction->IsInvokeStaticOrDirect() && |
| 1378 | invoke_instruction->AsInvokeStaticOrDirect()->IsStaticWithImplicitClinitCheck()) { |
| 1379 | // Case of a static method that cannot be inlined because it implicitly |
| 1380 | // requires an initialization check of its declaring class. |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1381 | LOG_FAIL(kNotInlinedDexCache) << "Method " << method->PrettyMethod() |
| 1382 | << " is not inlined because it is static and requires a clinit" |
| 1383 | << " check that cannot be emitted due to Dex cache limitations"; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 1384 | return false; |
| 1385 | } |
| 1386 | |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1387 | if (!TryBuildAndInlineHelper( |
| 1388 | invoke_instruction, method, receiver_type, same_dex_file, return_replacement)) { |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 1389 | return false; |
| 1390 | } |
| 1391 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1392 | LOG_SUCCESS() << method->PrettyMethod(); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 1393 | MaybeRecordStat(kInlinedInvoke); |
| 1394 | return true; |
| 1395 | } |
| 1396 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1397 | static HInstruction* GetInvokeInputForArgVRegIndex(HInvoke* invoke_instruction, |
| 1398 | size_t arg_vreg_index) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1399 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1400 | size_t input_index = 0; |
| 1401 | for (size_t i = 0; i < arg_vreg_index; ++i, ++input_index) { |
| 1402 | DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments()); |
| 1403 | if (Primitive::Is64BitType(invoke_instruction->InputAt(input_index)->GetType())) { |
| 1404 | ++i; |
| 1405 | DCHECK_NE(i, arg_vreg_index); |
| 1406 | } |
| 1407 | } |
| 1408 | DCHECK_LT(input_index, invoke_instruction->GetNumberOfArguments()); |
| 1409 | return invoke_instruction->InputAt(input_index); |
| 1410 | } |
| 1411 | |
| 1412 | // Try to recognize known simple patterns and replace invoke call with appropriate instructions. |
| 1413 | bool HInliner::TryPatternSubstitution(HInvoke* invoke_instruction, |
| 1414 | ArtMethod* resolved_method, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1415 | HInstruction** return_replacement) { |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1416 | InlineMethod inline_method; |
| 1417 | if (!InlineMethodAnalyser::AnalyseMethodCode(resolved_method, &inline_method)) { |
| 1418 | return false; |
| 1419 | } |
| 1420 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1421 | switch (inline_method.opcode) { |
| 1422 | case kInlineOpNop: |
| 1423 | DCHECK_EQ(invoke_instruction->GetType(), Primitive::kPrimVoid); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1424 | *return_replacement = nullptr; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1425 | break; |
| 1426 | case kInlineOpReturnArg: |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1427 | *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, |
| 1428 | inline_method.d.return_data.arg); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1429 | break; |
| 1430 | case kInlineOpNonWideConst: |
| 1431 | if (resolved_method->GetShorty()[0] == 'L') { |
| 1432 | DCHECK_EQ(inline_method.d.data, 0u); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1433 | *return_replacement = graph_->GetNullConstant(); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1434 | } else { |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1435 | *return_replacement = graph_->GetIntConstant(static_cast<int32_t>(inline_method.d.data)); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1436 | } |
| 1437 | break; |
| 1438 | case kInlineOpIGet: { |
| 1439 | const InlineIGetIPutData& data = inline_method.d.ifield_data; |
| 1440 | if (data.method_is_static || data.object_arg != 0u) { |
| 1441 | // TODO: Needs null check. |
| 1442 | return false; |
| 1443 | } |
| 1444 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1445 | HInstanceFieldGet* iget = CreateInstanceFieldGet(data.field_idx, resolved_method, obj); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1446 | DCHECK_EQ(iget->GetFieldOffset().Uint32Value(), data.field_offset); |
| 1447 | DCHECK_EQ(iget->IsVolatile() ? 1u : 0u, data.is_volatile); |
| 1448 | invoke_instruction->GetBlock()->InsertInstructionBefore(iget, invoke_instruction); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1449 | *return_replacement = iget; |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1450 | break; |
| 1451 | } |
| 1452 | case kInlineOpIPut: { |
| 1453 | const InlineIGetIPutData& data = inline_method.d.ifield_data; |
| 1454 | if (data.method_is_static || data.object_arg != 0u) { |
| 1455 | // TODO: Needs null check. |
| 1456 | return false; |
| 1457 | } |
| 1458 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, data.object_arg); |
| 1459 | HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, data.src_arg); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1460 | HInstanceFieldSet* iput = CreateInstanceFieldSet(data.field_idx, resolved_method, obj, value); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1461 | DCHECK_EQ(iput->GetFieldOffset().Uint32Value(), data.field_offset); |
| 1462 | DCHECK_EQ(iput->IsVolatile() ? 1u : 0u, data.is_volatile); |
| 1463 | invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction); |
| 1464 | if (data.return_arg_plus1 != 0u) { |
| 1465 | size_t return_arg = data.return_arg_plus1 - 1u; |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1466 | *return_replacement = GetInvokeInputForArgVRegIndex(invoke_instruction, return_arg); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1467 | } |
| 1468 | break; |
| 1469 | } |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1470 | case kInlineOpConstructor: { |
| 1471 | const InlineConstructorData& data = inline_method.d.constructor_data; |
| 1472 | // Get the indexes to arrays for easier processing. |
| 1473 | uint16_t iput_field_indexes[] = { |
| 1474 | data.iput0_field_index, data.iput1_field_index, data.iput2_field_index |
| 1475 | }; |
| 1476 | uint16_t iput_args[] = { data.iput0_arg, data.iput1_arg, data.iput2_arg }; |
| 1477 | static_assert(arraysize(iput_args) == arraysize(iput_field_indexes), "Size mismatch"); |
| 1478 | // Count valid field indexes. |
| 1479 | size_t number_of_iputs = 0u; |
| 1480 | while (number_of_iputs != arraysize(iput_field_indexes) && |
| 1481 | iput_field_indexes[number_of_iputs] != DexFile::kDexNoIndex16) { |
| 1482 | // Check that there are no duplicate valid field indexes. |
| 1483 | DCHECK_EQ(0, std::count(iput_field_indexes + number_of_iputs + 1, |
| 1484 | iput_field_indexes + arraysize(iput_field_indexes), |
| 1485 | iput_field_indexes[number_of_iputs])); |
| 1486 | ++number_of_iputs; |
| 1487 | } |
| 1488 | // Check that there are no valid field indexes in the rest of the array. |
| 1489 | DCHECK_EQ(0, std::count_if(iput_field_indexes + number_of_iputs, |
| 1490 | iput_field_indexes + arraysize(iput_field_indexes), |
| 1491 | [](uint16_t index) { return index != DexFile::kDexNoIndex16; })); |
| 1492 | |
| 1493 | // Create HInstanceFieldSet for each IPUT that stores non-zero data. |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1494 | HInstruction* obj = GetInvokeInputForArgVRegIndex(invoke_instruction, /* this */ 0u); |
| 1495 | bool needs_constructor_barrier = false; |
| 1496 | for (size_t i = 0; i != number_of_iputs; ++i) { |
| 1497 | HInstruction* value = GetInvokeInputForArgVRegIndex(invoke_instruction, iput_args[i]); |
Roland Levillain | 1a65388 | 2016-03-18 18:05:57 +0000 | [diff] [blame] | 1498 | if (!value->IsConstant() || !value->AsConstant()->IsZeroBitPattern()) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1499 | uint16_t field_index = iput_field_indexes[i]; |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1500 | bool is_final; |
| 1501 | HInstanceFieldSet* iput = |
| 1502 | CreateInstanceFieldSet(field_index, resolved_method, obj, value, &is_final); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1503 | invoke_instruction->GetBlock()->InsertInstructionBefore(iput, invoke_instruction); |
| 1504 | |
| 1505 | // Check whether the field is final. If it is, we need to add a barrier. |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1506 | if (is_final) { |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1507 | needs_constructor_barrier = true; |
| 1508 | } |
| 1509 | } |
| 1510 | } |
| 1511 | if (needs_constructor_barrier) { |
Igor Murashkin | d01745e | 2017-04-05 16:40:31 -0700 | [diff] [blame] | 1512 | // See CompilerDriver::RequiresConstructorBarrier for more details. |
| 1513 | DCHECK(obj != nullptr) << "only non-static methods can have a constructor fence"; |
| 1514 | |
| 1515 | HConstructorFence* constructor_fence = |
| 1516 | new (graph_->GetArena()) HConstructorFence(obj, kNoDexPc, graph_->GetArena()); |
| 1517 | invoke_instruction->GetBlock()->InsertInstructionBefore(constructor_fence, |
| 1518 | invoke_instruction); |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1519 | } |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1520 | *return_replacement = nullptr; |
Vladimir Marko | 354efa6 | 2016-02-04 19:46:56 +0000 | [diff] [blame] | 1521 | break; |
| 1522 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1523 | default: |
| 1524 | LOG(FATAL) << "UNREACHABLE"; |
| 1525 | UNREACHABLE(); |
| 1526 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1527 | return true; |
| 1528 | } |
| 1529 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1530 | HInstanceFieldGet* HInliner::CreateInstanceFieldGet(uint32_t field_index, |
| 1531 | ArtMethod* referrer, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1532 | HInstruction* obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1533 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1534 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1535 | ArtField* resolved_field = |
| 1536 | class_linker->LookupResolvedField(field_index, referrer, /* is_static */ false); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1537 | DCHECK(resolved_field != nullptr); |
| 1538 | HInstanceFieldGet* iget = new (graph_->GetArena()) HInstanceFieldGet( |
| 1539 | obj, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1540 | resolved_field, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1541 | resolved_field->GetTypeAsPrimitiveType(), |
| 1542 | resolved_field->GetOffset(), |
| 1543 | resolved_field->IsVolatile(), |
| 1544 | field_index, |
| 1545 | resolved_field->GetDeclaringClass()->GetDexClassDefIndex(), |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1546 | *referrer->GetDexFile(), |
Vladimir Marko | adda435 | 2016-01-29 10:24:41 +0000 | [diff] [blame] | 1547 | // Read barrier generates a runtime call in slow path and we need a valid |
| 1548 | // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537. |
| 1549 | /* dex_pc */ 0); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1550 | if (iget->GetType() == Primitive::kPrimNot) { |
Vladimir Marko | 456307a | 2016-04-19 14:12:13 +0000 | [diff] [blame] | 1551 | // Use the same dex_cache that we used for field lookup as the hint_dex_cache. |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1552 | Handle<mirror::DexCache> dex_cache = handles_->NewHandle(referrer->GetDexCache()); |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1553 | ReferenceTypePropagation rtp(graph_, |
| 1554 | outer_compilation_unit_.GetClassLoader(), |
| 1555 | dex_cache, |
| 1556 | handles_, |
| 1557 | /* is_first_run */ false); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1558 | rtp.Visit(iget); |
| 1559 | } |
| 1560 | return iget; |
| 1561 | } |
| 1562 | |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1563 | HInstanceFieldSet* HInliner::CreateInstanceFieldSet(uint32_t field_index, |
| 1564 | ArtMethod* referrer, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1565 | HInstruction* obj, |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1566 | HInstruction* value, |
| 1567 | bool* is_final) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1568 | REQUIRES_SHARED(Locks::mutator_lock_) { |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1569 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 1570 | ArtField* resolved_field = |
| 1571 | class_linker->LookupResolvedField(field_index, referrer, /* is_static */ false); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1572 | DCHECK(resolved_field != nullptr); |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1573 | if (is_final != nullptr) { |
| 1574 | // This information is needed only for constructors. |
| 1575 | DCHECK(referrer->IsConstructor()); |
| 1576 | *is_final = resolved_field->IsFinal(); |
| 1577 | } |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1578 | HInstanceFieldSet* iput = new (graph_->GetArena()) HInstanceFieldSet( |
| 1579 | obj, |
| 1580 | value, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 1581 | resolved_field, |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1582 | resolved_field->GetTypeAsPrimitiveType(), |
| 1583 | resolved_field->GetOffset(), |
| 1584 | resolved_field->IsVolatile(), |
| 1585 | field_index, |
| 1586 | resolved_field->GetDeclaringClass()->GetDexClassDefIndex(), |
Vladimir Marko | f44d36c | 2017-03-14 14:18:46 +0000 | [diff] [blame] | 1587 | *referrer->GetDexFile(), |
Vladimir Marko | adda435 | 2016-01-29 10:24:41 +0000 | [diff] [blame] | 1588 | // Read barrier generates a runtime call in slow path and we need a valid |
| 1589 | // dex pc for the associated stack map. 0 is bogus but valid. Bug: 26854537. |
| 1590 | /* dex_pc */ 0); |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1591 | return iput; |
| 1592 | } |
Nicolas Geoffray | d9994f0 | 2016-02-11 17:35:55 +0000 | [diff] [blame] | 1593 | |
Vladimir Marko | b1d0ee1 | 2017-04-20 19:50:32 +0100 | [diff] [blame] | 1594 | template <typename T> |
| 1595 | static inline Handle<T> NewHandleIfDifferent(T* object, |
| 1596 | Handle<T> hint, |
| 1597 | VariableSizedHandleScope* handles) |
| 1598 | REQUIRES_SHARED(Locks::mutator_lock_) { |
| 1599 | return (object != hint.Get()) ? handles->NewHandle(object) : hint; |
| 1600 | } |
| 1601 | |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1602 | bool HInliner::TryBuildAndInlineHelper(HInvoke* invoke_instruction, |
| 1603 | ArtMethod* resolved_method, |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1604 | ReferenceTypeInfo receiver_type, |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1605 | bool same_dex_file, |
| 1606 | HInstruction** return_replacement) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1607 | DCHECK(!(resolved_method->IsStatic() && receiver_type.IsValid())); |
Nicolas Geoffray | c0365b1 | 2015-03-18 18:31:52 +0000 | [diff] [blame] | 1608 | ScopedObjectAccess soa(Thread::Current()); |
| 1609 | const DexFile::CodeItem* code_item = resolved_method->GetCodeItem(); |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1610 | const DexFile& callee_dex_file = *resolved_method->GetDexFile(); |
| 1611 | uint32_t method_index = resolved_method->GetDexMethodIndex(); |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1612 | ClassLinker* class_linker = caller_compilation_unit_.GetClassLinker(); |
Vladimir Marko | b1d0ee1 | 2017-04-20 19:50:32 +0100 | [diff] [blame] | 1613 | Handle<mirror::DexCache> dex_cache = NewHandleIfDifferent(resolved_method->GetDexCache(), |
| 1614 | caller_compilation_unit_.GetDexCache(), |
| 1615 | handles_); |
| 1616 | Handle<mirror::ClassLoader> class_loader = |
| 1617 | NewHandleIfDifferent(resolved_method->GetDeclaringClass()->GetClassLoader(), |
| 1618 | caller_compilation_unit_.GetClassLoader(), |
| 1619 | handles_); |
Nicolas Geoffray | f1aedb1 | 2016-07-28 03:49:14 +0100 | [diff] [blame] | 1620 | |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1621 | DexCompilationUnit dex_compilation_unit( |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1622 | class_loader, |
Nicolas Geoffray | 5b82d33 | 2016-02-18 14:22:32 +0000 | [diff] [blame] | 1623 | class_linker, |
| 1624 | callee_dex_file, |
| 1625 | code_item, |
| 1626 | resolved_method->GetDeclaringClass()->GetDexClassDefIndex(), |
| 1627 | method_index, |
| 1628 | resolved_method->GetAccessFlags(), |
| 1629 | /* verified_method */ nullptr, |
| 1630 | dex_cache); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1631 | |
Nicolas Geoffray | 5e4e11e | 2016-09-22 13:17:41 +0100 | [diff] [blame] | 1632 | InvokeType invoke_type = invoke_instruction->GetInvokeType(); |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 1633 | if (invoke_type == kInterface) { |
| 1634 | // We have statically resolved the dispatch. To please the class linker |
| 1635 | // at runtime, we change this call as if it was a virtual call. |
| 1636 | invoke_type = kVirtual; |
| 1637 | } |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1638 | |
| 1639 | const int32_t caller_instruction_counter = graph_->GetCurrentInstructionId(); |
Nicolas Geoffray | e0fe7ae | 2015-03-09 10:02:49 +0000 | [diff] [blame] | 1640 | HGraph* callee_graph = new (graph_->GetArena()) HGraph( |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1641 | graph_->GetArena(), |
Guillaume "Vermeille" Sanchez | ae09d2d | 2015-05-29 10:52:55 +0100 | [diff] [blame] | 1642 | callee_dex_file, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1643 | method_index, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 1644 | compiler_driver_->GetInstructionSet(), |
Nicolas Geoffray | 3507105 | 2015-06-09 15:43:38 +0100 | [diff] [blame] | 1645 | invoke_type, |
Nicolas Geoffray | 0a23d74 | 2015-05-07 11:57:35 +0100 | [diff] [blame] | 1646 | graph_->IsDebuggable(), |
Nicolas Geoffray | b331feb | 2016-02-05 16:51:53 +0000 | [diff] [blame] | 1647 | /* osr */ false, |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1648 | caller_instruction_counter); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 1649 | callee_graph->SetArtMethod(resolved_method); |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 1650 | |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame] | 1651 | // When they are needed, allocate `inline_stats_` on the Arena instead |
Roland Levillain | a8013fd | 2016-04-04 15:34:31 +0100 | [diff] [blame] | 1652 | // of on the stack, as Clang might produce a stack frame too large |
| 1653 | // for this function, that would not fit the requirements of the |
| 1654 | // `-Wframe-larger-than` option. |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame] | 1655 | if (stats_ != nullptr) { |
| 1656 | // Reuse one object for all inline attempts from this caller to keep Arena memory usage low. |
| 1657 | if (inline_stats_ == nullptr) { |
| 1658 | void* storage = graph_->GetArena()->Alloc<OptimizingCompilerStats>(kArenaAllocMisc); |
| 1659 | inline_stats_ = new (storage) OptimizingCompilerStats; |
| 1660 | } else { |
| 1661 | inline_stats_->Reset(); |
| 1662 | } |
| 1663 | } |
David Brazdil | 5e8b137 | 2015-01-23 14:39:08 +0000 | [diff] [blame] | 1664 | HGraphBuilder builder(callee_graph, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1665 | &dex_compilation_unit, |
| 1666 | &outer_compilation_unit_, |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1667 | resolved_method->GetDexFile(), |
David Brazdil | 86ea7ee | 2016-02-16 09:26:07 +0000 | [diff] [blame] | 1668 | *code_item, |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1669 | compiler_driver_, |
Nicolas Geoffray | 83c8e27 | 2017-01-31 14:36:37 +0000 | [diff] [blame] | 1670 | codegen_, |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame] | 1671 | inline_stats_, |
Vladimir Marko | 97d7e1c | 2016-10-04 14:44:28 +0100 | [diff] [blame] | 1672 | resolved_method->GetQuickenedInfo(class_linker->GetImagePointerSize()), |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 1673 | dex_cache, |
| 1674 | handles_); |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1675 | |
David Brazdil | dee58d6 | 2016-04-07 09:54:26 +0000 | [diff] [blame] | 1676 | if (builder.BuildGraph() != kAnalysisSuccess) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1677 | LOG_FAIL(kNotInlinedCannotBuild) |
| 1678 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1679 | << " could not be built, so cannot be inlined"; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1680 | return false; |
| 1681 | } |
| 1682 | |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 1683 | if (!RegisterAllocator::CanAllocateRegistersFor(*callee_graph, |
| 1684 | compiler_driver_->GetInstructionSet())) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1685 | LOG_FAIL(kNotInlinedRegisterAllocator) |
| 1686 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1687 | << " cannot be inlined because of the register allocator"; |
Nicolas Geoffray | 259136f | 2014-12-17 23:21:58 +0000 | [diff] [blame] | 1688 | return false; |
| 1689 | } |
| 1690 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1691 | size_t parameter_index = 0; |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1692 | bool run_rtp = false; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1693 | for (HInstructionIterator instructions(callee_graph->GetEntryBlock()->GetInstructions()); |
| 1694 | !instructions.Done(); |
| 1695 | instructions.Advance()) { |
| 1696 | HInstruction* current = instructions.Current(); |
| 1697 | if (current->IsParameterValue()) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1698 | HInstruction* argument = invoke_instruction->InputAt(parameter_index); |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1699 | if (argument->IsNullConstant()) { |
| 1700 | current->ReplaceWith(callee_graph->GetNullConstant()); |
| 1701 | } else if (argument->IsIntConstant()) { |
| 1702 | current->ReplaceWith(callee_graph->GetIntConstant(argument->AsIntConstant()->GetValue())); |
| 1703 | } else if (argument->IsLongConstant()) { |
| 1704 | current->ReplaceWith(callee_graph->GetLongConstant(argument->AsLongConstant()->GetValue())); |
| 1705 | } else if (argument->IsFloatConstant()) { |
| 1706 | current->ReplaceWith( |
| 1707 | callee_graph->GetFloatConstant(argument->AsFloatConstant()->GetValue())); |
| 1708 | } else if (argument->IsDoubleConstant()) { |
| 1709 | current->ReplaceWith( |
| 1710 | callee_graph->GetDoubleConstant(argument->AsDoubleConstant()->GetValue())); |
| 1711 | } else if (argument->GetType() == Primitive::kPrimNot) { |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1712 | if (!resolved_method->IsStatic() && parameter_index == 0 && receiver_type.IsValid()) { |
| 1713 | run_rtp = true; |
| 1714 | current->SetReferenceTypeInfo(receiver_type); |
| 1715 | } else { |
| 1716 | current->SetReferenceTypeInfo(argument->GetReferenceTypeInfo()); |
| 1717 | } |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1718 | current->AsParameterValue()->SetCanBeNull(argument->CanBeNull()); |
| 1719 | } |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1720 | ++parameter_index; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1721 | } |
| 1722 | } |
| 1723 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1724 | // We have replaced formal arguments with actual arguments. If actual types |
| 1725 | // are more specific than the declared ones, run RTP again on the inner graph. |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1726 | if (run_rtp || ArgumentTypesMoreSpecific(invoke_instruction, resolved_method)) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1727 | ReferenceTypePropagation(callee_graph, |
Vladimir Marko | 8d6768d | 2017-03-14 10:13:21 +0000 | [diff] [blame] | 1728 | outer_compilation_unit_.GetClassLoader(), |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1729 | dex_compilation_unit.GetDexCache(), |
| 1730 | handles_, |
| 1731 | /* is_first_run */ false).Run(); |
| 1732 | } |
| 1733 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1734 | RunOptimizations(callee_graph, code_item, dex_compilation_unit); |
Nicolas Geoffray | ef87c5d | 2015-01-30 12:41:14 +0000 | [diff] [blame] | 1735 | |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1736 | HBasicBlock* exit_block = callee_graph->GetExitBlock(); |
| 1737 | if (exit_block == nullptr) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1738 | LOG_FAIL(kNotInlinedInfiniteLoop) |
| 1739 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1740 | << " could not be inlined because it has an infinite loop"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1741 | return false; |
| 1742 | } |
| 1743 | |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 1744 | bool has_one_return = false; |
Vladimir Marko | 6058455 | 2015-09-03 13:35:12 +0000 | [diff] [blame] | 1745 | for (HBasicBlock* predecessor : exit_block->GetPredecessors()) { |
| 1746 | if (predecessor->GetLastInstruction()->IsThrow()) { |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 1747 | if (invoke_instruction->GetBlock()->IsTryBlock()) { |
| 1748 | // TODO(ngeoffray): Support adding HTryBoundary in Hgraph::InlineInto. |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1749 | LOG_FAIL(kNotInlinedTryCatch) |
| 1750 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1751 | << " could not be inlined because one branch always throws and" |
| 1752 | << " caller is in a try/catch block"; |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 1753 | return false; |
| 1754 | } else if (graph_->GetExitBlock() == nullptr) { |
| 1755 | // TODO(ngeoffray): Support adding HExit in the caller graph. |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1756 | LOG_FAIL(kNotInlinedInfiniteLoop) |
| 1757 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1758 | << " could not be inlined because one branch always throws and" |
| 1759 | << " caller does not have an exit block"; |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 1760 | return false; |
Nicolas Geoffray | 1eede6a | 2017-03-02 16:14:53 +0000 | [diff] [blame] | 1761 | } else if (graph_->HasIrreducibleLoops()) { |
| 1762 | // TODO(ngeoffray): Support re-computing loop information to graphs with |
| 1763 | // irreducible loops? |
| 1764 | VLOG(compiler) << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1765 | << " could not be inlined because one branch always throws and" |
| 1766 | << " caller has irreducible loops"; |
| 1767 | return false; |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 1768 | } |
| 1769 | } else { |
| 1770 | has_one_return = true; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1771 | } |
| 1772 | } |
Nicolas Geoffray | fdb7d63 | 2017-02-08 15:07:18 +0000 | [diff] [blame] | 1773 | |
| 1774 | if (!has_one_return) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1775 | LOG_FAIL(kNotInlinedAlwaysThrows) |
| 1776 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1777 | << " could not be inlined because it always throws"; |
Nicolas Geoffray | d23eeef | 2015-05-18 22:31:29 +0100 | [diff] [blame] | 1778 | return false; |
| 1779 | } |
| 1780 | |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1781 | size_t number_of_instructions = 0; |
Vladimir Marko | 2c45bc9 | 2016-10-25 16:54:12 +0100 | [diff] [blame] | 1782 | // Skip the entry block, it does not contain instructions that prevent inlining. |
| 1783 | for (HBasicBlock* block : callee_graph->GetReversePostOrderSkipEntryBlock()) { |
David Sehr | c757dec | 2016-11-04 15:48:34 -0700 | [diff] [blame] | 1784 | if (block->IsLoopHeader()) { |
| 1785 | if (block->GetLoopInformation()->IsIrreducible()) { |
| 1786 | // Don't inline methods with irreducible loops, they could prevent some |
| 1787 | // optimizations to run. |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1788 | LOG_FAIL(kNotInlinedIrreducibleLoop) |
| 1789 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1790 | << " could not be inlined because it contains an irreducible loop"; |
David Sehr | c757dec | 2016-11-04 15:48:34 -0700 | [diff] [blame] | 1791 | return false; |
| 1792 | } |
| 1793 | if (!block->GetLoopInformation()->HasExitEdge()) { |
| 1794 | // Don't inline methods with loops without exit, since they cause the |
| 1795 | // loop information to be computed incorrectly when updating after |
| 1796 | // inlining. |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1797 | LOG_FAIL(kNotInlinedLoopWithoutExit) |
| 1798 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1799 | << " could not be inlined because it contains a loop with no exit"; |
David Sehr | c757dec | 2016-11-04 15:48:34 -0700 | [diff] [blame] | 1800 | return false; |
| 1801 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1802 | } |
| 1803 | |
| 1804 | for (HInstructionIterator instr_it(block->GetInstructions()); |
| 1805 | !instr_it.Done(); |
| 1806 | instr_it.Advance()) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1807 | if (++number_of_instructions >= inlining_budget_) { |
| 1808 | LOG_FAIL(kNotInlinedInstructionBudget) |
| 1809 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1810 | << " is not inlined because the outer method has reached" |
| 1811 | << " its instruction budget limit."; |
Nicolas Geoffray | e418dda | 2015-08-11 20:03:09 -0700 | [diff] [blame] | 1812 | return false; |
| 1813 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1814 | HInstruction* current = instr_it.Current(); |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1815 | if (current->NeedsEnvironment() && |
| 1816 | (total_number_of_dex_registers_ >= kMaximumNumberOfCumulatedDexRegisters)) { |
| 1817 | LOG_FAIL(kNotInlinedEnvironmentBudget) |
| 1818 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1819 | << " is not inlined because its caller has reached" |
| 1820 | << " its environment budget limit."; |
Nicolas Geoffray | 5949fa0 | 2015-12-18 10:57:10 +0000 | [diff] [blame] | 1821 | return false; |
| 1822 | } |
Nicolas Geoffray | 276d9da | 2015-02-02 18:24:11 +0000 | [diff] [blame] | 1823 | |
Nicolas Geoffray | fbdfa6d | 2017-02-03 10:43:13 +0000 | [diff] [blame] | 1824 | if (current->NeedsEnvironment() && |
| 1825 | !CanEncodeInlinedMethodInStackMap(*caller_compilation_unit_.GetDexFile(), |
| 1826 | resolved_method)) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1827 | LOG_FAIL(kNotInlinedStackMaps) |
| 1828 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1829 | << " could not be inlined because " << current->DebugName() |
| 1830 | << " needs an environment, is in a different dex file" |
| 1831 | << ", and cannot be encoded in the stack maps."; |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1832 | return false; |
| 1833 | } |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1834 | |
Vladimir Marko | dc151b2 | 2015-10-15 18:02:30 +0100 | [diff] [blame] | 1835 | if (!same_dex_file && current->NeedsDexCacheOfDeclaringClass()) { |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1836 | LOG_FAIL(kNotInlinedDexCache) |
| 1837 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1838 | << " could not be inlined because " << current->DebugName() |
| 1839 | << " it is in a different dex file and requires access to the dex cache"; |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 1840 | return false; |
| 1841 | } |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1842 | |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1843 | if (current->IsUnresolvedStaticFieldGet() || |
| 1844 | current->IsUnresolvedInstanceFieldGet() || |
| 1845 | current->IsUnresolvedStaticFieldSet() || |
| 1846 | current->IsUnresolvedInstanceFieldSet()) { |
| 1847 | // Entrypoint for unresolved fields does not handle inlined frames. |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1848 | LOG_FAIL(kNotInlinedUnresolvedEntrypoint) |
| 1849 | << "Method " << callee_dex_file.PrettyMethod(method_index) |
| 1850 | << " could not be inlined because it is using an unresolved" |
| 1851 | << " entrypoint"; |
Nicolas Geoffray | d930929 | 2015-10-31 22:21:31 +0000 | [diff] [blame] | 1852 | return false; |
| 1853 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 1854 | } |
| 1855 | } |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1856 | DCHECK_EQ(caller_instruction_counter, graph_->GetCurrentInstructionId()) |
| 1857 | << "No instructions can be added to the outer graph while inner graph is being built"; |
| 1858 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1859 | // Inline the callee graph inside the caller graph. |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1860 | const int32_t callee_instruction_counter = callee_graph->GetCurrentInstructionId(); |
| 1861 | graph_->SetCurrentInstructionId(callee_instruction_counter); |
Nicolas Geoffray | 55bd749 | 2016-02-16 15:37:12 +0000 | [diff] [blame] | 1862 | *return_replacement = callee_graph->InlineInto(graph_, invoke_instruction); |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1863 | // Update our budget for other inlining attempts in `caller_graph`. |
| 1864 | total_number_of_instructions_ += number_of_instructions; |
| 1865 | UpdateInliningBudget(); |
David Brazdil | 3f52306 | 2016-02-29 16:53:33 +0000 | [diff] [blame] | 1866 | |
| 1867 | DCHECK_EQ(callee_instruction_counter, callee_graph->GetCurrentInstructionId()) |
| 1868 | << "No instructions can be added to the inner graph during inlining into the outer graph"; |
| 1869 | |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame] | 1870 | if (stats_ != nullptr) { |
| 1871 | DCHECK(inline_stats_ != nullptr); |
| 1872 | inline_stats_->AddTo(stats_); |
| 1873 | } |
| 1874 | |
Vladimir Marko | be10e8e | 2016-01-22 12:09:44 +0000 | [diff] [blame] | 1875 | return true; |
| 1876 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 1877 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1878 | void HInliner::RunOptimizations(HGraph* callee_graph, |
| 1879 | const DexFile::CodeItem* code_item, |
| 1880 | const DexCompilationUnit& dex_compilation_unit) { |
Nicolas Geoffray | 93a18c5 | 2016-04-22 13:16:14 +0100 | [diff] [blame] | 1881 | // Note: if the outermost_graph_ is being compiled OSR, we should not run any |
| 1882 | // optimization that could lead to a HDeoptimize. The following optimizations do not. |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame] | 1883 | HDeadCodeElimination dce(callee_graph, inline_stats_, "dead_code_elimination$inliner"); |
Andreas Gampe | ca620d7 | 2016-11-08 08:09:33 -0800 | [diff] [blame] | 1884 | HConstantFolding fold(callee_graph, "constant_folding$inliner"); |
Nicolas Geoffray | 22384ae | 2016-12-12 22:33:36 +0000 | [diff] [blame] | 1885 | HSharpening sharpening(callee_graph, codegen_, dex_compilation_unit, compiler_driver_, handles_); |
Vladimir Marko | 6597946 | 2017-05-19 17:25:12 +0100 | [diff] [blame] | 1886 | InstructionSimplifier simplify(callee_graph, codegen_, compiler_driver_, inline_stats_); |
Vladimir Marko | 438709f | 2017-02-23 18:56:13 +0000 | [diff] [blame] | 1887 | IntrinsicsRecognizer intrinsics(callee_graph, inline_stats_); |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1888 | |
| 1889 | HOptimization* optimizations[] = { |
| 1890 | &intrinsics, |
| 1891 | &sharpening, |
| 1892 | &simplify, |
| 1893 | &fold, |
| 1894 | &dce, |
| 1895 | }; |
| 1896 | |
| 1897 | for (size_t i = 0; i < arraysize(optimizations); ++i) { |
| 1898 | HOptimization* optimization = optimizations[i]; |
| 1899 | optimization->Run(); |
| 1900 | } |
| 1901 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1902 | // Bail early for pathological cases on the environment (for example recursive calls, |
| 1903 | // or too large environment). |
| 1904 | if (total_number_of_dex_registers_ >= kMaximumNumberOfCumulatedDexRegisters) { |
| 1905 | LOG_NOTE() << "Calls in " << callee_graph->GetArtMethod()->PrettyMethod() |
| 1906 | << " will not be inlined because the outer method has reached" |
| 1907 | << " its environment budget limit."; |
| 1908 | return; |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1909 | } |
| 1910 | |
Nicolas Geoffray | f6d4668 | 2017-02-28 17:41:45 +0000 | [diff] [blame] | 1911 | // Bail early if we know we already are over the limit. |
| 1912 | size_t number_of_instructions = CountNumberOfInstructions(callee_graph); |
| 1913 | if (number_of_instructions > inlining_budget_) { |
| 1914 | LOG_NOTE() << "Calls in " << callee_graph->GetArtMethod()->PrettyMethod() |
| 1915 | << " will not be inlined because the outer method has reached" |
| 1916 | << " its instruction budget limit. " << number_of_instructions; |
| 1917 | return; |
| 1918 | } |
| 1919 | |
| 1920 | HInliner inliner(callee_graph, |
| 1921 | outermost_graph_, |
| 1922 | codegen_, |
| 1923 | outer_compilation_unit_, |
| 1924 | dex_compilation_unit, |
| 1925 | compiler_driver_, |
| 1926 | handles_, |
| 1927 | inline_stats_, |
| 1928 | total_number_of_dex_registers_ + code_item->registers_size_, |
| 1929 | total_number_of_instructions_ + number_of_instructions, |
| 1930 | this, |
| 1931 | depth_ + 1); |
| 1932 | inliner.Run(); |
Roland Levillain | a3aef2e | 2016-04-06 17:45:58 +0100 | [diff] [blame] | 1933 | } |
| 1934 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1935 | static bool IsReferenceTypeRefinement(ReferenceTypeInfo declared_rti, |
| 1936 | bool declared_can_be_null, |
| 1937 | HInstruction* actual_obj) |
Andreas Gampe | bdf7f1c | 2016-08-30 16:38:47 -0700 | [diff] [blame] | 1938 | REQUIRES_SHARED(Locks::mutator_lock_) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1939 | if (declared_can_be_null && !actual_obj->CanBeNull()) { |
| 1940 | return true; |
| 1941 | } |
| 1942 | |
| 1943 | ReferenceTypeInfo actual_rti = actual_obj->GetReferenceTypeInfo(); |
| 1944 | return (actual_rti.IsExact() && !declared_rti.IsExact()) || |
Nicolas Geoffray | 0f001b7 | 2017-01-04 16:46:23 +0000 | [diff] [blame] | 1945 | declared_rti.IsStrictSupertypeOf(actual_rti); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1946 | } |
| 1947 | |
| 1948 | ReferenceTypeInfo HInliner::GetClassRTI(mirror::Class* klass) { |
| 1949 | return ReferenceTypePropagation::IsAdmissible(klass) |
| 1950 | ? ReferenceTypeInfo::Create(handles_->NewHandle(klass)) |
| 1951 | : graph_->GetInexactObjectRti(); |
| 1952 | } |
| 1953 | |
| 1954 | bool HInliner::ArgumentTypesMoreSpecific(HInvoke* invoke_instruction, ArtMethod* resolved_method) { |
| 1955 | // If this is an instance call, test whether the type of the `this` argument |
| 1956 | // is more specific than the class which declares the method. |
| 1957 | if (!resolved_method->IsStatic()) { |
| 1958 | if (IsReferenceTypeRefinement(GetClassRTI(resolved_method->GetDeclaringClass()), |
| 1959 | /* declared_can_be_null */ false, |
| 1960 | invoke_instruction->InputAt(0u))) { |
| 1961 | return true; |
| 1962 | } |
| 1963 | } |
| 1964 | |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1965 | // Iterate over the list of parameter types and test whether any of the |
| 1966 | // actual inputs has a more specific reference type than the type declared in |
| 1967 | // the signature. |
| 1968 | const DexFile::TypeList* param_list = resolved_method->GetParameterTypeList(); |
| 1969 | for (size_t param_idx = 0, |
| 1970 | input_idx = resolved_method->IsStatic() ? 0 : 1, |
| 1971 | e = (param_list == nullptr ? 0 : param_list->Size()); |
| 1972 | param_idx < e; |
| 1973 | ++param_idx, ++input_idx) { |
| 1974 | HInstruction* input = invoke_instruction->InputAt(input_idx); |
| 1975 | if (input->GetType() == Primitive::kPrimNot) { |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1976 | mirror::Class* param_cls = resolved_method->GetClassFromTypeIndex( |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1977 | param_list->GetTypeItem(param_idx).type_idx_, |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 1978 | /* resolve */ false); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1979 | if (IsReferenceTypeRefinement(GetClassRTI(param_cls), |
| 1980 | /* declared_can_be_null */ true, |
| 1981 | input)) { |
| 1982 | return true; |
| 1983 | } |
| 1984 | } |
| 1985 | } |
| 1986 | |
| 1987 | return false; |
| 1988 | } |
| 1989 | |
| 1990 | bool HInliner::ReturnTypeMoreSpecific(HInvoke* invoke_instruction, |
| 1991 | HInstruction* return_replacement) { |
Alex Light | 68289a5 | 2015-12-15 17:30:30 -0800 | [diff] [blame] | 1992 | // 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] | 1993 | if (return_replacement != nullptr) { |
| 1994 | if (return_replacement->GetType() == Primitive::kPrimNot) { |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 1995 | // Test if the return type is a refinement of the declared return type. |
| 1996 | if (IsReferenceTypeRefinement(invoke_instruction->GetReferenceTypeInfo(), |
| 1997 | /* declared_can_be_null */ true, |
| 1998 | return_replacement)) { |
| 1999 | return true; |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 2000 | } else if (return_replacement->IsInstanceFieldGet()) { |
| 2001 | HInstanceFieldGet* field_get = return_replacement->AsInstanceFieldGet(); |
| 2002 | ClassLinker* class_linker = Runtime::Current()->GetClassLinker(); |
| 2003 | if (field_get->GetFieldInfo().GetField() == |
| 2004 | class_linker->GetClassRoot(ClassLinker::kJavaLangObject)->GetInstanceField(0)) { |
| 2005 | return true; |
| 2006 | } |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 2007 | } |
| 2008 | } else if (return_replacement->IsInstanceOf()) { |
| 2009 | // Inlining InstanceOf into an If may put a tighter bound on reference types. |
| 2010 | return true; |
| 2011 | } |
| 2012 | } |
| 2013 | |
| 2014 | return false; |
| 2015 | } |
| 2016 | |
| 2017 | void HInliner::FixUpReturnReferenceType(ArtMethod* resolved_method, |
| 2018 | HInstruction* return_replacement) { |
| 2019 | if (return_replacement != nullptr) { |
| 2020 | if (return_replacement->GetType() == Primitive::kPrimNot) { |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 2021 | if (!return_replacement->GetReferenceTypeInfo().IsValid()) { |
| 2022 | // Make sure that we have a valid type for the return. We may get an invalid one when |
| 2023 | // we inline invokes with multiple branches and create a Phi for the result. |
| 2024 | // TODO: we could be more precise by merging the phi inputs but that requires |
| 2025 | // some functionality from the reference type propagation. |
| 2026 | DCHECK(return_replacement->IsPhi()); |
Vladimir Marko | 942fd31 | 2017-01-16 20:52:19 +0000 | [diff] [blame] | 2027 | mirror::Class* cls = resolved_method->GetReturnType(false /* resolve */); |
David Brazdil | 94ab38f | 2016-06-21 17:48:19 +0100 | [diff] [blame] | 2028 | return_replacement->SetReferenceTypeInfo(GetClassRTI(cls)); |
Nicolas Geoffray | 73be1e8 | 2015-09-17 15:22:56 +0100 | [diff] [blame] | 2029 | } |
Calin Juravle | cdfed3d | 2015-10-26 14:05:01 +0000 | [diff] [blame] | 2030 | } |
Calin Juravle | 2e76830 | 2015-07-28 14:41:11 +0000 | [diff] [blame] | 2031 | } |
Nicolas Geoffray | e53798a | 2014-12-01 10:31:54 +0000 | [diff] [blame] | 2032 | } |
| 2033 | |
| 2034 | } // namespace art |