blob: b8e1379ef9286b52a4b7cca36e1e244df7fe9173 [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) {
160 if (load_class->NeedsAccessCheck()) {
161 // We need to call the runtime anyway, so we simply get the class as that call's return value.
162 return;
163 }
164 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
165 // Loading from the ArtMethod* is the most efficient retrieval.
166 // TODO: This may not actually be true for all architectures and
167 // locations of target classes. The additional register pressure
168 // for using the ArtMethod* should be considered.
169 return;
170 }
171
172 DCHECK_EQ(load_class->GetLoadKind(), HLoadClass::LoadKind::kDexCacheViaMethod);
173 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
174
175 const DexFile& dex_file = load_class->GetDexFile();
176 uint32_t type_index = load_class->GetTypeIndex();
177
178 bool is_in_dex_cache = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700179 bool is_in_boot_image = false;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100180 HLoadClass::LoadKind desired_load_kind;
181 uint64_t address = 0u; // Class or dex cache element address.
182 {
183 ScopedObjectAccess soa(Thread::Current());
184 StackHandleScope<1> hs(soa.Self());
185 Runtime* runtime = Runtime::Current();
186 ClassLinker* class_linker = runtime->GetClassLinker();
187 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
188 ? compilation_unit_.GetDexCache()
189 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
190 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
191
192 if (compiler_driver_->IsBootImage()) {
193 // Compiling boot image. Check if the class is a boot image class.
194 DCHECK(!runtime->UseJitCompilation());
195 if (!compiler_driver_->GetSupportBootImageFixup()) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700196 // MIPS64 or compiler_driver_test. Do not sharpen.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100197 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700198 } else if ((klass != nullptr) && compiler_driver_->IsImageClass(
199 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
200 is_in_boot_image = true;
201 is_in_dex_cache = true;
202 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
203 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
204 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100205 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700206 // Not a boot image class. We must go through the dex cache.
207 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
208 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100209 }
210 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700211 is_in_boot_image = (klass != nullptr) && runtime->GetHeap()->ObjectIsInBootImageSpace(klass);
212 if (runtime->UseJitCompilation()) {
213 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
214 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
215 is_in_dex_cache = (klass != nullptr);
216 if (is_in_boot_image) {
217 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
218 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
219 address = reinterpret_cast64<uint64_t>(klass);
220 } else {
221 // Note: If the class is not in the dex cache or isn't initialized, the
222 // instruction needs environment and will not be inlined across dex files.
223 // Within a dex file, the slow-path helper loads the correct class and
224 // inlined frames are used correctly for OOM stack trace.
225 // TODO: Write a test for this. Bug: 29416588
226 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
227 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
228 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
229 }
230 // AOT app compilation. Check if the class is in the boot image.
231 } else if (is_in_boot_image && !codegen_->GetCompilerOptions().GetCompilePic()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100232 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 }
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700248 if (is_in_boot_image) {
249 load_class->MarkInBootImage();
250 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100251
252 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
253 switch (load_kind) {
254 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
255 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
256 case HLoadClass::LoadKind::kDexCacheViaMethod:
257 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
258 break;
259 case HLoadClass::LoadKind::kBootImageAddress:
260 case HLoadClass::LoadKind::kDexCacheAddress:
261 DCHECK_NE(address, 0u);
262 load_class->SetLoadKindWithAddress(load_kind, address);
263 break;
264 case HLoadClass::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700265 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100266 DexCacheArraysLayout layout(pointer_size, &dex_file);
267 size_t element_index = layout.TypeOffset(type_index);
268 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
269 break;
270 }
271 default:
272 LOG(FATAL) << "Unexpected load kind: " << load_kind;
273 UNREACHABLE();
274 }
275}
276
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000277void HSharpening::ProcessLoadString(HLoadString* load_string) {
278 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
279 DCHECK(!load_string->IsInDexCache());
280
281 const DexFile& dex_file = load_string->GetDexFile();
282 uint32_t string_index = load_string->GetStringIndex();
283
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700284 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000285 uint64_t address = 0u; // String or dex cache element address.
286 {
287 Runtime* runtime = Runtime::Current();
288 ClassLinker* class_linker = runtime->GetClassLinker();
289 ScopedObjectAccess soa(Thread::Current());
290 StackHandleScope<1> hs(soa.Self());
291 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
292 ? compilation_unit_.GetDexCache()
293 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
294
295 if (compiler_driver_->IsBootImage()) {
296 // Compiling boot image. Resolve the string and allocate it if needed.
Calin Juravleffc87072016-04-20 14:22:09 +0100297 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000298 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
299 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000300 if (compiler_driver_->GetSupportBootImageFixup()) {
301 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
302 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
303 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
304 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
305 } else {
306 // MIPS64 or compiler_driver_test. Do not sharpen.
307 DCHECK_EQ(desired_load_kind, HLoadString::LoadKind::kDexCacheViaMethod);
308 }
Calin Juravleffc87072016-04-20 14:22:09 +0100309 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100310 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700311 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100312 mirror::String* string = dex_cache->GetResolvedString(string_index);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100313 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
314 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
315 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100316 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000317 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100318 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000319 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100320 if (string != nullptr &&
321 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
322 !codegen_->GetCompilerOptions().GetCompilePic()) {
323 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
324 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000325 }
326 }
327 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000328
329 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
330 switch (load_kind) {
331 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
332 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
333 case HLoadString::LoadKind::kDexCacheViaMethod:
334 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
335 break;
336 case HLoadString::LoadKind::kBootImageAddress:
337 case HLoadString::LoadKind::kDexCacheAddress:
338 DCHECK_NE(address, 0u);
339 load_string->SetLoadKindWithAddress(load_kind, address);
340 break;
341 case HLoadString::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700342 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000343 DexCacheArraysLayout layout(pointer_size, &dex_file);
344 size_t element_index = layout.StringOffset(string_index);
345 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
346 break;
347 }
348 }
349}
350
Vladimir Markodc151b22015-10-15 18:02:30 +0100351} // namespace art