Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 1 | /* |
| 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 22 | #include "dex_instruction_utils.h" |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 23 | #include "offsets.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | class CompilerDriver; |
| 28 | class 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 | |
| 37 | class MirFieldInfo { |
| 38 | public: |
| 39 | uint16_t FieldIndex() const { |
| 40 | return field_idx_; |
| 41 | } |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 42 | void SetFieldIndex(uint16_t field_idx) { |
| 43 | field_idx_ = field_idx; |
| 44 | } |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 45 | |
| 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 57 | void SetDeclaringDexFile(const DexFile* dex_file) { |
| 58 | declaring_dex_file_ = dex_file; |
| 59 | } |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 60 | |
| 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 73 | // IGET_QUICK, IGET_BYTE_QUICK, ... |
| 74 | bool IsQuickened() const { |
| 75 | return (flags_ & kFlagIsQuickened) != 0u; |
| 76 | } |
| 77 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 78 | DexMemAccessType MemAccessType() const { |
| 79 | return static_cast<DexMemAccessType>((flags_ >> kBitMemAccessTypeBegin) & kMemAccessTypeMask); |
| 80 | } |
| 81 | |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 82 | 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 Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 90 | protected: |
| 91 | enum { |
| 92 | kBitIsStatic = 0, |
| 93 | kBitIsVolatile, |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 94 | kBitIsQuickened, |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 95 | kBitMemAccessTypeBegin, |
| 96 | kBitMemAccessTypeEnd = kBitMemAccessTypeBegin + 3, // 3 bits for raw type. |
| 97 | kFieldInfoBitEnd = kBitMemAccessTypeEnd |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 98 | }; |
| 99 | static constexpr uint16_t kFlagIsVolatile = 1u << kBitIsVolatile; |
| 100 | static constexpr uint16_t kFlagIsStatic = 1u << kBitIsStatic; |
Mathieu Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 101 | static constexpr uint16_t kFlagIsQuickened = 1u << kBitIsQuickened; |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 102 | static constexpr uint16_t kMemAccessTypeMask = 7u; |
| 103 | static_assert((1u << (kBitMemAccessTypeEnd - kBitMemAccessTypeBegin)) - 1u == kMemAccessTypeMask, |
| 104 | "Invalid raw type mask"); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 105 | |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 106 | MirFieldInfo(uint16_t field_idx, uint16_t flags, DexMemAccessType type) |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 107 | : field_idx_(field_idx), |
Vladimir Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 108 | flags_(flags | static_cast<uint16_t>(type) << kBitMemAccessTypeBegin), |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 109 | 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 | |
| 131 | class MirIFieldLoweringInfo : public MirFieldInfo { |
| 132 | public: |
| 133 | // For each requested instance field retrieve the field's declaring location (dex file, class |
Vladimir Marko | a24122d | 2014-03-07 10:18:14 +0000 | [diff] [blame] | 134 | // index and field index) and volatility and compute whether we can fast path the access |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 135 | // 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 141 | 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 Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 145 | 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 Chartier | e5f13e5 | 2015-02-24 09:37:21 -0800 | [diff] [blame] | 160 | void CheckEquals(const MirIFieldLoweringInfo& other) const { |
| 161 | MirFieldInfo::CheckEquals(other); |
| 162 | CHECK_EQ(field_offset_.Uint32Value(), other.field_offset_.Uint32Value()); |
| 163 | } |
| 164 | |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 165 | private: |
| 166 | enum { |
| 167 | kBitFastGet = kFieldInfoBitEnd, |
| 168 | kBitFastPut, |
| 169 | kIFieldLoweringInfoBitEnd |
| 170 | }; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 171 | static_assert(kIFieldLoweringInfoBitEnd <= 16, "Too many flags"); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 172 | 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 Marko | 7baa6f8 | 2014-10-09 18:01:24 +0100 | [diff] [blame] | 178 | friend class NullCheckEliminationTest; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 179 | friend class GlobalValueNumberingTest; |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 180 | friend class GvnDeadCodeEliminationTest; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 181 | friend class LocalValueNumberingTest; |
| 182 | }; |
| 183 | |
| 184 | class MirSFieldLoweringInfo : public MirFieldInfo { |
| 185 | public: |
| 186 | // For each requested static field retrieve the field's declaring location (dex file, class |
Vladimir Marko | a24122d | 2014-03-07 10:18:14 +0000 | [diff] [blame] | 187 | // index and field index) and volatility and compute whether we can fast path the access with |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 188 | // 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 Marko | af6925b | 2014-10-31 16:37:32 +0000 | [diff] [blame] | 197 | explicit MirSFieldLoweringInfo(uint16_t field_idx, DexMemAccessType type) |
| 198 | : MirFieldInfo(field_idx, kFlagIsVolatile | kFlagIsStatic, type), |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 199 | 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 Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 215 | bool IsClassInitialized() const { |
| 216 | return (flags_ & kFlagClassIsInitialized) != 0u; |
| 217 | } |
| 218 | |
| 219 | bool IsClassInDexCache() const { |
| 220 | return (flags_ & kFlagClassIsInDexCache) != 0u; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 221 | } |
| 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 Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 236 | kBitClassIsInitialized, |
| 237 | kBitClassIsInDexCache, |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 238 | kSFieldLoweringInfoBitEnd |
| 239 | }; |
Andreas Gampe | 785d2f2 | 2014-11-03 22:57:30 -0800 | [diff] [blame] | 240 | static_assert(kSFieldLoweringInfoBitEnd <= 16, "Too many flags"); |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 241 | static constexpr uint16_t kFlagFastGet = 1u << kBitFastGet; |
| 242 | static constexpr uint16_t kFlagFastPut = 1u << kBitFastPut; |
| 243 | static constexpr uint16_t kFlagIsReferrersClass = 1u << kBitIsReferrersClass; |
Vladimir Marko | 66c6d7b | 2014-10-16 15:41:48 +0100 | [diff] [blame] | 244 | static constexpr uint16_t kFlagClassIsInitialized = 1u << kBitClassIsInitialized; |
| 245 | static constexpr uint16_t kFlagClassIsInDexCache = 1u << kBitClassIsInDexCache; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 246 | |
| 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 Marko | bfea9c2 | 2014-01-17 17:49:33 +0000 | [diff] [blame] | 253 | friend class ClassInitCheckEliminationTest; |
Vladimir Marko | 95a0597 | 2014-05-30 10:01:32 +0100 | [diff] [blame] | 254 | friend class GlobalValueNumberingTest; |
Vladimir Marko | 7a01dc2 | 2015-01-02 17:00:44 +0000 | [diff] [blame] | 255 | friend class GvnDeadCodeEliminationTest; |
Vladimir Marko | be0e546 | 2014-02-26 11:24:15 +0000 | [diff] [blame] | 256 | friend class LocalValueNumberingTest; |
| 257 | }; |
| 258 | |
| 259 | } // namespace art |
| 260 | |
| 261 | #endif // ART_COMPILER_DEX_MIR_FIELD_INFO_H_ |