blob: 8637db13ad458e2c88859178b4705c2ada22f61d [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"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000022#include "class_linker.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010023#include "code_generator.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010024#include "driver/compiler_options.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000025#include "driver/dex_compilation_unit.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000026#include "gc/heap.h"
27#include "gc/space/image_space.h"
28#include "handle_scope-inl.h"
29#include "mirror/dex_cache.h"
30#include "mirror/string.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010031#include "nodes.h"
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000032#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070033#include "scoped_thread_state_change-inl.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070034#include "utils/dex_cache_arrays_layout-inl.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010035
36namespace art {
37
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000038static bool IsInBootImage(ArtMethod* method) {
39 const std::vector<gc::space::ImageSpace*>& image_spaces =
40 Runtime::Current()->GetHeap()->GetBootImageSpaces();
41 for (gc::space::ImageSpace* image_space : image_spaces) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +010042 const ImageSection& method_section = image_space->GetImageHeader().GetMethodsSection();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000043 if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
44 return true;
45 }
46 }
47 return false;
48}
49
Vladimir Markobb089b62018-06-28 17:30:16 +010050static bool BootImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
Vladimir Markodc4bcce2018-06-21 16:15:42 +010051 DCHECK(compiler_options.IsBootImage());
Vladimir Marko65979462017-05-19 17:25:12 +010052 ScopedObjectAccess soa(Thread::Current());
53 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
54 DCHECK(klass != nullptr);
55 const DexFile& dex_file = klass->GetDexFile();
Vladimir Markodc4bcce2018-06-21 16:15:42 +010056 return compiler_options.IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
Vladimir Marko65979462017-05-19 17:25:12 +010057}
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000058
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +010059HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenInvokeStaticOrDirect(
60 ArtMethod* callee, CodeGenerator* codegen) {
61 if (kIsDebugBuild) {
62 ScopedObjectAccess soa(Thread::Current()); // Required for GetDeclaringClass below.
63 DCHECK(callee != nullptr);
64 DCHECK(!(callee->IsConstructor() && callee->GetDeclaringClass()->IsStringClass()));
Vladimir Markodc151b22015-10-15 18:02:30 +010065 }
66
Vladimir Markodc151b22015-10-15 18:02:30 +010067 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
68 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
69 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010070
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000071 // Note: we never call an ArtMethod through a known code pointer, as
72 // we do not want to keep on invoking it if it gets deoptimized. This
73 // applies to both AOT and JIT.
74 // This also avoids having to find out if the code pointer of an ArtMethod
75 // is the resolution trampoline (for ensuring the class is initialized), or
76 // the interpreter entrypoint. Such code pointers we do not want to call
77 // directly.
78 // Only in the case of a recursive call can we call directly, as we know the
79 // class is initialized already or being initialized, and the call will not
80 // be invoked once the method is deoptimized.
81
Alex Light1ebe4fe2017-01-30 14:57:11 -080082 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
83 // situations.
Vladimir Markobb089b62018-06-28 17:30:16 +010084 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000085 if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000086 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +010087 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
88 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Vladimir Markobb089b62018-06-28 17:30:16 +010089 } else if (compiler_options.IsBootImage()) {
90 if (!compiler_options.GetCompilePic()) {
91 // Test configuration, do not sharpen.
92 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
93 } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
94 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative;
95 } else {
96 // Use PC-relative access to the .bss methods array.
97 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
98 }
99 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100100 } else if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000101 // JIT or on-device AOT compilation referencing a boot image method.
102 // Use the method address directly.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100103 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000104 method_load_data = reinterpret_cast<uintptr_t>(callee);
105 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markob066d432018-01-03 13:14:37 +0000106 } else if (IsInBootImage(callee)) {
107 // Use PC-relative access to the .data.bimg.rel.ro methods array.
108 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
Vladimir Markob066d432018-01-03 13:14:37 +0000109 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100110 } else {
Vladimir Markob066d432018-01-03 13:14:37 +0000111 // Use PC-relative access to the .bss methods array.
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100112 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000113 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100114 }
115
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000116 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100117 // For debuggable apps always use the code pointer from ArtMethod
118 // so that we don't circumvent instrumentation stubs if installed.
119 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
120 }
121
122 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000123 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100124 };
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +0100125 return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
Vladimir Markodc151b22015-10-15 18:02:30 +0100126}
127
Vladimir Marko28e012a2017-12-07 11:22:59 +0000128HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
129 HLoadClass* load_class,
130 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000131 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000132 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100133 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700134 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
135 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700136 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100137
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000138 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
139
Vladimir Marko41559982017-01-06 14:04:23 +0000140 if (load_class->NeedsAccessCheck()) {
141 // 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 +0000142 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000143 // Loading from the ArtMethod* is the most efficient retrieval in code size.
144 // TODO: This may not actually be true for all architectures and
145 // locations of target classes. The additional register pressure
146 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000147 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000148 const DexFile& dex_file = load_class->GetDexFile();
149 dex::TypeIndex type_index = load_class->GetTypeIndex();
150
151 bool is_in_boot_image = false;
152 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
153 Runtime* runtime = Runtime::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100154 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
155 if (compiler_options.IsBootImage()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000156 // Compiling boot image. Check if the class is a boot image class.
157 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100158 if (!compiler_options.GetCompilePic()) {
159 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100160 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko65979462017-05-19 17:25:12 +0100161 } else if ((klass != nullptr) &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100162 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000163 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100164 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000165 } else {
166 // Not a boot image class.
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100167 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000168 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100169 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000170 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800171 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000172 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
173 if (runtime->UseJitCompilation()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100174 DCHECK(!compiler_options.GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000175 if (is_in_boot_image) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100176 desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800177 } else if (klass != nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000178 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
179 } else {
180 // Class not loaded yet. This happens when the dex code requesting
181 // this `HLoadClass` hasn't been executed in the interpreter.
182 // Fallback to the dex cache.
183 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100184 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000185 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100186 } else if (is_in_boot_image) {
187 // AOT app compilation, boot image class.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100188 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000189 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100190 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000191 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
192 }
193 }
194 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
195
196 if (is_in_boot_image) {
197 load_class->MarkInBootImage();
198 }
199 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
200 }
201
202 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100203 if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000204 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
205 // We actually cannot reference this class, we're forced to bail.
206 // We cannot reference this class with Bss, as the entrypoint will lookup the class
207 // in the caller's dex file, but that dex file does not reference the class.
208 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100209 }
210 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000211 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100212}
213
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100214static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
Vladimir Marko175e7862018-03-27 09:03:13 +0000215 REQUIRES_SHARED(Locks::mutator_lock_) {
216 DCHECK(!klass->IsProxyClass());
217 DCHECK(!klass->IsArrayClass());
218
219 if (Runtime::Current()->UseJitCompilation()) {
220 // If we're JITting, try to assign a type check bitstring (fall through).
221 } else if (codegen->GetCompilerOptions().IsBootImage()) {
222 const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100223 if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000224 return false;
225 }
226 // If the target is a boot image class, try to assign a type check bitstring (fall through).
227 // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
228 } else {
229 // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
230 // already assigned in the boot image.
231 return false;
232 }
233
234 // Try to assign a type check bitstring.
235 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
Mathieu Chartiera297b552018-11-07 11:41:01 -0800236 if ((false) && // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
Vladimir Marko175e7862018-03-27 09:03:13 +0000237 // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
238 kIsDebugBuild &&
239 codegen->GetCompilerOptions().IsBootImage() &&
240 codegen->GetCompilerOptions().IsForceDeterminism()) {
241 SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
242 CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
243 << klass->PrettyDescriptor() << "/" << old_state
244 << " in " << codegen->GetGraph()->PrettyMethod();
245 }
246 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
247 return state == SubtypeCheckInfo::kAssigned;
248}
249
250TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
251 CodeGenerator* codegen,
Vladimir Marko175e7862018-03-27 09:03:13 +0000252 bool needs_access_check) {
253 if (klass == nullptr) {
254 return TypeCheckKind::kUnresolvedCheck;
255 } else if (klass->IsInterface()) {
256 return TypeCheckKind::kInterfaceCheck;
257 } else if (klass->IsArrayClass()) {
258 if (klass->GetComponentType()->IsObjectClass()) {
259 return TypeCheckKind::kArrayObjectCheck;
260 } else if (klass->CannotBeAssignedFromOtherTypes()) {
261 return TypeCheckKind::kExactCheck;
262 } else {
263 return TypeCheckKind::kArrayCheck;
264 }
265 } else if (klass->IsFinal()) { // TODO: Consider using bitstring for final classes.
266 return TypeCheckKind::kExactCheck;
267 } else if (kBitstringSubtypeCheckEnabled &&
268 !needs_access_check &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100269 CanUseTypeCheckBitstring(klass, codegen)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000270 // TODO: We should not need the `!needs_access_check` check but getting rid of that
271 // requires rewriting some optimizations in instruction simplifier.
272 return TypeCheckKind::kBitstringCheck;
273 } else if (klass->IsAbstract()) {
274 return TypeCheckKind::kAbstractClassCheck;
275 } else {
276 return TypeCheckKind::kClassHierarchyCheck;
277 }
278}
279
Vladimir Marko28e012a2017-12-07 11:22:59 +0000280void HSharpening::ProcessLoadString(
281 HLoadString* load_string,
282 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000283 const DexCompilationUnit& dex_compilation_unit,
284 VariableSizedHandleScope* handles) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100285 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000286
287 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800288 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000289
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000290 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000291 {
292 Runtime* runtime = Runtime::Current();
293 ClassLinker* class_linker = runtime->GetClassLinker();
294 ScopedObjectAccess soa(Thread::Current());
295 StackHandleScope<1> hs(soa.Self());
Vladimir Marko28e012a2017-12-07 11:22:59 +0000296 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
297 ? dex_compilation_unit.GetDexCache()
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000298 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Vladimir Marko28e012a2017-12-07 11:22:59 +0000299 ObjPtr<mirror::String> string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000300
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100301 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
302 if (compiler_options.IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000303 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
304 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100305 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100306 if (compiler_options.GetCompilePic()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100307 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Vladimir Marko403aafa2019-03-06 18:04:14 +0000308 if (compiler_options.IsForceDeterminism()) {
309 // Strings for methods we're compiling should be pre-resolved but Strings in inlined
310 // methods may not be if these inlined methods are not in the boot image profile.
311 // Multiple threads allocating new Strings can cause non-deterministic boot image
312 // because of the image relying on the order of GC roots we walk. (We could fix that
313 // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
314 // strings even if that results in omitting them from the boot image and using the
315 // sub-optimal load kind kBssEntry.
316 string = class_linker->LookupString(string_index, dex_cache.Get());
317 } else {
318 string = class_linker->ResolveString(string_index, dex_cache);
319 CHECK(string != nullptr);
320 }
321 if (string != nullptr) {
322 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
323 } else {
324 desired_load_kind = HLoadString::LoadKind::kBssEntry;
325 }
Vladimir Marko95026872016-09-09 09:16:31 +0000326 } else {
Vladimir Markobb089b62018-06-28 17:30:16 +0100327 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100328 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000329 }
Calin Juravleffc87072016-04-20 14:22:09 +0100330 } else if (runtime->UseJitCompilation()) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000331 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000332 string = class_linker->LookupString(string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000333 if (string != nullptr) {
334 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100335 desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000336 } else {
337 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
338 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000339 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100340 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100341 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000342 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100343 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000344 string = class_linker->LookupString(string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100345 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100346 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000347 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000348 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000349 }
350 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000351 if (string != nullptr) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000352 load_string->SetString(handles->NewHandle(string));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000353 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000354 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000355 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000356
Vladimir Marko28e012a2017-12-07 11:22:59 +0000357 HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000358 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000359}
360
Vladimir Markodc151b22015-10-15 18:02:30 +0100361} // namespace art