blob: 81163e296e1b8566b679e944ebff7fefbfe0a4e8 [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;
179 HLoadClass::LoadKind desired_load_kind;
180 uint64_t address = 0u; // Class or dex cache element address.
181 {
182 ScopedObjectAccess soa(Thread::Current());
183 StackHandleScope<1> hs(soa.Self());
184 Runtime* runtime = Runtime::Current();
185 ClassLinker* class_linker = runtime->GetClassLinker();
186 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
187 ? compilation_unit_.GetDexCache()
188 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
189 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
190
191 if (compiler_driver_->IsBootImage()) {
192 // Compiling boot image. Check if the class is a boot image class.
193 DCHECK(!runtime->UseJitCompilation());
194 if (!compiler_driver_->GetSupportBootImageFixup()) {
195 // MIPS/MIPS64 or compiler_driver_test. Do not sharpen.
196 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
197 } else {
198 if (klass != nullptr &&
199 compiler_driver_->IsImageClass(
200 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
201 is_in_dex_cache = true;
202 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
203 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
204 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
205 } else {
206 // 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;
209 }
210 }
211 } else if (runtime->UseJitCompilation()) {
212 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
213 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
214 is_in_dex_cache = (klass != nullptr);
215 if (klass != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(klass)) {
216 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
217 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
218 address = reinterpret_cast64<uint64_t>(klass);
219 } else {
220 // Note: If the class is not in the dex cache or isn't initialized, the
221 // instruction needs environment and will not be inlined across dex files.
222 // Within a dex file, the slow-path helper loads the correct class and
223 // inlined frames are used correctly for OOM stack trace.
224 // TODO: Write a test for this. Bug: 29416588
225 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
226 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
227 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
228 }
229 } else {
230 // AOT app compilation. Check if the class is in the boot image.
231 if ((klass != nullptr) &&
232 runtime->GetHeap()->ObjectIsInBootImageSpace(klass) &&
233 !codegen_->GetCompilerOptions().GetCompilePic()) {
234 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
235 address = reinterpret_cast64<uint64_t>(klass);
236 } else {
237 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
238 // Use PC-relative load from the dex cache if the dex file belongs
239 // to the oat file that we're currently compiling.
240 desired_load_kind =
241 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_class->GetDexFile())
242 ? HLoadClass::LoadKind::kDexCachePcRelative
243 : HLoadClass::LoadKind::kDexCacheViaMethod;
244 }
245 }
246 }
247 if (is_in_dex_cache) {
248 load_class->MarkInDexCache();
249 }
250
251 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
252 switch (load_kind) {
253 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
254 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
255 case HLoadClass::LoadKind::kDexCacheViaMethod:
256 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
257 break;
258 case HLoadClass::LoadKind::kBootImageAddress:
259 case HLoadClass::LoadKind::kDexCacheAddress:
260 DCHECK_NE(address, 0u);
261 load_class->SetLoadKindWithAddress(load_kind, address);
262 break;
263 case HLoadClass::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700264 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100265 DexCacheArraysLayout layout(pointer_size, &dex_file);
266 size_t element_index = layout.TypeOffset(type_index);
267 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
268 break;
269 }
270 default:
271 LOG(FATAL) << "Unexpected load kind: " << load_kind;
272 UNREACHABLE();
273 }
274}
275
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000276void HSharpening::ProcessLoadString(HLoadString* load_string) {
277 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
278 DCHECK(!load_string->IsInDexCache());
279
280 const DexFile& dex_file = load_string->GetDexFile();
281 uint32_t string_index = load_string->GetStringIndex();
282
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700283 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000284 uint64_t address = 0u; // String or dex cache element address.
285 {
286 Runtime* runtime = Runtime::Current();
287 ClassLinker* class_linker = runtime->GetClassLinker();
288 ScopedObjectAccess soa(Thread::Current());
289 StackHandleScope<1> hs(soa.Self());
290 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
291 ? compilation_unit_.GetDexCache()
292 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
293
294 if (compiler_driver_->IsBootImage()) {
295 // Compiling boot image. Resolve the string and allocate it if needed.
Calin Juravleffc87072016-04-20 14:22:09 +0100296 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000297 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
298 CHECK(string != nullptr);
Vladimir Markof508c562016-09-01 15:33:04 +0000299 // TODO: In follow up CL, add PcRelative and Address back in.
Calin Juravleffc87072016-04-20 14:22:09 +0100300 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100301 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700302 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100303 mirror::String* string = dex_cache->GetResolvedString(string_index);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100304 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
305 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
306 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100307 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000308 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100309 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000310 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100311 if (string != nullptr &&
312 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
313 !codegen_->GetCompilerOptions().GetCompilePic()) {
314 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
315 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000316 }
317 }
318 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000319
320 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
321 switch (load_kind) {
322 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
323 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
324 case HLoadString::LoadKind::kDexCacheViaMethod:
325 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
326 break;
327 case HLoadString::LoadKind::kBootImageAddress:
328 case HLoadString::LoadKind::kDexCacheAddress:
329 DCHECK_NE(address, 0u);
330 load_string->SetLoadKindWithAddress(load_kind, address);
331 break;
332 case HLoadString::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700333 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000334 DexCacheArraysLayout layout(pointer_size, &dex_file);
335 size_t element_index = layout.StringOffset(string_index);
336 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
337 break;
338 }
339 }
340}
341
Vladimir Markodc151b22015-10-15 18:02:30 +0100342} // namespace art