1 /*
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 
19 #include "base/casts.h"
20 #include "base/enums.h"
21 #include "class_linker.h"
22 #include "code_generator.h"
23 #include "driver/compiler_options.h"
24 #include "driver/dex_compilation_unit.h"
25 #include "utils/dex_cache_arrays_layout-inl.h"
26 #include "driver/compiler_driver.h"
27 #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"
32 #include "nodes.h"
33 #include "runtime.h"
34 #include "scoped_thread_state_change-inl.h"
35 
36 namespace art {
37 
Run()38 void 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         SharpenInvokeStaticOrDirect(instruction->AsInvokeStaticOrDirect(), codegen_);
45       } else if (instruction->IsLoadString()) {
46         ProcessLoadString(instruction->AsLoadString());
47       }
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.
51     }
52   }
53 }
54 
IsInBootImage(ArtMethod * method)55 static 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 
AOTCanEmbedMethod(ArtMethod * method,const CompilerOptions & options)67 static bool AOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& options) {
68   return IsInBootImage(method) && !options.GetCompilePic();
69 }
70 
71 
SharpenInvokeStaticOrDirect(HInvokeStaticOrDirect * invoke,CodeGenerator * codegen)72 void HSharpening::SharpenInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke,
73                                               CodeGenerator* codegen) {
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 
80   ArtMethod* callee = invoke->GetResolvedMethod();
81   DCHECK(callee != nullptr);
82 
83   HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
84   HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
85   uint64_t method_load_data = 0u;
86 
87   // 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 
98   // 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()) {
101     // Recursive call.
102     method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
103     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
104   } 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;
111   } else {
112     // Use PC-relative access to the dex cache arrays.
113     method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kDexCachePcRelative;
114     // Note: we use the invoke's graph instead of the codegen graph, which are
115     // different when inlining (the codegen graph is the most outer graph). The
116     // invoke's dex method index is relative to the dex file where the invoke's graph
117     // was built from.
118     DexCacheArraysLayout layout(GetInstructionSetPointerSize(codegen->GetInstructionSet()),
119                                 &invoke->GetBlock()->GetGraph()->GetDexFile());
120     method_load_data = layout.MethodOffset(invoke->GetDexMethodIndex());
121     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
122   }
123 
124   if (codegen->GetGraph()->IsDebuggable()) {
125     // For debuggable apps always use the code pointer from ArtMethod
126     // so that we don't circumvent instrumentation stubs if installed.
127     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
128   }
129 
130   HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
131       method_load_kind, code_ptr_location, method_load_data
132   };
133   HInvokeStaticOrDirect::DispatchInfo dispatch_info =
134       codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, invoke);
135   invoke->SetDispatchInfo(dispatch_info);
136 }
137 
ComputeLoadClassKind(HLoadClass * load_class,CodeGenerator * codegen,CompilerDriver * compiler_driver,const DexCompilationUnit & dex_compilation_unit)138 HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(HLoadClass* load_class,
139                                                        CodeGenerator* codegen,
140                                                        CompilerDriver* compiler_driver,
141                                                        const DexCompilationUnit& dex_compilation_unit) {
142   Handle<mirror::Class> klass = load_class->GetClass();
143   DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kDexCacheViaMethod ||
144          load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
145       << load_class->GetLoadKind();
146   DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
147 
148   HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
149 
150   if (load_class->NeedsAccessCheck()) {
151     // We need to call the runtime anyway, so we simply get the class as that call's return value.
152   } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
153     // Loading from the ArtMethod* is the most efficient retrieval in code size.
154     // TODO: This may not actually be true for all architectures and
155     // locations of target classes. The additional register pressure
156     // for using the ArtMethod* should be considered.
157   } else {
158     const DexFile& dex_file = load_class->GetDexFile();
159     dex::TypeIndex type_index = load_class->GetTypeIndex();
160 
161     bool is_in_boot_image = false;
162     HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
163     Runtime* runtime = Runtime::Current();
164     if (codegen->GetCompilerOptions().IsBootImage()) {
165       // Compiling boot image. Check if the class is a boot image class.
166       DCHECK(!runtime->UseJitCompilation());
167       if (!compiler_driver->GetSupportBootImageFixup()) {
168         // compiler_driver_test. Do not sharpen.
169         desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
170       } else if ((klass != nullptr) && compiler_driver->IsImageClass(
171           dex_file.StringDataByIdx(dex_file.GetTypeId(type_index).descriptor_idx_))) {
172         is_in_boot_image = true;
173         desired_load_kind = codegen->GetCompilerOptions().GetCompilePic()
174             ? HLoadClass::LoadKind::kBootImageLinkTimePcRelative
175             : HLoadClass::LoadKind::kBootImageLinkTimeAddress;
176       } else {
177         // Not a boot image class.
178         DCHECK(ContainsElement(compiler_driver->GetDexFilesForOatFile(), &dex_file));
179         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
180       }
181     } else {
182       is_in_boot_image = (klass != nullptr) &&
183           runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
184       if (runtime->UseJitCompilation()) {
185         // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
186         // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
187         if (is_in_boot_image) {
188           // TODO: Use direct pointers for all non-moving spaces, not just boot image. Bug: 29530787
189           desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
190         } else if (klass != nullptr) {
191           desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
192         } else {
193           // Class not loaded yet. This happens when the dex code requesting
194           // this `HLoadClass` hasn't been executed in the interpreter.
195           // Fallback to the dex cache.
196           // TODO(ngeoffray): Generate HDeoptimize instead.
197           desired_load_kind = HLoadClass::LoadKind::kDexCacheViaMethod;
198         }
199       } else if (is_in_boot_image && !codegen->GetCompilerOptions().GetCompilePic()) {
200         // AOT app compilation. Check if the class is in the boot image.
201         desired_load_kind = HLoadClass::LoadKind::kBootImageAddress;
202       } else {
203         // Not JIT and either the klass is not in boot image or we are compiling in PIC mode.
204         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
205       }
206     }
207     DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
208 
209     if (is_in_boot_image) {
210       load_class->MarkInBootImage();
211     }
212     load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
213   }
214 
215   if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
216     if ((load_kind == HLoadClass::LoadKind::kDexCacheViaMethod) ||
217         (load_kind == HLoadClass::LoadKind::kBssEntry)) {
218       // We actually cannot reference this class, we're forced to bail.
219       // We cannot reference this class with Bss, as the entrypoint will lookup the class
220       // in the caller's dex file, but that dex file does not reference the class.
221       return HLoadClass::LoadKind::kInvalid;
222     }
223   }
224   return load_kind;
225 }
226 
ProcessLoadString(HLoadString * load_string)227 void HSharpening::ProcessLoadString(HLoadString* load_string) {
228   DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kDexCacheViaMethod);
229 
230   const DexFile& dex_file = load_string->GetDexFile();
231   dex::StringIndex string_index = load_string->GetStringIndex();
232 
233   HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
234   {
235     Runtime* runtime = Runtime::Current();
236     ClassLinker* class_linker = runtime->GetClassLinker();
237     ScopedObjectAccess soa(Thread::Current());
238     StackHandleScope<1> hs(soa.Self());
239     Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *compilation_unit_.GetDexFile())
240         ? compilation_unit_.GetDexCache()
241         : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
242     mirror::String* string = nullptr;
243 
244     if (codegen_->GetCompilerOptions().IsBootImage()) {
245       // Compiling boot image. Resolve the string and allocate it if needed, to ensure
246       // the string will be added to the boot image.
247       DCHECK(!runtime->UseJitCompilation());
248       string = class_linker->ResolveString(dex_file, string_index, dex_cache);
249       CHECK(string != nullptr);
250       if (compiler_driver_->GetSupportBootImageFixup()) {
251         DCHECK(ContainsElement(compiler_driver_->GetDexFilesForOatFile(), &dex_file));
252         desired_load_kind = codegen_->GetCompilerOptions().GetCompilePic()
253             ? HLoadString::LoadKind::kBootImageLinkTimePcRelative
254             : HLoadString::LoadKind::kBootImageLinkTimeAddress;
255       } else {
256         // compiler_driver_test. Do not sharpen.
257         desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
258       }
259     } else if (runtime->UseJitCompilation()) {
260       // TODO: Make sure we don't set the "compile PIC" flag for JIT as that's bogus.
261       // DCHECK(!codegen_->GetCompilerOptions().GetCompilePic());
262       string = class_linker->LookupString(dex_file, string_index, dex_cache.Get());
263       if (string != nullptr) {
264         if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
265           desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
266         } else {
267           desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
268         }
269       } else {
270         desired_load_kind = HLoadString::LoadKind::kDexCacheViaMethod;
271       }
272     } else {
273       // AOT app compilation. Try to lookup the string without allocating if not found.
274       string = class_linker->LookupString(dex_file, string_index, dex_cache.Get());
275       if (string != nullptr &&
276           runtime->GetHeap()->ObjectIsInBootImageSpace(string) &&
277           !codegen_->GetCompilerOptions().GetCompilePic()) {
278         desired_load_kind = HLoadString::LoadKind::kBootImageAddress;
279       } else {
280         desired_load_kind = HLoadString::LoadKind::kBssEntry;
281       }
282     }
283     if (string != nullptr) {
284       load_string->SetString(handles_->NewHandle(string));
285     }
286   }
287   DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
288 
289   HLoadString::LoadKind load_kind = codegen_->GetSupportedLoadStringKind(desired_load_kind);
290   load_string->SetLoadKind(load_kind);
291 }
292 
293 }  // namespace art
294