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