blob: f07f02a719dc6968e1ab7dd5d4a0dd7eee150310 [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 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
Vladimir Markodc151b22015-10-15 18:02:30 +010073void HSharpening::ProcessInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
74 if (invoke->IsStringInit()) {
75 // Not using the dex cache arrays. But we could still try to use a better dispatch...
76 // TODO: Use direct_method and direct_code for the appropriate StringFactory method.
77 return;
78 }
79
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000080 ArtMethod* callee = invoke->GetResolvedMethod();
81 DCHECK(callee != nullptr);
Vladimir Markodc151b22015-10-15 18:02:30 +010082
83 HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
84 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
85 uint64_t method_load_data = 0u;
Vladimir Markodc151b22015-10-15 18:02:30 +010086
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +000087 // Note: we never call an ArtMethod through a known code pointer, as
88 // we do not want to keep on invoking it if it gets deoptimized. This
89 // applies to both AOT and JIT.
90 // This also avoids having to find out if the code pointer of an ArtMethod
91 // is the resolution trampoline (for ensuring the class is initialized), or
92 // the interpreter entrypoint. Such code pointers we do not want to call
93 // directly.
94 // Only in the case of a recursive call can we call directly, as we know the
95 // class is initialized already or being initialized, and the call will not
96 // be invoked once the method is deoptimized.
97
Alex Light1ebe4fe2017-01-30 14:57:11 -080098 // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
99 // situations.
100 if (callee == codegen_->GetGraph()->GetArtMethod() && !codegen_->GetGraph()->IsDebuggable()) {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000101 // Recursive call.
Vladimir Markodc151b22015-10-15 18:02:30 +0100102 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
103 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000104 } else if (Runtime::Current()->UseJitCompilation() ||
105 AOTCanEmbedMethod(callee, codegen_->GetCompilerOptions())) {
106 // JIT or on-device AOT compilation referencing a boot image method.
107 // Use the method address directly.
108 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress;
109 method_load_data = reinterpret_cast<uintptr_t>(callee);
110 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100111 } else {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000112 // Use PC-relative access to the dex cache arrays.
113 method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
114 DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen_->GetInstructionSet()),
115 &graph_->GetDexFile());
116 method_load_data = layout.MethodOffset(invoke->GetDexMethodIndex());
117 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
Vladimir Markodc151b22015-10-15 18:02:30 +0100118 }
119
120 if (graph_->IsDebuggable()) {
121 // For debuggable apps always use the code pointer from ArtMethod
122 // so that we don't circumvent instrumentation stubs if installed.
123 code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
124 }
125
126 HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
Nicolas Geoffrayc1a42cf2016-12-18 15:52:36 +0000127 method_load_kind, code_ptr_location, method_load_data
Vladimir Markodc151b22015-10-15 18:02:30 +0100128 };
129 HInvokeStaticOrDirect::DispatchInfo dispatch_info =
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +0100130 codegen_->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
Vladimir Markodc151b22015-10-15 18:02:30 +0100131 invoke->SetDispatchInfo(dispatch_info);
132}
133
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000134HLoadClass::LoadKind HSharpening::SharpenClass(HLoadClass* load_class,
135 CodeGenerator* codegen,
136 CompilerDriver* compiler_driver,
137 const DexCompilationUnit& dex_compilation_unit) {
Nicolas Geoffray5247c082017-01-13 14:17:29 +0000138 Handle<mirror::Class> klass = load_class->GetClass();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700139 DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
140 load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
141 << load_class->GetLoadKind();
Mathieu Chartier4a4a6012016-09-16 14:16:42 -0700142 DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100143
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000144 HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
145
Vladimir Marko41559982017-01-06 14:04:23 +0000146 if (load_class->NeedsAccessCheck()) {
147 // 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 +0000148 } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
Vladimir Marko41559982017-01-06 14:04:23 +0000149 // Loading from the ArtMethod* is the most efficient retrieval in code size.
150 // TODO: This may not actually be true for all architectures and
151 // locations of target classes. The additional register pressure
152 // for using the ArtMethod* should be considered.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000153 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000154 const DexFile& dex_file = load_class->GetDexFile();
155 dex::TypeIndex type_index = load_class->GetTypeIndex();
156
157 bool is_in_boot_image = false;
158 HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
159 Runtime* runtime = Runtime::Current();
160 if (codegen->GetCompilerOptions().IsBootImage()) {
161 // Compiling boot image. Check if the class is a boot image class.
162 DCHECK(!runtime->UseJitCompilation());
163 if (!compiler_driver->GetSupportBootImageFixup()) {
164 // compiler_driver_test. Do not sharpen.
Nicolas Geoffray56876342016-12-16 16:09:08 +0000165 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000166 } else if ((klass.Get() != nullptr) && compiler_driver->IsImageClass(
167 dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
168 is_in_boot_image = true;
169 desired_load_kind = codegen->GetCompilerOptions().GetCompilePic()
170 ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
171 : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
172 } else {
173 // Not a boot image class.
174 DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
175 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100176 }
Nicolas Geoffray56876342016-12-16 16:09:08 +0000177 } else {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000178 is_in_boot_image = (klass.Get() != nullptr) &&
179 runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
180 if (runtime->UseJitCompilation()) {
181 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
182 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
183 if (is_in_boot_image) {
184 // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
185 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
186 } else if (klass.Get() != nullptr) {
187 desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
188 } else {
189 // Class not loaded yet. This happens when the dex code requesting
190 // this `HLoadClass` hasn't been executed in the interpreter.
191 // Fallback to the dex cache.
192 // TODO(ngeoffray): Generate HDeoptimize instead.
193 desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
194 }
195 } else if (is_in_boot_image && !codegen->GetCompilerOptions().GetCompilePic()) {
196 // AOT app compilation. Check if the class is in the boot image.
197 desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
198 } else {
199 // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
200 desired_load_kind = HLoadClass::LoadKind::kBssEntry;
201 }
202 }
203 DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
204
205 if (is_in_boot_image) {
206 load_class->MarkInBootImage();
207 }
208 load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
209 }
210
211 if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
212 if ((load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) ||
213 (load_kind == HLoadClass::LoadKind::kBssEntry)) {
214 // We actually cannot reference this class, we're forced to bail.
215 // We cannot reference this class with Bss, as the entrypoint will lookup the class
216 // in the caller's dex file, but that dex file does not reference the class.
217 return HLoadClass::LoadKind::kInvalid;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100218 }
219 }
Nicolas Geoffray83c8e272017-01-31 14:36:37 +0000220 return load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100221}
222
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000223void HSharpening::ProcessLoadString(HLoadString* load_string) {
224 DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000225
226 const DexFile& dex_file = load_string->GetDexFile();
Andreas Gampe8a0128a2016-11-28 07:38:35 -0800227 dex::StringIndex string_index = load_string->GetStringIndex();
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000228
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000229 HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000230 {
231 Runtime* runtime = Runtime::Current();
232 ClassLinker* class_linker = runtime->GetClassLinker();
233 ScopedObjectAccess soa(Thread::Current());
234 StackHandleScope<1> hs(soa.Self());
235 Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
236 ? compilation_unit_.GetDexCache()
237 : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000238 mirror::String* string = nullptr;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000239
Vladimir Markoaad75c62016-10-03 08:46:48 +0000240 if (codegen_->GetCompilerOptions().IsBootImage()) {
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000241 // Compiling boot image. Resolve the string and allocate it if needed, to ensure
242 // the string will be added to the boot image.
Calin Juravleffc87072016-04-20 14:22:09 +0100243 DCHECK(!runtime->UseJitCompilation());
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000244 string = class_linker->ResolveString(dex_file, string_index, dex_cache);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000245 CHECK(string != nullptr);
Vladimir Marko95026872016-09-09 09:16:31 +0000246 if (compiler_driver_->GetSupportBootImageFixup()) {
247 DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
248 desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
249 ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
250 : HLoadString::LoadKind::kBootImageLinkTimeAddress;
251 } else {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000252 // compiler_driver_test. Do not sharpen.
253 desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Marko95026872016-09-09 09:16:31 +0000254 }
Calin Juravleffc87072016-04-20 14:22:09 +0100255 } else if (runtime->UseJitCompilation()) {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100256 // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
Christina Wadsworth5a5d0fa2016-08-19 14:38:01 -0700257 // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000258 string = class_linker->LookupString(dex_file, string_index, dex_cache);
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000259 if (string != nullptr) {
260 if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
261 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
Nicolas Geoffray132d8362016-11-16 09:19:42 +0000262 } else {
263 desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
264 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000265 } else {
266 desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100267 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000268 } else {
Vladimir Markodb8e62d2016-03-30 16:30:21 +0100269 // AOT app compilation. Try to lookup the string without allocating if not found.
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000270 string = class_linker->LookupString(dex_file, string_index, dex_cache);
Vladimir Markodbb7f5b2016-03-30 13:23:58 +0100271 if (string != nullptr &&
272 runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
273 !codegen_->GetCompilerOptions().GetCompilePic()) {
274 desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
Vladimir Markoaad75c62016-10-03 08:46:48 +0000275 } else {
Vladimir Marko1bc4b172016-10-24 16:53:39 +0000276 desired_load_kind = HLoadString::LoadKind::kBssEntry;
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000277 }
278 }
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000279 if (string != nullptr) {
280 load_string->SetString(handles_->NewHandle(string));
281 }
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000282 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000283 DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000284
285 HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +0000286 load_string->SetLoadKind(load_kind);
Vladimir Markocac5a7e2016-02-22 10:39:50 +0000287}
288
Vladimir Markodc151b22015-10-15 18:02:30 +0100289} // namespace art