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 | |
Johnny Chen | 9e08876 | 2010-03-17 17:52:21 +0000 | [diff] [blame] | 38 | static inline const char *getAddrOpcStr(AddrOpc Op) { |
| 39 | return Op == sub ? "-" : ""; |
| 40 | } |
| 41 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 42 | static inline const char *getShiftOpcStr(ShiftOpc Op) { |
| 43 | switch (Op) { |
Chris Lattner | 8514e21 | 2009-10-19 21:23:15 +0000 | [diff] [blame] | 44 | default: assert(0 && "Unknown shift opc!"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 52 | |
Dan Gohman | 475871a | 2008-07-27 21:46:04 +0000 | [diff] [blame] | 53 | static inline ShiftOpc getShiftOpcForNode(SDValue N) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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) { |
Chris Lattner | 8514e21 | 2009-10-19 21:23:15 +0000 | [diff] [blame] | 77 | default: assert(0 && "Unknown addressing sub-mode!"); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 91 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 98 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 | } |
Bob Wilson | d83712a | 2009-03-30 18:49:37 +0000 | [diff] [blame] | 128 | /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 129 | /// the rotate amount. |
| 130 | static inline unsigned getSOImmValRot(unsigned Imm) { |
| 131 | return (Imm >> 8) * 2; |
| 132 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 133 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 142 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 143 | // Use CTZ to compute the rotate amount. |
| 144 | unsigned TZ = CountTrailingZeros_32(Imm); |
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 | // Rotate amount must be even. Something like 0x200 must be rotated 8 bits, |
| 147 | // not 9. |
| 148 | unsigned RotAmt = TZ & ~1; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 149 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 skip the first run of ones, then |
| 155 | // retry the hunt. |
| 156 | if (Imm & 1) { |
| 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 |
| 160 | // ones. |
| 161 | unsigned TZ2 = CountTrailingZeros_32(Imm & ~((1 << TrailingOnes)-1)); |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 162 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 163 | // Rotate amount must be even. |
| 164 | unsigned RotAmt2 = TZ2 & ~1; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 165 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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. |
| 169 | } |
| 170 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 171 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 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 |
| 174 | // handle. |
| 175 | return (32-RotAmt)&31; // HW rotates right, not left. |
| 176 | } |
| 177 | |
| 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 |
| 183 | // of zero. |
| 184 | if ((Arg & ~255U) == 0) return Arg; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 185 | |
Johnny Chen | e6f8387 | 2010-03-17 18:32:39 +0000 | [diff] [blame] | 186 | unsigned RotAmt = getSOImmValRotate(Arg); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 187 | |
| 188 | // If this cannot be handled with a single shifter_op, bail out. |
| 189 | if (rotr32(~255U, RotAmt) & Arg) |
| 190 | return -1; |
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 | // Encode this correctly. |
| 193 | return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8); |
| 194 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 195 | |
Johnny Chen | bb6e9d8 | 2010-04-12 18:46:53 +0000 | [diff] [blame] | 196 | /// getSOImmValOneRotate - Try to handle Imm with an immediate shifter |
| 197 | /// operand, computing the rotate amount to use. If this immediate value |
| 198 | /// cannot be handled with a single shifter-op, return 0. |
| 199 | static unsigned getSOImmValOneRotate(unsigned Imm) { |
| 200 | // A5.2.4 Constants with multiple encodings |
| 201 | // The lowest unsigned value of rotation wins! |
| 202 | for (unsigned R = 1; R <= 15; ++R) |
| 203 | if ((Imm & rotr32(~255U, 2*R)) == 0) |
| 204 | return 2*R; |
| 205 | |
| 206 | // Failed to find a suitable rotate amount. |
| 207 | return 0; |
| 208 | } |
| 209 | |
| 210 | /// getSOImmValOneOrNoRotate - Given a 32-bit immediate, if it is something |
| 211 | /// that can fit into a shifter_operand immediate operand, return the 12-bit |
| 212 | /// encoding for it. If not, return -1. This is different from getSOImmVal() |
| 213 | /// in that getSOImmVal() is used during codegen, for example, |
| 214 | /// rewriteARMFrameIndex() where return value of -1 is not considered fatal. |
| 215 | /// |
| 216 | /// The current consumer of this API is printSOImm() within ARMInstPrinter.cpp |
| 217 | /// where return value of -1 indicates that the Arg is not a valid so_imm val! |
| 218 | static inline int getSOImmValOneOrNoRotate(unsigned Arg) { |
| 219 | // 8-bit (or less) immediates are trivially shifter_operands with a rotate |
| 220 | // of zero. |
| 221 | if ((Arg & ~255U) == 0) return Arg; |
| 222 | |
| 223 | unsigned RotAmt = getSOImmValOneRotate(Arg); |
| 224 | |
| 225 | // If this cannot be handled with a single shifter_op, bail out. |
| 226 | if (rotr32(~255U, RotAmt) & Arg) |
| 227 | return -1; |
| 228 | |
| 229 | // Encode this correctly. |
| 230 | return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8); |
| 231 | } |
| 232 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 233 | /// isSOImmTwoPartVal - Return true if the specified value can be obtained by |
| 234 | /// or'ing together two SOImmVal's. |
| 235 | static inline bool isSOImmTwoPartVal(unsigned V) { |
| 236 | // If this can be handled with a single shifter_op, bail out. |
| 237 | V = rotr32(~255U, getSOImmValRotate(V)) & V; |
| 238 | if (V == 0) |
| 239 | return false; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 240 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 241 | // If this can be handled with two shifter_op's, accept. |
| 242 | V = rotr32(~255U, getSOImmValRotate(V)) & V; |
| 243 | return V == 0; |
| 244 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 245 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 246 | /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal, |
| 247 | /// return the first chunk of it. |
| 248 | static inline unsigned getSOImmTwoPartFirst(unsigned V) { |
| 249 | return rotr32(255U, getSOImmValRotate(V)) & V; |
| 250 | } |
| 251 | |
| 252 | /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal, |
| 253 | /// return the second chunk of it. |
| 254 | static inline unsigned getSOImmTwoPartSecond(unsigned V) { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 255 | // Mask out the first hunk. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 256 | V = rotr32(~255U, getSOImmValRotate(V)) & V; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 257 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 258 | // Take what's left. |
| 259 | assert(V == (rotr32(255U, getSOImmValRotate(V)) & V)); |
| 260 | return V; |
| 261 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 262 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 263 | /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed |
| 264 | /// by a left shift. Returns the shift amount to use. |
| 265 | static inline unsigned getThumbImmValShift(unsigned Imm) { |
| 266 | // 8-bit (or less) immediates are trivially immediate operand with a shift |
| 267 | // of zero. |
| 268 | if ((Imm & ~255U) == 0) return 0; |
| 269 | |
| 270 | // Use CTZ to compute the shift amount. |
| 271 | return CountTrailingZeros_32(Imm); |
| 272 | } |
| 273 | |
| 274 | /// isThumbImmShiftedVal - Return true if the specified value can be obtained |
| 275 | /// by left shifting a 8-bit immediate. |
| 276 | static inline bool isThumbImmShiftedVal(unsigned V) { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 277 | // If this can be handled with |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 278 | V = (~255U << getThumbImmValShift(V)) & V; |
| 279 | return V == 0; |
| 280 | } |
| 281 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 282 | /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed |
| 283 | /// by a left shift. Returns the shift amount to use. |
| 284 | static inline unsigned getThumbImm16ValShift(unsigned Imm) { |
| 285 | // 16-bit (or less) immediates are trivially immediate operand with a shift |
| 286 | // of zero. |
| 287 | if ((Imm & ~65535U) == 0) return 0; |
| 288 | |
| 289 | // Use CTZ to compute the shift amount. |
| 290 | return CountTrailingZeros_32(Imm); |
| 291 | } |
| 292 | |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 293 | /// isThumbImm16ShiftedVal - Return true if the specified value can be |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 294 | /// obtained by left shifting a 16-bit immediate. |
| 295 | static inline bool isThumbImm16ShiftedVal(unsigned V) { |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 296 | // If this can be handled with |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 297 | V = (~65535U << getThumbImm16ValShift(V)) & V; |
| 298 | return V == 0; |
| 299 | } |
| 300 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 301 | /// getThumbImmNonShiftedVal - If V is a value that satisfies |
| 302 | /// isThumbImmShiftedVal, return the non-shiftd value. |
| 303 | static inline unsigned getThumbImmNonShiftedVal(unsigned V) { |
| 304 | return V >> getThumbImmValShift(V); |
| 305 | } |
| 306 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 307 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 308 | /// getT2SOImmValSplat - Return the 12-bit encoded representation |
| 309 | /// if the specified value can be obtained by splatting the low 8 bits |
| 310 | /// into every other byte or every byte of a 32-bit value. i.e., |
| 311 | /// 00000000 00000000 00000000 abcdefgh control = 0 |
| 312 | /// 00000000 abcdefgh 00000000 abcdefgh control = 1 |
| 313 | /// abcdefgh 00000000 abcdefgh 00000000 control = 2 |
| 314 | /// abcdefgh abcdefgh abcdefgh abcdefgh control = 3 |
| 315 | /// Return -1 if none of the above apply. |
| 316 | /// See ARM Reference Manual A6.3.2. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 317 | static inline int getT2SOImmValSplatVal(unsigned V) { |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 318 | unsigned u, Vs, Imm; |
| 319 | // control = 0 |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 320 | if ((V & 0xffffff00) == 0) |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 321 | return V; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 322 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 323 | // If the value is zeroes in the first byte, just shift those off |
| 324 | Vs = ((V & 0xff) == 0) ? V >> 8 : V; |
| 325 | // Any passing value only has 8 bits of payload, splatted across the word |
| 326 | Imm = Vs & 0xff; |
| 327 | // Likewise, any passing values have the payload splatted into the 3rd byte |
| 328 | u = Imm | (Imm << 16); |
| 329 | |
| 330 | // control = 1 or 2 |
| 331 | if (Vs == u) |
| 332 | return (((Vs == V) ? 1 : 2) << 8) | Imm; |
| 333 | |
| 334 | // control = 3 |
| 335 | if (Vs == (u | (u << 8))) |
| 336 | return (3 << 8) | Imm; |
| 337 | |
| 338 | return -1; |
| 339 | } |
| 340 | |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 341 | /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 342 | /// specified value is a rotated 8-bit value. Return -1 if no rotation |
| 343 | /// encoding is possible. |
| 344 | /// See ARM Reference Manual A6.3.2. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 345 | static inline int getT2SOImmValRotateVal(unsigned V) { |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 346 | unsigned RotAmt = CountLeadingZeros_32(V); |
| 347 | if (RotAmt >= 24) |
| 348 | return -1; |
| 349 | |
| 350 | // If 'Arg' can be handled with a single shifter_op return the value. |
| 351 | if ((rotr32(0xff000000U, RotAmt) & V) == V) |
| 352 | return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7); |
| 353 | |
| 354 | return -1; |
| 355 | } |
| 356 | |
| 357 | /// 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] | 358 | /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 359 | /// encoding for it. If not, return -1. |
| 360 | /// See ARM Reference Manual A6.3.2. |
| 361 | static inline int getT2SOImmVal(unsigned Arg) { |
| 362 | // If 'Arg' is an 8-bit splat, then get the encoded value. |
Evan Cheng | 6495f63 | 2009-07-28 05:48:47 +0000 | [diff] [blame] | 363 | int Splat = getT2SOImmValSplatVal(Arg); |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 364 | if (Splat != -1) |
| 365 | return Splat; |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 366 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 367 | // 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] | 368 | int Rot = getT2SOImmValRotateVal(Arg); |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 369 | if (Rot != -1) |
| 370 | return Rot; |
| 371 | |
| 372 | return -1; |
| 373 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 374 | |
Jim Grosbach | 65b7f3a | 2009-10-21 20:44:34 +0000 | [diff] [blame] | 375 | static inline unsigned getT2SOImmValRotate(unsigned V) { |
| 376 | if ((V & ~255U) == 0) return 0; |
| 377 | // Use CTZ to compute the rotate amount. |
| 378 | unsigned RotAmt = CountTrailingZeros_32(V); |
| 379 | return (32 - RotAmt) & 31; |
| 380 | } |
| 381 | |
| 382 | static inline bool isT2SOImmTwoPartVal (unsigned Imm) { |
| 383 | unsigned V = Imm; |
| 384 | // Passing values can be any combination of splat values and shifter |
| 385 | // values. If this can be handled with a single shifter or splat, bail |
| 386 | // out. Those should be handled directly, not with a two-part val. |
| 387 | if (getT2SOImmValSplatVal(V) != -1) |
| 388 | return false; |
| 389 | V = rotr32 (~255U, getT2SOImmValRotate(V)) & V; |
| 390 | if (V == 0) |
| 391 | return false; |
| 392 | |
| 393 | // If this can be handled as an immediate, accept. |
| 394 | if (getT2SOImmVal(V) != -1) return true; |
| 395 | |
| 396 | // Likewise, try masking out a splat value first. |
| 397 | V = Imm; |
| 398 | if (getT2SOImmValSplatVal(V & 0xff00ff00U) != -1) |
| 399 | V &= ~0xff00ff00U; |
| 400 | else if (getT2SOImmValSplatVal(V & 0x00ff00ffU) != -1) |
| 401 | V &= ~0x00ff00ffU; |
| 402 | // If what's left can be handled as an immediate, accept. |
| 403 | if (getT2SOImmVal(V) != -1) return true; |
| 404 | |
| 405 | // Otherwise, do not accept. |
| 406 | return false; |
| 407 | } |
| 408 | |
| 409 | static inline unsigned getT2SOImmTwoPartFirst(unsigned Imm) { |
| 410 | assert (isT2SOImmTwoPartVal(Imm) && |
| 411 | "Immedate cannot be encoded as two part immediate!"); |
| 412 | // Try a shifter operand as one part |
| 413 | unsigned V = rotr32 (~255, getT2SOImmValRotate(Imm)) & Imm; |
| 414 | // If the rest is encodable as an immediate, then return it. |
| 415 | if (getT2SOImmVal(V) != -1) return V; |
| 416 | |
| 417 | // Try masking out a splat value first. |
| 418 | if (getT2SOImmValSplatVal(Imm & 0xff00ff00U) != -1) |
| 419 | return Imm & 0xff00ff00U; |
| 420 | |
| 421 | // The other splat is all that's left as an option. |
| 422 | assert (getT2SOImmValSplatVal(Imm & 0x00ff00ffU) != -1); |
| 423 | return Imm & 0x00ff00ffU; |
| 424 | } |
| 425 | |
| 426 | static inline unsigned getT2SOImmTwoPartSecond(unsigned Imm) { |
| 427 | // Mask out the first hunk |
| 428 | Imm ^= getT2SOImmTwoPartFirst(Imm); |
| 429 | // Return what's left |
| 430 | assert (getT2SOImmVal(Imm) != -1 && |
| 431 | "Unable to encode second part of T2 two part SO immediate"); |
| 432 | return Imm; |
| 433 | } |
| 434 | |
Evan Cheng | f49810c | 2009-06-23 17:48:47 +0000 | [diff] [blame] | 435 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 436 | //===--------------------------------------------------------------------===// |
| 437 | // Addressing Mode #2 |
| 438 | //===--------------------------------------------------------------------===// |
| 439 | // |
| 440 | // This is used for most simple load/store instructions. |
| 441 | // |
| 442 | // addrmode2 := reg +/- reg shop imm |
| 443 | // addrmode2 := reg +/- imm12 |
| 444 | // |
| 445 | // The first operand is always a Reg. The second operand is a reg if in |
| 446 | // reg/reg form, otherwise it's reg#0. The third field encodes the operation |
| 447 | // in bit 12, the immediate in bits 0-11, and the shift op in 13-15. |
| 448 | // |
| 449 | // If this addressing mode is a frame index (before prolog/epilog insertion |
| 450 | // and code rewriting), this operand will have the form: FI#, reg0, <offs> |
| 451 | // with no shift amount for the frame offset. |
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 | static inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO) { |
| 454 | assert(Imm12 < (1 << 12) && "Imm too large!"); |
| 455 | bool isSub = Opc == sub; |
| 456 | return Imm12 | ((int)isSub << 12) | (SO << 13); |
| 457 | } |
| 458 | static inline unsigned getAM2Offset(unsigned AM2Opc) { |
| 459 | return AM2Opc & ((1 << 12)-1); |
| 460 | } |
| 461 | static inline AddrOpc getAM2Op(unsigned AM2Opc) { |
| 462 | return ((AM2Opc >> 12) & 1) ? sub : add; |
| 463 | } |
| 464 | static inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) { |
| 465 | return (ShiftOpc)(AM2Opc >> 13); |
| 466 | } |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 467 | |
| 468 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 469 | //===--------------------------------------------------------------------===// |
| 470 | // Addressing Mode #3 |
| 471 | //===--------------------------------------------------------------------===// |
| 472 | // |
| 473 | // This is used for sign-extending loads, and load/store-pair instructions. |
| 474 | // |
| 475 | // addrmode3 := reg +/- reg |
| 476 | // addrmode3 := reg +/- imm8 |
| 477 | // |
| 478 | // The first operand is always a Reg. The second operand is a reg if in |
| 479 | // reg/reg form, otherwise it's reg#0. The third field encodes the operation |
| 480 | // in bit 8, the immediate in bits 0-7. |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 481 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 482 | /// getAM3Opc - This function encodes the addrmode3 opc field. |
| 483 | static inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset) { |
| 484 | bool isSub = Opc == sub; |
| 485 | return ((int)isSub << 8) | Offset; |
| 486 | } |
| 487 | static inline unsigned char getAM3Offset(unsigned AM3Opc) { |
| 488 | return AM3Opc & 0xFF; |
| 489 | } |
| 490 | static inline AddrOpc getAM3Op(unsigned AM3Opc) { |
| 491 | return ((AM3Opc >> 8) & 1) ? sub : add; |
| 492 | } |
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 | //===--------------------------------------------------------------------===// |
| 495 | // Addressing Mode #4 |
| 496 | //===--------------------------------------------------------------------===// |
| 497 | // |
| 498 | // This is used for load / store multiple instructions. |
| 499 | // |
| 500 | // addrmode4 := reg, <mode> |
| 501 | // |
| 502 | // The four modes are: |
| 503 | // IA - Increment after |
| 504 | // IB - Increment before |
| 505 | // DA - Decrement after |
| 506 | // DB - Decrement before |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 507 | |
| 508 | static inline AMSubMode getAM4SubMode(unsigned Mode) { |
| 509 | return (AMSubMode)(Mode & 0x7); |
| 510 | } |
| 511 | |
Bob Wilson | ab34605 | 2010-03-16 17:46:45 +0000 | [diff] [blame] | 512 | static inline unsigned getAM4ModeImm(AMSubMode SubMode) { |
| 513 | return (int)SubMode; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 514 | } |
| 515 | |
| 516 | //===--------------------------------------------------------------------===// |
| 517 | // Addressing Mode #5 |
| 518 | //===--------------------------------------------------------------------===// |
| 519 | // |
| 520 | // This is used for coprocessor instructions, such as FP load/stores. |
| 521 | // |
| 522 | // addrmode5 := reg +/- imm8*4 |
| 523 | // |
Bob Wilson | d4d826e | 2009-07-01 21:22:45 +0000 | [diff] [blame] | 524 | // The first operand is always a Reg. The second operand encodes the |
| 525 | // operation in bit 8 and the immediate in bits 0-7. |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 526 | // |
Bob Wilson | d4d826e | 2009-07-01 21:22:45 +0000 | [diff] [blame] | 527 | // 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] | 528 | // encodes the number of registers (or 2 times the number of registers |
| 529 | // for DPR ops) in bits 0-7. In addition, bits 8-10 encode one of the |
| 530 | // following two sub-modes: |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 531 | // |
| 532 | // IA - Increment after |
| 533 | // DB - Decrement before |
Jim Grosbach | 764ab52 | 2009-08-11 15:33:49 +0000 | [diff] [blame] | 534 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 535 | /// getAM5Opc - This function encodes the addrmode5 opc field. |
| 536 | static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) { |
| 537 | bool isSub = Opc == sub; |
| 538 | return ((int)isSub << 8) | Offset; |
| 539 | } |
| 540 | static inline unsigned char getAM5Offset(unsigned AM5Opc) { |
| 541 | return AM5Opc & 0xFF; |
| 542 | } |
| 543 | static inline AddrOpc getAM5Op(unsigned AM5Opc) { |
| 544 | return ((AM5Opc >> 8) & 1) ? sub : add; |
| 545 | } |
| 546 | |
Jim Grosbach | e516549 | 2009-11-09 00:11:35 +0000 | [diff] [blame] | 547 | /// getAM5Opc - This function encodes the addrmode5 opc field for VLDM and |
| 548 | /// VSTM instructions. |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 549 | static inline unsigned getAM5Opc(AMSubMode SubMode, unsigned char Offset) { |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 550 | assert((SubMode == ia || SubMode == db) && |
| 551 | "Illegal addressing mode 5 sub-mode!"); |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 552 | return ((int)SubMode << 8) | Offset; |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 553 | } |
| 554 | static inline AMSubMode getAM5SubMode(unsigned AM5Opc) { |
Bob Wilson | 2d357f6 | 2010-03-16 18:38:09 +0000 | [diff] [blame] | 555 | return (AMSubMode)((AM5Opc >> 8) & 0x7); |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 556 | } |
Bob Wilson | 8b024a5 | 2009-07-01 23:16:05 +0000 | [diff] [blame] | 557 | |
| 558 | //===--------------------------------------------------------------------===// |
| 559 | // Addressing Mode #6 |
| 560 | //===--------------------------------------------------------------------===// |
| 561 | // |
| 562 | // This is used for NEON load / store instructions. |
| 563 | // |
Bob Wilson | 226036e | 2010-03-20 22:13:40 +0000 | [diff] [blame] | 564 | // addrmode6 := reg with optional alignment |
Bob Wilson | 8b024a5 | 2009-07-01 23:16:05 +0000 | [diff] [blame] | 565 | // |
Bob Wilson | 226036e | 2010-03-20 22:13:40 +0000 | [diff] [blame] | 566 | // This is stored in two operands [regaddr, align]. The first is the |
| 567 | // address register. The second operand is the value of the alignment |
| 568 | // specifier to use or zero if no explicit alignment. |
Bob Wilson | 8b024a5 | 2009-07-01 23:16:05 +0000 | [diff] [blame] | 569 | |
Evan Cheng | a8e2989 | 2007-01-19 07:51:42 +0000 | [diff] [blame] | 570 | } // end namespace ARM_AM |
| 571 | } // end namespace llvm |
| 572 | |
| 573 | #endif |
| 574 | |