Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2012 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 | #ifndef ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_ |
| 18 | #define ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_ |
| 19 | |
| 20 | #include "compiler_driver.h" |
Mingyao Yang | 98d1cc8 | 2014-05-15 17:02:16 -0700 | [diff] [blame] | 21 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 22 | #include "art_field-inl.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 23 | #include "art_method-inl.h" |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 24 | #include "base/enums.h" |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 25 | #include "class_linker-inl.h" |
Andreas Gampe | 53c913b | 2014-08-12 23:19:23 -0700 | [diff] [blame] | 26 | #include "dex_compilation_unit.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 27 | #include "mirror/class_loader.h" |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 28 | #include "mirror/dex_cache-inl.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 29 | #include "scoped_thread_state_change.h" |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 30 | #include "handle_scope-inl.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 31 | |
| 32 | namespace art { |
| 33 | |
| 34 | inline mirror::DexCache* CompilerDriver::GetDexCache(const DexCompilationUnit* mUnit) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 35 | return mUnit->GetClassLinker()->FindDexCache(Thread::Current(), *mUnit->GetDexFile(), false); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 36 | } |
| 37 | |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 38 | inline mirror::ClassLoader* CompilerDriver::GetClassLoader(const ScopedObjectAccess& soa, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 39 | const DexCompilationUnit* mUnit) { |
| 40 | return soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()); |
| 41 | } |
| 42 | |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 43 | inline mirror::Class* CompilerDriver::ResolveClass( |
| 44 | const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
| 45 | Handle<mirror::ClassLoader> class_loader, uint16_t cls_index, |
| 46 | const DexCompilationUnit* mUnit) { |
| 47 | DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile()); |
| 48 | DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); |
| 49 | mirror::Class* cls = mUnit->GetClassLinker()->ResolveType( |
| 50 | *mUnit->GetDexFile(), cls_index, dex_cache, class_loader); |
| 51 | DCHECK_EQ(cls == nullptr, soa.Self()->IsExceptionPending()); |
| 52 | if (UNLIKELY(cls == nullptr)) { |
| 53 | // Clean up any exception left by type resolution. |
| 54 | soa.Self()->ClearException(); |
| 55 | } |
| 56 | return cls; |
| 57 | } |
| 58 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 59 | inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass( |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 60 | const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 61 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) { |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 62 | DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile()); |
| 63 | DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 64 | const DexFile::MethodId& referrer_method_id = |
| 65 | mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex()); |
Nicolas Geoffray | 9437b78 | 2015-03-25 10:08:51 +0000 | [diff] [blame] | 66 | return ResolveClass(soa, dex_cache, class_loader, referrer_method_id.class_idx_, mUnit); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 67 | } |
| 68 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 69 | inline ArtField* CompilerDriver::ResolveFieldWithDexFile( |
Nicolas Geoffray | e503832 | 2014-07-04 09:41:32 +0100 | [diff] [blame] | 70 | const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 71 | Handle<mirror::ClassLoader> class_loader, const DexFile* dex_file, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 72 | uint32_t field_idx, bool is_static) { |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 73 | DCHECK_EQ(dex_cache->GetDexFile(), dex_file); |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 74 | ArtField* resolved_field = Runtime::Current()->GetClassLinker()->ResolveField( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 75 | *dex_file, field_idx, dex_cache, class_loader, is_static); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 76 | DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending()); |
| 77 | if (UNLIKELY(resolved_field == nullptr)) { |
| 78 | // Clean up any exception left by type resolution. |
| 79 | soa.Self()->ClearException(); |
| 80 | return nullptr; |
| 81 | } |
| 82 | if (UNLIKELY(resolved_field->IsStatic() != is_static)) { |
| 83 | // ClassLinker can return a field of the wrong kind directly from the DexCache. |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 84 | // Silently return null on such incompatible class change. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 85 | return nullptr; |
| 86 | } |
| 87 | return resolved_field; |
| 88 | } |
| 89 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 90 | inline mirror::DexCache* CompilerDriver::FindDexCache(const DexFile* dex_file) { |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 91 | return Runtime::Current()->GetClassLinker()->FindDexCache(Thread::Current(), *dex_file, false); |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 94 | inline ArtField* CompilerDriver::ResolveField( |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 95 | const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
| 96 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, |
| 97 | uint32_t field_idx, bool is_static) { |
| 98 | DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); |
| 99 | return ResolveFieldWithDexFile(soa, dex_cache, class_loader, mUnit->GetDexFile(), field_idx, |
| 100 | is_static); |
| 101 | } |
| 102 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 103 | inline void CompilerDriver::GetResolvedFieldDexFileLocation( |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 104 | ArtField* resolved_field, const DexFile** declaring_dex_file, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 105 | uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) { |
| 106 | mirror::Class* declaring_class = resolved_field->GetDeclaringClass(); |
| 107 | *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile(); |
| 108 | *declaring_class_idx = declaring_class->GetDexTypeIndex(); |
| 109 | *declaring_field_idx = resolved_field->GetDexFieldIndex(); |
| 110 | } |
| 111 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 112 | inline bool CompilerDriver::IsFieldVolatile(ArtField* field) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 113 | return field->IsVolatile(); |
| 114 | } |
| 115 | |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 116 | inline MemberOffset CompilerDriver::GetFieldOffset(ArtField* field) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 117 | return field->GetOffset(); |
| 118 | } |
| 119 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 120 | inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField( |
| 121 | mirror::DexCache* dex_cache, mirror::Class* referrer_class, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 122 | ArtField* resolved_field, uint16_t field_idx) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 123 | DCHECK(!resolved_field->IsStatic()); |
| 124 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 125 | bool fast_get = referrer_class != nullptr && |
| 126 | referrer_class->CanAccessResolvedField(fields_class, resolved_field, |
| 127 | dex_cache, field_idx); |
| 128 | bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 129 | return std::make_pair(fast_get, fast_put); |
| 130 | } |
| 131 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 132 | template <typename ArtMember> |
| 133 | inline bool CompilerDriver::CanAccessResolvedMember(mirror::Class* referrer_class ATTRIBUTE_UNUSED, |
| 134 | mirror::Class* access_to ATTRIBUTE_UNUSED, |
| 135 | ArtMember* member ATTRIBUTE_UNUSED, |
| 136 | mirror::DexCache* dex_cache ATTRIBUTE_UNUSED, |
| 137 | uint32_t field_idx ATTRIBUTE_UNUSED) { |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 138 | // Not defined for ArtMember values other than ArtField or ArtMethod. |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 139 | UNREACHABLE(); |
| 140 | } |
| 141 | |
| 142 | template <> |
| 143 | inline bool CompilerDriver::CanAccessResolvedMember<ArtField>(mirror::Class* referrer_class, |
| 144 | mirror::Class* access_to, |
| 145 | ArtField* field, |
| 146 | mirror::DexCache* dex_cache, |
| 147 | uint32_t field_idx) { |
| 148 | return referrer_class->CanAccessResolvedField(access_to, field, dex_cache, field_idx); |
| 149 | } |
| 150 | |
| 151 | template <> |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 152 | inline bool CompilerDriver::CanAccessResolvedMember<ArtMethod>( |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 153 | mirror::Class* referrer_class, |
| 154 | mirror::Class* access_to, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 155 | ArtMethod* method, |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 156 | mirror::DexCache* dex_cache, |
| 157 | uint32_t field_idx) { |
| 158 | return referrer_class->CanAccessResolvedMethod(access_to, method, dex_cache, field_idx); |
| 159 | } |
| 160 | |
| 161 | template <typename ArtMember> |
| 162 | inline std::pair<bool, bool> CompilerDriver::IsClassOfStaticMemberAvailableToReferrer( |
| 163 | mirror::DexCache* dex_cache, |
| 164 | mirror::Class* referrer_class, |
| 165 | ArtMember* resolved_member, |
| 166 | uint16_t member_idx, |
| 167 | uint32_t* storage_index) { |
| 168 | DCHECK(resolved_member->IsStatic()); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 169 | if (LIKELY(referrer_class != nullptr)) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 170 | mirror::Class* members_class = resolved_member->GetDeclaringClass(); |
| 171 | if (members_class == referrer_class) { |
| 172 | *storage_index = members_class->GetDexTypeIndex(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 173 | return std::make_pair(true, true); |
| 174 | } |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 175 | if (CanAccessResolvedMember<ArtMember>( |
| 176 | referrer_class, members_class, resolved_member, dex_cache, member_idx)) { |
| 177 | // We have the resolved member, we must make it into a index for the referrer |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 178 | // in its static storage (which may fail if it doesn't have a slot for it) |
| 179 | // TODO: for images we can elide the static storage base null check |
| 180 | // if we know there's a non-null entry in the image |
| 181 | const DexFile* dex_file = dex_cache->GetDexFile(); |
| 182 | uint32_t storage_idx = DexFile::kDexNoIndex; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 183 | if (LIKELY(members_class->GetDexCache() == dex_cache)) { |
| 184 | // common case where the dex cache of both the referrer and the member are the same, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 185 | // no need to search the dex file |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 186 | storage_idx = members_class->GetDexTypeIndex(); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 187 | } else { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 188 | // Search dex file for localized ssb index, may fail if member's class is a parent |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 189 | // of the class mentioned in the dex file and there is no dex cache entry. |
Nicolas Geoffray | e4084a5 | 2016-02-18 14:43:42 +0000 | [diff] [blame] | 190 | storage_idx = resolved_member->GetDeclaringClass()->FindTypeIndexInOtherDexFile(*dex_file); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 191 | } |
| 192 | if (storage_idx != DexFile::kDexNoIndex) { |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 193 | *storage_index = storage_idx; |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 194 | return std::make_pair(true, !resolved_member->IsFinal()); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 195 | } |
| 196 | } |
| 197 | } |
| 198 | // Conservative defaults. |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 199 | *storage_index = DexFile::kDexNoIndex; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 200 | return std::make_pair(false, false); |
| 201 | } |
| 202 | |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 203 | inline std::pair<bool, bool> CompilerDriver::IsFastStaticField( |
| 204 | mirror::DexCache* dex_cache, mirror::Class* referrer_class, |
| 205 | ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) { |
| 206 | return IsClassOfStaticMemberAvailableToReferrer( |
| 207 | dex_cache, referrer_class, resolved_field, field_idx, storage_index); |
| 208 | } |
| 209 | |
| 210 | inline bool CompilerDriver::IsClassOfStaticMethodAvailableToReferrer( |
| 211 | mirror::DexCache* dex_cache, mirror::Class* referrer_class, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 212 | ArtMethod* resolved_method, uint16_t method_idx, uint32_t* storage_index) { |
Roland Levillain | 4c0eb42 | 2015-04-24 16:43:49 +0100 | [diff] [blame] | 213 | std::pair<bool, bool> result = IsClassOfStaticMemberAvailableToReferrer( |
| 214 | dex_cache, referrer_class, resolved_method, method_idx, storage_index); |
| 215 | // Only the first member of `result` is meaningful, as there is no |
| 216 | // "write access" to a method. |
| 217 | return result.first; |
| 218 | } |
| 219 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 220 | inline bool CompilerDriver::IsStaticFieldInReferrerClass(mirror::Class* referrer_class, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 221 | ArtField* resolved_field) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 222 | DCHECK(resolved_field->IsStatic()); |
| 223 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
| 224 | return referrer_class == fields_class; |
| 225 | } |
| 226 | |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 227 | inline bool CompilerDriver::CanAssumeClassIsInitialized(mirror::Class* klass) { |
| 228 | // Being loaded is a pre-requisite for being initialized but let's do the cheap check first. |
| 229 | // |
| 230 | // NOTE: When AOT compiling an app, we eagerly initialize app classes (and potentially their |
| 231 | // super classes in the boot image) but only those that have a trivial initialization, i.e. |
| 232 | // without <clinit>() or static values in the dex file for that class or any of its super |
| 233 | // classes. So while we could see the klass as initialized during AOT compilation and have |
| 234 | // it only loaded at runtime, the needed initialization would have to be trivial and |
| 235 | // unobservable from Java, so we may as well treat it as initialized. |
| 236 | if (!klass->IsInitialized()) { |
| 237 | return false; |
| 238 | } |
| 239 | return CanAssumeClassIsLoaded(klass); |
| 240 | } |
| 241 | |
| 242 | inline bool CompilerDriver::CanReferrerAssumeClassIsInitialized(mirror::Class* referrer_class, |
| 243 | mirror::Class* klass) { |
Nicolas Geoffray | b783b40 | 2015-06-22 11:06:43 +0100 | [diff] [blame] | 244 | return (referrer_class != nullptr |
| 245 | && !referrer_class->IsInterface() |
| 246 | && referrer_class->IsSubClass(klass)) |
| 247 | || CanAssumeClassIsInitialized(klass); |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 248 | } |
| 249 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 250 | inline bool CompilerDriver::IsStaticFieldsClassInitialized(mirror::Class* referrer_class, |
Mathieu Chartier | c785344 | 2015-03-27 14:35:38 -0700 | [diff] [blame] | 251 | ArtField* resolved_field) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 252 | DCHECK(resolved_field->IsStatic()); |
| 253 | mirror::Class* fields_class = resolved_field->GetDeclaringClass(); |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 254 | return CanReferrerAssumeClassIsInitialized(referrer_class, fields_class); |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 255 | } |
| 256 | |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 257 | inline ArtMethod* CompilerDriver::ResolveMethod( |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 258 | ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
| 259 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 260 | uint32_t method_idx, InvokeType invoke_type, bool check_incompatible_class_change) { |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 261 | DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader())); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 262 | ArtMethod* resolved_method = |
| 263 | check_incompatible_class_change |
| 264 | ? mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kForceICCECheck>( |
| 265 | *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type) |
| 266 | : mUnit->GetClassLinker()->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
| 267 | *dex_cache->GetDexFile(), method_idx, dex_cache, class_loader, nullptr, invoke_type); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 268 | if (UNLIKELY(resolved_method == nullptr)) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 269 | DCHECK(soa.Self()->IsExceptionPending()); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 270 | // Clean up any exception left by type resolution. |
| 271 | soa.Self()->ClearException(); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 272 | } |
| 273 | return resolved_method; |
| 274 | } |
| 275 | |
| 276 | inline void CompilerDriver::GetResolvedMethodDexFileLocation( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 277 | ArtMethod* resolved_method, const DexFile** declaring_dex_file, |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 278 | uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) { |
| 279 | mirror::Class* declaring_class = resolved_method->GetDeclaringClass(); |
| 280 | *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile(); |
| 281 | *declaring_class_idx = declaring_class->GetDexTypeIndex(); |
| 282 | *declaring_method_idx = resolved_method->GetDexMethodIndex(); |
| 283 | } |
| 284 | |
| 285 | inline uint16_t CompilerDriver::GetResolvedMethodVTableIndex( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 286 | ArtMethod* resolved_method, InvokeType type) { |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 287 | if (type == kVirtual || type == kSuper) { |
| 288 | return resolved_method->GetMethodIndex(); |
| 289 | } else if (type == kInterface) { |
| 290 | return resolved_method->GetDexMethodIndex(); |
| 291 | } else { |
| 292 | return DexFile::kDexNoIndex16; |
| 293 | } |
| 294 | } |
| 295 | |
| 296 | inline int CompilerDriver::IsFastInvoke( |
Mathieu Chartier | 0cd8135 | 2014-05-22 16:48:55 -0700 | [diff] [blame] | 297 | ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache, |
| 298 | Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 299 | mirror::Class* referrer_class, ArtMethod* resolved_method, InvokeType* invoke_type, |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 300 | MethodReference* target_method, const MethodReference* devirt_target, |
| 301 | uintptr_t* direct_code, uintptr_t* direct_method) { |
| 302 | // Don't try to fast-path if we don't understand the caller's class. |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 303 | // Referrer_class is the class that this invoke is contained in. |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 304 | if (UNLIKELY(referrer_class == nullptr)) { |
| 305 | return 0; |
| 306 | } |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 307 | StackHandleScope<2> hs(soa.Self()); |
| 308 | // Methods_class is the class refered to by the class_idx field of the methodId the method_idx is |
| 309 | // pointing to. |
| 310 | // For example in |
| 311 | // .class LABC; |
| 312 | // .super LDEF; |
| 313 | // .method hi()V |
| 314 | // ... |
| 315 | // invoke-super {p0}, LDEF;->hi()V |
| 316 | // ... |
| 317 | // .end method |
| 318 | // the referrer_class is 'ABC' and the methods_class is DEF. Note that the methods class is 'DEF' |
| 319 | // even if 'DEF' inherits the method from it's superclass. |
| 320 | Handle<mirror::Class> methods_class(hs.NewHandle(mUnit->GetClassLinker()->ResolveType( |
| 321 | *target_method->dex_file, |
| 322 | target_method->dex_file->GetMethodId(target_method->dex_method_index).class_idx_, |
| 323 | dex_cache, |
| 324 | class_loader))); |
| 325 | DCHECK(methods_class.Get() != nullptr); |
| 326 | mirror::Class* methods_declaring_class = resolved_method->GetDeclaringClass(); |
| 327 | if (UNLIKELY(!referrer_class->CanAccessResolvedMethod(methods_declaring_class, resolved_method, |
Mathieu Chartier | eb8167a | 2014-05-07 15:43:14 -0700 | [diff] [blame] | 328 | dex_cache.Get(), |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 329 | target_method->dex_method_index))) { |
| 330 | return 0; |
| 331 | } |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 332 | // Sharpen a virtual call into a direct call when the target is known not to have been |
| 333 | // overridden (ie is final). |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 334 | const bool same_dex_file = target_method->dex_file == mUnit->GetDexFile(); |
| 335 | bool can_sharpen_virtual_based_on_type = same_dex_file && |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 336 | (*invoke_type == kVirtual) && (resolved_method->IsFinal() || |
| 337 | methods_declaring_class->IsFinal()); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 338 | // For invoke-super, ensure the vtable index will be correct to dispatch in the vtable of |
| 339 | // the super class. |
Andreas Gampe | 542451c | 2016-07-26 09:02:02 -0700 | [diff] [blame] | 340 | const PointerSize pointer_size = InstructionSetPointerSize(GetInstructionSet()); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 341 | // TODO We should be able to sharpen if we are going into the boot image as well. |
| 342 | bool can_sharpen_super_based_on_type = same_dex_file && |
| 343 | (*invoke_type == kSuper) && |
| 344 | !methods_class->IsInterface() && |
| 345 | (referrer_class != methods_declaring_class) && |
| 346 | referrer_class->IsSubClass(methods_declaring_class) && |
| 347 | resolved_method->GetMethodIndex() < methods_declaring_class->GetVTableLength() && |
| 348 | (methods_declaring_class->GetVTableEntry( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 349 | resolved_method->GetMethodIndex(), pointer_size) == resolved_method) && |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 350 | resolved_method->IsInvokable(); |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 351 | // TODO We should be able to sharpen if we are going into the boot image as well. |
| 352 | bool can_sharpen_interface_super_based_on_type = same_dex_file && |
| 353 | (*invoke_type == kSuper) && |
| 354 | methods_class->IsInterface() && |
| 355 | methods_class->IsAssignableFrom(referrer_class) && |
| 356 | resolved_method->IsInvokable(); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 357 | |
Alex Light | fedd91d | 2016-01-07 14:49:16 -0800 | [diff] [blame] | 358 | if (can_sharpen_virtual_based_on_type || |
| 359 | can_sharpen_super_based_on_type || |
| 360 | can_sharpen_interface_super_based_on_type) { |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 361 | // Sharpen a virtual call into a direct call. The method_idx is into referrer's |
| 362 | // dex cache, check that this resolved method is where we expect it. |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 363 | CHECK_EQ(target_method->dex_file, mUnit->GetDexFile()); |
Mathieu Chartier | 673ed3d | 2015-08-28 14:56:43 -0700 | [diff] [blame] | 364 | DCHECK_EQ(dex_cache.Get(), mUnit->GetClassLinker()->FindDexCache( |
| 365 | soa.Self(), *mUnit->GetDexFile(), false)); |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 366 | CHECK_EQ(referrer_class->GetDexCache()->GetResolvedMethod( |
| 367 | target_method->dex_method_index, pointer_size), |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 368 | resolved_method) << PrettyMethod(resolved_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 369 | int stats_flags = kFlagMethodResolved; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame] | 370 | GetCodeAndMethodForDirectCall(/*out*/invoke_type, |
| 371 | kDirect, // Sharp type |
| 372 | false, // The dex cache is guaranteed to be available |
| 373 | referrer_class, resolved_method, |
| 374 | /*out*/&stats_flags, |
| 375 | target_method, |
| 376 | /*out*/direct_code, |
| 377 | /*out*/direct_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 378 | DCHECK_NE(*invoke_type, kSuper) << PrettyMethod(resolved_method); |
| 379 | if (*invoke_type == kDirect) { |
| 380 | stats_flags |= kFlagsMethodResolvedVirtualMadeDirect; |
| 381 | } |
| 382 | return stats_flags; |
| 383 | } |
| 384 | |
| 385 | if ((*invoke_type == kVirtual || *invoke_type == kInterface) && devirt_target != nullptr) { |
| 386 | // Post-verification callback recorded a more precise invoke target based on its type info. |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 387 | ArtMethod* called_method; |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 388 | ClassLinker* class_linker = mUnit->GetClassLinker(); |
| 389 | if (LIKELY(devirt_target->dex_file == mUnit->GetDexFile())) { |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 390 | called_method = class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 391 | *devirt_target->dex_file, devirt_target->dex_method_index, dex_cache, class_loader, |
| 392 | nullptr, kVirtual); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 393 | } else { |
Mathieu Chartier | f284d44 | 2016-06-02 11:48:30 -0700 | [diff] [blame] | 394 | auto target_dex_cache(hs.NewHandle(class_linker->RegisterDexFile(*devirt_target->dex_file, |
| 395 | class_loader.Get()))); |
Andreas Gampe | 42ef8ab | 2015-12-03 17:27:32 -0800 | [diff] [blame] | 396 | called_method = class_linker->ResolveMethod<ClassLinker::kNoICCECheckForCache>( |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 397 | *devirt_target->dex_file, devirt_target->dex_method_index, target_dex_cache, |
| 398 | class_loader, nullptr, kVirtual); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 399 | } |
Mathieu Chartier | 2cebb24 | 2015-04-21 16:50:40 -0700 | [diff] [blame] | 400 | CHECK(called_method != nullptr); |
Alex Light | 9139e00 | 2015-10-09 15:59:48 -0700 | [diff] [blame] | 401 | CHECK(called_method->IsInvokable()); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 402 | int stats_flags = kFlagMethodResolved; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame] | 403 | GetCodeAndMethodForDirectCall(/*out*/invoke_type, |
| 404 | kDirect, // Sharp type |
| 405 | true, // The dex cache may not be available |
| 406 | referrer_class, called_method, |
| 407 | /*out*/&stats_flags, |
| 408 | target_method, |
| 409 | /*out*/direct_code, |
| 410 | /*out*/direct_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 411 | DCHECK_NE(*invoke_type, kSuper); |
| 412 | if (*invoke_type == kDirect) { |
| 413 | stats_flags |= kFlagsMethodResolvedPreciseTypeDevirtualization; |
| 414 | } |
| 415 | return stats_flags; |
| 416 | } |
| 417 | |
| 418 | if (UNLIKELY(*invoke_type == kSuper)) { |
| 419 | // Unsharpened super calls are suspicious so go slow-path. |
| 420 | return 0; |
| 421 | } |
| 422 | |
| 423 | // Sharpening failed so generate a regular resolved method dispatch. |
| 424 | int stats_flags = kFlagMethodResolved; |
Igor Murashkin | d6dee67 | 2014-10-16 18:36:16 -0700 | [diff] [blame] | 425 | GetCodeAndMethodForDirectCall(/*out*/invoke_type, |
| 426 | *invoke_type, // Sharp type |
| 427 | false, // The dex cache is guaranteed to be available |
| 428 | referrer_class, resolved_method, |
| 429 | /*out*/&stats_flags, |
| 430 | target_method, |
| 431 | /*out*/direct_code, |
| 432 | /*out*/direct_method); |
Vladimir Marko | f096aad | 2014-01-23 15:51:58 +0000 | [diff] [blame] | 433 | return stats_flags; |
| 434 | } |
| 435 | |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 436 | inline bool CompilerDriver::IsMethodsClassInitialized(mirror::Class* referrer_class, |
Mathieu Chartier | e401d14 | 2015-04-22 13:56:20 -0700 | [diff] [blame] | 437 | ArtMethod* resolved_method) { |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 438 | if (!resolved_method->IsStatic()) { |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 439 | return true; |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 440 | } |
| 441 | mirror::Class* methods_class = resolved_method->GetDeclaringClass(); |
Vladimir Marko | 07785bb | 2015-06-15 18:52:54 +0100 | [diff] [blame] | 442 | return CanReferrerAssumeClassIsInitialized(referrer_class, methods_class); |
Vladimir Marko | 9820b7c | 2014-01-02 16:40:37 +0000 | [diff] [blame] | 443 | } |
| 444 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 445 | } // namespace art |
| 446 | |
| 447 | #endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_ |