blob: 45ae3366c1b736ab82b6bee3d19562bbe9a0379d [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 Markocac5a7e2016-02-22 10:39:50 +000019#include "class_linker.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010020#include "code_generator.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000021#include "driver/dex_compilation_unit.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010022#include "utils/dex_cache_arrays_layout-inl.h"
23#include "driver/compiler_driver.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000024#include "gc/heap.h"
25#include "gc/space/image_space.h"
26#include "handle_scope-inl.h"
27#include "mirror/dex_cache.h"
28#include "mirror/string.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010029#include "nodes.h"
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000030#include "runtime.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000031#include "scoped_thread_state_change.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010032
33namespace art {
34
35void HSharpening::Run() {
36 // We don't care about the order of the blocks here.
37 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
38 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
39 HInstruction* instruction = it.Current();
40 if (instruction->IsInvokeStaticOrDirect()) {
41 ProcessInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect());
Vladimir Markocac5a7e2016-02-22 10:39:50 +000042 } else if (instruction->IsLoadString()) {
43 ProcessLoadString(instruction->AsLoadString());
Vladimir Markodc151b22015-10-15 18:02:30 +010044 }
45 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
46 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
47 // because we know the type better when inlining.
Vladimir Markocac5a7e2016-02-22 10:39:50 +000048 // TODO: HLoadClass - select better load kind if available.
Vladimir Markodc151b22015-10-15 18:02:30 +010049 }
50 }
51}
52
53void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
54 if (invoke->IsStringInit()) {
55 // Not using the dex cache arrays. But we could still try to use a better dispatch...
56 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
57 return;
58 }
59
60 // TODO: Avoid CompilerDriver.
Nicolas Geoffraye5234232015-12-02 09:06:11 +000061 InvokeType original_invoke_type = invoke->GetOriginalInvokeType();
62 InvokeType optimized_invoke_type = original_invoke_type;
Vladimir Markodc151b22015-10-15 18:02:30 +010063 MethodReference target_method(&graph_->GetDexFile(), invoke->GetDexMethodIndex());
64 int vtable_idx;
65 uintptr_t direct_code, direct_method;
66 bool success = compiler_driver_->ComputeInvokeInfo(
67 &compilation_unit_,
68 invoke->GetDexPc(),
69 false /* update_stats: already updated in builder */,
70 true /* enable_devirtualization */,
Nicolas Geoffraye5234232015-12-02 09:06:11 +000071 &optimized_invoke_type,
Vladimir Markodc151b22015-10-15 18:02:30 +010072 &target_method,
73 &vtable_idx,
74 &direct_code,
75 &direct_method);
Nicolas Geoffraye5234232015-12-02 09:06:11 +000076 if (!success) {
77 // TODO: try using kDexCachePcRelative. It's always a valid method load
78 // kind as long as it's supported by the codegen
79 return;
80 }
81 invoke->SetOptimizedInvokeType(optimized_invoke_type);
82 invoke->SetTargetMethod(target_method);
Vladimir Markodc151b22015-10-15 18:02:30 +010083
84 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
85 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
86 uint64_t method_load_data = 0u;
87 uint64_t direct_code_ptr = 0u;
88
89 HGraph* outer_graph = codegen_->GetGraph();
90 if (target_method.dex_file == &outer_graph->GetDexFile() &&
91 target_method.dex_method_index == outer_graph->GetMethodIdx()) {
92 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
93 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
94 } else {
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000095 bool use_pc_relative_instructions =
96 ((direct_method == 0u || direct_code == static_cast<uintptr_t>(-1))) &&
97 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), target_method.dex_file);
Vladimir Markodc151b22015-10-15 18:02:30 +010098 if (direct_method != 0u) { // Should we use a direct pointer to the method?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +000099 // Note: For JIT, kDirectAddressWithFixup doesn't make sense at all and while
100 // kDirectAddress would be fine for image methods, we don't support it at the moment.
101 DCHECK(!Runtime::Current()->UseJit());
Vladimir Markodc151b22015-10-15 18:02:30 +0100102 if (direct_method != static_cast<uintptr_t>(-1)) { // Is the method pointer known now?
103 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
104 method_load_data = direct_method;
105 } else { // The direct pointer will be known at link time.
106 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddressWithFixup;
107 }
108 } else { // Use dex cache.
109 DCHECK_EQ(target_method.dex_file, &graph_->GetDexFile());
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000110 if (use_pc_relative_instructions) { // Can we use PC-relative access to the dex cache arrays?
111 DCHECK(!Runtime::Current()->UseJit());
Vladimir Markodc151b22015-10-15 18:02:30 +0100112 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000113 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
114 &graph_->GetDexFile());
Vladimir Markodc151b22015-10-15 18:02:30 +0100115 method_load_data = layout.MethodOffset(target_method.dex_method_index);
116 } else { // We must go through the ArtMethod's pointer to resolved methods.
117 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCacheViaMethod;
118 }
119 }
120 if (direct_code != 0u) { // Should we use a direct pointer to the code?
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000121 // Note: For JIT, kCallPCRelative and kCallDirectWithFixup don't make sense at all and
122 // while kCallDirect would be fine for image methods, we don't support it at the moment.
123 DCHECK(!Runtime::Current()->UseJit());
Vladimir Markodc151b22015-10-15 18:02:30 +0100124 if (direct_code != static_cast<uintptr_t>(-1)) { // Is the code pointer known now?
125 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirect;
126 direct_code_ptr = direct_code;
Vladimir Markod1eaf0d2015-10-29 12:18:29 +0000127 } else if (use_pc_relative_instructions) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100128 // Use PC-relative calls for invokes within a multi-dex oat file.
Vladimir Markodc151b22015-10-15 18:02:30 +0100129 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallPCRelative;
130 } else { // The direct pointer will be known at link time.
131 // NOTE: This is used for app->boot calls when compiling an app against
132 // a relocatable but not yet relocated image.
133 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallDirectWithFixup;
134 }
135 } else { // We must use the code pointer from the ArtMethod.
136 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
137 }
138 }
139
140 if (graph_->IsDebuggable()) {
141 // For debuggable apps always use the code pointer from ArtMethod
142 // so that we don't circumvent instrumentation stubs if installed.
143 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
144 }
145
146 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
147 method_load_kind, code_ptr_location, method_load_data, direct_code_ptr
148 };
149 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
150 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info,
151 invoke->GetTargetMethod());
152 invoke->SetDispatchInfo(dispatch_info);
153}
154
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000155void HSharpening::ProcessLoadString(HLoadString* load_string) {
156 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
157 DCHECK(!load_string->IsInDexCache());
158
159 const DexFile& dex_file = load_string->GetDexFile();
160 uint32_t string_index = load_string->GetStringIndex();
161
162 bool is_in_dex_cache = false;
163 HLoadString::LoadKind desired_load_kind;
164 uint64_t address = 0u; // String or dex cache element address.
165 {
166 Runtime* runtime = Runtime::Current();
167 ClassLinker* class_linker = runtime->GetClassLinker();
168 ScopedObjectAccess soa(Thread::Current());
169 StackHandleScope<1> hs(soa.Self());
170 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
171 ? compilation_unit_.GetDexCache()
172 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
173
174 if (compiler_driver_->IsBootImage()) {
175 // Compiling boot image. Resolve the string and allocate it if needed.
176 DCHECK(!runtime->UseJit());
177 mirror::String* string = class_linker->ResolveString(dex_file, string_index, dex_cache);
178 CHECK(string != nullptr);
179 if (!compiler_driver_->GetSupportBootImageFixup()) {
180 // MIPS/MIPS64 or compiler_driver_test. Do not sharpen.
181 desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
182 } else {
183 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(),
184 &load_string->GetDexFile()));
185 is_in_dex_cache = true;
186 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
187 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
188 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
189 }
190 } else {
191 // Not compiling boot image. Try to lookup the string without allocating if not found.
192 mirror::String* string = class_linker->LookupString(dex_file, string_index, dex_cache);
193 if (runtime->UseJit()) {
194 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
195 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
196 is_in_dex_cache = (string != nullptr);
197 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
198 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
199 // Convert to uintptr_t first to avoid sign-extension if a 32-bit pointer is "signed."
200 address = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(string));
201 } else {
202 // Note: If the string is not in the dex cache, the instruction needs environment
203 // and will not be inlined across dex files. Within a dex file, the slow-path helper
204 // loads the correct string and inlined frames are used correctly for OOM stack trace.
205 // TODO: Write a test for this.
206 desired_load_kind = HLoadString::LoadKind::kDexCacheAddress;
207 void* dex_cache_element_address = &dex_cache->GetStrings()[string_index];
208 // Convert to uintptr_t first to avoid sign-extension if a 32-bit pointer is "signed."
209 address = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(dex_cache_element_address));
210 }
211 } else if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
212 if (codegen_->GetCompilerOptions().GetCompilePic()) {
213 // Use PC-relative load from the dex cache if the dex file belongs
214 // to the oat file that we're currently compiling.
215 desired_load_kind =
216 ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &load_string->GetDexFile())
217 ? HLoadString::LoadKind::kDexCachePcRelative
218 : HLoadString::LoadKind::kDexCacheViaMethod;
219 } else {
220 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
221 // Convert to uintptr_t first to avoid sign-extension if a 32-bit pointer is "signed."
222 address = static_cast<uint64_t>(reinterpret_cast<uintptr_t>(string));
223 }
224 } else {
225 // Not JIT and the string is not in boot image.
226 desired_load_kind = HLoadString::LoadKind::kDexCachePcRelative;
227 }
228 }
229 }
230 if (is_in_dex_cache) {
231 load_string->MarkInDexCache();
232 }
233
234 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
235 switch (load_kind) {
236 case HLoadString::LoadKind::kBootImageLinkTimeAddress:
237 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
238 case HLoadString::LoadKind::kDexCacheViaMethod:
239 load_string->SetLoadKindWithStringReference(load_kind, dex_file, string_index);
240 break;
241 case HLoadString::LoadKind::kBootImageAddress:
242 case HLoadString::LoadKind::kDexCacheAddress:
243 DCHECK_NE(address, 0u);
244 load_string->SetLoadKindWithAddress(load_kind, address);
245 break;
246 case HLoadString::LoadKind::kDexCachePcRelative: {
247 size_t pointer_size = InstructionSetPointerSize(codegen_->GetInstructionSet());
248 DexCacheArraysLayout layout(pointer_size, &dex_file);
249 size_t element_index = layout.StringOffset(string_index);
250 load_string->SetLoadKindWithDexCacheReference(load_kind, dex_file, element_index);
251 break;
252 }
253 }
254}
255
Vladimir Markodc151b22015-10-15 18:02:30 +0100256} // namespace art