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 *getShiftOpcStr(ShiftOpc Op) {
40 default: assert(0 && "Unknown shift opc!");
41 case ARM_AM::asr: return "asr";
42 case ARM_AM::lsl: return "lsl";
43 case ARM_AM::lsr: return "lsr";
44 case ARM_AM::ror: return "ror";
45 case ARM_AM::rrx: return "rrx";
49 static inline ShiftOpc getShiftOpcForNode(SDValue N) {
50 switch (N.getOpcode()) {
51 default: return ARM_AM::no_shift;
52 case ISD::SHL: return ARM_AM::lsl;
53 case ISD::SRL: return ARM_AM::lsr;
54 case ISD::SRA: return ARM_AM::asr;
55 case ISD::ROTR: return ARM_AM::ror;
56 //case ISD::ROTL: // Only if imm -> turn into ROTR.
57 // Can't handle RRX here, because it would require folding a flag into
58 // the addressing mode. :( This causes us to miss certain things.
59 //case ARMISD::RRX: return ARM_AM::rrx;
71 static inline const char *getAMSubModeStr(AMSubMode Mode) {
73 default: assert(0 && "Unknown addressing sub-mode!");
74 case ARM_AM::ia: return "ia";
75 case ARM_AM::ib: return "ib";
76 case ARM_AM::da: return "da";
77 case ARM_AM::db: return "db";
81 static inline const char *getAMSubModeAltStr(AMSubMode Mode, bool isLD) {
83 default: assert(0 && "Unknown addressing sub-mode!");
84 case ARM_AM::ia: return isLD ? "fd" : "ea";
85 case ARM_AM::ib: return isLD ? "ed" : "fa";
86 case ARM_AM::da: return isLD ? "fa" : "ed";
87 case ARM_AM::db: return isLD ? "ea" : "fd";
91 /// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits.
93 static inline unsigned rotr32(unsigned Val, unsigned Amt) {
94 assert(Amt < 32 && "Invalid rotate amount");
95 return (Val >> Amt) | (Val << ((32-Amt)&31));
98 /// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits.
100 static inline unsigned rotl32(unsigned Val, unsigned Amt) {
101 assert(Amt < 32 && "Invalid rotate amount");
102 return (Val << Amt) | (Val >> ((32-Amt)&31));
105 //===--------------------------------------------------------------------===//
106 // Addressing Mode #1: shift_operand with registers
107 //===--------------------------------------------------------------------===//
109 // This 'addressing mode' is used for arithmetic instructions. It can
110 // represent things like:
112 // reg [asr|lsl|lsr|ror|rrx] reg
113 // reg [asr|lsl|lsr|ror|rrx] imm
115 // This is stored three operands [rega, regb, opc]. The first is the base
116 // reg, the second is the shift amount (or reg0 if not present or imm). The
117 // third operand encodes the shift opcode and the imm if a reg isn't present.
119 static inline unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm) {
120 return ShOp | (Imm << 3);
122 static inline unsigned getSORegOffset(unsigned Op) {
125 static inline ShiftOpc getSORegShOp(unsigned Op) {
126 return (ShiftOpc)(Op & 7);
129 /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return
130 /// the 8-bit imm value.
131 static inline unsigned getSOImmValImm(unsigned Imm) {
134 /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return
135 /// the rotate amount.
136 static inline unsigned getSOImmValRot(unsigned Imm) {
137 return (Imm >> 8) * 2;
140 /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,
141 /// computing the rotate amount to use. If this immediate value cannot be
142 /// handled with a single shifter-op, determine a good rotate amount that will
143 /// take a maximal chunk of bits out of the immediate.
144 static inline unsigned getSOImmValRotate(unsigned Imm) {
145 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
147 if ((Imm & ~255U) == 0) return 0;
149 // Use CTZ to compute the rotate amount.
150 unsigned TZ = CountTrailingZeros_32(Imm);
152 // Rotate amount must be even. Something like 0x200 must be rotated 8 bits,
154 unsigned RotAmt = TZ & ~1;
156 // If we can handle this spread, return it.
157 if ((rotr32(Imm, RotAmt) & ~255U) == 0)
158 return (32-RotAmt)&31; // HW rotates right, not left.
160 // For values like 0xF000000F, we should skip the first run of ones, then
163 unsigned TrailingOnes = CountTrailingZeros_32(~Imm);
164 if (TrailingOnes != 32) { // Avoid overflow on 0xFFFFFFFF
165 // Restart the search for a high-order bit after the initial seconds of
167 unsigned TZ2 = CountTrailingZeros_32(Imm & ~((1 << TrailingOnes)-1));
169 // Rotate amount must be even.
170 unsigned RotAmt2 = TZ2 & ~1;
172 // If this fits, use it.
173 if (RotAmt2 != 32 && (rotr32(Imm, RotAmt2) & ~255U) == 0)
174 return (32-RotAmt2)&31; // HW rotates right, not left.
178 // Otherwise, we have no way to cover this span of bits with a single
179 // shifter_op immediate. Return a chunk of bits that will be useful to
181 return (32-RotAmt)&31; // HW rotates right, not left.
184 /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit
185 /// into an shifter_operand immediate operand, return the 12-bit encoding for
186 /// it. If not, return -1.
187 static inline int getSOImmVal(unsigned Arg) {
188 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
190 if ((Arg & ~255U) == 0) return Arg;
192 unsigned RotAmt = getSOImmValRotate(Arg);
194 // If this cannot be handled with a single shifter_op, bail out.
195 if (rotr32(~255U, RotAmt) & Arg)
198 // Encode this correctly.
199 return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8);
202 /// isSOImmTwoPartVal - Return true if the specified value can be obtained by
203 /// or'ing together two SOImmVal's.
204 static inline bool isSOImmTwoPartVal(unsigned V) {
205 // If this can be handled with a single shifter_op, bail out.
206 V = rotr32(~255U, getSOImmValRotate(V)) & V;
210 // If this can be handled with two shifter_op's, accept.
211 V = rotr32(~255U, getSOImmValRotate(V)) & V;
215 /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,
216 /// return the first chunk of it.
217 static inline unsigned getSOImmTwoPartFirst(unsigned V) {
218 return rotr32(255U, getSOImmValRotate(V)) & V;
221 /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,
222 /// return the second chunk of it.
223 static inline unsigned getSOImmTwoPartSecond(unsigned V) {
224 // Mask out the first hunk.
225 V = rotr32(~255U, getSOImmValRotate(V)) & V;
228 assert(V == (rotr32(255U, getSOImmValRotate(V)) & V));
232 /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed
233 /// by a left shift. Returns the shift amount to use.
234 static inline unsigned getThumbImmValShift(unsigned Imm) {
235 // 8-bit (or less) immediates are trivially immediate operand with a shift
237 if ((Imm & ~255U) == 0) return 0;
239 // Use CTZ to compute the shift amount.
240 return CountTrailingZeros_32(Imm);
243 /// isThumbImmShiftedVal - Return true if the specified value can be obtained
244 /// by left shifting a 8-bit immediate.
245 static inline bool isThumbImmShiftedVal(unsigned V) {
246 // If this can be handled with
247 V = (~255U << getThumbImmValShift(V)) & V;
251 /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed
252 /// by a left shift. Returns the shift amount to use.
253 static inline unsigned getThumbImm16ValShift(unsigned Imm) {
254 // 16-bit (or less) immediates are trivially immediate operand with a shift
256 if ((Imm & ~65535U) == 0) return 0;
258 // Use CTZ to compute the shift amount.
259 return CountTrailingZeros_32(Imm);
262 /// isThumbImm16ShiftedVal - Return true if the specified value can be
263 /// obtained by left shifting a 16-bit immediate.
264 static inline bool isThumbImm16ShiftedVal(unsigned V) {
265 // If this can be handled with
266 V = (~65535U << getThumbImm16ValShift(V)) & V;
270 /// getThumbImmNonShiftedVal - If V is a value that satisfies
271 /// isThumbImmShiftedVal, return the non-shiftd value.
272 static inline unsigned getThumbImmNonShiftedVal(unsigned V) {
273 return V >> getThumbImmValShift(V);
276 /// getT2SOImmValDecode - Given a 12-bit encoded Thumb-2 modified immediate,
277 /// return the corresponding 32-bit immediate value.
278 /// See ARM Reference Manual A6.3.2.
279 static inline unsigned getT2SOImmValDecode(unsigned Imm) {
280 unsigned Base = Imm & 0xff;
281 switch ((Imm >> 8) & 0xf) {
285 return Base | (Base << 16);
287 return (Base << 8) | (Base << 24);
289 return Base | (Base << 8) | (Base << 16) | (Base << 24);
295 unsigned RotAmount = ((Imm >> 7) & 0x1f) - 8;
296 return (Base | 0x80) << (24 - RotAmount);
299 /// getT2SOImmValSplat - Return the 12-bit encoded representation
300 /// if the specified value can be obtained by splatting the low 8 bits
301 /// into every other byte or every byte of a 32-bit value. i.e.,
302 /// 00000000 00000000 00000000 abcdefgh control = 0
303 /// 00000000 abcdefgh 00000000 abcdefgh control = 1
304 /// abcdefgh 00000000 abcdefgh 00000000 control = 2
305 /// abcdefgh abcdefgh abcdefgh abcdefgh control = 3
306 /// Return -1 if none of the above apply.
307 /// See ARM Reference Manual A6.3.2.
308 static inline int getT2SOImmValSplat (unsigned V) {
311 if ((V & 0xffffff00) == 0)
314 // If the value is zeroes in the first byte, just shift those off
315 Vs = ((V & 0xff) == 0) ? V >> 8 : V;
316 // Any passing value only has 8 bits of payload, splatted across the word
318 // Likewise, any passing values have the payload splatted into the 3rd byte
319 u = Imm | (Imm << 16);
323 return (((Vs == V) ? 1 : 2) << 8) | Imm;
326 if (Vs == (u | (u << 8)))
327 return (3 << 8) | Imm;
332 /// getT2SOImmValRotate - Return the 12-bit encoded representation if the
333 /// specified value is a rotated 8-bit value. Return -1 if no rotation
334 /// encoding is possible.
335 /// See ARM Reference Manual A6.3.2.
336 static inline int getT2SOImmValRotate (unsigned V) {
337 unsigned RotAmt = CountLeadingZeros_32(V);
341 // If 'Arg' can be handled with a single shifter_op return the value.
342 if ((rotr32(0xff000000U, RotAmt) & V) == V)
343 return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7);
348 /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit
349 /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit
350 /// encoding for it. If not, return -1.
351 /// See ARM Reference Manual A6.3.2.
352 static inline int getT2SOImmVal(unsigned Arg) {
353 // If 'Arg' is an 8-bit splat, then get the encoded value.
354 int Splat = getT2SOImmValSplat(Arg);
358 // If 'Arg' can be handled with a single shifter_op return the value.
359 int Rot = getT2SOImmValRotate(Arg);
367 //===--------------------------------------------------------------------===//
368 // Addressing Mode #2
369 //===--------------------------------------------------------------------===//
371 // This is used for most simple load/store instructions.
373 // addrmode2 := reg +/- reg shop imm
374 // addrmode2 := reg +/- imm12
376 // The first operand is always a Reg. The second operand is a reg if in
377 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
378 // in bit 12, the immediate in bits 0-11, and the shift op in 13-15.
380 // If this addressing mode is a frame index (before prolog/epilog insertion
381 // and code rewriting), this operand will have the form: FI#, reg0, <offs>
382 // with no shift amount for the frame offset.
384 static inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO) {
385 assert(Imm12 < (1 << 12) && "Imm too large!");
386 bool isSub = Opc == sub;
387 return Imm12 | ((int)isSub << 12) | (SO << 13);
389 static inline unsigned getAM2Offset(unsigned AM2Opc) {
390 return AM2Opc & ((1 << 12)-1);
392 static inline AddrOpc getAM2Op(unsigned AM2Opc) {
393 return ((AM2Opc >> 12) & 1) ? sub : add;
395 static inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) {
396 return (ShiftOpc)(AM2Opc >> 13);
400 //===--------------------------------------------------------------------===//
401 // Addressing Mode #3
402 //===--------------------------------------------------------------------===//
404 // This is used for sign-extending loads, and load/store-pair instructions.
406 // addrmode3 := reg +/- reg
407 // addrmode3 := reg +/- imm8
409 // The first operand is always a Reg. The second operand is a reg if in
410 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
411 // in bit 8, the immediate in bits 0-7.
413 /// getAM3Opc - This function encodes the addrmode3 opc field.
414 static inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset) {
415 bool isSub = Opc == sub;
416 return ((int)isSub << 8) | Offset;
418 static inline unsigned char getAM3Offset(unsigned AM3Opc) {
419 return AM3Opc & 0xFF;
421 static inline AddrOpc getAM3Op(unsigned AM3Opc) {
422 return ((AM3Opc >> 8) & 1) ? sub : add;
425 //===--------------------------------------------------------------------===//
426 // Addressing Mode #4
427 //===--------------------------------------------------------------------===//
429 // This is used for load / store multiple instructions.
431 // addrmode4 := reg, <mode>
433 // The four modes are:
434 // IA - Increment after
435 // IB - Increment before
436 // DA - Decrement after
437 // DB - Decrement before
439 // If the 4th bit (writeback)is set, then the base register is updated after
440 // the memory transfer.
442 static inline AMSubMode getAM4SubMode(unsigned Mode) {
443 return (AMSubMode)(Mode & 0x7);
446 static inline unsigned getAM4ModeImm(AMSubMode SubMode, bool WB = false) {
447 return (int)SubMode | ((int)WB << 3);
450 static inline bool getAM4WBFlag(unsigned Mode) {
451 return (Mode >> 3) & 1;
454 //===--------------------------------------------------------------------===//
455 // Addressing Mode #5
456 //===--------------------------------------------------------------------===//
458 // This is used for coprocessor instructions, such as FP load/stores.
460 // addrmode5 := reg +/- imm8*4
462 // The first operand is always a Reg. The third field encodes the operation
463 // in bit 8, the immediate in bits 0-7.
465 // This can also be used for FP load/store multiple ops. The third field encodes
466 // writeback mode in bit 8, the number of registers (or 2 times the number of
467 // registers for DPR ops) in bits 0-7. In addition, bit 9-11 encodes one of the
468 // following two sub-modes:
470 // IA - Increment after
471 // DB - Decrement before
473 /// getAM5Opc - This function encodes the addrmode5 opc field.
474 static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) {
475 bool isSub = Opc == sub;
476 return ((int)isSub << 8) | Offset;
478 static inline unsigned char getAM5Offset(unsigned AM5Opc) {
479 return AM5Opc & 0xFF;
481 static inline AddrOpc getAM5Op(unsigned AM5Opc) {
482 return ((AM5Opc >> 8) & 1) ? sub : add;
485 /// getAM5Opc - This function encodes the addrmode5 opc field for FLDM and
486 /// FSTM instructions.
487 static inline unsigned getAM5Opc(AMSubMode SubMode, bool WB,
488 unsigned char Offset) {
489 assert((SubMode == ia || SubMode == db) &&
490 "Illegal addressing mode 5 sub-mode!");
491 return ((int)SubMode << 9) | ((int)WB << 8) | Offset;
493 static inline AMSubMode getAM5SubMode(unsigned AM5Opc) {
494 return (AMSubMode)((AM5Opc >> 9) & 0x7);
496 static inline bool getAM5WBFlag(unsigned AM5Opc) {
497 return ((AM5Opc >> 8) & 1);
500 } // end namespace ARM_AM
501 } // end namespace llvm