blob: e46c9a7081873a3efce8ad69652e6123dfd3ad34 [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 Marko65979462017-05-19 17:25:12 +010019#include "art_method-inl.h"
Vladimir Markodb8e62d2016-03-30 16:30:21 +010020#include "base/casts.h"
Andreas Gampe542451c2016-07-26 09:02:02 -070021#include "base/enums.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000022#include "class_linker.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010023#include "code_generator.h"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070024#include "driver/compiler_driver.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "driver/compiler_options.h"
Vladimir Markocac5a7e2016-02-22 10:39:50 +000026#include "driver/dex_compilation_unit.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"
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070035#include "utils/dex_cache_arrays_layout-inl.h"
Vladimir Markodc151b22015-10-15 18:02:30 +010036
37namespace art {
38
39void HSharpening::Run() {
40 // We don't care about the order of the blocks here.
41 for (HBasicBlock* block : graph_->GetReversePostOrder()) {
42 for (HInstructionIterator it(block->GetInstructions()); !it.Done(); it.Advance()) {
43 HInstruction* instruction = it.Current();
44 if (instruction->IsInvokeStaticOrDirect()) {
Vladimir Marko65979462017-05-19 17:25:12 +010045 SharpenInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect(),
46 codegen_,
47 compiler_driver_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000048 } else if (instruction->IsLoadString()) {
49 ProcessLoadString(instruction->AsLoadString());
Vladimir Markodc151b22015-10-15 18:02:30 +010050 }
51 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
52 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
53 // because we know the type better when inlining.
Vladimir Markodc151b22015-10-15 18:02:30 +010054 }
55 }
56}
57
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000058static bool IsInBootImage(ArtMethod* method) {
59 const std::vector<gc::space::ImageSpace*>& image_spaces =
60 Runtime::Current()->GetHeap()->GetBootImageSpaces();
61 for (gc::space::ImageSpace* image_space : image_spaces) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +010062 const ImageSection& method_section = image_space->GetImageHeader().GetMethodsSection();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000063 if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
64 return true;
65 }
66 }
67 return false;
68}
69
70static bool AOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& options) {
Richard Uhlerc52f3032017-03-02 13:45:45 +000071 return IsInBootImage(method) && !options.GetCompilePic();
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000072}
73
Vladimir Marko65979462017-05-19 17:25:12 +010074static bool BootImageAOTCanEmbedMethod(ArtMethod* method, CompilerDriver* compiler_driver) {
75 DCHECK(compiler_driver->GetCompilerOptions().IsBootImage());
76 if (!compiler_driver->GetSupportBootImageFixup()) {
77 return false;
78 }
79 ScopedObjectAccess soa(Thread::Current());
80 ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
81 DCHECK(klass != nullptr);
82 const DexFile& dex_file = klass->GetDexFile();
83 return compiler_driver->IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
84}
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000085
86void HSharpening::SharpenInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke,
Vladimir Marko65979462017-05-19 17:25:12 +010087 CodeGenerator* codegen,
88 CompilerDriver* compiler_driver) {
Vladimir Markodc151b22015-10-15 18:02:30 +010089 if (invoke->IsStringInit()) {
90 // Not using the dex cache arrays. But we could still try to use a better dispatch...
91 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
92 return;
93 }
94
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000095 ArtMethod* callee = invoke->GetResolvedMethod();
96 DCHECK(callee != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010097
98 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
99 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
100 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +0100101
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000102 // Note: we never call an ArtMethod through a known code pointer, as
103 // we do not want to keep on invoking it if it gets deoptimized. This
104 // applies to both AOT and JIT.
105 // This also avoids having to find out if the code pointer of an ArtMethod
106 // is the resolution trampoline (for ensuring the class is initialized), or
107 // the interpreter entrypoint. Such code pointers we do not want to call
108 // directly.
109 // Only in the case of a recursive call can we call directly, as we know the
110 // class is initialized already or being initialized, and the call will not
111 // be invoked once the method is deoptimized.
112
Alex Light1ebe4fe2017-01-30 14:57:11 -0800113 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
114 // situations.
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000115 if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000116 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +0100117 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
118 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000119 } else if (Runtime::Current()->UseJitCompilation() ||
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000120 AOTCanEmbedMethod(callee, codegen->GetCompilerOptions())) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000121 // JIT or on-device AOT compilation referencing a boot image method.
122 // Use the method address directly.
123 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
124 method_load_data = reinterpret_cast<uintptr_t>(callee);
125 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Marko65979462017-05-19 17:25:12 +0100126 } else if (codegen->GetCompilerOptions().IsBootImage() &&
127 BootImageAOTCanEmbedMethod(callee, compiler_driver)) {
128 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative;
129 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100130 } else {
Vladimir Marko0eb882b2017-05-15 13:39:18 +0100131 // Use PC-relative access to the .bss methods arrays.
132 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000133 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100134 }
135
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000136 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100137 // For debuggable apps always use the code pointer from ArtMethod
138 // so that we don't circumvent instrumentation stubs if installed.
139 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
140 }
141
142 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000143 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100144 };
145 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000146 codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100147 invoke->SetDispatchInfo(dispatch_info);
148}
149
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000150HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(HLoadClass* load_class,
151 CodeGenerator* codegen,
152 CompilerDriver* compiler_driver,
153 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000154 Handle<mirror::Class> klass = load_class->GetClass();
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100155 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700156 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
157 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700158 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100159
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000160 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
161
Vladimir Marko41559982017-01-06 14:04:23 +0000162 if (load_class->NeedsAccessCheck()) {
163 // We need to call the runtime anyway, so we simply get the class as that call's return value.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000164 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000165 // Loading from the ArtMethod* is the most efficient retrieval in code size.
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.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000169 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000170 const DexFile& dex_file = load_class->GetDexFile();
171 dex::TypeIndex type_index = load_class->GetTypeIndex();
172
173 bool is_in_boot_image = false;
174 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
175 Runtime* runtime = Runtime::Current();
176 if (codegen->GetCompilerOptions().IsBootImage()) {
177 // Compiling boot image. Check if the class is a boot image class.
178 DCHECK(!runtime->UseJitCompilation());
179 if (!compiler_driver->GetSupportBootImageFixup()) {
180 // compiler_driver_test. Do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100181 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Vladimir Marko65979462017-05-19 17:25:12 +0100182 } else if ((klass != nullptr) &&
183 compiler_driver->IsImageClass(dex_file.StringByTypeIdx(type_index))) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000184 is_in_boot_image = true;
Vladimir Marko764d4542017-05-16 10:31:41 +0100185 desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000186 } else {
187 // Not a boot image class.
188 DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
189 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100190 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000191 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800192 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000193 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
194 if (runtime->UseJitCompilation()) {
Vladimir Marko764d4542017-05-16 10:31:41 +0100195 DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000196 if (is_in_boot_image) {
197 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
198 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800199 } else if (klass != nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000200 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
201 } else {
202 // Class not loaded yet. This happens when the dex code requesting
203 // this `HLoadClass` hasn't been executed in the interpreter.
204 // Fallback to the dex cache.
205 // TODO(ngeoffray): Generate HDeoptimize instead.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100206 desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000207 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100208 } else if (is_in_boot_image) {
209 // AOT app compilation, boot image class.
210 if (codegen->GetCompilerOptions().GetCompilePic()) {
211 desired_load_kind = HLoadClass::LoadKind::kBootImageClassTable;
212 } else {
213 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
214 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000215 } else {
Vladimir Marko94ec2db2017-09-06 17:21:03 +0100216 // Not JIT and the klass is not in boot image.
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000217 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
218 }
219 }
220 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
221
222 if (is_in_boot_image) {
223 load_class->MarkInBootImage();
224 }
225 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
226 }
227
228 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100229 if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000230 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
231 // We actually cannot reference this class, we're forced to bail.
232 // We cannot reference this class with Bss, as the entrypoint will lookup the class
233 // in the caller's dex file, but that dex file does not reference the class.
234 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100235 }
236 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000237 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100238}
239
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000240void HSharpening::ProcessLoadString(HLoadString* load_string) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100241 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000242
243 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800244 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000245
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000246 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000247 {
248 Runtime* runtime = Runtime::Current();
249 ClassLinker* class_linker = runtime->GetClassLinker();
250 ScopedObjectAccess soa(Thread::Current());
251 StackHandleScope<1> hs(soa.Self());
252 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
253 ? compilation_unit_.GetDexCache()
254 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000255 mirror::String* string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000256
Vladimir Markoaad75c62016-10-03 08:46:48 +0000257 if (codegen_->GetCompilerOptions().IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000258 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
259 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100260 DCHECK(!runtime->UseJitCompilation());
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000261 string = class_linker->ResolveString(dex_file, string_index, dex_cache);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000262 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000263 if (compiler_driver_->GetSupportBootImageFixup()) {
264 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
Vladimir Marko764d4542017-05-16 10:31:41 +0100265 desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
Vladimir Marko95026872016-09-09 09:16:31 +0000266 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000267 // compiler_driver_test. Do not sharpen.
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100268 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Marko95026872016-09-09 09:16:31 +0000269 }
Calin Juravleffc87072016-04-20 14:22:09 +0100270 } else if (runtime->UseJitCompilation()) {
Vladimir Marko764d4542017-05-16 10:31:41 +0100271 DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Vladimir Markof25cc732017-03-16 16:18:15 +0000272 string = class_linker->LookupString(dex_file, string_index, dex_cache.Get());
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000273 if (string != nullptr) {
274 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
275 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000276 } else {
277 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
278 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000279 } else {
Vladimir Marko847e6ce2017-06-02 13:55:07 +0100280 desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100281 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000282 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100283 // AOT app compilation. Try to lookup the string without allocating if not found.
Vladimir Markof25cc732017-03-16 16:18:15 +0000284 string = class_linker->LookupString(dex_file, string_index, dex_cache.Get());
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100285 if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
286 if (codegen_->GetCompilerOptions().GetCompilePic()) {
287 desired_load_kind = HLoadString::LoadKind::kBootImageInternTable;
288 } else {
289 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
290 }
Vladimir Markoaad75c62016-10-03 08:46:48 +0000291 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000292 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000293 }
294 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000295 if (string != nullptr) {
296 load_string->SetString(handles_->NewHandle(string));
297 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000298 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000299 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000300
301 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000302 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000303}
304
Vladimir Markodc151b22015-10-15 18:02:30 +0100305} // namespace art