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