blob: b8471e372144e3a9b402747234081eb498444f71 [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 Geoffraybdb2ecc2018-09-18 14:33:55 +010060HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenInvokeStaticOrDirect(
61 ArtMethod* callee, CodeGenerator* codegen) {
62 if (kIsDebugBuild) {
63 ScopedObjectAccess soa(Thread::Current()); // Required for GetDeclaringClass below.
64 DCHECK(callee != nullptr);
65 DCHECK(!(callee->IsConstructor() && callee->GetDeclaringClass()->IsStringClass()));
Vladimir Markodc151b22015-10-15 18:02:30 +010066 }
67
Vladimir Markodc151b22015-10-15 18:02:30 +010068 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
69 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
70 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010071
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000072 // Note: we never call an ArtMethod through a known code pointer, as
73 // we do not want to keep on invoking it if it gets deoptimized. This
74 // applies to both AOT and JIT.
75 // This also avoids having to find out if the code pointer of an ArtMethod
76 // is the resolution trampoline (for ensuring the class is initialized), or
77 // the interpreter entrypoint. Such code pointers we do not want to call
78 // directly.
79 // Only in the case of a recursive call can we call directly, as we know the
80 // class is initialized already or being initialized, and the call will not
81 // be invoked once the method is deoptimized.
82
Alex Light1ebe4fe2017-01-30 14:57:11 -080083 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
84 // situations.
Vladimir Markobb089b62018-06-28 17:30:16 +010085 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000086 if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000087 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +010088 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
89 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Vladimir Marko44ca0752019-07-29 10:18:25 +010090 } else if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
Vladimir Markobb089b62018-06-28 17:30:16 +010091 if (!compiler_options.GetCompilePic()) {
92 // Test configuration, do not sharpen.
93 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
Vladimir Marko44ca0752019-07-29 10:18:25 +010094 } else if (IsInBootImage(callee)) {
95 DCHECK(compiler_options.IsBootImageExtension());
96 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
Vladimir Markobb089b62018-06-28 17:30:16 +010097 } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
98 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative;
99 } else {
100 // Use PC-relative access to the .bss methods array.
101 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
102 }
103 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100104 } else if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +0100105 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000106 if (Runtime::Current()->GetJit()->CanEncodeMethod(
107 callee,
108 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
109 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress;
110 method_load_data = reinterpret_cast<uintptr_t>(callee);
111 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
112 } else {
113 // Do not sharpen.
114 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
115 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
116 }
Vladimir Markob066d432018-01-03 13:14:37 +0000117 } else if (IsInBootImage(callee)) {
118 // Use PC-relative access to the .data.bimg.rel.ro methods array.
119 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
Vladimir Markob066d432018-01-03 13:14:37 +0000120 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100121 } else {
Vladimir Markob066d432018-01-03 13:14:37 +0000122 // Use PC-relative access to the .bss methods array.
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100123 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000124 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100125 }
126
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000127 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100128 // For debuggable apps always use the code pointer from ArtMethod
129 // so that we don't circumvent instrumentation stubs if installed.
130 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
131 }
132
133 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000134 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100135 };
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100136 return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
Vladimir Markodc151b22015-10-15 18:02:30 +0100137}
138
Vladimir Marko28e012a2017-12-07 11:22:59 +0000139HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
140 HLoadClass* load_class,
141 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000142 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000143 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100144 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700145 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
146 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700147 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100148
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000149 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
150
Vladimir Marko41559982017-01-06 14:04:23 +0000151 if (load_class->NeedsAccessCheck()) {
152 // We need to call the runtime anyway, so we simply get the class as that call's return value.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000153 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000154 // Loading from the ArtMethod* is the most efficient retrieval in code size.
155 // TODO: This may not actually be true for all architectures and
156 // locations of target classes. The additional register pressure
157 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000158 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000159 const DexFile& dex_file = load_class->GetDexFile();
160 dex::TypeIndex type_index = load_class->GetTypeIndex();
161
162 bool is_in_boot_image = false;
163 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
164 Runtime* runtime = Runtime::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100165 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Vladimir Marko44ca0752019-07-29 10:18:25 +0100166 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
167 // Compiling boot image or boot image extension. Check if the class is a boot image class.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000168 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100169 if (!compiler_options.GetCompilePic()) {
170 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100171 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko44ca0752019-07-29 10:18:25 +0100172 } else if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get())) {
173 DCHECK(compiler_options.IsBootImageExtension());
174 is_in_boot_image = true;
175 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Vladimir Marko65979462017-05-19 17:25:12 +0100176 } else if ((klass != nullptr) &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100177 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000178 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100179 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000180 } else {
181 // Not a boot image class.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000182 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100183 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000184 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800185 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000186 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
187 if (runtime->UseJitCompilation()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100188 DCHECK(!compiler_options.GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000189 if (is_in_boot_image) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100190 desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800191 } else if (klass != nullptr) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000192 if (runtime->GetJit()->CanEncodeClass(
193 klass.Get(),
194 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
195 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
196 } else {
197 // Shared JIT code cannot encode a literal that the GC can move.
198 VLOG(jit) << "Unable to encode in shared region class literal: "
199 << klass->PrettyClass();
200 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
201 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000202 } else {
203 // Class not loaded yet. This happens when the dex code requesting
204 // this `HLoadClass` hasn't been executed in the interpreter.
205 // Fallback to the dex cache.
206 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100207 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000208 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100209 } else if (is_in_boot_image) {
210 // AOT app compilation, boot image class.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100211 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000212 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100213 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000214 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
215 }
216 }
217 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
218
219 if (is_in_boot_image) {
220 load_class->MarkInBootImage();
221 }
222 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
223 }
224
225 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100226 if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000227 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
228 // We actually cannot reference this class, we're forced to bail.
229 // We cannot reference this class with Bss, as the entrypoint will lookup the class
230 // in the caller's dex file, but that dex file does not reference the class.
231 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100232 }
233 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000234 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100235}
236
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100237static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
Vladimir Marko175e7862018-03-27 09:03:13 +0000238 REQUIRES_SHARED(Locks::mutator_lock_) {
239 DCHECK(!klass->IsProxyClass());
240 DCHECK(!klass->IsArrayClass());
241
242 if (Runtime::Current()->UseJitCompilation()) {
243 // If we're JITting, try to assign a type check bitstring (fall through).
244 } else if (codegen->GetCompilerOptions().IsBootImage()) {
245 const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100246 if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000247 return false;
248 }
249 // If the target is a boot image class, try to assign a type check bitstring (fall through).
250 // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
251 } else {
252 // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
253 // already assigned in the boot image.
254 return false;
255 }
256
257 // Try to assign a type check bitstring.
258 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
Mathieu Chartiera297b552018-11-07 11:41:01 -0800259 if ((false) && // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
Vladimir Marko175e7862018-03-27 09:03:13 +0000260 // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
261 kIsDebugBuild &&
262 codegen->GetCompilerOptions().IsBootImage() &&
263 codegen->GetCompilerOptions().IsForceDeterminism()) {
264 SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
265 CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
266 << klass->PrettyDescriptor() << "/" << old_state
267 << " in " << codegen->GetGraph()->PrettyMethod();
268 }
269 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
270 return state == SubtypeCheckInfo::kAssigned;
271}
272
273TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
274 CodeGenerator* codegen,
Vladimir Marko175e7862018-03-27 09:03:13 +0000275 bool needs_access_check) {
276 if (klass == nullptr) {
277 return TypeCheckKind::kUnresolvedCheck;
278 } else if (klass->IsInterface()) {
279 return TypeCheckKind::kInterfaceCheck;
280 } else if (klass->IsArrayClass()) {
281 if (klass->GetComponentType()->IsObjectClass()) {
282 return TypeCheckKind::kArrayObjectCheck;
283 } else if (klass->CannotBeAssignedFromOtherTypes()) {
284 return TypeCheckKind::kExactCheck;
285 } else {
286 return TypeCheckKind::kArrayCheck;
287 }
288 } else if (klass->IsFinal()) { // TODO: Consider using bitstring for final classes.
289 return TypeCheckKind::kExactCheck;
290 } else if (kBitstringSubtypeCheckEnabled &&
291 !needs_access_check &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100292 CanUseTypeCheckBitstring(klass, codegen)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000293 // TODO: We should not need the `!needs_access_check` check but getting rid of that
294 // requires rewriting some optimizations in instruction simplifier.
295 return TypeCheckKind::kBitstringCheck;
296 } else if (klass->IsAbstract()) {
297 return TypeCheckKind::kAbstractClassCheck;
298 } else {
299 return TypeCheckKind::kClassHierarchyCheck;
300 }
301}
302
Vladimir Marko28e012a2017-12-07 11:22:59 +0000303void HSharpening::ProcessLoadString(
304 HLoadString* load_string,
305 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000306 const DexCompilationUnit& dex_compilation_unit,
307 VariableSizedHandleScope* handles) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100308 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000309
310 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800311 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000312
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000313 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000314 {
315 Runtime* runtime = Runtime::Current();
316 ClassLinker* class_linker = runtime->GetClassLinker();
317 ScopedObjectAccess soa(Thread::Current());
318 StackHandleScope<1> hs(soa.Self());
Vladimir Marko28e012a2017-12-07 11:22:59 +0000319 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
320 ? dex_compilation_unit.GetDexCache()
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000321 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Vladimir Marko28e012a2017-12-07 11:22:59 +0000322 ObjPtr<mirror::String> string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000323
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100324 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Vladimir Marko44ca0752019-07-29 10:18:25 +0100325 if (compiler_options.IsBootImage() || compiler_options.IsBootImageExtension()) {
326 // Compiling boot image or boot image extension. Resolve the string and allocate it
327 // if needed, to ensure the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100328 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100329 if (compiler_options.GetCompilePic()) {
Vladimir Marko403aafa2019-03-06 18:04:14 +0000330 if (compiler_options.IsForceDeterminism()) {
331 // Strings for methods we're compiling should be pre-resolved but Strings in inlined
332 // methods may not be if these inlined methods are not in the boot image profile.
333 // Multiple threads allocating new Strings can cause non-deterministic boot image
334 // because of the image relying on the order of GC roots we walk. (We could fix that
335 // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
336 // strings even if that results in omitting them from the boot image and using the
337 // sub-optimal load kind kBssEntry.
338 string = class_linker->LookupString(string_index, dex_cache.Get());
339 } else {
340 string = class_linker->ResolveString(string_index, dex_cache);
341 CHECK(string != nullptr);
342 }
343 if (string != nullptr) {
Vladimir Marko44ca0752019-07-29 10:18:25 +0100344 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
345 DCHECK(compiler_options.IsBootImageExtension());
346 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
347 } else {
348 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
349 }
Vladimir Marko403aafa2019-03-06 18:04:14 +0000350 } else {
351 desired_load_kind = HLoadString::LoadKind::kBssEntry;
352 }
Vladimir Marko95026872016-09-09 09:16:31 +0000353 } else {
Vladimir Markobb089b62018-06-28 17:30:16 +0100354 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100355 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000356 }
Calin Juravleffc87072016-04-20 14:22:09 +0100357 } else if (runtime->UseJitCompilation()) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000358 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000359 string = class_linker->LookupString(string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000360 if (string != nullptr) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000361 gc::Heap* heap = runtime->GetHeap();
362 if (heap->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100363 desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000364 } else if (runtime->GetJit()->CanEncodeString(
365 string,
366 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
Nicolas Geoffray2fef66b2019-06-26 22:00:02 +0000367 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000368 } else {
369 // Shared JIT code cannot encode a literal that the GC can move.
370 VLOG(jit) << "Unable to encode in shared region string literal: "
371 << string->ToModifiedUtf8();
372 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000373 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000374 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100375 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100376 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000377 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100378 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000379 string = class_linker->LookupString(string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100380 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100381 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000382 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000383 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000384 }
385 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000386 if (string != nullptr) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000387 load_string->SetString(handles->NewHandle(string));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000388 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000389 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000390 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000391
Vladimir Marko28e012a2017-12-07 11:22:59 +0000392 HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000393 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000394}
395
Vladimir Markodc151b22015-10-15 18:02:30 +0100396} // namespace art