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