blob: 9fdeccfa1a01016a2bf2c3f5f46a0159af7d3b9a [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 Markodb8e62d2016-03-30 16:30:21 +010019#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070020#include "base/enums.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000021#include "class_linker.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010022#include "code_generator.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "driver/compiler_options.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000024#include "driver/dex_compilation_unit.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010025#include "utils/dex_cache_arrays_layout-inl.h"
26#include "driver/compiler_driver.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"
30#include "mirror/dex_cache.h"
31#include "mirror/string.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010032#include "nodes.h"
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000033#include "runtime.h"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010035
36namespace art {
37
38void HSharpening::Run() {
39 // 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()) {
44 ProcessInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010045 } else if (instruction->IsLoadClass()) {
46 ProcessLoadClass(instruction->AsLoadClass());
Vladimir Markocac5a7e2016-02-22 10:39:50 +000047 } else if (instruction->IsLoadString()) {
48 ProcessLoadString(instruction->AsLoadString());
Vladimir Markodc151b22015-10-15 18:02:30 +010049 }
50 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
51 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
52 // because we know the type better when inlining.
Vladimir Markodc151b22015-10-15 18:02:30 +010053 }
54 }
55}
56
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000057static bool IsInBootImage(ArtMethod* method) {
58 const std::vector<gc::space::ImageSpace*>& image_spaces =
59 Runtime::Current()->GetHeap()->GetBootImageSpaces();
60 for (gc::space::ImageSpace* image_space : image_spaces) {
61 const auto& method_section = image_space->GetImageHeader().GetMethodsSection();
62 if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
63 return true;
64 }
65 }
66 return false;
67}
68
69static bool AOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& options) {
70 // Including patch information means the AOT code will be patched, which we don't
71 // support in the compiler, and is anyways moving away b/33192586.
72 return IsInBootImage(method) && !options.GetCompilePic() && !options.GetIncludePatchInformation();
73}
74
Vladimir Markodc151b22015-10-15 18:02:30 +010075void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
76 if (invoke->IsStringInit()) {
77 // Not using the dex cache arrays. But we could still try to use a better dispatch...
78 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
79 return;
80 }
81
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000082 ArtMethod* callee = invoke->GetResolvedMethod();
83 DCHECK(callee != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010084
85 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
86 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
87 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010088
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000089 // Note: we never call an ArtMethod through a known code pointer, as
90 // we do not want to keep on invoking it if it gets deoptimized. This
91 // applies to both AOT and JIT.
92 // This also avoids having to find out if the code pointer of an ArtMethod
93 // is the resolution trampoline (for ensuring the class is initialized), or
94 // the interpreter entrypoint. Such code pointers we do not want to call
95 // directly.
96 // Only in the case of a recursive call can we call directly, as we know the
97 // class is initialized already or being initialized, and the call will not
98 // be invoked once the method is deoptimized.
99
100 if (callee == codegen_->GetGraph()->GetArtMethod()) {
101 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +0100102 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
103 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000104 } else if (Runtime::Current()->UseJitCompilation() ||
105 AOTCanEmbedMethod(callee, codegen_->GetCompilerOptions())) {
106 // JIT or on-device AOT compilation referencing a boot image method.
107 // Use the method address directly.
108 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
109 method_load_data = reinterpret_cast<uintptr_t>(callee);
110 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100111 } else {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000112 // Use PC-relative access to the dex cache arrays.
113 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
114 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
115 &graph_->GetDexFile());
116 method_load_data = layout.MethodOffset(invoke->GetDexMethodIndex());
117 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100118 }
119
120 if (graph_->IsDebuggable()) {
121 // For debuggable apps always use the code pointer from ArtMethod
122 // so that we don't circumvent instrumentation stubs if installed.
123 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
124 }
125
126 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000127 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100128 };
129 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100130 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100131 invoke->SetDispatchInfo(dispatch_info);
132}
133
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100134void HSharpening::ProcessLoadClass(HLoadClass* load_class) {
Nicolas Geoffray56876342016-12-16 16:09:08 +0000135 ScopedObjectAccess soa(Thread::Current());
136 StackHandleScope<1> hs(soa.Self());
137 Runtime* runtime = Runtime::Current();
138 ClassLinker* class_linker = runtime->GetClassLinker();
139 const DexFile& dex_file = load_class->GetDexFile();
140 dex::TypeIndex type_index = load_class->GetTypeIndex();
141 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
142 ? compilation_unit_.GetDexCache()
143 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
144 mirror::Class* cls = dex_cache->GetResolvedType(type_index);
145 SharpenClass(load_class, cls, handles_, codegen_, compiler_driver_);
146}
147
148void HSharpening::SharpenClass(HLoadClass* load_class,
149 mirror::Class* klass,
150 VariableSizedHandleScope* handles,
151 CodeGenerator* codegen,
152 CompilerDriver* compiler_driver) {
153 ScopedAssertNoThreadSuspension sants("Sharpening class in compiler");
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700154 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
155 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
156 << load_class->GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100157 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700158 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100159
160 const DexFile& dex_file = load_class->GetDexFile();
Andreas Gampea5b09a62016-11-17 15:21:22 -0800161 dex::TypeIndex type_index = load_class->GetTypeIndex();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100162
163 bool is_in_dex_cache = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700164 bool is_in_boot_image = false;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000165 HLoadClass::LoadKind desired_load_kind = static_cast<HLoadClass::LoadKind>(-1);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100166 uint64_t address = 0u; // Class or dex cache element address.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000167 Runtime* runtime = Runtime::Current();
168 if (codegen->GetCompilerOptions().IsBootImage()) {
169 // Compiling boot image. Check if the class is a boot image class.
170 DCHECK(!runtime->UseJitCompilation());
171 if (!compiler_driver->GetSupportBootImageFixup()) {
172 // MIPS64 or compiler_driver_test. Do not sharpen.
173 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
174 } else if ((klass != nullptr) && compiler_driver->IsImageClass(
175 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
176 is_in_boot_image = true;
177 is_in_dex_cache = true;
178 desired_load_kind = codegen->GetCompilerOptions().GetCompilePic()
179 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
180 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100181 } else {
Nicolas Geoffray56876342016-12-16 16:09:08 +0000182 // Not a boot image class. We must go through the dex cache.
183 DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
184 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
185 }
186 } else {
187 is_in_boot_image = (klass != nullptr) && runtime->GetHeap()->ObjectIsInBootImageSpace(klass);
188 if (runtime->UseJitCompilation()) {
189 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
190 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
191 is_in_dex_cache = (klass != nullptr);
192 if (is_in_boot_image) {
193 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100194 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
195 address = reinterpret_cast64<uint64_t>(klass);
Nicolas Geoffray56876342016-12-16 16:09:08 +0000196 } else if (is_in_dex_cache) {
197 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
198 // We store in the address field the location of the stack reference maintained
199 // by the handle. We do this now so that the code generation does not need to figure
200 // out which class loader to use.
201 address = reinterpret_cast<uint64_t>(handles->NewHandle(klass).GetReference());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100202 } else {
Nicolas Geoffray56876342016-12-16 16:09:08 +0000203 // Class not loaded yet. This happens when the dex code requesting
204 // this `HLoadClass` hasn't been executed in the interpreter.
205 // Fallback to the dex cache.
206 // TODO(ngeoffray): Generate HDeoptimize instead.
207 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100208 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000209 } else if (is_in_boot_image && !codegen->GetCompilerOptions().GetCompilePic()) {
210 // AOT app compilation. Check if the class is in the boot image.
211 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
212 address = reinterpret_cast64<uint64_t>(klass);
213 } else {
214 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
215 // Use PC-relative load from the dex cache if the dex file belongs
216 // to the oat file that we're currently compiling.
217 desired_load_kind =
218 ContainsElement(compiler_driver->GetDexFilesForOatFile(), &load_class->GetDexFile())
219 ? HLoadClass::LoadKind::kDexCachePcRelative
220 : HLoadClass::LoadKind::kDexCacheViaMethod;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100221 }
222 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000223 DCHECK_NE(desired_load_kind, static_cast<HLoadClass::LoadKind>(-1));
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700224
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700225 if (is_in_boot_image) {
226 load_class->MarkInBootImage();
227 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100228
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700229 if (load_class->NeedsAccessCheck()) {
230 // We need to call the runtime anyway, so we simply get the class as that call's return value.
231 return;
232 }
233
234 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
235 // Loading from the ArtMethod* is the most efficient retrieval in code size.
236 // TODO: This may not actually be true for all architectures and
237 // locations of target classes. The additional register pressure
238 // for using the ArtMethod* should be considered.
239 return;
240 }
241
242 if (is_in_dex_cache) {
243 load_class->MarkInDexCache();
244 }
245
Nicolas Geoffray56876342016-12-16 16:09:08 +0000246 HLoadClass::LoadKind load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100247 switch (load_kind) {
248 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
249 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
250 case HLoadClass::LoadKind::kDexCacheViaMethod:
251 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
252 break;
253 case HLoadClass::LoadKind::kBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +0000254 case HLoadClass::LoadKind::kJitTableAddress:
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100255 DCHECK_NE(address, 0u);
256 load_class->SetLoadKindWithAddress(load_kind, address);
257 break;
258 case HLoadClass::LoadKind::kDexCachePcRelative: {
Nicolas Geoffray56876342016-12-16 16:09:08 +0000259 PointerSize pointer_size = InstructionSetPointerSize(codegen->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100260 DexCacheArraysLayout layout(pointer_size, &dex_file);
261 size_t element_index = layout.TypeOffset(type_index);
262 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
263 break;
264 }
265 default:
266 LOG(FATAL) << "Unexpected load kind: " << load_kind;
267 UNREACHABLE();
268 }
269}
270
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000271void HSharpening::ProcessLoadString(HLoadString* load_string) {
272 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
273 DCHECK(!load_string->IsInDexCache());
274
275 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800276 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000277
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700278 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000279 uint64_t address = 0u; // String or dex cache element address.
280 {
281 Runtime* runtime = Runtime::Current();
282 ClassLinker* class_linker = runtime->GetClassLinker();
283 ScopedObjectAccess soa(Thread::Current());
284 StackHandleScope<1> hs(soa.Self());
285 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
286 ? compilation_unit_.GetDexCache()
287 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
288
Vladimir Markoaad75c62016-10-03 08:46:48 +0000289 if (codegen_->GetCompilerOptions().IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000290 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
291 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100292 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000293 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
294 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000295 if (compiler_driver_->GetSupportBootImageFixup()) {
296 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
297 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
298 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
299 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
300 } else {
301 // MIPS64 or compiler_driver_test. Do not sharpen.
302 DCHECK_EQ(desired_load_kind, HLoadString::LoadKind::kDexCacheViaMethod);
303 }
Calin Juravleffc87072016-04-20 14:22:09 +0100304 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100305 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700306 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000307 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
308 if (string != nullptr) {
309 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
310 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
311 address = reinterpret_cast64<uint64_t>(string);
312 } else {
313 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
314 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100315 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000316 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100317 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000318 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100319 if (string != nullptr &&
320 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
321 !codegen_->GetCompilerOptions().GetCompilePic()) {
322 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
323 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000324 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000325 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000326 }
327 }
328 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000329
330 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
331 switch (load_kind) {
332 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
333 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +0000334 case HLoadString::LoadKind::kBssEntry:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000335 case HLoadString::LoadKind::kDexCacheViaMethod:
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000336 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000337 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
338 break;
339 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000340 DCHECK_NE(address, 0u);
341 load_string->SetLoadKindWithAddress(load_kind, address);
342 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000343 }
344}
345
Vladimir Markodc151b22015-10-15 18:02:30 +0100346} // namespace art