blob: 706933a1b470fe6cf1670fbb6b3627de23cd2239 [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
Vladimir Marko83642482014-06-11 12:12:07 +010020#include "base/logging.h"
Andreas Gampeccc60262014-07-04 18:02:38 -070021#include "compiler_enums.h" // For WideKind
Bill Buzbee00e1ec62014-02-27 23:44:13 +000022
23namespace art {
24
25/*
buzbee091cc402014-03-31 10:14:40 -070026 * 16-bit representation of the physical register container holding a Dalvik value.
buzbeed111c6e2014-05-11 21:09:53 -070027 * The encoding allows up to 64 physical elements per storage class, and supports eight
buzbee091cc402014-03-31 10:14:40 -070028 * register container shapes.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000029 *
buzbeed111c6e2014-05-11 21:09:53 -070030 * [V] [HHHHH] [SSS] [F] [LLLLLL]
Bill Buzbee00e1ec62014-02-27 23:44:13 +000031 *
buzbeed111c6e2014-05-11 21:09:53 -070032 * [LLLLLL]
buzbee091cc402014-03-31 10:14:40 -070033 * Physical register number for the low or solo register.
buzbeed111c6e2014-05-11 21:09:53 -070034 * 0..63
Bill Buzbee00e1ec62014-02-27 23:44:13 +000035 *
buzbee091cc402014-03-31 10:14:40 -070036 * [F]
37 * Describes type of the [LLLLL] register.
38 * 0: Core
39 * 1: Floating point
Bill Buzbee00e1ec62014-02-27 23:44:13 +000040 *
buzbee091cc402014-03-31 10:14:40 -070041 * [SSS]
42 * Shape of the register container.
43 * 000: Invalid
44 * 001: 32-bit solo register
45 * 010: 64-bit solo register
46 * 011: 64-bit pair consisting of two 32-bit solo registers
47 * 100: 128-bit solo register
48 * 101: 256-bit solo register
49 * 110: 512-bit solo register
50 * 111: 1024-bit solo register
Bill Buzbee00e1ec62014-02-27 23:44:13 +000051 *
buzbee091cc402014-03-31 10:14:40 -070052 * [HHHHH]
53 * Physical register number of the high register (valid only for register pair).
54 * 0..31
Bill Buzbee00e1ec62014-02-27 23:44:13 +000055 *
buzbee091cc402014-03-31 10:14:40 -070056 * [V]
57 * 0 -> Invalid
58 * 1 -> Valid
59 *
60 * Note that in all non-invalid cases, we can determine if the storage is floating point
buzbeed111c6e2014-05-11 21:09:53 -070061 * by testing bit 7. Note also that a register pair is effectively limited to a pair of
62 * physical register numbers in the 0..31 range.
buzbee091cc402014-03-31 10:14:40 -070063 *
64 * On some target architectures, the same underlying physical register container can be given
65 * different views. For example, Arm's 32-bit single-precision floating point registers
66 * s2 and s3 map to the low and high halves of double-precision d1. Similarly, X86's xmm3
67 * vector register can be viewed as 32-bit, 64-bit, 128-bit, etc. In these cases the use of
68 * one view will affect the other views. The RegStorage class does not concern itself
69 * with potential aliasing. That will be done using the associated RegisterInfo struct.
70 * Distinct RegStorage elements should be created for each view of a physical register
71 * container. The management of the aliased physical elements will be handled via RegisterInfo
72 * records.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000073 */
74
75class RegStorage {
76 public:
77 enum RegStorageKind {
buzbee091cc402014-03-31 10:14:40 -070078 kValidMask = 0x8000,
79 kValid = 0x8000,
80 kInvalid = 0x0000,
buzbeed111c6e2014-05-11 21:09:53 -070081 kShapeMask = 0x0380,
82 k32BitSolo = 0x0080,
83 k64BitSolo = 0x0100,
84 k64BitPair = 0x0180,
85 k128BitSolo = 0x0200,
86 k256BitSolo = 0x0280,
87 k512BitSolo = 0x0300,
88 k1024BitSolo = 0x0380,
89 k64BitMask = 0x0300,
90 k64Bits = 0x0100,
91 kShapeTypeMask = 0x03c0,
92 kFloatingPoint = 0x0040,
buzbee091cc402014-03-31 10:14:40 -070093 kCoreRegister = 0x0000,
Bill Buzbee00e1ec62014-02-27 23:44:13 +000094 };
95
buzbeed111c6e2014-05-11 21:09:53 -070096 static const uint16_t kRegValMask = 0x03ff; // Num, type and shape.
97 static const uint16_t kRegTypeMask = 0x007f; // Num and type.
98 static const uint16_t kRegNumMask = 0x003f; // Num only.
99 static const uint16_t kHighRegNumMask = 0x001f; // 0..31 for high reg
buzbee091cc402014-03-31 10:14:40 -0700100 static const uint16_t kMaxRegs = kRegValMask + 1;
buzbeed111c6e2014-05-11 21:09:53 -0700101 // TODO: deprecate use of kInvalidRegVal and speed up GetReg(). Rely on valid bit instead.
102 static const uint16_t kInvalidRegVal = 0x03ff;
103 static const uint16_t kHighRegShift = 10;
104 static const uint16_t kHighRegMask = (kHighRegNumMask << kHighRegShift);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000105
buzbee091cc402014-03-31 10:14:40 -0700106 // Reg is [F][LLLLL], will override any existing shape and use rs_kind.
Vladimir Marko83642482014-06-11 12:12:07 +0100107 constexpr RegStorage(RegStorageKind rs_kind, int reg)
108 : reg_(
109 DCHECK_CONSTEXPR(rs_kind != k64BitPair, , 0u)
110 DCHECK_CONSTEXPR((rs_kind & ~kShapeMask) == 0, , 0u)
111 kValid | rs_kind | (reg & kRegTypeMask)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000112 }
Vladimir Marko83642482014-06-11 12:12:07 +0100113 constexpr RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg)
114 : reg_(
115 DCHECK_CONSTEXPR(rs_kind == k64BitPair, << rs_kind, 0u)
116 DCHECK_CONSTEXPR((low_reg & kFloatingPoint) == (high_reg & kFloatingPoint),
117 << low_reg << ", " << high_reg, 0u)
118 DCHECK_CONSTEXPR((high_reg & kRegNumMask) <= kHighRegNumMask,
119 << "High reg must be in 0..31: " << high_reg, false)
120 kValid | rs_kind | ((high_reg & kHighRegNumMask) << kHighRegShift) |
121 (low_reg & kRegTypeMask)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000122 }
buzbee091cc402014-03-31 10:14:40 -0700123 constexpr explicit RegStorage(uint16_t val) : reg_(val) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000124 RegStorage() : reg_(kInvalid) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000125
buzbeeb5860fb2014-06-21 15:31:01 -0700126 // We do not provide a general operator overload for equality of reg storage, as this is
127 // dangerous in the case of architectures with multiple views, and the naming ExactEquals
128 // expresses the exact match expressed here. It is more likely that a comparison between the views
129 // is intended in most cases. Such code can be found in, for example, Mir2Lir::IsSameReg.
130 //
131 // If you know what you are doing, include reg_storage_eq.h, which defines == and != for brevity.
132
133 bool ExactlyEquals(const RegStorage& rhs) const {
buzbee2700f7e2014-03-07 09:46:20 -0800134 return (reg_ == rhs.GetRawBits());
135 }
136
buzbeeb5860fb2014-06-21 15:31:01 -0700137 bool NotExactlyEquals(const RegStorage& rhs) const {
buzbee2700f7e2014-03-07 09:46:20 -0800138 return (reg_ != rhs.GetRawBits());
139 }
140
Vladimir Marko83642482014-06-11 12:12:07 +0100141 constexpr bool Valid() const {
buzbee091cc402014-03-31 10:14:40 -0700142 return ((reg_ & kValidMask) == kValid);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000143 }
144
Vladimir Marko83642482014-06-11 12:12:07 +0100145 constexpr bool Is32Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700146 return ((reg_ & kShapeMask) == k32BitSolo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000147 }
148
Vladimir Marko83642482014-06-11 12:12:07 +0100149 constexpr bool Is64Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700150 return ((reg_ & k64BitMask) == k64Bits);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000151 }
152
Andreas Gampeccc60262014-07-04 18:02:38 -0700153 constexpr WideKind GetWideKind() const {
154 return Is64Bit() ? kWide : kNotWide;
155 }
156
Vladimir Marko83642482014-06-11 12:12:07 +0100157 constexpr bool Is64BitSolo() const {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700158 return ((reg_ & kShapeMask) == k64BitSolo);
159 }
160
Vladimir Marko83642482014-06-11 12:12:07 +0100161 constexpr bool IsPair() const {
buzbee091cc402014-03-31 10:14:40 -0700162 return ((reg_ & kShapeMask) == k64BitPair);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000163 }
164
Vladimir Marko83642482014-06-11 12:12:07 +0100165 constexpr bool IsFloat() const {
166 return
167 DCHECK_CONSTEXPR(Valid(), , false)
168 ((reg_ & kFloatingPoint) == kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000169 }
170
Vladimir Marko83642482014-06-11 12:12:07 +0100171 constexpr bool IsDouble() const {
172 return
173 DCHECK_CONSTEXPR(Valid(), , false)
174 (reg_ & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
buzbee091cc402014-03-31 10:14:40 -0700175 }
176
Vladimir Marko83642482014-06-11 12:12:07 +0100177 constexpr bool IsSingle() const {
178 return
179 DCHECK_CONSTEXPR(Valid(), , false)
180 (reg_ & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
buzbee091cc402014-03-31 10:14:40 -0700181 }
182
Vladimir Marko83642482014-06-11 12:12:07 +0100183 static constexpr bool IsFloat(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700184 return ((reg & kFloatingPoint) == kFloatingPoint);
185 }
186
Vladimir Marko83642482014-06-11 12:12:07 +0100187 static constexpr bool IsDouble(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700188 return (reg & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
189 }
190
Vladimir Marko83642482014-06-11 12:12:07 +0100191 static constexpr bool IsSingle(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700192 return (reg & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000193 }
194
Serban Constantinescu63999682014-07-15 17:44:21 +0100195 static constexpr bool Is32Bit(uint16_t reg) {
196 return ((reg & kShapeMask) == k32BitSolo);
197 }
198
199 static constexpr bool Is64Bit(uint16_t reg) {
200 return ((reg & k64BitMask) == k64Bits);
201 }
202
203 static constexpr bool Is64BitSolo(uint16_t reg) {
204 return ((reg & kShapeMask) == k64BitSolo);
205 }
206
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000207 // Used to retrieve either the low register of a pair, or the only register.
208 int GetReg() const {
buzbee091cc402014-03-31 10:14:40 -0700209 DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_;
buzbee2700f7e2014-03-07 09:46:20 -0800210 return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000211 }
212
buzbee091cc402014-03-31 10:14:40 -0700213 // Sets shape, type and num of solo.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000214 void SetReg(int reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800215 DCHECK(Valid());
buzbee091cc402014-03-31 10:14:40 -0700216 DCHECK(!IsPair());
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000217 reg_ = (reg_ & ~kRegValMask) | reg;
buzbee2700f7e2014-03-07 09:46:20 -0800218 }
219
buzbee091cc402014-03-31 10:14:40 -0700220 // Set the reg number and type only, target remain 64-bit pair.
buzbee2700f7e2014-03-07 09:46:20 -0800221 void SetLowReg(int reg) {
222 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700223 reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask);
buzbee2700f7e2014-03-07 09:46:20 -0800224 }
225
buzbee091cc402014-03-31 10:14:40 -0700226 // Retrieve the least significant register of a pair and return as 32-bit solo.
buzbee2700f7e2014-03-07 09:46:20 -0800227 int GetLowReg() const {
228 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700229 return ((reg_ & kRegTypeMask) | k32BitSolo);
buzbee2700f7e2014-03-07 09:46:20 -0800230 }
231
232 // Create a stand-alone RegStorage from the low reg of a pair.
233 RegStorage GetLow() const {
234 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700235 return RegStorage(k32BitSolo, reg_ & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000236 }
237
238 // Retrieve the most significant register of a pair.
239 int GetHighReg() const {
240 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700241 return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift) | (reg_ & kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000242 }
243
buzbee2700f7e2014-03-07 09:46:20 -0800244 // Create a stand-alone RegStorage from the high reg of a pair.
245 RegStorage GetHigh() const {
246 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700247 return RegStorage(kValid | GetHighReg());
buzbee2700f7e2014-03-07 09:46:20 -0800248 }
249
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000250 void SetHighReg(int reg) {
251 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700252 reg_ = (reg_ & ~kHighRegMask) | ((reg & kHighRegNumMask) << kHighRegShift);
buzbee091cc402014-03-31 10:14:40 -0700253 }
254
255 // Return the register number of low or solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100256 constexpr int GetRegNum() const {
buzbee091cc402014-03-31 10:14:40 -0700257 return reg_ & kRegNumMask;
258 }
259
buzbee091cc402014-03-31 10:14:40 -0700260 // Is register number in 0..7?
Vladimir Marko83642482014-06-11 12:12:07 +0100261 constexpr bool Low8() const {
buzbee091cc402014-03-31 10:14:40 -0700262 return GetRegNum() < 8;
263 }
264
265 // Is register number in 0..3?
Vladimir Marko83642482014-06-11 12:12:07 +0100266 constexpr bool Low4() const {
buzbee091cc402014-03-31 10:14:40 -0700267 return GetRegNum() < 4;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000268 }
269
buzbee2700f7e2014-03-07 09:46:20 -0800270 // Combine 2 32-bit solo regs into a pair.
271 static RegStorage MakeRegPair(RegStorage low, RegStorage high) {
272 DCHECK(!low.IsPair());
273 DCHECK(low.Is32Bit());
274 DCHECK(!high.IsPair());
275 DCHECK(high.Is32Bit());
276 return RegStorage(k64BitPair, low.GetReg(), high.GetReg());
277 }
278
Vladimir Marko83642482014-06-11 12:12:07 +0100279 static constexpr bool SameRegType(RegStorage reg1, RegStorage reg2) {
Serban Constantinescu63999682014-07-15 17:44:21 +0100280 return ((reg1.reg_ & kShapeTypeMask) == (reg2.reg_ & kShapeTypeMask));
buzbee091cc402014-03-31 10:14:40 -0700281 }
282
Vladimir Marko83642482014-06-11 12:12:07 +0100283 static constexpr bool SameRegType(int reg1, int reg2) {
Serban Constantinescu63999682014-07-15 17:44:21 +0100284 return ((reg1 & kShapeTypeMask) == (reg2 & kShapeTypeMask));
buzbee091cc402014-03-31 10:14:40 -0700285 }
286
buzbee2700f7e2014-03-07 09:46:20 -0800287 // Create a 32-bit solo.
288 static RegStorage Solo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700289 return RegStorage(k32BitSolo, reg_num & kRegTypeMask);
290 }
291
292 // Create a floating point 32-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100293 static constexpr RegStorage FloatSolo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700294 return RegStorage(k32BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800295 }
296
Mark Mendellfe945782014-05-22 09:52:36 -0400297 // Create a 128-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100298 static constexpr RegStorage Solo128(int reg_num) {
Mark Mendellfe945782014-05-22 09:52:36 -0400299 return RegStorage(k128BitSolo, reg_num & kRegTypeMask);
300 }
301
buzbee2700f7e2014-03-07 09:46:20 -0800302 // Create a 64-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100303 static constexpr RegStorage Solo64(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700304 return RegStorage(k64BitSolo, reg_num & kRegTypeMask);
305 }
306
307 // Create a floating point 64-bit solo.
308 static RegStorage FloatSolo64(int reg_num) {
309 return RegStorage(k64BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800310 }
311
Vladimir Marko83642482014-06-11 12:12:07 +0100312 static constexpr RegStorage InvalidReg() {
buzbee2700f7e2014-03-07 09:46:20 -0800313 return RegStorage(kInvalid);
314 }
315
Vladimir Marko83642482014-06-11 12:12:07 +0100316 static constexpr uint16_t RegNum(int raw_reg_bits) {
buzbee091cc402014-03-31 10:14:40 -0700317 return raw_reg_bits & kRegNumMask;
318 }
319
Vladimir Marko83642482014-06-11 12:12:07 +0100320 constexpr int GetRawBits() const {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000321 return reg_;
322 }
323
Vladimir Marko83642482014-06-11 12:12:07 +0100324 size_t StorageSize() const {
buzbee091cc402014-03-31 10:14:40 -0700325 switch (reg_ & kShapeMask) {
326 case kInvalid: return 0;
327 case k32BitSolo: return 4;
328 case k64BitSolo: return 8;
329 case k64BitPair: return 8; // Is this useful? Might want to disallow taking size of pair.
330 case k128BitSolo: return 16;
331 case k256BitSolo: return 32;
332 case k512BitSolo: return 64;
333 case k1024BitSolo: return 128;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100334 default: LOG(FATAL) << "Unexpected shape";
buzbee091cc402014-03-31 10:14:40 -0700335 }
336 return 0;
337 }
338
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000339 private:
340 uint16_t reg_;
341};
342
343} // namespace art
344
345#endif // ART_COMPILER_DEX_REG_STORAGE_H_