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