Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +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_REG_STORAGE_H_ |
| 18 | #define ART_COMPILER_DEX_REG_STORAGE_H_ |
| 19 | |
| 20 | |
| 21 | namespace art { |
| 22 | |
| 23 | /* |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 24 | * 16-bit representation of the physical register container holding a Dalvik value. |
| 25 | * The encoding allows up to 32 physical elements per storage class, and supports eight |
| 26 | * register container shapes. |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 27 | * |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 28 | * [V] [D] [HHHHH] [SSS] [F] [LLLLL] |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 29 | * |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 30 | * [LLLLL] |
| 31 | * Physical register number for the low or solo register. |
| 32 | * 0..31 |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 33 | * |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 34 | * [F] |
| 35 | * Describes type of the [LLLLL] register. |
| 36 | * 0: Core |
| 37 | * 1: Floating point |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 38 | * |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 39 | * [SSS] |
| 40 | * Shape of the register container. |
| 41 | * 000: Invalid |
| 42 | * 001: 32-bit solo register |
| 43 | * 010: 64-bit solo register |
| 44 | * 011: 64-bit pair consisting of two 32-bit solo registers |
| 45 | * 100: 128-bit solo register |
| 46 | * 101: 256-bit solo register |
| 47 | * 110: 512-bit solo register |
| 48 | * 111: 1024-bit solo register |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 49 | * |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 50 | * [HHHHH] |
| 51 | * Physical register number of the high register (valid only for register pair). |
| 52 | * 0..31 |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 53 | * |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 54 | * [D] |
| 55 | * Describes type of the [HHHHH] register (valid only for register pair). |
| 56 | * 0: Core |
| 57 | * 1: Floating point |
| 58 | * |
| 59 | * [V] |
| 60 | * 0 -> Invalid |
| 61 | * 1 -> Valid |
| 62 | * |
| 63 | * Note that in all non-invalid cases, we can determine if the storage is floating point |
| 64 | * by testing bit 6. Though a mismatch appears to be permitted by the format, the [F][D] values |
| 65 | * from each half of a pair must match (this allows the high and low regs of a pair to be more |
| 66 | * easily individually manipulated). |
| 67 | * |
| 68 | * On some target architectures, the same underlying physical register container can be given |
| 69 | * different views. For example, Arm's 32-bit single-precision floating point registers |
| 70 | * s2 and s3 map to the low and high halves of double-precision d1. Similarly, X86's xmm3 |
| 71 | * vector register can be viewed as 32-bit, 64-bit, 128-bit, etc. In these cases the use of |
| 72 | * one view will affect the other views. The RegStorage class does not concern itself |
| 73 | * with potential aliasing. That will be done using the associated RegisterInfo struct. |
| 74 | * Distinct RegStorage elements should be created for each view of a physical register |
| 75 | * container. The management of the aliased physical elements will be handled via RegisterInfo |
| 76 | * records. |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 77 | */ |
| 78 | |
| 79 | class RegStorage { |
| 80 | public: |
| 81 | enum RegStorageKind { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 82 | kValidMask = 0x8000, |
| 83 | kValid = 0x8000, |
| 84 | kInvalid = 0x0000, |
| 85 | kShapeMask = 0x01c0, |
| 86 | k32BitSolo = 0x0040, |
| 87 | k64BitSolo = 0x0080, |
| 88 | k64BitPair = 0x00c0, |
| 89 | k128BitSolo = 0x0100, |
| 90 | k256BitSolo = 0x0140, |
| 91 | k512BitSolo = 0x0180, |
| 92 | k1024BitSolo = 0x01c0, |
| 93 | k64BitMask = 0x0180, |
| 94 | k64Bits = 0x0080, |
| 95 | kShapeTypeMask = 0x01e0, |
| 96 | kFloatingPoint = 0x0020, |
| 97 | kCoreRegister = 0x0000, |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 98 | }; |
| 99 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 100 | static const uint16_t kRegValMask = 0x01ff; // Num, type and shape. |
| 101 | static const uint16_t kRegTypeMask = 0x003f; // Num and type. |
| 102 | static const uint16_t kRegNumMask = 0x001f; // Num only. |
| 103 | static const uint16_t kMaxRegs = kRegValMask + 1; |
| 104 | // TODO: deprecate use of kInvalidRegVal and speed up GetReg(). |
| 105 | static const uint16_t kInvalidRegVal = 0x01ff; |
| 106 | static const uint16_t kHighRegShift = 9; |
| 107 | static const uint16_t kShapeMaskShift = 6; |
| 108 | static const uint16_t kHighRegMask = (kRegTypeMask << kHighRegShift); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 109 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 110 | // Reg is [F][LLLLL], will override any existing shape and use rs_kind. |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 111 | RegStorage(RegStorageKind rs_kind, int reg) { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 112 | DCHECK_NE(rs_kind, k64BitPair); |
| 113 | DCHECK_EQ(rs_kind & ~kShapeMask, 0); |
| 114 | reg_ = kValid | rs_kind | (reg & kRegTypeMask); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 115 | } |
| 116 | RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg) { |
| 117 | DCHECK_EQ(rs_kind, k64BitPair); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 118 | DCHECK_EQ(low_reg & kFloatingPoint, high_reg & kFloatingPoint); |
| 119 | reg_ = kValid | rs_kind | ((high_reg & kRegTypeMask) << kHighRegShift) | (low_reg & kRegTypeMask); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 120 | } |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 121 | constexpr explicit RegStorage(uint16_t val) : reg_(val) {} |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 122 | RegStorage() : reg_(kInvalid) {} |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 123 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 124 | bool operator==(const RegStorage rhs) const { |
| 125 | return (reg_ == rhs.GetRawBits()); |
| 126 | } |
| 127 | |
| 128 | bool operator!=(const RegStorage rhs) const { |
| 129 | return (reg_ != rhs.GetRawBits()); |
| 130 | } |
| 131 | |
| 132 | bool Valid() const { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 133 | return ((reg_ & kValidMask) == kValid); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | bool Is32Bit() const { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 137 | return ((reg_ & kShapeMask) == k32BitSolo); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 138 | } |
| 139 | |
| 140 | bool Is64Bit() const { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 141 | return ((reg_ & k64BitMask) == k64Bits); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 142 | } |
| 143 | |
| 144 | bool IsPair() const { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 145 | return ((reg_ & kShapeMask) == k64BitPair); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 146 | } |
| 147 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 148 | bool IsFloat() const { |
| 149 | DCHECK(Valid()); |
| 150 | return ((reg_ & kFloatingPoint) == kFloatingPoint); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 151 | } |
| 152 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 153 | bool IsDouble() const { |
| 154 | DCHECK(Valid()); |
| 155 | return (reg_ & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits); |
| 156 | } |
| 157 | |
| 158 | bool IsSingle() const { |
| 159 | DCHECK(Valid()); |
| 160 | return (reg_ & (kFloatingPoint | k64BitMask)) == kFloatingPoint; |
| 161 | } |
| 162 | |
| 163 | static bool IsFloat(uint16_t reg) { |
| 164 | return ((reg & kFloatingPoint) == kFloatingPoint); |
| 165 | } |
| 166 | |
| 167 | static bool IsDouble(uint16_t reg) { |
| 168 | return (reg & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits); |
| 169 | } |
| 170 | |
| 171 | static bool IsSingle(uint16_t reg) { |
| 172 | return (reg & (kFloatingPoint | k64BitMask)) == kFloatingPoint; |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 173 | } |
| 174 | |
| 175 | // Used to retrieve either the low register of a pair, or the only register. |
| 176 | int GetReg() const { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 177 | DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 178 | return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal; |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 179 | } |
| 180 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 181 | // Sets shape, type and num of solo. |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 182 | void SetReg(int reg) { |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 183 | DCHECK(Valid()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 184 | DCHECK(!IsPair()); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 185 | reg_ = (reg_ & ~kRegValMask) | reg; |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 186 | } |
| 187 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 188 | // Set the reg number and type only, target remain 64-bit pair. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 189 | void SetLowReg(int reg) { |
| 190 | DCHECK(IsPair()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 191 | reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 192 | } |
| 193 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 194 | // Retrieve the least significant register of a pair and return as 32-bit solo. |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 195 | int GetLowReg() const { |
| 196 | DCHECK(IsPair()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 197 | return ((reg_ & kRegTypeMask) | k32BitSolo); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 198 | } |
| 199 | |
| 200 | // Create a stand-alone RegStorage from the low reg of a pair. |
| 201 | RegStorage GetLow() const { |
| 202 | DCHECK(IsPair()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 203 | return RegStorage(k32BitSolo, reg_ & kRegTypeMask); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | // Retrieve the most significant register of a pair. |
| 207 | int GetHighReg() const { |
| 208 | DCHECK(IsPair()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 209 | return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift); |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 210 | } |
| 211 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 212 | // Create a stand-alone RegStorage from the high reg of a pair. |
| 213 | RegStorage GetHigh() const { |
| 214 | DCHECK(IsPair()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 215 | return RegStorage(kValid | GetHighReg()); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 216 | } |
| 217 | |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 218 | void SetHighReg(int reg) { |
| 219 | DCHECK(IsPair()); |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 220 | reg_ = (reg_ & ~kHighRegMask) | ((reg & kRegTypeMask) << kHighRegShift); |
| 221 | } |
| 222 | |
| 223 | // Return the register number of low or solo. |
| 224 | int GetRegNum() const { |
| 225 | return reg_ & kRegNumMask; |
| 226 | } |
| 227 | |
| 228 | // Aliased double to low single. |
| 229 | RegStorage DoubleToLowSingle() const { |
| 230 | DCHECK(IsDouble()); |
| 231 | return FloatSolo32(GetRegNum() << 1); |
| 232 | } |
| 233 | |
| 234 | // Aliased double to high single. |
| 235 | RegStorage DoubleToHighSingle() const { |
| 236 | DCHECK(IsDouble()); |
| 237 | return FloatSolo32((GetRegNum() << 1) + 1); |
| 238 | } |
| 239 | |
| 240 | // Single to aliased double. |
| 241 | RegStorage SingleToDouble() const { |
| 242 | DCHECK(IsSingle()); |
| 243 | return FloatSolo64(GetRegNum() >> 1); |
| 244 | } |
| 245 | |
| 246 | // Is register number in 0..7? |
| 247 | bool Low8() const { |
| 248 | return GetRegNum() < 8; |
| 249 | } |
| 250 | |
| 251 | // Is register number in 0..3? |
| 252 | bool Low4() const { |
| 253 | return GetRegNum() < 4; |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 254 | } |
| 255 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 256 | // Combine 2 32-bit solo regs into a pair. |
| 257 | static RegStorage MakeRegPair(RegStorage low, RegStorage high) { |
| 258 | DCHECK(!low.IsPair()); |
| 259 | DCHECK(low.Is32Bit()); |
| 260 | DCHECK(!high.IsPair()); |
| 261 | DCHECK(high.Is32Bit()); |
| 262 | return RegStorage(k64BitPair, low.GetReg(), high.GetReg()); |
| 263 | } |
| 264 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 265 | static bool SameRegType(RegStorage reg1, RegStorage reg2) { |
| 266 | return (reg1.IsDouble() == reg2.IsDouble()) && (reg1.IsSingle() == reg2.IsSingle()); |
| 267 | } |
| 268 | |
| 269 | static bool SameRegType(int reg1, int reg2) { |
| 270 | return (IsDouble(reg1) == IsDouble(reg2)) && (IsSingle(reg1) == IsSingle(reg2)); |
| 271 | } |
| 272 | |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 273 | // Create a 32-bit solo. |
| 274 | static RegStorage Solo32(int reg_num) { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 275 | return RegStorage(k32BitSolo, reg_num & kRegTypeMask); |
| 276 | } |
| 277 | |
| 278 | // Create a floating point 32-bit solo. |
| 279 | static RegStorage FloatSolo32(int reg_num) { |
| 280 | return RegStorage(k32BitSolo, (reg_num & kRegNumMask) | kFloatingPoint); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 281 | } |
| 282 | |
| 283 | // Create a 64-bit solo. |
| 284 | static RegStorage Solo64(int reg_num) { |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 285 | return RegStorage(k64BitSolo, reg_num & kRegTypeMask); |
| 286 | } |
| 287 | |
| 288 | // Create a floating point 64-bit solo. |
| 289 | static RegStorage FloatSolo64(int reg_num) { |
| 290 | return RegStorage(k64BitSolo, (reg_num & kRegNumMask) | kFloatingPoint); |
buzbee | 2700f7e | 2014-03-07 09:46:20 -0800 | [diff] [blame] | 291 | } |
| 292 | |
| 293 | static RegStorage InvalidReg() { |
| 294 | return RegStorage(kInvalid); |
| 295 | } |
| 296 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 297 | static uint16_t RegNum(int raw_reg_bits) { |
| 298 | return raw_reg_bits & kRegNumMask; |
| 299 | } |
| 300 | |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 301 | int GetRawBits() const { |
| 302 | return reg_; |
| 303 | } |
| 304 | |
buzbee | 091cc40 | 2014-03-31 10:14:40 -0700 | [diff] [blame] | 305 | size_t StorageSize() { |
| 306 | switch (reg_ & kShapeMask) { |
| 307 | case kInvalid: return 0; |
| 308 | case k32BitSolo: return 4; |
| 309 | case k64BitSolo: return 8; |
| 310 | case k64BitPair: return 8; // Is this useful? Might want to disallow taking size of pair. |
| 311 | case k128BitSolo: return 16; |
| 312 | case k256BitSolo: return 32; |
| 313 | case k512BitSolo: return 64; |
| 314 | case k1024BitSolo: return 128; |
| 315 | default: LOG(FATAL) << "Unexpected shap"; |
| 316 | } |
| 317 | return 0; |
| 318 | } |
| 319 | |
Bill Buzbee | 00e1ec6 | 2014-02-27 23:44:13 +0000 | [diff] [blame] | 320 | private: |
| 321 | uint16_t reg_; |
| 322 | }; |
| 323 | |
| 324 | } // namespace art |
| 325 | |
| 326 | #endif // ART_COMPILER_DEX_REG_STORAGE_H_ |