blob: e64c0054106749603d917cfef1cf1373d33bda1a [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"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000034#include "scoped_thread_state_change.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
64 // TODO: Avoid CompilerDriver.
Nicolas Geoffraye5234232015-12-02 09:06:11 +000065 InvokeType original_invoke_type = invoke->GetOriginalInvokeType();
66 InvokeType optimized_invoke_type = original_invoke_type;
Vladimir Markodc151b22015-10-15 18:02:30 +010067 MethodReference target_method(&graph_->GetDexFile(), invoke->GetDexMethodIndex());
68 int vtable_idx;
69 uintptr_t direct_code, direct_method;
70 bool success = compiler_driver_->ComputeInvokeInfo(
71 &compilation_unit_,
72 invoke->GetDexPc(),
73 false /* update_stats: already updated in builder */,
74 true /* enable_devirtualization */,
Nicolas Geoffraye5234232015-12-02 09:06:11 +000075 &optimized_invoke_type,
Vladimir Markodc151b22015-10-15 18:02:30 +010076 &target_method,
77 &vtable_idx,
78 &direct_code,
79 &direct_method);
Nicolas Geoffraye5234232015-12-02 09:06:11 +000080 if (!success) {
81 // TODO: try using kDexCachePcRelative. It's always a valid method load
82 // kind as long as it's supported by the codegen
83 return;
84 }
85 invoke->SetOptimizedInvokeType(optimized_invoke_type);
86 invoke->SetTargetMethod(target_method);
Vladimir Markodc151b22015-10-15 18:02:30 +010087
88 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
89 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
90 uint64_t method_load_data = 0u;
91 uint64_t direct_code_ptr = 0u;
92
93 HGraph* outer_graph = codegen_->GetGraph();
94 if (target_method.dex_file == &outer_graph->GetDexFile() &&
95 target_method.dex_method_index == outer_graph->GetMethodIdx()) {
96 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
97 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
98 } else {
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000099 bool use_pc_relative_instructions =
100 ((direct_method == 0u || direct_code == static_cast<uintptr_t>(-1))) &&
101 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), target_method.dex_file);
Vladimir Markodc151b22015-10-15 18:02:30 +0100102 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000103 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
104 // kDirectAddress would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100105 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100106 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
107 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
108 method_load_data = direct_method;
109 } else { // The direct pointer will be known at link time.
110 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
111 }
112 } else { // Use dex cache.
113 DCHECK_EQ(target_method.dex_file, &graph_->GetDexFile());
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000114 if (use_pc_relative_instructions) { // Can we use PC-relative access to the dex cache arrays?
Calin Juravleffc87072016-04-20 14:22:09 +0100115 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100116 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000117 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
118 &graph_->GetDexFile());
Vladimir Markodc151b22015-10-15 18:02:30 +0100119 method_load_data = layout.MethodOffset(target_method.dex_method_index);
120 } else { // We must go through the ArtMethod's pointer to resolved methods.
121 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
122 }
123 }
124 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000125 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
126 // while kCallDirect would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100127 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100128 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
129 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
130 direct_code_ptr = direct_code;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000131 } else if (use_pc_relative_instructions) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100132 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100133 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
134 } else { // The direct pointer will be known at link time.
135 // NOTE: This is used for app->boot calls when compiling an app against
136 // a relocatable but not yet relocated image.
137 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
138 }
139 } else { // We must use the code pointer from the ArtMethod.
140 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
141 }
142 }
143
144 if (graph_->IsDebuggable()) {
145 // For debuggable apps always use the code pointer from ArtMethod
146 // so that we don't circumvent instrumentation stubs if installed.
147 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
148 }
149
150 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
151 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
152 };
153 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
154 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info,
155 invoke->GetTargetMethod());
156 invoke->SetDispatchInfo(dispatch_info);
157}
158
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100159void HSharpening::ProcessLoadClass(HLoadClass* load_class) {
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700160 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
161 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
162 << load_class->GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100163 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700164 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100165
166 const DexFile& dex_file = load_class->GetDexFile();
167 uint32_t type_index = load_class->GetTypeIndex();
168
169 bool is_in_dex_cache = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700170 bool is_in_boot_image = false;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100171 HLoadClass::LoadKind desired_load_kind;
172 uint64_t address = 0u; // Class or dex cache element address.
173 {
174 ScopedObjectAccess soa(Thread::Current());
175 StackHandleScope<1> hs(soa.Self());
176 Runtime* runtime = Runtime::Current();
177 ClassLinker* class_linker = runtime->GetClassLinker();
178 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
179 ? compilation_unit_.GetDexCache()
180 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
181 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
182
183 if (compiler_driver_->IsBootImage()) {
184 // Compiling boot image. Check if the class is a boot image class.
185 DCHECK(!runtime->UseJitCompilation());
186 if (!compiler_driver_->GetSupportBootImageFixup()) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700187 // MIPS64 or compiler_driver_test. Do not sharpen.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100188 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700189 } else if ((klass != nullptr) && compiler_driver_->IsImageClass(
190 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
191 is_in_boot_image = true;
192 is_in_dex_cache = true;
193 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
194 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
195 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100196 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700197 // Not a boot image class. We must go through the dex cache.
198 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
199 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100200 }
201 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700202 is_in_boot_image = (klass != nullptr) && runtime->GetHeap()->ObjectIsInBootImageSpace(klass);
203 if (runtime->UseJitCompilation()) {
204 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
205 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
206 is_in_dex_cache = (klass != nullptr);
207 if (is_in_boot_image) {
208 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
209 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
210 address = reinterpret_cast64<uint64_t>(klass);
211 } else {
212 // Note: If the class is not in the dex cache or isn't initialized, the
213 // instruction needs environment and will not be inlined across dex files.
214 // Within a dex file, the slow-path helper loads the correct class and
215 // inlined frames are used correctly for OOM stack trace.
216 // TODO: Write a test for this. Bug: 29416588
217 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
218 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
219 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
220 }
221 // AOT app compilation. Check if the class is in the boot image.
222 } else if (is_in_boot_image && !codegen_->GetCompilerOptions().GetCompilePic()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100223 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
224 address = reinterpret_cast64<uint64_t>(klass);
225 } else {
226 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
227 // Use PC-relative load from the dex cache if the dex file belongs
228 // to the oat file that we're currently compiling.
229 desired_load_kind =
230 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_class->GetDexFile())
231 ? HLoadClass::LoadKind::kDexCachePcRelative
232 : HLoadClass::LoadKind::kDexCacheViaMethod;
233 }
234 }
235 }
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700236
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700237 if (is_in_boot_image) {
238 load_class->MarkInBootImage();
239 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100240
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700241 if (load_class->NeedsAccessCheck()) {
242 // We need to call the runtime anyway, so we simply get the class as that call's return value.
243 return;
244 }
245
246 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
247 // Loading from the ArtMethod* is the most efficient retrieval in code size.
248 // TODO: This may not actually be true for all architectures and
249 // locations of target classes. The additional register pressure
250 // for using the ArtMethod* should be considered.
251 return;
252 }
253
254 if (is_in_dex_cache) {
255 load_class->MarkInDexCache();
256 }
257
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100258 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
259 switch (load_kind) {
260 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
261 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
262 case HLoadClass::LoadKind::kDexCacheViaMethod:
263 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
264 break;
265 case HLoadClass::LoadKind::kBootImageAddress:
266 case HLoadClass::LoadKind::kDexCacheAddress:
267 DCHECK_NE(address, 0u);
268 load_class->SetLoadKindWithAddress(load_kind, address);
269 break;
270 case HLoadClass::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700271 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100272 DexCacheArraysLayout layout(pointer_size, &dex_file);
273 size_t element_index = layout.TypeOffset(type_index);
274 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
275 break;
276 }
277 default:
278 LOG(FATAL) << "Unexpected load kind: " << load_kind;
279 UNREACHABLE();
280 }
281}
282
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000283void HSharpening::ProcessLoadString(HLoadString* load_string) {
284 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
285 DCHECK(!load_string->IsInDexCache());
286
287 const DexFile& dex_file = load_string->GetDexFile();
288 uint32_t string_index = load_string->GetStringIndex();
289
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700290 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000291 uint64_t address = 0u; // String or dex cache element address.
292 {
293 Runtime* runtime = Runtime::Current();
294 ClassLinker* class_linker = runtime->GetClassLinker();
295 ScopedObjectAccess soa(Thread::Current());
296 StackHandleScope<1> hs(soa.Self());
297 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
298 ? compilation_unit_.GetDexCache()
299 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
300
301 if (compiler_driver_->IsBootImage()) {
302 // Compiling boot image. Resolve the string and allocate it if needed.
Calin Juravleffc87072016-04-20 14:22:09 +0100303 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000304 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
305 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000306 if (compiler_driver_->GetSupportBootImageFixup()) {
307 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
308 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
309 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
310 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
311 } else {
312 // MIPS64 or compiler_driver_test. Do not sharpen.
313 DCHECK_EQ(desired_load_kind, HLoadString::LoadKind::kDexCacheViaMethod);
314 }
Calin Juravleffc87072016-04-20 14:22:09 +0100315 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100316 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700317 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100318 mirror::String* string = dex_cache->GetResolvedString(string_index);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100319 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
320 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
321 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100322 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000323 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100324 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000325 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100326 if (string != nullptr &&
327 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
328 !codegen_->GetCompilerOptions().GetCompilePic()) {
329 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
330 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000331 }
332 }
333 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000334
335 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
336 switch (load_kind) {
337 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
338 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
339 case HLoadString::LoadKind::kDexCacheViaMethod:
340 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
341 break;
342 case HLoadString::LoadKind::kBootImageAddress:
343 case HLoadString::LoadKind::kDexCacheAddress:
344 DCHECK_NE(address, 0u);
345 load_string->SetLoadKindWithAddress(load_kind, address);
346 break;
347 case HLoadString::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700348 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000349 DexCacheArraysLayout layout(pointer_size, &dex_file);
350 size_t element_index = layout.StringOffset(string_index);
351 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
352 break;
353 }
354 }
355}
356
Vladimir Markodc151b22015-10-15 18:02:30 +0100357} // namespace art