Debian Codegraph @

js/src/jit/arm64/vixl/Instructions-vixl.cpp

raw
1 // Copyright 2015, ARM Limited
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are met:
6 //
7 // * Redistributions of source code must retain the above copyright notice,
8 // this list of conditions and the following disclaimer.
9 // * Redistributions in binary form must reproduce the above copyright notice,
10 // this list of conditions and the following disclaimer in the documentation
11 // and/or other materials provided with the distribution.
12 // * Neither the name of ARM Limited nor the names of its contributors may be
13 // used to endorse or promote products derived from this software without
14 // specific prior written permission.
15 //
16 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS CONTRIBUTORS "AS IS" AND
17 // ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 // WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 // DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
20 // FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 // DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
22 // SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
23 // CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 // OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
25 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26
27 #include "jit/arm64/vixl/Instructions-vixl.h"
28
29 #include "jit/arm64/vixl/Assembler-vixl.h"
30
31 namespace vixl {
32
33 static uint64_t RepeatBitsAcrossReg(unsigned reg_size,
34 uint64_t value,
35 unsigned width) {
36 VIXL_ASSERT((width == 2) || (width == 4) || (width == 8) || (width == 16) ||
37 (width == 32));
38 VIXL_ASSERT((reg_size == kWRegSize) || (reg_size == kXRegSize));
39 uint64_t result = value & ((UINT64_C(1) << width) - 1);
40 for (unsigned i = width; i < reg_size; i *= 2) {
41 result |= (result << i);
42 }
43 return result;
44 }
45
46
47 bool Instruction::IsLoad() const {
48 if (Mask(LoadStoreAnyFMask) != LoadStoreAnyFixed) {
49 return false;
50 }
51
52 if (Mask(LoadStorePairAnyFMask) == LoadStorePairAnyFixed) {
53 return Mask(LoadStorePairLBit) != 0;
54 } else {
55 LoadStoreOp op = static_cast<LoadStoreOp>(Mask(LoadStoreMask));
56 switch (op) {
57 case LDRB_w:
58 case LDRH_w:
59 case LDR_w:
60 case LDR_x:
61 case LDRSB_w:
62 case LDRSB_x:
63 case LDRSH_w:
64 case LDRSH_x:
65 case LDRSW_x:
66 case LDR_b:
67 case LDR_h:
68 case LDR_s:
69 case LDR_d:
70 case LDR_q: return true;
71 default: return false;
72 }
73 }
74 }
75
76
77 bool Instruction::IsStore() const {
78 if (Mask(LoadStoreAnyFMask) != LoadStoreAnyFixed) {
79 return false;
80 }
81
82 if (Mask(LoadStorePairAnyFMask) == LoadStorePairAnyFixed) {
83 return Mask(LoadStorePairLBit) == 0;
84 } else {
85 LoadStoreOp op = static_cast<LoadStoreOp>(Mask(LoadStoreMask));
86 switch (op) {
87 case STRB_w:
88 case STRH_w:
89 case STR_w:
90 case STR_x:
91 case STR_b:
92 case STR_h:
93 case STR_s:
94 case STR_d:
95 case STR_q: return true;
96 default: return false;
97 }
98 }
99 }
100
101
102 // Logical immediates can't encode zero, so a return value of zero is used to
103 // indicate a failure case. Specifically, where the constraints on imm_s are
104 // not met.
105 uint64_t Instruction::ImmLogical() const {
106 unsigned reg_size = SixtyFourBits() ? kXRegSize : kWRegSize;
107 int32_t n = BitN();
108 int32_t imm_s = ImmSetBits();
109 int32_t imm_r = ImmRotate();
110
111 // An integer is constructed from the n, imm_s and imm_r bits according to
112 // the following table:
113 //
114 // N imms immr size S R
115 // 1 ssssss rrrrrr 64 UInt(ssssss) UInt(rrrrrr)
116 // 0 0sssss xrrrrr 32 UInt(sssss) UInt(rrrrr)
117 // 0 10ssss xxrrrr 16 UInt(ssss) UInt(rrrr)
118 // 0 110sss xxxrrr 8 UInt(sss) UInt(rrr)
119 // 0 1110ss xxxxrr 4 UInt(ss) UInt(rr)
120 // 0 11110s xxxxxr 2 UInt(s) UInt(r)
121 // (s bits must not be all set)
122 //
123 // A pattern is constructed of size bits, where the least significant S+1
124 // bits are set. The pattern is rotated right by R, and repeated across a
125 // 32 or 64-bit value, depending on destination register width.
126 //
127
128 if (n == 1) {
129 if (imm_s == 0x3f) {
130 return 0;
131 }
132 uint64_t bits = (UINT64_C(1) << (imm_s + 1)) - 1;
133 return RotateRight(bits, imm_r, 64);
134 } else {
135 if ((imm_s >> 1) == 0x1f) {
136 return 0;
137 }
138 for (int width = 0x20; width >= 0x2; width >>= 1) {
139 if ((imm_s & width) == 0) {
140 int mask = width - 1;
141 if ((imm_s & mask) == mask) {
142 return 0;
143 }
144 uint64_t bits = (UINT64_C(1) << ((imm_s & mask) + 1)) - 1;
145 return RepeatBitsAcrossReg(reg_size,
146 RotateRight(bits, imm_r & mask, width),
147 width);
148 }
149 }
150 }
151 VIXL_UNREACHABLE();
152 return 0;
153 }
154
155
156 uint32_t Instruction::ImmNEONabcdefgh() const {
157 return ImmNEONabc() << 5 | ImmNEONdefgh();
158 }
159
160
161 float Instruction::Imm8ToFP32(uint32_t imm8) {
162 // Imm8: abcdefgh (8 bits)
163 // Single: aBbb.bbbc.defg.h000.0000.0000.0000.0000 (32 bits)
164 // where B is b ^ 1
165 uint32_t bits = imm8;
166 uint32_t bit7 = (bits >> 7) & 0x1;
167 uint32_t bit6 = (bits >> 6) & 0x1;
168 uint32_t bit5_to_0 = bits & 0x3f;
169 uint32_t result = (bit7 << 31) | ((32 - bit6) << 25) | (bit5_to_0 << 19);
170
171 return RawbitsToFloat(result);
172 }
173
174
175 float Instruction::ImmFP32() const {
176 return Imm8ToFP32(ImmFP());
177 }
178
179
180 double Instruction::Imm8ToFP64(uint32_t imm8) {
181 // Imm8: abcdefgh (8 bits)
182 // Double: aBbb.bbbb.bbcd.efgh.0000.0000.0000.0000
183 // 0000.0000.0000.0000.0000.0000.0000.0000 (64 bits)
184 // where B is b ^ 1
185 uint32_t bits = imm8;
186 uint64_t bit7 = (bits >> 7) & 0x1;
187 uint64_t bit6 = (bits >> 6) & 0x1;
188 uint64_t bit5_to_0 = bits & 0x3f;
189 uint64_t result = (bit7 << 63) | ((256 - bit6) << 54) | (bit5_to_0 << 48);
190
191 return RawbitsToDouble(result);
192 }
193
194
195 double Instruction::ImmFP64() const {
196 return Imm8ToFP64(ImmFP());
197 }
198
199
200 float Instruction::ImmNEONFP32() const {
201 return Imm8ToFP32(ImmNEONabcdefgh());
202 }
203
204
205 double Instruction::ImmNEONFP64() const {
206 return Imm8ToFP64(ImmNEONabcdefgh());
207 }
208
209 unsigned CalcLSPairDataSize(LoadStorePairOp op) {
210 VIXL_STATIC_ASSERT(kXRegSizeInBytes == kDRegSizeInBytes);
211 VIXL_STATIC_ASSERT(kWRegSizeInBytes == kSRegSizeInBytes);
212 switch (op) {
213 case STP_q:
214 case LDP_q: return kQRegSizeInBytesLog2;
215 case STP_x:
216 case LDP_x:
217 case STP_d:
218 case LDP_d: return kXRegSizeInBytesLog2;
219 default: return kWRegSizeInBytesLog2;
220 }
221 }
222
223
224 int Instruction::ImmBranchRangeBitwidth(ImmBranchType branch_type) {
225 switch (branch_type) {
226 case UncondBranchType:
227 return ImmUncondBranch_width;
228 case CondBranchType:
229 return ImmCondBranch_width;
230 case CompareBranchType:
231 return ImmCmpBranch_width;
232 case TestBranchType:
233 return ImmTestBranch_width;
234 default:
235 VIXL_UNREACHABLE();
236 return 0;
237 }
238 }
239
240
241 int32_t Instruction::ImmBranchForwardRange(ImmBranchType branch_type) {
242 int32_t encoded_max = 1 << (ImmBranchRangeBitwidth(branch_type) - 1);
243 return encoded_max * kInstructionSize;
244 }
245
246
247 bool Instruction::IsValidImmPCOffset(ImmBranchType branch_type,
248 int64_t offset) {
249 return IsIntN(ImmBranchRangeBitwidth(branch_type), offset);
250 }
251
252 ImmBranchRangeType Instruction::ImmBranchTypeToRange(ImmBranchType branch_type)
253 {
254 switch (branch_type) {
255 case UncondBranchType:
256 return UncondBranchRangeType;
257 case CondBranchType:
258 case CompareBranchType:
259 return CondBranchRangeType;
260 case TestBranchType:
261 return TestBranchRangeType;
262 default:
263 return UnknownBranchRangeType;
264 }
265 }
266
267 int32_t Instruction::ImmBranchMaxForwardOffset(ImmBranchRangeType range_type)
268 {
269 // Branches encode a pc-relative two's complement number of 32-bit
270 // instructions. Compute the number of bytes corresponding to the largest
271 // positive number of instructions that can be encoded.
272 switch(range_type) {
273 case TestBranchRangeType:
274 return ((1 << ImmTestBranch_width) - 1) / 2 * kInstructionSize;
275 case CondBranchRangeType:
276 return ((1 << ImmCondBranch_width) - 1) / 2 * kInstructionSize;
277 case UncondBranchRangeType:
278 return ((1 << ImmUncondBranch_width) - 1) / 2 * kInstructionSize;
279 default:
280 VIXL_UNREACHABLE();
281 return 0;
282 }
283 }
284
285 int32_t Instruction::ImmBranchMinBackwardOffset(ImmBranchRangeType range_type)
286 {
287 switch(range_type) {
288 case TestBranchRangeType:
289 return -int32_t(1 << ImmTestBranch_width) / int32_t(2 * kInstructionSize);
290 case CondBranchRangeType:
291 return -int32_t(1 << ImmCondBranch_width) / int32_t(2 * kInstructionSize);
292 case UncondBranchRangeType:
293 return -int32_t(1 << ImmUncondBranch_width) / int32_t(2 * kInstructionSize);
294 default:
295 VIXL_UNREACHABLE();
296 return 0;
297 }
298 }
299
300 const Instruction* Instruction::ImmPCOffsetTarget() const {
301 const Instruction * base = this;
302 ptrdiff_t offset;
303 if (IsPCRelAddressing()) {
304 // ADR and ADRP.
305 offset = ImmPCRel();
306 if (Mask(PCRelAddressingMask) == ADRP) {
307 base = AlignDown(base, kPageSize);
308 offset *= kPageSize;
309 } else {
310 VIXL_ASSERT(Mask(PCRelAddressingMask) == ADR);
311 }
312 } else {
313 // All PC-relative branches.
314 VIXL_ASSERT(BranchType() != UnknownBranchType);
315 // Relative branch offsets are instruction-size-aligned.
316 offset = ImmBranch() << kInstructionSizeLog2;
317 }
318 return base + offset;
319 }
320
321
322 int Instruction::ImmBranch() const {
323 switch (BranchType()) {
324 case CondBranchType: return ImmCondBranch();
325 case UncondBranchType: return ImmUncondBranch();
326 case CompareBranchType: return ImmCmpBranch();
327 case TestBranchType: return ImmTestBranch();
328 default: VIXL_UNREACHABLE();
329 }
330 return 0;
331 }
332
333
334 void Instruction::SetImmPCOffsetTarget(const Instruction* target) {
335 if (IsPCRelAddressing()) {
336 SetPCRelImmTarget(target);
337 } else {
338 SetBranchImmTarget(target);
339 }
340 }
341
342
343 void Instruction::SetPCRelImmTarget(const Instruction* target) {
344 ptrdiff_t imm21;
345 if ((Mask(PCRelAddressingMask) == ADR)) {
346 imm21 = target - this;
347 } else {
348 VIXL_ASSERT(Mask(PCRelAddressingMask) == ADRP);
349 uintptr_t this_page = reinterpret_cast<uintptr_t>(this) / kPageSize;
350 uintptr_t target_page = reinterpret_cast<uintptr_t>(target) / kPageSize;
351 imm21 = target_page - this_page;
352 }
353 Instr imm = Assembler::ImmPCRelAddress(static_cast<int32_t>(imm21));
354
355 SetInstructionBits(Mask(~ImmPCRel_mask) | imm);
356 }
357
358
359 void Instruction::SetBranchImmTarget(const Instruction* target) {
360 VIXL_ASSERT(((target - this) & 3) == 0);
361 Instr branch_imm = 0;
362 uint32_t imm_mask = 0;
363 int offset = static_cast<int>((target - this) >> kInstructionSizeLog2);
364 switch (BranchType()) {
365 case CondBranchType: {
366 branch_imm = Assembler::ImmCondBranch(offset);
367 imm_mask = ImmCondBranch_mask;
368 break;
369 }
370 case UncondBranchType: {
371 branch_imm = Assembler::ImmUncondBranch(offset);
372 imm_mask = ImmUncondBranch_mask;
373 break;
374 }
375 case CompareBranchType: {
376 branch_imm = Assembler::ImmCmpBranch(offset);
377 imm_mask = ImmCmpBranch_mask;
378 break;
379 }
380 case TestBranchType: {
381 branch_imm = Assembler::ImmTestBranch(offset);
382 imm_mask = ImmTestBranch_mask;
383 break;
384 }
385 default: VIXL_UNREACHABLE();
386 }
387 SetInstructionBits(Mask(~imm_mask) | branch_imm);
388 }
389
390
391 void Instruction::SetImmLLiteral(const Instruction* source) {
392 VIXL_ASSERT(IsWordAligned(source));
393 ptrdiff_t offset = (source - this) >> kLiteralEntrySizeLog2;
394 Instr imm = Assembler::ImmLLiteral(static_cast<int>(offset));
395 Instr mask = ImmLLiteral_mask;
396
397 SetInstructionBits(Mask(~mask) | imm);
398 }
399
400
401 VectorFormat VectorFormatHalfWidth(const VectorFormat vform) {
402 VIXL_ASSERT(vform == kFormat8H || vform == kFormat4S || vform == kFormat2D ||
403 vform == kFormatH || vform == kFormatS || vform == kFormatD);
404 switch (vform) {
405 case kFormat8H: return kFormat8B;
406 case kFormat4S: return kFormat4H;
407 case kFormat2D: return kFormat2S;
408 case kFormatH: return kFormatB;
409 case kFormatS: return kFormatH;
410 case kFormatD: return kFormatS;
411 default: VIXL_UNREACHABLE(); return kFormatUndefined;
412 }
413 }
414
415
416 VectorFormat VectorFormatDoubleWidth(const VectorFormat vform) {
417 VIXL_ASSERT(vform == kFormat8B || vform == kFormat4H || vform == kFormat2S ||
418 vform == kFormatB || vform == kFormatH || vform == kFormatS);
419 switch (vform) {
420 case kFormat8B: return kFormat8H;
421 case kFormat4H: return kFormat4S;
422 case kFormat2S: return kFormat2D;
423 case kFormatB: return kFormatH;
424 case kFormatH: return kFormatS;
425 case kFormatS: return kFormatD;
426 default: VIXL_UNREACHABLE(); return kFormatUndefined;
427 }
428 }
429
430
431 VectorFormat VectorFormatFillQ(const VectorFormat vform) {
432 switch (vform) {
433 case kFormatB:
434 case kFormat8B:
435 case kFormat16B: return kFormat16B;
436 case kFormatH:
437 case kFormat4H:
438 case kFormat8H: return kFormat8H;
439 case kFormatS:
440 case kFormat2S:
441 case kFormat4S: return kFormat4S;
442 case kFormatD:
443 case kFormat1D:
444 case kFormat2D: return kFormat2D;
445 default: VIXL_UNREACHABLE(); return kFormatUndefined;
446 }
447 }
448
449 VectorFormat VectorFormatHalfWidthDoubleLanes(const VectorFormat vform) {
450 switch (vform) {
451 case kFormat4H: return kFormat8B;
452 case kFormat8H: return kFormat16B;
453 case kFormat2S: return kFormat4H;
454 case kFormat4S: return kFormat8H;
455 case kFormat1D: return kFormat2S;
456 case kFormat2D: return kFormat4S;
457 default: VIXL_UNREACHABLE(); return kFormatUndefined;
458 }
459 }
460
461 VectorFormat VectorFormatDoubleLanes(const VectorFormat vform) {
462 VIXL_ASSERT(vform == kFormat8B || vform == kFormat4H || vform == kFormat2S);
463 switch (vform) {
464 case kFormat8B: return kFormat16B;
465 case kFormat4H: return kFormat8H;
466 case kFormat2S: return kFormat4S;
467 default: VIXL_UNREACHABLE(); return kFormatUndefined;
468 }
469 }
470
471
472 VectorFormat VectorFormatHalfLanes(const VectorFormat vform) {
473 VIXL_ASSERT(vform == kFormat16B || vform == kFormat8H || vform == kFormat4S);
474 switch (vform) {
475 case kFormat16B: return kFormat8B;
476 case kFormat8H: return kFormat4H;
477 case kFormat4S: return kFormat2S;
478 default: VIXL_UNREACHABLE(); return kFormatUndefined;
479 }
480 }
481
482
483 VectorFormat ScalarFormatFromLaneSize(int laneSize) {
484 switch (laneSize) {
485 case 8: return kFormatB;
486 case 16: return kFormatH;
487 case 32: return kFormatS;
488 case 64: return kFormatD;
489 default: VIXL_UNREACHABLE(); return kFormatUndefined;
490 }
491 }
492
493
494 unsigned RegisterSizeInBitsFromFormat(VectorFormat vform) {
495 VIXL_ASSERT(vform != kFormatUndefined);
496 switch (vform) {
497 case kFormatB: return kBRegSize;
498 case kFormatH: return kHRegSize;
499 case kFormatS: return kSRegSize;
500 case kFormatD: return kDRegSize;
501 case kFormat8B:
502 case kFormat4H:
503 case kFormat2S:
504 case kFormat1D: return kDRegSize;
505 default: return kQRegSize;
506 }
507 }
508
509
510 unsigned RegisterSizeInBytesFromFormat(VectorFormat vform) {
511 return RegisterSizeInBitsFromFormat(vform) / 8;
512 }
513
514
515 unsigned LaneSizeInBitsFromFormat(VectorFormat vform) {
516 VIXL_ASSERT(vform != kFormatUndefined);
517 switch (vform) {
518 case kFormatB:
519 case kFormat8B:
520 case kFormat16B: return 8;
521 case kFormatH:
522 case kFormat4H:
523 case kFormat8H: return 16;
524 case kFormatS:
525 case kFormat2S:
526 case kFormat4S: return 32;
527 case kFormatD:
528 case kFormat1D:
529 case kFormat2D: return 64;
530 default: VIXL_UNREACHABLE(); return 0;
531 }
532 }
533
534
535 int LaneSizeInBytesFromFormat(VectorFormat vform) {
536 return LaneSizeInBitsFromFormat(vform) / 8;
537 }
538
539
540 int LaneSizeInBytesLog2FromFormat(VectorFormat vform) {
541 VIXL_ASSERT(vform != kFormatUndefined);
542 switch (vform) {
543 case kFormatB:
544 case kFormat8B:
545 case kFormat16B: return 0;
546 case kFormatH:
547 case kFormat4H:
548 case kFormat8H: return 1;
549 case kFormatS:
550 case kFormat2S:
551 case kFormat4S: return 2;
552 case kFormatD:
553 case kFormat1D:
554 case kFormat2D: return 3;
555 default: VIXL_UNREACHABLE(); return 0;
556 }
557 }
558
559
560 int LaneCountFromFormat(VectorFormat vform) {
561 VIXL_ASSERT(vform != kFormatUndefined);
562 switch (vform) {
563 case kFormat16B: return 16;
564 case kFormat8B:
565 case kFormat8H: return 8;
566 case kFormat4H:
567 case kFormat4S: return 4;
568 case kFormat2S:
569 case kFormat2D: return 2;
570 case kFormat1D:
571 case kFormatB:
572 case kFormatH:
573 case kFormatS:
574 case kFormatD: return 1;
575 default: VIXL_UNREACHABLE(); return 0;
576 }
577 }
578
579
580 int MaxLaneCountFromFormat(VectorFormat vform) {
581 VIXL_ASSERT(vform != kFormatUndefined);
582 switch (vform) {
583 case kFormatB:
584 case kFormat8B:
585 case kFormat16B: return 16;
586 case kFormatH:
587 case kFormat4H:
588 case kFormat8H: return 8;
589 case kFormatS:
590 case kFormat2S:
591 case kFormat4S: return 4;
592 case kFormatD:
593 case kFormat1D:
594 case kFormat2D: return 2;
595 default: VIXL_UNREACHABLE(); return 0;
596 }
597 }
598
599
600 // Does 'vform' indicate a vector format or a scalar format?
601 bool IsVectorFormat(VectorFormat vform) {
602 VIXL_ASSERT(vform != kFormatUndefined);
603 switch (vform) {
604 case kFormatB:
605 case kFormatH:
606 case kFormatS:
607 case kFormatD: return false;
608 default: return true;
609 }
610 }
611
612
613 int64_t MaxIntFromFormat(VectorFormat vform) {
614 return INT64_MAX >> (64 - LaneSizeInBitsFromFormat(vform));
615 }
616
617
618 int64_t MinIntFromFormat(VectorFormat vform) {
619 return INT64_MIN >> (64 - LaneSizeInBitsFromFormat(vform));
620 }
621
622
623 uint64_t MaxUintFromFormat(VectorFormat vform) {
624 return UINT64_MAX >> (64 - LaneSizeInBitsFromFormat(vform));
625 }
626 } // namespace vixl
627