Home | History | Annotate | Download | only in optimizing
      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 "art_method-inl.h"
     20 #include "base/casts.h"
     21 #include "base/enums.h"
     22 #include "class_linker.h"
     23 #include "code_generator.h"
     24 #include "driver/compiler_options.h"
     25 #include "driver/dex_compilation_unit.h"
     26 #include "gc/heap.h"
     27 #include "gc/space/image_space.h"
     28 #include "handle_scope-inl.h"
     29 #include "mirror/dex_cache.h"
     30 #include "mirror/string.h"
     31 #include "nodes.h"
     32 #include "runtime.h"
     33 #include "scoped_thread_state_change-inl.h"
     34 #include "utils/dex_cache_arrays_layout-inl.h"
     35 
     36 namespace art {
     37 
     38 static bool IsInBootImage(ArtMethod* method) {
     39   const std::vector<gc::space::ImageSpace*>& image_spaces =
     40       Runtime::Current()->GetHeap()->GetBootImageSpaces();
     41   for (gc::space::ImageSpace* image_space : image_spaces) {
     42     const ImageSection& method_section = image_space->GetImageHeader().GetMethodsSection();
     43     if (method_section.Contains(reinterpret_cast<uint8_t*>(method) - image_space->Begin())) {
     44       return true;
     45     }
     46   }
     47   return false;
     48 }
     49 
     50 static bool BootImageAOTCanEmbedMethod(ArtMethod* method, const CompilerOptions& compiler_options) {
     51   DCHECK(compiler_options.IsBootImage());
     52   ScopedObjectAccess soa(Thread::Current());
     53   ObjPtr<mirror::Class> klass = method->GetDeclaringClass();
     54   DCHECK(klass != nullptr);
     55   const DexFile& dex_file = klass->GetDexFile();
     56   return compiler_options.IsImageClass(dex_file.StringByTypeIdx(klass->GetDexTypeIndex()));
     57 }
     58 
     59 HInvokeStaticOrDirect::DispatchInfo HSharpening::SharpenInvokeStaticOrDirect(
     60     ArtMethod* callee, CodeGenerator* codegen) {
     61   if (kIsDebugBuild) {
     62     ScopedObjectAccess soa(Thread::Current());  // Required for GetDeclaringClass below.
     63     DCHECK(callee != nullptr);
     64     DCHECK(!(callee->IsConstructor() && callee->GetDeclaringClass()->IsStringClass()));
     65   }
     66 
     67   HInvokeStaticOrDirect::MethodLoadKind method_load_kind;
     68   HInvokeStaticOrDirect::CodePtrLocation code_ptr_location;
     69   uint64_t method_load_data = 0u;
     70 
     71   // Note: we never call an ArtMethod through a known code pointer, as
     72   // we do not want to keep on invoking it if it gets deoptimized. This
     73   // applies to both AOT and JIT.
     74   // This also avoids having to find out if the code pointer of an ArtMethod
     75   // is the resolution trampoline (for ensuring the class is initialized), or
     76   // the interpreter entrypoint. Such code pointers we do not want to call
     77   // directly.
     78   // Only in the case of a recursive call can we call directly, as we know the
     79   // class is initialized already or being initialized, and the call will not
     80   // be invoked once the method is deoptimized.
     81 
     82   // We don't optimize for debuggable as it would prevent us from obsoleting the method in some
     83   // situations.
     84   const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
     85   if (callee == codegen->GetGraph()->GetArtMethod() && !codegen->GetGraph()->IsDebuggable()) {
     86     // Recursive call.
     87     method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRecursive;
     88     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallSelf;
     89   } else if (compiler_options.IsBootImage()) {
     90     if (!compiler_options.GetCompilePic()) {
     91       // Test configuration, do not sharpen.
     92       method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
     93     } else if (BootImageAOTCanEmbedMethod(callee, compiler_options)) {
     94       method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative;
     95     } else {
     96       // Use PC-relative access to the .bss methods array.
     97       method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
     98     }
     99     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
    100   } else if (Runtime::Current()->UseJitCompilation()) {
    101     // JIT or on-device AOT compilation referencing a boot image method.
    102     // Use the method address directly.
    103     method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress;
    104     method_load_data = reinterpret_cast<uintptr_t>(callee);
    105     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
    106   } else if (IsInBootImage(callee)) {
    107     // Use PC-relative access to the .data.bimg.rel.ro methods array.
    108     method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo;
    109     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
    110   } else {
    111     // Use PC-relative access to the .bss methods array.
    112     method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kBssEntry;
    113     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
    114   }
    115 
    116   if (codegen->GetGraph()->IsDebuggable()) {
    117     // For debuggable apps always use the code pointer from ArtMethod
    118     // so that we don't circumvent instrumentation stubs if installed.
    119     code_ptr_location = HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod;
    120   }
    121 
    122   HInvokeStaticOrDirect::DispatchInfo desired_dispatch_info = {
    123       method_load_kind, code_ptr_location, method_load_data
    124   };
    125   return codegen->GetSupportedInvokeStaticOrDirectDispatch(desired_dispatch_info, callee);
    126 }
    127 
    128 HLoadClass::LoadKind HSharpening::ComputeLoadClassKind(
    129     HLoadClass* load_class,
    130     CodeGenerator* codegen,
    131     const DexCompilationUnit& dex_compilation_unit) {
    132   Handle<mirror::Class> klass = load_class->GetClass();
    133   DCHECK(load_class->GetLoadKind() == HLoadClass::LoadKind::kRuntimeCall ||
    134          load_class->GetLoadKind() == HLoadClass::LoadKind::kReferrersClass)
    135       << load_class->GetLoadKind();
    136   DCHECK(!load_class->IsInBootImage()) << "HLoadClass should not be optimized before sharpening.";
    137 
    138   HLoadClass::LoadKind load_kind = load_class->GetLoadKind();
    139 
    140   if (load_class->NeedsAccessCheck()) {
    141     // We need to call the runtime anyway, so we simply get the class as that call's return value.
    142   } else if (load_kind == HLoadClass::LoadKind::kReferrersClass) {
    143     // Loading from the ArtMethod* is the most efficient retrieval in code size.
    144     // TODO: This may not actually be true for all architectures and
    145     // locations of target classes. The additional register pressure
    146     // for using the ArtMethod* should be considered.
    147   } else {
    148     const DexFile& dex_file = load_class->GetDexFile();
    149     dex::TypeIndex type_index = load_class->GetTypeIndex();
    150 
    151     bool is_in_boot_image = false;
    152     HLoadClass::LoadKind desired_load_kind = HLoadClass::LoadKind::kInvalid;
    153     Runtime* runtime = Runtime::Current();
    154     const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
    155     if (compiler_options.IsBootImage()) {
    156       // Compiling boot image. Check if the class is a boot image class.
    157       DCHECK(!runtime->UseJitCompilation());
    158       if (!compiler_options.GetCompilePic()) {
    159         // Test configuration, do not sharpen.
    160         desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
    161       } else if ((klass != nullptr) &&
    162                  compiler_options.IsImageClass(dex_file.StringByTypeIdx(type_index))) {
    163         is_in_boot_image = true;
    164         desired_load_kind = HLoadClass::LoadKind::kBootImageLinkTimePcRelative;
    165       } else {
    166         // Not a boot image class.
    167         DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
    168         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
    169       }
    170     } else {
    171       is_in_boot_image = (klass != nullptr) &&
    172           runtime->GetHeap()->ObjectIsInBootImageSpace(klass.Get());
    173       if (runtime->UseJitCompilation()) {
    174         DCHECK(!compiler_options.GetCompilePic());
    175         if (is_in_boot_image) {
    176           desired_load_kind = HLoadClass::LoadKind::kJitBootImageAddress;
    177         } else if (klass != nullptr) {
    178           desired_load_kind = HLoadClass::LoadKind::kJitTableAddress;
    179         } else {
    180           // Class not loaded yet. This happens when the dex code requesting
    181           // this `HLoadClass` hasn't been executed in the interpreter.
    182           // Fallback to the dex cache.
    183           // TODO(ngeoffray): Generate HDeoptimize instead.
    184           desired_load_kind = HLoadClass::LoadKind::kRuntimeCall;
    185         }
    186       } else if (is_in_boot_image) {
    187         // AOT app compilation, boot image class.
    188         desired_load_kind = HLoadClass::LoadKind::kBootImageRelRo;
    189       } else {
    190         // Not JIT and the klass is not in boot image.
    191         desired_load_kind = HLoadClass::LoadKind::kBssEntry;
    192       }
    193     }
    194     DCHECK_NE(desired_load_kind, HLoadClass::LoadKind::kInvalid);
    195 
    196     if (is_in_boot_image) {
    197       load_class->MarkInBootImage();
    198     }
    199     load_kind = codegen->GetSupportedLoadClassKind(desired_load_kind);
    200   }
    201 
    202   if (!IsSameDexFile(load_class->GetDexFile(), *dex_compilation_unit.GetDexFile())) {
    203     if ((load_kind == HLoadClass::LoadKind::kRuntimeCall) ||
    204         (load_kind == HLoadClass::LoadKind::kBssEntry)) {
    205       // We actually cannot reference this class, we're forced to bail.
    206       // We cannot reference this class with Bss, as the entrypoint will lookup the class
    207       // in the caller's dex file, but that dex file does not reference the class.
    208       return HLoadClass::LoadKind::kInvalid;
    209     }
    210   }
    211   return load_kind;
    212 }
    213 
    214 static inline bool CanUseTypeCheckBitstring(ObjPtr<mirror::Class> klass, CodeGenerator* codegen)
    215     REQUIRES_SHARED(Locks::mutator_lock_) {
    216   DCHECK(!klass->IsProxyClass());
    217   DCHECK(!klass->IsArrayClass());
    218 
    219   if (Runtime::Current()->UseJitCompilation()) {
    220     // If we're JITting, try to assign a type check bitstring (fall through).
    221   } else if (codegen->GetCompilerOptions().IsBootImage()) {
    222     const char* descriptor = klass->GetDexFile().StringByTypeIdx(klass->GetDexTypeIndex());
    223     if (!codegen->GetCompilerOptions().IsImageClass(descriptor)) {
    224       return false;
    225     }
    226     // If the target is a boot image class, try to assign a type check bitstring (fall through).
    227     // (If --force-determinism, this was already done; repeating is OK and yields the same result.)
    228   } else {
    229     // TODO: Use the bitstring also for AOT app compilation if the target class has a bitstring
    230     // already assigned in the boot image.
    231     return false;
    232   }
    233 
    234   // Try to assign a type check bitstring.
    235   MutexLock subtype_check_lock(Thread::Current(), *Locks::subtype_check_lock_);
    236   if ((false) &&  // FIXME: Inliner does not respect CompilerDriver::ShouldCompileMethod()
    237                   // and we're hitting an unassigned bitstring in dex2oat_image_test. b/26687569
    238       kIsDebugBuild &&
    239       codegen->GetCompilerOptions().IsBootImage() &&
    240       codegen->GetCompilerOptions().IsForceDeterminism()) {
    241     SubtypeCheckInfo::State old_state = SubtypeCheck<ObjPtr<mirror::Class>>::GetState(klass);
    242     CHECK(old_state == SubtypeCheckInfo::kAssigned || old_state == SubtypeCheckInfo::kOverflowed)
    243         << klass->PrettyDescriptor() << "/" << old_state
    244         << " in " << codegen->GetGraph()->PrettyMethod();
    245   }
    246   SubtypeCheckInfo::State state = SubtypeCheck<ObjPtr<mirror::Class>>::EnsureAssigned(klass);
    247   return state == SubtypeCheckInfo::kAssigned;
    248 }
    249 
    250 TypeCheckKind HSharpening::ComputeTypeCheckKind(ObjPtr<mirror::Class> klass,
    251                                                 CodeGenerator* codegen,
    252                                                 bool needs_access_check) {
    253   if (klass == nullptr) {
    254     return TypeCheckKind::kUnresolvedCheck;
    255   } else if (klass->IsInterface()) {
    256     return TypeCheckKind::kInterfaceCheck;
    257   } else if (klass->IsArrayClass()) {
    258     if (klass->GetComponentType()->IsObjectClass()) {
    259       return TypeCheckKind::kArrayObjectCheck;
    260     } else if (klass->CannotBeAssignedFromOtherTypes()) {
    261       return TypeCheckKind::kExactCheck;
    262     } else {
    263       return TypeCheckKind::kArrayCheck;
    264     }
    265   } else if (klass->IsFinal()) {  // TODO: Consider using bitstring for final classes.
    266     return TypeCheckKind::kExactCheck;
    267   } else if (kBitstringSubtypeCheckEnabled &&
    268              !needs_access_check &&
    269              CanUseTypeCheckBitstring(klass, codegen)) {
    270     // TODO: We should not need the `!needs_access_check` check but getting rid of that
    271     // requires rewriting some optimizations in instruction simplifier.
    272     return TypeCheckKind::kBitstringCheck;
    273   } else if (klass->IsAbstract()) {
    274     return TypeCheckKind::kAbstractClassCheck;
    275   } else {
    276     return TypeCheckKind::kClassHierarchyCheck;
    277   }
    278 }
    279 
    280 void HSharpening::ProcessLoadString(
    281     HLoadString* load_string,
    282     CodeGenerator* codegen,
    283     const DexCompilationUnit& dex_compilation_unit,
    284     VariableSizedHandleScope* handles) {
    285   DCHECK_EQ(load_string->GetLoadKind(), HLoadString::LoadKind::kRuntimeCall);
    286 
    287   const DexFile& dex_file = load_string->GetDexFile();
    288   dex::StringIndex string_index = load_string->GetStringIndex();
    289 
    290   HLoadString::LoadKind desired_load_kind = static_cast<HLoadString::LoadKind>(-1);
    291   {
    292     Runtime* runtime = Runtime::Current();
    293     ClassLinker* class_linker = runtime->GetClassLinker();
    294     ScopedObjectAccess soa(Thread::Current());
    295     StackHandleScope<1> hs(soa.Self());
    296     Handle<mirror::DexCache> dex_cache = IsSameDexFile(dex_file, *dex_compilation_unit.GetDexFile())
    297         ? dex_compilation_unit.GetDexCache()
    298         : hs.NewHandle(class_linker->FindDexCache(soa.Self(), dex_file));
    299     ObjPtr<mirror::String> string = nullptr;
    300 
    301     const CompilerOptions& compiler_options = codegen->GetCompilerOptions();
    302     if (compiler_options.IsBootImage()) {
    303       // Compiling boot image. Resolve the string and allocate it if needed, to ensure
    304       // the string will be added to the boot image.
    305       DCHECK(!runtime->UseJitCompilation());
    306       if (compiler_options.GetCompilePic()) {
    307         DCHECK(ContainsElement(compiler_options.GetDexFilesForOatFile(), &dex_file));
    308         if (compiler_options.IsForceDeterminism()) {
    309           // Strings for methods we're compiling should be pre-resolved but Strings in inlined
    310           // methods may not be if these inlined methods are not in the boot image profile.
    311           // Multiple threads allocating new Strings can cause non-deterministic boot image
    312           // because of the image relying on the order of GC roots we walk. (We could fix that
    313           // by ordering the roots we walk in ImageWriter.) Therefore we avoid allocating these
    314           // strings even if that results in omitting them from the boot image and using the
    315           // sub-optimal load kind kBssEntry.
    316           string = class_linker->LookupString(string_index, dex_cache.Get());
    317         } else {
    318           string = class_linker->ResolveString(string_index, dex_cache);
    319           CHECK(string != nullptr);
    320         }
    321         if (string != nullptr) {
    322           desired_load_kind = HLoadString::LoadKind::kBootImageLinkTimePcRelative;
    323         } else {
    324           desired_load_kind = HLoadString::LoadKind::kBssEntry;
    325         }
    326       } else {
    327         // Test configuration, do not sharpen.
    328         desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
    329       }
    330     } else if (runtime->UseJitCompilation()) {
    331       DCHECK(!codegen->GetCompilerOptions().GetCompilePic());
    332       string = class_linker->LookupString(string_index, dex_cache.Get());
    333       if (string != nullptr) {
    334         if (runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
    335           desired_load_kind = HLoadString::LoadKind::kJitBootImageAddress;
    336         } else {
    337           desired_load_kind = HLoadString::LoadKind::kJitTableAddress;
    338         }
    339       } else {
    340         desired_load_kind = HLoadString::LoadKind::kRuntimeCall;
    341       }
    342     } else {
    343       // AOT app compilation. Try to lookup the string without allocating if not found.
    344       string = class_linker->LookupString(string_index, dex_cache.Get());
    345       if (string != nullptr && runtime->GetHeap()->ObjectIsInBootImageSpace(string)) {
    346         desired_load_kind = HLoadString::LoadKind::kBootImageRelRo;
    347       } else {
    348         desired_load_kind = HLoadString::LoadKind::kBssEntry;
    349       }
    350     }
    351     if (string != nullptr) {
    352       load_string->SetString(handles->NewHandle(string));
    353     }
    354   }
    355   DCHECK_NE(desired_load_kind, static_cast<HLoadString::LoadKind>(-1));
    356 
    357   HLoadString::LoadKind load_kind = codegen->GetSupportedLoadStringKind(desired_load_kind);
    358   load_string->SetLoadKind(load_kind);
    359 }
    360 
    361 }  // namespace art
    362