blob: 8f1827be696cfe3e201123a95b6b90b90ea20bea [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()) {
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000044 SharpenInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect(), codegen_);
Vladimir Markocac5a7e2016-02-22 10:39:50 +000045 } else if (instruction->IsLoadString()) {
46 ProcessLoadString(instruction->AsLoadString());
Vladimir Markodc151b22015-10-15 18:02:30 +010047 }
48 // TODO: Move the sharpening of invoke-virtual/-interface/-super from HGraphBuilder
49 // here. Rewrite it to avoid the CompilerDriver's reliance on verifier data
50 // because we know the type better when inlining.
Vladimir Markodc151b22015-10-15 18:02:30 +010051 }
52 }
53}
54
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000055static bool IsInBootImage(ArtMethod* method) {
56 const std::vector<gc::space::ImageSpace*>& image_spaces =
57 Runtime::Current()->GetHeap()->GetBootImageSpaces();
58 for (gc::space::ImageSpace* image_space : image_spaces) {
59 const auto& method_section = image_space->GetImageHeader().GetMethodsSection();
60 if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
61 return true;
62 }
63 }
64 return false;
65}
66
67static bool AOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& options) {
68 // Including patch information means the AOT code will be patched, which we don't
69 // support in the compiler, and is anyways moving away b/33192586.
70 return IsInBootImage(method) && !options.GetCompilePic() && !options.GetIncludePatchInformation();
71}
72
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +000073
74void HSharpening::SharpenInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke,
75 CodeGenerator* codegen) {
Vladimir Markodc151b22015-10-15 18:02:30 +010076 if (invoke->IsStringInit()) {
77 // Not using the dex cache arrays. But we could still try to use a better dispatch...
78 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
79 return;
80 }
81
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000082 ArtMethod* callee = invoke->GetResolvedMethod();
83 DCHECK(callee != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010084
85 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
86 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
87 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010088
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000089 // Note: we never call an ArtMethod through a known code pointer, as
90 // we do not want to keep on invoking it if it gets deoptimized. This
91 // applies to both AOT and JIT.
92 // This also avoids having to find out if the code pointer of an ArtMethod
93 // is the resolution trampoline (for ensuring the class is initialized), or
94 // the interpreter entrypoint. Such code pointers we do not want to call
95 // directly.
96 // Only in the case of a recursive call can we call directly, as we know the
97 // class is initialized already or being initialized, and the call will not
98 // be invoked once the method is deoptimized.
99
Alex Light1ebe4fe2017-01-30 14:57:11 -0800100 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
101 // situations.
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000102 if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000103 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +0100104 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
105 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000106 } else if (Runtime::Current()->UseJitCompilation() ||
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000107 AOTCanEmbedMethod(callee, codegen->GetCompilerOptions())) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000108 // JIT or on-device AOT compilation referencing a boot image method.
109 // Use the method address directly.
110 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
111 method_load_data = reinterpret_cast<uintptr_t>(callee);
112 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100113 } else {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000114 // Use PC-relative access to the dex cache arrays.
115 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000116 // Note: we use the invoke's graph instead of the codegen graph, which are
117 // different when inlining (the codegen graph is the most outer graph). The
118 // invoke's dex method index is relative to the dex file where the invoke's graph
119 // was built from.
120 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen->GetInstructionSet()),
121 &invoke->GetBlock()->GetGraph()->GetDexFile());
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000122 method_load_data = layout.MethodOffset(invoke->GetDexMethodIndex());
123 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100124 }
125
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000126 if (codegen->GetGraph()->IsDebuggable()) {
Vladimir Markodc151b22015-10-15 18:02:30 +0100127 // For debuggable apps always use the code pointer from ArtMethod
128 // so that we don't circumvent instrumentation stubs if installed.
129 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
130 }
131
132 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000133 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100134 };
135 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000136 codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100137 invoke->SetDispatchInfo(dispatch_info);
138}
139
Nicolas Geoffrayc4aa82c2017-03-06 14:38:52 +0000140HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(HLoadClass* load_class,
141 CodeGenerator* codegen,
142 CompilerDriver* compiler_driver,
143 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000144 Handle<mirror::Class> klass = load_class->GetClass();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700145 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
146 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
147 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700148 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100149
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000150 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
151
Vladimir Marko41559982017-01-06 14:04:23 +0000152 if (load_class->NeedsAccessCheck()) {
153 // 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 +0000154 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000155 // Loading from the ArtMethod* is the most efficient retrieval in code size.
156 // TODO: This may not actually be true for all architectures and
157 // locations of target classes. The additional register pressure
158 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000159 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000160 const DexFile& dex_file = load_class->GetDexFile();
161 dex::TypeIndex type_index = load_class->GetTypeIndex();
162
163 bool is_in_boot_image = false;
164 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
165 Runtime* runtime = Runtime::Current();
166 if (codegen->GetCompilerOptions().IsBootImage()) {
167 // Compiling boot image. Check if the class is a boot image class.
168 DCHECK(!runtime->UseJitCompilation());
169 if (!compiler_driver->GetSupportBootImageFixup()) {
170 // compiler_driver_test. Do not sharpen.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000171 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800172 } else if ((klass != nullptr) && compiler_driver->IsImageClass(
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000173 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
174 is_in_boot_image = true;
175 desired_load_kind = codegen->GetCompilerOptions().GetCompilePic()
176 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
177 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
178 } else {
179 // Not a boot image class.
180 DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
181 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100182 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000183 } else {
Andreas Gampefa4333d2017-02-14 11:10:34 -0800184 is_in_boot_image = (klass != nullptr) &&
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000185 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
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 if (is_in_boot_image) {
190 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
191 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
Andreas Gampefa4333d2017-02-14 11:10:34 -0800192 } else if (klass != nullptr) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000193 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
194 } else {
195 // Class not loaded yet. This happens when the dex code requesting
196 // this `HLoadClass` hasn't been executed in the interpreter.
197 // Fallback to the dex cache.
198 // TODO(ngeoffray): Generate HDeoptimize instead.
199 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
200 }
201 } else if (is_in_boot_image && !codegen->GetCompilerOptions().GetCompilePic()) {
202 // AOT app compilation. Check if the class is in the boot image.
203 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
204 } else {
205 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
206 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
207 }
208 }
209 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
210
211 if (is_in_boot_image) {
212 load_class->MarkInBootImage();
213 }
214 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
215 }
216
217 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
218 if ((load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) ||
219 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
220 // We actually cannot reference this class, we're forced to bail.
221 // We cannot reference this class with Bss, as the entrypoint will lookup the class
222 // in the caller's dex file, but that dex file does not reference the class.
223 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100224 }
225 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000226 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100227}
228
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000229void HSharpening::ProcessLoadString(HLoadString* load_string) {
230 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000231
232 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800233 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000234
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000235 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000236 {
237 Runtime* runtime = Runtime::Current();
238 ClassLinker* class_linker = runtime->GetClassLinker();
239 ScopedObjectAccess soa(Thread::Current());
240 StackHandleScope<1> hs(soa.Self());
241 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
242 ? compilation_unit_.GetDexCache()
243 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000244 mirror::String* string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000245
Vladimir Markoaad75c62016-10-03 08:46:48 +0000246 if (codegen_->GetCompilerOptions().IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000247 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
248 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100249 DCHECK(!runtime->UseJitCompilation());
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000250 string = class_linker->ResolveString(dex_file, string_index, dex_cache);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000251 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000252 if (compiler_driver_->GetSupportBootImageFixup()) {
253 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
254 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
255 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
256 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
257 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000258 // compiler_driver_test. Do not sharpen.
259 desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Marko95026872016-09-09 09:16:31 +0000260 }
Calin Juravleffc87072016-04-20 14:22:09 +0100261 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100262 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700263 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000264 string = class_linker->LookupString(dex_file, string_index, dex_cache);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000265 if (string != nullptr) {
266 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
267 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000268 } else {
269 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
270 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000271 } else {
272 desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100273 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000274 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100275 // AOT app compilation. Try to lookup the string without allocating if not found.
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000276 string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100277 if (string != nullptr &&
278 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
279 !codegen_->GetCompilerOptions().GetCompilePic()) {
280 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000281 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000282 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000283 }
284 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000285 if (string != nullptr) {
286 load_string->SetString(handles_->NewHandle(string));
287 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000288 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000289 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000290
291 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000292 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000293}
294
Vladimir Markodc151b22015-10-15 18:02:30 +0100295} // namespace art