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