Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 1 | //===- ARMAddressingModes.h - ARM Addressing Modes --------------*- C++ -*-===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
Chris Lattner | 4ee451d | 2007-12-29 20:36:04 +0000 | [diff] [blame] | 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 22 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | }; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 33 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 34 | enum AddrOpc { |
| 35 | add = '+', sub = '-' |
| 36 | }; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 37 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 38 | static inline const char *getShiftOpcStr(ShiftOpc Op) { |
| 39 | switch (Op) { |
Chris Lattner | 8514e21 | 2009-10-19 21:23:15 +0000 | [diff] [blame] | 40 | default: assert(0 && "Unknown shift opc!"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 48 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 49 | static inline ShiftOpc getShiftOpcForNode(SDValue N) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 8514e21 | 2009-10-19 21:23:15 +0000 | [diff] [blame] | 73 | default: assert(0 && "Unknown addressing sub-mode!"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 87 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 94 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | d83712a | 2009-03-30 18:49:37 +0000 | [diff] [blame] | 124 | /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 125 | /// the rotate amount. |
| 126 | static inline unsigned getSOImmValRot(unsigned Imm) { |
| 127 | return (Imm >> 8) * 2; |
| 128 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 129 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 138 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 139 | // Use CTZ to compute the rotate amount. |
| 140 | unsigned TZ = CountTrailingZeros_32(Imm); |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 141 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 142 | // Rotate amount must be even. Something like 0x200 must be rotated 8 bits, |
| 143 | // not 9. |
| 144 | unsigned RotAmt = TZ & ~1; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 145 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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)); |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 158 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 159 | // Rotate amount must be even. |
| 160 | unsigned RotAmt2 = TZ2 & ~1; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 161 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 167 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 181 | |
Bob Wilson | 49d9dc4 | 2010-03-16 16:59:47 +0000 | [diff] [blame] | 182 | unsigned RotAmt = getSOImmValRotate(Arg); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 183 | |
| 184 | // If this cannot be handled with a single shifter_op, bail out. |
| 185 | if (rotr32(~255U, RotAmt) & Arg) |
| 186 | return -1; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 187 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 188 | // Encode this correctly. |
| 189 | return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8); |
| 190 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 191 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 199 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 200 | // If this can be handled with two shifter_op's, accept. |
| 201 | V = rotr32(~255U, getSOImmValRotate(V)) & V; |
| 202 | return V == 0; |
| 203 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 204 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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) { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 214 | // Mask out the first hunk. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 215 | V = rotr32(~255U, getSOImmValRotate(V)) & V; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 216 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 217 | // Take what's left. |
| 218 | assert(V == (rotr32(255U, getSOImmValRotate(V)) & V)); |
| 219 | return V; |
| 220 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 221 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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) { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 236 | // If this can be handled with |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 237 | V = (~255U << getThumbImmValShift(V)) & V; |
| 238 | return V == 0; |
| 239 | } |
| 240 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 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 | |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 252 | /// isThumbImm16ShiftedVal - Return true if the specified value can be |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 253 | /// obtained by left shifting a 16-bit immediate. |
| 254 | static inline bool isThumbImm16ShiftedVal(unsigned V) { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 255 | // If this can be handled with |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 256 | V = (~65535U << getThumbImm16ValShift(V)) & V; |
| 257 | return V == 0; |
| 258 | } |
| 259 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 266 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 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. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 276 | static inline int getT2SOImmValSplatVal(unsigned V) { |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 277 | unsigned u, Vs, Imm; |
| 278 | // control = 0 |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 279 | if ((V & 0xffffff00) == 0) |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 280 | return V; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 281 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 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 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 300 | /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 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. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 304 | static inline int getT2SOImmValRotateVal(unsigned V) { |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 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 |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 317 | /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 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. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 322 | int Splat = getT2SOImmValSplatVal(Arg); |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 323 | if (Splat != -1) |
| 324 | return Splat; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 325 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 326 | // If 'Arg' can be handled with a single shifter_op return the value. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 327 | int Rot = getT2SOImmValRotateVal(Arg); |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 328 | if (Rot != -1) |
| 329 | return Rot; |
| 330 | |
| 331 | return -1; |
| 332 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 333 | |
Jim Grosbach | 65b7f3a | 2009-10-21 20:44:34 +0000 | [diff] [blame] | 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 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 394 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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. |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 411 | // |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 426 | |
| 427 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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. |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 440 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 452 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 466 | |
| 467 | static inline AMSubMode getAM4SubMode(unsigned Mode) { |
| 468 | return (AMSubMode)(Mode & 0x7); |
| 469 | } |
| 470 | |
Bob Wilson | ab34605 | 2010-03-16 17:46:45 +0000 | [diff] [blame] | 471 | static inline unsigned getAM4ModeImm(AMSubMode SubMode) { |
| 472 | return (int)SubMode; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 473 | } |
| 474 | |
| 475 | //===--------------------------------------------------------------------===// |
| 476 | // Addressing Mode #5 |
| 477 | //===--------------------------------------------------------------------===// |
| 478 | // |
| 479 | // This is used for coprocessor instructions, such as FP load/stores. |
| 480 | // |
| 481 | // addrmode5 := reg +/- imm8*4 |
| 482 | // |
Bob Wilson | d4d826e | 2009-07-01 21:22:45 +0000 | [diff] [blame] | 483 | // The first operand is always a Reg. The second operand encodes the |
| 484 | // operation in bit 8 and the immediate in bits 0-7. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 485 | // |
Bob Wilson | d4d826e | 2009-07-01 21:22:45 +0000 | [diff] [blame] | 486 | // This is also used for FP load/store multiple ops. The second operand |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 487 | // encodes the number of registers (or 2 times the number of registers |
| 488 | // for DPR ops) in bits 0-7. In addition, bits 8-10 encode one of the |
| 489 | // following two sub-modes: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 490 | // |
| 491 | // IA - Increment after |
| 492 | // DB - Decrement before |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 493 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 494 | /// getAM5Opc - This function encodes the addrmode5 opc field. |
| 495 | static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) { |
| 496 | bool isSub = Opc == sub; |
| 497 | return ((int)isSub << 8) | Offset; |
| 498 | } |
| 499 | static inline unsigned char getAM5Offset(unsigned AM5Opc) { |
| 500 | return AM5Opc & 0xFF; |
| 501 | } |
| 502 | static inline AddrOpc getAM5Op(unsigned AM5Opc) { |
| 503 | return ((AM5Opc >> 8) & 1) ? sub : add; |
| 504 | } |
| 505 | |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 506 | /// getAM5Opc - This function encodes the addrmode5 opc field for VLDM and |
| 507 | /// VSTM instructions. |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 508 | static inline unsigned getAM5Opc(AMSubMode SubMode, unsigned char Offset) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 509 | assert((SubMode == ia || SubMode == db) && |
| 510 | "Illegal addressing mode 5 sub-mode!"); |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 511 | return ((int)SubMode << 8) | Offset; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 512 | } |
| 513 | static inline AMSubMode getAM5SubMode(unsigned AM5Opc) { |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 514 | return (AMSubMode)((AM5Opc >> 8) & 0x7); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 515 | } |
Bob Wilson | 8b024a5 | 2009-07-01 23:16:05 +0000 | [diff] [blame] | 516 | |
| 517 | //===--------------------------------------------------------------------===// |
| 518 | // Addressing Mode #6 |
| 519 | //===--------------------------------------------------------------------===// |
| 520 | // |
| 521 | // This is used for NEON load / store instructions. |
| 522 | // |
Bob Wilson | a43e6bf | 2010-03-16 23:01:13 +0000 | [diff] [blame] | 523 | // addrmode6 := reg with optional writeback and alignment |
Bob Wilson | 8b024a5 | 2009-07-01 23:16:05 +0000 | [diff] [blame] | 524 | // |
Bob Wilson | a43e6bf | 2010-03-16 23:01:13 +0000 | [diff] [blame] | 525 | // This is stored in four operands [regaddr, regupdate, opc, align]. The |
| 526 | // first is the address register. The second register holds the value of |
| 527 | // a post-access increment for writeback or reg0 if no writeback or if the |
| 528 | // writeback increment is the size of the memory access. The third |
| 529 | // operand encodes whether there is writeback to the address register. The |
| 530 | // fourth operand is the value of the alignment specifier to use or zero if |
| 531 | // no explicit alignment. |
| 532 | |
| 533 | static inline unsigned getAM6Opc(bool WB = false) { |
| 534 | return (int)WB; |
| 535 | } |
| 536 | |
| 537 | static inline bool getAM6WBFlag(unsigned Mode) { |
| 538 | return Mode & 1; |
| 539 | } |
Bob Wilson | 8b024a5 | 2009-07-01 23:16:05 +0000 | [diff] [blame] | 540 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 541 | } // end namespace ARM_AM |
| 542 | } // end namespace llvm |
| 543 | |
| 544 | #endif |
| 545 | |