LLVM API Documentation
00001 //===- ARMAddressingModes.h - ARM Addressing Modes --------------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file contains the ARM addressing mode implementation stuff. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_TARGET_ARM_ARMADDRESSINGMODES_H 00015 #define LLVM_TARGET_ARM_ARMADDRESSINGMODES_H 00016 00017 #include "llvm/CodeGen/SelectionDAGNodes.h" 00018 #include "llvm/Support/ErrorHandling.h" 00019 #include "llvm/Support/MathExtras.h" 00020 #include <cassert> 00021 00022 namespace llvm { 00023 00024 /// ARM_AM - ARM Addressing Mode Stuff 00025 namespace ARM_AM { 00026 enum ShiftOpc { 00027 no_shift = 0, 00028 asr, 00029 lsl, 00030 lsr, 00031 ror, 00032 rrx 00033 }; 00034 00035 enum AddrOpc { 00036 add = '+', sub = '-' 00037 }; 00038 00039 static inline const char *getShiftOpcStr(ShiftOpc Op) { 00040 switch (Op) { 00041 default: llvm_unreachable("Unknown shift opc!"); 00042 case ARM_AM::asr: return "asr"; 00043 case ARM_AM::lsl: return "lsl"; 00044 case ARM_AM::lsr: return "lsr"; 00045 case ARM_AM::ror: return "ror"; 00046 case ARM_AM::rrx: return "rrx"; 00047 } 00048 } 00049 00050 static inline ShiftOpc getShiftOpcForNode(SDValue N) { 00051 switch (N.getOpcode()) { 00052 default: return ARM_AM::no_shift; 00053 case ISD::SHL: return ARM_AM::lsl; 00054 case ISD::SRL: return ARM_AM::lsr; 00055 case ISD::SRA: return ARM_AM::asr; 00056 case ISD::ROTR: return ARM_AM::ror; 00057 //case ISD::ROTL: // Only if imm -> turn into ROTR. 00058 // Can't handle RRX here, because it would require folding a flag into 00059 // the addressing mode. :( This causes us to miss certain things. 00060 //case ARMISD::RRX: return ARM_AM::rrx; 00061 } 00062 } 00063 00064 enum AMSubMode { 00065 bad_am_submode = 0, 00066 ia, 00067 ib, 00068 da, 00069 db 00070 }; 00071 00072 static inline const char *getAMSubModeStr(AMSubMode Mode) { 00073 switch (Mode) { 00074 default: llvm_unreachable("Unknown addressing sub-mode!"); 00075 case ARM_AM::ia: return "ia"; 00076 case ARM_AM::ib: return "ib"; 00077 case ARM_AM::da: return "da"; 00078 case ARM_AM::db: return "db"; 00079 } 00080 } 00081 00082 static inline const char *getAMSubModeAltStr(AMSubMode Mode, bool isLD) { 00083 switch (Mode) { 00084 default: llvm_unreachable("Unknown addressing sub-mode!"); 00085 case ARM_AM::ia: return isLD ? "fd" : "ea"; 00086 case ARM_AM::ib: return isLD ? "ed" : "fa"; 00087 case ARM_AM::da: return isLD ? "fa" : "ed"; 00088 case ARM_AM::db: return isLD ? "ea" : "fd"; 00089 } 00090 } 00091 00092 /// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits. 00093 /// 00094 static inline unsigned rotr32(unsigned Val, unsigned Amt) { 00095 assert(Amt < 32 && "Invalid rotate amount"); 00096 return (Val >> Amt) | (Val << ((32-Amt)&31)); 00097 } 00098 00099 /// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits. 00100 /// 00101 static inline unsigned rotl32(unsigned Val, unsigned Amt) { 00102 assert(Amt < 32 && "Invalid rotate amount"); 00103 return (Val << Amt) | (Val >> ((32-Amt)&31)); 00104 } 00105 00106 //===--------------------------------------------------------------------===// 00107 // Addressing Mode #1: shift_operand with registers 00108 //===--------------------------------------------------------------------===// 00109 // 00110 // This 'addressing mode' is used for arithmetic instructions. It can 00111 // represent things like: 00112 // reg 00113 // reg [asr|lsl|lsr|ror|rrx] reg 00114 // reg [asr|lsl|lsr|ror|rrx] imm 00115 // 00116 // This is stored three operands [rega, regb, opc]. The first is the base 00117 // reg, the second is the shift amount (or reg0 if not present or imm). The 00118 // third operand encodes the shift opcode and the imm if a reg isn't present. 00119 // 00120 static inline unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm) { 00121 return ShOp | (Imm << 3); 00122 } 00123 static inline unsigned getSORegOffset(unsigned Op) { 00124 return Op >> 3; 00125 } 00126 static inline ShiftOpc getSORegShOp(unsigned Op) { 00127 return (ShiftOpc)(Op & 7); 00128 } 00129 00130 /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return 00131 /// the 8-bit imm value. 00132 static inline unsigned getSOImmValImm(unsigned Imm) { 00133 return Imm & 0xFF; 00134 } 00135 /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return 00136 /// the rotate amount. 00137 static inline unsigned getSOImmValRot(unsigned Imm) { 00138 return (Imm >> 8) * 2; 00139 } 00140 00141 /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand, 00142 /// computing the rotate amount to use. If this immediate value cannot be 00143 /// handled with a single shifter-op, determine a good rotate amount that will 00144 /// take a maximal chunk of bits out of the immediate. 00145 static inline unsigned getSOImmValRotate(unsigned Imm) { 00146 // 8-bit (or less) immediates are trivially shifter_operands with a rotate 00147 // of zero. 00148 if ((Imm & ~255U) == 0) return 0; 00149 00150 // Use CTZ to compute the rotate amount. 00151 unsigned TZ = CountTrailingZeros_32(Imm); 00152 00153 // Rotate amount must be even. Something like 0x200 must be rotated 8 bits, 00154 // not 9. 00155 unsigned RotAmt = TZ & ~1; 00156 00157 // If we can handle this spread, return it. 00158 if ((rotr32(Imm, RotAmt) & ~255U) == 0) 00159 return (32-RotAmt)&31; // HW rotates right, not left. 00160 00161 // For values like 0xF000000F, we should skip the first run of ones, then 00162 // retry the hunt. 00163 if (Imm & 1) { 00164 unsigned TrailingOnes = CountTrailingZeros_32(~Imm); 00165 if (TrailingOnes != 32) { // Avoid overflow on 0xFFFFFFFF 00166 // Restart the search for a high-order bit after the initial seconds of 00167 // ones. 00168 unsigned TZ2 = CountTrailingZeros_32(Imm & ~((1 << TrailingOnes)-1)); 00169 00170 // Rotate amount must be even. 00171 unsigned RotAmt2 = TZ2 & ~1; 00172 00173 // If this fits, use it. 00174 if (RotAmt2 != 32 && (rotr32(Imm, RotAmt2) & ~255U) == 0) 00175 return (32-RotAmt2)&31; // HW rotates right, not left. 00176 } 00177 } 00178 00179 // Otherwise, we have no way to cover this span of bits with a single 00180 // shifter_op immediate. Return a chunk of bits that will be useful to 00181 // handle. 00182 return (32-RotAmt)&31; // HW rotates right, not left. 00183 } 00184 00185 /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit 00186 /// into an shifter_operand immediate operand, return the 12-bit encoding for 00187 /// it. If not, return -1. 00188 static inline int getSOImmVal(unsigned Arg) { 00189 // 8-bit (or less) immediates are trivially shifter_operands with a rotate 00190 // of zero. 00191 if ((Arg & ~255U) == 0) return Arg; 00192 00193 unsigned RotAmt = getSOImmValRotate(Arg); 00194 00195 // If this cannot be handled with a single shifter_op, bail out. 00196 if (rotr32(~255U, RotAmt) & Arg) 00197 return -1; 00198 00199 // Encode this correctly. 00200 return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8); 00201 } 00202 00203 /// isSOImmTwoPartVal - Return true if the specified value can be obtained by 00204 /// or'ing together two SOImmVal's. 00205 static inline bool isSOImmTwoPartVal(unsigned V) { 00206 // If this can be handled with a single shifter_op, bail out. 00207 V = rotr32(~255U, getSOImmValRotate(V)) & V; 00208 if (V == 0) 00209 return false; 00210 00211 // If this can be handled with two shifter_op's, accept. 00212 V = rotr32(~255U, getSOImmValRotate(V)) & V; 00213 return V == 0; 00214 } 00215 00216 /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal, 00217 /// return the first chunk of it. 00218 static inline unsigned getSOImmTwoPartFirst(unsigned V) { 00219 return rotr32(255U, getSOImmValRotate(V)) & V; 00220 } 00221 00222 /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal, 00223 /// return the second chunk of it. 00224 static inline unsigned getSOImmTwoPartSecond(unsigned V) { 00225 // Mask out the first hunk. 00226 V = rotr32(~255U, getSOImmValRotate(V)) & V; 00227 00228 // Take what's left. 00229 assert(V == (rotr32(255U, getSOImmValRotate(V)) & V)); 00230 return V; 00231 } 00232 00233 /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed 00234 /// by a left shift. Returns the shift amount to use. 00235 static inline unsigned getThumbImmValShift(unsigned Imm) { 00236 // 8-bit (or less) immediates are trivially immediate operand with a shift 00237 // of zero. 00238 if ((Imm & ~255U) == 0) return 0; 00239 00240 // Use CTZ to compute the shift amount. 00241 return CountTrailingZeros_32(Imm); 00242 } 00243 00244 /// isThumbImmShiftedVal - Return true if the specified value can be obtained 00245 /// by left shifting a 8-bit immediate. 00246 static inline bool isThumbImmShiftedVal(unsigned V) { 00247 // If this can be handled with 00248 V = (~255U << getThumbImmValShift(V)) & V; 00249 return V == 0; 00250 } 00251 00252 /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed 00253 /// by a left shift. Returns the shift amount to use. 00254 static inline unsigned getThumbImm16ValShift(unsigned Imm) { 00255 // 16-bit (or less) immediates are trivially immediate operand with a shift 00256 // of zero. 00257 if ((Imm & ~65535U) == 0) return 0; 00258 00259 // Use CTZ to compute the shift amount. 00260 return CountTrailingZeros_32(Imm); 00261 } 00262 00263 /// isThumbImm16ShiftedVal - Return true if the specified value can be 00264 /// obtained by left shifting a 16-bit immediate. 00265 static inline bool isThumbImm16ShiftedVal(unsigned V) { 00266 // If this can be handled with 00267 V = (~65535U << getThumbImm16ValShift(V)) & V; 00268 return V == 0; 00269 } 00270 00271 /// getThumbImmNonShiftedVal - If V is a value that satisfies 00272 /// isThumbImmShiftedVal, return the non-shiftd value. 00273 static inline unsigned getThumbImmNonShiftedVal(unsigned V) { 00274 return V >> getThumbImmValShift(V); 00275 } 00276 00277 00278 /// getT2SOImmValSplat - Return the 12-bit encoded representation 00279 /// if the specified value can be obtained by splatting the low 8 bits 00280 /// into every other byte or every byte of a 32-bit value. i.e., 00281 /// 00000000 00000000 00000000 abcdefgh control = 0 00282 /// 00000000 abcdefgh 00000000 abcdefgh control = 1 00283 /// abcdefgh 00000000 abcdefgh 00000000 control = 2 00284 /// abcdefgh abcdefgh abcdefgh abcdefgh control = 3 00285 /// Return -1 if none of the above apply. 00286 /// See ARM Reference Manual A6.3.2. 00287 static inline int getT2SOImmValSplatVal(unsigned V) { 00288 unsigned u, Vs, Imm; 00289 // control = 0 00290 if ((V & 0xffffff00) == 0) 00291 return V; 00292 00293 // If the value is zeroes in the first byte, just shift those off 00294 Vs = ((V & 0xff) == 0) ? V >> 8 : V; 00295 // Any passing value only has 8 bits of payload, splatted across the word 00296 Imm = Vs & 0xff; 00297 // Likewise, any passing values have the payload splatted into the 3rd byte 00298 u = Imm | (Imm << 16); 00299 00300 // control = 1 or 2 00301 if (Vs == u) 00302 return (((Vs == V) ? 1 : 2) << 8) | Imm; 00303 00304 // control = 3 00305 if (Vs == (u | (u << 8))) 00306 return (3 << 8) | Imm; 00307 00308 return -1; 00309 } 00310 00311 /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the 00312 /// specified value is a rotated 8-bit value. Return -1 if no rotation 00313 /// encoding is possible. 00314 /// See ARM Reference Manual A6.3.2. 00315 static inline int getT2SOImmValRotateVal(unsigned V) { 00316 unsigned RotAmt = CountLeadingZeros_32(V); 00317 if (RotAmt >= 24) 00318 return -1; 00319 00320 // If 'Arg' can be handled with a single shifter_op return the value. 00321 if ((rotr32(0xff000000U, RotAmt) & V) == V) 00322 return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7); 00323 00324 return -1; 00325 } 00326 00327 /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit 00328 /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit 00329 /// encoding for it. If not, return -1. 00330 /// See ARM Reference Manual A6.3.2. 00331 static inline int getT2SOImmVal(unsigned Arg) { 00332 // If 'Arg' is an 8-bit splat, then get the encoded value. 00333 int Splat = getT2SOImmValSplatVal(Arg); 00334 if (Splat != -1) 00335 return Splat; 00336 00337 // If 'Arg' can be handled with a single shifter_op return the value. 00338 int Rot = getT2SOImmValRotateVal(Arg); 00339 if (Rot != -1) 00340 return Rot; 00341 00342 return -1; 00343 } 00344 00345 00346 //===--------------------------------------------------------------------===// 00347 // Addressing Mode #2 00348 //===--------------------------------------------------------------------===// 00349 // 00350 // This is used for most simple load/store instructions. 00351 // 00352 // addrmode2 := reg +/- reg shop imm 00353 // addrmode2 := reg +/- imm12 00354 // 00355 // The first operand is always a Reg. The second operand is a reg if in 00356 // reg/reg form, otherwise it's reg#0. The third field encodes the operation 00357 // in bit 12, the immediate in bits 0-11, and the shift op in 13-15. 00358 // 00359 // If this addressing mode is a frame index (before prolog/epilog insertion 00360 // and code rewriting), this operand will have the form: FI#, reg0, <offs> 00361 // with no shift amount for the frame offset. 00362 // 00363 static inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO) { 00364 assert(Imm12 < (1 << 12) && "Imm too large!"); 00365 bool isSub = Opc == sub; 00366 return Imm12 | ((int)isSub << 12) | (SO << 13); 00367 } 00368 static inline unsigned getAM2Offset(unsigned AM2Opc) { 00369 return AM2Opc & ((1 << 12)-1); 00370 } 00371 static inline AddrOpc getAM2Op(unsigned AM2Opc) { 00372 return ((AM2Opc >> 12) & 1) ? sub : add; 00373 } 00374 static inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) { 00375 return (ShiftOpc)(AM2Opc >> 13); 00376 } 00377 00378 00379 //===--------------------------------------------------------------------===// 00380 // Addressing Mode #3 00381 //===--------------------------------------------------------------------===// 00382 // 00383 // This is used for sign-extending loads, and load/store-pair instructions. 00384 // 00385 // addrmode3 := reg +/- reg 00386 // addrmode3 := reg +/- imm8 00387 // 00388 // The first operand is always a Reg. The second operand is a reg if in 00389 // reg/reg form, otherwise it's reg#0. The third field encodes the operation 00390 // in bit 8, the immediate in bits 0-7. 00391 00392 /// getAM3Opc - This function encodes the addrmode3 opc field. 00393 static inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset) { 00394 bool isSub = Opc == sub; 00395 return ((int)isSub << 8) | Offset; 00396 } 00397 static inline unsigned char getAM3Offset(unsigned AM3Opc) { 00398 return AM3Opc & 0xFF; 00399 } 00400 static inline AddrOpc getAM3Op(unsigned AM3Opc) { 00401 return ((AM3Opc >> 8) & 1) ? sub : add; 00402 } 00403 00404 //===--------------------------------------------------------------------===// 00405 // Addressing Mode #4 00406 //===--------------------------------------------------------------------===// 00407 // 00408 // This is used for load / store multiple instructions. 00409 // 00410 // addrmode4 := reg, <mode> 00411 // 00412 // The four modes are: 00413 // IA - Increment after 00414 // IB - Increment before 00415 // DA - Decrement after 00416 // DB - Decrement before 00417 // 00418 // If the 4th bit (writeback)is set, then the base register is updated after 00419 // the memory transfer. 00420 00421 static inline AMSubMode getAM4SubMode(unsigned Mode) { 00422 return (AMSubMode)(Mode & 0x7); 00423 } 00424 00425 static inline unsigned getAM4ModeImm(AMSubMode SubMode, bool WB = false) { 00426 return (int)SubMode | ((int)WB << 3); 00427 } 00428 00429 static inline bool getAM4WBFlag(unsigned Mode) { 00430 return (Mode >> 3) & 1; 00431 } 00432 00433 //===--------------------------------------------------------------------===// 00434 // Addressing Mode #5 00435 //===--------------------------------------------------------------------===// 00436 // 00437 // This is used for coprocessor instructions, such as FP load/stores. 00438 // 00439 // addrmode5 := reg +/- imm8*4 00440 // 00441 // The first operand is always a Reg. The second operand encodes the 00442 // operation in bit 8 and the immediate in bits 0-7. 00443 // 00444 // This is also used for FP load/store multiple ops. The second operand 00445 // encodes the writeback mode in bit 8 and the number of registers (or 2 00446 // times the number of registers for DPR ops) in bits 0-7. In addition, 00447 // bits 9-11 encode one of the following two sub-modes: 00448 // 00449 // IA - Increment after 00450 // DB - Decrement before 00451 00452 /// getAM5Opc - This function encodes the addrmode5 opc field. 00453 static inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) { 00454 bool isSub = Opc == sub; 00455 return ((int)isSub << 8) | Offset; 00456 } 00457 static inline unsigned char getAM5Offset(unsigned AM5Opc) { 00458 return AM5Opc & 0xFF; 00459 } 00460 static inline AddrOpc getAM5Op(unsigned AM5Opc) { 00461 return ((AM5Opc >> 8) & 1) ? sub : add; 00462 } 00463 00464 /// getAM5Opc - This function encodes the addrmode5 opc field for FLDM and 00465 /// FSTM instructions. 00466 static inline unsigned getAM5Opc(AMSubMode SubMode, bool WB, 00467 unsigned char Offset) { 00468 assert((SubMode == ia || SubMode == db) && 00469 "Illegal addressing mode 5 sub-mode!"); 00470 return ((int)SubMode << 9) | ((int)WB << 8) | Offset; 00471 } 00472 static inline AMSubMode getAM5SubMode(unsigned AM5Opc) { 00473 return (AMSubMode)((AM5Opc >> 9) & 0x7); 00474 } 00475 static inline bool getAM5WBFlag(unsigned AM5Opc) { 00476 return ((AM5Opc >> 8) & 1); 00477 } 00478 00479 //===--------------------------------------------------------------------===// 00480 // Addressing Mode #6 00481 //===--------------------------------------------------------------------===// 00482 // 00483 // This is used for NEON load / store instructions. 00484 // 00485 // addrmode6 := reg with optional writeback 00486 // 00487 // This is stored in three operands [regaddr, regupdate, opc]. The first is 00488 // the address register. The second register holds the value of a post-access 00489 // increment for writeback or reg0 if no writeback or if the writeback 00490 // increment is the size of the memory access. The third operand encodes 00491 // whether there is writeback to the address register. 00492 00493 static inline unsigned getAM6Opc(bool WB = false) { 00494 return (int)WB; 00495 } 00496 00497 static inline bool getAM6WBFlag(unsigned Mode) { 00498 return Mode & 1; 00499 } 00500 00501 } // end namespace ARM_AM 00502 } // end namespace llvm 00503 00504 #endif 00505