blob: e1ea5964cf674ab0ea6a9f0cdc0601e15694232d [file] [log] [blame]
Jia Liub22310f2012-02-18 12:03:15 +00001//===-- ARMAddressingModes.h - ARM Addressing Modes -------------*- C++ -*-===//
Evan Cheng10043e22007-01-19 07:51:42 +00002//
3// The LLVM Compiler Infrastructure
4//
Chris Lattnerf3ebc3f2007-12-29 20:36:04 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Evan Cheng10043e22007-01-19 07:51:42 +00007//
8//===----------------------------------------------------------------------===//
9//
10// This file contains the ARM addressing mode implementation stuff.
11//
12//===----------------------------------------------------------------------===//
13
Benjamin Kramera7c40ef2014-08-13 16:26:38 +000014#ifndef LLVM_LIB_TARGET_ARM_MCTARGETDESC_ARMADDRESSINGMODES_H
15#define LLVM_LIB_TARGET_ARM_MCTARGETDESC_ARMADDRESSINGMODES_H
Evan Cheng10043e22007-01-19 07:51:42 +000016
Jim Grosbachefc761a2011-09-30 00:50:06 +000017#include "llvm/ADT/APFloat.h"
18#include "llvm/ADT/APInt.h"
JF Bastienc4986ce2018-09-08 03:55:25 +000019#include "llvm/ADT/bit.h"
Craig Toppere55c5562012-02-07 02:50:20 +000020#include "llvm/Support/ErrorHandling.h"
Evan Cheng10043e22007-01-19 07:51:42 +000021#include "llvm/Support/MathExtras.h"
22#include <cassert>
23
24namespace llvm {
Jim Grosbachf24f9d92009-08-11 15:33:49 +000025
Evan Cheng10043e22007-01-19 07:51:42 +000026/// ARM_AM - ARM Addressing Mode Stuff
27namespace ARM_AM {
28 enum ShiftOpc {
29 no_shift = 0,
30 asr,
31 lsl,
32 lsr,
33 ror,
34 rrx
35 };
Jim Grosbachf24f9d92009-08-11 15:33:49 +000036
Evan Cheng10043e22007-01-19 07:51:42 +000037 enum AddrOpc {
Jim Grosbachd3595712011-08-03 23:50:40 +000038 sub = 0,
39 add
Evan Cheng10043e22007-01-19 07:51:42 +000040 };
Jim Grosbachf24f9d92009-08-11 15:33:49 +000041
David Blaikiec70b3922017-10-24 21:29:21 +000042 inline const char *getAddrOpcStr(AddrOpc Op) { return Op == sub ? "-" : ""; }
Johnny Chen8f3004c2010-03-17 17:52:21 +000043
David Blaikiec70b3922017-10-24 21:29:21 +000044 inline const char *getShiftOpcStr(ShiftOpc Op) {
Evan Cheng10043e22007-01-19 07:51:42 +000045 switch (Op) {
Craig Toppere55c5562012-02-07 02:50:20 +000046 default: llvm_unreachable("Unknown shift opc!");
Evan Cheng10043e22007-01-19 07:51:42 +000047 case ARM_AM::asr: return "asr";
48 case ARM_AM::lsl: return "lsl";
49 case ARM_AM::lsr: return "lsr";
50 case ARM_AM::ror: return "ror";
51 case ARM_AM::rrx: return "rrx";
52 }
53 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +000054
David Blaikiec70b3922017-10-24 21:29:21 +000055 inline unsigned getShiftOpcEncoding(ShiftOpc Op) {
Jim Grosbachb7c29622010-10-11 23:16:21 +000056 switch (Op) {
Craig Toppere55c5562012-02-07 02:50:20 +000057 default: llvm_unreachable("Unknown shift opc!");
Jim Grosbachb7c29622010-10-11 23:16:21 +000058 case ARM_AM::asr: return 2;
59 case ARM_AM::lsl: return 0;
60 case ARM_AM::lsr: return 1;
61 case ARM_AM::ror: return 3;
62 }
63 }
64
Evan Cheng10043e22007-01-19 07:51:42 +000065 enum AMSubMode {
66 bad_am_submode = 0,
67 ia,
68 ib,
69 da,
70 db
71 };
72
David Blaikiec70b3922017-10-24 21:29:21 +000073 inline const char *getAMSubModeStr(AMSubMode Mode) {
Evan Cheng10043e22007-01-19 07:51:42 +000074 switch (Mode) {
Craig Toppere55c5562012-02-07 02:50:20 +000075 default: llvm_unreachable("Unknown addressing sub-mode!");
Evan Cheng10043e22007-01-19 07:51:42 +000076 case ARM_AM::ia: return "ia";
77 case ARM_AM::ib: return "ib";
78 case ARM_AM::da: return "da";
79 case ARM_AM::db: return "db";
80 }
81 }
82
Evan Cheng10043e22007-01-19 07:51:42 +000083 /// rotr32 - Rotate a 32-bit unsigned value right by a specified # bits.
84 ///
David Blaikiec70b3922017-10-24 21:29:21 +000085 inline unsigned rotr32(unsigned Val, unsigned Amt) {
Evan Cheng10043e22007-01-19 07:51:42 +000086 assert(Amt < 32 && "Invalid rotate amount");
87 return (Val >> Amt) | (Val << ((32-Amt)&31));
88 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +000089
Evan Cheng10043e22007-01-19 07:51:42 +000090 /// rotl32 - Rotate a 32-bit unsigned value left by a specified # bits.
91 ///
David Blaikiec70b3922017-10-24 21:29:21 +000092 inline unsigned rotl32(unsigned Val, unsigned Amt) {
Evan Cheng10043e22007-01-19 07:51:42 +000093 assert(Amt < 32 && "Invalid rotate amount");
94 return (Val << Amt) | (Val >> ((32-Amt)&31));
95 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +000096
Evan Cheng10043e22007-01-19 07:51:42 +000097 //===--------------------------------------------------------------------===//
98 // Addressing Mode #1: shift_operand with registers
99 //===--------------------------------------------------------------------===//
100 //
101 // This 'addressing mode' is used for arithmetic instructions. It can
102 // represent things like:
103 // reg
104 // reg [asr|lsl|lsr|ror|rrx] reg
105 // reg [asr|lsl|lsr|ror|rrx] imm
106 //
107 // This is stored three operands [rega, regb, opc]. The first is the base
108 // reg, the second is the shift amount (or reg0 if not present or imm). The
109 // third operand encodes the shift opcode and the imm if a reg isn't present.
110 //
David Blaikiec70b3922017-10-24 21:29:21 +0000111 inline unsigned getSORegOpc(ShiftOpc ShOp, unsigned Imm) {
Evan Cheng10043e22007-01-19 07:51:42 +0000112 return ShOp | (Imm << 3);
113 }
David Blaikiec70b3922017-10-24 21:29:21 +0000114 inline unsigned getSORegOffset(unsigned Op) { return Op >> 3; }
115 inline ShiftOpc getSORegShOp(unsigned Op) { return (ShiftOpc)(Op & 7); }
Evan Cheng10043e22007-01-19 07:51:42 +0000116
117 /// getSOImmValImm - Given an encoded imm field for the reg/imm form, return
118 /// the 8-bit imm value.
David Blaikiec70b3922017-10-24 21:29:21 +0000119 inline unsigned getSOImmValImm(unsigned Imm) { return Imm & 0xFF; }
Bob Wilson57178e82009-03-30 18:49:37 +0000120 /// getSOImmValRot - Given an encoded imm field for the reg/imm form, return
Evan Cheng10043e22007-01-19 07:51:42 +0000121 /// the rotate amount.
David Blaikiec70b3922017-10-24 21:29:21 +0000122 inline unsigned getSOImmValRot(unsigned Imm) { return (Imm >> 8) * 2; }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000123
Evan Cheng10043e22007-01-19 07:51:42 +0000124 /// getSOImmValRotate - Try to handle Imm with an immediate shifter operand,
125 /// computing the rotate amount to use. If this immediate value cannot be
126 /// handled with a single shifter-op, determine a good rotate amount that will
127 /// take a maximal chunk of bits out of the immediate.
David Blaikiec70b3922017-10-24 21:29:21 +0000128 inline unsigned getSOImmValRotate(unsigned Imm) {
Evan Cheng10043e22007-01-19 07:51:42 +0000129 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
130 // of zero.
131 if ((Imm & ~255U) == 0) return 0;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000132
Evan Cheng10043e22007-01-19 07:51:42 +0000133 // Use CTZ to compute the rotate amount.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000134 unsigned TZ = countTrailingZeros(Imm);
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000135
Evan Cheng10043e22007-01-19 07:51:42 +0000136 // Rotate amount must be even. Something like 0x200 must be rotated 8 bits,
137 // not 9.
138 unsigned RotAmt = TZ & ~1;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000139
Evan Cheng10043e22007-01-19 07:51:42 +0000140 // If we can handle this spread, return it.
141 if ((rotr32(Imm, RotAmt) & ~255U) == 0)
142 return (32-RotAmt)&31; // HW rotates right, not left.
143
Johnny Chen44d7d182010-04-13 20:35:16 +0000144 // For values like 0xF000000F, we should ignore the low 6 bits, then
Evan Cheng10043e22007-01-19 07:51:42 +0000145 // retry the hunt.
Johnny Chen44d7d182010-04-13 20:35:16 +0000146 if (Imm & 63U) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000147 unsigned TZ2 = countTrailingZeros(Imm & ~63U);
Bob Wilsonaf7674c2010-04-13 02:11:48 +0000148 unsigned RotAmt2 = TZ2 & ~1;
149 if ((rotr32(Imm, RotAmt2) & ~255U) == 0)
150 return (32-RotAmt2)&31; // HW rotates right, not left.
Evan Cheng10043e22007-01-19 07:51:42 +0000151 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000152
Evan Cheng10043e22007-01-19 07:51:42 +0000153 // Otherwise, we have no way to cover this span of bits with a single
154 // shifter_op immediate. Return a chunk of bits that will be useful to
155 // handle.
156 return (32-RotAmt)&31; // HW rotates right, not left.
157 }
158
159 /// getSOImmVal - Given a 32-bit immediate, if it is something that can fit
160 /// into an shifter_operand immediate operand, return the 12-bit encoding for
161 /// it. If not, return -1.
David Blaikiec70b3922017-10-24 21:29:21 +0000162 inline int getSOImmVal(unsigned Arg) {
Evan Cheng10043e22007-01-19 07:51:42 +0000163 // 8-bit (or less) immediates are trivially shifter_operands with a rotate
164 // of zero.
165 if ((Arg & ~255U) == 0) return Arg;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000166
Johnny Chen6e81f672010-03-17 18:32:39 +0000167 unsigned RotAmt = getSOImmValRotate(Arg);
Evan Cheng10043e22007-01-19 07:51:42 +0000168
169 // If this cannot be handled with a single shifter_op, bail out.
170 if (rotr32(~255U, RotAmt) & Arg)
171 return -1;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000172
Evan Cheng10043e22007-01-19 07:51:42 +0000173 // Encode this correctly.
174 return rotl32(Arg, RotAmt) | ((RotAmt>>1) << 8);
175 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000176
Evan Cheng10043e22007-01-19 07:51:42 +0000177 /// isSOImmTwoPartVal - Return true if the specified value can be obtained by
178 /// or'ing together two SOImmVal's.
David Blaikiec70b3922017-10-24 21:29:21 +0000179 inline bool isSOImmTwoPartVal(unsigned V) {
Evan Cheng10043e22007-01-19 07:51:42 +0000180 // If this can be handled with a single shifter_op, bail out.
181 V = rotr32(~255U, getSOImmValRotate(V)) & V;
182 if (V == 0)
183 return false;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000184
Evan Cheng10043e22007-01-19 07:51:42 +0000185 // If this can be handled with two shifter_op's, accept.
186 V = rotr32(~255U, getSOImmValRotate(V)) & V;
187 return V == 0;
188 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000189
Evan Cheng10043e22007-01-19 07:51:42 +0000190 /// getSOImmTwoPartFirst - If V is a value that satisfies isSOImmTwoPartVal,
191 /// return the first chunk of it.
David Blaikiec70b3922017-10-24 21:29:21 +0000192 inline unsigned getSOImmTwoPartFirst(unsigned V) {
Evan Cheng10043e22007-01-19 07:51:42 +0000193 return rotr32(255U, getSOImmValRotate(V)) & V;
194 }
195
196 /// getSOImmTwoPartSecond - If V is a value that satisfies isSOImmTwoPartVal,
197 /// return the second chunk of it.
David Blaikiec70b3922017-10-24 21:29:21 +0000198 inline unsigned getSOImmTwoPartSecond(unsigned V) {
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000199 // Mask out the first hunk.
Evan Cheng10043e22007-01-19 07:51:42 +0000200 V = rotr32(~255U, getSOImmValRotate(V)) & V;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000201
Evan Cheng10043e22007-01-19 07:51:42 +0000202 // Take what's left.
203 assert(V == (rotr32(255U, getSOImmValRotate(V)) & V));
204 return V;
205 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000206
Evan Cheng10043e22007-01-19 07:51:42 +0000207 /// getThumbImmValShift - Try to handle Imm with a 8-bit immediate followed
208 /// by a left shift. Returns the shift amount to use.
David Blaikiec70b3922017-10-24 21:29:21 +0000209 inline unsigned getThumbImmValShift(unsigned Imm) {
Evan Cheng10043e22007-01-19 07:51:42 +0000210 // 8-bit (or less) immediates are trivially immediate operand with a shift
211 // of zero.
212 if ((Imm & ~255U) == 0) return 0;
213
214 // Use CTZ to compute the shift amount.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000215 return countTrailingZeros(Imm);
Evan Cheng10043e22007-01-19 07:51:42 +0000216 }
217
218 /// isThumbImmShiftedVal - Return true if the specified value can be obtained
219 /// by left shifting a 8-bit immediate.
David Blaikiec70b3922017-10-24 21:29:21 +0000220 inline bool isThumbImmShiftedVal(unsigned V) {
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000221 // If this can be handled with
Evan Cheng10043e22007-01-19 07:51:42 +0000222 V = (~255U << getThumbImmValShift(V)) & V;
223 return V == 0;
224 }
225
Evan Cheng431cf562009-06-23 17:48:47 +0000226 /// getThumbImm16ValShift - Try to handle Imm with a 16-bit immediate followed
227 /// by a left shift. Returns the shift amount to use.
David Blaikiec70b3922017-10-24 21:29:21 +0000228 inline unsigned getThumbImm16ValShift(unsigned Imm) {
Evan Cheng431cf562009-06-23 17:48:47 +0000229 // 16-bit (or less) immediates are trivially immediate operand with a shift
230 // of zero.
231 if ((Imm & ~65535U) == 0) return 0;
232
233 // Use CTZ to compute the shift amount.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000234 return countTrailingZeros(Imm);
Evan Cheng431cf562009-06-23 17:48:47 +0000235 }
236
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000237 /// isThumbImm16ShiftedVal - Return true if the specified value can be
Evan Cheng431cf562009-06-23 17:48:47 +0000238 /// obtained by left shifting a 16-bit immediate.
David Blaikiec70b3922017-10-24 21:29:21 +0000239 inline bool isThumbImm16ShiftedVal(unsigned V) {
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000240 // If this can be handled with
Evan Cheng431cf562009-06-23 17:48:47 +0000241 V = (~65535U << getThumbImm16ValShift(V)) & V;
242 return V == 0;
243 }
244
Evan Cheng10043e22007-01-19 07:51:42 +0000245 /// getThumbImmNonShiftedVal - If V is a value that satisfies
246 /// isThumbImmShiftedVal, return the non-shiftd value.
David Blaikiec70b3922017-10-24 21:29:21 +0000247 inline unsigned getThumbImmNonShiftedVal(unsigned V) {
Evan Cheng10043e22007-01-19 07:51:42 +0000248 return V >> getThumbImmValShift(V);
249 }
250
Evan Cheng780748d2009-07-28 05:48:47 +0000251
Evan Cheng431cf562009-06-23 17:48:47 +0000252 /// getT2SOImmValSplat - Return the 12-bit encoded representation
253 /// if the specified value can be obtained by splatting the low 8 bits
254 /// into every other byte or every byte of a 32-bit value. i.e.,
255 /// 00000000 00000000 00000000 abcdefgh control = 0
256 /// 00000000 abcdefgh 00000000 abcdefgh control = 1
257 /// abcdefgh 00000000 abcdefgh 00000000 control = 2
258 /// abcdefgh abcdefgh abcdefgh abcdefgh control = 3
259 /// Return -1 if none of the above apply.
260 /// See ARM Reference Manual A6.3.2.
David Blaikiec70b3922017-10-24 21:29:21 +0000261 inline int getT2SOImmValSplatVal(unsigned V) {
Evan Cheng431cf562009-06-23 17:48:47 +0000262 unsigned u, Vs, Imm;
263 // control = 0
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000264 if ((V & 0xffffff00) == 0)
Evan Cheng431cf562009-06-23 17:48:47 +0000265 return V;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000266
Evan Cheng431cf562009-06-23 17:48:47 +0000267 // If the value is zeroes in the first byte, just shift those off
268 Vs = ((V & 0xff) == 0) ? V >> 8 : V;
269 // Any passing value only has 8 bits of payload, splatted across the word
270 Imm = Vs & 0xff;
271 // Likewise, any passing values have the payload splatted into the 3rd byte
272 u = Imm | (Imm << 16);
273
274 // control = 1 or 2
275 if (Vs == u)
276 return (((Vs == V) ? 1 : 2) << 8) | Imm;
277
278 // control = 3
279 if (Vs == (u | (u << 8)))
280 return (3 << 8) | Imm;
281
282 return -1;
283 }
284
Evan Cheng780748d2009-07-28 05:48:47 +0000285 /// getT2SOImmValRotateVal - Return the 12-bit encoded representation if the
Evan Cheng431cf562009-06-23 17:48:47 +0000286 /// specified value is a rotated 8-bit value. Return -1 if no rotation
287 /// encoding is possible.
288 /// See ARM Reference Manual A6.3.2.
David Blaikiec70b3922017-10-24 21:29:21 +0000289 inline int getT2SOImmValRotateVal(unsigned V) {
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000290 unsigned RotAmt = countLeadingZeros(V);
Evan Cheng431cf562009-06-23 17:48:47 +0000291 if (RotAmt >= 24)
292 return -1;
293
294 // If 'Arg' can be handled with a single shifter_op return the value.
295 if ((rotr32(0xff000000U, RotAmt) & V) == V)
296 return (rotr32(V, 24 - RotAmt) & 0x7f) | ((RotAmt + 8) << 7);
297
298 return -1;
299 }
300
301 /// getT2SOImmVal - Given a 32-bit immediate, if it is something that can fit
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000302 /// into a Thumb-2 shifter_operand immediate operand, return the 12-bit
Evan Cheng431cf562009-06-23 17:48:47 +0000303 /// encoding for it. If not, return -1.
304 /// See ARM Reference Manual A6.3.2.
David Blaikiec70b3922017-10-24 21:29:21 +0000305 inline int getT2SOImmVal(unsigned Arg) {
Evan Cheng431cf562009-06-23 17:48:47 +0000306 // If 'Arg' is an 8-bit splat, then get the encoded value.
Evan Cheng780748d2009-07-28 05:48:47 +0000307 int Splat = getT2SOImmValSplatVal(Arg);
Evan Cheng431cf562009-06-23 17:48:47 +0000308 if (Splat != -1)
309 return Splat;
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000310
Evan Cheng431cf562009-06-23 17:48:47 +0000311 // If 'Arg' can be handled with a single shifter_op return the value.
Evan Cheng780748d2009-07-28 05:48:47 +0000312 int Rot = getT2SOImmValRotateVal(Arg);
Evan Cheng431cf562009-06-23 17:48:47 +0000313 if (Rot != -1)
314 return Rot;
315
316 return -1;
317 }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000318
David Blaikiec70b3922017-10-24 21:29:21 +0000319 inline unsigned getT2SOImmValRotate(unsigned V) {
Jim Grosbacha93ca3c2009-10-21 20:44:34 +0000320 if ((V & ~255U) == 0) return 0;
321 // Use CTZ to compute the rotate amount.
Michael J. Spencerdf1ecbd72013-05-24 22:23:49 +0000322 unsigned RotAmt = countTrailingZeros(V);
Jim Grosbacha93ca3c2009-10-21 20:44:34 +0000323 return (32 - RotAmt) & 31;
324 }
325
David Blaikiec70b3922017-10-24 21:29:21 +0000326 inline bool isT2SOImmTwoPartVal(unsigned Imm) {
Jim Grosbacha93ca3c2009-10-21 20:44:34 +0000327 unsigned V = Imm;
328 // Passing values can be any combination of splat values and shifter
329 // values. If this can be handled with a single shifter or splat, bail
330 // out. Those should be handled directly, not with a two-part val.
331 if (getT2SOImmValSplatVal(V) != -1)
332 return false;
333 V = rotr32 (~255U, getT2SOImmValRotate(V)) & V;
334 if (V == 0)
335 return false;
336
337 // If this can be handled as an immediate, accept.
338 if (getT2SOImmVal(V) != -1) return true;
339
340 // Likewise, try masking out a splat value first.
341 V = Imm;
342 if (getT2SOImmValSplatVal(V & 0xff00ff00U) != -1)
343 V &= ~0xff00ff00U;
344 else if (getT2SOImmValSplatVal(V & 0x00ff00ffU) != -1)
345 V &= ~0x00ff00ffU;
346 // If what's left can be handled as an immediate, accept.
347 if (getT2SOImmVal(V) != -1) return true;
348
349 // Otherwise, do not accept.
350 return false;
351 }
352
David Blaikiec70b3922017-10-24 21:29:21 +0000353 inline unsigned getT2SOImmTwoPartFirst(unsigned Imm) {
Jim Grosbacha93ca3c2009-10-21 20:44:34 +0000354 assert (isT2SOImmTwoPartVal(Imm) &&
355 "Immedate cannot be encoded as two part immediate!");
356 // Try a shifter operand as one part
357 unsigned V = rotr32 (~255, getT2SOImmValRotate(Imm)) & Imm;
358 // If the rest is encodable as an immediate, then return it.
359 if (getT2SOImmVal(V) != -1) return V;
360
361 // Try masking out a splat value first.
362 if (getT2SOImmValSplatVal(Imm & 0xff00ff00U) != -1)
363 return Imm & 0xff00ff00U;
364
365 // The other splat is all that's left as an option.
366 assert (getT2SOImmValSplatVal(Imm & 0x00ff00ffU) != -1);
367 return Imm & 0x00ff00ffU;
368 }
369
David Blaikiec70b3922017-10-24 21:29:21 +0000370 inline unsigned getT2SOImmTwoPartSecond(unsigned Imm) {
Jim Grosbacha93ca3c2009-10-21 20:44:34 +0000371 // Mask out the first hunk
372 Imm ^= getT2SOImmTwoPartFirst(Imm);
373 // Return what's left
374 assert (getT2SOImmVal(Imm) != -1 &&
375 "Unable to encode second part of T2 two part SO immediate");
376 return Imm;
377 }
378
Evan Cheng431cf562009-06-23 17:48:47 +0000379
Evan Cheng10043e22007-01-19 07:51:42 +0000380 //===--------------------------------------------------------------------===//
381 // Addressing Mode #2
382 //===--------------------------------------------------------------------===//
383 //
384 // This is used for most simple load/store instructions.
385 //
386 // addrmode2 := reg +/- reg shop imm
387 // addrmode2 := reg +/- imm12
388 //
389 // The first operand is always a Reg. The second operand is a reg if in
390 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
Bruno Cardoso Lopesab830502011-03-31 23:26:08 +0000391 // in bit 12, the immediate in bits 0-11, and the shift op in 13-15. The
Bruno Cardoso Lopesbda36322011-04-04 17:18:19 +0000392 // fourth operand 16-17 encodes the index mode.
Evan Cheng10043e22007-01-19 07:51:42 +0000393 //
394 // If this addressing mode is a frame index (before prolog/epilog insertion
395 // and code rewriting), this operand will have the form: FI#, reg0, <offs>
396 // with no shift amount for the frame offset.
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000397 //
David Blaikiec70b3922017-10-24 21:29:21 +0000398 inline unsigned getAM2Opc(AddrOpc Opc, unsigned Imm12, ShiftOpc SO,
399 unsigned IdxMode = 0) {
Evan Cheng10043e22007-01-19 07:51:42 +0000400 assert(Imm12 < (1 << 12) && "Imm too large!");
401 bool isSub = Opc == sub;
Bruno Cardoso Lopesab830502011-03-31 23:26:08 +0000402 return Imm12 | ((int)isSub << 12) | (SO << 13) | (IdxMode << 16) ;
Evan Cheng10043e22007-01-19 07:51:42 +0000403 }
David Blaikiec70b3922017-10-24 21:29:21 +0000404 inline unsigned getAM2Offset(unsigned AM2Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000405 return AM2Opc & ((1 << 12)-1);
406 }
David Blaikiec70b3922017-10-24 21:29:21 +0000407 inline AddrOpc getAM2Op(unsigned AM2Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000408 return ((AM2Opc >> 12) & 1) ? sub : add;
409 }
David Blaikiec70b3922017-10-24 21:29:21 +0000410 inline ShiftOpc getAM2ShiftOpc(unsigned AM2Opc) {
Bruno Cardoso Lopesab830502011-03-31 23:26:08 +0000411 return (ShiftOpc)((AM2Opc >> 13) & 7);
412 }
David Blaikiec70b3922017-10-24 21:29:21 +0000413 inline unsigned getAM2IdxMode(unsigned AM2Opc) { return (AM2Opc >> 16); }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000414
Evan Cheng10043e22007-01-19 07:51:42 +0000415 //===--------------------------------------------------------------------===//
416 // Addressing Mode #3
417 //===--------------------------------------------------------------------===//
418 //
419 // This is used for sign-extending loads, and load/store-pair instructions.
420 //
421 // addrmode3 := reg +/- reg
422 // addrmode3 := reg +/- imm8
423 //
424 // The first operand is always a Reg. The second operand is a reg if in
425 // reg/reg form, otherwise it's reg#0. The third field encodes the operation
Bruno Cardoso Lopesbda36322011-04-04 17:18:19 +0000426 // in bit 8, the immediate in bits 0-7. The fourth operand 9-10 encodes the
427 // index mode.
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000428
Evan Cheng10043e22007-01-19 07:51:42 +0000429 /// getAM3Opc - This function encodes the addrmode3 opc field.
David Blaikiec70b3922017-10-24 21:29:21 +0000430 inline unsigned getAM3Opc(AddrOpc Opc, unsigned char Offset,
431 unsigned IdxMode = 0) {
Evan Cheng10043e22007-01-19 07:51:42 +0000432 bool isSub = Opc == sub;
Bruno Cardoso Lopesbda36322011-04-04 17:18:19 +0000433 return ((int)isSub << 8) | Offset | (IdxMode << 9);
Evan Cheng10043e22007-01-19 07:51:42 +0000434 }
David Blaikiec70b3922017-10-24 21:29:21 +0000435 inline unsigned char getAM3Offset(unsigned AM3Opc) { return AM3Opc & 0xFF; }
436 inline AddrOpc getAM3Op(unsigned AM3Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000437 return ((AM3Opc >> 8) & 1) ? sub : add;
438 }
David Blaikiec70b3922017-10-24 21:29:21 +0000439 inline unsigned getAM3IdxMode(unsigned AM3Opc) { return (AM3Opc >> 9); }
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000440
Evan Cheng10043e22007-01-19 07:51:42 +0000441 //===--------------------------------------------------------------------===//
442 // Addressing Mode #4
443 //===--------------------------------------------------------------------===//
444 //
445 // This is used for load / store multiple instructions.
446 //
447 // addrmode4 := reg, <mode>
448 //
449 // The four modes are:
450 // IA - Increment after
451 // IB - Increment before
452 // DA - Decrement after
453 // DB - Decrement before
Bob Wilson13ce07f2010-08-27 23:18:17 +0000454 // For VFP instructions, only the IA and DB modes are valid.
Evan Cheng10043e22007-01-19 07:51:42 +0000455
David Blaikiec70b3922017-10-24 21:29:21 +0000456 inline AMSubMode getAM4SubMode(unsigned Mode) {
Evan Cheng10043e22007-01-19 07:51:42 +0000457 return (AMSubMode)(Mode & 0x7);
458 }
459
David Blaikiec70b3922017-10-24 21:29:21 +0000460 inline unsigned getAM4ModeImm(AMSubMode SubMode) { return (int)SubMode; }
Evan Cheng10043e22007-01-19 07:51:42 +0000461
462 //===--------------------------------------------------------------------===//
463 // Addressing Mode #5
464 //===--------------------------------------------------------------------===//
465 //
466 // This is used for coprocessor instructions, such as FP load/stores.
467 //
468 // addrmode5 := reg +/- imm8*4
469 //
Bob Wilsonbbbf8052009-07-01 21:22:45 +0000470 // The first operand is always a Reg. The second operand encodes the
Oliver Stannard65b85382016-01-25 10:26:26 +0000471 // operation (add or subtract) in bit 8 and the immediate in bits 0-7.
Jim Grosbachf24f9d92009-08-11 15:33:49 +0000472
Evan Cheng10043e22007-01-19 07:51:42 +0000473 /// getAM5Opc - This function encodes the addrmode5 opc field.
David Blaikiec70b3922017-10-24 21:29:21 +0000474 inline unsigned getAM5Opc(AddrOpc Opc, unsigned char Offset) {
Evan Cheng10043e22007-01-19 07:51:42 +0000475 bool isSub = Opc == sub;
476 return ((int)isSub << 8) | Offset;
477 }
David Blaikiec70b3922017-10-24 21:29:21 +0000478 inline unsigned char getAM5Offset(unsigned AM5Opc) { return AM5Opc & 0xFF; }
479 inline AddrOpc getAM5Op(unsigned AM5Opc) {
Evan Cheng10043e22007-01-19 07:51:42 +0000480 return ((AM5Opc >> 8) & 1) ? sub : add;
481 }
482
Bob Wilsondeb35af2009-07-01 23:16:05 +0000483 //===--------------------------------------------------------------------===//
Oliver Stannard65b85382016-01-25 10:26:26 +0000484 // Addressing Mode #5 FP16
485 //===--------------------------------------------------------------------===//
486 //
487 // This is used for coprocessor instructions, such as 16-bit FP load/stores.
488 //
489 // addrmode5fp16 := reg +/- imm8*2
490 //
491 // The first operand is always a Reg. The second operand encodes the
492 // operation (add or subtract) in bit 8 and the immediate in bits 0-7.
493
494 /// getAM5FP16Opc - This function encodes the addrmode5fp16 opc field.
David Blaikiec70b3922017-10-24 21:29:21 +0000495 inline unsigned getAM5FP16Opc(AddrOpc Opc, unsigned char Offset) {
Oliver Stannard65b85382016-01-25 10:26:26 +0000496 bool isSub = Opc == sub;
497 return ((int)isSub << 8) | Offset;
498 }
David Blaikiec70b3922017-10-24 21:29:21 +0000499 inline unsigned char getAM5FP16Offset(unsigned AM5Opc) {
Oliver Stannard65b85382016-01-25 10:26:26 +0000500 return AM5Opc & 0xFF;
501 }
David Blaikiec70b3922017-10-24 21:29:21 +0000502 inline AddrOpc getAM5FP16Op(unsigned AM5Opc) {
Oliver Stannard65b85382016-01-25 10:26:26 +0000503 return ((AM5Opc >> 8) & 1) ? sub : add;
504 }
505
506 //===--------------------------------------------------------------------===//
Bob Wilsondeb35af2009-07-01 23:16:05 +0000507 // Addressing Mode #6
508 //===--------------------------------------------------------------------===//
509 //
510 // This is used for NEON load / store instructions.
511 //
Bob Wilsonae08a732010-03-20 22:13:40 +0000512 // addrmode6 := reg with optional alignment
Bob Wilsondeb35af2009-07-01 23:16:05 +0000513 //
Bob Wilsonae08a732010-03-20 22:13:40 +0000514 // This is stored in two operands [regaddr, align]. The first is the
515 // address register. The second operand is the value of the alignment
Bob Wilson0b9aafd2010-07-14 23:54:43 +0000516 // specifier in bytes or zero if no explicit alignment.
517 // Valid alignments depend on the specific instruction.
Bob Wilsondeb35af2009-07-01 23:16:05 +0000518
Bob Wilsonc1c6f472010-07-13 04:44:34 +0000519 //===--------------------------------------------------------------------===//
520 // NEON Modified Immediates
521 //===--------------------------------------------------------------------===//
522 //
523 // Several NEON instructions (e.g., VMOV) take a "modified immediate"
524 // vector operand, where a small immediate encoded in the instruction
525 // specifies a full NEON vector value. These modified immediates are
526 // represented here as encoded integers. The low 8 bits hold the immediate
527 // value; bit 12 holds the "Op" field of the instruction, and bits 11-8 hold
528 // the "Cmode" field of the instruction. The interfaces below treat the
529 // Op and Cmode values as a single 5-bit value.
530
David Blaikiec70b3922017-10-24 21:29:21 +0000531 inline unsigned createNEONModImm(unsigned OpCmode, unsigned Val) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +0000532 return (OpCmode << 8) | Val;
533 }
David Blaikiec70b3922017-10-24 21:29:21 +0000534 inline unsigned getNEONModImmOpCmode(unsigned ModImm) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +0000535 return (ModImm >> 8) & 0x1f;
536 }
David Blaikiec70b3922017-10-24 21:29:21 +0000537 inline unsigned getNEONModImmVal(unsigned ModImm) { return ModImm & 0xff; }
Bob Wilsonc1c6f472010-07-13 04:44:34 +0000538
539 /// decodeNEONModImm - Decode a NEON modified immediate value into the
540 /// element value and the element size in bits. (If the element size is
541 /// smaller than the vector, it is splatted into all the elements.)
David Blaikiec70b3922017-10-24 21:29:21 +0000542 inline uint64_t decodeNEONModImm(unsigned ModImm, unsigned &EltBits) {
Bob Wilsonc1c6f472010-07-13 04:44:34 +0000543 unsigned OpCmode = getNEONModImmOpCmode(ModImm);
544 unsigned Imm8 = getNEONModImmVal(ModImm);
545 uint64_t Val = 0;
546
547 if (OpCmode == 0xe) {
548 // 8-bit vector elements
549 Val = Imm8;
550 EltBits = 8;
551 } else if ((OpCmode & 0xc) == 0x8) {
552 // 16-bit vector elements
553 unsigned ByteNum = (OpCmode & 0x6) >> 1;
554 Val = Imm8 << (8 * ByteNum);
555 EltBits = 16;
556 } else if ((OpCmode & 0x8) == 0) {
557 // 32-bit vector elements, zero with one byte set
558 unsigned ByteNum = (OpCmode & 0x6) >> 1;
559 Val = Imm8 << (8 * ByteNum);
560 EltBits = 32;
561 } else if ((OpCmode & 0xe) == 0xc) {
562 // 32-bit vector elements, one byte with low bits set
563 unsigned ByteNum = 1 + (OpCmode & 0x1);
564 Val = (Imm8 << (8 * ByteNum)) | (0xffff >> (8 * (2 - ByteNum)));
565 EltBits = 32;
566 } else if (OpCmode == 0x1e) {
567 // 64-bit vector elements
568 for (unsigned ByteNum = 0; ByteNum < 8; ++ByteNum) {
569 if ((ModImm >> ByteNum) & 1)
570 Val |= (uint64_t)0xff << (8 * ByteNum);
571 }
572 EltBits = 64;
573 } else {
Craig Toppere55c5562012-02-07 02:50:20 +0000574 llvm_unreachable("Unsupported NEON immediate");
Bob Wilsonc1c6f472010-07-13 04:44:34 +0000575 }
576 return Val;
577 }
578
Renato Golinf5dd1da2014-09-25 11:31:24 +0000579 // Generic validation for single-byte immediate (0X00, 00X0, etc).
David Blaikiec70b3922017-10-24 21:29:21 +0000580 inline bool isNEONBytesplat(unsigned Value, unsigned Size) {
Renato Golinf5dd1da2014-09-25 11:31:24 +0000581 assert(Size >= 1 && Size <= 4 && "Invalid size");
582 unsigned count = 0;
583 for (unsigned i = 0; i < Size; ++i) {
584 if (Value & 0xff) count++;
585 Value >>= 8;
586 }
587 return count == 1;
588 }
589
590 /// Checks if Value is a correct immediate for instructions like VBIC/VORR.
David Blaikiec70b3922017-10-24 21:29:21 +0000591 inline bool isNEONi16splat(unsigned Value) {
Renato Golinf5dd1da2014-09-25 11:31:24 +0000592 if (Value > 0xffff)
593 return false;
594 // i16 value with set bits only in one byte X0 or 0X.
595 return Value == 0 || isNEONBytesplat(Value, 2);
596 }
597
598 // Encode NEON 16 bits Splat immediate for instructions like VBIC/VORR
David Blaikiec70b3922017-10-24 21:29:21 +0000599 inline unsigned encodeNEONi16splat(unsigned Value) {
Renato Golinf5dd1da2014-09-25 11:31:24 +0000600 assert(isNEONi16splat(Value) && "Invalid NEON splat value");
601 if (Value >= 0x100)
602 Value = (Value >> 8) | 0xa00;
603 else
604 Value |= 0x800;
605 return Value;
606 }
607
608 /// Checks if Value is a correct immediate for instructions like VBIC/VORR.
David Blaikiec70b3922017-10-24 21:29:21 +0000609 inline bool isNEONi32splat(unsigned Value) {
Renato Golinf5dd1da2014-09-25 11:31:24 +0000610 // i32 value with set bits only in one byte X000, 0X00, 00X0, or 000X.
611 return Value == 0 || isNEONBytesplat(Value, 4);
612 }
613
614 /// Encode NEON 32 bits Splat immediate for instructions like VBIC/VORR.
David Blaikiec70b3922017-10-24 21:29:21 +0000615 inline unsigned encodeNEONi32splat(unsigned Value) {
Renato Golinf5dd1da2014-09-25 11:31:24 +0000616 assert(isNEONi32splat(Value) && "Invalid NEON splat value");
617 if (Value >= 0x100 && Value <= 0xff00)
618 Value = (Value >> 8) | 0x200;
619 else if (Value > 0xffff && Value <= 0xff0000)
620 Value = (Value >> 16) | 0x400;
621 else if (Value > 0xffffff)
622 Value = (Value >> 24) | 0x600;
623 return Value;
624 }
625
Jim Grosbachefc761a2011-09-30 00:50:06 +0000626 //===--------------------------------------------------------------------===//
627 // Floating-point Immediates
628 //
David Blaikiec70b3922017-10-24 21:29:21 +0000629 inline float getFPImmFloat(unsigned Imm) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000630 // We expect an 8-bit binary encoding of a floating-point number here.
Jim Grosbachefc761a2011-09-30 00:50:06 +0000631
632 uint8_t Sign = (Imm >> 7) & 0x1;
633 uint8_t Exp = (Imm >> 4) & 0x7;
634 uint8_t Mantissa = Imm & 0xf;
635
JF Bastienc4986ce2018-09-08 03:55:25 +0000636 // 8-bit FP IEEE Float Encoding
Jim Grosbachefc761a2011-09-30 00:50:06 +0000637 // abcd efgh aBbbbbbc defgh000 00000000 00000000
638 //
639 // where B = NOT(b);
JF Bastienc4986ce2018-09-08 03:55:25 +0000640 uint32_t I = 0;
641 I |= Sign << 31;
642 I |= ((Exp & 0x4) != 0 ? 0 : 1) << 30;
643 I |= ((Exp & 0x4) != 0 ? 0x1f : 0) << 25;
644 I |= (Exp & 0x3) << 23;
645 I |= Mantissa << 19;
JF Bastienf03058e2018-09-08 04:07:41 +0000646 return bit_cast<float>(I);
Jim Grosbachefc761a2011-09-30 00:50:06 +0000647 }
648
Oliver Stannard65b85382016-01-25 10:26:26 +0000649 /// getFP16Imm - Return an 8-bit floating-point version of the 16-bit
650 /// floating-point value. If the value cannot be represented as an 8-bit
651 /// floating-point value, then return -1.
David Blaikiec70b3922017-10-24 21:29:21 +0000652 inline int getFP16Imm(const APInt &Imm) {
Oliver Stannard65b85382016-01-25 10:26:26 +0000653 uint32_t Sign = Imm.lshr(15).getZExtValue() & 1;
654 int32_t Exp = (Imm.lshr(10).getSExtValue() & 0x1f) - 15; // -14 to 15
655 int64_t Mantissa = Imm.getZExtValue() & 0x3ff; // 10 bits
656
657 // We can handle 4 bits of mantissa.
658 // mantissa = (16+UInt(e:f:g:h))/16.
659 if (Mantissa & 0x3f)
660 return -1;
661 Mantissa >>= 6;
662
663 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
664 if (Exp < -3 || Exp > 4)
665 return -1;
666 Exp = ((Exp+3) & 0x7) ^ 4;
667
668 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
669 }
670
David Blaikiec70b3922017-10-24 21:29:21 +0000671 inline int getFP16Imm(const APFloat &FPImm) {
Oliver Stannard65b85382016-01-25 10:26:26 +0000672 return getFP16Imm(FPImm.bitcastToAPInt());
673 }
674
Jim Grosbachefc761a2011-09-30 00:50:06 +0000675 /// getFP32Imm - Return an 8-bit floating-point version of the 32-bit
676 /// floating-point value. If the value cannot be represented as an 8-bit
677 /// floating-point value, then return -1.
David Blaikiec70b3922017-10-24 21:29:21 +0000678 inline int getFP32Imm(const APInt &Imm) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000679 uint32_t Sign = Imm.lshr(31).getZExtValue() & 1;
680 int32_t Exp = (Imm.lshr(23).getSExtValue() & 0xff) - 127; // -126 to 127
681 int64_t Mantissa = Imm.getZExtValue() & 0x7fffff; // 23 bits
682
683 // We can handle 4 bits of mantissa.
684 // mantissa = (16+UInt(e:f:g:h))/16.
685 if (Mantissa & 0x7ffff)
686 return -1;
687 Mantissa >>= 19;
688 if ((Mantissa & 0xf) != Mantissa)
689 return -1;
690
691 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
692 if (Exp < -3 || Exp > 4)
693 return -1;
694 Exp = ((Exp+3) & 0x7) ^ 4;
695
696 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
697 }
698
David Blaikiec70b3922017-10-24 21:29:21 +0000699 inline int getFP32Imm(const APFloat &FPImm) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000700 return getFP32Imm(FPImm.bitcastToAPInt());
701 }
702
703 /// getFP64Imm - Return an 8-bit floating-point version of the 64-bit
704 /// floating-point value. If the value cannot be represented as an 8-bit
705 /// floating-point value, then return -1.
David Blaikiec70b3922017-10-24 21:29:21 +0000706 inline int getFP64Imm(const APInt &Imm) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000707 uint64_t Sign = Imm.lshr(63).getZExtValue() & 1;
Jim Grosbach69e6f902011-10-03 23:03:26 +0000708 int64_t Exp = (Imm.lshr(52).getSExtValue() & 0x7ff) - 1023; // -1022 to 1023
Jim Grosbachefc761a2011-09-30 00:50:06 +0000709 uint64_t Mantissa = Imm.getZExtValue() & 0xfffffffffffffULL;
710
711 // We can handle 4 bits of mantissa.
712 // mantissa = (16+UInt(e:f:g:h))/16.
713 if (Mantissa & 0xffffffffffffULL)
714 return -1;
715 Mantissa >>= 48;
716 if ((Mantissa & 0xf) != Mantissa)
717 return -1;
718
719 // We can handle 3 bits of exponent: exp == UInt(NOT(b):c:d)-3
720 if (Exp < -3 || Exp > 4)
721 return -1;
722 Exp = ((Exp+3) & 0x7) ^ 4;
723
724 return ((int)Sign << 7) | (Exp << 4) | Mantissa;
725 }
726
David Blaikiec70b3922017-10-24 21:29:21 +0000727 inline int getFP64Imm(const APFloat &FPImm) {
Jim Grosbachefc761a2011-09-30 00:50:06 +0000728 return getFP64Imm(FPImm.bitcastToAPInt());
729 }
730
Evan Cheng10043e22007-01-19 07:51:42 +0000731} // end namespace ARM_AM
732} // end namespace llvm
733
734#endif
735