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