1 //===- ARMAddressingModes.h - ARM Addressing Modes --------------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file contains the ARM addressing mode implementation stuff.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_TARGET_ARM_ARMADDRESSINGMODES_H
15 #define LLVM_TARGET_ARM_ARMADDRESSINGMODES_H
17 #include "llvm/CodeGen/SelectionDAGNodes.h"
18 #include "llvm/Support/MathExtras.h"
23 /// ARM_AM - ARM Addressing Mode Stuff
38 static inline const char *getAddrOpcStr(AddrOpc Op) {
39 return Op == sub ? "-" : "";
42 static inline const char *getShiftOpcStr(ShiftOpc Op) {
44 default: assert(0 && "Unknown shift opc!");
45 case ARM_AM::asr: return "asr";
46 case ARM_AM::lsl: return "lsl";
47 case ARM_AM::lsr: return "lsr";
48 case ARM_AM::ror: return "ror";
49 case ARM_AM::rrx: return "rrx";
53 static inline ShiftOpc getShiftOpcForNode(SDValue N) {
54 switch (N.getOpcode()) {
55 default: return ARM_AM::no_shift;
56 case ISD::SHL: return ARM_AM::lsl;
57 case ISD::SRL: return ARM_AM::lsr;
58 case ISD::SRA: return ARM_AM::asr;
59 case ISD::ROTR: return ARM_AM::ror;
60 //case ISD::ROTL: // Only if imm -> turn into ROTR.
61 // Can't handle RRX here, because it would require folding a flag into
62 // the addressing mode. :( This causes us to miss certain things.
63 //case ARMISD::RRX: return ARM_AM::rrx;
75 static inline const char *getAMSubModeStr(AMSubMode Mode) {
77 default: assert(0 && "Unknown addressing sub-mode!");
78 case ARM_AM::ia: return "ia";
79 case ARM_AM::ib: return "ib";
80 case ARM_AM::da: return "da";
81 case ARM_AM::db: return "db";
85 /// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits.
87 static inline unsigned rotr32(unsigned Val, unsigned Amt) {
88 assert(Amt < 32 && "Invalid rotate amount");
89 return (Val >> Amt) | (Val << ((32-Amt)&31));
92 /// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits.
94 static inline unsigned rotl32(unsigned Val, unsigned Amt) {
95 assert(Amt < 32 && "Invalid rotate amount");
96 return (Val << Amt) | (Val >> ((32-Amt)&31));
99 //===--------------------------------------------------------------------===//
100 // Addressing Mode #1: shift_operand with registers
101 //===--------------------------------------------------------------------===//
103 // This 'addressing mode' is used for arithmetic instructions. It can
104 // represent things like:
106 // reg [asr|lsl|lsr|ror|rrx] reg
107 // reg [asr|lsl|lsr|ror|rrx] imm
109 // This is stored three operands [rega, regb, opc]. The first is the base
110 // reg, the second is the shift amount (or reg0 if not present or imm). The
111 // third operand encodes the shift opcode and the imm if a reg isn't present.
113 static inline unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm) {
114 return ShOp | (Imm << 3);
116 static inline unsigned getSORegOffset(unsigned Op) {
119 static inline ShiftOpc getSORegShOp(unsigned Op) {
120 return (ShiftOpc)(Op & 7);
123 /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return
124 /// the 8-bit imm value.
125 static inline unsigned getSOImmValImm(unsigned Imm) {
128 /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return
129 /// the rotate amount.
130 static inline unsigned getSOImmValRot(unsigned Imm) {
131 return (Imm >> 8) * 2;
134 /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,
135 /// computing the rotate amount to use. If this immediate value cannot be
136 /// handled with a single shifter-op, determine a good rotate amount that will
137 /// take a maximal chunk of bits out of the immediate.
138 static inline unsigned getSOImmValRotate(unsigned Imm) {
139 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
141 if ((Imm & ~255U) == 0) return 0;
143 // Use CTZ to compute the rotate amount.
144 unsigned TZ = CountTrailingZeros_32(Imm);
146 // Rotate amount must be even. Something like 0x200 must be rotated 8 bits,
148 unsigned RotAmt = TZ & ~1;
150 // If we can handle this spread, return it.
151 if ((rotr32(Imm, RotAmt) & ~255U) == 0)
152 return (32-RotAmt)&31; // HW rotates right, not left.
154 // For values like 0xF000000F, we should skip the first run of ones, then
157 unsigned TrailingOnes = CountTrailingZeros_32(~Imm);
158 if (TrailingOnes != 32) { // Avoid overflow on 0xFFFFFFFF
159 // Restart the search for a high-order bit after the initial seconds of
161 unsigned TZ2 = CountTrailingZeros_32(Imm & ~((1 << TrailingOnes)-1));
163 // Rotate amount must be even.
164 unsigned RotAmt2 = TZ2 & ~1;
166 // If this fits, use it.
167 if (RotAmt2 != 32 && (rotr32(Imm, RotAmt2) & ~255U) == 0)
168 return (32-RotAmt2)&31; // HW rotates right, not left.
172 // Otherwise, we have no way to cover this span of bits with a single
173 // shifter_op immediate. Return a chunk of bits that will be useful to
175 return (32-RotAmt)&31; // HW rotates right, not left.
178 /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit
179 /// into an shifter_operand immediate operand, return the 12-bit encoding for
180 /// it. If not, return -1.
181 static inline int getSOImmVal(unsigned Arg) {
182 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
184 if ((Arg & ~255U) == 0) return Arg;
186 unsigned RotAmt = getSOImmValRotate(Arg);
188 // If this cannot be handled with a single shifter_op, bail out.
189 if (rotr32(~255U, RotAmt) & Arg)
192 // Encode this correctly.
193 return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8);
196 /// isSOImmTwoPartVal - Return true if the specified value can be obtained by
197 /// or'ing together two SOImmVal's.
198 static inline bool isSOImmTwoPartVal(unsigned V) {
199 // If this can be handled with a single shifter_op, bail out.
200 V = rotr32(~255U, getSOImmValRotate(V)) & V;
204 // If this can be handled with two shifter_op's, accept.
205 V = rotr32(~255U, getSOImmValRotate(V)) & V;
209 /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,
210 /// return the first chunk of it.
211 static inline unsigned getSOImmTwoPartFirst(unsigned V) {
212 return rotr32(255U, getSOImmValRotate(V)) & V;
215 /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,
216 /// return the second chunk of it.
217 static inline unsigned getSOImmTwoPartSecond(unsigned V) {
218 // Mask out the first hunk.
219 V = rotr32(~255U, getSOImmValRotate(V)) & V;
222 assert(V == (rotr32(255U, getSOImmValRotate(V)) & V));
226 /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed
227 /// by a left shift. Returns the shift amount to use.
228 static inline unsigned getThumbImmValShift(unsigned Imm) {
229 // 8-bit (or less) immediates are trivially immediate operand with a shift
231 if ((Imm & ~255U) == 0) return 0;
233 // Use CTZ to compute the shift amount.
234 return CountTrailingZeros_32(Imm);
237 /// isThumbImmShiftedVal - Return true if the specified value can be obtained
238 /// by left shifting a 8-bit immediate.
239 static inline bool isThumbImmShiftedVal(unsigned V) {
240 // If this can be handled with
241 V = (~255U << getThumbImmValShift(V)) & V;
245 /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed
246 /// by a left shift. Returns the shift amount to use.
247 static inline unsigned getThumbImm16ValShift(unsigned Imm) {
248 // 16-bit (or less) immediates are trivially immediate operand with a shift
250 if ((Imm & ~65535U) == 0) return 0;
252 // Use CTZ to compute the shift amount.
253 return CountTrailingZeros_32(Imm);
256 /// isThumbImm16ShiftedVal - Return true if the specified value can be
257 /// obtained by left shifting a 16-bit immediate.
258 static inline bool isThumbImm16ShiftedVal(unsigned V) {
259 // If this can be handled with
260 V = (~65535U << getThumbImm16ValShift(V)) & V;
264 /// getThumbImmNonShiftedVal - If V is a value that satisfies
265 /// isThumbImmShiftedVal, return the non-shiftd value.
266 static inline unsigned getThumbImmNonShiftedVal(unsigned V) {
267 return V >> getThumbImmValShift(V);
271 /// getT2SOImmValSplat - Return the 12-bit encoded representation
272 /// if the specified value can be obtained by splatting the low 8 bits
273 /// into every other byte or every byte of a 32-bit value. i.e.,
274 /// 00000000 00000000 00000000 abcdefgh control = 0
275 /// 00000000 abcdefgh 00000000 abcdefgh control = 1
276 /// abcdefgh 00000000 abcdefgh 00000000 control = 2
277 /// abcdefgh abcdefgh abcdefgh abcdefgh control = 3
278 /// Return -1 if none of the above apply.
279 /// See ARM Reference Manual A6.3.2.
280 static inline int getT2SOImmValSplatVal(unsigned V) {
283 if ((V & 0xffffff00) == 0)
286 // If the value is zeroes in the first byte, just shift those off
287 Vs = ((V & 0xff) == 0) ? V >> 8 : V;
288 // Any passing value only has 8 bits of payload, splatted across the word
290 // Likewise, any passing values have the payload splatted into the 3rd byte
291 u = Imm | (Imm << 16);
295 return (((Vs == V) ? 1 : 2) << 8) | Imm;
298 if (Vs == (u | (u << 8)))
299 return (3 << 8) | Imm;
304 /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the
305 /// specified value is a rotated 8-bit value. Return -1 if no rotation
306 /// encoding is possible.
307 /// See ARM Reference Manual A6.3.2.
308 static inline int getT2SOImmValRotateVal(unsigned V) {
309 unsigned RotAmt = CountLeadingZeros_32(V);
313 // If 'Arg' can be handled with a single shifter_op return the value.
314 if ((rotr32(0xff000000U, RotAmt) & V) == V)
315 return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7);
320 /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit
321 /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit
322 /// encoding for it. If not, return -1.
323 /// See ARM Reference Manual A6.3.2.
324 static inline int getT2SOImmVal(unsigned Arg) {
325 // If 'Arg' is an 8-bit splat, then get the encoded value.
326 int Splat = getT2SOImmValSplatVal(Arg);
330 // If 'Arg' can be handled with a single shifter_op return the value.
331 int Rot = getT2SOImmValRotateVal(Arg);
338 static inline unsigned getT2SOImmValRotate(unsigned V) {
339 if ((V & ~255U) == 0) return 0;
340 // Use CTZ to compute the rotate amount.
341 unsigned RotAmt = CountTrailingZeros_32(V);
342 return (32 - RotAmt) & 31;
345 static inline bool isT2SOImmTwoPartVal (unsigned Imm) {
347 // Passing values can be any combination of splat values and shifter
348 // values. If this can be handled with a single shifter or splat, bail
349 // out. Those should be handled directly, not with a two-part val.
350 if (getT2SOImmValSplatVal(V) != -1)
352 V = rotr32 (~255U, getT2SOImmValRotate(V)) & V;
356 // If this can be handled as an immediate, accept.
357 if (getT2SOImmVal(V) != -1) return true;
359 // Likewise, try masking out a splat value first.
361 if (getT2SOImmValSplatVal(V & 0xff00ff00U) != -1)
363 else if (getT2SOImmValSplatVal(V & 0x00ff00ffU) != -1)
365 // If what's left can be handled as an immediate, accept.
366 if (getT2SOImmVal(V) != -1) return true;
368 // Otherwise, do not accept.
372 static inline unsigned getT2SOImmTwoPartFirst(unsigned Imm) {
373 assert (isT2SOImmTwoPartVal(Imm) &&
374 "Immedate cannot be encoded as two part immediate!");
375 // Try a shifter operand as one part
376 unsigned V = rotr32 (~255, getT2SOImmValRotate(Imm)) & Imm;
377 // If the rest is encodable as an immediate, then return it.
378 if (getT2SOImmVal(V) != -1) return V;
380 // Try masking out a splat value first.
381 if (getT2SOImmValSplatVal(Imm & 0xff00ff00U) != -1)
382 return Imm & 0xff00ff00U;
384 // The other splat is all that's left as an option.
385 assert (getT2SOImmValSplatVal(Imm & 0x00ff00ffU) != -1);
386 return Imm & 0x00ff00ffU;
389 static inline unsigned getT2SOImmTwoPartSecond(unsigned Imm) {
390 // Mask out the first hunk
391 Imm ^= getT2SOImmTwoPartFirst(Imm);
392 // Return what's left
393 assert (getT2SOImmVal(Imm) != -1 &&
394 "Unable to encode second part of T2 two part SO immediate");
399 //===--------------------------------------------------------------------===//
400 // Addressing Mode #2
401 //===--------------------------------------------------------------------===//
403 // This is used for most simple load/store instructions.
405 // addrmode2 := reg +/- reg shop imm
406 // addrmode2 := reg +/- imm12
408 // The first operand is always a Reg. The second operand is a reg if in
409 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
410 // in bit 12, the immediate in bits 0-11, and the shift op in 13-15.
412 // If this addressing mode is a frame index (before prolog/epilog insertion
413 // and code rewriting), this operand will have the form: FI#, reg0, <offs>
414 // with no shift amount for the frame offset.
416 static inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO) {
417 assert(Imm12 < (1 << 12) && "Imm too large!");
418 bool isSub = Opc == sub;
419 return Imm12 | ((int)isSub << 12) | (SO << 13);
421 static inline unsigned getAM2Offset(unsigned AM2Opc) {
422 return AM2Opc & ((1 << 12)-1);
424 static inline AddrOpc getAM2Op(unsigned AM2Opc) {
425 return ((AM2Opc >> 12) & 1) ? sub : add;
427 static inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) {
428 return (ShiftOpc)(AM2Opc >> 13);
432 //===--------------------------------------------------------------------===//
433 // Addressing Mode #3
434 //===--------------------------------------------------------------------===//
436 // This is used for sign-extending loads, and load/store-pair instructions.
438 // addrmode3 := reg +/- reg
439 // addrmode3 := reg +/- imm8
441 // The first operand is always a Reg. The second operand is a reg if in
442 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
443 // in bit 8, the immediate in bits 0-7.
445 /// getAM3Opc - This function encodes the addrmode3 opc field.
446 static inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset) {
447 bool isSub = Opc == sub;
448 return ((int)isSub << 8) | Offset;
450 static inline unsigned char getAM3Offset(unsigned AM3Opc) {
451 return AM3Opc & 0xFF;
453 static inline AddrOpc getAM3Op(unsigned AM3Opc) {
454 return ((AM3Opc >> 8) & 1) ? sub : add;
457 //===--------------------------------------------------------------------===//
458 // Addressing Mode #4
459 //===--------------------------------------------------------------------===//
461 // This is used for load / store multiple instructions.
463 // addrmode4 := reg, <mode>
465 // The four modes are:
466 // IA - Increment after
467 // IB - Increment before
468 // DA - Decrement after
469 // DB - Decrement before
471 static inline AMSubMode getAM4SubMode(unsigned Mode) {
472 return (AMSubMode)(Mode & 0x7);
475 static inline unsigned getAM4ModeImm(AMSubMode SubMode) {
479 //===--------------------------------------------------------------------===//
480 // Addressing Mode #5
481 //===--------------------------------------------------------------------===//
483 // This is used for coprocessor instructions, such as FP load/stores.
485 // addrmode5 := reg +/- imm8*4
487 // The first operand is always a Reg. The second operand encodes the
488 // operation in bit 8 and the immediate in bits 0-7.
490 // This is also used for FP load/store multiple ops. The second operand
491 // encodes the number of registers (or 2 times the number of registers
492 // for DPR ops) in bits 0-7. In addition, bits 8-10 encode one of the
493 // following two sub-modes:
495 // IA - Increment after
496 // DB - Decrement before
498 /// getAM5Opc - This function encodes the addrmode5 opc field.
499 static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) {
500 bool isSub = Opc == sub;
501 return ((int)isSub << 8) | Offset;
503 static inline unsigned char getAM5Offset(unsigned AM5Opc) {
504 return AM5Opc & 0xFF;
506 static inline AddrOpc getAM5Op(unsigned AM5Opc) {
507 return ((AM5Opc >> 8) & 1) ? sub : add;
510 /// getAM5Opc - This function encodes the addrmode5 opc field for VLDM and
511 /// VSTM instructions.
512 static inline unsigned getAM5Opc(AMSubMode SubMode, unsigned char Offset) {
513 assert((SubMode == ia || SubMode == db) &&
514 "Illegal addressing mode 5 sub-mode!");
515 return ((int)SubMode << 8) | Offset;
517 static inline AMSubMode getAM5SubMode(unsigned AM5Opc) {
518 return (AMSubMode)((AM5Opc >> 8) & 0x7);
521 //===--------------------------------------------------------------------===//
522 // Addressing Mode #6
523 //===--------------------------------------------------------------------===//
525 // This is used for NEON load / store instructions.
527 // addrmode6 := reg with optional alignment
529 // This is stored in two operands [regaddr, align]. The first is the
530 // address register. The second operand is the value of the alignment
531 // specifier to use or zero if no explicit alignment.
533 } // end namespace ARM_AM
534 } // end namespace llvm