xref: /aosp_15_r20/art/compiler/optimizing/sharpening.cc (revision 795d594fd825385562da6b089ea9b2033f3abf5a)
1*795d594fSAndroid Build Coastguard Worker /*
2*795d594fSAndroid Build Coastguard Worker  * Copyright (C) 2015 The Android Open Source Project
3*795d594fSAndroid Build Coastguard Worker  *
4*795d594fSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*795d594fSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*795d594fSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*795d594fSAndroid Build Coastguard Worker  *
8*795d594fSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*795d594fSAndroid Build Coastguard Worker  *
10*795d594fSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*795d594fSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*795d594fSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*795d594fSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*795d594fSAndroid Build Coastguard Worker  * limitations under the License.
15*795d594fSAndroid Build Coastguard Worker  */
16*795d594fSAndroid Build Coastguard Worker 
17*795d594fSAndroid Build Coastguard Worker #include "sharpening.h"
18*795d594fSAndroid Build Coastguard Worker 
19*795d594fSAndroid Build Coastguard Worker #include "art_method-inl.h"
20*795d594fSAndroid Build Coastguard Worker #include "base/casts.h"
21*795d594fSAndroid Build Coastguard Worker #include "base/logging.h"
22*795d594fSAndroid Build Coastguard Worker #include "base/pointer_size.h"
23*795d594fSAndroid Build Coastguard Worker #include "class_linker.h"
24*795d594fSAndroid Build Coastguard Worker #include "code_generator.h"
25*795d594fSAndroid Build Coastguard Worker #include "driver/compiler_options.h"
26*795d594fSAndroid Build Coastguard Worker #include "driver/dex_compilation_unit.h"
27*795d594fSAndroid Build Coastguard Worker #include "gc/heap.h"
28*795d594fSAndroid Build Coastguard Worker #include "gc/space/image_space.h"
29*795d594fSAndroid Build Coastguard Worker #include "handle_scope-inl.h"
30*795d594fSAndroid Build Coastguard Worker #include "jit/jit.h"
31*795d594fSAndroid Build Coastguard Worker #include "mirror/dex_cache.h"
32*795d594fSAndroid Build Coastguard Worker #include "mirror/string.h"
33*795d594fSAndroid Build Coastguard Worker #include "nodes.h"
34*795d594fSAndroid Build Coastguard Worker #include "runtime.h"
35*795d594fSAndroid Build Coastguard Worker #include "scoped_thread_state_change-inl.h"
36*795d594fSAndroid Build Coastguard Worker 
37*795d594fSAndroid Build Coastguard Worker namespace art HIDDEN {
38*795d594fSAndroid Build Coastguard Worker 
IsInBootImage(ArtMethod * method)39*795d594fSAndroid Build Coastguard Worker static bool IsInBootImage(ArtMethod* method) {
40*795d594fSAndroid Build Coastguard Worker   gc::Heap* heap = Runtime::Current()->GetHeap();
41*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(heap->IsBootImageAddress(method),
42*795d594fSAndroid Build Coastguard Worker             std::any_of(heap->GetBootImageSpaces().begin(),
43*795d594fSAndroid Build Coastguard Worker                         heap->GetBootImageSpaces().end(),
44*795d594fSAndroid Build Coastguard Worker                         [=](gc::space::ImageSpace* space) REQUIRES_SHARED(Locks::mutator_lock_) {
45*795d594fSAndroid Build Coastguard Worker                           return space->GetImageHeader().GetMethodsSection().Contains(
46*795d594fSAndroid Build Coastguard Worker                               reinterpret_cast<uint8_t*>(method) - space->Begin());
47*795d594fSAndroid Build Coastguard Worker                         }));
48*795d594fSAndroid Build Coastguard Worker   return heap->IsBootImageAddress(method);
49*795d594fSAndroid Build Coastguard Worker }
50*795d594fSAndroid Build Coastguard Worker 
ImageAOTCanEmbedMethod(ArtMethod * method,const CompilerOptions & compiler_options)51*795d594fSAndroid Build Coastguard Worker static bool ImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
52*795d594fSAndroid Build Coastguard Worker   DCHECK(compiler_options.IsBootImage() ||
53*795d594fSAndroid Build Coastguard Worker          compiler_options.IsBootImageExtension() ||
54*795d594fSAndroid Build Coastguard Worker          compiler_options.IsAppImage());
55*795d594fSAndroid Build Coastguard Worker   ScopedObjectAccess soa(Thread::Current());
56*795d594fSAndroid Build Coastguard Worker   ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
57*795d594fSAndroid Build Coastguard Worker   DCHECK(klass != nullptr);
58*795d594fSAndroid Build Coastguard Worker   const DexFile& dex_file = klass->GetDexFile();
59*795d594fSAndroid Build Coastguard Worker   return compiler_options.IsImageClass(dex_file.GetTypeDescriptor(klass->GetDexTypeIndex()));
60*795d594fSAndroid Build Coastguard Worker }
61*795d594fSAndroid Build Coastguard Worker 
SharpenLoadMethod(ArtMethod * callee,bool has_method_id,bool for_interface_call,CodeGenerator * codegen)62*795d594fSAndroid Build Coastguard Worker HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenLoadMethod(
63*795d594fSAndroid Build Coastguard Worker     ArtMethod* callee,
64*795d594fSAndroid Build Coastguard Worker     bool has_method_id,
65*795d594fSAndroid Build Coastguard Worker     bool for_interface_call,
66*795d594fSAndroid Build Coastguard Worker     CodeGenerator* codegen) {
67*795d594fSAndroid Build Coastguard Worker   if (kIsDebugBuild) {
68*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());  // Required for `IsStringConstructor()` below.
69*795d594fSAndroid Build Coastguard Worker     DCHECK(callee != nullptr);
70*795d594fSAndroid Build Coastguard Worker     DCHECK(!callee->IsStringConstructor());
71*795d594fSAndroid Build Coastguard Worker   }
72*795d594fSAndroid Build Coastguard Worker 
73*795d594fSAndroid Build Coastguard Worker   MethodLoadKind method_load_kind;
74*795d594fSAndroid Build Coastguard Worker   CodePtrLocation code_ptr_location;
75*795d594fSAndroid Build Coastguard Worker   uint64_t method_load_data = 0u;
76*795d594fSAndroid Build Coastguard Worker 
77*795d594fSAndroid Build Coastguard Worker   // Note: we never call an ArtMethod through a known code pointer, as
78*795d594fSAndroid Build Coastguard Worker   // we do not want to keep on invoking it if it gets deoptimized. This
79*795d594fSAndroid Build Coastguard Worker   // applies to both AOT and JIT.
80*795d594fSAndroid Build Coastguard Worker   // This also avoids having to find out if the code pointer of an ArtMethod
81*795d594fSAndroid Build Coastguard Worker   // is the resolution trampoline (for ensuring the class is initialized), or
82*795d594fSAndroid Build Coastguard Worker   // the interpreter entrypoint. Such code pointers we do not want to call
83*795d594fSAndroid Build Coastguard Worker   // directly.
84*795d594fSAndroid Build Coastguard Worker   // Only in the case of a recursive call can we call directly, as we know the
85*795d594fSAndroid Build Coastguard Worker   // class is initialized already or being initialized, and the call will not
86*795d594fSAndroid Build Coastguard Worker   // be invoked once the method is deoptimized.
87*795d594fSAndroid Build Coastguard Worker 
88*795d594fSAndroid Build Coastguard Worker   // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
89*795d594fSAndroid Build Coastguard Worker   // situations.
90*795d594fSAndroid Build Coastguard Worker   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
91*795d594fSAndroid Build Coastguard Worker   if (callee == codegen->GetGraph()->GetArtMethod() &&
92*795d594fSAndroid Build Coastguard Worker       !codegen->GetGraph()->IsDebuggable() &&
93*795d594fSAndroid Build Coastguard Worker       // The runtime expects the canonical interface method being passed as
94*795d594fSAndroid Build Coastguard Worker       // hidden argument when doing an invokeinterface. Because default methods
95*795d594fSAndroid Build Coastguard Worker       // can be called through invokevirtual, we may get a copied method if we
96*795d594fSAndroid Build Coastguard Worker       // load 'recursively'.
97*795d594fSAndroid Build Coastguard Worker       (!for_interface_call || !callee->IsDefault())) {
98*795d594fSAndroid Build Coastguard Worker     // Recursive load.
99*795d594fSAndroid Build Coastguard Worker     method_load_kind = MethodLoadKind::kRecursive;
100*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallSelf;
101*795d594fSAndroid Build Coastguard Worker   } else if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
102*795d594fSAndroid Build Coastguard Worker     if (!compiler_options.GetCompilePic()) {
103*795d594fSAndroid Build Coastguard Worker       // Test configuration, do not sharpen.
104*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kRuntimeCall;
105*795d594fSAndroid Build Coastguard Worker     } else if (IsInBootImage(callee)) {
106*795d594fSAndroid Build Coastguard Worker       DCHECK(compiler_options.IsBootImageExtension());
107*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kBootImageRelRo;
108*795d594fSAndroid Build Coastguard Worker     } else if (ImageAOTCanEmbedMethod(callee, compiler_options)) {
109*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kBootImageLinkTimePcRelative;
110*795d594fSAndroid Build Coastguard Worker     } else if (!has_method_id) {
111*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kRuntimeCall;
112*795d594fSAndroid Build Coastguard Worker     } else {
113*795d594fSAndroid Build Coastguard Worker       DCHECK(!callee->IsCopied());
114*795d594fSAndroid Build Coastguard Worker       // Use PC-relative access to the .bss methods array.
115*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kBssEntry;
116*795d594fSAndroid Build Coastguard Worker     }
117*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallArtMethod;
118*795d594fSAndroid Build Coastguard Worker   } else if (compiler_options.IsJitCompiler()) {
119*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
120*795d594fSAndroid Build Coastguard Worker     if (Runtime::Current()->GetJit()->CanEncodeMethod(
121*795d594fSAndroid Build Coastguard Worker             callee,
122*795d594fSAndroid Build Coastguard Worker             compiler_options.IsJitCompilerForSharedCode())) {
123*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kJitDirectAddress;
124*795d594fSAndroid Build Coastguard Worker       method_load_data = reinterpret_cast<uintptr_t>(callee);
125*795d594fSAndroid Build Coastguard Worker       code_ptr_location = CodePtrLocation::kCallArtMethod;
126*795d594fSAndroid Build Coastguard Worker     } else {
127*795d594fSAndroid Build Coastguard Worker       // Do not sharpen.
128*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kRuntimeCall;
129*795d594fSAndroid Build Coastguard Worker       code_ptr_location = CodePtrLocation::kCallArtMethod;
130*795d594fSAndroid Build Coastguard Worker     }
131*795d594fSAndroid Build Coastguard Worker   } else if (IsInBootImage(callee)) {
132*795d594fSAndroid Build Coastguard Worker     // Use PC-relative access to the .data.img.rel.ro boot image methods array.
133*795d594fSAndroid Build Coastguard Worker     method_load_kind = MethodLoadKind::kBootImageRelRo;
134*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallArtMethod;
135*795d594fSAndroid Build Coastguard Worker   } else if (!has_method_id) {
136*795d594fSAndroid Build Coastguard Worker     method_load_kind = MethodLoadKind::kRuntimeCall;
137*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallArtMethod;
138*795d594fSAndroid Build Coastguard Worker   } else {
139*795d594fSAndroid Build Coastguard Worker     DCHECK(!callee->IsCopied());
140*795d594fSAndroid Build Coastguard Worker     if (compiler_options.IsAppImage() && ImageAOTCanEmbedMethod(callee, compiler_options)) {
141*795d594fSAndroid Build Coastguard Worker       // Use PC-relative access to the .data.img.rel.ro app image methods array.
142*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kAppImageRelRo;
143*795d594fSAndroid Build Coastguard Worker     } else {
144*795d594fSAndroid Build Coastguard Worker       // Use PC-relative access to the .bss methods array.
145*795d594fSAndroid Build Coastguard Worker       method_load_kind = MethodLoadKind::kBssEntry;
146*795d594fSAndroid Build Coastguard Worker     }
147*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallArtMethod;
148*795d594fSAndroid Build Coastguard Worker   }
149*795d594fSAndroid Build Coastguard Worker 
150*795d594fSAndroid Build Coastguard Worker   if (method_load_kind != MethodLoadKind::kRuntimeCall && callee->IsCriticalNative()) {
151*795d594fSAndroid Build Coastguard Worker     DCHECK_NE(method_load_kind, MethodLoadKind::kRecursive);
152*795d594fSAndroid Build Coastguard Worker     DCHECK(callee->IsStatic());
153*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallCriticalNative;
154*795d594fSAndroid Build Coastguard Worker   }
155*795d594fSAndroid Build Coastguard Worker 
156*795d594fSAndroid Build Coastguard Worker   if (codegen->GetGraph()->IsDebuggable()) {
157*795d594fSAndroid Build Coastguard Worker     // For debuggable apps always use the code pointer from ArtMethod
158*795d594fSAndroid Build Coastguard Worker     // so that we don't circumvent instrumentation stubs if installed.
159*795d594fSAndroid Build Coastguard Worker     code_ptr_location = CodePtrLocation::kCallArtMethod;
160*795d594fSAndroid Build Coastguard Worker   }
161*795d594fSAndroid Build Coastguard Worker 
162*795d594fSAndroid Build Coastguard Worker   HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
163*795d594fSAndroid Build Coastguard Worker       method_load_kind, code_ptr_location, method_load_data
164*795d594fSAndroid Build Coastguard Worker   };
165*795d594fSAndroid Build Coastguard Worker   return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
166*795d594fSAndroid Build Coastguard Worker }
167*795d594fSAndroid Build Coastguard Worker 
ComputeLoadClassKind(HLoadClass * load_class,CodeGenerator * codegen,const DexCompilationUnit & dex_compilation_unit)168*795d594fSAndroid Build Coastguard Worker HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
169*795d594fSAndroid Build Coastguard Worker     HLoadClass* load_class,
170*795d594fSAndroid Build Coastguard Worker     CodeGenerator* codegen,
171*795d594fSAndroid Build Coastguard Worker     const DexCompilationUnit& dex_compilation_unit) {
172*795d594fSAndroid Build Coastguard Worker   Handle<mirror::Class> klass = load_class->GetClass();
173*795d594fSAndroid Build Coastguard Worker   DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
174*795d594fSAndroid Build Coastguard Worker          load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
175*795d594fSAndroid Build Coastguard Worker       << load_class->GetLoadKind();
176*795d594fSAndroid Build Coastguard Worker   DCHECK(!load_class->IsInImage()) << "HLoadClass should not be optimized before sharpening.";
177*795d594fSAndroid Build Coastguard Worker   const DexFile& dex_file = load_class->GetDexFile();
178*795d594fSAndroid Build Coastguard Worker   dex::TypeIndex type_index = load_class->GetTypeIndex();
179*795d594fSAndroid Build Coastguard Worker   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
180*795d594fSAndroid Build Coastguard Worker 
181*795d594fSAndroid Build Coastguard Worker   auto is_class_in_current_image = [&]() {
182*795d594fSAndroid Build Coastguard Worker     return compiler_options.IsGeneratingImage() &&
183*795d594fSAndroid Build Coastguard Worker            compiler_options.IsImageClass(dex_file.GetTypeDescriptor(type_index));
184*795d594fSAndroid Build Coastguard Worker   };
185*795d594fSAndroid Build Coastguard Worker 
186*795d594fSAndroid Build Coastguard Worker   bool is_in_image = false;
187*795d594fSAndroid Build Coastguard Worker   HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
188*795d594fSAndroid Build Coastguard Worker 
189*795d594fSAndroid Build Coastguard Worker   if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
190*795d594fSAndroid Build Coastguard Worker     DCHECK(!load_class->NeedsAccessCheck());
191*795d594fSAndroid Build Coastguard Worker     // Loading from the ArtMethod* is the most efficient retrieval in code size.
192*795d594fSAndroid Build Coastguard Worker     // TODO: This may not actually be true for all architectures and
193*795d594fSAndroid Build Coastguard Worker     // locations of target classes. The additional register pressure
194*795d594fSAndroid Build Coastguard Worker     // for using the ArtMethod* should be considered.
195*795d594fSAndroid Build Coastguard Worker     desired_load_kind = HLoadClass::LoadKind::kReferrersClass;
196*795d594fSAndroid Build Coastguard Worker     // Determine whether the referrer's class is in the boot image.
197*795d594fSAndroid Build Coastguard Worker     is_in_image = is_class_in_current_image();
198*795d594fSAndroid Build Coastguard Worker   } else if (load_class->NeedsAccessCheck()) {
199*795d594fSAndroid Build Coastguard Worker     DCHECK_EQ(load_class->GetLoadKind(), HLoadClass::LoadKind::kRuntimeCall);
200*795d594fSAndroid Build Coastguard Worker     if (klass != nullptr) {
201*795d594fSAndroid Build Coastguard Worker       // Resolved class that needs access check must be really inaccessible
202*795d594fSAndroid Build Coastguard Worker       // and the access check is bound to fail. Just emit the runtime call.
203*795d594fSAndroid Build Coastguard Worker       desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
204*795d594fSAndroid Build Coastguard Worker       // Determine whether the class is in the boot image.
205*795d594fSAndroid Build Coastguard Worker       is_in_image = Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass.Get()) ||
206*795d594fSAndroid Build Coastguard Worker                     is_class_in_current_image();
207*795d594fSAndroid Build Coastguard Worker     } else if (compiler_options.IsJitCompiler()) {
208*795d594fSAndroid Build Coastguard Worker       // Unresolved class while JITting means that either we never hit this
209*795d594fSAndroid Build Coastguard Worker       // instruction or it failed. Either way, just emit the runtime call.
210*795d594fSAndroid Build Coastguard Worker       // (Though we could consider emitting Deoptimize instead and
211*795d594fSAndroid Build Coastguard Worker       // recompile if the instruction succeeds in interpreter.)
212*795d594fSAndroid Build Coastguard Worker       desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
213*795d594fSAndroid Build Coastguard Worker     } else {
214*795d594fSAndroid Build Coastguard Worker       // For AOT, check if the class is in the same literal package as the
215*795d594fSAndroid Build Coastguard Worker       // compiling class and pick an appropriate .bss entry.
216*795d594fSAndroid Build Coastguard Worker       auto package_length = [](const char* descriptor) {
217*795d594fSAndroid Build Coastguard Worker         const char* slash_pos = strrchr(descriptor, '/');
218*795d594fSAndroid Build Coastguard Worker         return (slash_pos != nullptr) ? static_cast<size_t>(slash_pos - descriptor) : 0u;
219*795d594fSAndroid Build Coastguard Worker       };
220*795d594fSAndroid Build Coastguard Worker       const char* klass_descriptor = dex_file.GetTypeDescriptor(type_index);
221*795d594fSAndroid Build Coastguard Worker       const uint32_t klass_package_length = package_length(klass_descriptor);
222*795d594fSAndroid Build Coastguard Worker       const DexFile* referrer_dex_file = dex_compilation_unit.GetDexFile();
223*795d594fSAndroid Build Coastguard Worker       const dex::TypeIndex referrer_type_index =
224*795d594fSAndroid Build Coastguard Worker           referrer_dex_file->GetClassDef(dex_compilation_unit.GetClassDefIndex()).class_idx_;
225*795d594fSAndroid Build Coastguard Worker       const char* referrer_descriptor = referrer_dex_file->GetTypeDescriptor(referrer_type_index);
226*795d594fSAndroid Build Coastguard Worker       const uint32_t referrer_package_length = package_length(referrer_descriptor);
227*795d594fSAndroid Build Coastguard Worker       bool same_package =
228*795d594fSAndroid Build Coastguard Worker           (referrer_package_length == klass_package_length) &&
229*795d594fSAndroid Build Coastguard Worker           memcmp(referrer_descriptor, klass_descriptor, referrer_package_length) == 0;
230*795d594fSAndroid Build Coastguard Worker       desired_load_kind = same_package
231*795d594fSAndroid Build Coastguard Worker           ? HLoadClass::LoadKind::kBssEntryPackage
232*795d594fSAndroid Build Coastguard Worker           : HLoadClass::LoadKind::kBssEntryPublic;
233*795d594fSAndroid Build Coastguard Worker     }
234*795d594fSAndroid Build Coastguard Worker   } else {
235*795d594fSAndroid Build Coastguard Worker     Runtime* runtime = Runtime::Current();
236*795d594fSAndroid Build Coastguard Worker     if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
237*795d594fSAndroid Build Coastguard Worker       // Compiling boot image or boot image extension. Check if the class is a boot image class.
238*795d594fSAndroid Build Coastguard Worker       DCHECK(!compiler_options.IsJitCompiler());
239*795d594fSAndroid Build Coastguard Worker       if (!compiler_options.GetCompilePic()) {
240*795d594fSAndroid Build Coastguard Worker         // Test configuration, do not sharpen.
241*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
242*795d594fSAndroid Build Coastguard Worker         // Determine whether the class is in the boot image.
243*795d594fSAndroid Build Coastguard Worker         is_in_image = Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass.Get()) ||
244*795d594fSAndroid Build Coastguard Worker                       is_class_in_current_image();
245*795d594fSAndroid Build Coastguard Worker       } else if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get())) {
246*795d594fSAndroid Build Coastguard Worker         DCHECK(compiler_options.IsBootImageExtension());
247*795d594fSAndroid Build Coastguard Worker         is_in_image = true;
248*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
249*795d594fSAndroid Build Coastguard Worker       } else if ((klass != nullptr) &&
250*795d594fSAndroid Build Coastguard Worker                  compiler_options.IsImageClass(dex_file.GetTypeDescriptor(type_index))) {
251*795d594fSAndroid Build Coastguard Worker         is_in_image = true;
252*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
253*795d594fSAndroid Build Coastguard Worker       } else {
254*795d594fSAndroid Build Coastguard Worker         // Not a boot image class.
255*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
256*795d594fSAndroid Build Coastguard Worker       }
257*795d594fSAndroid Build Coastguard Worker     } else {
258*795d594fSAndroid Build Coastguard Worker       is_in_image = (klass != nullptr) && runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
259*795d594fSAndroid Build Coastguard Worker       if (compiler_options.IsJitCompiler()) {
260*795d594fSAndroid Build Coastguard Worker         DCHECK(!compiler_options.GetCompilePic());
261*795d594fSAndroid Build Coastguard Worker         if (is_in_image) {
262*795d594fSAndroid Build Coastguard Worker           desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
263*795d594fSAndroid Build Coastguard Worker         } else if (klass != nullptr) {
264*795d594fSAndroid Build Coastguard Worker           if (runtime->GetJit()->CanEncodeClass(
265*795d594fSAndroid Build Coastguard Worker                   klass.Get(),
266*795d594fSAndroid Build Coastguard Worker                   compiler_options.IsJitCompilerForSharedCode())) {
267*795d594fSAndroid Build Coastguard Worker             desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
268*795d594fSAndroid Build Coastguard Worker           } else {
269*795d594fSAndroid Build Coastguard Worker             // Shared JIT code cannot encode a literal that the GC can move.
270*795d594fSAndroid Build Coastguard Worker             VLOG(jit) << "Unable to encode in shared region class literal: "
271*795d594fSAndroid Build Coastguard Worker                       << klass->PrettyClass();
272*795d594fSAndroid Build Coastguard Worker             desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
273*795d594fSAndroid Build Coastguard Worker           }
274*795d594fSAndroid Build Coastguard Worker         } else {
275*795d594fSAndroid Build Coastguard Worker           // Class not loaded yet. This happens when the dex code requesting
276*795d594fSAndroid Build Coastguard Worker           // this `HLoadClass` hasn't been executed in the interpreter.
277*795d594fSAndroid Build Coastguard Worker           // Fallback to the dex cache.
278*795d594fSAndroid Build Coastguard Worker           // TODO(ngeoffray): Generate HDeoptimize instead.
279*795d594fSAndroid Build Coastguard Worker           desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
280*795d594fSAndroid Build Coastguard Worker         }
281*795d594fSAndroid Build Coastguard Worker       } else if (is_in_image) {
282*795d594fSAndroid Build Coastguard Worker         // AOT app compilation, boot image class.
283*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
284*795d594fSAndroid Build Coastguard Worker       } else if (compiler_options.IsAppImage() && is_class_in_current_image()) {
285*795d594fSAndroid Build Coastguard Worker         // AOT app compilation, app image class.
286*795d594fSAndroid Build Coastguard Worker         is_in_image = true;
287*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kAppImageRelRo;
288*795d594fSAndroid Build Coastguard Worker       } else {
289*795d594fSAndroid Build Coastguard Worker         // Not JIT and the klass is not in boot image or app image.
290*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
291*795d594fSAndroid Build Coastguard Worker       }
292*795d594fSAndroid Build Coastguard Worker     }
293*795d594fSAndroid Build Coastguard Worker   }
294*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
295*795d594fSAndroid Build Coastguard Worker 
296*795d594fSAndroid Build Coastguard Worker   if (is_in_image) {
297*795d594fSAndroid Build Coastguard Worker     load_class->MarkInImage();
298*795d594fSAndroid Build Coastguard Worker   }
299*795d594fSAndroid Build Coastguard Worker   HLoadClass::LoadKind load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
300*795d594fSAndroid Build Coastguard Worker 
301*795d594fSAndroid Build Coastguard Worker   if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
302*795d594fSAndroid Build Coastguard Worker     if (load_kind == HLoadClass::LoadKind::kRuntimeCall ||
303*795d594fSAndroid Build Coastguard Worker         load_kind == HLoadClass::LoadKind::kBssEntry ||
304*795d594fSAndroid Build Coastguard Worker         load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
305*795d594fSAndroid Build Coastguard Worker         load_kind == HLoadClass::LoadKind::kBssEntryPackage) {
306*795d594fSAndroid Build Coastguard Worker       // We actually cannot reference this class, we're forced to bail.
307*795d594fSAndroid Build Coastguard Worker       // We cannot reference this class with Bss, as the entrypoint will lookup the class
308*795d594fSAndroid Build Coastguard Worker       // in the caller's dex file, but that dex file does not reference the class.
309*795d594fSAndroid Build Coastguard Worker       // TODO(solanes): We could theoretically enable this optimization for kBssEntry* but this
310*795d594fSAndroid Build Coastguard Worker       // requires some changes to the entrypoints, particularly artResolveTypeFromCode and
311*795d594fSAndroid Build Coastguard Worker       // artResolveTypeAndVerifyAccessFromCode. Currently, they assume that the `load_class`'s
312*795d594fSAndroid Build Coastguard Worker       // Dexfile and the `dex_compilation_unit` DexFile is the same and will try to use the type
313*795d594fSAndroid Build Coastguard Worker       // index in the incorrect DexFile by using the `caller`'s DexFile. A possibility is to add
314*795d594fSAndroid Build Coastguard Worker       // another parameter to it pointing to the correct DexFile to use.
315*795d594fSAndroid Build Coastguard Worker       return HLoadClass::LoadKind::kInvalid;
316*795d594fSAndroid Build Coastguard Worker     }
317*795d594fSAndroid Build Coastguard Worker   }
318*795d594fSAndroid Build Coastguard Worker   return load_kind;
319*795d594fSAndroid Build Coastguard Worker }
320*795d594fSAndroid Build Coastguard Worker 
CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass,CodeGenerator * codegen)321*795d594fSAndroid Build Coastguard Worker static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
322*795d594fSAndroid Build Coastguard Worker     REQUIRES_SHARED(Locks::mutator_lock_) {
323*795d594fSAndroid Build Coastguard Worker   DCHECK(!klass->IsProxyClass());
324*795d594fSAndroid Build Coastguard Worker   DCHECK(!klass->IsArrayClass());
325*795d594fSAndroid Build Coastguard Worker 
326*795d594fSAndroid Build Coastguard Worker   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
327*795d594fSAndroid Build Coastguard Worker   if (compiler_options.IsJitCompiler()) {
328*795d594fSAndroid Build Coastguard Worker     // If we're JITting, try to assign a type check bitstring (fall through).
329*795d594fSAndroid Build Coastguard Worker   } else if (codegen->GetCompilerOptions().IsBootImage()) {
330*795d594fSAndroid Build Coastguard Worker     const char* descriptor = klass->GetDexFile().GetTypeDescriptor(klass->GetDexTypeIndex());
331*795d594fSAndroid Build Coastguard Worker     if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
332*795d594fSAndroid Build Coastguard Worker       return false;
333*795d594fSAndroid Build Coastguard Worker     }
334*795d594fSAndroid Build Coastguard Worker     // If the target is a boot image class, try to assign a type check bitstring (fall through).
335*795d594fSAndroid Build Coastguard Worker     // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
336*795d594fSAndroid Build Coastguard Worker   } else {
337*795d594fSAndroid Build Coastguard Worker     // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
338*795d594fSAndroid Build Coastguard Worker     // already assigned in the boot image.
339*795d594fSAndroid Build Coastguard Worker     return false;
340*795d594fSAndroid Build Coastguard Worker   }
341*795d594fSAndroid Build Coastguard Worker 
342*795d594fSAndroid Build Coastguard Worker   // Try to assign a type check bitstring.
343*795d594fSAndroid Build Coastguard Worker   MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
344*795d594fSAndroid Build Coastguard Worker   if ((false) &&  // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
345*795d594fSAndroid Build Coastguard Worker                   // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
346*795d594fSAndroid Build Coastguard Worker       kIsDebugBuild &&
347*795d594fSAndroid Build Coastguard Worker       compiler_options.IsBootImage() &&
348*795d594fSAndroid Build Coastguard Worker       compiler_options.IsForceDeterminism()) {
349*795d594fSAndroid Build Coastguard Worker     SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
350*795d594fSAndroid Build Coastguard Worker     CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
351*795d594fSAndroid Build Coastguard Worker         << klass->PrettyDescriptor() << "/" << old_state
352*795d594fSAndroid Build Coastguard Worker         << " in " << codegen->GetGraph()->PrettyMethod();
353*795d594fSAndroid Build Coastguard Worker   }
354*795d594fSAndroid Build Coastguard Worker   SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
355*795d594fSAndroid Build Coastguard Worker   return state == SubtypeCheckInfo::kAssigned;
356*795d594fSAndroid Build Coastguard Worker }
357*795d594fSAndroid Build Coastguard Worker 
ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,CodeGenerator * codegen,bool needs_access_check)358*795d594fSAndroid Build Coastguard Worker TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
359*795d594fSAndroid Build Coastguard Worker                                                 CodeGenerator* codegen,
360*795d594fSAndroid Build Coastguard Worker                                                 bool needs_access_check) {
361*795d594fSAndroid Build Coastguard Worker   if (klass == nullptr) {
362*795d594fSAndroid Build Coastguard Worker     return TypeCheckKind::kUnresolvedCheck;
363*795d594fSAndroid Build Coastguard Worker   } else if (klass->IsInterface()) {
364*795d594fSAndroid Build Coastguard Worker     return TypeCheckKind::kInterfaceCheck;
365*795d594fSAndroid Build Coastguard Worker   } else if (klass->IsArrayClass()) {
366*795d594fSAndroid Build Coastguard Worker     if (klass->GetComponentType()->IsObjectClass()) {
367*795d594fSAndroid Build Coastguard Worker       return TypeCheckKind::kArrayObjectCheck;
368*795d594fSAndroid Build Coastguard Worker     } else if (klass->CannotBeAssignedFromOtherTypes()) {
369*795d594fSAndroid Build Coastguard Worker       return TypeCheckKind::kExactCheck;
370*795d594fSAndroid Build Coastguard Worker     } else {
371*795d594fSAndroid Build Coastguard Worker       return TypeCheckKind::kArrayCheck;
372*795d594fSAndroid Build Coastguard Worker     }
373*795d594fSAndroid Build Coastguard Worker   } else if (klass->IsFinal()) {  // TODO: Consider using bitstring for final classes.
374*795d594fSAndroid Build Coastguard Worker     return TypeCheckKind::kExactCheck;
375*795d594fSAndroid Build Coastguard Worker   } else if (kBitstringSubtypeCheckEnabled &&
376*795d594fSAndroid Build Coastguard Worker              !needs_access_check &&
377*795d594fSAndroid Build Coastguard Worker              CanUseTypeCheckBitstring(klass, codegen)) {
378*795d594fSAndroid Build Coastguard Worker     // TODO: We should not need the `!needs_access_check` check but getting rid of that
379*795d594fSAndroid Build Coastguard Worker     // requires rewriting some optimizations in instruction simplifier.
380*795d594fSAndroid Build Coastguard Worker     return TypeCheckKind::kBitstringCheck;
381*795d594fSAndroid Build Coastguard Worker   } else if (klass->IsAbstract()) {
382*795d594fSAndroid Build Coastguard Worker     return TypeCheckKind::kAbstractClassCheck;
383*795d594fSAndroid Build Coastguard Worker   } else {
384*795d594fSAndroid Build Coastguard Worker     return TypeCheckKind::kClassHierarchyCheck;
385*795d594fSAndroid Build Coastguard Worker   }
386*795d594fSAndroid Build Coastguard Worker }
387*795d594fSAndroid Build Coastguard Worker 
ProcessLoadString(HLoadString * load_string,CodeGenerator * codegen,const DexCompilationUnit & dex_compilation_unit,VariableSizedHandleScope * handles)388*795d594fSAndroid Build Coastguard Worker void HSharpening::ProcessLoadString(
389*795d594fSAndroid Build Coastguard Worker     HLoadString* load_string,
390*795d594fSAndroid Build Coastguard Worker     CodeGenerator* codegen,
391*795d594fSAndroid Build Coastguard Worker     const DexCompilationUnit& dex_compilation_unit,
392*795d594fSAndroid Build Coastguard Worker     VariableSizedHandleScope* handles) {
393*795d594fSAndroid Build Coastguard Worker   DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
394*795d594fSAndroid Build Coastguard Worker 
395*795d594fSAndroid Build Coastguard Worker   const DexFile& dex_file = load_string->GetDexFile();
396*795d594fSAndroid Build Coastguard Worker   dex::StringIndex string_index = load_string->GetStringIndex();
397*795d594fSAndroid Build Coastguard Worker 
398*795d594fSAndroid Build Coastguard Worker   HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
399*795d594fSAndroid Build Coastguard Worker   {
400*795d594fSAndroid Build Coastguard Worker     Runtime* runtime = Runtime::Current();
401*795d594fSAndroid Build Coastguard Worker     ClassLinker* class_linker = runtime->GetClassLinker();
402*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
403*795d594fSAndroid Build Coastguard Worker     StackHandleScope<1> hs(soa.Self());
404*795d594fSAndroid Build Coastguard Worker     Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
405*795d594fSAndroid Build Coastguard Worker         ? dex_compilation_unit.GetDexCache()
406*795d594fSAndroid Build Coastguard Worker         : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
407*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::String> string = nullptr;
408*795d594fSAndroid Build Coastguard Worker 
409*795d594fSAndroid Build Coastguard Worker     const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
410*795d594fSAndroid Build Coastguard Worker     if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
411*795d594fSAndroid Build Coastguard Worker       // Compiling boot image or boot image extension. Resolve the string and allocate it
412*795d594fSAndroid Build Coastguard Worker       // if needed, to ensure the string will be added to the boot image.
413*795d594fSAndroid Build Coastguard Worker       DCHECK(!compiler_options.IsJitCompiler());
414*795d594fSAndroid Build Coastguard Worker       if (compiler_options.GetCompilePic()) {
415*795d594fSAndroid Build Coastguard Worker         if (compiler_options.IsForceDeterminism()) {
416*795d594fSAndroid Build Coastguard Worker           // Strings for methods we're compiling should be pre-resolved but Strings in inlined
417*795d594fSAndroid Build Coastguard Worker           // methods may not be if these inlined methods are not in the boot image profile.
418*795d594fSAndroid Build Coastguard Worker           // Multiple threads allocating new Strings can cause non-deterministic boot image
419*795d594fSAndroid Build Coastguard Worker           // because of the image relying on the order of GC roots we walk. (We could fix that
420*795d594fSAndroid Build Coastguard Worker           // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
421*795d594fSAndroid Build Coastguard Worker           // strings even if that results in omitting them from the boot image and using the
422*795d594fSAndroid Build Coastguard Worker           // sub-optimal load kind kBssEntry.
423*795d594fSAndroid Build Coastguard Worker           string = class_linker->LookupString(string_index, dex_cache.Get());
424*795d594fSAndroid Build Coastguard Worker         } else {
425*795d594fSAndroid Build Coastguard Worker           string = class_linker->ResolveString(string_index, dex_cache);
426*795d594fSAndroid Build Coastguard Worker           CHECK(string != nullptr);
427*795d594fSAndroid Build Coastguard Worker         }
428*795d594fSAndroid Build Coastguard Worker         if (string != nullptr) {
429*795d594fSAndroid Build Coastguard Worker           if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
430*795d594fSAndroid Build Coastguard Worker             DCHECK(compiler_options.IsBootImageExtension());
431*795d594fSAndroid Build Coastguard Worker             desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
432*795d594fSAndroid Build Coastguard Worker           } else {
433*795d594fSAndroid Build Coastguard Worker             desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
434*795d594fSAndroid Build Coastguard Worker           }
435*795d594fSAndroid Build Coastguard Worker         } else {
436*795d594fSAndroid Build Coastguard Worker           desired_load_kind = HLoadString::LoadKind::kBssEntry;
437*795d594fSAndroid Build Coastguard Worker         }
438*795d594fSAndroid Build Coastguard Worker       } else {
439*795d594fSAndroid Build Coastguard Worker         // Test configuration, do not sharpen.
440*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
441*795d594fSAndroid Build Coastguard Worker       }
442*795d594fSAndroid Build Coastguard Worker     } else if (compiler_options.IsJitCompiler()) {
443*795d594fSAndroid Build Coastguard Worker       DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
444*795d594fSAndroid Build Coastguard Worker       string = class_linker->LookupString(string_index, dex_cache.Get());
445*795d594fSAndroid Build Coastguard Worker       if (string != nullptr) {
446*795d594fSAndroid Build Coastguard Worker         gc::Heap* heap = runtime->GetHeap();
447*795d594fSAndroid Build Coastguard Worker         if (heap->ObjectIsInBootImageSpace(string)) {
448*795d594fSAndroid Build Coastguard Worker           desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
449*795d594fSAndroid Build Coastguard Worker         } else if (runtime->GetJit()->CanEncodeString(
450*795d594fSAndroid Build Coastguard Worker                   string,
451*795d594fSAndroid Build Coastguard Worker                   compiler_options.IsJitCompilerForSharedCode())) {
452*795d594fSAndroid Build Coastguard Worker           desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
453*795d594fSAndroid Build Coastguard Worker         } else {
454*795d594fSAndroid Build Coastguard Worker           // Shared JIT code cannot encode a literal that the GC can move.
455*795d594fSAndroid Build Coastguard Worker           VLOG(jit) << "Unable to encode in shared region string literal: "
456*795d594fSAndroid Build Coastguard Worker                     << string->ToModifiedUtf8();
457*795d594fSAndroid Build Coastguard Worker           desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
458*795d594fSAndroid Build Coastguard Worker         }
459*795d594fSAndroid Build Coastguard Worker       } else {
460*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
461*795d594fSAndroid Build Coastguard Worker       }
462*795d594fSAndroid Build Coastguard Worker     } else {
463*795d594fSAndroid Build Coastguard Worker       // AOT app compilation. Try to lookup the string without allocating if not found.
464*795d594fSAndroid Build Coastguard Worker       string = class_linker->LookupString(string_index, dex_cache.Get());
465*795d594fSAndroid Build Coastguard Worker       if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
466*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
467*795d594fSAndroid Build Coastguard Worker       } else {
468*795d594fSAndroid Build Coastguard Worker         desired_load_kind = HLoadString::LoadKind::kBssEntry;
469*795d594fSAndroid Build Coastguard Worker       }
470*795d594fSAndroid Build Coastguard Worker     }
471*795d594fSAndroid Build Coastguard Worker     if (string != nullptr) {
472*795d594fSAndroid Build Coastguard Worker       load_string->SetString(handles->NewHandle(string));
473*795d594fSAndroid Build Coastguard Worker     }
474*795d594fSAndroid Build Coastguard Worker   }
475*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
476*795d594fSAndroid Build Coastguard Worker 
477*795d594fSAndroid Build Coastguard Worker   HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
478*795d594fSAndroid Build Coastguard Worker   load_string->SetLoadKind(load_kind);
479*795d594fSAndroid Build Coastguard Worker }
480*795d594fSAndroid Build Coastguard Worker 
ProcessLoadMethodType(HLoadMethodType * load_method_type,CodeGenerator * codegen,const DexCompilationUnit & dex_compilation_unit,VariableSizedHandleScope * handles)481*795d594fSAndroid Build Coastguard Worker void HSharpening::ProcessLoadMethodType(
482*795d594fSAndroid Build Coastguard Worker     HLoadMethodType* load_method_type,
483*795d594fSAndroid Build Coastguard Worker     CodeGenerator* codegen,
484*795d594fSAndroid Build Coastguard Worker     const DexCompilationUnit& dex_compilation_unit,
485*795d594fSAndroid Build Coastguard Worker     VariableSizedHandleScope* handles) {
486*795d594fSAndroid Build Coastguard Worker   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
487*795d594fSAndroid Build Coastguard Worker 
488*795d594fSAndroid Build Coastguard Worker   HLoadMethodType::LoadKind desired_load_kind = static_cast<HLoadMethodType::LoadKind>(-1);
489*795d594fSAndroid Build Coastguard Worker 
490*795d594fSAndroid Build Coastguard Worker   if (compiler_options.IsJitCompiler()) {
491*795d594fSAndroid Build Coastguard Worker     DCHECK(!compiler_options.GetCompilePic());
492*795d594fSAndroid Build Coastguard Worker     Runtime* runtime = Runtime::Current();
493*795d594fSAndroid Build Coastguard Worker     ClassLinker* class_linker = runtime->GetClassLinker();
494*795d594fSAndroid Build Coastguard Worker     ScopedObjectAccess soa(Thread::Current());
495*795d594fSAndroid Build Coastguard Worker     ObjPtr<mirror::MethodType> method_type =
496*795d594fSAndroid Build Coastguard Worker         class_linker->ResolveMethodType(Thread::Current(),
497*795d594fSAndroid Build Coastguard Worker                                         load_method_type->GetProtoIndex(),
498*795d594fSAndroid Build Coastguard Worker                                         dex_compilation_unit.GetDexCache(),
499*795d594fSAndroid Build Coastguard Worker                                         dex_compilation_unit.GetClassLoader());
500*795d594fSAndroid Build Coastguard Worker 
501*795d594fSAndroid Build Coastguard Worker     if (method_type != nullptr) {
502*795d594fSAndroid Build Coastguard Worker       load_method_type->SetMethodType(handles->NewHandle(method_type));
503*795d594fSAndroid Build Coastguard Worker       desired_load_kind = HLoadMethodType::LoadKind::kJitTableAddress;
504*795d594fSAndroid Build Coastguard Worker     } else {
505*795d594fSAndroid Build Coastguard Worker       DCHECK_EQ(load_method_type->GetLoadKind(), HLoadMethodType::LoadKind::kRuntimeCall);
506*795d594fSAndroid Build Coastguard Worker       desired_load_kind = HLoadMethodType::LoadKind::kRuntimeCall;
507*795d594fSAndroid Build Coastguard Worker       Thread::Current()->ClearException();
508*795d594fSAndroid Build Coastguard Worker     }
509*795d594fSAndroid Build Coastguard Worker   } else {
510*795d594fSAndroid Build Coastguard Worker     if (compiler_options.GetCompilePic()) {
511*795d594fSAndroid Build Coastguard Worker       desired_load_kind = HLoadMethodType::LoadKind::kBssEntry;
512*795d594fSAndroid Build Coastguard Worker     } else {
513*795d594fSAndroid Build Coastguard Worker       // Test configuration, do not sharpen.
514*795d594fSAndroid Build Coastguard Worker       desired_load_kind = HLoadMethodType::LoadKind::kRuntimeCall;
515*795d594fSAndroid Build Coastguard Worker     }
516*795d594fSAndroid Build Coastguard Worker   }
517*795d594fSAndroid Build Coastguard Worker 
518*795d594fSAndroid Build Coastguard Worker   DCHECK_NE(desired_load_kind, static_cast<HLoadMethodType::LoadKind>(-1));
519*795d594fSAndroid Build Coastguard Worker   load_method_type->SetLoadKind(desired_load_kind);
520*795d594fSAndroid Build Coastguard Worker }
521*795d594fSAndroid Build Coastguard Worker 
522*795d594fSAndroid Build Coastguard Worker }  // namespace art
523