blob: 97f34e6c32b8ab41dcc602e8d90afcde5a485161 [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"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000020#include "class_linker.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010021#include "code_generator.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000022#include "driver/dex_compilation_unit.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010023#include "utils/dex_cache_arrays_layout-inl.h"
24#include "driver/compiler_driver.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000025#include "gc/heap.h"
26#include "gc/space/image_space.h"
27#include "handle_scope-inl.h"
28#include "mirror/dex_cache.h"
29#include "mirror/string.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010030#include "nodes.h"
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000031#include "runtime.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000032#include "scoped_thread_state_change.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010033
34namespace art {
35
36void HSharpening::Run() {
37 // We don't care about the order of the blocks here.
38 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
39 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
40 HInstruction* instruction = it.Current();
41 if (instruction->IsInvokeStaticOrDirect()) {
42 ProcessInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010043 } else if (instruction->IsLoadClass()) {
44 ProcessLoadClass(instruction->AsLoadClass());
Vladimir Markocac5a7e2016-02-22 10:39:50 +000045 } else if (instruction->IsLoadString()) {
46 ProcessLoadString(instruction->AsLoadString());
Vladimir Markodc151b22015-10-15 18:02:30 +010047 }
48 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
49 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
50 // because we know the type better when inlining.
Vladimir Markodc151b22015-10-15 18:02:30 +010051 }
52 }
53}
54
55void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
56 if (invoke->IsStringInit()) {
57 // Not using the dex cache arrays. But we could still try to use a better dispatch...
58 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
59 return;
60 }
61
62 // TODO: Avoid CompilerDriver.
Nicolas Geoffraye5234232015-12-02 09:06:11 +000063 InvokeType original_invoke_type = invoke->GetOriginalInvokeType();
64 InvokeType optimized_invoke_type = original_invoke_type;
Vladimir Markodc151b22015-10-15 18:02:30 +010065 MethodReference target_method(&graph_->GetDexFile(), invoke->GetDexMethodIndex());
66 int vtable_idx;
67 uintptr_t direct_code, direct_method;
68 bool success = compiler_driver_->ComputeInvokeInfo(
69 &compilation_unit_,
70 invoke->GetDexPc(),
71 false /* update_stats: already updated in builder */,
72 true /* enable_devirtualization */,
Nicolas Geoffraye5234232015-12-02 09:06:11 +000073 &optimized_invoke_type,
Vladimir Markodc151b22015-10-15 18:02:30 +010074 &target_method,
75 &vtable_idx,
76 &direct_code,
77 &direct_method);
Nicolas Geoffraye5234232015-12-02 09:06:11 +000078 if (!success) {
79 // TODO: try using kDexCachePcRelative. It's always a valid method load
80 // kind as long as it's supported by the codegen
81 return;
82 }
83 invoke->SetOptimizedInvokeType(optimized_invoke_type);
84 invoke->SetTargetMethod(target_method);
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;
89 uint64_t direct_code_ptr = 0u;
90
91 HGraph* outer_graph = codegen_->GetGraph();
92 if (target_method.dex_file == &outer_graph->GetDexFile() &&
93 target_method.dex_method_index == outer_graph->GetMethodIdx()) {
94 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
95 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
96 } else {
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000097 bool use_pc_relative_instructions =
98 ((direct_method == 0u || direct_code == static_cast<uintptr_t>(-1))) &&
99 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), target_method.dex_file);
Vladimir Markodc151b22015-10-15 18:02:30 +0100100 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000101 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
102 // kDirectAddress would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100103 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100104 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
105 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
106 method_load_data = direct_method;
107 } else { // The direct pointer will be known at link time.
108 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
109 }
110 } else { // Use dex cache.
111 DCHECK_EQ(target_method.dex_file, &graph_->GetDexFile());
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000112 if (use_pc_relative_instructions) { // Can we use PC-relative access to the dex cache arrays?
Calin Juravleffc87072016-04-20 14:22:09 +0100113 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100114 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000115 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
116 &graph_->GetDexFile());
Vladimir Markodc151b22015-10-15 18:02:30 +0100117 method_load_data = layout.MethodOffset(target_method.dex_method_index);
118 } else { // We must go through the ArtMethod's pointer to resolved methods.
119 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
120 }
121 }
122 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000123 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
124 // while kCallDirect would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100125 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100126 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
127 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
128 direct_code_ptr = direct_code;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000129 } else if (use_pc_relative_instructions) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100130 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100131 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
132 } else { // The direct pointer will be known at link time.
133 // NOTE: This is used for app->boot calls when compiling an app against
134 // a relocatable but not yet relocated image.
135 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
136 }
137 } else { // We must use the code pointer from the ArtMethod.
138 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
139 }
140 }
141
142 if (graph_->IsDebuggable()) {
143 // For debuggable apps always use the code pointer from ArtMethod
144 // so that we don't circumvent instrumentation stubs if installed.
145 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
146 }
147
148 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
149 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
150 };
151 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
152 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info,
153 invoke->GetTargetMethod());
154 invoke->SetDispatchInfo(dispatch_info);
155}
156
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100157void HSharpening::ProcessLoadClass(HLoadClass* load_class) {
158 if (load_class->NeedsAccessCheck()) {
159 // We need to call the runtime anyway, so we simply get the class as that call's return value.
160 return;
161 }
162 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
163 // Loading from the ArtMethod* is the most efficient retrieval.
164 // TODO: This may not actually be true for all architectures and
165 // locations of target classes. The additional register pressure
166 // for using the ArtMethod* should be considered.
167 return;
168 }
169
170 DCHECK_EQ(load_class->GetLoadKind(), HLoadClass::LoadKind::kDexCacheViaMethod);
171 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
172
173 const DexFile& dex_file = load_class->GetDexFile();
174 uint32_t type_index = load_class->GetTypeIndex();
175
176 bool is_in_dex_cache = false;
177 HLoadClass::LoadKind desired_load_kind;
178 uint64_t address = 0u; // Class or dex cache element address.
179 {
180 ScopedObjectAccess soa(Thread::Current());
181 StackHandleScope<1> hs(soa.Self());
182 Runtime* runtime = Runtime::Current();
183 ClassLinker* class_linker = runtime->GetClassLinker();
184 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
185 ? compilation_unit_.GetDexCache()
186 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
187 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
188
189 if (compiler_driver_->IsBootImage()) {
190 // Compiling boot image. Check if the class is a boot image class.
191 DCHECK(!runtime->UseJitCompilation());
192 if (!compiler_driver_->GetSupportBootImageFixup()) {
193 // MIPS/MIPS64 or compiler_driver_test. Do not sharpen.
194 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
195 } else {
196 if (klass != nullptr &&
197 compiler_driver_->IsImageClass(
198 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
199 is_in_dex_cache = true;
200 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
201 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
202 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
203 } else {
204 // Not a boot image class. We must go through the dex cache.
205 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
206 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
207 }
208 }
209 } else if (runtime->UseJitCompilation()) {
210 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
211 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
212 is_in_dex_cache = (klass != nullptr);
213 if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass)) {
214 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
215 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
216 address = reinterpret_cast64<uint64_t>(klass);
217 } else {
218 // Note: If the class is not in the dex cache or isn't initialized, the
219 // instruction needs environment and will not be inlined across dex files.
220 // Within a dex file, the slow-path helper loads the correct class and
221 // inlined frames are used correctly for OOM stack trace.
222 // TODO: Write a test for this. Bug: 29416588
223 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
224 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
225 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
226 }
227 } else {
228 // AOT app compilation. Check if the class is in the boot image.
229 if ((klass != nullptr) &&
230 runtime->GetHeap()->ObjectIsInBootImageSpace(klass) &&
231 !codegen_->GetCompilerOptions().GetCompilePic()) {
232 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
233 address = reinterpret_cast64<uint64_t>(klass);
234 } else {
235 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
236 // Use PC-relative load from the dex cache if the dex file belongs
237 // to the oat file that we're currently compiling.
238 desired_load_kind =
239 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_class->GetDexFile())
240 ? HLoadClass::LoadKind::kDexCachePcRelative
241 : HLoadClass::LoadKind::kDexCacheViaMethod;
242 }
243 }
244 }
245 if (is_in_dex_cache) {
246 load_class->MarkInDexCache();
247 }
248
249 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
250 switch (load_kind) {
251 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
252 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
253 case HLoadClass::LoadKind::kDexCacheViaMethod:
254 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
255 break;
256 case HLoadClass::LoadKind::kBootImageAddress:
257 case HLoadClass::LoadKind::kDexCacheAddress:
258 DCHECK_NE(address, 0u);
259 load_class->SetLoadKindWithAddress(load_kind, address);
260 break;
261 case HLoadClass::LoadKind::kDexCachePcRelative: {
262 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
263 DexCacheArraysLayout layout(pointer_size, &dex_file);
264 size_t element_index = layout.TypeOffset(type_index);
265 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
266 break;
267 }
268 default:
269 LOG(FATAL) << "Unexpected load kind: " << load_kind;
270 UNREACHABLE();
271 }
272}
273
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000274void HSharpening::ProcessLoadString(HLoadString* load_string) {
275 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
276 DCHECK(!load_string->IsInDexCache());
277
278 const DexFile& dex_file = load_string->GetDexFile();
279 uint32_t string_index = load_string->GetStringIndex();
280
281 bool is_in_dex_cache = false;
282 HLoadString::LoadKind desired_load_kind;
283 uint64_t address = 0u; // String or dex cache element address.
284 {
285 Runtime* runtime = Runtime::Current();
286 ClassLinker* class_linker = runtime->GetClassLinker();
287 ScopedObjectAccess soa(Thread::Current());
288 StackHandleScope<1> hs(soa.Self());
289 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
290 ? compilation_unit_.GetDexCache()
291 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
292
293 if (compiler_driver_->IsBootImage()) {
294 // Compiling boot image. Resolve the string and allocate it if needed.
Calin Juravleffc87072016-04-20 14:22:09 +0100295 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000296 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
297 CHECK(string != nullptr);
298 if (!compiler_driver_->GetSupportBootImageFixup()) {
299 // MIPS/MIPS64 or compiler_driver_test. Do not sharpen.
300 desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
301 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100302 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000303 is_in_dex_cache = true;
304 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
305 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
306 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
307 }
Calin Juravleffc87072016-04-20 14:22:09 +0100308 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100309 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
310 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
311 mirror::String* string = dex_cache->GetResolvedString(string_index);
312 is_in_dex_cache = (string != nullptr);
313 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100314 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100315 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
316 address = reinterpret_cast64<uint64_t>(string);
317 } else {
318 // Note: If the string is not in the dex cache, the instruction needs environment
319 // and will not be inlined across dex files. Within a dex file, the slow-path helper
320 // loads the correct string and inlined frames are used correctly for OOM stack trace.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100321 // TODO: Write a test for this. Bug: 29416588
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100322 desired_load_kind = HLoadString::LoadKind::kDexCacheAddress;
323 void* dex_cache_element_address = &dex_cache->GetStrings()[string_index];
324 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
325 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000326 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100327 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000328 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100329 if (string != nullptr &&
330 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
331 !codegen_->GetCompilerOptions().GetCompilePic()) {
332 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
333 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000334 } else {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100335 // Not JIT and either the string is not in boot image or we are compiling in PIC mode.
336 // Use PC-relative load from the dex cache if the dex file belongs
337 // to the oat file that we're currently compiling.
338 desired_load_kind = ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file)
339 ? HLoadString::LoadKind::kDexCachePcRelative
340 : HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000341 }
342 }
343 }
344 if (is_in_dex_cache) {
345 load_string->MarkInDexCache();
346 }
347
348 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
349 switch (load_kind) {
350 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
351 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
352 case HLoadString::LoadKind::kDexCacheViaMethod:
353 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
354 break;
355 case HLoadString::LoadKind::kBootImageAddress:
356 case HLoadString::LoadKind::kDexCacheAddress:
357 DCHECK_NE(address, 0u);
358 load_string->SetLoadKindWithAddress(load_kind, address);
359 break;
360 case HLoadString::LoadKind::kDexCachePcRelative: {
361 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
362 DexCacheArraysLayout layout(pointer_size, &dex_file);
363 size_t element_index = layout.StringOffset(string_index);
364 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
365 break;
366 }
367 }
368}
369
Vladimir Markodc151b22015-10-15 18:02:30 +0100370} // namespace art