blob: df5aa7b5cefade791afd03ae991dd2744066df4e [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.
25 * The encoding allows up to 32 physical elements per storage class, and supports eight
26 * register container shapes.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000027 *
buzbee091cc402014-03-31 10:14:40 -070028 * [V] [D] [HHHHH] [SSS] [F] [LLLLL]
Bill Buzbee00e1ec62014-02-27 23:44:13 +000029 *
buzbee091cc402014-03-31 10:14:40 -070030 * [LLLLL]
31 * Physical register number for the low or solo register.
32 * 0..31
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 * [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 Buzbee00e1ec62014-02-27 23:44:13 +000077 */
78
79class RegStorage {
80 public:
81 enum RegStorageKind {
buzbee091cc402014-03-31 10:14:40 -070082 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 Buzbee00e1ec62014-02-27 23:44:13 +000098 };
99
buzbee091cc402014-03-31 10:14:40 -0700100 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 Buzbee00e1ec62014-02-27 23:44:13 +0000109
buzbee091cc402014-03-31 10:14:40 -0700110 // Reg is [F][LLLLL], will override any existing shape and use rs_kind.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000111 RegStorage(RegStorageKind rs_kind, int reg) {
buzbee091cc402014-03-31 10:14:40 -0700112 DCHECK_NE(rs_kind, k64BitPair);
113 DCHECK_EQ(rs_kind & ~kShapeMask, 0);
114 reg_ = kValid | rs_kind | (reg & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000115 }
116 RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg) {
117 DCHECK_EQ(rs_kind, k64BitPair);
buzbee091cc402014-03-31 10:14:40 -0700118 DCHECK_EQ(low_reg & kFloatingPoint, high_reg & kFloatingPoint);
119 reg_ = kValid | rs_kind | ((high_reg & kRegTypeMask) << kHighRegShift) | (low_reg & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000120 }
buzbee091cc402014-03-31 10:14:40 -0700121 constexpr explicit RegStorage(uint16_t val) : reg_(val) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000122 RegStorage() : reg_(kInvalid) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000123
buzbee2700f7e2014-03-07 09:46:20 -0800124 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 {
buzbee091cc402014-03-31 10:14:40 -0700133 return ((reg_ & kValidMask) == kValid);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000134 }
135
136 bool Is32Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700137 return ((reg_ & kShapeMask) == k32BitSolo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000138 }
139
140 bool Is64Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700141 return ((reg_ & k64BitMask) == k64Bits);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000142 }
143
144 bool IsPair() const {
buzbee091cc402014-03-31 10:14:40 -0700145 return ((reg_ & kShapeMask) == k64BitPair);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000146 }
147
buzbee091cc402014-03-31 10:14:40 -0700148 bool IsFloat() const {
149 DCHECK(Valid());
150 return ((reg_ & kFloatingPoint) == kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000151 }
152
buzbee091cc402014-03-31 10:14:40 -0700153 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 Buzbee00e1ec62014-02-27 23:44:13 +0000173 }
174
175 // Used to retrieve either the low register of a pair, or the only register.
176 int GetReg() const {
buzbee091cc402014-03-31 10:14:40 -0700177 DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_;
buzbee2700f7e2014-03-07 09:46:20 -0800178 return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000179 }
180
buzbee091cc402014-03-31 10:14:40 -0700181 // Sets shape, type and num of solo.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000182 void SetReg(int reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800183 DCHECK(Valid());
buzbee091cc402014-03-31 10:14:40 -0700184 DCHECK(!IsPair());
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000185 reg_ = (reg_ & ~kRegValMask) | reg;
buzbee2700f7e2014-03-07 09:46:20 -0800186 }
187
buzbee091cc402014-03-31 10:14:40 -0700188 // Set the reg number and type only, target remain 64-bit pair.
buzbee2700f7e2014-03-07 09:46:20 -0800189 void SetLowReg(int reg) {
190 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700191 reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask);
buzbee2700f7e2014-03-07 09:46:20 -0800192 }
193
buzbee091cc402014-03-31 10:14:40 -0700194 // Retrieve the least significant register of a pair and return as 32-bit solo.
buzbee2700f7e2014-03-07 09:46:20 -0800195 int GetLowReg() const {
196 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700197 return ((reg_ & kRegTypeMask) | k32BitSolo);
buzbee2700f7e2014-03-07 09:46:20 -0800198 }
199
200 // Create a stand-alone RegStorage from the low reg of a pair.
201 RegStorage GetLow() const {
202 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700203 return RegStorage(k32BitSolo, reg_ & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000204 }
205
206 // Retrieve the most significant register of a pair.
207 int GetHighReg() const {
208 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700209 return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000210 }
211
buzbee2700f7e2014-03-07 09:46:20 -0800212 // Create a stand-alone RegStorage from the high reg of a pair.
213 RegStorage GetHigh() const {
214 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700215 return RegStorage(kValid | GetHighReg());
buzbee2700f7e2014-03-07 09:46:20 -0800216 }
217
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000218 void SetHighReg(int reg) {
219 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700220 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 Buzbee00e1ec62014-02-27 23:44:13 +0000254 }
255
buzbee2700f7e2014-03-07 09:46:20 -0800256 // 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
buzbee091cc402014-03-31 10:14:40 -0700265 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
buzbee2700f7e2014-03-07 09:46:20 -0800273 // Create a 32-bit solo.
274 static RegStorage Solo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700275 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);
buzbee2700f7e2014-03-07 09:46:20 -0800281 }
282
283 // Create a 64-bit solo.
284 static RegStorage Solo64(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700285 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);
buzbee2700f7e2014-03-07 09:46:20 -0800291 }
292
293 static RegStorage InvalidReg() {
294 return RegStorage(kInvalid);
295 }
296
buzbee091cc402014-03-31 10:14:40 -0700297 static uint16_t RegNum(int raw_reg_bits) {
298 return raw_reg_bits & kRegNumMask;
299 }
300
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000301 int GetRawBits() const {
302 return reg_;
303 }
304
buzbee091cc402014-03-31 10:14:40 -0700305 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 Buzbee00e1ec62014-02-27 23:44:13 +0000320 private:
321 uint16_t reg_;
322};
323
324} // namespace art
325
326#endif // ART_COMPILER_DEX_REG_STORAGE_H_