blob: 03d277f648e3484d9ce6428ce1bcbd346b84b25a [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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070036#include "utils/dex_cache_arrays_layout-inl.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010037
38namespace art {
39
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000040static bool IsInBootImage(ArtMethod* method) {
41 const std::vector<gc::space::ImageSpace*>& image_spaces =
42 Runtime::Current()->GetHeap()->GetBootImageSpaces();
43 for (gc::space::ImageSpace* image_space : image_spaces) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +010044 const ImageSection& method_section = image_space->GetImageHeader().GetMethodsSection();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000045 if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
46 return true;
47 }
48 }
49 return false;
50}
51
Vladimir Markobb089b62018-06-28 17:30:16 +010052static bool BootImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
Vladimir Markodc4bcce2018-06-21 16:15:42 +010053 DCHECK(compiler_options.IsBootImage());
Vladimir Marko65979462017-05-19 17:25:12 +010054 ScopedObjectAccess soa(Thread::Current());
55 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
56 DCHECK(klass != nullptr);
57 const DexFile& dex_file = klass->GetDexFile();
Vladimir Markodc4bcce2018-06-21 16:15:42 +010058 return compiler_options.IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
Vladimir Marko65979462017-05-19 17:25:12 +010059}
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000060
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +010061HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenInvokeStaticOrDirect(
62 ArtMethod* callee, CodeGenerator* codegen) {
63 if (kIsDebugBuild) {
64 ScopedObjectAccess soa(Thread::Current()); // Required for GetDeclaringClass below.
65 DCHECK(callee != nullptr);
66 DCHECK(!(callee->IsConstructor() && callee->GetDeclaringClass()->IsStringClass()));
Vladimir Markodc151b22015-10-15 18:02:30 +010067 }
68
Vladimir Markodc151b22015-10-15 18:02:30 +010069 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
70 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
71 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010072
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000073 // Note: we never call an ArtMethod through a known code pointer, as
74 // we do not want to keep on invoking it if it gets deoptimized. This
75 // applies to both AOT and JIT.
76 // This also avoids having to find out if the code pointer of an ArtMethod
77 // is the resolution trampoline (for ensuring the class is initialized), or
78 // the interpreter entrypoint. Such code pointers we do not want to call
79 // directly.
80 // Only in the case of a recursive call can we call directly, as we know the
81 // class is initialized already or being initialized, and the call will not
82 // be invoked once the method is deoptimized.
83
Alex Light1ebe4fe2017-01-30 14:57:11 -080084 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
85 // situations.
Vladimir Markobb089b62018-06-28 17:30:16 +010086 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000087 if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000088 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +010089 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
90 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Vladimir Markobb089b62018-06-28 17:30:16 +010091 } else if (compiler_options.IsBootImage()) {
92 if (!compiler_options.GetCompilePic()) {
93 // Test configuration, do not sharpen.
94 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
95 } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
96 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative;
97 } else {
98 // Use PC-relative access to the .bss methods array.
99 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
100 }
101 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100102 } else if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffray05b41c42019-06-28 12:46:33 +0100103 ScopedObjectAccess soa(Thread::Current());
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000104 if (Runtime::Current()->GetJit()->CanEncodeMethod(
105 callee,
106 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
107 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress;
108 method_load_data = reinterpret_cast<uintptr_t>(callee);
109 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
110 } else {
111 // Do not sharpen.
112 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
113 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
114 }
Vladimir Markob066d432018-01-03 13:14:37 +0000115 } else if (IsInBootImage(callee)) {
116 // Use PC-relative access to the .data.bimg.rel.ro methods array.
117 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
Vladimir Markob066d432018-01-03 13:14:37 +0000118 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100119 } else {
Vladimir Markob066d432018-01-03 13:14:37 +0000120 // Use PC-relative access to the .bss methods array.
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100121 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000122 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100123 }
124
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000125 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100126 // For debuggable apps always use the code pointer from ArtMethod
127 // so that we don't circumvent instrumentation stubs if installed.
128 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
129 }
130
131 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000132 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100133 };
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100134 return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
Vladimir Markodc151b22015-10-15 18:02:30 +0100135}
136
Vladimir Marko28e012a2017-12-07 11:22:59 +0000137HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
138 HLoadClass* load_class,
139 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000140 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000141 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100142 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700143 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
144 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700145 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100146
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000147 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
148
Vladimir Marko41559982017-01-06 14:04:23 +0000149 if (load_class->NeedsAccessCheck()) {
150 // 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 +0000151 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000152 // Loading from the ArtMethod* is the most efficient retrieval in code size.
153 // TODO: This may not actually be true for all architectures and
154 // locations of target classes. The additional register pressure
155 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000156 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000157 const DexFile& dex_file = load_class->GetDexFile();
158 dex::TypeIndex type_index = load_class->GetTypeIndex();
159
160 bool is_in_boot_image = false;
161 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
162 Runtime* runtime = Runtime::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100163 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
164 if (compiler_options.IsBootImage()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000165 // Compiling boot image. Check if the class is a boot image class.
166 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100167 if (!compiler_options.GetCompilePic()) {
168 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100169 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko65979462017-05-19 17:25:12 +0100170 } else if ((klass != nullptr) &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100171 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000172 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100173 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000174 } else {
175 // Not a boot image class.
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100176 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000177 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100178 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000179 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800180 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000181 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
182 if (runtime->UseJitCompilation()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100183 DCHECK(!compiler_options.GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000184 if (is_in_boot_image) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100185 desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800186 } else if (klass != nullptr) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000187 if (runtime->GetJit()->CanEncodeClass(
188 klass.Get(),
189 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
190 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
191 } else {
192 // Shared JIT code cannot encode a literal that the GC can move.
193 VLOG(jit) << "Unable to encode in shared region class literal: "
194 << klass->PrettyClass();
195 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
196 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000197 } else {
198 // Class not loaded yet. This happens when the dex code requesting
199 // this `HLoadClass` hasn't been executed in the interpreter.
200 // Fallback to the dex cache.
201 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100202 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000203 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100204 } else if (is_in_boot_image) {
205 // AOT app compilation, boot image class.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100206 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000207 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100208 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000209 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
210 }
211 }
212 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
213
214 if (is_in_boot_image) {
215 load_class->MarkInBootImage();
216 }
217 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
218 }
219
220 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100221 if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000222 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
223 // We actually cannot reference this class, we're forced to bail.
224 // We cannot reference this class with Bss, as the entrypoint will lookup the class
225 // in the caller's dex file, but that dex file does not reference the class.
226 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100227 }
228 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000229 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100230}
231
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100232static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
Vladimir Marko175e7862018-03-27 09:03:13 +0000233 REQUIRES_SHARED(Locks::mutator_lock_) {
234 DCHECK(!klass->IsProxyClass());
235 DCHECK(!klass->IsArrayClass());
236
237 if (Runtime::Current()->UseJitCompilation()) {
238 // If we're JITting, try to assign a type check bitstring (fall through).
239 } else if (codegen->GetCompilerOptions().IsBootImage()) {
240 const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100241 if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000242 return false;
243 }
244 // If the target is a boot image class, try to assign a type check bitstring (fall through).
245 // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
246 } else {
247 // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
248 // already assigned in the boot image.
249 return false;
250 }
251
252 // Try to assign a type check bitstring.
253 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
Mathieu Chartiera297b552018-11-07 11:41:01 -0800254 if ((false) && // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
Vladimir Marko175e7862018-03-27 09:03:13 +0000255 // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
256 kIsDebugBuild &&
257 codegen->GetCompilerOptions().IsBootImage() &&
258 codegen->GetCompilerOptions().IsForceDeterminism()) {
259 SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
260 CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
261 << klass->PrettyDescriptor() << "/" << old_state
262 << " in " << codegen->GetGraph()->PrettyMethod();
263 }
264 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
265 return state == SubtypeCheckInfo::kAssigned;
266}
267
268TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
269 CodeGenerator* codegen,
Vladimir Marko175e7862018-03-27 09:03:13 +0000270 bool needs_access_check) {
271 if (klass == nullptr) {
272 return TypeCheckKind::kUnresolvedCheck;
273 } else if (klass->IsInterface()) {
274 return TypeCheckKind::kInterfaceCheck;
275 } else if (klass->IsArrayClass()) {
276 if (klass->GetComponentType()->IsObjectClass()) {
277 return TypeCheckKind::kArrayObjectCheck;
278 } else if (klass->CannotBeAssignedFromOtherTypes()) {
279 return TypeCheckKind::kExactCheck;
280 } else {
281 return TypeCheckKind::kArrayCheck;
282 }
283 } else if (klass->IsFinal()) { // TODO: Consider using bitstring for final classes.
284 return TypeCheckKind::kExactCheck;
285 } else if (kBitstringSubtypeCheckEnabled &&
286 !needs_access_check &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100287 CanUseTypeCheckBitstring(klass, codegen)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000288 // TODO: We should not need the `!needs_access_check` check but getting rid of that
289 // requires rewriting some optimizations in instruction simplifier.
290 return TypeCheckKind::kBitstringCheck;
291 } else if (klass->IsAbstract()) {
292 return TypeCheckKind::kAbstractClassCheck;
293 } else {
294 return TypeCheckKind::kClassHierarchyCheck;
295 }
296}
297
Vladimir Marko28e012a2017-12-07 11:22:59 +0000298void HSharpening::ProcessLoadString(
299 HLoadString* load_string,
300 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000301 const DexCompilationUnit& dex_compilation_unit,
302 VariableSizedHandleScope* handles) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100303 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000304
305 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800306 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000307
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000308 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000309 {
310 Runtime* runtime = Runtime::Current();
311 ClassLinker* class_linker = runtime->GetClassLinker();
312 ScopedObjectAccess soa(Thread::Current());
313 StackHandleScope<1> hs(soa.Self());
Vladimir Marko28e012a2017-12-07 11:22:59 +0000314 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
315 ? dex_compilation_unit.GetDexCache()
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000316 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Vladimir Marko28e012a2017-12-07 11:22:59 +0000317 ObjPtr<mirror::String> string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000318
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100319 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
320 if (compiler_options.IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000321 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
322 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100323 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100324 if (compiler_options.GetCompilePic()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100325 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Vladimir Marko403aafa2019-03-06 18:04:14 +0000326 if (compiler_options.IsForceDeterminism()) {
327 // Strings for methods we're compiling should be pre-resolved but Strings in inlined
328 // methods may not be if these inlined methods are not in the boot image profile.
329 // Multiple threads allocating new Strings can cause non-deterministic boot image
330 // because of the image relying on the order of GC roots we walk. (We could fix that
331 // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
332 // strings even if that results in omitting them from the boot image and using the
333 // sub-optimal load kind kBssEntry.
334 string = class_linker->LookupString(string_index, dex_cache.Get());
335 } else {
336 string = class_linker->ResolveString(string_index, dex_cache);
337 CHECK(string != nullptr);
338 }
339 if (string != nullptr) {
340 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
341 } else {
342 desired_load_kind = HLoadString::LoadKind::kBssEntry;
343 }
Vladimir Marko95026872016-09-09 09:16:31 +0000344 } else {
Vladimir Markobb089b62018-06-28 17:30:16 +0100345 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100346 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000347 }
Calin Juravleffc87072016-04-20 14:22:09 +0100348 } else if (runtime->UseJitCompilation()) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000349 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000350 string = class_linker->LookupString(string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000351 if (string != nullptr) {
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000352 gc::Heap* heap = runtime->GetHeap();
353 if (heap->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100354 desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000355 } else if (runtime->GetJit()->CanEncodeString(
356 string,
357 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
Nicolas Geoffray2fef66b2019-06-26 22:00:02 +0000358 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
Nicolas Geoffraya48c3df2019-06-27 13:11:12 +0000359 } else {
360 // Shared JIT code cannot encode a literal that the GC can move.
361 VLOG(jit) << "Unable to encode in shared region string literal: "
362 << string->ToModifiedUtf8();
363 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000364 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000365 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100366 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100367 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000368 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100369 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000370 string = class_linker->LookupString(string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100371 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100372 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000373 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000374 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000375 }
376 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000377 if (string != nullptr) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000378 load_string->SetString(handles->NewHandle(string));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000379 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000380 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000381 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000382
Vladimir Marko28e012a2017-12-07 11:22:59 +0000383 HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000384 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000385}
386
Vladimir Markodc151b22015-10-15 18:02:30 +0100387} // namespace art