blob: ca5695893e129e0dcb6af000aaa72676b25fae77 [file] [log] [blame]
Vladimir Markobe0e5462014-02-26 11:24:15 +00001/*
2 * Copyright (C) 2014 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_DEX_MIR_FIELD_INFO_H_
18#define ART_COMPILER_DEX_MIR_FIELD_INFO_H_
19
20#include "base/macros.h"
21#include "dex_file.h"
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080022#include "dex_instruction_utils.h"
Vladimir Markobe0e5462014-02-26 11:24:15 +000023#include "offsets.h"
24
25namespace art {
26
27class CompilerDriver;
28class DexCompilationUnit;
29
30/*
31 * Field info is calculated from the perspective of the compilation unit that accesses
32 * the field and stored in that unit's MIRGraph. Therefore it does not need to reference the
33 * dex file or method for which it has been calculated. However, we do store the declaring
34 * field index, class index and dex file of the resolved field to help distinguish between fields.
35 */
36
37class MirFieldInfo {
38 public:
39 uint16_t FieldIndex() const {
40 return field_idx_;
41 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080042 void SetFieldIndex(uint16_t field_idx) {
43 field_idx_ = field_idx;
44 }
Vladimir Markobe0e5462014-02-26 11:24:15 +000045
46 bool IsStatic() const {
47 return (flags_ & kFlagIsStatic) != 0u;
48 }
49
50 bool IsResolved() const {
51 return declaring_dex_file_ != nullptr;
52 }
53
54 const DexFile* DeclaringDexFile() const {
55 return declaring_dex_file_;
56 }
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080057 void SetDeclaringDexFile(const DexFile* dex_file) {
58 declaring_dex_file_ = dex_file;
59 }
Vladimir Markobe0e5462014-02-26 11:24:15 +000060
61 uint16_t DeclaringClassIndex() const {
62 return declaring_class_idx_;
63 }
64
65 uint16_t DeclaringFieldIndex() const {
66 return declaring_field_idx_;
67 }
68
69 bool IsVolatile() const {
70 return (flags_ & kFlagIsVolatile) != 0u;
71 }
72
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080073 // IGET_QUICK, IGET_BYTE_QUICK, ...
74 bool IsQuickened() const {
75 return (flags_ & kFlagIsQuickened) != 0u;
76 }
77
Vladimir Markoaf6925b2014-10-31 16:37:32 +000078 DexMemAccessType MemAccessType() const {
79 return static_cast<DexMemAccessType>((flags_ >> kBitMemAccessTypeBegin) & kMemAccessTypeMask);
80 }
81
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080082 void CheckEquals(const MirFieldInfo& other) const {
83 CHECK_EQ(field_idx_, other.field_idx_);
84 CHECK_EQ(flags_, other.flags_);
85 CHECK_EQ(declaring_field_idx_, other.declaring_field_idx_);
86 CHECK_EQ(declaring_class_idx_, other.declaring_class_idx_);
87 CHECK_EQ(declaring_dex_file_, other.declaring_dex_file_);
88 }
89
Vladimir Markobe0e5462014-02-26 11:24:15 +000090 protected:
91 enum {
92 kBitIsStatic = 0,
93 kBitIsVolatile,
Mathieu Chartiere5f13e52015-02-24 09:37:21 -080094 kBitIsQuickened,
Vladimir Markoaf6925b2014-10-31 16:37:32 +000095 kBitMemAccessTypeBegin,
96 kBitMemAccessTypeEnd = kBitMemAccessTypeBegin + 3, // 3 bits for raw type.
97 kFieldInfoBitEnd = kBitMemAccessTypeEnd
Vladimir Markobe0e5462014-02-26 11:24:15 +000098 };
99 static constexpr uint16_t kFlagIsVolatile = 1u << kBitIsVolatile;
100 static constexpr uint16_t kFlagIsStatic = 1u << kBitIsStatic;
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800101 static constexpr uint16_t kFlagIsQuickened = 1u << kBitIsQuickened;
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000102 static constexpr uint16_t kMemAccessTypeMask = 7u;
103 static_assert((1u << (kBitMemAccessTypeEnd - kBitMemAccessTypeBegin)) - 1u == kMemAccessTypeMask,
104 "Invalid raw type mask");
Vladimir Markobe0e5462014-02-26 11:24:15 +0000105
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000106 MirFieldInfo(uint16_t field_idx, uint16_t flags, DexMemAccessType type)
Vladimir Markobe0e5462014-02-26 11:24:15 +0000107 : field_idx_(field_idx),
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000108 flags_(flags | static_cast<uint16_t>(type) << kBitMemAccessTypeBegin),
Vladimir Markobe0e5462014-02-26 11:24:15 +0000109 declaring_field_idx_(0u),
110 declaring_class_idx_(0u),
111 declaring_dex_file_(nullptr) {
112 }
113
114 // Make copy-ctor/assign/dtor protected to avoid slicing.
115 MirFieldInfo(const MirFieldInfo& other) = default;
116 MirFieldInfo& operator=(const MirFieldInfo& other) = default;
117 ~MirFieldInfo() = default;
118
119 // The field index in the compiling method's dex file.
120 uint16_t field_idx_;
121 // Flags, for volatility and derived class data.
122 uint16_t flags_;
123 // The field index in the dex file that defines field, 0 if unresolved.
124 uint16_t declaring_field_idx_;
125 // The type index of the class declaring the field, 0 if unresolved.
126 uint16_t declaring_class_idx_;
127 // The dex file that defines the class containing the field and the field, nullptr if unresolved.
128 const DexFile* declaring_dex_file_;
129};
130
131class MirIFieldLoweringInfo : public MirFieldInfo {
132 public:
133 // For each requested instance field retrieve the field's declaring location (dex file, class
Vladimir Markoa24122d2014-03-07 10:18:14 +0000134 // index and field index) and volatility and compute whether we can fast path the access
Vladimir Markobe0e5462014-02-26 11:24:15 +0000135 // with IGET/IPUT. For fast path fields, retrieve the field offset.
136 static void Resolve(CompilerDriver* compiler_driver, const DexCompilationUnit* mUnit,
137 MirIFieldLoweringInfo* field_infos, size_t count)
138 LOCKS_EXCLUDED(Locks::mutator_lock_);
139
140 // Construct an unresolved instance field lowering info.
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800141 explicit MirIFieldLoweringInfo(uint16_t field_idx, DexMemAccessType type, bool is_quickened)
142 : MirFieldInfo(field_idx,
143 kFlagIsVolatile | (is_quickened ? kFlagIsQuickened : 0u),
144 type), // Without kFlagIsStatic.
Vladimir Markobe0e5462014-02-26 11:24:15 +0000145 field_offset_(0u) {
146 }
147
148 bool FastGet() const {
149 return (flags_ & kFlagFastGet) != 0u;
150 }
151
152 bool FastPut() const {
153 return (flags_ & kFlagFastPut) != 0u;
154 }
155
156 MemberOffset FieldOffset() const {
157 return field_offset_;
158 }
159
Mathieu Chartiere5f13e52015-02-24 09:37:21 -0800160 void CheckEquals(const MirIFieldLoweringInfo& other) const {
161 MirFieldInfo::CheckEquals(other);
162 CHECK_EQ(field_offset_.Uint32Value(), other.field_offset_.Uint32Value());
163 }
164
Vladimir Markobe0e5462014-02-26 11:24:15 +0000165 private:
166 enum {
167 kBitFastGet = kFieldInfoBitEnd,
168 kBitFastPut,
169 kIFieldLoweringInfoBitEnd
170 };
Andreas Gampe785d2f22014-11-03 22:57:30 -0800171 static_assert(kIFieldLoweringInfoBitEnd <= 16, "Too many flags");
Vladimir Markobe0e5462014-02-26 11:24:15 +0000172 static constexpr uint16_t kFlagFastGet = 1u << kBitFastGet;
173 static constexpr uint16_t kFlagFastPut = 1u << kBitFastPut;
174
175 // The member offset of the field, 0u if unresolved.
176 MemberOffset field_offset_;
177
Vladimir Marko7baa6f82014-10-09 18:01:24 +0100178 friend class NullCheckEliminationTest;
Vladimir Marko95a05972014-05-30 10:01:32 +0100179 friend class GlobalValueNumberingTest;
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000180 friend class GvnDeadCodeEliminationTest;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000181 friend class LocalValueNumberingTest;
182};
183
184class MirSFieldLoweringInfo : public MirFieldInfo {
185 public:
186 // For each requested static field retrieve the field's declaring location (dex file, class
Vladimir Markoa24122d2014-03-07 10:18:14 +0000187 // index and field index) and volatility and compute whether we can fast path the access with
Vladimir Markobe0e5462014-02-26 11:24:15 +0000188 // IGET/IPUT. For fast path fields (at least for IGET), retrieve the information needed for
189 // the field access, i.e. the field offset, whether the field is in the same class as the
190 // method being compiled, whether the declaring class can be safely assumed to be initialized
191 // and the type index of the declaring class in the compiled method's dex file.
192 static void Resolve(CompilerDriver* compiler_driver, const DexCompilationUnit* mUnit,
193 MirSFieldLoweringInfo* field_infos, size_t count)
194 LOCKS_EXCLUDED(Locks::mutator_lock_);
195
196 // Construct an unresolved static field lowering info.
Vladimir Markoaf6925b2014-10-31 16:37:32 +0000197 explicit MirSFieldLoweringInfo(uint16_t field_idx, DexMemAccessType type)
198 : MirFieldInfo(field_idx, kFlagIsVolatile | kFlagIsStatic, type),
Vladimir Markobe0e5462014-02-26 11:24:15 +0000199 field_offset_(0u),
200 storage_index_(DexFile::kDexNoIndex) {
201 }
202
203 bool FastGet() const {
204 return (flags_ & kFlagFastGet) != 0u;
205 }
206
207 bool FastPut() const {
208 return (flags_ & kFlagFastPut) != 0u;
209 }
210
211 bool IsReferrersClass() const {
212 return (flags_ & kFlagIsReferrersClass) != 0u;
213 }
214
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100215 bool IsClassInitialized() const {
216 return (flags_ & kFlagClassIsInitialized) != 0u;
217 }
218
219 bool IsClassInDexCache() const {
220 return (flags_ & kFlagClassIsInDexCache) != 0u;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000221 }
222
223 MemberOffset FieldOffset() const {
224 return field_offset_;
225 }
226
227 uint32_t StorageIndex() const {
228 return storage_index_;
229 }
230
231 private:
232 enum {
233 kBitFastGet = kFieldInfoBitEnd,
234 kBitFastPut,
235 kBitIsReferrersClass,
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100236 kBitClassIsInitialized,
237 kBitClassIsInDexCache,
Vladimir Markobe0e5462014-02-26 11:24:15 +0000238 kSFieldLoweringInfoBitEnd
239 };
Andreas Gampe785d2f22014-11-03 22:57:30 -0800240 static_assert(kSFieldLoweringInfoBitEnd <= 16, "Too many flags");
Vladimir Markobe0e5462014-02-26 11:24:15 +0000241 static constexpr uint16_t kFlagFastGet = 1u << kBitFastGet;
242 static constexpr uint16_t kFlagFastPut = 1u << kBitFastPut;
243 static constexpr uint16_t kFlagIsReferrersClass = 1u << kBitIsReferrersClass;
Vladimir Marko66c6d7b2014-10-16 15:41:48 +0100244 static constexpr uint16_t kFlagClassIsInitialized = 1u << kBitClassIsInitialized;
245 static constexpr uint16_t kFlagClassIsInDexCache = 1u << kBitClassIsInDexCache;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000246
247 // The member offset of the field, 0u if unresolved.
248 MemberOffset field_offset_;
249 // The type index of the declaring class in the compiling method's dex file,
250 // -1 if the field is unresolved or there's no appropriate TypeId in that dex file.
251 uint32_t storage_index_;
252
Vladimir Markobfea9c22014-01-17 17:49:33 +0000253 friend class ClassInitCheckEliminationTest;
Vladimir Marko95a05972014-05-30 10:01:32 +0100254 friend class GlobalValueNumberingTest;
Vladimir Marko7a01dc22015-01-02 17:00:44 +0000255 friend class GvnDeadCodeEliminationTest;
Vladimir Markobe0e5462014-02-26 11:24:15 +0000256 friend class LocalValueNumberingTest;
257};
258
259} // namespace art
260
261#endif // ART_COMPILER_DEX_MIR_FIELD_INFO_H_