Stop using the old pre-UAL syntax for LDM/STM instruction suffixes.
[oota-llvm.git] / lib / Target / ARM / ARMAddressingModes.h
1 //===- ARMAddressingModes.h - ARM Addressing Modes --------------*- C++ -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file contains the ARM addressing mode implementation stuff.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_TARGET_ARM_ARMADDRESSINGMODES_H
15 #define LLVM_TARGET_ARM_ARMADDRESSINGMODES_H
16
17 #include "llvm/CodeGen/SelectionDAGNodes.h"
18 #include "llvm/Support/MathExtras.h"
19 #include <cassert>
20
21 namespace llvm {
22
23 /// ARM_AM - ARM Addressing Mode Stuff
24 namespace ARM_AM {
25   enum ShiftOpc {
26     no_shift = 0,
27     asr,
28     lsl,
29     lsr,
30     ror,
31     rrx
32   };
33
34   enum AddrOpc {
35     add = '+', sub = '-'
36   };
37
38   static inline const char *getShiftOpcStr(ShiftOpc Op) {
39     switch (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";
46     }
47   }
48
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;
60     }
61   }
62
63   enum AMSubMode {
64     bad_am_submode = 0,
65     ia,
66     ib,
67     da,
68     db
69   };
70
71   static inline const char *getAMSubModeStr(AMSubMode Mode) {
72     switch (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";
78     }
79   }
80
81   /// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits.
82   ///
83   static inline unsigned rotr32(unsigned Val, unsigned Amt) {
84     assert(Amt < 32 && "Invalid rotate amount");
85     return (Val >> Amt) | (Val << ((32-Amt)&31));
86   }
87
88   /// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits.
89   ///
90   static inline unsigned rotl32(unsigned Val, unsigned Amt) {
91     assert(Amt < 32 && "Invalid rotate amount");
92     return (Val << Amt) | (Val >> ((32-Amt)&31));
93   }
94
95   //===--------------------------------------------------------------------===//
96   // Addressing Mode #1: shift_operand with registers
97   //===--------------------------------------------------------------------===//
98   //
99   // This 'addressing mode' is used for arithmetic instructions.  It can
100   // represent things like:
101   //   reg
102   //   reg [asr|lsl|lsr|ror|rrx] reg
103   //   reg [asr|lsl|lsr|ror|rrx] imm
104   //
105   // This is stored three operands [rega, regb, opc].  The first is the base
106   // reg, the second is the shift amount (or reg0 if not present or imm).  The
107   // third operand encodes the shift opcode and the imm if a reg isn't present.
108   //
109   static inline unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm) {
110     return ShOp | (Imm << 3);
111   }
112   static inline unsigned getSORegOffset(unsigned Op) {
113     return Op >> 3;
114   }
115   static inline ShiftOpc getSORegShOp(unsigned Op) {
116     return (ShiftOpc)(Op & 7);
117   }
118
119   /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return
120   /// the 8-bit imm value.
121   static inline unsigned getSOImmValImm(unsigned Imm) {
122     return Imm & 0xFF;
123   }
124   /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return
125   /// the rotate amount.
126   static inline unsigned getSOImmValRot(unsigned Imm) {
127     return (Imm >> 8) * 2;
128   }
129
130   /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,
131   /// computing the rotate amount to use.  If this immediate value cannot be
132   /// handled with a single shifter-op, determine a good rotate amount that will
133   /// take a maximal chunk of bits out of the immediate.
134   static inline unsigned getSOImmValRotate(unsigned Imm) {
135     // 8-bit (or less) immediates are trivially shifter_operands with a rotate
136     // of zero.
137     if ((Imm & ~255U) == 0) return 0;
138
139     // Use CTZ to compute the rotate amount.
140     unsigned TZ = CountTrailingZeros_32(Imm);
141
142     // Rotate amount must be even.  Something like 0x200 must be rotated 8 bits,
143     // not 9.
144     unsigned RotAmt = TZ & ~1;
145
146     // If we can handle this spread, return it.
147     if ((rotr32(Imm, RotAmt) & ~255U) == 0)
148       return (32-RotAmt)&31;  // HW rotates right, not left.
149
150     // For values like 0xF000000F, we should skip the first run of ones, then
151     // retry the hunt.
152     if (Imm & 1) {
153       unsigned TrailingOnes = CountTrailingZeros_32(~Imm);
154       if (TrailingOnes != 32) {  // Avoid overflow on 0xFFFFFFFF
155         // Restart the search for a high-order bit after the initial seconds of
156         // ones.
157         unsigned TZ2 = CountTrailingZeros_32(Imm & ~((1 << TrailingOnes)-1));
158
159         // Rotate amount must be even.
160         unsigned RotAmt2 = TZ2 & ~1;
161
162         // If this fits, use it.
163         if (RotAmt2 != 32 && (rotr32(Imm, RotAmt2) & ~255U) == 0)
164           return (32-RotAmt2)&31;  // HW rotates right, not left.
165       }
166     }
167
168     // Otherwise, we have no way to cover this span of bits with a single
169     // shifter_op immediate.  Return a chunk of bits that will be useful to
170     // handle.
171     return (32-RotAmt)&31;  // HW rotates right, not left.
172   }
173
174   /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit
175   /// into an shifter_operand immediate operand, return the 12-bit encoding for
176   /// it.  If not, return -1.
177   static inline int getSOImmVal(unsigned Arg) {
178     // 8-bit (or less) immediates are trivially shifter_operands with a rotate
179     // of zero.
180     if ((Arg & ~255U) == 0) return Arg;
181
182     unsigned RotAmt = getSOImmValRotate(Arg);
183
184     // If this cannot be handled with a single shifter_op, bail out.
185     if (rotr32(~255U, RotAmt) & Arg)
186       return -1;
187
188     // Encode this correctly.
189     return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8);
190   }
191
192   /// isSOImmTwoPartVal - Return true if the specified value can be obtained by
193   /// or'ing together two SOImmVal's.
194   static inline bool isSOImmTwoPartVal(unsigned V) {
195     // If this can be handled with a single shifter_op, bail out.
196     V = rotr32(~255U, getSOImmValRotate(V)) & V;
197     if (V == 0)
198       return false;
199
200     // If this can be handled with two shifter_op's, accept.
201     V = rotr32(~255U, getSOImmValRotate(V)) & V;
202     return V == 0;
203   }
204
205   /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,
206   /// return the first chunk of it.
207   static inline unsigned getSOImmTwoPartFirst(unsigned V) {
208     return rotr32(255U, getSOImmValRotate(V)) & V;
209   }
210
211   /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,
212   /// return the second chunk of it.
213   static inline unsigned getSOImmTwoPartSecond(unsigned V) {
214     // Mask out the first hunk.
215     V = rotr32(~255U, getSOImmValRotate(V)) & V;
216
217     // Take what's left.
218     assert(V == (rotr32(255U, getSOImmValRotate(V)) & V));
219     return V;
220   }
221
222   /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed
223   /// by a left shift. Returns the shift amount to use.
224   static inline unsigned getThumbImmValShift(unsigned Imm) {
225     // 8-bit (or less) immediates are trivially immediate operand with a shift
226     // of zero.
227     if ((Imm & ~255U) == 0) return 0;
228
229     // Use CTZ to compute the shift amount.
230     return CountTrailingZeros_32(Imm);
231   }
232
233   /// isThumbImmShiftedVal - Return true if the specified value can be obtained
234   /// by left shifting a 8-bit immediate.
235   static inline bool isThumbImmShiftedVal(unsigned V) {
236     // If this can be handled with
237     V = (~255U << getThumbImmValShift(V)) & V;
238     return V == 0;
239   }
240
241   /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed
242   /// by a left shift. Returns the shift amount to use.
243   static inline unsigned getThumbImm16ValShift(unsigned Imm) {
244     // 16-bit (or less) immediates are trivially immediate operand with a shift
245     // of zero.
246     if ((Imm & ~65535U) == 0) return 0;
247
248     // Use CTZ to compute the shift amount.
249     return CountTrailingZeros_32(Imm);
250   }
251
252   /// isThumbImm16ShiftedVal - Return true if the specified value can be
253   /// obtained by left shifting a 16-bit immediate.
254   static inline bool isThumbImm16ShiftedVal(unsigned V) {
255     // If this can be handled with
256     V = (~65535U << getThumbImm16ValShift(V)) & V;
257     return V == 0;
258   }
259
260   /// getThumbImmNonShiftedVal - If V is a value that satisfies
261   /// isThumbImmShiftedVal, return the non-shiftd value.
262   static inline unsigned getThumbImmNonShiftedVal(unsigned V) {
263     return V >> getThumbImmValShift(V);
264   }
265
266
267   /// getT2SOImmValSplat - Return the 12-bit encoded representation
268   /// if the specified value can be obtained by splatting the low 8 bits
269   /// into every other byte or every byte of a 32-bit value. i.e.,
270   ///     00000000 00000000 00000000 abcdefgh    control = 0
271   ///     00000000 abcdefgh 00000000 abcdefgh    control = 1
272   ///     abcdefgh 00000000 abcdefgh 00000000    control = 2
273   ///     abcdefgh abcdefgh abcdefgh abcdefgh    control = 3
274   /// Return -1 if none of the above apply.
275   /// See ARM Reference Manual A6.3.2.
276   static inline int getT2SOImmValSplatVal(unsigned V) {
277     unsigned u, Vs, Imm;
278     // control = 0
279     if ((V & 0xffffff00) == 0)
280       return V;
281
282     // If the value is zeroes in the first byte, just shift those off
283     Vs = ((V & 0xff) == 0) ? V >> 8 : V;
284     // Any passing value only has 8 bits of payload, splatted across the word
285     Imm = Vs & 0xff;
286     // Likewise, any passing values have the payload splatted into the 3rd byte
287     u = Imm | (Imm << 16);
288
289     // control = 1 or 2
290     if (Vs == u)
291       return (((Vs == V) ? 1 : 2) << 8) | Imm;
292
293     // control = 3
294     if (Vs == (u | (u << 8)))
295       return (3 << 8) | Imm;
296
297     return -1;
298   }
299
300   /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the
301   /// specified value is a rotated 8-bit value. Return -1 if no rotation
302   /// encoding is possible.
303   /// See ARM Reference Manual A6.3.2.
304   static inline int getT2SOImmValRotateVal(unsigned V) {
305     unsigned RotAmt = CountLeadingZeros_32(V);
306     if (RotAmt >= 24)
307       return -1;
308
309     // If 'Arg' can be handled with a single shifter_op return the value.
310     if ((rotr32(0xff000000U, RotAmt) & V) == V)
311       return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7);
312
313     return -1;
314   }
315
316   /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit
317   /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit
318   /// encoding for it.  If not, return -1.
319   /// See ARM Reference Manual A6.3.2.
320   static inline int getT2SOImmVal(unsigned Arg) {
321     // If 'Arg' is an 8-bit splat, then get the encoded value.
322     int Splat = getT2SOImmValSplatVal(Arg);
323     if (Splat != -1)
324       return Splat;
325
326     // If 'Arg' can be handled with a single shifter_op return the value.
327     int Rot = getT2SOImmValRotateVal(Arg);
328     if (Rot != -1)
329       return Rot;
330
331     return -1;
332   }
333
334   static inline unsigned getT2SOImmValRotate(unsigned V) {
335     if ((V & ~255U) == 0) return 0;
336     // Use CTZ to compute the rotate amount.
337     unsigned RotAmt = CountTrailingZeros_32(V);
338     return (32 - RotAmt) & 31;
339   }
340
341   static inline bool isT2SOImmTwoPartVal (unsigned Imm) {
342     unsigned V = Imm;
343     // Passing values can be any combination of splat values and shifter
344     // values. If this can be handled with a single shifter or splat, bail
345     // out. Those should be handled directly, not with a two-part val.
346     if (getT2SOImmValSplatVal(V) != -1)
347       return false;
348     V = rotr32 (~255U, getT2SOImmValRotate(V)) & V;
349     if (V == 0)
350       return false;
351
352     // If this can be handled as an immediate, accept.
353     if (getT2SOImmVal(V) != -1) return true;
354
355     // Likewise, try masking out a splat value first.
356     V = Imm;
357     if (getT2SOImmValSplatVal(V & 0xff00ff00U) != -1)
358       V &= ~0xff00ff00U;
359     else if (getT2SOImmValSplatVal(V & 0x00ff00ffU) != -1)
360       V &= ~0x00ff00ffU;
361     // If what's left can be handled as an immediate, accept.
362     if (getT2SOImmVal(V) != -1) return true;
363
364     // Otherwise, do not accept.
365     return false;
366   }
367
368   static inline unsigned getT2SOImmTwoPartFirst(unsigned Imm) {
369     assert (isT2SOImmTwoPartVal(Imm) &&
370             "Immedate cannot be encoded as two part immediate!");
371     // Try a shifter operand as one part
372     unsigned V = rotr32 (~255, getT2SOImmValRotate(Imm)) & Imm;
373     // If the rest is encodable as an immediate, then return it.
374     if (getT2SOImmVal(V) != -1) return V;
375
376     // Try masking out a splat value first.
377     if (getT2SOImmValSplatVal(Imm & 0xff00ff00U) != -1)
378       return Imm & 0xff00ff00U;
379
380     // The other splat is all that's left as an option.
381     assert (getT2SOImmValSplatVal(Imm & 0x00ff00ffU) != -1);
382     return Imm & 0x00ff00ffU;
383   }
384
385   static inline unsigned getT2SOImmTwoPartSecond(unsigned Imm) {
386     // Mask out the first hunk
387     Imm ^= getT2SOImmTwoPartFirst(Imm);
388     // Return what's left
389     assert (getT2SOImmVal(Imm) != -1 &&
390             "Unable to encode second part of T2 two part SO immediate");
391     return Imm;
392   }
393
394
395   //===--------------------------------------------------------------------===//
396   // Addressing Mode #2
397   //===--------------------------------------------------------------------===//
398   //
399   // This is used for most simple load/store instructions.
400   //
401   // addrmode2 := reg +/- reg shop imm
402   // addrmode2 := reg +/- imm12
403   //
404   // The first operand is always a Reg.  The second operand is a reg if in
405   // reg/reg form, otherwise it's reg#0.  The third field encodes the operation
406   // in bit 12, the immediate in bits 0-11, and the shift op in 13-15.
407   //
408   // If this addressing mode is a frame index (before prolog/epilog insertion
409   // and code rewriting), this operand will have the form:  FI#, reg0, <offs>
410   // with no shift amount for the frame offset.
411   //
412   static inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO) {
413     assert(Imm12 < (1 << 12) && "Imm too large!");
414     bool isSub = Opc == sub;
415     return Imm12 | ((int)isSub << 12) | (SO << 13);
416   }
417   static inline unsigned getAM2Offset(unsigned AM2Opc) {
418     return AM2Opc & ((1 << 12)-1);
419   }
420   static inline AddrOpc getAM2Op(unsigned AM2Opc) {
421     return ((AM2Opc >> 12) & 1) ? sub : add;
422   }
423   static inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) {
424     return (ShiftOpc)(AM2Opc >> 13);
425   }
426
427
428   //===--------------------------------------------------------------------===//
429   // Addressing Mode #3
430   //===--------------------------------------------------------------------===//
431   //
432   // This is used for sign-extending loads, and load/store-pair instructions.
433   //
434   // addrmode3 := reg +/- reg
435   // addrmode3 := reg +/- imm8
436   //
437   // The first operand is always a Reg.  The second operand is a reg if in
438   // reg/reg form, otherwise it's reg#0.  The third field encodes the operation
439   // in bit 8, the immediate in bits 0-7.
440
441   /// getAM3Opc - This function encodes the addrmode3 opc field.
442   static inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset) {
443     bool isSub = Opc == sub;
444     return ((int)isSub << 8) | Offset;
445   }
446   static inline unsigned char getAM3Offset(unsigned AM3Opc) {
447     return AM3Opc & 0xFF;
448   }
449   static inline AddrOpc getAM3Op(unsigned AM3Opc) {
450     return ((AM3Opc >> 8) & 1) ? sub : add;
451   }
452
453   //===--------------------------------------------------------------------===//
454   // Addressing Mode #4
455   //===--------------------------------------------------------------------===//
456   //
457   // This is used for load / store multiple instructions.
458   //
459   // addrmode4 := reg, <mode>
460   //
461   // The four modes are:
462   //    IA - Increment after
463   //    IB - Increment before
464   //    DA - Decrement after
465   //    DB - Decrement before
466   //
467   // If the 4th bit (writeback)is set, then the base register is updated after
468   // the memory transfer.
469
470   static inline AMSubMode getAM4SubMode(unsigned Mode) {
471     return (AMSubMode)(Mode & 0x7);
472   }
473
474   static inline unsigned getAM4ModeImm(AMSubMode SubMode, bool WB = false) {
475     return (int)SubMode | ((int)WB << 3);
476   }
477
478   static inline bool getAM4WBFlag(unsigned Mode) {
479     return (Mode >> 3) & 1;
480   }
481
482   //===--------------------------------------------------------------------===//
483   // Addressing Mode #5
484   //===--------------------------------------------------------------------===//
485   //
486   // This is used for coprocessor instructions, such as FP load/stores.
487   //
488   // addrmode5 := reg +/- imm8*4
489   //
490   // The first operand is always a Reg.  The second operand encodes the
491   // operation in bit 8 and the immediate in bits 0-7.
492   //
493   // This is also used for FP load/store multiple ops. The second operand
494   // encodes the writeback mode in bit 8 and the number of registers (or 2
495   // times the number of registers for DPR ops) in bits 0-7. In addition,
496   // bits 9-11 encode one of the following two sub-modes:
497   //
498   //    IA - Increment after
499   //    DB - Decrement before
500
501   /// getAM5Opc - This function encodes the addrmode5 opc field.
502   static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) {
503     bool isSub = Opc == sub;
504     return ((int)isSub << 8) | Offset;
505   }
506   static inline unsigned char getAM5Offset(unsigned AM5Opc) {
507     return AM5Opc & 0xFF;
508   }
509   static inline AddrOpc getAM5Op(unsigned AM5Opc) {
510     return ((AM5Opc >> 8) & 1) ? sub : add;
511   }
512
513   /// getAM5Opc - This function encodes the addrmode5 opc field for VLDM and
514   /// VSTM instructions.
515   static inline unsigned getAM5Opc(AMSubMode SubMode, bool WB,
516                                    unsigned char Offset) {
517     assert((SubMode == ia || SubMode == db) &&
518            "Illegal addressing mode 5 sub-mode!");
519     return ((int)SubMode << 9) | ((int)WB << 8) | Offset;
520   }
521   static inline AMSubMode getAM5SubMode(unsigned AM5Opc) {
522     return (AMSubMode)((AM5Opc >> 9) & 0x7);
523   }
524   static inline bool getAM5WBFlag(unsigned AM5Opc) {
525     return ((AM5Opc >> 8) & 1);
526   }
527
528   //===--------------------------------------------------------------------===//
529   // Addressing Mode #6
530   //===--------------------------------------------------------------------===//
531   //
532   // This is used for NEON load / store instructions.
533   //
534   // addrmode6 := reg with optional writeback and alignment
535   //
536   // This is stored in four operands [regaddr, regupdate, opc, align].  The
537   // first is the address register.  The second register holds the value of
538   // a post-access increment for writeback or reg0 if no writeback or if the
539   // writeback increment is the size of the memory access.  The third
540   // operand encodes whether there is writeback to the address register. The
541   // fourth operand is the value of the alignment specifier to use or zero if
542   // no explicit alignment.
543
544   static inline unsigned getAM6Opc(bool WB = false) {
545     return (int)WB;
546   }
547
548   static inline bool getAM6WBFlag(unsigned Mode) {
549     return Mode & 1;
550   }
551
552 } // end namespace ARM_AM
553 } // end namespace llvm
554
555 #endif
556