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