1 //===-- ARMAddressingModes.h - ARM Addressing Modes -------------*- C++ -*-===//
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7 //===----------------------------------------------------------------------===//
9 // This file contains the ARM addressing mode implementation stuff.
11 //===----------------------------------------------------------------------===//
13 #ifndef LLVM_LIB_TARGET_ARM_MCTARGETDESC_ARMADDRESSINGMODES_H
14 #define LLVM_LIB_TARGET_ARM_MCTARGETDESC_ARMADDRESSINGMODES_H
16 #include "llvm/ADT/APFloat.h"
17 #include "llvm/ADT/APInt.h"
18 #include "llvm/ADT/bit.h"
19 #include "llvm/Support/ErrorHandling.h"
20 #include "llvm/Support/MathExtras.h"
25 /// ARM_AM - ARM Addressing Mode Stuff
42 inline const char *getAddrOpcStr(AddrOpc Op
) { return Op
== sub
? "-" : ""; }
44 inline const StringRef
getShiftOpcStr(ShiftOpc Op
) {
46 default: llvm_unreachable("Unknown shift opc!");
47 case ARM_AM::asr
: return "asr";
48 case ARM_AM::lsl
: return "lsl";
49 case ARM_AM::lsr
: return "lsr";
50 case ARM_AM::ror
: return "ror";
51 case ARM_AM::rrx
: return "rrx";
52 case ARM_AM::uxtw
: return "uxtw";
56 inline unsigned getShiftOpcEncoding(ShiftOpc Op
) {
58 default: llvm_unreachable("Unknown shift opc!");
59 case ARM_AM::asr
: return 2;
60 case ARM_AM::lsl
: return 0;
61 case ARM_AM::lsr
: return 1;
62 case ARM_AM::ror
: return 3;
74 inline const char *getAMSubModeStr(AMSubMode Mode
) {
76 default: llvm_unreachable("Unknown addressing sub-mode!");
77 case ARM_AM::ia
: return "ia";
78 case ARM_AM::ib
: return "ib";
79 case ARM_AM::da
: return "da";
80 case ARM_AM::db
: return "db";
84 //===--------------------------------------------------------------------===//
85 // Addressing Mode #1: shift_operand with registers
86 //===--------------------------------------------------------------------===//
88 // This 'addressing mode' is used for arithmetic instructions. It can
89 // represent things like:
91 // reg [asr|lsl|lsr|ror|rrx] reg
92 // reg [asr|lsl|lsr|ror|rrx] imm
94 // This is stored three operands [rega, regb, opc]. The first is the base
95 // reg, the second is the shift amount (or reg0 if not present or imm). The
96 // third operand encodes the shift opcode and the imm if a reg isn't present.
98 inline unsigned getSORegOpc(ShiftOpc ShOp
, unsigned Imm
) {
99 return ShOp
| (Imm
<< 3);
101 inline unsigned getSORegOffset(unsigned Op
) { return Op
>> 3; }
102 inline ShiftOpc
getSORegShOp(unsigned Op
) { return (ShiftOpc
)(Op
& 7); }
104 /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return
105 /// the 8-bit imm value.
106 inline unsigned getSOImmValImm(unsigned Imm
) { return Imm
& 0xFF; }
107 /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return
108 /// the rotate amount.
109 inline unsigned getSOImmValRot(unsigned Imm
) { return (Imm
>> 8) * 2; }
111 /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,
112 /// computing the rotate amount to use. If this immediate value cannot be
113 /// handled with a single shifter-op, determine a good rotate amount that will
114 /// take a maximal chunk of bits out of the immediate.
115 inline unsigned getSOImmValRotate(unsigned Imm
) {
116 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
118 if ((Imm
& ~255U) == 0) return 0;
120 // Use CTZ to compute the rotate amount.
121 unsigned TZ
= llvm::countr_zero(Imm
);
123 // Rotate amount must be even. Something like 0x200 must be rotated 8 bits,
125 unsigned RotAmt
= TZ
& ~1;
127 // If we can handle this spread, return it.
128 if ((llvm::rotr
<uint32_t>(Imm
, RotAmt
) & ~255U) == 0)
129 return (32-RotAmt
)&31; // HW rotates right, not left.
131 // For values like 0xF000000F, we should ignore the low 6 bits, then
134 unsigned TZ2
= llvm::countr_zero(Imm
& ~63U);
135 unsigned RotAmt2
= TZ2
& ~1;
136 if ((llvm::rotr
<uint32_t>(Imm
, RotAmt2
) & ~255U) == 0)
137 return (32-RotAmt2
)&31; // HW rotates right, not left.
140 // Otherwise, we have no way to cover this span of bits with a single
141 // shifter_op immediate. Return a chunk of bits that will be useful to
143 return (32-RotAmt
)&31; // HW rotates right, not left.
146 /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit
147 /// into an shifter_operand immediate operand, return the 12-bit encoding for
148 /// it. If not, return -1.
149 inline int getSOImmVal(unsigned Arg
) {
150 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
152 if ((Arg
& ~255U) == 0) return Arg
;
154 unsigned RotAmt
= getSOImmValRotate(Arg
);
156 // If this cannot be handled with a single shifter_op, bail out.
157 if (llvm::rotr
<uint32_t>(~255U, RotAmt
) & Arg
)
160 // Encode this correctly.
161 return llvm::rotl
<uint32_t>(Arg
, RotAmt
) | ((RotAmt
>> 1) << 8);
164 /// isSOImmTwoPartVal - Return true if the specified value can be obtained by
165 /// or'ing together two SOImmVal's.
166 inline bool isSOImmTwoPartVal(unsigned V
) {
167 // If this can be handled with a single shifter_op, bail out.
168 V
= llvm::rotr
<uint32_t>(~255U, getSOImmValRotate(V
)) & V
;
172 // If this can be handled with two shifter_op's, accept.
173 V
= llvm::rotr
<uint32_t>(~255U, getSOImmValRotate(V
)) & V
;
177 /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,
178 /// return the first chunk of it.
179 inline unsigned getSOImmTwoPartFirst(unsigned V
) {
180 return llvm::rotr
<uint32_t>(255U, getSOImmValRotate(V
)) & V
;
183 /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,
184 /// return the second chunk of it.
185 inline unsigned getSOImmTwoPartSecond(unsigned V
) {
186 // Mask out the first hunk.
187 V
= llvm::rotr
<uint32_t>(~255U, getSOImmValRotate(V
)) & V
;
190 assert(V
== (llvm::rotr
<uint32_t>(255U, getSOImmValRotate(V
)) & V
));
194 /// isSOImmTwoPartValNeg - Return true if the specified value can be obtained
195 /// by two SOImmVal, that -V = First + Second.
196 /// "R+V" can be optimized to (sub (sub R, First), Second).
197 /// "R=V" can be optimized to (sub (mvn R, ~(-First)), Second).
198 inline bool isSOImmTwoPartValNeg(unsigned V
) {
200 if (!isSOImmTwoPartVal(-V
))
202 // Return false if ~(-First) is not a SoImmval.
203 First
= getSOImmTwoPartFirst(-V
);
205 return !(llvm::rotr
<uint32_t>(~255U, getSOImmValRotate(First
)) & First
);
208 /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed
209 /// by a left shift. Returns the shift amount to use.
210 inline unsigned getThumbImmValShift(unsigned Imm
) {
211 // 8-bit (or less) immediates are trivially immediate operand with a shift
213 if ((Imm
& ~255U) == 0) return 0;
215 // Use CTZ to compute the shift amount.
216 return llvm::countr_zero(Imm
);
219 /// isThumbImmShiftedVal - Return true if the specified value can be obtained
220 /// by left shifting a 8-bit immediate.
221 inline bool isThumbImmShiftedVal(unsigned V
) {
222 // If this can be handled with
223 V
= (~255U << getThumbImmValShift(V
)) & V
;
227 /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed
228 /// by a left shift. Returns the shift amount to use.
229 inline unsigned getThumbImm16ValShift(unsigned Imm
) {
230 // 16-bit (or less) immediates are trivially immediate operand with a shift
232 if ((Imm
& ~65535U) == 0) return 0;
234 // Use CTZ to compute the shift amount.
235 return llvm::countr_zero(Imm
);
238 /// isThumbImm16ShiftedVal - Return true if the specified value can be
239 /// obtained by left shifting a 16-bit immediate.
240 inline bool isThumbImm16ShiftedVal(unsigned V
) {
241 // If this can be handled with
242 V
= (~65535U << getThumbImm16ValShift(V
)) & V
;
246 /// getThumbImmNonShiftedVal - If V is a value that satisfies
247 /// isThumbImmShiftedVal, return the non-shiftd value.
248 inline unsigned getThumbImmNonShiftedVal(unsigned V
) {
249 return V
>> getThumbImmValShift(V
);
253 /// getT2SOImmValSplat - Return the 12-bit encoded representation
254 /// if the specified value can be obtained by splatting the low 8 bits
255 /// into every other byte or every byte of a 32-bit value. i.e.,
256 /// 00000000 00000000 00000000 abcdefgh control = 0
257 /// 00000000 abcdefgh 00000000 abcdefgh control = 1
258 /// abcdefgh 00000000 abcdefgh 00000000 control = 2
259 /// abcdefgh abcdefgh abcdefgh abcdefgh control = 3
260 /// Return -1 if none of the above apply.
261 /// See ARM Reference Manual A6.3.2.
262 inline int getT2SOImmValSplatVal(unsigned V
) {
265 if ((V
& 0xffffff00) == 0)
268 // If the value is zeroes in the first byte, just shift those off
269 Vs
= ((V
& 0xff) == 0) ? V
>> 8 : V
;
270 // Any passing value only has 8 bits of payload, splatted across the word
272 // Likewise, any passing values have the payload splatted into the 3rd byte
273 u
= Imm
| (Imm
<< 16);
277 return (((Vs
== V
) ? 1 : 2) << 8) | Imm
;
280 if (Vs
== (u
| (u
<< 8)))
281 return (3 << 8) | Imm
;
286 /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the
287 /// specified value is a rotated 8-bit value. Return -1 if no rotation
288 /// encoding is possible.
289 /// See ARM Reference Manual A6.3.2.
290 inline int getT2SOImmValRotateVal(unsigned V
) {
291 unsigned RotAmt
= llvm::countl_zero(V
);
295 // If 'Arg' can be handled with a single shifter_op return the value.
296 if ((llvm::rotr
<uint32_t>(0xff000000U
, RotAmt
) & V
) == V
)
297 return (llvm::rotr
<uint32_t>(V
, 24 - RotAmt
) & 0x7f) |
303 /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit
304 /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit
305 /// encoding for it. If not, return -1.
306 /// See ARM Reference Manual A6.3.2.
307 inline int getT2SOImmVal(unsigned Arg
) {
308 // If 'Arg' is an 8-bit splat, then get the encoded value.
309 int Splat
= getT2SOImmValSplatVal(Arg
);
313 // If 'Arg' can be handled with a single shifter_op return the value.
314 int Rot
= getT2SOImmValRotateVal(Arg
);
321 inline unsigned getT2SOImmValRotate(unsigned V
) {
322 if ((V
& ~255U) == 0) return 0;
323 // Use CTZ to compute the rotate amount.
324 unsigned RotAmt
= llvm::countr_zero(V
);
325 return (32 - RotAmt
) & 31;
328 inline bool isT2SOImmTwoPartVal(unsigned Imm
) {
330 // Passing values can be any combination of splat values and shifter
331 // values. If this can be handled with a single shifter or splat, bail
332 // out. Those should be handled directly, not with a two-part val.
333 if (getT2SOImmValSplatVal(V
) != -1)
335 V
= llvm::rotr
<uint32_t>(~255U, getT2SOImmValRotate(V
)) & V
;
339 // If this can be handled as an immediate, accept.
340 if (getT2SOImmVal(V
) != -1) return true;
342 // Likewise, try masking out a splat value first.
344 if (getT2SOImmValSplatVal(V
& 0xff00ff00U
) != -1)
346 else if (getT2SOImmValSplatVal(V
& 0x00ff00ffU
) != -1)
348 // If what's left can be handled as an immediate, accept.
349 if (getT2SOImmVal(V
) != -1) return true;
351 // Otherwise, do not accept.
355 inline unsigned getT2SOImmTwoPartFirst(unsigned Imm
) {
356 assert (isT2SOImmTwoPartVal(Imm
) &&
357 "Immedate cannot be encoded as two part immediate!");
358 // Try a shifter operand as one part
359 unsigned V
= llvm::rotr
<uint32_t>(~255, getT2SOImmValRotate(Imm
)) & Imm
;
360 // If the rest is encodable as an immediate, then return it.
361 if (getT2SOImmVal(V
) != -1) return V
;
363 // Try masking out a splat value first.
364 if (getT2SOImmValSplatVal(Imm
& 0xff00ff00U
) != -1)
365 return Imm
& 0xff00ff00U
;
367 // The other splat is all that's left as an option.
368 assert (getT2SOImmValSplatVal(Imm
& 0x00ff00ffU
) != -1);
369 return Imm
& 0x00ff00ffU
;
372 inline unsigned getT2SOImmTwoPartSecond(unsigned Imm
) {
373 // Mask out the first hunk
374 Imm
^= getT2SOImmTwoPartFirst(Imm
);
375 // Return what's left
376 assert (getT2SOImmVal(Imm
) != -1 &&
377 "Unable to encode second part of T2 two part SO immediate");
382 //===--------------------------------------------------------------------===//
383 // Addressing Mode #2
384 //===--------------------------------------------------------------------===//
386 // This is used for most simple load/store instructions.
388 // addrmode2 := reg +/- reg shop imm
389 // addrmode2 := reg +/- imm12
391 // The first operand is always a Reg. The second operand is a reg if in
392 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
393 // in bit 12, the immediate in bits 0-11, and the shift op in 13-15. The
394 // fourth operand 16-17 encodes the index mode.
396 // If this addressing mode is a frame index (before prolog/epilog insertion
397 // and code rewriting), this operand will have the form: FI#, reg0, <offs>
398 // with no shift amount for the frame offset.
400 inline unsigned getAM2Opc(AddrOpc Opc
, unsigned Imm12
, ShiftOpc SO
,
401 unsigned IdxMode
= 0) {
402 assert(Imm12
< (1 << 12) && "Imm too large!");
403 bool isSub
= Opc
== sub
;
404 return Imm12
| ((int)isSub
<< 12) | (SO
<< 13) | (IdxMode
<< 16) ;
406 inline unsigned getAM2Offset(unsigned AM2Opc
) {
407 return AM2Opc
& ((1 << 12)-1);
409 inline AddrOpc
getAM2Op(unsigned AM2Opc
) {
410 return ((AM2Opc
>> 12) & 1) ? sub
: add
;
412 inline ShiftOpc
getAM2ShiftOpc(unsigned AM2Opc
) {
413 return (ShiftOpc
)((AM2Opc
>> 13) & 7);
415 inline unsigned getAM2IdxMode(unsigned AM2Opc
) { return (AM2Opc
>> 16); }
417 //===--------------------------------------------------------------------===//
418 // Addressing Mode #3
419 //===--------------------------------------------------------------------===//
421 // This is used for sign-extending loads, and load/store-pair instructions.
423 // addrmode3 := reg +/- reg
424 // addrmode3 := reg +/- imm8
426 // The first operand is always a Reg. The second operand is a reg if in
427 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
428 // in bit 8, the immediate in bits 0-7. The fourth operand 9-10 encodes the
431 /// getAM3Opc - This function encodes the addrmode3 opc field.
432 inline unsigned getAM3Opc(AddrOpc Opc
, unsigned char Offset
,
433 unsigned IdxMode
= 0) {
434 bool isSub
= Opc
== sub
;
435 return ((int)isSub
<< 8) | Offset
| (IdxMode
<< 9);
437 inline unsigned char getAM3Offset(unsigned AM3Opc
) { return AM3Opc
& 0xFF; }
438 inline AddrOpc
getAM3Op(unsigned AM3Opc
) {
439 return ((AM3Opc
>> 8) & 1) ? sub
: add
;
441 inline unsigned getAM3IdxMode(unsigned AM3Opc
) { return (AM3Opc
>> 9); }
443 //===--------------------------------------------------------------------===//
444 // Addressing Mode #4
445 //===--------------------------------------------------------------------===//
447 // This is used for load / store multiple instructions.
449 // addrmode4 := reg, <mode>
451 // The four modes are:
452 // IA - Increment after
453 // IB - Increment before
454 // DA - Decrement after
455 // DB - Decrement before
456 // For VFP instructions, only the IA and DB modes are valid.
458 inline AMSubMode
getAM4SubMode(unsigned Mode
) {
459 return (AMSubMode
)(Mode
& 0x7);
462 inline unsigned getAM4ModeImm(AMSubMode SubMode
) { return (int)SubMode
; }
464 //===--------------------------------------------------------------------===//
465 // Addressing Mode #5
466 //===--------------------------------------------------------------------===//
468 // This is used for coprocessor instructions, such as FP load/stores.
470 // addrmode5 := reg +/- imm8*4
472 // The first operand is always a Reg. The second operand encodes the
473 // operation (add or subtract) in bit 8 and the immediate in bits 0-7.
475 /// getAM5Opc - This function encodes the addrmode5 opc field.
476 inline unsigned getAM5Opc(AddrOpc Opc
, unsigned char Offset
) {
477 bool isSub
= Opc
== sub
;
478 return ((int)isSub
<< 8) | Offset
;
480 inline unsigned char getAM5Offset(unsigned AM5Opc
) { return AM5Opc
& 0xFF; }
481 inline AddrOpc
getAM5Op(unsigned AM5Opc
) {
482 return ((AM5Opc
>> 8) & 1) ? sub
: add
;
485 //===--------------------------------------------------------------------===//
486 // Addressing Mode #5 FP16
487 //===--------------------------------------------------------------------===//
489 // This is used for coprocessor instructions, such as 16-bit FP load/stores.
491 // addrmode5fp16 := reg +/- imm8*2
493 // The first operand is always a Reg. The second operand encodes the
494 // operation (add or subtract) in bit 8 and the immediate in bits 0-7.
496 /// getAM5FP16Opc - This function encodes the addrmode5fp16 opc field.
497 inline unsigned getAM5FP16Opc(AddrOpc Opc
, unsigned char Offset
) {
498 bool isSub
= Opc
== sub
;
499 return ((int)isSub
<< 8) | Offset
;
501 inline unsigned char getAM5FP16Offset(unsigned AM5Opc
) {
502 return AM5Opc
& 0xFF;
504 inline AddrOpc
getAM5FP16Op(unsigned AM5Opc
) {
505 return ((AM5Opc
>> 8) & 1) ? sub
: add
;
508 //===--------------------------------------------------------------------===//
509 // Addressing Mode #6
510 //===--------------------------------------------------------------------===//
512 // This is used for NEON load / store instructions.
514 // addrmode6 := reg with optional alignment
516 // This is stored in two operands [regaddr, align]. The first is the
517 // address register. The second operand is the value of the alignment
518 // specifier in bytes or zero if no explicit alignment.
519 // Valid alignments depend on the specific instruction.
521 //===--------------------------------------------------------------------===//
522 // NEON/MVE Modified Immediates
523 //===--------------------------------------------------------------------===//
525 // Several NEON and MVE instructions (e.g., VMOV) take a "modified immediate"
526 // vector operand, where a small immediate encoded in the instruction
527 // specifies a full NEON vector value. These modified immediates are
528 // represented here as encoded integers. The low 8 bits hold the immediate
529 // value; bit 12 holds the "Op" field of the instruction, and bits 11-8 hold
530 // the "Cmode" field of the instruction. The interfaces below treat the
531 // Op and Cmode values as a single 5-bit value.
533 inline unsigned createVMOVModImm(unsigned OpCmode
, unsigned Val
) {
534 return (OpCmode
<< 8) | Val
;
536 inline unsigned getVMOVModImmOpCmode(unsigned ModImm
) {
537 return (ModImm
>> 8) & 0x1f;
539 inline unsigned getVMOVModImmVal(unsigned ModImm
) { return ModImm
& 0xff; }
541 /// decodeVMOVModImm - Decode a NEON/MVE modified immediate value into the
542 /// element value and the element size in bits. (If the element size is
543 /// smaller than the vector, it is splatted into all the elements.)
544 inline uint64_t decodeVMOVModImm(unsigned ModImm
, unsigned &EltBits
) {
545 unsigned OpCmode
= getVMOVModImmOpCmode(ModImm
);
546 unsigned Imm8
= getVMOVModImmVal(ModImm
);
549 if (OpCmode
== 0xe) {
550 // 8-bit vector elements
553 } else if ((OpCmode
& 0xc) == 0x8) {
554 // 16-bit vector elements
555 unsigned ByteNum
= (OpCmode
& 0x6) >> 1;
556 Val
= Imm8
<< (8 * ByteNum
);
558 } else if ((OpCmode
& 0x8) == 0) {
559 // 32-bit vector elements, zero with one byte set
560 unsigned ByteNum
= (OpCmode
& 0x6) >> 1;
561 Val
= Imm8
<< (8 * ByteNum
);
563 } else if ((OpCmode
& 0xe) == 0xc) {
564 // 32-bit vector elements, one byte with low bits set
565 unsigned ByteNum
= 1 + (OpCmode
& 0x1);
566 Val
= (Imm8
<< (8 * ByteNum
)) | (0xffff >> (8 * (2 - ByteNum
)));
568 } else if (OpCmode
== 0x1e) {
569 // 64-bit vector elements
570 for (unsigned ByteNum
= 0; ByteNum
< 8; ++ByteNum
) {
571 if ((ModImm
>> ByteNum
) & 1)
572 Val
|= (uint64_t)0xff << (8 * ByteNum
);
576 llvm_unreachable("Unsupported VMOV immediate");
581 // Generic validation for single-byte immediate (0X00, 00X0, etc).
582 inline bool isNEONBytesplat(unsigned Value
, unsigned Size
) {
583 assert(Size
>= 1 && Size
<= 4 && "Invalid size");
585 for (unsigned i
= 0; i
< Size
; ++i
) {
586 if (Value
& 0xff) count
++;
592 /// Checks if Value is a correct immediate for instructions like VBIC/VORR.
593 inline bool isNEONi16splat(unsigned Value
) {
596 // i16 value with set bits only in one byte X0 or 0X.
597 return Value
== 0 || isNEONBytesplat(Value
, 2);
600 // Encode NEON 16 bits Splat immediate for instructions like VBIC/VORR
601 inline unsigned encodeNEONi16splat(unsigned Value
) {
602 assert(isNEONi16splat(Value
) && "Invalid NEON splat value");
604 Value
= (Value
>> 8) | 0xa00;
610 /// Checks if Value is a correct immediate for instructions like VBIC/VORR.
611 inline bool isNEONi32splat(unsigned Value
) {
612 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
613 return Value
== 0 || isNEONBytesplat(Value
, 4);
616 /// Encode NEON 32 bits Splat immediate for instructions like VBIC/VORR.
617 inline unsigned encodeNEONi32splat(unsigned Value
) {
618 assert(isNEONi32splat(Value
) && "Invalid NEON splat value");
619 if (Value
>= 0x100 && Value
<= 0xff00)
620 Value
= (Value
>> 8) | 0x200;
621 else if (Value
> 0xffff && Value
<= 0xff0000)
622 Value
= (Value
>> 16) | 0x400;
623 else if (Value
> 0xffffff)
624 Value
= (Value
>> 24) | 0x600;
628 //===--------------------------------------------------------------------===//
629 // Floating-point Immediates
631 inline float getFPImmFloat(unsigned Imm
) {
632 // We expect an 8-bit binary encoding of a floating-point number here.
634 uint8_t Sign
= (Imm
>> 7) & 0x1;
635 uint8_t Exp
= (Imm
>> 4) & 0x7;
636 uint8_t Mantissa
= Imm
& 0xf;
638 // 8-bit FP IEEE Float Encoding
639 // abcd efgh aBbbbbbc defgh000 00000000 00000000
644 I
|= ((Exp
& 0x4) != 0 ? 0 : 1) << 30;
645 I
|= ((Exp
& 0x4) != 0 ? 0x1f : 0) << 25;
646 I
|= (Exp
& 0x3) << 23;
648 return bit_cast
<float>(I
);
651 /// getFP16Imm - Return an 8-bit floating-point version of the 16-bit
652 /// floating-point value. If the value cannot be represented as an 8-bit
653 /// floating-point value, then return -1.
654 inline int getFP16Imm(const APInt
&Imm
) {
655 uint32_t Sign
= Imm
.lshr(15).getZExtValue() & 1;
656 int32_t Exp
= (Imm
.lshr(10).getSExtValue() & 0x1f) - 15; // -14 to 15
657 int64_t Mantissa
= Imm
.getZExtValue() & 0x3ff; // 10 bits
659 // We can handle 4 bits of mantissa.
660 // mantissa = (16+UInt(e:f:g:h))/16.
665 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
666 if (Exp
< -3 || Exp
> 4)
668 Exp
= ((Exp
+3) & 0x7) ^ 4;
670 return ((int)Sign
<< 7) | (Exp
<< 4) | Mantissa
;
673 inline int getFP16Imm(const APFloat
&FPImm
) {
674 return getFP16Imm(FPImm
.bitcastToAPInt());
677 /// If this is a FP16Imm encoded as a fp32 value, return the 8-bit encoding
678 /// for it. Otherwise return -1 like getFP16Imm.
679 inline int getFP32FP16Imm(const APInt
&Imm
) {
680 if (Imm
.getActiveBits() > 16)
682 return ARM_AM::getFP16Imm(Imm
.trunc(16));
685 inline int getFP32FP16Imm(const APFloat
&FPImm
) {
686 return getFP32FP16Imm(FPImm
.bitcastToAPInt());
689 /// getFP32Imm - Return an 8-bit floating-point version of the 32-bit
690 /// floating-point value. If the value cannot be represented as an 8-bit
691 /// floating-point value, then return -1.
692 inline int getFP32Imm(const APInt
&Imm
) {
693 uint32_t Sign
= Imm
.lshr(31).getZExtValue() & 1;
694 int32_t Exp
= (Imm
.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
695 int64_t Mantissa
= Imm
.getZExtValue() & 0x7fffff; // 23 bits
697 // We can handle 4 bits of mantissa.
698 // mantissa = (16+UInt(e:f:g:h))/16.
699 if (Mantissa
& 0x7ffff)
702 if ((Mantissa
& 0xf) != Mantissa
)
705 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
706 if (Exp
< -3 || Exp
> 4)
708 Exp
= ((Exp
+3) & 0x7) ^ 4;
710 return ((int)Sign
<< 7) | (Exp
<< 4) | Mantissa
;
713 inline int getFP32Imm(const APFloat
&FPImm
) {
714 return getFP32Imm(FPImm
.bitcastToAPInt());
717 /// getFP64Imm - Return an 8-bit floating-point version of the 64-bit
718 /// floating-point value. If the value cannot be represented as an 8-bit
719 /// floating-point value, then return -1.
720 inline int getFP64Imm(const APInt
&Imm
) {
721 uint64_t Sign
= Imm
.lshr(63).getZExtValue() & 1;
722 int64_t Exp
= (Imm
.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
723 uint64_t Mantissa
= Imm
.getZExtValue() & 0xfffffffffffffULL
;
725 // We can handle 4 bits of mantissa.
726 // mantissa = (16+UInt(e:f:g:h))/16.
727 if (Mantissa
& 0xffffffffffffULL
)
730 if ((Mantissa
& 0xf) != Mantissa
)
733 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
734 if (Exp
< -3 || Exp
> 4)
736 Exp
= ((Exp
+3) & 0x7) ^ 4;
738 return ((int)Sign
<< 7) | (Exp
<< 4) | Mantissa
;
741 inline int getFP64Imm(const APFloat
&FPImm
) {
742 return getFP64Imm(FPImm
.bitcastToAPInt());
745 } // end namespace ARM_AM
746 } // end namespace llvm