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