Debian Codegraph @

js/src/jit/loong64/Architecture-loong64.h

raw
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*-
2 * vim: set ts=8 sts=2 et sw=2 tw=80:
3 * This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this
5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6
7 #ifndef jit_loong64_Architecture_loong64_h
8 #define jit_loong64_Architecture_loong64_h
9
10 #include "mozilla/MathAlgorithms.h"
11
12 #include <algorithm>
13 #include <iterator>
14
15 #include "jit/shared/Architecture-shared.h"
16
17 #include "js/Utility.h"
18
19 namespace js {
20 namespace jit {
21
22 // LoongArch64 has 32 64-bit integer registers, r0 though r31.
23 // The program counter is not accessible as a register.
24 //
25 // SIMD and scalar floating-point registers share a register bank.
26 // Floating-point registers are f0 through f31.
27 // 128 bit SIMD registers are vr0 through vr31.
28 // e.g., f0 is the bottom 64 bits of vr0.
29
30 // LoongArch64 INT Register Convention:
31 // Name Alias Usage
32 // $r0 $zero Constant zero
33 // $r1 $ra Return address
34 // $r2 $tp TLS
35 // $r3 $sp Stack pointer
36 // $r4-$r11 $a0-$a7 Argument registers
37 // $r4-$r5 $v0-$v1 Return values
38 // $r12-$r20 $t0-$t8 Temporary registers
39 // $r21 $x Reserved
40 // $r22 $fp Frame pointer
41 // $r23-$r31 $s0-$s8 Callee-saved registers
42
43 // LoongArch64 FP Register Convention:
44 // Name Alias Usage
45 // $f0-$f7 $fa0-$fa7 Argument registers
46 // $f0-$f1 $fv0-$fv1 Return values
47 // $f8-f23 $ft0-$ft15 Temporary registers
48 // $f24-$f31 $fs0-$fs7 Callee-saved registers
49
50 class Registers {
51 public:
52 enum RegisterID {
53 r0 = 0,
54 r1,
55 r2,
56 r3,
57 r4,
58 r5,
59 r6,
60 r7,
61 r8,
62 r9,
63 r10,
64 r11,
65 r12,
66 r13,
67 r14,
68 r15,
69 r16,
70 r17,
71 r18,
72 r19,
73 r20,
74 r21,
75 r22,
76 r23,
77 r24,
78 r25,
79 r26,
80 r27,
81 r28,
82 r29,
83 r30,
84 r31,
85 zero = r0,
86 ra = r1,
87 tp = r2,
88 sp = r3,
89 a0 = r4,
90 a1 = r5,
91 a2 = r6,
92 a3 = r7,
93 a4 = r8,
94 a5 = r9,
95 a6 = r10,
96 a7 = r11,
97 t0 = r12,
98 t1 = r13,
99 t2 = r14,
100 t3 = r15,
101 t4 = r16,
102 t5 = r17,
103 t6 = r18,
104 t7 = r19,
105 t8 = r20,
106 rx = r21,
107 fp = r22,
108 s0 = r23,
109 s1 = r24,
110 s2 = r25,
111 s3 = r26,
112 s4 = r27,
113 s5 = r28,
114 s6 = r29,
115 s7 = r30,
116 s8 = r31,
117 invalid_reg,
118 };
119 typedef uint8_t Code;
120 typedef RegisterID Encoding;
121 typedef uint32_t SetType;
122
123 static const Encoding StackPointer = sp;
124 static const Encoding Invalid = invalid_reg;
125
126 // Content spilled during bailouts.
127 union RegisterContent {
128 uintptr_t r;
129 };
130
131 static uint32_t SetSize(SetType x) {
132 static_assert(sizeof(SetType) == 4, "SetType must be 32 bits");
133 return mozilla::CountPopulation32(x);
134 }
135 static uint32_t FirstBit(SetType x) {
136 return mozilla::CountTrailingZeroes32(x);
137 }
138 static uint32_t LastBit(SetType x) {
139 return 31 - mozilla::CountLeadingZeroes32(x);
140 }
141
142 static const char* GetName(uint32_t code) {
143 static const char* const Names[] = {
144 "zero", "ra", "tp", "sp", "a0", "a1", "a2", "a3", "a4", "a5", "a6",
145 "a7", "t0", "t1", "t2", "t3", "t4", "t5", "t6", "t7", "t8", "rx",
146 "fp", "s0", "s1", "s2", "s3", "s4", "s5", "s6", "s7", "s8"};
147 static_assert(Total == std::size(Names), "Table is the correct size");
148 if (code >= Total) {
149 return "invalid";
150 }
151 return Names[code];
152 }
153
154 static Code FromName(const char* name);
155
156 static const uint32_t Total = 32;
157 static const uint32_t TotalPhys = 32;
158 static const uint32_t Allocatable =
159 23; // No named special-function registers.
160
161 static const SetType AllMask = 0xFFFFFFFF;
162 static const SetType NoneMask = 0x0;
163
164 static const SetType ArgRegMask =
165 (1U << Registers::a0) | (1U << Registers::a1) | (1U << Registers::a2) |
166 (1U << Registers::a3) | (1U << Registers::a4) | (1U << Registers::a5) |
167 (1U << Registers::a6) | (1U << Registers::a7);
168
169 static const SetType VolatileMask =
170 (1U << Registers::a0) | (1U << Registers::a1) | (1U << Registers::a2) |
171 (1U << Registers::a3) | (1U << Registers::a4) | (1U << Registers::a5) |
172 (1U << Registers::a6) | (1U << Registers::a7) | (1U << Registers::t0) |
173 (1U << Registers::t1) | (1U << Registers::t2) | (1U << Registers::t3) |
174 (1U << Registers::t4) | (1U << Registers::t5) | (1U << Registers::t6);
175
176 // We use this constant to save registers when entering functions. This
177 // is why $ra is added here even though it is not "Non Volatile".
178 static const SetType NonVolatileMask =
179 (1U << Registers::ra) | (1U << Registers::fp) | (1U << Registers::s0) |
180 (1U << Registers::s1) | (1u << Registers::s2) | (1U << Registers::s3) |
181 (1U << Registers::s4) | (1U << Registers::s5) | (1U << Registers::s6) |
182 (1U << Registers::s7) | (1U << Registers::s8);
183
184 static const SetType NonAllocatableMask =
185 (1U << Registers::zero) | // Always be zero.
186 (1U << Registers::t7) | // First scratch register.
187 (1U << Registers::t8) | // Second scratch register.
188 (1U << Registers::rx) | // Reserved Register.
189 (1U << Registers::ra) | (1U << Registers::tp) | (1U << Registers::sp) |
190 (1U << Registers::fp);
191
192 static const SetType WrapperMask = VolatileMask;
193
194 // Registers returned from a JS -> JS call.
195 static const SetType JSCallMask = (1 << Registers::a2);
196
197 // Registers returned from a JS -> C call.
198 static const SetType CallMask = (1 << Registers::a0);
199
200 static const SetType AllocatableMask = AllMask & ~NonAllocatableMask;
201 };
202
203 // Smallest integer type that can hold a register bitmask.
204 typedef uint32_t PackedRegisterMask;
205
206 template <typename T>
207 class TypedRegisterSet;
208
209 class FloatRegisters {
210 public:
211 enum FPRegisterID {
212 f0 = 0,
213 f1,
214 f2,
215 f3,
216 f4,
217 f5,
218 f6,
219 f7,
220 f8,
221 f9,
222 f10,
223 f11,
224 f12,
225 f13,
226 f14,
227 f15,
228 f16,
229 f17,
230 f18,
231 f19,
232 f20,
233 f21,
234 f22,
235 f23, // Scratch register.
236 f24,
237 f25,
238 f26,
239 f27,
240 f28,
241 f29,
242 f30,
243 f31,
244 };
245
246 // Eight bits: (invalid << 7) | (kind << 5) | encoding
247 typedef uint8_t Code;
248 typedef FPRegisterID Encoding;
249 typedef uint64_t SetType;
250
251 enum Kind : uint8_t { Double, Single, NumTypes };
252
253 static constexpr Code Invalid = 0x80;
254
255 static const char* GetName(uint32_t code) {
256 static const char* const Names[] = {
257 "f0", "f1", "f2", "f3", "f4", "f5", "f6", "f7",
258 "f8", "f9", "f10", "f11", "f12", "f13", "f14", "f15",
259 "f16", "f17", "f18", "f19", "f20", "f21", "f22", "f23",
260 "f24", "f25", "f26", "f27", "f28", "f29", "f30", "f31"};
261 static_assert(TotalPhys == std::size(Names), "Table is the correct size");
262 if (code >= Total) {
263 return "invalid";
264 }
265 // Single and double fpu registers share same names on LoongArch.
266 return Names[code % TotalPhys];
267 }
268
269 static Code FromName(const char* name);
270
271 static const uint32_t TotalPhys = 32;
272 static const uint32_t Total = TotalPhys * NumTypes;
273 static const uint32_t Allocatable = 31; // Without f23, the scratch register.
274
275 static_assert(sizeof(SetType) * 8 >= Total,
276 "SetType should be large enough to enumerate all registers.");
277
278 // Magic values which are used to duplicate a mask of physical register for
279 // a specific type of register. A multiplication is used to copy and shift
280 // the bits of the physical register mask.
281 static const SetType SpreadSingle = SetType(1)
282 << (uint32_t(Single) * TotalPhys);
283 static const SetType SpreadDouble = SetType(1)
284 << (uint32_t(Double) * TotalPhys);
285 static const SetType Spread = SpreadSingle | SpreadDouble;
286
287 static const SetType AllPhysMask = ((SetType(1) << TotalPhys) - 1);
288 static const SetType AllMask = AllPhysMask * Spread;
289 static const SetType AllSingleMask = AllPhysMask * SpreadSingle;
290 static const SetType AllDoubleMask = AllPhysMask * SpreadDouble;
291 static const SetType NoneMask = SetType(0);
292
293 // TODO(loong64): Much less than ARM64 here.
294 static const SetType NonVolatileMask =
295 SetType((1U << FloatRegisters::f24) | (1U << FloatRegisters::f25) |
296 (1U << FloatRegisters::f26) | (1U << FloatRegisters::f27) |
297 (1U << FloatRegisters::f28) | (1U << FloatRegisters::f29) |
298 (1U << FloatRegisters::f30) | (1U << FloatRegisters::f31)) *
299 Spread;
300
301 static const SetType VolatileMask = AllMask & ~NonVolatileMask;
302
303 static const SetType WrapperMask = VolatileMask;
304
305 // f23 is the scratch register.
306 static const SetType NonAllocatableMask =
307 (SetType(1) << FloatRegisters::f23) * Spread;
308
309 static const SetType AllocatableMask = AllMask & ~NonAllocatableMask;
310
311 // Content spilled during bailouts.
312 union RegisterContent {
313 float s;
314 double d;
315 };
316
317 static constexpr Encoding encoding(Code c) {
318 // assert() not available in constexpr function.
319 // assert(c < Total);
320 return Encoding(c & 31);
321 }
322
323 static constexpr Kind kind(Code c) {
324 // assert() not available in constexpr function.
325 // assert(c < Total && ((c >> 5) & 3) < NumTypes);
326 return Kind((c >> 5) & 3);
327 }
328
329 static constexpr Code fromParts(uint32_t encoding, uint32_t kind,
330 uint32_t invalid) {
331 return Code((invalid << 7) | (kind << 5) | encoding);
332 }
333 };
334
335 static const uint32_t SpillSlotSize =
336 std::max(sizeof(Registers::RegisterContent),
337 sizeof(FloatRegisters::RegisterContent));
338
339 static constexpr uint32_t ShadowStackSpace = 0;
340 static const uint32_t SizeOfReturnAddressAfterCall = 0;
341
342 // When our only strategy for far jumps is to encode the offset directly, and
343 // not insert any jump islands during assembly for even further jumps, then the
344 // architecture restricts us to -2^27 .. 2^27-4, to fit into a signed 28-bit
345 // value. We further reduce this range to allow the far-jump inserting code to
346 // have some breathing room.
347 static const uint32_t JumpImmediateRange = ((1 << 27) - (20 * 1024 * 1024));
348
349 struct FloatRegister {
350 typedef FloatRegisters Codes;
351 typedef size_t Code;
352 typedef Codes::Encoding Encoding;
353 typedef Codes::SetType SetType;
354
355 static uint32_t SetSize(SetType x) {
356 static_assert(sizeof(SetType) == 8, "SetType must be 64 bits");
357 x |= x >> FloatRegisters::TotalPhys;
358 x &= FloatRegisters::AllPhysMask;
359 return mozilla::CountPopulation32(x);
360 }
361
362 static uint32_t FirstBit(SetType x) {
363 static_assert(sizeof(SetType) == 8, "SetType");
364 return mozilla::CountTrailingZeroes64(x);
365 }
366 static uint32_t LastBit(SetType x) {
367 static_assert(sizeof(SetType) == 8, "SetType");
368 return 63 - mozilla::CountLeadingZeroes64(x);
369 }
370
371 private:
372 // These fields only hold valid values: an invalid register is always
373 // represented as a valid encoding and kind with the invalid_ bit set.
374 uint8_t encoding_; // 32 encodings
375 uint8_t kind_; // Double, Single; more later
376 bool invalid_;
377
378 typedef Codes::Kind Kind;
379
380 public:
381 constexpr FloatRegister(Encoding encoding, Kind kind)
382 : encoding_(encoding), kind_(kind), invalid_(false) {
383 // assert(uint32_t(encoding) < Codes::TotalPhys);
384 }
385
386 constexpr FloatRegister()
387 : encoding_(0), kind_(FloatRegisters::Double), invalid_(true) {}
388
389 static FloatRegister FromCode(uint32_t i) {
390 MOZ_ASSERT(i < Codes::Total);
391 return FloatRegister(FloatRegisters::encoding(i), FloatRegisters::kind(i));
392 }
393
394 bool isSingle() const {
395 MOZ_ASSERT(!invalid_);
396 return kind_ == FloatRegisters::Single;
397 }
398 bool isDouble() const {
399 MOZ_ASSERT(!invalid_);
400 return kind_ == FloatRegisters::Double;
401 }
402 bool isSimd128() const {
403 MOZ_ASSERT(!invalid_);
404 return false;
405 }
406 bool isInvalid() const { return invalid_; }
407
408 FloatRegister asSingle() const {
409 MOZ_ASSERT(!invalid_);
410 return FloatRegister(Encoding(encoding_), FloatRegisters::Single);
411 }
412 FloatRegister asDouble() const {
413 MOZ_ASSERT(!invalid_);
414 return FloatRegister(Encoding(encoding_), FloatRegisters::Double);
415 }
416 FloatRegister asSimd128() const { MOZ_CRASH(); }
417
418 constexpr uint32_t size() const {
419 MOZ_ASSERT(!invalid_);
420 if (kind_ == FloatRegisters::Double) {
421 return sizeof(double);
422 }
423 MOZ_ASSERT(kind_ == FloatRegisters::Single);
424 return sizeof(float);
425 }
426
427 constexpr Code code() const {
428 // assert(!invalid_);
429 return Codes::fromParts(encoding_, kind_, invalid_);
430 }
431
432 constexpr Encoding encoding() const {
433 MOZ_ASSERT(!invalid_);
434 return Encoding(encoding_);
435 }
436
437 const char* name() const { return FloatRegisters::GetName(code()); }
438 bool volatile_() const {
439 MOZ_ASSERT(!invalid_);
440 return !!((SetType(1) << code()) & FloatRegisters::VolatileMask);
441 }
442 constexpr bool operator!=(FloatRegister other) const {
443 return code() != other.code();
444 }
445 constexpr bool operator==(FloatRegister other) const {
446 return code() == other.code();
447 }
448
449 bool aliases(FloatRegister other) const {
450 return other.encoding_ == encoding_;
451 }
452 // Ensure that two floating point registers' types are equivalent.
453 bool equiv(FloatRegister other) const {
454 MOZ_ASSERT(!invalid_);
455 return kind_ == other.kind_;
456 }
457
458 uint32_t numAliased() const { return Codes::NumTypes; }
459 uint32_t numAlignedAliased() { return numAliased(); }
460
461 FloatRegister aliased(uint32_t aliasIdx) {
462 MOZ_ASSERT(!invalid_);
463 MOZ_ASSERT(aliasIdx < numAliased());
464 return FloatRegister(Encoding(encoding_),
465 Kind((aliasIdx + kind_) % numAliased()));
466 }
467 FloatRegister alignedAliased(uint32_t aliasIdx) {
468 MOZ_ASSERT(aliasIdx < numAliased());
469 return aliased(aliasIdx);
470 }
471 SetType alignedOrDominatedAliasedSet() const {
472 return Codes::Spread << encoding_;
473 }
474
475 static constexpr RegTypeName DefaultType = RegTypeName::Float64;
476
477 template <RegTypeName Name = DefaultType>
478 static SetType LiveAsIndexableSet(SetType s) {
479 return SetType(0);
480 }
481
482 template <RegTypeName Name = DefaultType>
483 static SetType AllocatableAsIndexableSet(SetType s) {
484 static_assert(Name != RegTypeName::Any, "Allocatable set are not iterable");
485 return LiveAsIndexableSet<Name>(s);
486 }
487
488 static TypedRegisterSet<FloatRegister> ReduceSetForPush(
489 const TypedRegisterSet<FloatRegister>& s);
490 static uint32_t GetPushSizeInBytes(const TypedRegisterSet<FloatRegister>& s);
491 uint32_t getRegisterDumpOffsetInBytes();
492 };
493
494 template <>
495 inline FloatRegister::SetType
496 FloatRegister::LiveAsIndexableSet<RegTypeName::Float32>(SetType set) {
497 return set & FloatRegisters::AllSingleMask;
498 }
499
500 template <>
501 inline FloatRegister::SetType
502 FloatRegister::LiveAsIndexableSet<RegTypeName::Float64>(SetType set) {
503 return set & FloatRegisters::AllDoubleMask;
504 }
505
506 template <>
507 inline FloatRegister::SetType
508 FloatRegister::LiveAsIndexableSet<RegTypeName::Any>(SetType set) {
509 return set;
510 }
511
512 // LoongArch doesn't have double registers that cannot be treated as float32.
513 inline bool hasUnaliasedDouble() { return false; }
514
515 // LoongArch doesn't have double registers that alias multiple floats.
516 inline bool hasMultiAlias() { return false; }
517
518 uint32_t GetLOONG64Flags();
519
520 } // namespace jit
521 } // namespace js
522
523 #endif /* jit_loong64_Architecture_loong64_h */