blob: 3b891f2f201caaab56e2364b22e3fdf5da09252d [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"
Bill Buzbee00e1ec62014-02-27 23:44:13 +000021
22namespace art {
23
24/*
buzbee091cc402014-03-31 10:14:40 -070025 * 16-bit representation of the physical register container holding a Dalvik value.
buzbeed111c6e2014-05-11 21:09:53 -070026 * The encoding allows up to 64 physical elements per storage class, and supports eight
buzbee091cc402014-03-31 10:14:40 -070027 * register container shapes.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000028 *
buzbeed111c6e2014-05-11 21:09:53 -070029 * [V] [HHHHH] [SSS] [F] [LLLLLL]
Bill Buzbee00e1ec62014-02-27 23:44:13 +000030 *
buzbeed111c6e2014-05-11 21:09:53 -070031 * [LLLLLL]
buzbee091cc402014-03-31 10:14:40 -070032 * Physical register number for the low or solo register.
buzbeed111c6e2014-05-11 21:09:53 -070033 * 0..63
Bill Buzbee00e1ec62014-02-27 23:44:13 +000034 *
buzbee091cc402014-03-31 10:14:40 -070035 * [F]
36 * Describes type of the [LLLLL] register.
37 * 0: Core
38 * 1: Floating point
Bill Buzbee00e1ec62014-02-27 23:44:13 +000039 *
buzbee091cc402014-03-31 10:14:40 -070040 * [SSS]
41 * Shape of the register container.
42 * 000: Invalid
43 * 001: 32-bit solo register
44 * 010: 64-bit solo register
45 * 011: 64-bit pair consisting of two 32-bit solo registers
46 * 100: 128-bit solo register
47 * 101: 256-bit solo register
48 * 110: 512-bit solo register
49 * 111: 1024-bit solo register
Bill Buzbee00e1ec62014-02-27 23:44:13 +000050 *
buzbee091cc402014-03-31 10:14:40 -070051 * [HHHHH]
52 * Physical register number of the high register (valid only for register pair).
53 * 0..31
Bill Buzbee00e1ec62014-02-27 23:44:13 +000054 *
buzbee091cc402014-03-31 10:14:40 -070055 * [V]
56 * 0 -> Invalid
57 * 1 -> Valid
58 *
59 * Note that in all non-invalid cases, we can determine if the storage is floating point
buzbeed111c6e2014-05-11 21:09:53 -070060 * by testing bit 7. Note also that a register pair is effectively limited to a pair of
61 * physical register numbers in the 0..31 range.
buzbee091cc402014-03-31 10:14:40 -070062 *
63 * On some target architectures, the same underlying physical register container can be given
64 * different views. For example, Arm's 32-bit single-precision floating point registers
65 * s2 and s3 map to the low and high halves of double-precision d1. Similarly, X86's xmm3
66 * vector register can be viewed as 32-bit, 64-bit, 128-bit, etc. In these cases the use of
67 * one view will affect the other views. The RegStorage class does not concern itself
68 * with potential aliasing. That will be done using the associated RegisterInfo struct.
69 * Distinct RegStorage elements should be created for each view of a physical register
70 * container. The management of the aliased physical elements will be handled via RegisterInfo
71 * records.
Bill Buzbee00e1ec62014-02-27 23:44:13 +000072 */
73
74class RegStorage {
75 public:
76 enum RegStorageKind {
buzbee091cc402014-03-31 10:14:40 -070077 kValidMask = 0x8000,
78 kValid = 0x8000,
79 kInvalid = 0x0000,
buzbeed111c6e2014-05-11 21:09:53 -070080 kShapeMask = 0x0380,
81 k32BitSolo = 0x0080,
82 k64BitSolo = 0x0100,
83 k64BitPair = 0x0180,
84 k128BitSolo = 0x0200,
85 k256BitSolo = 0x0280,
86 k512BitSolo = 0x0300,
87 k1024BitSolo = 0x0380,
88 k64BitMask = 0x0300,
89 k64Bits = 0x0100,
90 kShapeTypeMask = 0x03c0,
91 kFloatingPoint = 0x0040,
buzbee091cc402014-03-31 10:14:40 -070092 kCoreRegister = 0x0000,
Bill Buzbee00e1ec62014-02-27 23:44:13 +000093 };
94
buzbeed111c6e2014-05-11 21:09:53 -070095 static const uint16_t kRegValMask = 0x03ff; // Num, type and shape.
96 static const uint16_t kRegTypeMask = 0x007f; // Num and type.
97 static const uint16_t kRegNumMask = 0x003f; // Num only.
98 static const uint16_t kHighRegNumMask = 0x001f; // 0..31 for high reg
buzbee091cc402014-03-31 10:14:40 -070099 static const uint16_t kMaxRegs = kRegValMask + 1;
buzbeed111c6e2014-05-11 21:09:53 -0700100 // TODO: deprecate use of kInvalidRegVal and speed up GetReg(). Rely on valid bit instead.
101 static const uint16_t kInvalidRegVal = 0x03ff;
102 static const uint16_t kHighRegShift = 10;
103 static const uint16_t kHighRegMask = (kHighRegNumMask << kHighRegShift);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000104
buzbee091cc402014-03-31 10:14:40 -0700105 // Reg is [F][LLLLL], will override any existing shape and use rs_kind.
Vladimir Marko83642482014-06-11 12:12:07 +0100106 constexpr RegStorage(RegStorageKind rs_kind, int reg)
107 : reg_(
108 DCHECK_CONSTEXPR(rs_kind != k64BitPair, , 0u)
109 DCHECK_CONSTEXPR((rs_kind & ~kShapeMask) == 0, , 0u)
110 kValid | rs_kind | (reg & kRegTypeMask)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000111 }
Vladimir Marko83642482014-06-11 12:12:07 +0100112 constexpr RegStorage(RegStorageKind rs_kind, int low_reg, int high_reg)
113 : reg_(
114 DCHECK_CONSTEXPR(rs_kind == k64BitPair, << rs_kind, 0u)
115 DCHECK_CONSTEXPR((low_reg & kFloatingPoint) == (high_reg & kFloatingPoint),
116 << low_reg << ", " << high_reg, 0u)
117 DCHECK_CONSTEXPR((high_reg & kRegNumMask) <= kHighRegNumMask,
118 << "High reg must be in 0..31: " << high_reg, false)
119 kValid | rs_kind | ((high_reg & kHighRegNumMask) << kHighRegShift) |
120 (low_reg & kRegTypeMask)) {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000121 }
buzbee091cc402014-03-31 10:14:40 -0700122 constexpr explicit RegStorage(uint16_t val) : reg_(val) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000123 RegStorage() : reg_(kInvalid) {}
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000124
buzbee2700f7e2014-03-07 09:46:20 -0800125 bool operator==(const RegStorage rhs) const {
126 return (reg_ == rhs.GetRawBits());
127 }
128
129 bool operator!=(const RegStorage rhs) const {
130 return (reg_ != rhs.GetRawBits());
131 }
132
Vladimir Marko83642482014-06-11 12:12:07 +0100133 constexpr bool Valid() const {
buzbee091cc402014-03-31 10:14:40 -0700134 return ((reg_ & kValidMask) == kValid);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000135 }
136
Vladimir Marko83642482014-06-11 12:12:07 +0100137 constexpr bool Is32Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700138 return ((reg_ & kShapeMask) == k32BitSolo);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000139 }
140
Vladimir Marko83642482014-06-11 12:12:07 +0100141 constexpr bool Is64Bit() const {
buzbee091cc402014-03-31 10:14:40 -0700142 return ((reg_ & k64BitMask) == k64Bits);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000143 }
144
Vladimir Marko83642482014-06-11 12:12:07 +0100145 constexpr bool Is64BitSolo() const {
Dmitry Petrochenko9ee801f2014-05-12 11:31:37 +0700146 return ((reg_ & kShapeMask) == k64BitSolo);
147 }
148
Vladimir Marko83642482014-06-11 12:12:07 +0100149 constexpr bool IsPair() const {
buzbee091cc402014-03-31 10:14:40 -0700150 return ((reg_ & kShapeMask) == k64BitPair);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000151 }
152
Vladimir Marko83642482014-06-11 12:12:07 +0100153 constexpr bool IsFloat() const {
154 return
155 DCHECK_CONSTEXPR(Valid(), , false)
156 ((reg_ & kFloatingPoint) == kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000157 }
158
Vladimir Marko83642482014-06-11 12:12:07 +0100159 constexpr bool IsDouble() const {
160 return
161 DCHECK_CONSTEXPR(Valid(), , false)
162 (reg_ & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
buzbee091cc402014-03-31 10:14:40 -0700163 }
164
Vladimir Marko83642482014-06-11 12:12:07 +0100165 constexpr bool IsSingle() const {
166 return
167 DCHECK_CONSTEXPR(Valid(), , false)
168 (reg_ & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
buzbee091cc402014-03-31 10:14:40 -0700169 }
170
Vladimir Marko83642482014-06-11 12:12:07 +0100171 static constexpr bool IsFloat(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700172 return ((reg & kFloatingPoint) == kFloatingPoint);
173 }
174
Vladimir Marko83642482014-06-11 12:12:07 +0100175 static constexpr bool IsDouble(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700176 return (reg & (kFloatingPoint | k64BitMask)) == (kFloatingPoint | k64Bits);
177 }
178
Vladimir Marko83642482014-06-11 12:12:07 +0100179 static constexpr bool IsSingle(uint16_t reg) {
buzbee091cc402014-03-31 10:14:40 -0700180 return (reg & (kFloatingPoint | k64BitMask)) == kFloatingPoint;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000181 }
182
183 // Used to retrieve either the low register of a pair, or the only register.
184 int GetReg() const {
buzbee091cc402014-03-31 10:14:40 -0700185 DCHECK(!IsPair()) << "reg_ = 0x" << std::hex << reg_;
buzbee2700f7e2014-03-07 09:46:20 -0800186 return Valid() ? (reg_ & kRegValMask) : kInvalidRegVal;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000187 }
188
buzbee091cc402014-03-31 10:14:40 -0700189 // Sets shape, type and num of solo.
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000190 void SetReg(int reg) {
buzbee2700f7e2014-03-07 09:46:20 -0800191 DCHECK(Valid());
buzbee091cc402014-03-31 10:14:40 -0700192 DCHECK(!IsPair());
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000193 reg_ = (reg_ & ~kRegValMask) | reg;
buzbee2700f7e2014-03-07 09:46:20 -0800194 }
195
buzbee091cc402014-03-31 10:14:40 -0700196 // Set the reg number and type only, target remain 64-bit pair.
buzbee2700f7e2014-03-07 09:46:20 -0800197 void SetLowReg(int reg) {
198 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700199 reg_ = (reg_ & ~kRegTypeMask) | (reg & kRegTypeMask);
buzbee2700f7e2014-03-07 09:46:20 -0800200 }
201
buzbee091cc402014-03-31 10:14:40 -0700202 // Retrieve the least significant register of a pair and return as 32-bit solo.
buzbee2700f7e2014-03-07 09:46:20 -0800203 int GetLowReg() const {
204 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700205 return ((reg_ & kRegTypeMask) | k32BitSolo);
buzbee2700f7e2014-03-07 09:46:20 -0800206 }
207
208 // Create a stand-alone RegStorage from the low reg of a pair.
209 RegStorage GetLow() const {
210 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700211 return RegStorage(k32BitSolo, reg_ & kRegTypeMask);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000212 }
213
214 // Retrieve the most significant register of a pair.
215 int GetHighReg() const {
216 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700217 return k32BitSolo | ((reg_ & kHighRegMask) >> kHighRegShift) | (reg_ & kFloatingPoint);
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000218 }
219
buzbee2700f7e2014-03-07 09:46:20 -0800220 // Create a stand-alone RegStorage from the high reg of a pair.
221 RegStorage GetHigh() const {
222 DCHECK(IsPair());
buzbee091cc402014-03-31 10:14:40 -0700223 return RegStorage(kValid | GetHighReg());
buzbee2700f7e2014-03-07 09:46:20 -0800224 }
225
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000226 void SetHighReg(int reg) {
227 DCHECK(IsPair());
buzbeed111c6e2014-05-11 21:09:53 -0700228 reg_ = (reg_ & ~kHighRegMask) | ((reg & kHighRegNumMask) << kHighRegShift);
buzbee091cc402014-03-31 10:14:40 -0700229 }
230
231 // Return the register number of low or solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100232 constexpr int GetRegNum() const {
buzbee091cc402014-03-31 10:14:40 -0700233 return reg_ & kRegNumMask;
234 }
235
buzbee091cc402014-03-31 10:14:40 -0700236 // Is register number in 0..7?
Vladimir Marko83642482014-06-11 12:12:07 +0100237 constexpr bool Low8() const {
buzbee091cc402014-03-31 10:14:40 -0700238 return GetRegNum() < 8;
239 }
240
241 // Is register number in 0..3?
Vladimir Marko83642482014-06-11 12:12:07 +0100242 constexpr bool Low4() const {
buzbee091cc402014-03-31 10:14:40 -0700243 return GetRegNum() < 4;
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000244 }
245
buzbee2700f7e2014-03-07 09:46:20 -0800246 // Combine 2 32-bit solo regs into a pair.
247 static RegStorage MakeRegPair(RegStorage low, RegStorage high) {
248 DCHECK(!low.IsPair());
249 DCHECK(low.Is32Bit());
250 DCHECK(!high.IsPair());
251 DCHECK(high.Is32Bit());
252 return RegStorage(k64BitPair, low.GetReg(), high.GetReg());
253 }
254
Vladimir Marko83642482014-06-11 12:12:07 +0100255 static constexpr bool SameRegType(RegStorage reg1, RegStorage reg2) {
buzbee091cc402014-03-31 10:14:40 -0700256 return (reg1.IsDouble() == reg2.IsDouble()) && (reg1.IsSingle() == reg2.IsSingle());
257 }
258
Vladimir Marko83642482014-06-11 12:12:07 +0100259 static constexpr bool SameRegType(int reg1, int reg2) {
buzbee091cc402014-03-31 10:14:40 -0700260 return (IsDouble(reg1) == IsDouble(reg2)) && (IsSingle(reg1) == IsSingle(reg2));
261 }
262
buzbee2700f7e2014-03-07 09:46:20 -0800263 // Create a 32-bit solo.
264 static RegStorage Solo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700265 return RegStorage(k32BitSolo, reg_num & kRegTypeMask);
266 }
267
268 // Create a floating point 32-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100269 static constexpr RegStorage FloatSolo32(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700270 return RegStorage(k32BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800271 }
272
Mark Mendellfe945782014-05-22 09:52:36 -0400273 // Create a 128-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100274 static constexpr RegStorage Solo128(int reg_num) {
Mark Mendellfe945782014-05-22 09:52:36 -0400275 return RegStorage(k128BitSolo, reg_num & kRegTypeMask);
276 }
277
buzbee2700f7e2014-03-07 09:46:20 -0800278 // Create a 64-bit solo.
Vladimir Marko83642482014-06-11 12:12:07 +0100279 static constexpr RegStorage Solo64(int reg_num) {
buzbee091cc402014-03-31 10:14:40 -0700280 return RegStorage(k64BitSolo, reg_num & kRegTypeMask);
281 }
282
283 // Create a floating point 64-bit solo.
284 static RegStorage FloatSolo64(int reg_num) {
285 return RegStorage(k64BitSolo, (reg_num & kRegNumMask) | kFloatingPoint);
buzbee2700f7e2014-03-07 09:46:20 -0800286 }
287
Vladimir Marko83642482014-06-11 12:12:07 +0100288 static constexpr RegStorage InvalidReg() {
buzbee2700f7e2014-03-07 09:46:20 -0800289 return RegStorage(kInvalid);
290 }
291
Vladimir Marko83642482014-06-11 12:12:07 +0100292 static constexpr uint16_t RegNum(int raw_reg_bits) {
buzbee091cc402014-03-31 10:14:40 -0700293 return raw_reg_bits & kRegNumMask;
294 }
295
Vladimir Marko83642482014-06-11 12:12:07 +0100296 constexpr int GetRawBits() const {
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000297 return reg_;
298 }
299
Vladimir Marko83642482014-06-11 12:12:07 +0100300 size_t StorageSize() const {
buzbee091cc402014-03-31 10:14:40 -0700301 switch (reg_ & kShapeMask) {
302 case kInvalid: return 0;
303 case k32BitSolo: return 4;
304 case k64BitSolo: return 8;
305 case k64BitPair: return 8; // Is this useful? Might want to disallow taking size of pair.
306 case k128BitSolo: return 16;
307 case k256BitSolo: return 32;
308 case k512BitSolo: return 64;
309 case k1024BitSolo: return 128;
Serban Constantinescued65c5e2014-05-22 15:10:18 +0100310 default: LOG(FATAL) << "Unexpected shape";
buzbee091cc402014-03-31 10:14:40 -0700311 }
312 return 0;
313 }
314
Bill Buzbee00e1ec62014-02-27 23:44:13 +0000315 private:
316 uint16_t reg_;
317};
318
319} // namespace art
320
321#endif // ART_COMPILER_DEX_REG_STORAGE_H_