blob: 15254edcab6d9a982d1129c04bdb363f8c3142d3 [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
57void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
58 if (invoke->IsStringInit()) {
59 // Not using the dex cache arrays. But we could still try to use a better dispatch...
60 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
61 return;
62 }
63
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010064 HGraph* outer_graph = codegen_->GetGraph();
65 ArtMethod* compiling_method = graph_->GetArtMethod();
Vladimir Markodc151b22015-10-15 18:02:30 +010066
67 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
68 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
69 uint64_t method_load_data = 0u;
70 uint64_t direct_code_ptr = 0u;
71
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010072 if (invoke->GetResolvedMethod() == outer_graph->GetArtMethod()) {
73 DCHECK(outer_graph->GetArtMethod() != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010074 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
75 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
76 } else {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010077 uintptr_t direct_code, direct_method;
78 {
79 ScopedObjectAccess soa(Thread::Current());
80 compiler_driver_->GetCodeAndMethodForDirectCall(
81 (compiling_method == nullptr) ? nullptr : compiling_method->GetDeclaringClass(),
82 invoke->GetResolvedMethod(),
83 &direct_code,
84 &direct_method);
85 }
Vladimir Markodc151b22015-10-15 18:02:30 +010086 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000087 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
88 // kDirectAddress would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +010089 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +010090 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
91 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
92 method_load_data = direct_method;
93 } else { // The direct pointer will be known at link time.
94 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
95 }
96 } else { // Use dex cache.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010097 if (!Runtime::Current()->UseJitCompilation()) {
98 // Use PC-relative access to the dex cache arrays.
Vladimir Markodc151b22015-10-15 18:02:30 +010099 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000100 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
101 &graph_->GetDexFile());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100102 method_load_data = layout.MethodOffset(invoke->GetDexMethodIndex());
Vladimir Markodc151b22015-10-15 18:02:30 +0100103 } else { // We must go through the ArtMethod's pointer to resolved methods.
104 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
105 }
106 }
107 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000108 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
109 // while kCallDirect would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100110 DCHECK(!Runtime::Current()->UseJitCompilation());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100111 const DexFile* dex_file_of_callee = invoke->GetTargetMethod().dex_file;
Vladimir Markodc151b22015-10-15 18:02:30 +0100112 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
113 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
114 direct_code_ptr = direct_code;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100115 } else if (ContainsElement(compiler_driver_->GetDexFilesForOatFile(), dex_file_of_callee)) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100116 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100117 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
118 } else { // The direct pointer will be known at link time.
119 // NOTE: This is used for app->boot calls when compiling an app against
120 // a relocatable but not yet relocated image.
121 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
122 }
123 } else { // We must use the code pointer from the ArtMethod.
124 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
125 }
126 }
127
128 if (graph_->IsDebuggable()) {
129 // For debuggable apps always use the code pointer from ArtMethod
130 // so that we don't circumvent instrumentation stubs if installed.
131 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
132 }
133
134 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
135 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
136 };
137 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100138 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100139 invoke->SetDispatchInfo(dispatch_info);
140}
141
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100142void HSharpening::ProcessLoadClass(HLoadClass* load_class) {
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700143 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
144 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
145 << load_class->GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100146 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700147 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100148
149 const DexFile& dex_file = load_class->GetDexFile();
150 uint32_t type_index = load_class->GetTypeIndex();
151
152 bool is_in_dex_cache = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700153 bool is_in_boot_image = false;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100154 HLoadClass::LoadKind desired_load_kind;
155 uint64_t address = 0u; // Class or dex cache element address.
156 {
157 ScopedObjectAccess soa(Thread::Current());
158 StackHandleScope<1> hs(soa.Self());
159 Runtime* runtime = Runtime::Current();
160 ClassLinker* class_linker = runtime->GetClassLinker();
161 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
162 ? compilation_unit_.GetDexCache()
163 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
164 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000165 if (codegen_->GetCompilerOptions().IsBootImage()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100166 // Compiling boot image. Check if the class is a boot image class.
167 DCHECK(!runtime->UseJitCompilation());
168 if (!compiler_driver_->GetSupportBootImageFixup()) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700169 // MIPS64 or compiler_driver_test. Do not sharpen.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100170 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700171 } else if ((klass != nullptr) && compiler_driver_->IsImageClass(
172 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
173 is_in_boot_image = true;
174 is_in_dex_cache = true;
175 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
176 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
177 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100178 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700179 // Not a boot image class. We must go through the dex cache.
180 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
181 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100182 }
183 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700184 is_in_boot_image = (klass != nullptr) && runtime->GetHeap()->ObjectIsInBootImageSpace(klass);
185 if (runtime->UseJitCompilation()) {
186 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
187 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
188 is_in_dex_cache = (klass != nullptr);
189 if (is_in_boot_image) {
190 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
191 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
192 address = reinterpret_cast64<uint64_t>(klass);
193 } else {
194 // Note: If the class is not in the dex cache or isn't initialized, the
195 // instruction needs environment and will not be inlined across dex files.
196 // Within a dex file, the slow-path helper loads the correct class and
197 // inlined frames are used correctly for OOM stack trace.
198 // TODO: Write a test for this. Bug: 29416588
199 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
200 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
201 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
202 }
203 // AOT app compilation. Check if the class is in the boot image.
204 } else if (is_in_boot_image && !codegen_->GetCompilerOptions().GetCompilePic()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100205 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
206 address = reinterpret_cast64<uint64_t>(klass);
207 } else {
208 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
209 // Use PC-relative load from the dex cache if the dex file belongs
210 // to the oat file that we're currently compiling.
211 desired_load_kind =
212 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_class->GetDexFile())
213 ? HLoadClass::LoadKind::kDexCachePcRelative
214 : HLoadClass::LoadKind::kDexCacheViaMethod;
215 }
216 }
217 }
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700218
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700219 if (is_in_boot_image) {
220 load_class->MarkInBootImage();
221 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100222
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700223 if (load_class->NeedsAccessCheck()) {
224 // We need to call the runtime anyway, so we simply get the class as that call's return value.
225 return;
226 }
227
228 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
229 // Loading from the ArtMethod* is the most efficient retrieval in code size.
230 // TODO: This may not actually be true for all architectures and
231 // locations of target classes. The additional register pressure
232 // for using the ArtMethod* should be considered.
233 return;
234 }
235
236 if (is_in_dex_cache) {
237 load_class->MarkInDexCache();
238 }
239
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100240 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
241 switch (load_kind) {
242 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
243 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
244 case HLoadClass::LoadKind::kDexCacheViaMethod:
245 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
246 break;
247 case HLoadClass::LoadKind::kBootImageAddress:
248 case HLoadClass::LoadKind::kDexCacheAddress:
249 DCHECK_NE(address, 0u);
250 load_class->SetLoadKindWithAddress(load_kind, address);
251 break;
252 case HLoadClass::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700253 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100254 DexCacheArraysLayout layout(pointer_size, &dex_file);
255 size_t element_index = layout.TypeOffset(type_index);
256 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
257 break;
258 }
259 default:
260 LOG(FATAL) << "Unexpected load kind: " << load_kind;
261 UNREACHABLE();
262 }
263}
264
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000265void HSharpening::ProcessLoadString(HLoadString* load_string) {
266 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
267 DCHECK(!load_string->IsInDexCache());
268
269 const DexFile& dex_file = load_string->GetDexFile();
270 uint32_t string_index = load_string->GetStringIndex();
271
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700272 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000273 uint64_t address = 0u; // String or dex cache element address.
274 {
275 Runtime* runtime = Runtime::Current();
276 ClassLinker* class_linker = runtime->GetClassLinker();
277 ScopedObjectAccess soa(Thread::Current());
278 StackHandleScope<1> hs(soa.Self());
279 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
280 ? compilation_unit_.GetDexCache()
281 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
282
Vladimir Markoaad75c62016-10-03 08:46:48 +0000283 if (codegen_->GetCompilerOptions().IsBootImage()) {
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +0100284 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
285 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100286 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000287 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
288 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000289 if (compiler_driver_->GetSupportBootImageFixup()) {
290 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
291 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
292 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
293 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
294 } else {
295 // MIPS64 or compiler_driver_test. Do not sharpen.
296 DCHECK_EQ(desired_load_kind, HLoadString::LoadKind::kDexCacheViaMethod);
297 }
Calin Juravleffc87072016-04-20 14:22:09 +0100298 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100299 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700300 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +0100301 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
302 if (string != nullptr) {
303 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
304 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
305 address = reinterpret_cast64<uint64_t>(string);
306 } else {
307 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
308 }
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100309 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000310 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100311 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000312 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100313 if (string != nullptr &&
314 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
315 !codegen_->GetCompilerOptions().GetCompilePic()) {
316 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
317 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000318 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000319 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000320 }
321 }
322 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000323
324 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
325 switch (load_kind) {
326 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
327 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +0000328 case HLoadString::LoadKind::kBssEntry:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000329 case HLoadString::LoadKind::kDexCacheViaMethod:
Nicolas Geoffrayac3ebc32016-10-05 13:13:50 +0100330 case HLoadString::LoadKind::kJitTableAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000331 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
332 break;
333 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000334 DCHECK_NE(address, 0u);
335 load_string->SetLoadKindWithAddress(load_kind, address);
336 break;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000337 }
338}
339
Vladimir Markodc151b22015-10-15 18:02:30 +0100340} // namespace art