blob: 3a91b08d5e5ce40c28c65ea02b1c1f59a4b62663 [file] [log] [blame]
Vladimir Markobe0e5462014-02-26 11:24:15 +00001/*
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 Yang98d1cc82014-05-15 17:02:16 -070021
Vladimir Markobe0e5462014-02-26 11:24:15 +000022#include "dex/compiler_ir.h"
Andreas Gampe53c913b2014-08-12 23:19:23 -070023#include "dex_compilation_unit.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000024#include "mirror/art_field-inl.h"
Vladimir Markof096aad2014-01-23 15:51:58 +000025#include "mirror/art_method-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000026#include "mirror/class_loader.h"
Vladimir Markof096aad2014-01-23 15:51:58 +000027#include "mirror/dex_cache-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000028#include "mirror/art_field-inl.h"
29#include "scoped_thread_state_change.h"
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070030#include "handle_scope-inl.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000031
32namespace art {
33
34inline mirror::DexCache* CompilerDriver::GetDexCache(const DexCompilationUnit* mUnit) {
35 return mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile());
36}
37
38inline mirror::ClassLoader* CompilerDriver::GetClassLoader(ScopedObjectAccess& soa,
39 const DexCompilationUnit* mUnit) {
40 return soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader());
41}
42
43inline mirror::Class* CompilerDriver::ResolveCompilingMethodsClass(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010044 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070045 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070046 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
47 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markobe0e5462014-02-26 11:24:15 +000048 const DexFile::MethodId& referrer_method_id =
49 mUnit->GetDexFile()->GetMethodId(mUnit->GetDexMethodIndex());
50 mirror::Class* referrer_class = mUnit->GetClassLinker()->ResolveType(
51 *mUnit->GetDexFile(), referrer_method_id.class_idx_, dex_cache, class_loader);
52 DCHECK_EQ(referrer_class == nullptr, soa.Self()->IsExceptionPending());
53 if (UNLIKELY(referrer_class == nullptr)) {
54 // Clean up any exception left by type resolution.
55 soa.Self()->ClearException();
56 }
57 return referrer_class;
58}
59
60inline mirror::ArtField* CompilerDriver::ResolveField(
Nicolas Geoffraye5038322014-07-04 09:41:32 +010061 const ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
Mathieu Chartier0cd81352014-05-22 16:48:55 -070062 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Vladimir Markobe0e5462014-02-26 11:24:15 +000063 uint32_t field_idx, bool is_static) {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -070064 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
65 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markobe0e5462014-02-26 11:24:15 +000066 mirror::ArtField* resolved_field = mUnit->GetClassLinker()->ResolveField(
67 *mUnit->GetDexFile(), field_idx, dex_cache, class_loader, is_static);
68 DCHECK_EQ(resolved_field == nullptr, soa.Self()->IsExceptionPending());
69 if (UNLIKELY(resolved_field == nullptr)) {
70 // Clean up any exception left by type resolution.
71 soa.Self()->ClearException();
72 return nullptr;
73 }
74 if (UNLIKELY(resolved_field->IsStatic() != is_static)) {
75 // ClassLinker can return a field of the wrong kind directly from the DexCache.
76 // Silently return nullptr on such incompatible class change.
77 return nullptr;
78 }
79 return resolved_field;
80}
81
82inline void CompilerDriver::GetResolvedFieldDexFileLocation(
83 mirror::ArtField* resolved_field, const DexFile** declaring_dex_file,
84 uint16_t* declaring_class_idx, uint16_t* declaring_field_idx) {
85 mirror::Class* declaring_class = resolved_field->GetDeclaringClass();
86 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
87 *declaring_class_idx = declaring_class->GetDexTypeIndex();
88 *declaring_field_idx = resolved_field->GetDexFieldIndex();
89}
90
91inline bool CompilerDriver::IsFieldVolatile(mirror::ArtField* field) {
92 return field->IsVolatile();
93}
94
Vladimir Marko66c6d7b2014-10-16 15:41:48 +010095inline MemberOffset CompilerDriver::GetFieldOffset(mirror::ArtField* field) {
96 return field->GetOffset();
97}
98
Vladimir Markobe0e5462014-02-26 11:24:15 +000099inline std::pair<bool, bool> CompilerDriver::IsFastInstanceField(
100 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Nicolas Geoffraye5038322014-07-04 09:41:32 +0100101 mirror::ArtField* resolved_field, uint16_t field_idx) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000102 DCHECK(!resolved_field->IsStatic());
103 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
104 bool fast_get = referrer_class != nullptr &&
105 referrer_class->CanAccessResolvedField(fields_class, resolved_field,
106 dex_cache, field_idx);
107 bool fast_put = fast_get && (!resolved_field->IsFinal() || fields_class == referrer_class);
Vladimir Markobe0e5462014-02-26 11:24:15 +0000108 return std::make_pair(fast_get, fast_put);
109}
110
111inline std::pair<bool, bool> CompilerDriver::IsFastStaticField(
112 mirror::DexCache* dex_cache, mirror::Class* referrer_class,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100113 mirror::ArtField* resolved_field, uint16_t field_idx, uint32_t* storage_index) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000114 DCHECK(resolved_field->IsStatic());
115 if (LIKELY(referrer_class != nullptr)) {
116 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
117 if (fields_class == referrer_class) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000118 *storage_index = fields_class->GetDexTypeIndex();
Vladimir Markobe0e5462014-02-26 11:24:15 +0000119 return std::make_pair(true, true);
120 }
121 if (referrer_class->CanAccessResolvedField(fields_class, resolved_field,
122 dex_cache, field_idx)) {
123 // We have the resolved field, we must make it into a index for the referrer
124 // in its static storage (which may fail if it doesn't have a slot for it)
125 // TODO: for images we can elide the static storage base null check
126 // if we know there's a non-null entry in the image
127 const DexFile* dex_file = dex_cache->GetDexFile();
128 uint32_t storage_idx = DexFile::kDexNoIndex;
129 if (LIKELY(fields_class->GetDexCache() == dex_cache)) {
130 // common case where the dex cache of both the referrer and the field are the same,
131 // no need to search the dex file
132 storage_idx = fields_class->GetDexTypeIndex();
133 } else {
134 // Search dex file for localized ssb index, may fail if field's class is a parent
135 // of the class mentioned in the dex file and there is no dex cache entry.
Ian Rogers08f1f502014-12-02 15:04:37 -0800136 std::string temp;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000137 const DexFile::StringId* string_id =
Ian Rogers08f1f502014-12-02 15:04:37 -0800138 dex_file->FindStringId(resolved_field->GetDeclaringClass()->GetDescriptor(&temp));
Vladimir Markobe0e5462014-02-26 11:24:15 +0000139 if (string_id != nullptr) {
140 const DexFile::TypeId* type_id =
141 dex_file->FindTypeId(dex_file->GetIndexForStringId(*string_id));
142 if (type_id != nullptr) {
143 // medium path, needs check of static storage base being initialized
144 storage_idx = dex_file->GetIndexForTypeId(*type_id);
145 }
146 }
147 }
148 if (storage_idx != DexFile::kDexNoIndex) {
Vladimir Markobe0e5462014-02-26 11:24:15 +0000149 *storage_index = storage_idx;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000150 return std::make_pair(true, !resolved_field->IsFinal());
151 }
152 }
153 }
154 // Conservative defaults.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000155 *storage_index = DexFile::kDexNoIndex;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000156 return std::make_pair(false, false);
157}
158
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100159inline bool CompilerDriver::IsStaticFieldInReferrerClass(mirror::Class* referrer_class,
160 mirror::ArtField* resolved_field) {
161 DCHECK(resolved_field->IsStatic());
162 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
163 return referrer_class == fields_class;
164}
165
166inline bool CompilerDriver::IsStaticFieldsClassInitialized(mirror::Class* referrer_class,
167 mirror::ArtField* resolved_field) {
168 DCHECK(resolved_field->IsStatic());
169 mirror::Class* fields_class = resolved_field->GetDeclaringClass();
170 return fields_class == referrer_class || fields_class->IsInitialized();
171}
172
Vladimir Markof096aad2014-01-23 15:51:58 +0000173inline mirror::ArtMethod* CompilerDriver::ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700174 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
175 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Vladimir Markof096aad2014-01-23 15:51:58 +0000176 uint32_t method_idx, InvokeType invoke_type) {
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700177 DCHECK_EQ(dex_cache->GetDexFile(), mUnit->GetDexFile());
178 DCHECK_EQ(class_loader.Get(), soa.Decode<mirror::ClassLoader*>(mUnit->GetClassLoader()));
Vladimir Markof096aad2014-01-23 15:51:58 +0000179 mirror::ArtMethod* resolved_method = mUnit->GetClassLinker()->ResolveMethod(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700180 *mUnit->GetDexFile(), method_idx, dex_cache, class_loader, NullHandle<mirror::ArtMethod>(),
181 invoke_type);
Vladimir Markof096aad2014-01-23 15:51:58 +0000182 DCHECK_EQ(resolved_method == nullptr, soa.Self()->IsExceptionPending());
183 if (UNLIKELY(resolved_method == nullptr)) {
184 // Clean up any exception left by type resolution.
185 soa.Self()->ClearException();
186 return nullptr;
187 }
188 if (UNLIKELY(resolved_method->CheckIncompatibleClassChange(invoke_type))) {
189 // Silently return nullptr on incompatible class change.
190 return nullptr;
191 }
192 return resolved_method;
193}
194
195inline void CompilerDriver::GetResolvedMethodDexFileLocation(
196 mirror::ArtMethod* resolved_method, const DexFile** declaring_dex_file,
197 uint16_t* declaring_class_idx, uint16_t* declaring_method_idx) {
198 mirror::Class* declaring_class = resolved_method->GetDeclaringClass();
199 *declaring_dex_file = declaring_class->GetDexCache()->GetDexFile();
200 *declaring_class_idx = declaring_class->GetDexTypeIndex();
201 *declaring_method_idx = resolved_method->GetDexMethodIndex();
202}
203
204inline uint16_t CompilerDriver::GetResolvedMethodVTableIndex(
205 mirror::ArtMethod* resolved_method, InvokeType type) {
206 if (type == kVirtual || type == kSuper) {
207 return resolved_method->GetMethodIndex();
208 } else if (type == kInterface) {
209 return resolved_method->GetDexMethodIndex();
210 } else {
211 return DexFile::kDexNoIndex16;
212 }
213}
214
215inline int CompilerDriver::IsFastInvoke(
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700216 ScopedObjectAccess& soa, Handle<mirror::DexCache> dex_cache,
217 Handle<mirror::ClassLoader> class_loader, const DexCompilationUnit* mUnit,
Vladimir Markof096aad2014-01-23 15:51:58 +0000218 mirror::Class* referrer_class, mirror::ArtMethod* resolved_method, InvokeType* invoke_type,
219 MethodReference* target_method, const MethodReference* devirt_target,
220 uintptr_t* direct_code, uintptr_t* direct_method) {
221 // Don't try to fast-path if we don't understand the caller's class.
222 if (UNLIKELY(referrer_class == nullptr)) {
223 return 0;
224 }
225 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
226 if (UNLIKELY(!referrer_class->CanAccessResolvedMethod(methods_class, resolved_method,
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700227 dex_cache.Get(),
Vladimir Markof096aad2014-01-23 15:51:58 +0000228 target_method->dex_method_index))) {
229 return 0;
230 }
231
232 // Sharpen a virtual call into a direct call when the target is known not to have been
233 // overridden (ie is final).
234 bool can_sharpen_virtual_based_on_type =
235 (*invoke_type == kVirtual) && (resolved_method->IsFinal() || methods_class->IsFinal());
236 // For invoke-super, ensure the vtable index will be correct to dispatch in the vtable of
237 // the super class.
238 bool can_sharpen_super_based_on_type = (*invoke_type == kSuper) &&
239 (referrer_class != methods_class) && referrer_class->IsSubClass(methods_class) &&
Mingyao Yang2cdbad72014-07-16 10:44:41 -0700240 resolved_method->GetMethodIndex() < methods_class->GetVTableLength() &&
Vladimir Marko920506d2014-11-18 14:47:31 +0000241 (methods_class->GetVTableEntry(resolved_method->GetMethodIndex()) == resolved_method) &&
242 !resolved_method->IsAbstract();
Vladimir Markof096aad2014-01-23 15:51:58 +0000243
244 if (can_sharpen_virtual_based_on_type || can_sharpen_super_based_on_type) {
245 // Sharpen a virtual call into a direct call. The method_idx is into referrer's
246 // dex cache, check that this resolved method is where we expect it.
247 CHECK(target_method->dex_file == mUnit->GetDexFile());
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700248 DCHECK(dex_cache.Get() == mUnit->GetClassLinker()->FindDexCache(*mUnit->GetDexFile()));
Vladimir Markof096aad2014-01-23 15:51:58 +0000249 CHECK(referrer_class->GetDexCache()->GetResolvedMethod(target_method->dex_method_index) ==
250 resolved_method) << PrettyMethod(resolved_method);
251 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700252 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
253 kDirect, // Sharp type
254 false, // The dex cache is guaranteed to be available
255 referrer_class, resolved_method,
256 /*out*/&stats_flags,
257 target_method,
258 /*out*/direct_code,
259 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000260 DCHECK_NE(*invoke_type, kSuper) << PrettyMethod(resolved_method);
261 if (*invoke_type == kDirect) {
262 stats_flags |= kFlagsMethodResolvedVirtualMadeDirect;
263 }
264 return stats_flags;
265 }
266
267 if ((*invoke_type == kVirtual || *invoke_type == kInterface) && devirt_target != nullptr) {
268 // Post-verification callback recorded a more precise invoke target based on its type info.
269 mirror::ArtMethod* called_method;
270 ClassLinker* class_linker = mUnit->GetClassLinker();
271 if (LIKELY(devirt_target->dex_file == mUnit->GetDexFile())) {
272 called_method = class_linker->ResolveMethod(*devirt_target->dex_file,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700273 devirt_target->dex_method_index, dex_cache,
274 class_loader, NullHandle<mirror::ArtMethod>(),
275 kVirtual);
Vladimir Markof096aad2014-01-23 15:51:58 +0000276 } else {
Mathieu Chartiereb8167a2014-05-07 15:43:14 -0700277 StackHandleScope<1> hs(soa.Self());
278 Handle<mirror::DexCache> target_dex_cache(
279 hs.NewHandle(class_linker->FindDexCache(*devirt_target->dex_file)));
Vladimir Markof096aad2014-01-23 15:51:58 +0000280 called_method = class_linker->ResolveMethod(*devirt_target->dex_file,
281 devirt_target->dex_method_index,
Mathieu Chartier0cd81352014-05-22 16:48:55 -0700282 target_dex_cache, class_loader,
283 NullHandle<mirror::ArtMethod>(), kVirtual);
Vladimir Markof096aad2014-01-23 15:51:58 +0000284 }
285 CHECK(called_method != NULL);
286 CHECK(!called_method->IsAbstract());
287 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700288 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
289 kDirect, // Sharp type
290 true, // The dex cache may not be available
291 referrer_class, called_method,
292 /*out*/&stats_flags,
293 target_method,
294 /*out*/direct_code,
295 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000296 DCHECK_NE(*invoke_type, kSuper);
297 if (*invoke_type == kDirect) {
298 stats_flags |= kFlagsMethodResolvedPreciseTypeDevirtualization;
299 }
300 return stats_flags;
301 }
302
303 if (UNLIKELY(*invoke_type == kSuper)) {
304 // Unsharpened super calls are suspicious so go slow-path.
305 return 0;
306 }
307
308 // Sharpening failed so generate a regular resolved method dispatch.
309 int stats_flags = kFlagMethodResolved;
Igor Murashkind6dee672014-10-16 18:36:16 -0700310 GetCodeAndMethodForDirectCall(/*out*/invoke_type,
311 *invoke_type, // Sharp type
312 false, // The dex cache is guaranteed to be available
313 referrer_class, resolved_method,
314 /*out*/&stats_flags,
315 target_method,
316 /*out*/direct_code,
317 /*out*/direct_method);
Vladimir Markof096aad2014-01-23 15:51:58 +0000318 return stats_flags;
319}
320
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100321inline bool CompilerDriver::IsMethodsClassInitialized(mirror::Class* referrer_class,
322 mirror::ArtMethod* resolved_method) {
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000323 if (!resolved_method->IsStatic()) {
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100324 return true;
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000325 }
326 mirror::Class* methods_class = resolved_method->GetDeclaringClass();
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100327 return methods_class == referrer_class || methods_class->IsInitialized();
Vladimir Marko9820b7c2014-01-02 16:40:37 +0000328}
329
Vladimir Markobe0e5462014-02-26 11:24:15 +0000330} // namespace art
331
332#endif // ART_COMPILER_DRIVER_COMPILER_DRIVER_INL_H_