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