blob: 6effc306dce4a2b05f4aa00af05c774c8bc532d4 [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 Markocac5a7e2016-02-22 10:39:50 +000023#include "driver/dex_compilation_unit.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010024#include "utils/dex_cache_arrays_layout-inl.h"
25#include "driver/compiler_driver.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000026#include "gc/heap.h"
27#include "gc/space/image_space.h"
28#include "handle_scope-inl.h"
29#include "mirror/dex_cache.h"
30#include "mirror/string.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010031#include "nodes.h"
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000032#include "runtime.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000033#include "scoped_thread_state_change.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010034
35namespace art {
36
37void HSharpening::Run() {
38 // We don't care about the order of the blocks here.
39 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
40 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
41 HInstruction* instruction = it.Current();
42 if (instruction->IsInvokeStaticOrDirect()) {
43 ProcessInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +010044 } else if (instruction->IsLoadClass()) {
45 ProcessLoadClass(instruction->AsLoadClass());
Vladimir Markocac5a7e2016-02-22 10:39:50 +000046 } else if (instruction->IsLoadString()) {
47 ProcessLoadString(instruction->AsLoadString());
Vladimir Markodc151b22015-10-15 18:02:30 +010048 }
49 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
50 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
51 // because we know the type better when inlining.
Vladimir Markodc151b22015-10-15 18:02:30 +010052 }
53 }
54}
55
56void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
57 if (invoke->IsStringInit()) {
58 // Not using the dex cache arrays. But we could still try to use a better dispatch...
59 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
60 return;
61 }
62
63 // TODO: Avoid CompilerDriver.
Nicolas Geoffraye5234232015-12-02 09:06:11 +000064 InvokeType original_invoke_type = invoke->GetOriginalInvokeType();
65 InvokeType optimized_invoke_type = original_invoke_type;
Vladimir Markodc151b22015-10-15 18:02:30 +010066 MethodReference target_method(&graph_->GetDexFile(), invoke->GetDexMethodIndex());
67 int vtable_idx;
68 uintptr_t direct_code, direct_method;
69 bool success = compiler_driver_->ComputeInvokeInfo(
70 &compilation_unit_,
71 invoke->GetDexPc(),
72 false /* update_stats: already updated in builder */,
73 true /* enable_devirtualization */,
Nicolas Geoffraye5234232015-12-02 09:06:11 +000074 &optimized_invoke_type,
Vladimir Markodc151b22015-10-15 18:02:30 +010075 &target_method,
76 &vtable_idx,
77 &direct_code,
78 &direct_method);
Nicolas Geoffraye5234232015-12-02 09:06:11 +000079 if (!success) {
80 // TODO: try using kDexCachePcRelative. It's always a valid method load
81 // kind as long as it's supported by the codegen
82 return;
83 }
84 invoke->SetOptimizedInvokeType(optimized_invoke_type);
85 invoke->SetTargetMethod(target_method);
Vladimir Markodc151b22015-10-15 18:02:30 +010086
87 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
88 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
89 uint64_t method_load_data = 0u;
90 uint64_t direct_code_ptr = 0u;
91
92 HGraph* outer_graph = codegen_->GetGraph();
93 if (target_method.dex_file == &outer_graph->GetDexFile() &&
94 target_method.dex_method_index == outer_graph->GetMethodIdx()) {
95 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
96 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
97 } else {
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000098 bool use_pc_relative_instructions =
99 ((direct_method == 0u || direct_code == static_cast<uintptr_t>(-1))) &&
100 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), target_method.dex_file);
Vladimir Markodc151b22015-10-15 18:02:30 +0100101 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000102 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
103 // kDirectAddress would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100104 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100105 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
106 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
107 method_load_data = direct_method;
108 } else { // The direct pointer will be known at link time.
109 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
110 }
111 } else { // Use dex cache.
112 DCHECK_EQ(target_method.dex_file, &graph_->GetDexFile());
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000113 if (use_pc_relative_instructions) { // Can we use PC-relative access to the dex cache arrays?
Calin Juravleffc87072016-04-20 14:22:09 +0100114 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100115 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000116 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
117 &graph_->GetDexFile());
Vladimir Markodc151b22015-10-15 18:02:30 +0100118 method_load_data = layout.MethodOffset(target_method.dex_method_index);
119 } else { // We must go through the ArtMethod's pointer to resolved methods.
120 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
121 }
122 }
123 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000124 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
125 // while kCallDirect would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100126 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +0100127 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
128 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
129 direct_code_ptr = direct_code;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000130 } else if (use_pc_relative_instructions) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100131 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100132 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
133 } else { // The direct pointer will be known at link time.
134 // NOTE: This is used for app->boot calls when compiling an app against
135 // a relocatable but not yet relocated image.
136 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
137 }
138 } else { // We must use the code pointer from the ArtMethod.
139 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
140 }
141 }
142
143 if (graph_->IsDebuggable()) {
144 // For debuggable apps always use the code pointer from ArtMethod
145 // so that we don't circumvent instrumentation stubs if installed.
146 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
147 }
148
149 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
150 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
151 };
152 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
153 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info,
154 invoke->GetTargetMethod());
155 invoke->SetDispatchInfo(dispatch_info);
156}
157
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100158void HSharpening::ProcessLoadClass(HLoadClass* load_class) {
159 if (load_class->NeedsAccessCheck()) {
160 // We need to call the runtime anyway, so we simply get the class as that call's return value.
161 return;
162 }
163 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
164 // Loading from the ArtMethod* is the most efficient retrieval.
165 // TODO: This may not actually be true for all architectures and
166 // locations of target classes. The additional register pressure
167 // for using the ArtMethod* should be considered.
168 return;
169 }
170
171 DCHECK_EQ(load_class->GetLoadKind(), HLoadClass::LoadKind::kDexCacheViaMethod);
172 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
173
174 const DexFile& dex_file = load_class->GetDexFile();
175 uint32_t type_index = load_class->GetTypeIndex();
176
177 bool is_in_dex_cache = false;
178 HLoadClass::LoadKind desired_load_kind;
179 uint64_t address = 0u; // Class or dex cache element address.
180 {
181 ScopedObjectAccess soa(Thread::Current());
182 StackHandleScope<1> hs(soa.Self());
183 Runtime* runtime = Runtime::Current();
184 ClassLinker* class_linker = runtime->GetClassLinker();
185 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
186 ? compilation_unit_.GetDexCache()
187 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
188 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
189
190 if (compiler_driver_->IsBootImage()) {
191 // Compiling boot image. Check if the class is a boot image class.
192 DCHECK(!runtime->UseJitCompilation());
193 if (!compiler_driver_->GetSupportBootImageFixup()) {
194 // MIPS/MIPS64 or compiler_driver_test. Do not sharpen.
195 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
196 } else {
197 if (klass != nullptr &&
198 compiler_driver_->IsImageClass(
199 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
200 is_in_dex_cache = true;
201 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
202 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
203 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
204 } else {
205 // Not a boot image class. We must go through the dex cache.
206 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
207 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
208 }
209 }
210 } else if (runtime->UseJitCompilation()) {
211 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
212 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
213 is_in_dex_cache = (klass != nullptr);
214 if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass)) {
215 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
216 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
217 address = reinterpret_cast64<uint64_t>(klass);
218 } else {
219 // Note: If the class is not in the dex cache or isn't initialized, the
220 // instruction needs environment and will not be inlined across dex files.
221 // Within a dex file, the slow-path helper loads the correct class and
222 // inlined frames are used correctly for OOM stack trace.
223 // TODO: Write a test for this. Bug: 29416588
224 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
225 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
226 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
227 }
228 } else {
229 // AOT app compilation. Check if the class is in the boot image.
230 if ((klass != nullptr) &&
231 runtime->GetHeap()->ObjectIsInBootImageSpace(klass) &&
232 !codegen_->GetCompilerOptions().GetCompilePic()) {
233 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
234 address = reinterpret_cast64<uint64_t>(klass);
235 } else {
236 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
237 // Use PC-relative load from the dex cache if the dex file belongs
238 // to the oat file that we're currently compiling.
239 desired_load_kind =
240 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_class->GetDexFile())
241 ? HLoadClass::LoadKind::kDexCachePcRelative
242 : HLoadClass::LoadKind::kDexCacheViaMethod;
243 }
244 }
245 }
246 if (is_in_dex_cache) {
247 load_class->MarkInDexCache();
248 }
249
250 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
251 switch (load_kind) {
252 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
253 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
254 case HLoadClass::LoadKind::kDexCacheViaMethod:
255 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
256 break;
257 case HLoadClass::LoadKind::kBootImageAddress:
258 case HLoadClass::LoadKind::kDexCacheAddress:
259 DCHECK_NE(address, 0u);
260 load_class->SetLoadKindWithAddress(load_kind, address);
261 break;
262 case HLoadClass::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700263 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100264 DexCacheArraysLayout layout(pointer_size, &dex_file);
265 size_t element_index = layout.TypeOffset(type_index);
266 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
267 break;
268 }
269 default:
270 LOG(FATAL) << "Unexpected load kind: " << load_kind;
271 UNREACHABLE();
272 }
273}
274
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000275void HSharpening::ProcessLoadString(HLoadString* load_string) {
276 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
277 DCHECK(!load_string->IsInDexCache());
278
279 const DexFile& dex_file = load_string->GetDexFile();
280 uint32_t string_index = load_string->GetStringIndex();
281
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700282 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000283 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);
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700298 // TODO: In follow up CL, add PcRelative and Address back in.
Calin Juravleffc87072016-04-20 14:22:09 +0100299 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100300 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700301 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100302 mirror::String* string = dex_cache->GetResolvedString(string_index);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100303 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
304 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
305 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100306 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000307 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100308 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000309 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100310 if (string != nullptr &&
311 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
312 !codegen_->GetCompilerOptions().GetCompilePic()) {
313 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
314 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000315 }
316 }
317 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000318
319 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
320 switch (load_kind) {
321 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
322 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
323 case HLoadString::LoadKind::kDexCacheViaMethod:
324 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
325 break;
326 case HLoadString::LoadKind::kBootImageAddress:
327 case HLoadString::LoadKind::kDexCacheAddress:
328 DCHECK_NE(address, 0u);
329 load_string->SetLoadKindWithAddress(load_kind, address);
330 break;
331 case HLoadString::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700332 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000333 DexCacheArraysLayout layout(pointer_size, &dex_file);
334 size_t element_index = layout.StringOffset(string_index);
335 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
336 break;
337 }
338 }
339}
340
Vladimir Markodc151b22015-10-15 18:02:30 +0100341} // namespace art