blob: 17cf3d3477336bc5354c4627f3146594ee58037d [file] [log] [blame]
Vladimir Markodc151b22015-10-15 18:02:30 +01001/*
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
Vladimir Marko65979462017-05-19 17:25:12 +010019#include "art_method-inl.h"
Vladimir Markodb8e62d2016-03-30 16:30:21 +010020#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000022#include "base/logging.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000023#include "class_linker.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010024#include "code_generator.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "driver/compiler_options.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000026#include "driver/dex_compilation_unit.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000027#include "gc/heap.h"
28#include "gc/space/image_space.h"
29#include "handle_scope-inl.h"
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +000030#include "jit/jit.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000031#include "mirror/dex_cache.h"
32#include "mirror/string.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010033#include "nodes.h"
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000034#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070035#include "scoped_thread_state_change-inl.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010036
Vladimir Marko0a516052019-10-14 13:00:44 +000037namespace art {
Vladimir Markodc151b22015-10-15 18:02:30 +010038
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000039static bool IsInBootImage(ArtMethod* method) {
Vladimir Marko7cde4582019-07-05 13:26:11 +010040 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);
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000049}
50
Vladimir Markobb089b62018-06-28 17:30:16 +010051static bool BootImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
Vladimir Marko44ca0752019-07-29 10:18:25 +010052 DCHECK(compiler_options.IsBootImage() || compiler_options.IsBootImageExtension());
Vladimir Marko65979462017-05-19 17:25:12 +010053 ScopedObjectAccess soa(Thread::Current());
54 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
55 DCHECK(klass != nullptr);
56 const DexFile& dex_file = klass->GetDexFile();
Vladimir Markodc4bcce2018-06-21 16:15:42 +010057 return compiler_options.IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
Vladimir Marko65979462017-05-19 17:25:12 +010058}
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000059
Nicolas Geoffray8d34a182020-09-16 09:46:58 +010060HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenLoadMethod(
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +000061 ArtMethod* callee,
62 bool has_method_id,
63 bool for_interface_call,
64 CodeGenerator* codegen) {
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +010065 if (kIsDebugBuild) {
66 ScopedObjectAccess soa(Thread::Current()); // Required for GetDeclaringClass below.
67 DCHECK(callee != nullptr);
68 DCHECK(!(callee->IsConstructor() && callee->GetDeclaringClass()->IsStringClass()));
Vladimir Markodc151b22015-10-15 18:02:30 +010069 }
70
Nicolas Geoffray6d69b522020-09-23 14:47:28 +010071 MethodLoadKind method_load_kind;
72 CodePtrLocation code_ptr_location;
Vladimir Markodc151b22015-10-15 18:02:30 +010073 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010074
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000075 // Note: we never call an ArtMethod through a known code pointer, as
76 // we do not want to keep on invoking it if it gets deoptimized. This
77 // applies to both AOT and JIT.
78 // This also avoids having to find out if the code pointer of an ArtMethod
79 // is the resolution trampoline (for ensuring the class is initialized), or
80 // the interpreter entrypoint. Such code pointers we do not want to call
81 // directly.
82 // Only in the case of a recursive call can we call directly, as we know the
83 // class is initialized already or being initialized, and the call will not
84 // be invoked once the method is deoptimized.
85
Alex Light1ebe4fe2017-01-30 14:57:11 -080086 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
87 // situations.
Vladimir Markobb089b62018-06-28 17:30:16 +010088 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Nicolas Geoffrayd5a86952021-01-19 10:35:54 +000089 if (callee == codegen->GetGraph()->GetArtMethod() &&
90 !codegen->GetGraph()->IsDebuggable() &&
91 // The runtime expects the canonical interface method being passed as
92 // hidden argument when doing an invokeinterface. Because default methods
93 // can be called through invokevirtual, we may get a copied method if we
94 // load 'recursively'.
95 (!for_interface_call || !callee->IsDefault())) {
96 // Recursive load.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +010097 method_load_kind = MethodLoadKind::kRecursive;
98 code_ptr_location = CodePtrLocation::kCallSelf;
Vladimir Marko44ca0752019-07-29 10:18:25 +010099 } else if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Markobb089b62018-06-28 17:30:16 +0100100 if (!compiler_options.GetCompilePic()) {
101 // Test configuration, do not sharpen.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100102 method_load_kind = MethodLoadKind::kRuntimeCall;
Vladimir Marko44ca0752019-07-29 10:18:25 +0100103 } else if (IsInBootImage(callee)) {
104 DCHECK(compiler_options.IsBootImageExtension());
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100105 method_load_kind = MethodLoadKind::kBootImageRelRo;
Vladimir Markobb089b62018-06-28 17:30:16 +0100106 } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100107 method_load_kind = MethodLoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray1fef8772020-09-09 13:57:17 +0100108 } else if (!has_method_id) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100109 method_load_kind = MethodLoadKind::kRuntimeCall;
Vladimir Markobb089b62018-06-28 17:30:16 +0100110 } else {
Nicolas Geoffray25b9c7d2020-09-17 17:34:34 +0100111 DCHECK(!callee->IsCopied());
Vladimir Markobb089b62018-06-28 17:30:16 +0100112 // Use PC-relative access to the .bss methods array.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100113 method_load_kind = MethodLoadKind::kBssEntry;
Vladimir Markobb089b62018-06-28 17:30:16 +0100114 }
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100115 code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Marko695348f2020-05-19 14:42:02 +0100116 } else if (compiler_options.IsJitCompiler()) {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +0100117 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000118 if (Runtime::Current()->GetJit()->CanEncodeMethod(
119 callee,
Vladimir Marko695348f2020-05-19 14:42:02 +0100120 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100121 method_load_kind = MethodLoadKind::kJitDirectAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000122 method_load_data = reinterpret_cast<uintptr_t>(callee);
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100123 code_ptr_location = CodePtrLocation::kCallArtMethod;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000124 } else {
125 // Do not sharpen.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100126 method_load_kind = MethodLoadKind::kRuntimeCall;
127 code_ptr_location = CodePtrLocation::kCallArtMethod;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000128 }
Vladimir Markob066d432018-01-03 13:14:37 +0000129 } else if (IsInBootImage(callee)) {
130 // Use PC-relative access to the .data.bimg.rel.ro methods array.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100131 method_load_kind = MethodLoadKind::kBootImageRelRo;
132 code_ptr_location = CodePtrLocation::kCallArtMethod;
Nicolas Geoffray1fef8772020-09-09 13:57:17 +0100133 } else if (!has_method_id) {
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100134 method_load_kind = MethodLoadKind::kRuntimeCall;
135 code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100136 } else {
Nicolas Geoffray25b9c7d2020-09-17 17:34:34 +0100137 DCHECK(!callee->IsCopied());
Vladimir Markob066d432018-01-03 13:14:37 +0000138 // Use PC-relative access to the .bss methods array.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100139 method_load_kind = MethodLoadKind::kBssEntry;
140 code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100141 }
142
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100143 if (method_load_kind != MethodLoadKind::kRuntimeCall && callee->IsCriticalNative()) {
144 DCHECK_NE(method_load_kind, MethodLoadKind::kRecursive);
Vladimir Marko86c87522020-05-11 16:55:55 +0100145 DCHECK(callee->IsStatic());
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100146 code_ptr_location = CodePtrLocation::kCallCriticalNative;
Vladimir Marko86c87522020-05-11 16:55:55 +0100147 }
148
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000149 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100150 // For debuggable apps always use the code pointer from ArtMethod
151 // so that we don't circumvent instrumentation stubs if installed.
Nicolas Geoffray6d69b522020-09-23 14:47:28 +0100152 code_ptr_location = CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100153 }
154
155 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000156 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100157 };
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100158 return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
Vladimir Markodc151b22015-10-15 18:02:30 +0100159}
160
Vladimir Marko28e012a2017-12-07 11:22:59 +0000161HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
162 HLoadClass* load_class,
163 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000164 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000165 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100166 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700167 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
168 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700169 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Marko8f63f102020-09-28 12:10:28 +0100170 const DexFile& dex_file = load_class->GetDexFile();
171 dex::TypeIndex type_index = load_class->GetTypeIndex();
172 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100173
Vladimir Marko9d31daa2022-04-14 10:48:44 +0100174 auto is_class_in_current_boot_image = [&]() {
175 return (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) &&
176 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index));
177 };
178
Vladimir Marko8f63f102020-09-28 12:10:28 +0100179 bool is_in_boot_image = false;
180 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000181
Vladimir Marko8f63f102020-09-28 12:10:28 +0100182 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
183 DCHECK(!load_class->NeedsAccessCheck());
Vladimir Marko41559982017-01-06 14:04:23 +0000184 // Loading from the ArtMethod* is the most efficient retrieval in code size.
185 // TODO: This may not actually be true for all architectures and
186 // locations of target classes. The additional register pressure
187 // for using the ArtMethod* should be considered.
Vladimir Marko8f63f102020-09-28 12:10:28 +0100188 desired_load_kind = HLoadClass::LoadKind::kReferrersClass;
Vladimir Marko9d31daa2022-04-14 10:48:44 +0100189 // Determine whether the referrer's class is in the boot image.
190 is_in_boot_image = is_class_in_current_boot_image();
Vladimir Marko8f63f102020-09-28 12:10:28 +0100191 } else if (load_class->NeedsAccessCheck()) {
192 DCHECK_EQ(load_class->GetLoadKind(), HLoadClass::LoadKind::kRuntimeCall);
193 if (klass != nullptr) {
194 // Resolved class that needs access check must be really inaccessible
195 // and the access check is bound to fail. Just emit the runtime call.
196 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko9d31daa2022-04-14 10:48:44 +0100197 // Determine whether the class is in the boot image.
198 is_in_boot_image = Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass.Get()) ||
199 is_class_in_current_boot_image();
Vladimir Marko8f63f102020-09-28 12:10:28 +0100200 } else if (compiler_options.IsJitCompiler()) {
201 // Unresolved class while JITting means that either we never hit this
202 // instruction or it failed. Either way, just emit the runtime call.
203 // (Though we could consider emitting Deoptimize instead and
204 // recompile if the instruction succeeds in interpreter.)
205 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
206 } else {
207 // For AOT, check if the class is in the same literal package as the
208 // compiling class and pick an appropriate .bss entry.
209 auto package_length = [](const char* descriptor) {
210 const char* slash_pos = strrchr(descriptor, '/');
211 return (slash_pos != nullptr) ? static_cast<size_t>(slash_pos - descriptor) : 0u;
212 };
213 const char* klass_descriptor = dex_file.StringByTypeIdx(type_index);
214 const uint32_t klass_package_length = package_length(klass_descriptor);
215 const DexFile* referrer_dex_file = dex_compilation_unit.GetDexFile();
216 const dex::TypeIndex referrer_type_index =
217 referrer_dex_file->GetClassDef(dex_compilation_unit.GetClassDefIndex()).class_idx_;
218 const char* referrer_descriptor = referrer_dex_file->StringByTypeIdx(referrer_type_index);
219 const uint32_t referrer_package_length = package_length(referrer_descriptor);
220 bool same_package =
221 (referrer_package_length == klass_package_length) &&
222 memcmp(referrer_descriptor, klass_descriptor, referrer_package_length) == 0;
223 desired_load_kind = same_package
224 ? HLoadClass::LoadKind::kBssEntryPackage
225 : HLoadClass::LoadKind::kBssEntryPublic;
226 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000227 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000228 Runtime* runtime = Runtime::Current();
Vladimir Marko44ca0752019-07-29 10:18:25 +0100229 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
230 // Compiling boot image or boot image extension. Check if the class is a boot image class.
Vladimir Marko695348f2020-05-19 14:42:02 +0100231 DCHECK(!compiler_options.IsJitCompiler());
Vladimir Markobb089b62018-06-28 17:30:16 +0100232 if (!compiler_options.GetCompilePic()) {
233 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100234 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko9d31daa2022-04-14 10:48:44 +0100235 // Determine whether the class is in the boot image.
236 is_in_boot_image = Runtime::Current()->GetHeap()->ObjectIsInBootImageSpace(klass.Get()) ||
237 is_class_in_current_boot_image();
Vladimir Marko44ca0752019-07-29 10:18:25 +0100238 } else if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get())) {
239 DCHECK(compiler_options.IsBootImageExtension());
240 is_in_boot_image = true;
241 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Vladimir Marko65979462017-05-19 17:25:12 +0100242 } else if ((klass != nullptr) &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100243 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000244 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100245 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000246 } else {
247 // Not a boot image class.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000248 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100249 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000250 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800251 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000252 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
Vladimir Marko695348f2020-05-19 14:42:02 +0100253 if (compiler_options.IsJitCompiler()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100254 DCHECK(!compiler_options.GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000255 if (is_in_boot_image) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100256 desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800257 } else if (klass != nullptr) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000258 if (runtime->GetJit()->CanEncodeClass(
259 klass.Get(),
Vladimir Marko695348f2020-05-19 14:42:02 +0100260 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000261 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
262 } else {
263 // Shared JIT code cannot encode a literal that the GC can move.
264 VLOG(jit) << "Unable to encode in shared region class literal: "
265 << klass->PrettyClass();
266 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
267 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000268 } else {
269 // Class not loaded yet. This happens when the dex code requesting
270 // this `HLoadClass` hasn't been executed in the interpreter.
271 // Fallback to the dex cache.
272 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100273 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000274 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100275 } else if (is_in_boot_image) {
276 // AOT app compilation, boot image class.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100277 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000278 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100279 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000280 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
281 }
282 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000283 }
Vladimir Marko8f63f102020-09-28 12:10:28 +0100284 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
285
286 if (is_in_boot_image) {
287 load_class->MarkInBootImage();
288 }
289 HLoadClass::LoadKind load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000290
291 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Nicolas Geoffrayee7bd322022-01-17 09:17:23 +0000292 if (load_kind == HLoadClass::LoadKind::kRuntimeCall ||
293 load_kind == HLoadClass::LoadKind::kBssEntry ||
Vladimir Marko8f63f102020-09-28 12:10:28 +0100294 load_kind == HLoadClass::LoadKind::kBssEntryPublic ||
295 load_kind == HLoadClass::LoadKind::kBssEntryPackage) {
Nicolas Geoffrayee7bd322022-01-17 09:17:23 +0000296 // We actually cannot reference this class, we're forced to bail.
297 // We cannot reference this class with Bss, as the entrypoint will lookup the class
298 // in the caller's dex file, but that dex file does not reference the class.
Santiago Aboy Solanes69559a52022-02-09 11:37:33 +0000299 // TODO(solanes): We could theoretically enable this optimization for kBssEntry* but this
300 // requires some changes to the entrypoints, particularly artResolveTypeFromCode and
301 // artResolveTypeAndVerifyAccessFromCode. Currently, they assume that the `load_class`'s
302 // Dexfile and the `dex_compilation_unit` DexFile is the same and will try to use the type
303 // index in the incorrect DexFile by using the `caller`'s DexFile. A possibility is to add
304 // another parameter to it pointing to the correct DexFile to use.
Nicolas Geoffrayee7bd322022-01-17 09:17:23 +0000305 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100306 }
307 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000308 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100309}
310
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100311static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
Vladimir Marko175e7862018-03-27 09:03:13 +0000312 REQUIRES_SHARED(Locks::mutator_lock_) {
313 DCHECK(!klass->IsProxyClass());
314 DCHECK(!klass->IsArrayClass());
315
Vladimir Marko695348f2020-05-19 14:42:02 +0100316 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
317 if (compiler_options.IsJitCompiler()) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000318 // If we're JITting, try to assign a type check bitstring (fall through).
319 } else if (codegen->GetCompilerOptions().IsBootImage()) {
320 const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100321 if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000322 return false;
323 }
324 // If the target is a boot image class, try to assign a type check bitstring (fall through).
325 // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
326 } else {
327 // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
328 // already assigned in the boot image.
329 return false;
330 }
331
332 // Try to assign a type check bitstring.
333 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
Mathieu Chartiera297b552018-11-07 11:41:01 -0800334 if ((false) && // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
Vladimir Marko175e7862018-03-27 09:03:13 +0000335 // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
336 kIsDebugBuild &&
Vladimir Marko695348f2020-05-19 14:42:02 +0100337 compiler_options.IsBootImage() &&
338 compiler_options.IsForceDeterminism()) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000339 SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
340 CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
341 << klass->PrettyDescriptor() << "/" << old_state
342 << " in " << codegen->GetGraph()->PrettyMethod();
343 }
344 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
345 return state == SubtypeCheckInfo::kAssigned;
346}
347
348TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
349 CodeGenerator* codegen,
Vladimir Marko175e7862018-03-27 09:03:13 +0000350 bool needs_access_check) {
351 if (klass == nullptr) {
352 return TypeCheckKind::kUnresolvedCheck;
353 } else if (klass->IsInterface()) {
354 return TypeCheckKind::kInterfaceCheck;
355 } else if (klass->IsArrayClass()) {
356 if (klass->GetComponentType()->IsObjectClass()) {
357 return TypeCheckKind::kArrayObjectCheck;
358 } else if (klass->CannotBeAssignedFromOtherTypes()) {
359 return TypeCheckKind::kExactCheck;
360 } else {
361 return TypeCheckKind::kArrayCheck;
362 }
363 } else if (klass->IsFinal()) { // TODO: Consider using bitstring for final classes.
364 return TypeCheckKind::kExactCheck;
365 } else if (kBitstringSubtypeCheckEnabled &&
366 !needs_access_check &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100367 CanUseTypeCheckBitstring(klass, codegen)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000368 // TODO: We should not need the `!needs_access_check` check but getting rid of that
369 // requires rewriting some optimizations in instruction simplifier.
370 return TypeCheckKind::kBitstringCheck;
371 } else if (klass->IsAbstract()) {
372 return TypeCheckKind::kAbstractClassCheck;
373 } else {
374 return TypeCheckKind::kClassHierarchyCheck;
375 }
376}
377
Vladimir Marko28e012a2017-12-07 11:22:59 +0000378void HSharpening::ProcessLoadString(
379 HLoadString* load_string,
380 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000381 const DexCompilationUnit& dex_compilation_unit,
382 VariableSizedHandleScope* handles) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100383 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000384
385 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800386 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000387
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000388 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000389 {
390 Runtime* runtime = Runtime::Current();
391 ClassLinker* class_linker = runtime->GetClassLinker();
392 ScopedObjectAccess soa(Thread::Current());
393 StackHandleScope<1> hs(soa.Self());
Vladimir Marko28e012a2017-12-07 11:22:59 +0000394 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
395 ? dex_compilation_unit.GetDexCache()
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000396 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Vladimir Marko28e012a2017-12-07 11:22:59 +0000397 ObjPtr<mirror::String> string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000398
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100399 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Vladimir Marko44ca0752019-07-29 10:18:25 +0100400 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
401 // Compiling boot image or boot image extension. Resolve the string and allocate it
402 // if needed, to ensure the string will be added to the boot image.
Vladimir Marko695348f2020-05-19 14:42:02 +0100403 DCHECK(!compiler_options.IsJitCompiler());
Vladimir Markobb089b62018-06-28 17:30:16 +0100404 if (compiler_options.GetCompilePic()) {
Vladimir Marko403aafa2019-03-06 18:04:14 +0000405 if (compiler_options.IsForceDeterminism()) {
406 // Strings for methods we're compiling should be pre-resolved but Strings in inlined
407 // methods may not be if these inlined methods are not in the boot image profile.
408 // Multiple threads allocating new Strings can cause non-deterministic boot image
409 // because of the image relying on the order of GC roots we walk. (We could fix that
410 // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
411 // strings even if that results in omitting them from the boot image and using the
412 // sub-optimal load kind kBssEntry.
413 string = class_linker->LookupString(string_index, dex_cache.Get());
414 } else {
415 string = class_linker->ResolveString(string_index, dex_cache);
416 CHECK(string != nullptr);
417 }
418 if (string != nullptr) {
Vladimir Marko44ca0752019-07-29 10:18:25 +0100419 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
420 DCHECK(compiler_options.IsBootImageExtension());
421 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
422 } else {
423 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
424 }
Vladimir Marko403aafa2019-03-06 18:04:14 +0000425 } else {
426 desired_load_kind = HLoadString::LoadKind::kBssEntry;
427 }
Vladimir Marko95026872016-09-09 09:16:31 +0000428 } else {
Vladimir Markobb089b62018-06-28 17:30:16 +0100429 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100430 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000431 }
Vladimir Marko695348f2020-05-19 14:42:02 +0100432 } else if (compiler_options.IsJitCompiler()) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000433 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000434 string = class_linker->LookupString(string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000435 if (string != nullptr) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000436 gc::Heap* heap = runtime->GetHeap();
437 if (heap->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100438 desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000439 } else if (runtime->GetJit()->CanEncodeString(
440 string,
Vladimir Marko695348f2020-05-19 14:42:02 +0100441 compiler_options.IsJitCompilerForSharedCode())) {
Nicolas Geoffray2fef66b2019-06-26 22:00:02 +0000442 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000443 } else {
444 // Shared JIT code cannot encode a literal that the GC can move.
445 VLOG(jit) << "Unable to encode in shared region string literal: "
446 << string->ToModifiedUtf8();
447 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000448 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000449 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100450 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100451 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000452 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100453 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000454 string = class_linker->LookupString(string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100455 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100456 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000457 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000458 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000459 }
460 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000461 if (string != nullptr) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000462 load_string->SetString(handles->NewHandle(string));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000463 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000464 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000465 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000466
Vladimir Marko28e012a2017-12-07 11:22:59 +0000467 HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000468 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000469}
470
Vladimir Markodc151b22015-10-15 18:02:30 +0100471} // namespace art