blob: 979f516168bdbc1397cd2aae7b3ea27b5bb0af80 [file] [log] [blame]
Bill Buzbee00e1ec62014-02-27 23:44:13 +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_REG_STORAGE_H_
18#define ART_COMPILER_DEX_REG_STORAGE_H_
19
20
21namespace art {
22
23/*
buzbee091cc402014-03-31 10:14:40 -070024 * 16-bit representation of the physical register container holding a Dalvik value.
buzbeed111c6e2014-05-11 21:09:53 -070025 * The encoding allows up to 64 physical elements per storage class, and supports eight
buzbee091cc402014-03-31 10:14:40 -070026 * register container shapes.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000027 *
buzbeed111c6e2014-05-11 21:09:53 -070028 * [V] [HHHHH] [SSS] [F] [LLLLLL]
Bill Buzbee00e1ec62014-02-27 23:44:13 +000029 *
buzbeed111c6e2014-05-11 21:09:53 -070030 * [LLLLLL]
buzbee091cc402014-03-31 10:14:40 -070031 * Physical register number for the low or solo register.
buzbeed111c6e2014-05-11 21:09:53 -070032 * 0..63
Bill Buzbee00e1ec62014-02-27 23:44:13 +000033 *
buzbee091cc402014-03-31 10:14:40 -070034 * [F]
35 * Describes type of the [LLLLL] register.
36 * 0: Core
37 * 1: Floating point
Bill Buzbee00e1ec62014-02-27 23:44:13 +000038 *
buzbee091cc402014-03-31 10:14:40 -070039 * [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 Buzbee00e1ec62014-02-27 23:44:13 +000049 *
buzbee091cc402014-03-31 10:14:40 -070050 * [HHHHH]
51 * Physical register number of the high register (valid only for register pair).
52 * 0..31
Bill Buzbee00e1ec62014-02-27 23:44:13 +000053 *
buzbee091cc402014-03-31 10:14:40 -070054 * [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
buzbeed111c6e2014-05-11 21:09:53 -070059 * 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.
buzbee091cc402014-03-31 10:14:40 -070061 *
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 Buzbee00e1ec62014-02-27 23:44:13 +000071 */
72
73class RegStorage {
74 public:
75 enum RegStorageKind {
buzbee091cc402014-03-31 10:14:40 -070076 kValidMask = 0x8000,
77 kValid = 0x8000,
78 kInvalid = 0x0000,
buzbeed111c6e2014-05-11 21:09:53 -070079 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,
buzbee091cc402014-03-31 10:14:40 -070091 kCoreRegister = 0x0000,
Bill Buzbee00e1ec62014-02-27 23:44:13 +000092 };
93
buzbeed111c6e2014-05-11 21:09:53 -070094 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
buzbee091cc402014-03-31 10:14:40 -070098 static const uint16_t kMaxRegs = kRegValMask + 1;
buzbeed111c6e2014-05-11 21:09:53 -070099 // 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 Buzbee00e1ec62014-02-27 23:44:13 +0000103
buzbee091cc402014-03-31 10:14:40 -0700104 // Reg is [F][LLLLL], will override any existing shape and use rs_kind.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000105 RegStorage(RegStorageKind rs_kind, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700106 DCHECK_NE(rs_kind, k64BitPair);
107 DCHECK_EQ(rs_kind & ~kShapeMask, 0);
108 reg_ = kValid | rs_kind | (reg & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000109 }
110 RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg) {
111 DCHECK_EQ(rs_kind, k64BitPair);
buzbee091cc402014-03-31 10:14:40 -0700112 DCHECK_EQ(low_reg & kFloatingPoint, high_reg & kFloatingPoint);
buzbeed111c6e2014-05-11 21:09:53 -0700113 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 Buzbee00e1ec62014-02-27 23:44:13 +0000116 }
buzbee091cc402014-03-31 10:14:40 -0700117 constexpr explicit RegStorage(uint16_t val) : reg_(val) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000118 RegStorage() : reg_(kInvalid) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000119
buzbee2700f7e2014-03-07 09:46:20 -0800120 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 {
buzbee091cc402014-03-31 10:14:40 -0700129 return ((reg_ & kValidMask) == kValid);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000130 }
131
132 bool Is32Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700133 return ((reg_ & kShapeMask) == k32BitSolo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000134 }
135
136 bool Is64Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700137 return ((reg_ & k64BitMask) == k64Bits);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000138 }
139
140 bool IsPair() const {
buzbee091cc402014-03-31 10:14:40 -0700141 return ((reg_ & kShapeMask) == k64BitPair);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000142 }
143
buzbee091cc402014-03-31 10:14:40 -0700144 bool IsFloat() const {
145 DCHECK(Valid());
146 return ((reg_ & kFloatingPoint) == kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000147 }
148
buzbee091cc402014-03-31 10:14:40 -0700149 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 Buzbee00e1ec62014-02-27 23:44:13 +0000169 }
170
171 // Used to retrieve either the low register of a pair, or the only register.
172 int GetReg() const {
buzbee091cc402014-03-31 10:14:40 -0700173 DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_;
buzbee2700f7e2014-03-07 09:46:20 -0800174 return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000175 }
176
buzbee091cc402014-03-31 10:14:40 -0700177 // Sets shape, type and num of solo.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000178 void SetReg(int reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800179 DCHECK(Valid());
buzbee091cc402014-03-31 10:14:40 -0700180 DCHECK(!IsPair());
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000181 reg_ = (reg_ & ~kRegValMask) | reg;
buzbee2700f7e2014-03-07 09:46:20 -0800182 }
183
buzbee091cc402014-03-31 10:14:40 -0700184 // Set the reg number and type only, target remain 64-bit pair.
buzbee2700f7e2014-03-07 09:46:20 -0800185 void SetLowReg(int reg) {
186 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700187 reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask);
buzbee2700f7e2014-03-07 09:46:20 -0800188 }
189
buzbee091cc402014-03-31 10:14:40 -0700190 // Retrieve the least significant register of a pair and return as 32-bit solo.
buzbee2700f7e2014-03-07 09:46:20 -0800191 int GetLowReg() const {
192 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700193 return ((reg_ & kRegTypeMask) | k32BitSolo);
buzbee2700f7e2014-03-07 09:46:20 -0800194 }
195
196 // Create a stand-alone RegStorage from the low reg of a pair.
197 RegStorage GetLow() const {
198 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700199 return RegStorage(k32BitSolo, reg_ & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000200 }
201
202 // Retrieve the most significant register of a pair.
203 int GetHighReg() const {
204 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700205 return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift) | (reg_ & kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000206 }
207
buzbee2700f7e2014-03-07 09:46:20 -0800208 // Create a stand-alone RegStorage from the high reg of a pair.
209 RegStorage GetHigh() const {
210 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700211 return RegStorage(kValid | GetHighReg());
buzbee2700f7e2014-03-07 09:46:20 -0800212 }
213
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000214 void SetHighReg(int reg) {
215 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700216 reg_ = (reg_ & ~kHighRegMask) | ((reg & kHighRegNumMask) << kHighRegShift);
buzbee091cc402014-03-31 10:14:40 -0700217 }
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 Buzbee00e1ec62014-02-27 23:44:13 +0000250 }
251
buzbee2700f7e2014-03-07 09:46:20 -0800252 // 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
buzbee091cc402014-03-31 10:14:40 -0700261 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
buzbee2700f7e2014-03-07 09:46:20 -0800269 // Create a 32-bit solo.
270 static RegStorage Solo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700271 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);
buzbee2700f7e2014-03-07 09:46:20 -0800277 }
278
279 // Create a 64-bit solo.
280 static RegStorage Solo64(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700281 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);
buzbee2700f7e2014-03-07 09:46:20 -0800287 }
288
289 static RegStorage InvalidReg() {
290 return RegStorage(kInvalid);
291 }
292
buzbee091cc402014-03-31 10:14:40 -0700293 static uint16_t RegNum(int raw_reg_bits) {
294 return raw_reg_bits & kRegNumMask;
295 }
296
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000297 int GetRawBits() const {
298 return reg_;
299 }
300
buzbee091cc402014-03-31 10:14:40 -0700301 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 Buzbee00e1ec62014-02-27 23:44:13 +0000316 private:
317 uint16_t reg_;
318};
319
320} // namespace art
321
322#endif // ART_COMPILER_DEX_REG_STORAGE_H_