blob: 46ed011b531aba620520dc2d3f83e9dbc951a94d [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"
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070021#include "base/value_object.h"
Andreas Gampeccc60262014-07-04 18:02:38 -070022#include "compiler_enums.h" // For WideKind
Bill Buzbee00e1ec62014-02-27 23:44:13 +000023
24namespace art {
25
26/*
buzbee091cc402014-03-31 10:14:40 -070027 * 16-bit representation of the physical register container holding a Dalvik value.
buzbeed111c6e2014-05-11 21:09:53 -070028 * The encoding allows up to 64 physical elements per storage class, and supports eight
buzbee091cc402014-03-31 10:14:40 -070029 * register container shapes.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000030 *
buzbeed111c6e2014-05-11 21:09:53 -070031 * [V] [HHHHH] [SSS] [F] [LLLLLL]
Bill Buzbee00e1ec62014-02-27 23:44:13 +000032 *
buzbeed111c6e2014-05-11 21:09:53 -070033 * [LLLLLL]
buzbee091cc402014-03-31 10:14:40 -070034 * Physical register number for the low or solo register.
buzbeed111c6e2014-05-11 21:09:53 -070035 * 0..63
Bill Buzbee00e1ec62014-02-27 23:44:13 +000036 *
buzbee091cc402014-03-31 10:14:40 -070037 * [F]
38 * Describes type of the [LLLLL] register.
39 * 0: Core
40 * 1: Floating point
Bill Buzbee00e1ec62014-02-27 23:44:13 +000041 *
buzbee091cc402014-03-31 10:14:40 -070042 * [SSS]
43 * Shape of the register container.
44 * 000: Invalid
45 * 001: 32-bit solo register
46 * 010: 64-bit solo register
47 * 011: 64-bit pair consisting of two 32-bit solo registers
48 * 100: 128-bit solo register
49 * 101: 256-bit solo register
50 * 110: 512-bit solo register
51 * 111: 1024-bit solo register
Bill Buzbee00e1ec62014-02-27 23:44:13 +000052 *
buzbee091cc402014-03-31 10:14:40 -070053 * [HHHHH]
54 * Physical register number of the high register (valid only for register pair).
55 * 0..31
Bill Buzbee00e1ec62014-02-27 23:44:13 +000056 *
buzbee091cc402014-03-31 10:14:40 -070057 * [V]
58 * 0 -> Invalid
59 * 1 -> Valid
60 *
61 * Note that in all non-invalid cases, we can determine if the storage is floating point
buzbeed111c6e2014-05-11 21:09:53 -070062 * by testing bit 7. Note also that a register pair is effectively limited to a pair of
63 * physical register numbers in the 0..31 range.
buzbee091cc402014-03-31 10:14:40 -070064 *
65 * On some target architectures, the same underlying physical register container can be given
66 * different views. For example, Arm's 32-bit single-precision floating point registers
67 * s2 and s3 map to the low and high halves of double-precision d1. Similarly, X86's xmm3
68 * vector register can be viewed as 32-bit, 64-bit, 128-bit, etc. In these cases the use of
69 * one view will affect the other views. The RegStorage class does not concern itself
70 * with potential aliasing. That will be done using the associated RegisterInfo struct.
71 * Distinct RegStorage elements should be created for each view of a physical register
72 * container. The management of the aliased physical elements will be handled via RegisterInfo
73 * records.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000074 */
75
Ian Rogers6a3c1fc2014-10-31 00:33:20 -070076class RegStorage : public ValueObject {
Bill Buzbee00e1ec62014-02-27 23:44:13 +000077 public:
78 enum RegStorageKind {
buzbee091cc402014-03-31 10:14:40 -070079 kValidMask = 0x8000,
80 kValid = 0x8000,
81 kInvalid = 0x0000,
buzbeed111c6e2014-05-11 21:09:53 -070082 kShapeMask = 0x0380,
83 k32BitSolo = 0x0080,
84 k64BitSolo = 0x0100,
85 k64BitPair = 0x0180,
86 k128BitSolo = 0x0200,
87 k256BitSolo = 0x0280,
88 k512BitSolo = 0x0300,
89 k1024BitSolo = 0x0380,
90 k64BitMask = 0x0300,
91 k64Bits = 0x0100,
92 kShapeTypeMask = 0x03c0,
93 kFloatingPoint = 0x0040,
buzbee091cc402014-03-31 10:14:40 -070094 kCoreRegister = 0x0000,
Bill Buzbee00e1ec62014-02-27 23:44:13 +000095 };
96
buzbeed111c6e2014-05-11 21:09:53 -070097 static const uint16_t kRegValMask = 0x03ff; // Num, type and shape.
98 static const uint16_t kRegTypeMask = 0x007f; // Num and type.
99 static const uint16_t kRegNumMask = 0x003f; // Num only.
100 static const uint16_t kHighRegNumMask = 0x001f; // 0..31 for high reg
buzbee091cc402014-03-31 10:14:40 -0700101 static const uint16_t kMaxRegs = kRegValMask + 1;
buzbeed111c6e2014-05-11 21:09:53 -0700102 // TODO: deprecate use of kInvalidRegVal and speed up GetReg(). Rely on valid bit instead.
103 static const uint16_t kInvalidRegVal = 0x03ff;
104 static const uint16_t kHighRegShift = 10;
105 static const uint16_t kHighRegMask = (kHighRegNumMask << kHighRegShift);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000106
buzbee091cc402014-03-31 10:14:40 -0700107 // Reg is [F][LLLLL], will override any existing shape and use rs_kind.
Vladimir Marko83642482014-06-11 12:12:07 +0100108 constexpr RegStorage(RegStorageKind rs_kind, int reg)
109 : reg_(
110 DCHECK_CONSTEXPR(rs_kind != k64BitPair, , 0u)
111 DCHECK_CONSTEXPR((rs_kind & ~kShapeMask) == 0, , 0u)
112 kValid | rs_kind | (reg & kRegTypeMask)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000113 }
Vladimir Marko83642482014-06-11 12:12:07 +0100114 constexpr RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg)
115 : reg_(
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700116 DCHECK_CONSTEXPR(rs_kind == k64BitPair, << static_cast<int>(rs_kind), 0u)
Vladimir Marko83642482014-06-11 12:12:07 +0100117 DCHECK_CONSTEXPR((low_reg & kFloatingPoint) == (high_reg & kFloatingPoint),
118 << low_reg << ", " << high_reg, 0u)
119 DCHECK_CONSTEXPR((high_reg & kRegNumMask) <= kHighRegNumMask,
120 << "High reg must be in 0..31: " << high_reg, false)
121 kValid | rs_kind | ((high_reg & kHighRegNumMask) << kHighRegShift) |
122 (low_reg & kRegTypeMask)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000123 }
buzbee091cc402014-03-31 10:14:40 -0700124 constexpr explicit RegStorage(uint16_t val) : reg_(val) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000125 RegStorage() : reg_(kInvalid) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000126
buzbeeb5860fb2014-06-21 15:31:01 -0700127 // We do not provide a general operator overload for equality of reg storage, as this is
128 // dangerous in the case of architectures with multiple views, and the naming ExactEquals
129 // expresses the exact match expressed here. It is more likely that a comparison between the views
130 // is intended in most cases. Such code can be found in, for example, Mir2Lir::IsSameReg.
131 //
132 // If you know what you are doing, include reg_storage_eq.h, which defines == and != for brevity.
133
134 bool ExactlyEquals(const RegStorage& rhs) const {
buzbee2700f7e2014-03-07 09:46:20 -0800135 return (reg_ == rhs.GetRawBits());
136 }
137
buzbeeb5860fb2014-06-21 15:31:01 -0700138 bool NotExactlyEquals(const RegStorage& rhs) const {
buzbee2700f7e2014-03-07 09:46:20 -0800139 return (reg_ != rhs.GetRawBits());
140 }
141
Vladimir Marko83642482014-06-11 12:12:07 +0100142 constexpr bool Valid() const {
buzbee091cc402014-03-31 10:14:40 -0700143 return ((reg_ & kValidMask) == kValid);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000144 }
145
Vladimir Marko83642482014-06-11 12:12:07 +0100146 constexpr bool Is32Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700147 return ((reg_ & kShapeMask) == k32BitSolo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000148 }
149
Vladimir Marko83642482014-06-11 12:12:07 +0100150 constexpr bool Is64Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700151 return ((reg_ & k64BitMask) == k64Bits);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000152 }
153
Andreas Gampeccc60262014-07-04 18:02:38 -0700154 constexpr WideKind GetWideKind() const {
155 return Is64Bit() ? kWide : kNotWide;
156 }
157
Vladimir Marko83642482014-06-11 12:12:07 +0100158 constexpr bool Is64BitSolo() const {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700159 return ((reg_ & kShapeMask) == k64BitSolo);
160 }
161
Vladimir Marko83642482014-06-11 12:12:07 +0100162 constexpr bool IsPair() const {
buzbee091cc402014-03-31 10:14:40 -0700163 return ((reg_ & kShapeMask) == k64BitPair);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000164 }
165
Vladimir Marko83642482014-06-11 12:12:07 +0100166 constexpr bool IsFloat() const {
167 return
168 DCHECK_CONSTEXPR(Valid(), , false)
169 ((reg_ & kFloatingPoint) == kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000170 }
171
Vladimir Marko83642482014-06-11 12:12:07 +0100172 constexpr bool IsDouble() const {
173 return
174 DCHECK_CONSTEXPR(Valid(), , false)
175 (reg_ & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
buzbee091cc402014-03-31 10:14:40 -0700176 }
177
Vladimir Marko83642482014-06-11 12:12:07 +0100178 constexpr bool IsSingle() const {
179 return
180 DCHECK_CONSTEXPR(Valid(), , false)
181 (reg_ & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
buzbee091cc402014-03-31 10:14:40 -0700182 }
183
Vladimir Marko83642482014-06-11 12:12:07 +0100184 static constexpr bool IsFloat(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700185 return ((reg & kFloatingPoint) == kFloatingPoint);
186 }
187
Vladimir Marko83642482014-06-11 12:12:07 +0100188 static constexpr bool IsDouble(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700189 return (reg & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
190 }
191
Vladimir Marko83642482014-06-11 12:12:07 +0100192 static constexpr bool IsSingle(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700193 return (reg & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000194 }
195
Serban Constantinescu63999682014-07-15 17:44:21 +0100196 static constexpr bool Is32Bit(uint16_t reg) {
197 return ((reg & kShapeMask) == k32BitSolo);
198 }
199
200 static constexpr bool Is64Bit(uint16_t reg) {
201 return ((reg & k64BitMask) == k64Bits);
202 }
203
204 static constexpr bool Is64BitSolo(uint16_t reg) {
205 return ((reg & kShapeMask) == k64BitSolo);
206 }
207
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000208 // Used to retrieve either the low register of a pair, or the only register.
209 int GetReg() const {
buzbee091cc402014-03-31 10:14:40 -0700210 DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_;
buzbee2700f7e2014-03-07 09:46:20 -0800211 return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000212 }
213
buzbee091cc402014-03-31 10:14:40 -0700214 // Sets shape, type and num of solo.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000215 void SetReg(int reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800216 DCHECK(Valid());
buzbee091cc402014-03-31 10:14:40 -0700217 DCHECK(!IsPair());
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000218 reg_ = (reg_ & ~kRegValMask) | reg;
buzbee2700f7e2014-03-07 09:46:20 -0800219 }
220
buzbee091cc402014-03-31 10:14:40 -0700221 // Set the reg number and type only, target remain 64-bit pair.
buzbee2700f7e2014-03-07 09:46:20 -0800222 void SetLowReg(int reg) {
223 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700224 reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask);
buzbee2700f7e2014-03-07 09:46:20 -0800225 }
226
buzbee091cc402014-03-31 10:14:40 -0700227 // Retrieve the least significant register of a pair and return as 32-bit solo.
buzbee2700f7e2014-03-07 09:46:20 -0800228 int GetLowReg() const {
229 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700230 return ((reg_ & kRegTypeMask) | k32BitSolo);
buzbee2700f7e2014-03-07 09:46:20 -0800231 }
232
233 // Create a stand-alone RegStorage from the low reg of a pair.
234 RegStorage GetLow() const {
235 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700236 return RegStorage(k32BitSolo, reg_ & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000237 }
238
239 // Retrieve the most significant register of a pair.
240 int GetHighReg() const {
241 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700242 return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift) | (reg_ & kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000243 }
244
buzbee2700f7e2014-03-07 09:46:20 -0800245 // Create a stand-alone RegStorage from the high reg of a pair.
246 RegStorage GetHigh() const {
247 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700248 return RegStorage(kValid | GetHighReg());
buzbee2700f7e2014-03-07 09:46:20 -0800249 }
250
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000251 void SetHighReg(int reg) {
252 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700253 reg_ = (reg_ & ~kHighRegMask) | ((reg & kHighRegNumMask) << kHighRegShift);
buzbee091cc402014-03-31 10:14:40 -0700254 }
255
256 // Return the register number of low or solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100257 constexpr int GetRegNum() const {
buzbee091cc402014-03-31 10:14:40 -0700258 return reg_ & kRegNumMask;
259 }
260
buzbee091cc402014-03-31 10:14:40 -0700261 // Is register number in 0..7?
Vladimir Marko83642482014-06-11 12:12:07 +0100262 constexpr bool Low8() const {
buzbee091cc402014-03-31 10:14:40 -0700263 return GetRegNum() < 8;
264 }
265
266 // Is register number in 0..3?
Vladimir Marko83642482014-06-11 12:12:07 +0100267 constexpr bool Low4() const {
buzbee091cc402014-03-31 10:14:40 -0700268 return GetRegNum() < 4;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000269 }
270
buzbee2700f7e2014-03-07 09:46:20 -0800271 // Combine 2 32-bit solo regs into a pair.
272 static RegStorage MakeRegPair(RegStorage low, RegStorage high) {
273 DCHECK(!low.IsPair());
274 DCHECK(low.Is32Bit());
275 DCHECK(!high.IsPair());
276 DCHECK(high.Is32Bit());
277 return RegStorage(k64BitPair, low.GetReg(), high.GetReg());
278 }
279
Vladimir Marko83642482014-06-11 12:12:07 +0100280 static constexpr bool SameRegType(RegStorage reg1, RegStorage reg2) {
Serban Constantinescu63999682014-07-15 17:44:21 +0100281 return ((reg1.reg_ & kShapeTypeMask) == (reg2.reg_ & kShapeTypeMask));
buzbee091cc402014-03-31 10:14:40 -0700282 }
283
Vladimir Marko83642482014-06-11 12:12:07 +0100284 static constexpr bool SameRegType(int reg1, int reg2) {
Serban Constantinescu63999682014-07-15 17:44:21 +0100285 return ((reg1 & kShapeTypeMask) == (reg2 & kShapeTypeMask));
buzbee091cc402014-03-31 10:14:40 -0700286 }
287
buzbee2700f7e2014-03-07 09:46:20 -0800288 // Create a 32-bit solo.
289 static RegStorage Solo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700290 return RegStorage(k32BitSolo, reg_num & kRegTypeMask);
291 }
292
293 // Create a floating point 32-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100294 static constexpr RegStorage FloatSolo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700295 return RegStorage(k32BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800296 }
297
Mark Mendellfe945782014-05-22 09:52:36 -0400298 // Create a 128-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100299 static constexpr RegStorage Solo128(int reg_num) {
Mark Mendellfe945782014-05-22 09:52:36 -0400300 return RegStorage(k128BitSolo, reg_num & kRegTypeMask);
301 }
302
buzbee2700f7e2014-03-07 09:46:20 -0800303 // Create a 64-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100304 static constexpr RegStorage Solo64(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700305 return RegStorage(k64BitSolo, reg_num & kRegTypeMask);
306 }
307
308 // Create a floating point 64-bit solo.
309 static RegStorage FloatSolo64(int reg_num) {
310 return RegStorage(k64BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800311 }
312
Vladimir Marko83642482014-06-11 12:12:07 +0100313 static constexpr RegStorage InvalidReg() {
buzbee2700f7e2014-03-07 09:46:20 -0800314 return RegStorage(kInvalid);
315 }
316
Vladimir Marko83642482014-06-11 12:12:07 +0100317 static constexpr uint16_t RegNum(int raw_reg_bits) {
buzbee091cc402014-03-31 10:14:40 -0700318 return raw_reg_bits & kRegNumMask;
319 }
320
Vladimir Marko83642482014-06-11 12:12:07 +0100321 constexpr int GetRawBits() const {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000322 return reg_;
323 }
324
Vladimir Marko83642482014-06-11 12:12:07 +0100325 size_t StorageSize() const {
buzbee091cc402014-03-31 10:14:40 -0700326 switch (reg_ & kShapeMask) {
327 case kInvalid: return 0;
328 case k32BitSolo: return 4;
329 case k64BitSolo: return 8;
330 case k64BitPair: return 8; // Is this useful? Might want to disallow taking size of pair.
331 case k128BitSolo: return 16;
332 case k256BitSolo: return 32;
333 case k512BitSolo: return 64;
334 case k1024BitSolo: return 128;
Ian Rogers6a3c1fc2014-10-31 00:33:20 -0700335 default: LOG(FATAL) << "Unexpected shape"; UNREACHABLE();
buzbee091cc402014-03-31 10:14:40 -0700336 }
buzbee091cc402014-03-31 10:14:40 -0700337 }
338
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000339 private:
340 uint16_t reg_;
341};
Ian Rogersb28c1c02014-11-08 11:21:21 -0800342static inline std::ostream& operator<<(std::ostream& o, const RegStorage& rhs) {
343 return o << rhs.GetRawBits(); // TODO: better output.
344}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000345
346} // namespace art
347
348#endif // ART_COMPILER_DEX_REG_STORAGE_H_