blob: 3e22edc773f56bcc3ece642af49d9180f2226d49 [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 Geoffray05f87212019-06-19 10:00:00 +010022#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 Geoffray05f87212019-06-19 10:00:00 +010030#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 Geoffray05f87212019-06-19 10:00:00 +0100103 if (Runtime::Current()->GetJit()->CanEncodeMethod(
104 callee,
105 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
106 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress;
107 method_load_data = reinterpret_cast<uintptr_t>(callee);
108 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
109 } else {
110 // Do not sharpen.
111 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
112 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
113 }
Vladimir Markob066d432018-01-03 13:14:37 +0000114 } else if (IsInBootImage(callee)) {
115 // Use PC-relative access to the .data.bimg.rel.ro methods array.
116 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
Vladimir Markob066d432018-01-03 13:14:37 +0000117 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100118 } else {
Vladimir Markob066d432018-01-03 13:14:37 +0000119 // Use PC-relative access to the .bss methods array.
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100120 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000121 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100122 }
123
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000124 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100125 // For debuggable apps always use the code pointer from ArtMethod
126 // so that we don't circumvent instrumentation stubs if installed.
127 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
128 }
129
130 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000131 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100132 };
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100133 return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
Vladimir Markodc151b22015-10-15 18:02:30 +0100134}
135
Vladimir Marko28e012a2017-12-07 11:22:59 +0000136HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
137 HLoadClass* load_class,
138 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000139 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000140 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100141 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700142 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
143 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700144 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100145
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000146 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
147
Vladimir Marko41559982017-01-06 14:04:23 +0000148 if (load_class->NeedsAccessCheck()) {
149 // 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 +0000150 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000151 // Loading from the ArtMethod* is the most efficient retrieval in code size.
152 // TODO: This may not actually be true for all architectures and
153 // locations of target classes. The additional register pressure
154 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000155 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000156 const DexFile& dex_file = load_class->GetDexFile();
157 dex::TypeIndex type_index = load_class->GetTypeIndex();
158
159 bool is_in_boot_image = false;
160 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
161 Runtime* runtime = Runtime::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100162 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
163 if (compiler_options.IsBootImage()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000164 // Compiling boot image. Check if the class is a boot image class.
165 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100166 if (!compiler_options.GetCompilePic()) {
167 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100168 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko65979462017-05-19 17:25:12 +0100169 } else if ((klass != nullptr) &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100170 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000171 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100172 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000173 } else {
174 // Not a boot image class.
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100175 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000176 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100177 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000178 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800179 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000180 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
181 if (runtime->UseJitCompilation()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100182 DCHECK(!compiler_options.GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000183 if (is_in_boot_image) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100184 desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800185 } else if (klass != nullptr) {
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100186 if (runtime->GetJit()->CanEncodeClass(
187 klass.Get(),
188 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
189 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
190 } else {
191 // Shared JIT code cannot encode a literal that the GC can move.
192 VLOG(jit) << "Unable to encode in shared region class literal: "
193 << klass->PrettyClass();
194 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
195 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000196 } else {
197 // Class not loaded yet. This happens when the dex code requesting
198 // this `HLoadClass` hasn't been executed in the interpreter.
199 // Fallback to the dex cache.
200 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100201 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000202 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100203 } else if (is_in_boot_image) {
204 // AOT app compilation, boot image class.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100205 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000206 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100207 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000208 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
209 }
210 }
211 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
212
213 if (is_in_boot_image) {
214 load_class->MarkInBootImage();
215 }
216 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
217 }
218
219 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100220 if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000221 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
222 // We actually cannot reference this class, we're forced to bail.
223 // We cannot reference this class with Bss, as the entrypoint will lookup the class
224 // in the caller's dex file, but that dex file does not reference the class.
225 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100226 }
227 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000228 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100229}
230
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100231static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
Vladimir Marko175e7862018-03-27 09:03:13 +0000232 REQUIRES_SHARED(Locks::mutator_lock_) {
233 DCHECK(!klass->IsProxyClass());
234 DCHECK(!klass->IsArrayClass());
235
236 if (Runtime::Current()->UseJitCompilation()) {
237 // If we're JITting, try to assign a type check bitstring (fall through).
238 } else if (codegen->GetCompilerOptions().IsBootImage()) {
239 const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100240 if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000241 return false;
242 }
243 // If the target is a boot image class, try to assign a type check bitstring (fall through).
244 // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
245 } else {
246 // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
247 // already assigned in the boot image.
248 return false;
249 }
250
251 // Try to assign a type check bitstring.
252 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
Mathieu Chartiera297b552018-11-07 11:41:01 -0800253 if ((false) && // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
Vladimir Marko175e7862018-03-27 09:03:13 +0000254 // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
255 kIsDebugBuild &&
256 codegen->GetCompilerOptions().IsBootImage() &&
257 codegen->GetCompilerOptions().IsForceDeterminism()) {
258 SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
259 CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
260 << klass->PrettyDescriptor() << "/" << old_state
261 << " in " << codegen->GetGraph()->PrettyMethod();
262 }
263 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
264 return state == SubtypeCheckInfo::kAssigned;
265}
266
267TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
268 CodeGenerator* codegen,
Vladimir Marko175e7862018-03-27 09:03:13 +0000269 bool needs_access_check) {
270 if (klass == nullptr) {
271 return TypeCheckKind::kUnresolvedCheck;
272 } else if (klass->IsInterface()) {
273 return TypeCheckKind::kInterfaceCheck;
274 } else if (klass->IsArrayClass()) {
275 if (klass->GetComponentType()->IsObjectClass()) {
276 return TypeCheckKind::kArrayObjectCheck;
277 } else if (klass->CannotBeAssignedFromOtherTypes()) {
278 return TypeCheckKind::kExactCheck;
279 } else {
280 return TypeCheckKind::kArrayCheck;
281 }
282 } else if (klass->IsFinal()) { // TODO: Consider using bitstring for final classes.
283 return TypeCheckKind::kExactCheck;
284 } else if (kBitstringSubtypeCheckEnabled &&
285 !needs_access_check &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100286 CanUseTypeCheckBitstring(klass, codegen)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000287 // TODO: We should not need the `!needs_access_check` check but getting rid of that
288 // requires rewriting some optimizations in instruction simplifier.
289 return TypeCheckKind::kBitstringCheck;
290 } else if (klass->IsAbstract()) {
291 return TypeCheckKind::kAbstractClassCheck;
292 } else {
293 return TypeCheckKind::kClassHierarchyCheck;
294 }
295}
296
Vladimir Marko28e012a2017-12-07 11:22:59 +0000297void HSharpening::ProcessLoadString(
298 HLoadString* load_string,
299 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000300 const DexCompilationUnit& dex_compilation_unit,
301 VariableSizedHandleScope* handles) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100302 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000303
304 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800305 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000306
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000307 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000308 {
309 Runtime* runtime = Runtime::Current();
310 ClassLinker* class_linker = runtime->GetClassLinker();
311 ScopedObjectAccess soa(Thread::Current());
312 StackHandleScope<1> hs(soa.Self());
Vladimir Marko28e012a2017-12-07 11:22:59 +0000313 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
314 ? dex_compilation_unit.GetDexCache()
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000315 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Vladimir Marko28e012a2017-12-07 11:22:59 +0000316 ObjPtr<mirror::String> string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000317
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100318 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
319 if (compiler_options.IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000320 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
321 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100322 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100323 if (compiler_options.GetCompilePic()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100324 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Vladimir Marko403aafa2019-03-06 18:04:14 +0000325 if (compiler_options.IsForceDeterminism()) {
326 // Strings for methods we're compiling should be pre-resolved but Strings in inlined
327 // methods may not be if these inlined methods are not in the boot image profile.
328 // Multiple threads allocating new Strings can cause non-deterministic boot image
329 // because of the image relying on the order of GC roots we walk. (We could fix that
330 // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
331 // strings even if that results in omitting them from the boot image and using the
332 // sub-optimal load kind kBssEntry.
333 string = class_linker->LookupString(string_index, dex_cache.Get());
334 } else {
335 string = class_linker->ResolveString(string_index, dex_cache);
336 CHECK(string != nullptr);
337 }
338 if (string != nullptr) {
339 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
340 } else {
341 desired_load_kind = HLoadString::LoadKind::kBssEntry;
342 }
Vladimir Marko95026872016-09-09 09:16:31 +0000343 } else {
Vladimir Markobb089b62018-06-28 17:30:16 +0100344 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100345 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000346 }
Calin Juravleffc87072016-04-20 14:22:09 +0100347 } else if (runtime->UseJitCompilation()) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000348 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000349 string = class_linker->LookupString(string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000350 if (string != nullptr) {
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100351 gc::Heap* heap = runtime->GetHeap();
352 if (heap->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100353 desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100354 } else if (runtime->GetJit()->CanEncodeString(
355 string,
356 codegen->GetGraph()->IsCompilingForSharedJitCode())) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000357 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
Nicolas Geoffray05f87212019-06-19 10:00:00 +0100358 } else {
359 // Shared JIT code cannot encode a literal that the GC can move.
360 VLOG(jit) << "Unable to encode in shared region string literal: "
361 << string->ToModifiedUtf8();
362 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000363 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000364 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100365 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100366 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000367 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100368 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000369 string = class_linker->LookupString(string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100370 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100371 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000372 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000373 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000374 }
375 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000376 if (string != nullptr) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000377 load_string->SetString(handles->NewHandle(string));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000378 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000379 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000380 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000381
Vladimir Marko28e012a2017-12-07 11:22:59 +0000382 HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000383 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000384}
385
Vladimir Markodc151b22015-10-15 18:02:30 +0100386} // namespace art