blob: 5c2f57e3140a72e2e5522700a2c07418f7e54bed [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
Aart Bik24773202018-04-26 10:28:51 -070038bool HSharpening::Run() {
Vladimir Markodc151b22015-10-15 18:02:30 +010039 // We don't care about the order of the blocks here.
40 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
41 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
42 HInstruction* instruction = it.Current();
43 if (instruction->IsInvokeStaticOrDirect()) {
Vladimir Markobb089b62018-06-28 17:30:16 +010044 SharpenInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect(), codegen_);
Vladimir Markodc151b22015-10-15 18:02:30 +010045 }
46 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
47 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
48 // because we know the type better when inlining.
Vladimir Markodc151b22015-10-15 18:02:30 +010049 }
50 }
Aart Bik24773202018-04-26 10:28:51 -070051 return true;
Vladimir Markodc151b22015-10-15 18:02:30 +010052}
53
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000054static bool IsInBootImage(ArtMethod* method) {
55 const std::vector<gc::space::ImageSpace*>& image_spaces =
56 Runtime::Current()->GetHeap()->GetBootImageSpaces();
57 for (gc::space::ImageSpace* image_space : image_spaces) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +010058 const ImageSection& method_section = image_space->GetImageHeader().GetMethodsSection();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000059 if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
60 return true;
61 }
62 }
63 return false;
64}
65
Vladimir Markobb089b62018-06-28 17:30:16 +010066static bool BootImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
Vladimir Markodc4bcce2018-06-21 16:15:42 +010067 DCHECK(compiler_options.IsBootImage());
Vladimir Marko65979462017-05-19 17:25:12 +010068 ScopedObjectAccess soa(Thread::Current());
69 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
70 DCHECK(klass != nullptr);
71 const DexFile& dex_file = klass->GetDexFile();
Vladimir Markodc4bcce2018-06-21 16:15:42 +010072 return compiler_options.IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
Vladimir Marko65979462017-05-19 17:25:12 +010073}
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000074
75void HSharpening::SharpenInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke,
Vladimir Markobb089b62018-06-28 17:30:16 +010076 CodeGenerator* codegen) {
Vladimir Markodc151b22015-10-15 18:02:30 +010077 if (invoke->IsStringInit()) {
78 // Not using the dex cache arrays. But we could still try to use a better dispatch...
79 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
80 return;
81 }
82
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000083 ArtMethod* callee = invoke->GetResolvedMethod();
84 DCHECK(callee != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010085
86 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
87 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
88 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010089
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000090 // Note: we never call an ArtMethod through a known code pointer, as
91 // we do not want to keep on invoking it if it gets deoptimized. This
92 // applies to both AOT and JIT.
93 // This also avoids having to find out if the code pointer of an ArtMethod
94 // is the resolution trampoline (for ensuring the class is initialized), or
95 // the interpreter entrypoint. Such code pointers we do not want to call
96 // directly.
97 // Only in the case of a recursive call can we call directly, as we know the
98 // class is initialized already or being initialized, and the call will not
99 // be invoked once the method is deoptimized.
100
Alex Light1ebe4fe2017-01-30 14:57:11 -0800101 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
102 // situations.
Vladimir Markobb089b62018-06-28 17:30:16 +0100103 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000104 if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000105 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +0100106 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
107 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Vladimir Markobb089b62018-06-28 17:30:16 +0100108 } else if (compiler_options.IsBootImage()) {
109 if (!compiler_options.GetCompilePic()) {
110 // Test configuration, do not sharpen.
111 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
112 } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
113 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative;
114 } else {
115 // Use PC-relative access to the .bss methods array.
116 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
117 }
118 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100119 } else if (Runtime::Current()->UseJitCompilation()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000120 // JIT or on-device AOT compilation referencing a boot image method.
121 // Use the method address directly.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100122 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000123 method_load_data = reinterpret_cast<uintptr_t>(callee);
124 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markob066d432018-01-03 13:14:37 +0000125 } else if (IsInBootImage(callee)) {
126 // Use PC-relative access to the .data.bimg.rel.ro methods array.
127 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
Vladimir Markob066d432018-01-03 13:14:37 +0000128 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100129 } else {
Vladimir Markob066d432018-01-03 13:14:37 +0000130 // Use PC-relative access to the .bss methods array.
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100131 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000132 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100133 }
134
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000135 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100136 // For debuggable apps always use the code pointer from ArtMethod
137 // so that we don't circumvent instrumentation stubs if installed.
138 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
139 }
140
141 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000142 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100143 };
144 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000145 codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100146 invoke->SetDispatchInfo(dispatch_info);
147}
148
Vladimir Marko28e012a2017-12-07 11:22:59 +0000149HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
150 HLoadClass* load_class,
151 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000152 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000153 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100154 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700155 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
156 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700157 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100158
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000159 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
160
Vladimir Marko41559982017-01-06 14:04:23 +0000161 if (load_class->NeedsAccessCheck()) {
162 // 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 +0000163 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000164 // Loading from the ArtMethod* is the most efficient retrieval in code size.
165 // TODO: This may not actually be true for all architectures and
166 // locations of target classes. The additional register pressure
167 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000168 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000169 const DexFile& dex_file = load_class->GetDexFile();
170 dex::TypeIndex type_index = load_class->GetTypeIndex();
171
172 bool is_in_boot_image = false;
173 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
174 Runtime* runtime = Runtime::Current();
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100175 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
176 if (compiler_options.IsBootImage()) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000177 // Compiling boot image. Check if the class is a boot image class.
178 DCHECK(!runtime->UseJitCompilation());
Vladimir Markobb089b62018-06-28 17:30:16 +0100179 if (!compiler_options.GetCompilePic()) {
180 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100181 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko65979462017-05-19 17:25:12 +0100182 } else if ((klass != nullptr) &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100183 compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000184 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100185 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000186 } else {
187 // Not a boot image class.
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100188 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000189 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100190 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000191 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800192 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000193 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
194 if (runtime->UseJitCompilation()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100195 DCHECK(!compiler_options.GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000196 if (is_in_boot_image) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100197 desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800198 } else if (klass != nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000199 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
200 } else {
201 // Class not loaded yet. This happens when the dex code requesting
202 // this `HLoadClass` hasn't been executed in the interpreter.
203 // Fallback to the dex cache.
204 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100205 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000206 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100207 } else if (is_in_boot_image) {
208 // AOT app compilation, boot image class.
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100209 desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000210 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100211 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000212 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
213 }
214 }
215 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
216
217 if (is_in_boot_image) {
218 load_class->MarkInBootImage();
219 }
220 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
221 }
222
223 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100224 if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000225 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
226 // We actually cannot reference this class, we're forced to bail.
227 // We cannot reference this class with Bss, as the entrypoint will lookup the class
228 // in the caller's dex file, but that dex file does not reference the class.
229 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100230 }
231 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000232 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100233}
234
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100235static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
Vladimir Marko175e7862018-03-27 09:03:13 +0000236 REQUIRES_SHARED(Locks::mutator_lock_) {
237 DCHECK(!klass->IsProxyClass());
238 DCHECK(!klass->IsArrayClass());
239
240 if (Runtime::Current()->UseJitCompilation()) {
241 // If we're JITting, try to assign a type check bitstring (fall through).
242 } else if (codegen->GetCompilerOptions().IsBootImage()) {
243 const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100244 if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000245 return false;
246 }
247 // If the target is a boot image class, try to assign a type check bitstring (fall through).
248 // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
249 } else {
250 // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
251 // already assigned in the boot image.
252 return false;
253 }
254
255 // Try to assign a type check bitstring.
256 MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
Vladimir Markobb089b62018-06-28 17:30:16 +0100257 if ((false) && // FIXME: Inliner does not respect CompilerDriver::IsClassToCompile()
Vladimir Marko175e7862018-03-27 09:03:13 +0000258 // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
259 kIsDebugBuild &&
260 codegen->GetCompilerOptions().IsBootImage() &&
261 codegen->GetCompilerOptions().IsForceDeterminism()) {
262 SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
263 CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
264 << klass->PrettyDescriptor() << "/" << old_state
265 << " in " << codegen->GetGraph()->PrettyMethod();
266 }
267 SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
268 return state == SubtypeCheckInfo::kAssigned;
269}
270
271TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
272 CodeGenerator* codegen,
Vladimir Marko175e7862018-03-27 09:03:13 +0000273 bool needs_access_check) {
274 if (klass == nullptr) {
275 return TypeCheckKind::kUnresolvedCheck;
276 } else if (klass->IsInterface()) {
277 return TypeCheckKind::kInterfaceCheck;
278 } else if (klass->IsArrayClass()) {
279 if (klass->GetComponentType()->IsObjectClass()) {
280 return TypeCheckKind::kArrayObjectCheck;
281 } else if (klass->CannotBeAssignedFromOtherTypes()) {
282 return TypeCheckKind::kExactCheck;
283 } else {
284 return TypeCheckKind::kArrayCheck;
285 }
286 } else if (klass->IsFinal()) { // TODO: Consider using bitstring for final classes.
287 return TypeCheckKind::kExactCheck;
288 } else if (kBitstringSubtypeCheckEnabled &&
289 !needs_access_check &&
Vladimir Markodc4bcce2018-06-21 16:15:42 +0100290 CanUseTypeCheckBitstring(klass, codegen)) {
Vladimir Marko175e7862018-03-27 09:03:13 +0000291 // TODO: We should not need the `!needs_access_check` check but getting rid of that
292 // requires rewriting some optimizations in instruction simplifier.
293 return TypeCheckKind::kBitstringCheck;
294 } else if (klass->IsAbstract()) {
295 return TypeCheckKind::kAbstractClassCheck;
296 } else {
297 return TypeCheckKind::kClassHierarchyCheck;
298 }
299}
300
Vladimir Marko28e012a2017-12-07 11:22:59 +0000301void HSharpening::ProcessLoadString(
302 HLoadString* load_string,
303 CodeGenerator* codegen,
Vladimir Marko28e012a2017-12-07 11:22:59 +0000304 const DexCompilationUnit& dex_compilation_unit,
305 VariableSizedHandleScope* handles) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100306 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000307
308 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800309 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000310
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000311 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000312 {
313 Runtime* runtime = Runtime::Current();
314 ClassLinker* class_linker = runtime->GetClassLinker();
315 ScopedObjectAccess soa(Thread::Current());
316 StackHandleScope<1> hs(soa.Self());
Vladimir Marko28e012a2017-12-07 11:22:59 +0000317 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
318 ? dex_compilation_unit.GetDexCache()
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000319 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Vladimir Marko28e012a2017-12-07 11:22:59 +0000320 ObjPtr<mirror::String> string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000321
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100322 const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
323 if (compiler_options.IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000324 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
325 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100326 DCHECK(!runtime->UseJitCompilation());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000327 string = class_linker->ResolveString(string_index, dex_cache);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000328 CHECK(string != nullptr);
Vladimir Markobb089b62018-06-28 17:30:16 +0100329 if (compiler_options.GetCompilePic()) {
Vladimir Marko213ee2d2018-06-22 11:56:34 +0100330 DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
Vladimir Marko764d4542017-05-16 10:31:41 +0100331 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
Vladimir Marko95026872016-09-09 09:16:31 +0000332 } else {
Vladimir Markobb089b62018-06-28 17:30:16 +0100333 // Test configuration, do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100334 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000335 }
Calin Juravleffc87072016-04-20 14:22:09 +0100336 } else if (runtime->UseJitCompilation()) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000337 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000338 string = class_linker->LookupString(string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000339 if (string != nullptr) {
340 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100341 desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000342 } else {
343 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
344 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000345 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100346 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100347 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000348 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100349 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markoa64b52d2017-12-08 16:27:49 +0000350 string = class_linker->LookupString(string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100351 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +0100352 desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000353 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000354 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000355 }
356 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000357 if (string != nullptr) {
Vladimir Marko28e012a2017-12-07 11:22:59 +0000358 load_string->SetString(handles->NewHandle(string));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000359 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000360 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000361 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000362
Vladimir Marko28e012a2017-12-07 11:22:59 +0000363 HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000364 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000365}
366
Vladimir Markodc151b22015-10-15 18:02:30 +0100367} // namespace art