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