blob: a4a3e0695d69b11b507fa8238490d4727ea9cdc2 [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"
Mathieu Chartier0795f232016-09-27 18:43:30 -070034#include "scoped_thread_state_change-inl.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
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010064 HGraph* outer_graph = codegen_->GetGraph();
65 ArtMethod* compiling_method = graph_->GetArtMethod();
Vladimir Markodc151b22015-10-15 18:02:30 +010066
67 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
68 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
69 uint64_t method_load_data = 0u;
70 uint64_t direct_code_ptr = 0u;
71
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010072 if (invoke->GetResolvedMethod() == outer_graph->GetArtMethod()) {
73 DCHECK(outer_graph->GetArtMethod() != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010074 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
75 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
76 } else {
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010077 uintptr_t direct_code, direct_method;
78 {
79 ScopedObjectAccess soa(Thread::Current());
80 compiler_driver_->GetCodeAndMethodForDirectCall(
81 (compiling_method == nullptr) ? nullptr : compiling_method->GetDeclaringClass(),
82 invoke->GetResolvedMethod(),
83 &direct_code,
84 &direct_method);
85 }
Vladimir Markodc151b22015-10-15 18:02:30 +010086 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000087 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
88 // kDirectAddress would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +010089 DCHECK(!Runtime::Current()->UseJitCompilation());
Vladimir Markodc151b22015-10-15 18:02:30 +010090 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
91 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
92 method_load_data = direct_method;
93 } else { // The direct pointer will be known at link time.
94 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
95 }
96 } else { // Use dex cache.
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +010097 if (!Runtime::Current()->UseJitCompilation()) {
98 // Use PC-relative access to the dex cache arrays.
Vladimir Markodc151b22015-10-15 18:02:30 +010099 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000100 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
101 &graph_->GetDexFile());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100102 method_load_data = layout.MethodOffset(invoke->GetDexMethodIndex());
Vladimir Markodc151b22015-10-15 18:02:30 +0100103 } else { // We must go through the ArtMethod's pointer to resolved methods.
104 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
105 }
106 }
107 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000108 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
109 // while kCallDirect would be fine for image methods, we don't support it at the moment.
Calin Juravleffc87072016-04-20 14:22:09 +0100110 DCHECK(!Runtime::Current()->UseJitCompilation());
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100111 const DexFile* dex_file_of_callee = invoke->GetTargetMethod().dex_file;
Vladimir Markodc151b22015-10-15 18:02:30 +0100112 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
113 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
114 direct_code_ptr = direct_code;
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100115 } else if (ContainsElement(compiler_driver_->GetDexFilesForOatFile(), dex_file_of_callee)) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100116 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100117 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
118 } else { // The direct pointer will be known at link time.
119 // NOTE: This is used for app->boot calls when compiling an app against
120 // a relocatable but not yet relocated image.
121 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
122 }
123 } else { // We must use the code pointer from the ArtMethod.
124 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
125 }
126 }
127
128 if (graph_->IsDebuggable()) {
129 // For debuggable apps always use the code pointer from ArtMethod
130 // so that we don't circumvent instrumentation stubs if installed.
131 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
132 }
133
134 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
135 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
136 };
137 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100138 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100139 invoke->SetDispatchInfo(dispatch_info);
140}
141
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100142void HSharpening::ProcessLoadClass(HLoadClass* load_class) {
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700143 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
144 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
145 << load_class->GetLoadKind();
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100146 DCHECK(!load_class->IsInDexCache()) << "HLoadClass should not be optimized before sharpening.";
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700147 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100148
149 const DexFile& dex_file = load_class->GetDexFile();
150 uint32_t type_index = load_class->GetTypeIndex();
151
152 bool is_in_dex_cache = false;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700153 bool is_in_boot_image = false;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100154 HLoadClass::LoadKind desired_load_kind;
155 uint64_t address = 0u; // Class or dex cache element address.
156 {
157 ScopedObjectAccess soa(Thread::Current());
158 StackHandleScope<1> hs(soa.Self());
159 Runtime* runtime = Runtime::Current();
160 ClassLinker* class_linker = runtime->GetClassLinker();
161 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
162 ? compilation_unit_.GetDexCache()
163 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
164 mirror::Class* klass = dex_cache->GetResolvedType(type_index);
165
166 if (compiler_driver_->IsBootImage()) {
167 // Compiling boot image. Check if the class is a boot image class.
168 DCHECK(!runtime->UseJitCompilation());
169 if (!compiler_driver_->GetSupportBootImageFixup()) {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700170 // MIPS64 or compiler_driver_test. Do not sharpen.
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100171 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700172 } else if ((klass != nullptr) && compiler_driver_->IsImageClass(
173 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
174 is_in_boot_image = true;
175 is_in_dex_cache = true;
176 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
177 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
178 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100179 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700180 // Not a boot image class. We must go through the dex cache.
181 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
182 desired_load_kind = HLoadClass::LoadKind::kDexCachePcRelative;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100183 }
184 } else {
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700185 is_in_boot_image = (klass != nullptr) && runtime->GetHeap()->ObjectIsInBootImageSpace(klass);
186 if (runtime->UseJitCompilation()) {
187 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
188 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
189 is_in_dex_cache = (klass != nullptr);
190 if (is_in_boot_image) {
191 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
192 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
193 address = reinterpret_cast64<uint64_t>(klass);
194 } else {
195 // Note: If the class is not in the dex cache or isn't initialized, the
196 // instruction needs environment and will not be inlined across dex files.
197 // Within a dex file, the slow-path helper loads the correct class and
198 // inlined frames are used correctly for OOM stack trace.
199 // TODO: Write a test for this. Bug: 29416588
200 desired_load_kind = HLoadClass::LoadKind::kDexCacheAddress;
201 void* dex_cache_element_address = &dex_cache->GetResolvedTypes()[type_index];
202 address = reinterpret_cast64<uint64_t>(dex_cache_element_address);
203 }
204 // AOT app compilation. Check if the class is in the boot image.
205 } else if (is_in_boot_image && !codegen_->GetCompilerOptions().GetCompilePic()) {
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100206 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
207 address = reinterpret_cast64<uint64_t>(klass);
208 } else {
209 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
210 // Use PC-relative load from the dex cache if the dex file belongs
211 // to the oat file that we're currently compiling.
212 desired_load_kind =
213 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_class->GetDexFile())
214 ? HLoadClass::LoadKind::kDexCachePcRelative
215 : HLoadClass::LoadKind::kDexCacheViaMethod;
216 }
217 }
218 }
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700219
Mathieu Chartier31b12e32016-09-02 17:11:57 -0700220 if (is_in_boot_image) {
221 load_class->MarkInBootImage();
222 }
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100223
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700224 if (load_class->NeedsAccessCheck()) {
225 // We need to call the runtime anyway, so we simply get the class as that call's return value.
226 return;
227 }
228
229 if (load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass) {
230 // Loading from the ArtMethod* is the most efficient retrieval in code size.
231 // TODO: This may not actually be true for all architectures and
232 // locations of target classes. The additional register pressure
233 // for using the ArtMethod* should be considered.
234 return;
235 }
236
237 if (is_in_dex_cache) {
238 load_class->MarkInDexCache();
239 }
240
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100241 HLoadClass::LoadKind load_kind = codegen_->GetSupportedLoadClassKind(desired_load_kind);
242 switch (load_kind) {
243 case HLoadClass::LoadKind::kBootImageLinkTimeAddress:
244 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
245 case HLoadClass::LoadKind::kDexCacheViaMethod:
246 load_class->SetLoadKindWithTypeReference(load_kind, dex_file, type_index);
247 break;
248 case HLoadClass::LoadKind::kBootImageAddress:
249 case HLoadClass::LoadKind::kDexCacheAddress:
250 DCHECK_NE(address, 0u);
251 load_class->SetLoadKindWithAddress(load_kind, address);
252 break;
253 case HLoadClass::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700254 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100255 DexCacheArraysLayout layout(pointer_size, &dex_file);
256 size_t element_index = layout.TypeOffset(type_index);
257 load_class->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
258 break;
259 }
260 default:
261 LOG(FATAL) << "Unexpected load kind: " << load_kind;
262 UNREACHABLE();
263 }
264}
265
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000266void HSharpening::ProcessLoadString(HLoadString* load_string) {
267 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
268 DCHECK(!load_string->IsInDexCache());
269
270 const DexFile& dex_file = load_string->GetDexFile();
271 uint32_t string_index = load_string->GetStringIndex();
272
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -0700273 HLoadString::LoadKind desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000274 uint64_t address = 0u; // String or dex cache element address.
275 {
276 Runtime* runtime = Runtime::Current();
277 ClassLinker* class_linker = runtime->GetClassLinker();
278 ScopedObjectAccess soa(Thread::Current());
279 StackHandleScope<1> hs(soa.Self());
280 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
281 ? compilation_unit_.GetDexCache()
282 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
283
284 if (compiler_driver_->IsBootImage()) {
285 // Compiling boot image. Resolve the string and allocate it if needed.
Calin Juravleffc87072016-04-20 14:22:09 +0100286 DCHECK(!runtime->UseJitCompilation());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000287 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
288 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000289 if (compiler_driver_->GetSupportBootImageFixup()) {
290 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
291 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
292 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
293 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
294 } else {
295 // MIPS64 or compiler_driver_test. Do not sharpen.
296 DCHECK_EQ(desired_load_kind, HLoadString::LoadKind::kDexCacheViaMethod);
297 }
Calin Juravleffc87072016-04-20 14:22:09 +0100298 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100299 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700300 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100301 mirror::String* string = dex_cache->GetResolvedString(string_index);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100302 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
303 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
304 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100305 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000306 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100307 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000308 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100309 if (string != nullptr &&
310 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
311 !codegen_->GetCompilerOptions().GetCompilePic()) {
312 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
313 address = reinterpret_cast64<uint64_t>(string);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000314 }
315 }
316 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000317
318 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
319 switch (load_kind) {
320 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
321 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
322 case HLoadString::LoadKind::kDexCacheViaMethod:
323 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
324 break;
325 case HLoadString::LoadKind::kBootImageAddress:
326 case HLoadString::LoadKind::kDexCacheAddress:
327 DCHECK_NE(address, 0u);
328 load_string->SetLoadKindWithAddress(load_kind, address);
329 break;
330 case HLoadString::LoadKind::kDexCachePcRelative: {
Andreas Gampe542451c2016-07-26 09:02:02 -0700331 PointerSize pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000332 DexCacheArraysLayout layout(pointer_size, &dex_file);
333 size_t element_index = layout.StringOffset(string_index);
334 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
335 break;
336 }
337 }
338}
339
Vladimir Markodc151b22015-10-15 18:02:30 +0100340} // namespace art