[AArch64] Add support for NEON scalar floating-point compare instructions.
[oota-llvm.git] / lib / Target / AArch64 / AArch64InstrInfo.td
1 //===----- AArch64InstrInfo.td - AArch64 Instruction Info ----*- tablegen -*-=//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the AArch64 scalar instructions in TableGen format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // ARM Instruction Predicate Definitions.
16 //
17 def HasNEON          : Predicate<"Subtarget->hasNEON()">,
18                                  AssemblerPredicate<"FeatureNEON", "neon">;
19 def HasCrypto        : Predicate<"Subtarget->hasCrypto()">,
20                                  AssemblerPredicate<"FeatureCrypto","crypto">;
21
22 // Use fused MAC if more precision in FP computation is allowed.
23 def UseFusedMAC      : Predicate<"(TM.Options.AllowFPOpFusion =="
24                                  " FPOpFusion::Fast)">;
25 include "AArch64InstrFormats.td"
26
27 //===----------------------------------------------------------------------===//
28 // Target-specific ISD nodes and profiles
29 //===----------------------------------------------------------------------===//
30
31 def SDT_A64ret : SDTypeProfile<0, 0, []>;
32 def A64ret : SDNode<"AArch64ISD::Ret", SDT_A64ret, [SDNPHasChain,
33                                                     SDNPOptInGlue,
34                                                     SDNPVariadic]>;
35
36 // (ins NZCV, Condition, Dest)
37 def SDT_A64br_cc : SDTypeProfile<0, 3, [SDTCisVT<0, i32>]>;
38 def A64br_cc : SDNode<"AArch64ISD::BR_CC", SDT_A64br_cc, [SDNPHasChain]>;
39
40 // (outs Result), (ins NZCV, IfTrue, IfFalse, Condition)
41 def SDT_A64select_cc : SDTypeProfile<1, 4, [SDTCisVT<1, i32>,
42                                             SDTCisSameAs<0, 2>,
43                                             SDTCisSameAs<2, 3>]>;
44 def A64select_cc : SDNode<"AArch64ISD::SELECT_CC", SDT_A64select_cc>;
45
46 // (outs NZCV), (ins LHS, RHS, Condition)
47 def SDT_A64setcc : SDTypeProfile<1, 3, [SDTCisVT<0, i32>,
48                                         SDTCisSameAs<1, 2>]>;
49 def A64setcc : SDNode<"AArch64ISD::SETCC", SDT_A64setcc>;
50
51
52 // (outs GPR64), (ins)
53 def A64threadpointer : SDNode<"AArch64ISD::THREAD_POINTER", SDTPtrLeaf>;
54
55 // A64 compares don't care about the cond really (they set all flags) so a
56 // simple binary operator is useful.
57 def A64cmp : PatFrag<(ops node:$lhs, node:$rhs),
58                      (A64setcc node:$lhs, node:$rhs, cond)>;
59
60
61 // When matching a notional (CMP op1, (sub 0, op2)), we'd like to use a CMN
62 // instruction on the grounds that "op1 - (-op2) == op1 + op2". However, the C
63 // and V flags can be set differently by this operation. It comes down to
64 // whether "SInt(~op2)+1 == SInt(~op2+1)" (and the same for UInt). If they are
65 // then everything is fine. If not then the optimization is wrong. Thus general
66 // comparisons are only valid if op2 != 0.
67
68 // So, finally, the only LLVM-native comparisons that don't mention C and V are
69 // SETEQ and SETNE. They're the only ones we can safely use CMN for in the
70 // absence of information about op2.
71 def equality_cond : PatLeaf<(cond), [{
72   return N->get() == ISD::SETEQ || N->get() == ISD::SETNE;
73 }]>;
74
75 def A64cmn : PatFrag<(ops node:$lhs, node:$rhs),
76                      (A64setcc node:$lhs, (sub 0, node:$rhs), equality_cond)>;
77
78 // There are two layers of indirection here, driven by the following
79 // considerations.
80 //     + TableGen does not know CodeModel or Reloc so that decision should be
81 //       made for a variable/address at ISelLowering.
82 //     + The output of ISelLowering should be selectable (hence the Wrapper,
83 //       rather than a bare target opcode)
84 def SDTAArch64WrapperLarge : SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
85                                                   SDTCisSameAs<0, 2>,
86                                                   SDTCisSameAs<0, 3>,
87                                                   SDTCisSameAs<0, 4>,
88                                                   SDTCisPtrTy<0>]>;
89
90 def A64WrapperLarge :SDNode<"AArch64ISD::WrapperLarge", SDTAArch64WrapperLarge>;
91
92 def SDTAArch64WrapperSmall : SDTypeProfile<1, 3, [SDTCisSameAs<0, 1>,
93                                                   SDTCisSameAs<1, 2>,
94                                                   SDTCisVT<3, i32>,
95                                                   SDTCisPtrTy<0>]>;
96
97 def A64WrapperSmall :SDNode<"AArch64ISD::WrapperSmall", SDTAArch64WrapperSmall>;
98
99
100 def SDTAArch64GOTLoad : SDTypeProfile<1, 1, [SDTCisPtrTy<0>, SDTCisPtrTy<1>]>;
101 def A64GOTLoad : SDNode<"AArch64ISD::GOTLoad", SDTAArch64GOTLoad,
102                         [SDNPHasChain]>;
103
104
105 // (A64BFI LHS, RHS, LSB, Width)
106 def SDTA64BFI : SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
107                                      SDTCisSameAs<1, 2>,
108                                      SDTCisVT<3, i64>,
109                                      SDTCisVT<4, i64>]>;
110
111 def A64Bfi : SDNode<"AArch64ISD::BFI", SDTA64BFI>;
112
113 // (A64EXTR HiReg, LoReg, LSB)
114 def SDTA64EXTR : SDTypeProfile<1, 3, [SDTCisSameAs<0, 1>, SDTCisSameAs<1, 2>,
115                                       SDTCisVT<3, i64>]>;
116 def A64Extr : SDNode<"AArch64ISD::EXTR", SDTA64EXTR>;
117
118 // (A64[SU]BFX Field, ImmR, ImmS).
119 //
120 // Note that ImmR and ImmS are already encoded for the actual instructions. The
121 // more natural LSB and Width mix together to form ImmR and ImmS, something
122 // which TableGen can't handle.
123 def SDTA64BFX : SDTypeProfile<1, 3, [SDTCisVT<2, i64>, SDTCisVT<3, i64>]>;
124 def A64Sbfx : SDNode<"AArch64ISD::SBFX", SDTA64BFX>;
125
126 def A64Ubfx : SDNode<"AArch64ISD::UBFX", SDTA64BFX>;
127
128 class BinOpFrag<dag res> : PatFrag<(ops node:$LHS, node:$RHS), res>;
129
130 //===----------------------------------------------------------------------===//
131 // Call sequence pseudo-instructions
132 //===----------------------------------------------------------------------===//
133
134
135 def SDT_AArch64Call : SDTypeProfile<0, -1, [SDTCisPtrTy<0>]>;
136 def AArch64Call : SDNode<"AArch64ISD::Call", SDT_AArch64Call,
137                      [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue, SDNPVariadic]>;
138
139 def AArch64tcret : SDNode<"AArch64ISD::TC_RETURN", SDT_AArch64Call,
140                           [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
141
142 // The TLSDESCCALL node is a variant call which goes to an indirectly calculated
143 // destination but needs a relocation against a fixed symbol. As such it has two
144 // certain operands: the callee and the relocated variable.
145 //
146 // The TLS ABI only allows it to be selected to a BLR instructin (with
147 // appropriate relocation).
148 def SDTTLSDescCall : SDTypeProfile<0, -2, [SDTCisPtrTy<0>, SDTCisPtrTy<1>]>;
149
150 def A64tlsdesc_blr : SDNode<"AArch64ISD::TLSDESCCALL", SDTTLSDescCall,
151                             [SDNPInGlue, SDNPOutGlue, SDNPHasChain,
152                              SDNPVariadic]>;
153
154
155 def SDT_AArch64CallSeqStart : SDCallSeqStart<[ SDTCisPtrTy<0> ]>;
156 def AArch64callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_AArch64CallSeqStart,
157                                   [SDNPHasChain, SDNPOutGlue]>;
158
159 def SDT_AArch64CallSeqEnd   : SDCallSeqEnd<[ SDTCisPtrTy<0>, SDTCisPtrTy<1> ]>;
160 def AArch64callseq_end : SDNode<"ISD::CALLSEQ_END",   SDT_AArch64CallSeqEnd,
161                                 [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
162
163
164
165 // These pseudo-instructions have special semantics by virtue of being passed to
166 // the InstrInfo constructor. CALLSEQ_START/CALLSEQ_END are produced by
167 // LowerCall to (in our case) tell the back-end about stack adjustments for
168 // arguments passed on the stack. Here we select those markers to
169 // pseudo-instructions which explicitly set the stack, and finally in the
170 // RegisterInfo we convert them to a true stack adjustment.
171 let Defs = [XSP], Uses = [XSP] in {
172   def ADJCALLSTACKDOWN : PseudoInst<(outs), (ins i64imm:$amt),
173                                     [(AArch64callseq_start timm:$amt)]>;
174
175   def ADJCALLSTACKUP : PseudoInst<(outs), (ins i64imm:$amt1, i64imm:$amt2),
176                                  [(AArch64callseq_end timm:$amt1, timm:$amt2)]>;
177 }
178
179 //===----------------------------------------------------------------------===//
180 // Atomic operation pseudo-instructions
181 //===----------------------------------------------------------------------===//
182
183 // These get selected from C++ code as a pretty much direct translation from the
184 // generic DAG nodes. The one exception is the AtomicOrdering is added as an
185 // operand so that the eventual lowering can make use of it and choose
186 // acquire/release operations when required.
187
188 let usesCustomInserter = 1, hasCtrlDep = 1, mayLoad = 1, mayStore = 1 in {
189 multiclass AtomicSizes {
190   def _I8 : PseudoInst<(outs GPR32:$dst),
191                        (ins GPR64xsp:$ptr, GPR32:$incr, i32imm:$ordering), []>;
192   def _I16 : PseudoInst<(outs GPR32:$dst),
193                         (ins GPR64xsp:$ptr, GPR32:$incr, i32imm:$ordering), []>;
194   def _I32 : PseudoInst<(outs GPR32:$dst),
195                         (ins GPR64xsp:$ptr, GPR32:$incr, i32imm:$ordering), []>;
196   def _I64 : PseudoInst<(outs GPR64:$dst),
197                         (ins GPR64xsp:$ptr, GPR64:$incr, i32imm:$ordering), []>;
198 }
199 }
200
201 defm ATOMIC_LOAD_ADD  : AtomicSizes;
202 defm ATOMIC_LOAD_SUB  : AtomicSizes;
203 defm ATOMIC_LOAD_AND  : AtomicSizes;
204 defm ATOMIC_LOAD_OR   : AtomicSizes;
205 defm ATOMIC_LOAD_XOR  : AtomicSizes;
206 defm ATOMIC_LOAD_NAND : AtomicSizes;
207 defm ATOMIC_SWAP      : AtomicSizes;
208 let Defs = [NZCV] in {
209   // These operations need a CMP to calculate the correct value
210   defm ATOMIC_LOAD_MIN  : AtomicSizes;
211   defm ATOMIC_LOAD_MAX  : AtomicSizes;
212   defm ATOMIC_LOAD_UMIN : AtomicSizes;
213   defm ATOMIC_LOAD_UMAX : AtomicSizes;
214 }
215
216 class AtomicCmpSwap<RegisterClass GPRData>
217   : PseudoInst<(outs GPRData:$dst),
218                (ins GPR64xsp:$ptr, GPRData:$old, GPRData:$new,
219                     i32imm:$ordering), []> {
220   let usesCustomInserter = 1;
221   let hasCtrlDep = 1;
222   let mayLoad = 1;
223   let mayStore = 1;
224   let Defs = [NZCV];
225 }
226
227 def ATOMIC_CMP_SWAP_I8  : AtomicCmpSwap<GPR32>;
228 def ATOMIC_CMP_SWAP_I16 : AtomicCmpSwap<GPR32>;
229 def ATOMIC_CMP_SWAP_I32 : AtomicCmpSwap<GPR32>;
230 def ATOMIC_CMP_SWAP_I64 : AtomicCmpSwap<GPR64>;
231
232 //===----------------------------------------------------------------------===//
233 // Add-subtract (extended register) instructions
234 //===----------------------------------------------------------------------===//
235 // Contains: ADD, ADDS, SUB, SUBS + aliases CMN, CMP
236
237 // The RHS of these operations is conceptually a sign/zero-extended
238 // register, optionally shifted left by 1-4. The extension can be a
239 // NOP (e.g. "sxtx" sign-extending a 64-bit register to 64-bits) but
240 // must be specified with one exception:
241
242 // If one of the registers is sp/wsp then LSL is an alias for UXTW in
243 // 32-bit instructions and UXTX in 64-bit versions, the shift amount
244 // is not optional in that case (but can explicitly be 0), and the
245 // entire suffix can be skipped (e.g. "add sp, x3, x2").
246
247 multiclass extend_operands<string PREFIX, string Diag> {
248      def _asmoperand : AsmOperandClass {
249          let Name = PREFIX;
250          let RenderMethod = "addRegExtendOperands";
251          let PredicateMethod = "isRegExtend<A64SE::" # PREFIX # ">";
252          let DiagnosticType = "AddSubRegExtend" # Diag;
253      }
254
255      def _operand : Operand<i64>,
256                     ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 4; }]> {
257          let PrintMethod = "printRegExtendOperand<A64SE::" # PREFIX # ">";
258          let DecoderMethod = "DecodeRegExtendOperand";
259          let ParserMatchClass = !cast<AsmOperandClass>(PREFIX # "_asmoperand");
260      }
261 }
262
263 defm UXTB : extend_operands<"UXTB", "Small">;
264 defm UXTH : extend_operands<"UXTH", "Small">;
265 defm UXTW : extend_operands<"UXTW", "Small">;
266 defm UXTX : extend_operands<"UXTX", "Large">;
267 defm SXTB : extend_operands<"SXTB", "Small">;
268 defm SXTH : extend_operands<"SXTH", "Small">;
269 defm SXTW : extend_operands<"SXTW", "Small">;
270 defm SXTX : extend_operands<"SXTX", "Large">;
271
272 def LSL_extasmoperand : AsmOperandClass {
273     let Name = "RegExtendLSL";
274     let RenderMethod = "addRegExtendOperands";
275     let DiagnosticType = "AddSubRegExtendLarge";
276 }
277
278 def LSL_extoperand : Operand<i64> {
279     let ParserMatchClass = LSL_extasmoperand;
280 }
281
282
283 // The patterns for various sign-extensions are a little ugly and
284 // non-uniform because everything has already been promoted to the
285 // legal i64 and i32 types. We'll wrap the various variants up in a
286 // class for use later.
287 class extend_types {
288     dag uxtb; dag uxth; dag uxtw; dag uxtx;
289     dag sxtb; dag sxth; dag sxtw; dag sxtx;
290     ValueType ty;
291     RegisterClass GPR;
292 }
293
294 def extends_to_i64 : extend_types {
295     let uxtb = (and (anyext i32:$Rm), 255);
296     let uxth = (and (anyext i32:$Rm), 65535);
297     let uxtw = (zext i32:$Rm);
298     let uxtx = (i64 $Rm);
299
300     let sxtb = (sext_inreg (anyext i32:$Rm), i8);
301     let sxth = (sext_inreg (anyext i32:$Rm), i16);
302     let sxtw = (sext i32:$Rm);
303     let sxtx = (i64 $Rm);
304
305     let ty = i64;
306     let GPR = GPR64xsp;
307 }
308
309
310 def extends_to_i32 : extend_types {
311     let uxtb = (and i32:$Rm, 255);
312     let uxth = (and i32:$Rm, 65535);
313     let uxtw = (i32 i32:$Rm);
314     let uxtx = (i32 i32:$Rm);
315
316     let sxtb = (sext_inreg i32:$Rm, i8);
317     let sxth = (sext_inreg i32:$Rm, i16);
318     let sxtw = (i32 i32:$Rm);
319     let sxtx = (i32 i32:$Rm);
320
321     let ty = i32;
322     let GPR = GPR32wsp;
323 }
324
325 // Now, six of the extensions supported are easy and uniform: if the source size
326 // is 32-bits or less, then Rm is always a 32-bit register. We'll instantiate
327 // those instructions in one block.
328
329 // The uxtx/sxtx could potentially be merged in, but three facts dissuaded me:
330 //     + It would break the naming scheme: either ADDxx_uxtx or ADDww_uxtx would
331 //       be impossible.
332 //     + Patterns are very different as well.
333 //     + Passing different registers would be ugly (more fields in extend_types
334 //       would probably be the best option).
335 multiclass addsub_exts<bit sf, bit op, bit S, string asmop,
336                        SDPatternOperator opfrag,
337                        dag outs, extend_types exts> {
338     def w_uxtb : A64I_addsubext<sf, op, S, 0b00, 0b000,
339                     outs, (ins exts.GPR:$Rn, GPR32:$Rm, UXTB_operand:$Imm3),
340                     !strconcat(asmop, "$Rn, $Rm, $Imm3"),
341                     [(opfrag exts.ty:$Rn, (shl exts.uxtb, UXTB_operand:$Imm3))],
342                     NoItinerary>;
343     def w_uxth : A64I_addsubext<sf, op, S, 0b00, 0b001,
344                     outs, (ins exts.GPR:$Rn, GPR32:$Rm, UXTH_operand:$Imm3),
345                     !strconcat(asmop, "$Rn, $Rm, $Imm3"),
346                     [(opfrag exts.ty:$Rn, (shl exts.uxth, UXTH_operand:$Imm3))],
347                     NoItinerary>;
348     def w_uxtw : A64I_addsubext<sf, op, S, 0b00, 0b010,
349                     outs, (ins exts.GPR:$Rn, GPR32:$Rm, UXTW_operand:$Imm3),
350                     !strconcat(asmop, "$Rn, $Rm, $Imm3"),
351                     [(opfrag exts.ty:$Rn, (shl exts.uxtw, UXTW_operand:$Imm3))],
352                     NoItinerary>;
353
354     def w_sxtb : A64I_addsubext<sf, op, S, 0b00, 0b100,
355                     outs, (ins exts.GPR:$Rn, GPR32:$Rm, SXTB_operand:$Imm3),
356                     !strconcat(asmop, "$Rn, $Rm, $Imm3"),
357                     [(opfrag exts.ty:$Rn, (shl exts.sxtb, SXTB_operand:$Imm3))],
358                     NoItinerary>;
359     def w_sxth : A64I_addsubext<sf, op, S, 0b00, 0b101,
360                     outs, (ins exts.GPR:$Rn, GPR32:$Rm, SXTH_operand:$Imm3),
361                     !strconcat(asmop, "$Rn, $Rm, $Imm3"),
362                     [(opfrag exts.ty:$Rn, (shl exts.sxth, SXTH_operand:$Imm3))],
363                     NoItinerary>;
364     def w_sxtw : A64I_addsubext<sf, op, S, 0b00, 0b110,
365                     outs, (ins exts.GPR:$Rn, GPR32:$Rm, SXTW_operand:$Imm3),
366                     !strconcat(asmop, "$Rn, $Rm, $Imm3"),
367                     [(opfrag exts.ty:$Rn, (shl exts.sxtw, SXTW_operand:$Imm3))],
368                     NoItinerary>;
369 }
370
371 // These two could be merge in with the above, but their patterns aren't really
372 // necessary and the naming-scheme would necessarily break:
373 multiclass addsub_xxtx<bit op, bit S, string asmop, SDPatternOperator opfrag,
374                        dag outs> {
375     def x_uxtx : A64I_addsubext<0b1, op, S, 0b00, 0b011,
376                    outs,
377                    (ins GPR64xsp:$Rn, GPR64:$Rm, UXTX_operand:$Imm3),
378                    !strconcat(asmop, "$Rn, $Rm, $Imm3"),
379                    [(opfrag i64:$Rn, (shl i64:$Rm, UXTX_operand:$Imm3))],
380                    NoItinerary>;
381
382     def x_sxtx : A64I_addsubext<0b1, op, S, 0b00, 0b111,
383                    outs,
384                    (ins GPR64xsp:$Rn, GPR64:$Rm, SXTX_operand:$Imm3),
385                    !strconcat(asmop, "$Rn, $Rm, $Imm3"),
386                    [/* No Pattern: same as uxtx */],
387                    NoItinerary>;
388 }
389
390 multiclass addsub_wxtx<bit op, bit S, string asmop, dag outs> {
391     def w_uxtx : A64I_addsubext<0b0, op, S, 0b00, 0b011,
392                               outs,
393                               (ins GPR32wsp:$Rn, GPR32:$Rm, UXTX_operand:$Imm3),
394                               !strconcat(asmop, "$Rn, $Rm, $Imm3"),
395                               [/* No pattern: probably same as uxtw */],
396                               NoItinerary>;
397
398     def w_sxtx : A64I_addsubext<0b0, op, S, 0b00, 0b111,
399                               outs,
400                               (ins GPR32wsp:$Rn, GPR32:$Rm, SXTX_operand:$Imm3),
401                               !strconcat(asmop, "$Rn, $Rm, $Imm3"),
402                               [/* No Pattern: probably same as uxtw */],
403                               NoItinerary>;
404 }
405
406 class SetRD<RegisterClass RC, SDPatternOperator op>
407  : PatFrag<(ops node:$lhs, node:$rhs), (set RC:$Rd, (op node:$lhs, node:$rhs))>;
408 class SetNZCV<SDPatternOperator op>
409   : PatFrag<(ops node:$lhs, node:$rhs), (set NZCV, (op node:$lhs, node:$rhs))>;
410
411 defm ADDxx :addsub_exts<0b1, 0b0, 0b0, "add\t$Rd, ", SetRD<GPR64xsp, add>,
412                         (outs GPR64xsp:$Rd), extends_to_i64>,
413             addsub_xxtx<     0b0, 0b0, "add\t$Rd, ", SetRD<GPR64xsp, add>,
414                         (outs GPR64xsp:$Rd)>;
415 defm ADDww :addsub_exts<0b0, 0b0, 0b0, "add\t$Rd, ", SetRD<GPR32wsp, add>,
416                         (outs GPR32wsp:$Rd), extends_to_i32>,
417             addsub_wxtx<     0b0, 0b0, "add\t$Rd, ",
418                         (outs GPR32wsp:$Rd)>;
419 defm SUBxx :addsub_exts<0b1, 0b1, 0b0, "sub\t$Rd, ", SetRD<GPR64xsp, sub>,
420                         (outs GPR64xsp:$Rd), extends_to_i64>,
421             addsub_xxtx<     0b1, 0b0, "sub\t$Rd, ", SetRD<GPR64xsp, sub>,
422                         (outs GPR64xsp:$Rd)>;
423 defm SUBww :addsub_exts<0b0, 0b1, 0b0, "sub\t$Rd, ", SetRD<GPR32wsp, sub>,
424                         (outs GPR32wsp:$Rd), extends_to_i32>,
425             addsub_wxtx<     0b1, 0b0, "sub\t$Rd, ",
426                         (outs GPR32wsp:$Rd)>;
427
428 let Defs = [NZCV] in {
429 defm ADDSxx :addsub_exts<0b1, 0b0, 0b1, "adds\t$Rd, ", SetRD<GPR64, addc>,
430                          (outs GPR64:$Rd), extends_to_i64>,
431              addsub_xxtx<     0b0, 0b1, "adds\t$Rd, ", SetRD<GPR64, addc>,
432                          (outs GPR64:$Rd)>;
433 defm ADDSww :addsub_exts<0b0, 0b0, 0b1, "adds\t$Rd, ", SetRD<GPR32, addc>,
434                          (outs GPR32:$Rd), extends_to_i32>,
435              addsub_wxtx<     0b0, 0b1, "adds\t$Rd, ",
436                          (outs GPR32:$Rd)>;
437 defm SUBSxx :addsub_exts<0b1, 0b1, 0b1, "subs\t$Rd, ", SetRD<GPR64, subc>,
438                          (outs GPR64:$Rd), extends_to_i64>,
439              addsub_xxtx<     0b1, 0b1, "subs\t$Rd, ", SetRD<GPR64, subc>,
440                          (outs GPR64:$Rd)>;
441 defm SUBSww :addsub_exts<0b0, 0b1, 0b1, "subs\t$Rd, ", SetRD<GPR32, subc>,
442                          (outs GPR32:$Rd), extends_to_i32>,
443              addsub_wxtx<     0b1, 0b1, "subs\t$Rd, ",
444                          (outs GPR32:$Rd)>;
445
446
447 let Rd = 0b11111, isCompare = 1 in {
448 defm CMNx : addsub_exts<0b1, 0b0, 0b1, "cmn\t", SetNZCV<A64cmn>,
449                         (outs), extends_to_i64>,
450             addsub_xxtx<     0b0, 0b1, "cmn\t", SetNZCV<A64cmn>, (outs)>;
451 defm CMNw : addsub_exts<0b0, 0b0, 0b1, "cmn\t", SetNZCV<A64cmn>,
452                         (outs), extends_to_i32>,
453             addsub_wxtx<     0b0, 0b1, "cmn\t", (outs)>;
454 defm CMPx : addsub_exts<0b1, 0b1, 0b1, "cmp\t", SetNZCV<A64cmp>,
455                         (outs), extends_to_i64>,
456             addsub_xxtx<     0b1, 0b1, "cmp\t", SetNZCV<A64cmp>, (outs)>;
457 defm CMPw : addsub_exts<0b0, 0b1, 0b1, "cmp\t", SetNZCV<A64cmp>,
458                         (outs), extends_to_i32>,
459             addsub_wxtx<     0b1, 0b1, "cmp\t", (outs)>;
460 }
461 }
462
463 // Now patterns for the operation without a shift being needed. No patterns are
464 // created for uxtx/sxtx since they're non-uniform and it's expected that
465 // add/sub (shifted register) will handle those cases anyway.
466 multiclass addsubext_noshift_patterns<string prefix, SDPatternOperator nodeop,
467                                       extend_types exts> {
468     def : Pat<(nodeop exts.ty:$Rn, exts.uxtb),
469               (!cast<Instruction>(prefix # "w_uxtb") $Rn, $Rm, 0)>;
470     def : Pat<(nodeop exts.ty:$Rn, exts.uxth),
471               (!cast<Instruction>(prefix # "w_uxth") $Rn, $Rm, 0)>;
472     def : Pat<(nodeop exts.ty:$Rn, exts.uxtw),
473               (!cast<Instruction>(prefix # "w_uxtw") $Rn, $Rm, 0)>;
474
475     def : Pat<(nodeop exts.ty:$Rn, exts.sxtb),
476               (!cast<Instruction>(prefix # "w_sxtb") $Rn, $Rm, 0)>;
477     def : Pat<(nodeop exts.ty:$Rn, exts.sxth),
478               (!cast<Instruction>(prefix # "w_sxth") $Rn, $Rm, 0)>;
479     def : Pat<(nodeop exts.ty:$Rn, exts.sxtw),
480               (!cast<Instruction>(prefix # "w_sxtw") $Rn, $Rm, 0)>;
481 }
482
483 defm : addsubext_noshift_patterns<"ADDxx", add, extends_to_i64>;
484 defm : addsubext_noshift_patterns<"ADDww", add, extends_to_i32>;
485 defm : addsubext_noshift_patterns<"SUBxx", sub, extends_to_i64>;
486 defm : addsubext_noshift_patterns<"SUBww", sub, extends_to_i32>;
487
488 defm : addsubext_noshift_patterns<"CMNx", A64cmn, extends_to_i64>;
489 defm : addsubext_noshift_patterns<"CMNw", A64cmn, extends_to_i32>;
490 defm : addsubext_noshift_patterns<"CMPx", A64cmp, extends_to_i64>;
491 defm : addsubext_noshift_patterns<"CMPw", A64cmp, extends_to_i32>;
492
493 // An extend of "lsl #imm" is valid if and only if one of Rn and Rd is
494 // sp/wsp. It is synonymous with uxtx/uxtw depending on the size of the
495 // operation. Also permitted in this case is complete omission of the argument,
496 // which implies "lsl #0".
497 multiclass lsl_aliases<string asmop, Instruction inst, RegisterClass GPR_Rd,
498                        RegisterClass GPR_Rn, RegisterClass GPR_Rm> {
499     def : InstAlias<!strconcat(asmop, " $Rd, $Rn, $Rm"),
500                     (inst GPR_Rd:$Rd, GPR_Rn:$Rn, GPR_Rm:$Rm, 0)>;
501
502     def : InstAlias<!strconcat(asmop, " $Rd, $Rn, $Rm, $LSL"),
503                 (inst GPR_Rd:$Rd, GPR_Rn:$Rn, GPR_Rm:$Rm, LSL_extoperand:$LSL)>;
504
505 }
506
507 defm : lsl_aliases<"add",  ADDxxx_uxtx,  Rxsp, GPR64xsp, GPR64>;
508 defm : lsl_aliases<"add",  ADDxxx_uxtx,  GPR64xsp, Rxsp, GPR64>;
509 defm : lsl_aliases<"add",  ADDwww_uxtw,  Rwsp, GPR32wsp, GPR32>;
510 defm : lsl_aliases<"add",  ADDwww_uxtw,  GPR32wsp, Rwsp, GPR32>;
511 defm : lsl_aliases<"sub",  SUBxxx_uxtx,  Rxsp, GPR64xsp, GPR64>;
512 defm : lsl_aliases<"sub",  SUBxxx_uxtx,  GPR64xsp, Rxsp, GPR64>;
513 defm : lsl_aliases<"sub",  SUBwww_uxtw,  Rwsp, GPR32wsp, GPR32>;
514 defm : lsl_aliases<"sub",  SUBwww_uxtw,  GPR32wsp, Rwsp, GPR32>;
515
516 // Rd cannot be sp for flag-setting variants so only half of the aliases are
517 // needed.
518 defm : lsl_aliases<"adds", ADDSxxx_uxtx, GPR64, Rxsp, GPR64>;
519 defm : lsl_aliases<"adds", ADDSwww_uxtw, GPR32, Rwsp, GPR32>;
520 defm : lsl_aliases<"subs", SUBSxxx_uxtx, GPR64, Rxsp, GPR64>;
521 defm : lsl_aliases<"subs", SUBSwww_uxtw, GPR32, Rwsp, GPR32>;
522
523 // CMP unfortunately has to be different because the instruction doesn't have a
524 // dest register.
525 multiclass cmp_lsl_aliases<string asmop, Instruction inst,
526                        RegisterClass GPR_Rn, RegisterClass GPR_Rm> {
527     def : InstAlias<!strconcat(asmop, " $Rn, $Rm"),
528                     (inst GPR_Rn:$Rn, GPR_Rm:$Rm, 0)>;
529
530     def : InstAlias<!strconcat(asmop, " $Rn, $Rm, $LSL"),
531                     (inst GPR_Rn:$Rn, GPR_Rm:$Rm, LSL_extoperand:$LSL)>;
532 }
533
534 defm : cmp_lsl_aliases<"cmp", CMPxx_uxtx, Rxsp, GPR64>;
535 defm : cmp_lsl_aliases<"cmp", CMPww_uxtw, Rwsp, GPR32>;
536 defm : cmp_lsl_aliases<"cmn", CMNxx_uxtx, Rxsp, GPR64>;
537 defm : cmp_lsl_aliases<"cmn", CMNww_uxtw, Rwsp, GPR32>;
538
539 //===----------------------------------------------------------------------===//
540 // Add-subtract (immediate) instructions
541 //===----------------------------------------------------------------------===//
542 // Contains: ADD, ADDS, SUB, SUBS + aliases CMN, CMP, MOV
543
544 // These instructions accept a 12-bit unsigned immediate, optionally shifted
545 // left by 12 bits. Official assembly format specifies a 12 bit immediate with
546 // one of "", "LSL #0", "LSL #12" supplementary operands.
547
548 // There are surprisingly few ways to make this work with TableGen, so this
549 // implementation has separate instructions for the "LSL #0" and "LSL #12"
550 // variants.
551
552 // If the MCInst retained a single combined immediate (which could be 0x123000,
553 // for example) then both components (imm & shift) would have to be delegated to
554 // a single assembly operand. This would entail a separate operand parser
555 // (because the LSL would have to live in the same AArch64Operand as the
556 // immediate to be accessible); assembly parsing is rather complex and
557 // error-prone C++ code.
558 //
559 // By splitting the immediate, we can delegate handling this optional operand to
560 // an InstAlias. Supporting functions to generate the correct MCInst are still
561 // required, but these are essentially trivial and parsing can remain generic.
562 //
563 // Rejected plans with rationale:
564 // ------------------------------
565 //
566 // In an ideal world you'de have two first class immediate operands (in
567 // InOperandList, specifying imm12 and shift). Unfortunately this is not
568 // selectable by any means I could discover.
569 //
570 // An Instruction with two MCOperands hidden behind a single entry in
571 // InOperandList (expanded by ComplexPatterns and MIOperandInfo) was functional,
572 // but required more C++ code to handle encoding/decoding. Parsing (the intended
573 // main beneficiary) ended up equally complex because of the optional nature of
574 // "LSL #0".
575 //
576 // Attempting to circumvent the need for a custom OperandParser above by giving
577 // InstAliases without the "lsl #0" failed. add/sub could be accommodated but
578 // the cmp/cmn aliases didn't use the MIOperandInfo to determine how operands
579 // should be parsed: there was no way to accommodate an "lsl #12".
580
581 let ParserMethod = "ParseImmWithLSLOperand",
582     RenderMethod = "addImmWithLSLOperands" in {
583   // Derived PredicateMethod fields are different for each
584   def addsubimm_lsl0_asmoperand : AsmOperandClass {
585     let Name = "AddSubImmLSL0";
586     // If an error is reported against this operand, instruction could also be a
587     // register variant.
588     let DiagnosticType = "AddSubSecondSource";
589   }
590
591   def addsubimm_lsl12_asmoperand : AsmOperandClass {
592     let Name = "AddSubImmLSL12";
593     let DiagnosticType = "AddSubSecondSource";
594   }
595 }
596
597 def shr_12_XFORM : SDNodeXForm<imm, [{
598   return CurDAG->getTargetConstant(N->getSExtValue() >> 12, MVT::i32);
599 }]>;
600
601 def shr_12_neg_XFORM : SDNodeXForm<imm, [{
602   return CurDAG->getTargetConstant((-N->getSExtValue()) >> 12, MVT::i32);
603 }]>;
604
605 def neg_XFORM : SDNodeXForm<imm, [{
606   return CurDAG->getTargetConstant(-N->getSExtValue(), MVT::i32);
607 }]>;
608
609
610 multiclass addsub_imm_operands<ValueType ty> {
611  let PrintMethod = "printAddSubImmLSL0Operand",
612       EncoderMethod = "getAddSubImmOpValue",
613       ParserMatchClass = addsubimm_lsl0_asmoperand in {
614     def _posimm_lsl0 : Operand<ty>,
615         ImmLeaf<ty, [{ return Imm >= 0 && (Imm & ~0xfff) == 0; }]>;
616     def _negimm_lsl0 : Operand<ty>,
617         ImmLeaf<ty, [{ return Imm < 0 && (-Imm & ~0xfff) == 0; }],
618                 neg_XFORM>;
619   }
620
621   let PrintMethod = "printAddSubImmLSL12Operand",
622       EncoderMethod = "getAddSubImmOpValue",
623       ParserMatchClass = addsubimm_lsl12_asmoperand in {
624     def _posimm_lsl12 : Operand<ty>,
625         ImmLeaf<ty, [{ return Imm >= 0 && (Imm & ~0xfff000) == 0; }],
626                 shr_12_XFORM>;
627
628     def _negimm_lsl12 : Operand<ty>,
629         ImmLeaf<ty, [{ return Imm < 0 && (-Imm & ~0xfff000) == 0; }],
630                 shr_12_neg_XFORM>;
631   }
632 }
633
634 // The add operands don't need any transformation
635 defm addsubimm_operand_i32 : addsub_imm_operands<i32>;
636 defm addsubimm_operand_i64 : addsub_imm_operands<i64>;
637
638 multiclass addsubimm_varieties<string prefix, bit sf, bit op, bits<2> shift,
639                                string asmop, string cmpasmop,
640                                Operand imm_operand, Operand cmp_imm_operand,
641                                RegisterClass GPR, RegisterClass GPRsp,
642                                AArch64Reg ZR, ValueType Ty> {
643     // All registers for non-S variants allow SP
644   def _s : A64I_addsubimm<sf, op, 0b0, shift,
645                          (outs GPRsp:$Rd),
646                          (ins GPRsp:$Rn, imm_operand:$Imm12),
647                          !strconcat(asmop, "\t$Rd, $Rn, $Imm12"),
648                          [(set Ty:$Rd, (add Ty:$Rn, imm_operand:$Imm12))],
649                          NoItinerary>;
650
651
652   // S variants can read SP but would write to ZR
653   def _S : A64I_addsubimm<sf, op, 0b1, shift,
654                          (outs GPR:$Rd),
655                          (ins GPRsp:$Rn, imm_operand:$Imm12),
656                          !strconcat(asmop, "s\t$Rd, $Rn, $Imm12"),
657                          [(set Ty:$Rd, (addc Ty:$Rn, imm_operand:$Imm12))],
658                          NoItinerary> {
659     let Defs = [NZCV];
660   }
661
662   // Note that the pattern here for ADDS is subtle. Canonically CMP
663   // a, b becomes SUBS a, b. If b < 0 then this is equivalent to
664   // ADDS a, (-b). This is not true in general.
665   def _cmp : A64I_addsubimm<sf, op, 0b1, shift,
666                             (outs), (ins GPRsp:$Rn, imm_operand:$Imm12),
667                             !strconcat(cmpasmop, " $Rn, $Imm12"),
668                             [(set NZCV,
669                                   (A64cmp Ty:$Rn, cmp_imm_operand:$Imm12))],
670                             NoItinerary> {
671     let Rd = 0b11111;
672     let Defs = [NZCV];
673     let isCompare = 1;
674   }
675 }
676
677
678 multiclass addsubimm_shifts<string prefix, bit sf, bit op,
679            string asmop, string cmpasmop, string operand, string cmpoperand,
680            RegisterClass GPR, RegisterClass GPRsp, AArch64Reg ZR,
681            ValueType Ty> {
682   defm _lsl0 : addsubimm_varieties<prefix # "_lsl0", sf, op, 0b00,
683                                    asmop, cmpasmop,
684                                    !cast<Operand>(operand # "_lsl0"),
685                                    !cast<Operand>(cmpoperand # "_lsl0"),
686                                    GPR, GPRsp, ZR, Ty>;
687
688   defm _lsl12 : addsubimm_varieties<prefix # "_lsl12", sf, op, 0b01,
689                                     asmop, cmpasmop,
690                                     !cast<Operand>(operand # "_lsl12"),
691                                     !cast<Operand>(cmpoperand # "_lsl12"),
692                                     GPR, GPRsp, ZR, Ty>;
693 }
694
695 defm ADDwwi : addsubimm_shifts<"ADDwi", 0b0, 0b0, "add", "cmn",
696                               "addsubimm_operand_i32_posimm",
697                               "addsubimm_operand_i32_negimm",
698                               GPR32, GPR32wsp, WZR, i32>;
699 defm ADDxxi : addsubimm_shifts<"ADDxi", 0b1, 0b0, "add", "cmn",
700                               "addsubimm_operand_i64_posimm",
701                               "addsubimm_operand_i64_negimm",
702                               GPR64, GPR64xsp, XZR, i64>;
703 defm SUBwwi : addsubimm_shifts<"SUBwi", 0b0, 0b1, "sub", "cmp",
704                               "addsubimm_operand_i32_negimm",
705                               "addsubimm_operand_i32_posimm",
706                               GPR32, GPR32wsp, WZR, i32>;
707 defm SUBxxi : addsubimm_shifts<"SUBxi", 0b1, 0b1, "sub", "cmp",
708                               "addsubimm_operand_i64_negimm",
709                               "addsubimm_operand_i64_posimm",
710                               GPR64, GPR64xsp, XZR, i64>;
711
712 multiclass MOVsp<RegisterClass GPRsp, RegisterClass SP, Instruction addop> {
713   def _fromsp : InstAlias<"mov $Rd, $Rn",
714                           (addop GPRsp:$Rd, SP:$Rn, 0),
715                           0b1>;
716
717   def _tosp : InstAlias<"mov $Rd, $Rn",
718                         (addop SP:$Rd, GPRsp:$Rn, 0),
719                         0b1>;
720 }
721
722 // Recall Rxsp is a RegisterClass containing *just* xsp.
723 defm MOVxx : MOVsp<GPR64xsp, Rxsp, ADDxxi_lsl0_s>;
724 defm MOVww : MOVsp<GPR32wsp, Rwsp, ADDwwi_lsl0_s>;
725
726 //===----------------------------------------------------------------------===//
727 // Add-subtract (shifted register) instructions
728 //===----------------------------------------------------------------------===//
729 // Contains: ADD, ADDS, SUB, SUBS + aliases CMN, CMP, NEG, NEGS
730
731 //===-------------------------------
732 // 1. The "shifed register" operands. Shared with logical insts.
733 //===-------------------------------
734
735 multiclass shift_operands<string prefix, string form> {
736   def _asmoperand_i32 : AsmOperandClass {
737     let Name = "Shift" # form # "i32";
738     let RenderMethod = "addShiftOperands";
739     let PredicateMethod = "isShift<A64SE::" # form # ", false>";
740     let DiagnosticType = "AddSubRegShift32";
741   }
742
743   // Note that the operand type is intentionally i64 because the DAGCombiner
744   // puts these into a canonical form.
745   def _i32 : Operand<i64>, ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 31; }]> {
746     let ParserMatchClass
747           = !cast<AsmOperandClass>(prefix # "_asmoperand_i32");
748     let PrintMethod = "printShiftOperand<A64SE::" # form # ">";
749     let DecoderMethod = "Decode32BitShiftOperand";
750   }
751
752   def _asmoperand_i64 : AsmOperandClass {
753       let Name = "Shift" # form # "i64";
754       let RenderMethod = "addShiftOperands";
755       let PredicateMethod = "isShift<A64SE::" # form # ", true>";
756       let DiagnosticType = "AddSubRegShift64";
757   }
758
759   def _i64 : Operand<i64>, ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 63; }]> {
760     let ParserMatchClass
761           = !cast<AsmOperandClass>(prefix # "_asmoperand_i64");
762     let PrintMethod = "printShiftOperand<A64SE::" # form # ">";
763   }
764 }
765
766 defm lsl_operand : shift_operands<"lsl_operand", "LSL">;
767 defm lsr_operand : shift_operands<"lsr_operand", "LSR">;
768 defm asr_operand : shift_operands<"asr_operand", "ASR">;
769
770 // Not used for add/sub, but defined here for completeness. The "logical
771 // (shifted register)" instructions *do* have an ROR variant.
772 defm ror_operand : shift_operands<"ror_operand", "ROR">;
773
774 //===-------------------------------
775 // 2. The basic 3.5-operand ADD/SUB/ADDS/SUBS instructions.
776 //===-------------------------------
777
778 // N.b. the commutable parameter is just !N. It will be first against the wall
779 // when the revolution comes.
780 multiclass addsub_shifts<string prefix, bit sf, bit op, bit s, bit commutable,
781                          string asmop, SDPatternOperator opfrag, ValueType ty,
782                          RegisterClass GPR, list<Register> defs> {
783   let isCommutable = commutable, Defs = defs in {
784   def _lsl : A64I_addsubshift<sf, op, s, 0b00,
785                        (outs GPR:$Rd),
786                        (ins GPR:$Rn, GPR:$Rm,
787                             !cast<Operand>("lsl_operand_" # ty):$Imm6),
788                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
789                        [(set GPR:$Rd, (opfrag ty:$Rn, (shl ty:$Rm,
790                             !cast<Operand>("lsl_operand_" # ty):$Imm6))
791                        )],
792                        NoItinerary>;
793
794   def _lsr : A64I_addsubshift<sf, op, s, 0b01,
795                        (outs GPR:$Rd),
796                        (ins GPR:$Rn, GPR:$Rm,
797                             !cast<Operand>("lsr_operand_" # ty):$Imm6),
798                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
799                        [(set ty:$Rd, (opfrag ty:$Rn, (srl ty:$Rm,
800                             !cast<Operand>("lsr_operand_" # ty):$Imm6))
801                        )],
802                        NoItinerary>;
803
804   def _asr : A64I_addsubshift<sf, op, s, 0b10,
805                        (outs GPR:$Rd),
806                        (ins GPR:$Rn, GPR:$Rm,
807                             !cast<Operand>("asr_operand_" # ty):$Imm6),
808                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
809                        [(set ty:$Rd, (opfrag ty:$Rn, (sra ty:$Rm,
810                             !cast<Operand>("asr_operand_" # ty):$Imm6))
811                        )],
812                        NoItinerary>;
813   }
814
815   def _noshift
816       : InstAlias<!strconcat(asmop, " $Rd, $Rn, $Rm"),
817                  (!cast<Instruction>(prefix # "_lsl") GPR:$Rd, GPR:$Rn,
818                                                       GPR:$Rm, 0)>;
819
820   def : Pat<(opfrag ty:$Rn, ty:$Rm),
821             (!cast<Instruction>(prefix # "_lsl") $Rn, $Rm, 0)>;
822 }
823
824 multiclass addsub_sizes<string prefix, bit op, bit s, bit commutable,
825                          string asmop, SDPatternOperator opfrag,
826                          list<Register> defs> {
827   defm xxx : addsub_shifts<prefix # "xxx", 0b1, op, s,
828                            commutable, asmop, opfrag, i64, GPR64, defs>;
829   defm www : addsub_shifts<prefix # "www", 0b0, op, s,
830                            commutable, asmop, opfrag, i32, GPR32, defs>;
831 }
832
833
834 defm ADD : addsub_sizes<"ADD", 0b0, 0b0, 0b1, "add", add, []>;
835 defm SUB : addsub_sizes<"SUB", 0b1, 0b0, 0b0, "sub", sub, []>;
836
837 defm ADDS : addsub_sizes<"ADDS", 0b0, 0b1, 0b1, "adds", addc, [NZCV]>;
838 defm SUBS : addsub_sizes<"SUBS", 0b1, 0b1, 0b0, "subs", subc, [NZCV]>;
839
840 //===-------------------------------
841 // 1. The NEG/NEGS aliases
842 //===-------------------------------
843
844 multiclass neg_alias<Instruction INST, RegisterClass GPR, Register ZR,
845                      ValueType ty, Operand shift_operand, SDNode shiftop> {
846    def : InstAlias<"neg $Rd, $Rm, $Imm6",
847                    (INST GPR:$Rd, ZR, GPR:$Rm, shift_operand:$Imm6)>;
848
849    def : Pat<(sub 0, (shiftop ty:$Rm, shift_operand:$Imm6)),
850              (INST ZR, $Rm, shift_operand:$Imm6)>;
851 }
852
853 defm : neg_alias<SUBwww_lsl, GPR32, WZR, i32, lsl_operand_i32, shl>;
854 defm : neg_alias<SUBwww_lsr, GPR32, WZR, i32, lsr_operand_i32, srl>;
855 defm : neg_alias<SUBwww_asr, GPR32, WZR, i32, asr_operand_i32, sra>;
856 def : InstAlias<"neg $Rd, $Rm", (SUBwww_lsl GPR32:$Rd, WZR, GPR32:$Rm, 0)>;
857 def : Pat<(sub 0, i32:$Rm), (SUBwww_lsl WZR, $Rm, 0)>;
858
859 defm : neg_alias<SUBxxx_lsl, GPR64, XZR, i64, lsl_operand_i64, shl>;
860 defm : neg_alias<SUBxxx_lsr, GPR64, XZR, i64, lsr_operand_i64, srl>;
861 defm : neg_alias<SUBxxx_asr, GPR64, XZR, i64, asr_operand_i64, sra>;
862 def : InstAlias<"neg $Rd, $Rm", (SUBxxx_lsl GPR64:$Rd, XZR, GPR64:$Rm, 0)>;
863 def : Pat<(sub 0, i64:$Rm), (SUBxxx_lsl XZR, $Rm, 0)>;
864
865 // NEGS doesn't get any patterns yet: defining multiple outputs means C++ has to
866 // be involved.
867 class negs_alias<Instruction INST, RegisterClass GPR,
868                  Register ZR, Operand shift_operand, SDNode shiftop>
869   : InstAlias<"negs $Rd, $Rm, $Imm6",
870               (INST GPR:$Rd, ZR, GPR:$Rm, shift_operand:$Imm6)>;
871
872 def : negs_alias<SUBSwww_lsl, GPR32, WZR, lsl_operand_i32, shl>;
873 def : negs_alias<SUBSwww_lsr, GPR32, WZR, lsr_operand_i32, srl>;
874 def : negs_alias<SUBSwww_asr, GPR32, WZR, asr_operand_i32, sra>;
875 def : InstAlias<"negs $Rd, $Rm", (SUBSwww_lsl GPR32:$Rd, WZR, GPR32:$Rm, 0)>;
876
877 def : negs_alias<SUBSxxx_lsl, GPR64, XZR, lsl_operand_i64, shl>;
878 def : negs_alias<SUBSxxx_lsr, GPR64, XZR, lsr_operand_i64, srl>;
879 def : negs_alias<SUBSxxx_asr, GPR64, XZR, asr_operand_i64, sra>;
880 def : InstAlias<"negs $Rd, $Rm", (SUBSxxx_lsl GPR64:$Rd, XZR, GPR64:$Rm, 0)>;
881
882 //===-------------------------------
883 // 1. The CMP/CMN aliases
884 //===-------------------------------
885
886 multiclass cmp_shifts<string prefix, bit sf, bit op, bit commutable,
887                       string asmop, SDPatternOperator opfrag, ValueType ty,
888                       RegisterClass GPR> {
889   let isCommutable = commutable, Rd = 0b11111, Defs = [NZCV] in {
890   def _lsl : A64I_addsubshift<sf, op, 0b1, 0b00,
891                        (outs),
892                        (ins GPR:$Rn, GPR:$Rm,
893                             !cast<Operand>("lsl_operand_" # ty):$Imm6),
894                        !strconcat(asmop, "\t$Rn, $Rm, $Imm6"),
895                        [(set NZCV, (opfrag ty:$Rn, (shl ty:$Rm,
896                             !cast<Operand>("lsl_operand_" # ty):$Imm6))
897                        )],
898                        NoItinerary>;
899
900   def _lsr : A64I_addsubshift<sf, op, 0b1, 0b01,
901                        (outs),
902                        (ins GPR:$Rn, GPR:$Rm,
903                             !cast<Operand>("lsr_operand_" # ty):$Imm6),
904                        !strconcat(asmop, "\t$Rn, $Rm, $Imm6"),
905                        [(set NZCV, (opfrag ty:$Rn, (srl ty:$Rm,
906                             !cast<Operand>("lsr_operand_" # ty):$Imm6))
907                        )],
908                        NoItinerary>;
909
910   def _asr : A64I_addsubshift<sf, op, 0b1, 0b10,
911                        (outs),
912                        (ins GPR:$Rn, GPR:$Rm,
913                             !cast<Operand>("asr_operand_" # ty):$Imm6),
914                        !strconcat(asmop, "\t$Rn, $Rm, $Imm6"),
915                        [(set NZCV, (opfrag ty:$Rn, (sra ty:$Rm,
916                             !cast<Operand>("asr_operand_" # ty):$Imm6))
917                        )],
918                        NoItinerary>;
919   }
920
921   def _noshift
922       : InstAlias<!strconcat(asmop, " $Rn, $Rm"),
923                  (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
924
925   def : Pat<(opfrag ty:$Rn, ty:$Rm),
926             (!cast<Instruction>(prefix # "_lsl") $Rn, $Rm, 0)>;
927 }
928
929 defm CMPww : cmp_shifts<"CMPww", 0b0, 0b1, 0b0, "cmp", A64cmp, i32, GPR32>;
930 defm CMPxx : cmp_shifts<"CMPxx", 0b1, 0b1, 0b0, "cmp", A64cmp, i64, GPR64>;
931
932 defm CMNww : cmp_shifts<"CMNww", 0b0, 0b0, 0b1, "cmn", A64cmn, i32, GPR32>;
933 defm CMNxx : cmp_shifts<"CMNxx", 0b1, 0b0, 0b1, "cmn", A64cmn, i64, GPR64>;
934
935 //===----------------------------------------------------------------------===//
936 // Add-subtract (with carry) instructions
937 //===----------------------------------------------------------------------===//
938 // Contains: ADC, ADCS, SBC, SBCS + aliases NGC, NGCS
939
940 multiclass A64I_addsubcarrySizes<bit op, bit s, string asmop> {
941   let Uses = [NZCV] in {
942     def www : A64I_addsubcarry<0b0, op, s, 0b000000,
943                                (outs GPR32:$Rd), (ins GPR32:$Rn, GPR32:$Rm),
944                                !strconcat(asmop, "\t$Rd, $Rn, $Rm"),
945                                [], NoItinerary>;
946
947     def xxx : A64I_addsubcarry<0b1, op, s, 0b000000,
948                                (outs GPR64:$Rd), (ins GPR64:$Rn, GPR64:$Rm),
949                                !strconcat(asmop, "\t$Rd, $Rn, $Rm"),
950                                [], NoItinerary>;
951   }
952 }
953
954 let isCommutable = 1 in {
955   defm ADC : A64I_addsubcarrySizes<0b0, 0b0, "adc">;
956 }
957
958 defm SBC : A64I_addsubcarrySizes<0b1, 0b0, "sbc">;
959
960 let Defs = [NZCV] in {
961   let isCommutable = 1 in {
962     defm ADCS : A64I_addsubcarrySizes<0b0, 0b1, "adcs">;
963   }
964
965   defm SBCS : A64I_addsubcarrySizes<0b1, 0b1, "sbcs">;
966 }
967
968 def : InstAlias<"ngc $Rd, $Rm", (SBCwww GPR32:$Rd, WZR, GPR32:$Rm)>;
969 def : InstAlias<"ngc $Rd, $Rm", (SBCxxx GPR64:$Rd, XZR, GPR64:$Rm)>;
970 def : InstAlias<"ngcs $Rd, $Rm", (SBCSwww GPR32:$Rd, WZR, GPR32:$Rm)>;
971 def : InstAlias<"ngcs $Rd, $Rm", (SBCSxxx GPR64:$Rd, XZR, GPR64:$Rm)>;
972
973 // Note that adde and sube can form a chain longer than two (e.g. for 256-bit
974 // addition). So the flag-setting instructions are appropriate.
975 def : Pat<(adde i32:$Rn, i32:$Rm), (ADCSwww $Rn, $Rm)>;
976 def : Pat<(adde i64:$Rn, i64:$Rm), (ADCSxxx $Rn, $Rm)>;
977 def : Pat<(sube i32:$Rn, i32:$Rm), (SBCSwww $Rn, $Rm)>;
978 def : Pat<(sube i64:$Rn, i64:$Rm), (SBCSxxx $Rn, $Rm)>;
979
980 //===----------------------------------------------------------------------===//
981 // Bitfield
982 //===----------------------------------------------------------------------===//
983 // Contains: SBFM, BFM, UBFM, [SU]XT[BHW], ASR, LSR, LSL, SBFI[ZX], BFI, BFXIL,
984 //     UBFIZ, UBFX
985
986 // Because of the rather complicated nearly-overlapping aliases, the decoding of
987 // this range of instructions is handled manually. The architectural
988 // instructions are BFM, SBFM and UBFM but a disassembler should never produce
989 // these.
990 //
991 // In the end, the best option was to use BFM instructions for decoding under
992 // almost all circumstances, but to create aliasing *Instructions* for each of
993 // the canonical forms and specify a completely custom decoder which would
994 // substitute the correct MCInst as needed.
995 //
996 // This also simplifies instruction selection, parsing etc because the MCInsts
997 // have a shape that's closer to their use in code.
998
999 //===-------------------------------
1000 // 1. The architectural BFM instructions
1001 //===-------------------------------
1002
1003 def uimm5_asmoperand : AsmOperandClass {
1004   let Name = "UImm5";
1005   let PredicateMethod = "isUImm<5>";
1006   let RenderMethod = "addImmOperands";
1007   let DiagnosticType = "UImm5";
1008 }
1009
1010 def uimm6_asmoperand : AsmOperandClass {
1011   let Name = "UImm6";
1012   let PredicateMethod = "isUImm<6>";
1013   let RenderMethod = "addImmOperands";
1014   let DiagnosticType = "UImm6";
1015 }
1016
1017 def bitfield32_imm : Operand<i64>,
1018                      ImmLeaf<i64, [{ return Imm >= 0 && Imm < 32; }]> {
1019   let ParserMatchClass = uimm5_asmoperand;
1020
1021   let DecoderMethod = "DecodeBitfield32ImmOperand";
1022 }
1023
1024
1025 def bitfield64_imm : Operand<i64>,
1026                      ImmLeaf<i64, [{ return Imm >= 0 && Imm < 64; }]> {
1027   let ParserMatchClass = uimm6_asmoperand;
1028
1029   // Default decoder works in 64-bit case: the 6-bit field can take any value.
1030 }
1031
1032 multiclass A64I_bitfieldSizes<bits<2> opc, string asmop> {
1033   def wwii : A64I_bitfield<0b0, opc, 0b0, (outs GPR32:$Rd),
1034                     (ins GPR32:$Rn, bitfield32_imm:$ImmR, bitfield32_imm:$ImmS),
1035                     !strconcat(asmop, "\t$Rd, $Rn, $ImmR, $ImmS"),
1036                     [], NoItinerary> {
1037     let DecoderMethod = "DecodeBitfieldInstruction";
1038   }
1039
1040   def xxii : A64I_bitfield<0b1, opc, 0b1, (outs GPR64:$Rd),
1041                     (ins GPR64:$Rn, bitfield64_imm:$ImmR, bitfield64_imm:$ImmS),
1042                     !strconcat(asmop, "\t$Rd, $Rn, $ImmR, $ImmS"),
1043                     [], NoItinerary> {
1044     let DecoderMethod = "DecodeBitfieldInstruction";
1045   }
1046 }
1047
1048 defm SBFM : A64I_bitfieldSizes<0b00, "sbfm">;
1049 defm UBFM : A64I_bitfieldSizes<0b10, "ubfm">;
1050
1051 // BFM instructions modify the destination register rather than defining it
1052 // completely.
1053 def BFMwwii :
1054   A64I_bitfield<0b0, 0b01, 0b0, (outs GPR32:$Rd),
1055         (ins GPR32:$src, GPR32:$Rn, bitfield32_imm:$ImmR, bitfield32_imm:$ImmS),
1056         "bfm\t$Rd, $Rn, $ImmR, $ImmS", [], NoItinerary> {
1057   let DecoderMethod = "DecodeBitfieldInstruction";
1058   let Constraints = "$src = $Rd";
1059 }
1060
1061 def BFMxxii :
1062   A64I_bitfield<0b1, 0b01, 0b1, (outs GPR64:$Rd),
1063         (ins GPR64:$src, GPR64:$Rn, bitfield64_imm:$ImmR, bitfield64_imm:$ImmS),
1064         "bfm\t$Rd, $Rn, $ImmR, $ImmS", [], NoItinerary> {
1065   let DecoderMethod = "DecodeBitfieldInstruction";
1066   let Constraints = "$src = $Rd";
1067 }
1068
1069
1070 //===-------------------------------
1071 // 2. Extend aliases to 64-bit dest
1072 //===-------------------------------
1073
1074 // Unfortunately the extensions that end up as 64-bits cannot be handled by an
1075 // instruction alias: their syntax is (for example) "SXTB x0, w0", which needs
1076 // to be mapped to "SBFM x0, x0, #0, 7" (changing the class of Rn). InstAlias is
1077 // not capable of such a map as far as I'm aware
1078
1079 // Note that these instructions are strictly more specific than the
1080 // BFM ones (in ImmR) so they can handle their own decoding.
1081 class A64I_bf_ext<bit sf, bits<2> opc, RegisterClass GPRDest, ValueType dty,
1082                     string asmop, bits<6> imms, dag pattern>
1083   : A64I_bitfield<sf, opc, sf,
1084                   (outs GPRDest:$Rd), (ins GPR32:$Rn),
1085                   !strconcat(asmop, "\t$Rd, $Rn"),
1086                   [(set dty:$Rd, pattern)], NoItinerary> {
1087   let ImmR = 0b000000;
1088   let ImmS = imms;
1089 }
1090
1091 // Signed extensions
1092 def SXTBxw : A64I_bf_ext<0b1, 0b00, GPR64, i64, "sxtb", 7,
1093                          (sext_inreg (anyext i32:$Rn), i8)>;
1094 def SXTBww : A64I_bf_ext<0b0, 0b00, GPR32, i32, "sxtb", 7,
1095                          (sext_inreg i32:$Rn, i8)>;
1096 def SXTHxw : A64I_bf_ext<0b1, 0b00, GPR64, i64, "sxth", 15,
1097                          (sext_inreg (anyext i32:$Rn), i16)>;
1098 def SXTHww : A64I_bf_ext<0b0, 0b00, GPR32, i32, "sxth", 15,
1099                          (sext_inreg i32:$Rn, i16)>;
1100 def SXTWxw : A64I_bf_ext<0b1, 0b00, GPR64, i64, "sxtw", 31, (sext i32:$Rn)>;
1101
1102 // Unsigned extensions
1103 def UXTBww : A64I_bf_ext<0b0, 0b10, GPR32, i32, "uxtb", 7,
1104                          (and i32:$Rn, 255)>;
1105 def UXTHww : A64I_bf_ext<0b0, 0b10, GPR32, i32, "uxth", 15,
1106                          (and i32:$Rn, 65535)>;
1107
1108 // The 64-bit unsigned variants are not strictly architectural but recommended
1109 // for consistency.
1110 let isAsmParserOnly = 1 in {
1111   def UXTBxw : A64I_bf_ext<0b0, 0b10, GPR64, i64, "uxtb", 7,
1112                            (and (anyext i32:$Rn), 255)>;
1113   def UXTHxw : A64I_bf_ext<0b0, 0b10, GPR64, i64, "uxth", 15,
1114                            (and (anyext i32:$Rn), 65535)>;
1115 }
1116
1117 // Extra patterns for when the source register is actually 64-bits
1118 // too. There's no architectural difference here, it's just LLVM
1119 // shinanigans. There's no need for equivalent zero-extension patterns
1120 // because they'll already be caught by logical (immediate) matching.
1121 def : Pat<(sext_inreg i64:$Rn, i8),
1122           (SXTBxw (EXTRACT_SUBREG $Rn, sub_32))>;
1123 def : Pat<(sext_inreg i64:$Rn, i16),
1124           (SXTHxw (EXTRACT_SUBREG $Rn, sub_32))>;
1125 def : Pat<(sext_inreg i64:$Rn, i32),
1126           (SXTWxw (EXTRACT_SUBREG $Rn, sub_32))>;
1127
1128
1129 //===-------------------------------
1130 // 3. Aliases for ASR and LSR (the simple shifts)
1131 //===-------------------------------
1132
1133 // These also handle their own decoding because ImmS being set makes
1134 // them take precedence over BFM.
1135 multiclass A64I_shift<bits<2> opc, string asmop, SDNode opnode> {
1136   def wwi : A64I_bitfield<0b0, opc, 0b0,
1137                     (outs GPR32:$Rd), (ins GPR32:$Rn, bitfield32_imm:$ImmR),
1138                     !strconcat(asmop, "\t$Rd, $Rn, $ImmR"),
1139                     [(set i32:$Rd, (opnode i32:$Rn, bitfield32_imm:$ImmR))],
1140                     NoItinerary> {
1141     let ImmS = 31;
1142   }
1143
1144   def xxi : A64I_bitfield<0b1, opc, 0b1,
1145                     (outs GPR64:$Rd), (ins GPR64:$Rn, bitfield64_imm:$ImmR),
1146                     !strconcat(asmop, "\t$Rd, $Rn, $ImmR"),
1147                     [(set i64:$Rd, (opnode i64:$Rn, bitfield64_imm:$ImmR))],
1148                     NoItinerary> {
1149     let ImmS = 63;
1150   }
1151
1152 }
1153
1154 defm ASR : A64I_shift<0b00, "asr", sra>;
1155 defm LSR : A64I_shift<0b10, "lsr", srl>;
1156
1157 //===-------------------------------
1158 // 4. Aliases for LSL
1159 //===-------------------------------
1160
1161 // Unfortunately LSL and subsequent aliases are much more complicated. We need
1162 // to be able to say certain output instruction fields depend in a complex
1163 // manner on combinations of input assembly fields).
1164 //
1165 // MIOperandInfo *might* have been able to do it, but at the cost of
1166 // significantly more C++ code.
1167
1168 // N.b. contrary to usual practice these operands store the shift rather than
1169 // the machine bits in an MCInst. The complexity overhead of consistency
1170 // outweighed the benefits in this case (custom asmparser, printer and selection
1171 // vs custom encoder).
1172 def bitfield32_lsl_imm : Operand<i64>,
1173                          ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 31; }]> {
1174   let ParserMatchClass = uimm5_asmoperand;
1175   let EncoderMethod = "getBitfield32LSLOpValue";
1176 }
1177
1178 def bitfield64_lsl_imm : Operand<i64>,
1179                          ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 63; }]> {
1180   let ParserMatchClass = uimm6_asmoperand;
1181   let EncoderMethod = "getBitfield64LSLOpValue";
1182 }
1183
1184 class A64I_bitfield_lsl<bit sf, RegisterClass GPR, ValueType ty,
1185                         Operand operand>
1186   : A64I_bitfield<sf, 0b10, sf, (outs GPR:$Rd), (ins GPR:$Rn, operand:$FullImm),
1187                   "lsl\t$Rd, $Rn, $FullImm",
1188                   [(set ty:$Rd, (shl ty:$Rn, operand:$FullImm))],
1189                   NoItinerary> {
1190   bits<12> FullImm;
1191   let ImmR = FullImm{5-0};
1192   let ImmS = FullImm{11-6};
1193
1194   // No disassembler allowed because it would overlap with BFM which does the
1195   // actual work.
1196   let isAsmParserOnly = 1;
1197 }
1198
1199 def LSLwwi : A64I_bitfield_lsl<0b0, GPR32, i32, bitfield32_lsl_imm>;
1200 def LSLxxi : A64I_bitfield_lsl<0b1, GPR64, i64, bitfield64_lsl_imm>;
1201
1202 //===-------------------------------
1203 // 5. Aliases for bitfield extract instructions
1204 //===-------------------------------
1205
1206 def bfx32_width_asmoperand : AsmOperandClass {
1207   let Name = "BFX32Width";
1208   let PredicateMethod = "isBitfieldWidth<32>";
1209   let RenderMethod = "addBFXWidthOperands";
1210   let DiagnosticType = "Width32";
1211 }
1212
1213 def bfx32_width : Operand<i64>, ImmLeaf<i64, [{ return true; }]> {
1214   let PrintMethod = "printBFXWidthOperand";
1215   let ParserMatchClass = bfx32_width_asmoperand;
1216 }
1217
1218 def bfx64_width_asmoperand : AsmOperandClass {
1219   let Name = "BFX64Width";
1220   let PredicateMethod = "isBitfieldWidth<64>";
1221   let RenderMethod = "addBFXWidthOperands";
1222   let DiagnosticType = "Width64";
1223 }
1224
1225 def bfx64_width : Operand<i64> {
1226   let PrintMethod = "printBFXWidthOperand";
1227   let ParserMatchClass = bfx64_width_asmoperand;
1228 }
1229
1230
1231 multiclass A64I_bitfield_extract<bits<2> opc, string asmop, SDNode op> {
1232   def wwii : A64I_bitfield<0b0, opc, 0b0, (outs GPR32:$Rd),
1233                        (ins GPR32:$Rn, bitfield32_imm:$ImmR, bfx32_width:$ImmS),
1234                        !strconcat(asmop, "\t$Rd, $Rn, $ImmR, $ImmS"),
1235                        [(set i32:$Rd, (op i32:$Rn, imm:$ImmR, imm:$ImmS))],
1236                        NoItinerary> {
1237     // As above, no disassembler allowed.
1238     let isAsmParserOnly = 1;
1239   }
1240
1241   def xxii : A64I_bitfield<0b1, opc, 0b1, (outs GPR64:$Rd),
1242                        (ins GPR64:$Rn, bitfield64_imm:$ImmR, bfx64_width:$ImmS),
1243                        !strconcat(asmop, "\t$Rd, $Rn, $ImmR, $ImmS"),
1244                        [(set i64:$Rd, (op i64:$Rn, imm:$ImmR, imm:$ImmS))],
1245                        NoItinerary> {
1246     // As above, no disassembler allowed.
1247     let isAsmParserOnly = 1;
1248   }
1249 }
1250
1251 defm SBFX :  A64I_bitfield_extract<0b00, "sbfx", A64Sbfx>;
1252 defm UBFX :  A64I_bitfield_extract<0b10, "ubfx", A64Ubfx>;
1253
1254 // Again, variants based on BFM modify Rd so need it as an input too.
1255 def BFXILwwii : A64I_bitfield<0b0, 0b01, 0b0, (outs GPR32:$Rd),
1256            (ins GPR32:$src, GPR32:$Rn, bitfield32_imm:$ImmR, bfx32_width:$ImmS),
1257            "bfxil\t$Rd, $Rn, $ImmR, $ImmS", [], NoItinerary> {
1258   // As above, no disassembler allowed.
1259   let isAsmParserOnly = 1;
1260   let Constraints = "$src = $Rd";
1261 }
1262
1263 def BFXILxxii : A64I_bitfield<0b1, 0b01, 0b1, (outs GPR64:$Rd),
1264            (ins GPR64:$src, GPR64:$Rn, bitfield64_imm:$ImmR, bfx64_width:$ImmS),
1265            "bfxil\t$Rd, $Rn, $ImmR, $ImmS", [], NoItinerary> {
1266   // As above, no disassembler allowed.
1267   let isAsmParserOnly = 1;
1268   let Constraints = "$src = $Rd";
1269 }
1270
1271 // SBFX instructions can do a 1-instruction sign-extension of boolean values.
1272 def : Pat<(sext_inreg i64:$Rn, i1), (SBFXxxii $Rn, 0, 0)>;
1273 def : Pat<(sext_inreg i32:$Rn, i1), (SBFXwwii $Rn, 0, 0)>;
1274 def : Pat<(i64 (sext_inreg (anyext i32:$Rn), i1)),
1275           (SBFXxxii (SUBREG_TO_REG (i64 0), $Rn, sub_32), 0, 0)>;
1276
1277 // UBFX makes sense as an implementation of a 64-bit zero-extension too. Could
1278 // use either 64-bit or 32-bit variant, but 32-bit might be more efficient.
1279 def : Pat<(zext i32:$Rn), (SUBREG_TO_REG (i64 0), (UBFXwwii $Rn, 0, 31),
1280                                          sub_32)>;
1281
1282 //===-------------------------------
1283 // 6. Aliases for bitfield insert instructions
1284 //===-------------------------------
1285
1286 def bfi32_lsb_asmoperand : AsmOperandClass {
1287   let Name = "BFI32LSB";
1288   let PredicateMethod = "isUImm<5>";
1289   let RenderMethod = "addBFILSBOperands<32>";
1290   let DiagnosticType = "UImm5";
1291 }
1292
1293 def bfi32_lsb : Operand<i64>,
1294                 ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 31; }]> {
1295   let PrintMethod = "printBFILSBOperand<32>";
1296   let ParserMatchClass = bfi32_lsb_asmoperand;
1297 }
1298
1299 def bfi64_lsb_asmoperand : AsmOperandClass {
1300   let Name = "BFI64LSB";
1301   let PredicateMethod = "isUImm<6>";
1302   let RenderMethod = "addBFILSBOperands<64>";
1303   let DiagnosticType = "UImm6";
1304 }
1305
1306 def bfi64_lsb : Operand<i64>,
1307                 ImmLeaf<i64, [{ return Imm >= 0 && Imm <= 63; }]> {
1308   let PrintMethod = "printBFILSBOperand<64>";
1309   let ParserMatchClass = bfi64_lsb_asmoperand;
1310 }
1311
1312 // Width verification is performed during conversion so width operand can be
1313 // shared between 32/64-bit cases. Still needed for the print method though
1314 // because ImmR encodes "width - 1".
1315 def bfi32_width_asmoperand : AsmOperandClass {
1316   let Name = "BFI32Width";
1317   let PredicateMethod = "isBitfieldWidth<32>";
1318   let RenderMethod = "addBFIWidthOperands";
1319   let DiagnosticType = "Width32";
1320 }
1321
1322 def bfi32_width : Operand<i64>,
1323                   ImmLeaf<i64, [{ return Imm >= 1 && Imm <= 32; }]> {
1324   let PrintMethod = "printBFIWidthOperand";
1325   let ParserMatchClass = bfi32_width_asmoperand;
1326 }
1327
1328 def bfi64_width_asmoperand : AsmOperandClass {
1329   let Name = "BFI64Width";
1330   let PredicateMethod = "isBitfieldWidth<64>";
1331   let RenderMethod = "addBFIWidthOperands";
1332   let DiagnosticType = "Width64";
1333 }
1334
1335 def bfi64_width : Operand<i64>,
1336                   ImmLeaf<i64, [{ return Imm >= 1 && Imm <= 64; }]> {
1337   let PrintMethod = "printBFIWidthOperand";
1338   let ParserMatchClass = bfi64_width_asmoperand;
1339 }
1340
1341 multiclass A64I_bitfield_insert<bits<2> opc, string asmop> {
1342   def wwii : A64I_bitfield<0b0, opc, 0b0, (outs GPR32:$Rd),
1343                            (ins GPR32:$Rn, bfi32_lsb:$ImmR, bfi32_width:$ImmS),
1344                            !strconcat(asmop, "\t$Rd, $Rn, $ImmR, $ImmS"),
1345                            [], NoItinerary> {
1346     // As above, no disassembler allowed.
1347     let isAsmParserOnly = 1;
1348   }
1349
1350   def xxii : A64I_bitfield<0b1, opc, 0b1, (outs GPR64:$Rd),
1351                            (ins GPR64:$Rn, bfi64_lsb:$ImmR, bfi64_width:$ImmS),
1352                            !strconcat(asmop, "\t$Rd, $Rn, $ImmR, $ImmS"),
1353                            [], NoItinerary> {
1354     // As above, no disassembler allowed.
1355     let isAsmParserOnly = 1;
1356   }
1357 }
1358
1359 defm SBFIZ :  A64I_bitfield_insert<0b00, "sbfiz">;
1360 defm UBFIZ :  A64I_bitfield_insert<0b10, "ubfiz">;
1361
1362
1363 def BFIwwii : A64I_bitfield<0b0, 0b01, 0b0, (outs GPR32:$Rd),
1364                 (ins GPR32:$src, GPR32:$Rn, bfi32_lsb:$ImmR, bfi32_width:$ImmS),
1365                 "bfi\t$Rd, $Rn, $ImmR, $ImmS", [], NoItinerary> {
1366   // As above, no disassembler allowed.
1367   let isAsmParserOnly = 1;
1368   let Constraints = "$src = $Rd";
1369 }
1370
1371 def BFIxxii : A64I_bitfield<0b1, 0b01, 0b1, (outs GPR64:$Rd),
1372                 (ins GPR64:$src, GPR64:$Rn, bfi64_lsb:$ImmR, bfi64_width:$ImmS),
1373                 "bfi\t$Rd, $Rn, $ImmR, $ImmS", [], NoItinerary> {
1374   // As above, no disassembler allowed.
1375   let isAsmParserOnly = 1;
1376   let Constraints = "$src = $Rd";
1377 }
1378
1379 //===----------------------------------------------------------------------===//
1380 // Compare and branch (immediate)
1381 //===----------------------------------------------------------------------===//
1382 // Contains: CBZ, CBNZ
1383
1384 class label_asmoperand<int width, int scale> : AsmOperandClass {
1385   let Name = "Label" # width # "_" # scale;
1386   let PredicateMethod = "isLabel<" # width # "," # scale # ">";
1387   let RenderMethod = "addLabelOperands<" # width # ", " # scale # ">";
1388   let DiagnosticType = "Label";
1389 }
1390
1391 def label_wid19_scal4_asmoperand : label_asmoperand<19, 4>;
1392
1393 // All conditional immediate branches are the same really: 19 signed bits scaled
1394 // by the instruction-size (4).
1395 def bcc_target : Operand<OtherVT> {
1396   // This label is a 19-bit offset from PC, scaled by the instruction-width: 4.
1397   let ParserMatchClass = label_wid19_scal4_asmoperand;
1398   let PrintMethod = "printLabelOperand<19, 4>";
1399   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_condbr>";
1400   let OperandType = "OPERAND_PCREL";
1401 }
1402
1403 multiclass cmpbr_sizes<bit op, string asmop, ImmLeaf SETOP> {
1404   let isBranch = 1, isTerminator = 1 in {
1405   def x : A64I_cmpbr<0b1, op,
1406                      (outs),
1407                      (ins GPR64:$Rt, bcc_target:$Label),
1408                      !strconcat(asmop,"\t$Rt, $Label"),
1409                      [(A64br_cc (A64cmp i64:$Rt, 0), SETOP, bb:$Label)],
1410                      NoItinerary>;
1411
1412   def w : A64I_cmpbr<0b0, op,
1413                      (outs),
1414                      (ins GPR32:$Rt, bcc_target:$Label),
1415                      !strconcat(asmop,"\t$Rt, $Label"),
1416                      [(A64br_cc (A64cmp i32:$Rt, 0), SETOP, bb:$Label)],
1417                      NoItinerary>;
1418   }
1419 }
1420
1421 defm CBZ  : cmpbr_sizes<0b0, "cbz",  ImmLeaf<i32, [{
1422   return Imm == A64CC::EQ;
1423 }]> >;
1424 defm CBNZ : cmpbr_sizes<0b1, "cbnz", ImmLeaf<i32, [{
1425   return Imm == A64CC::NE;
1426 }]> >;
1427
1428 //===----------------------------------------------------------------------===//
1429 // Conditional branch (immediate) instructions
1430 //===----------------------------------------------------------------------===//
1431 // Contains: B.cc
1432
1433 def cond_code_asmoperand : AsmOperandClass {
1434   let Name = "CondCode";
1435   let DiagnosticType = "CondCode";
1436 }
1437
1438 def cond_code : Operand<i32>, ImmLeaf<i32, [{
1439   return Imm >= 0 && Imm <= 15;
1440 }]> {
1441   let PrintMethod = "printCondCodeOperand";
1442   let ParserMatchClass = cond_code_asmoperand;
1443 }
1444
1445 def Bcc : A64I_condbr<0b0, 0b0, (outs),
1446                 (ins cond_code:$Cond, bcc_target:$Label),
1447                 "b.$Cond $Label", [(A64br_cc NZCV, (i32 imm:$Cond), bb:$Label)],
1448                 NoItinerary> {
1449   let Uses = [NZCV];
1450   let isBranch = 1;
1451   let isTerminator = 1;
1452 }
1453
1454 //===----------------------------------------------------------------------===//
1455 // Conditional compare (immediate) instructions
1456 //===----------------------------------------------------------------------===//
1457 // Contains: CCMN, CCMP
1458
1459 def uimm4_asmoperand : AsmOperandClass {
1460   let Name = "UImm4";
1461   let PredicateMethod = "isUImm<4>";
1462   let RenderMethod = "addImmOperands";
1463   let DiagnosticType = "UImm4";
1464 }
1465
1466 def uimm4 : Operand<i32> {
1467   let ParserMatchClass = uimm4_asmoperand;
1468 }
1469
1470 def uimm5 : Operand<i32> {
1471   let ParserMatchClass = uimm5_asmoperand;
1472 }
1473
1474 // The only difference between this operand and the one for instructions like
1475 // B.cc is that it's parsed manually. The other get parsed implicitly as part of
1476 // the mnemonic handling.
1477 def cond_code_op_asmoperand : AsmOperandClass {
1478   let Name = "CondCodeOp";
1479   let RenderMethod = "addCondCodeOperands";
1480   let PredicateMethod = "isCondCode";
1481   let ParserMethod = "ParseCondCodeOperand";
1482   let DiagnosticType = "CondCode";
1483 }
1484
1485 def cond_code_op : Operand<i32> {
1486   let PrintMethod = "printCondCodeOperand";
1487   let ParserMatchClass = cond_code_op_asmoperand;
1488 }
1489
1490 class A64I_condcmpimmImpl<bit sf, bit op, RegisterClass GPR, string asmop>
1491   : A64I_condcmpimm<sf, op, 0b0, 0b0, 0b1, (outs),
1492                 (ins GPR:$Rn, uimm5:$UImm5, uimm4:$NZCVImm, cond_code_op:$Cond),
1493                 !strconcat(asmop, "\t$Rn, $UImm5, $NZCVImm, $Cond"),
1494                 [], NoItinerary> {
1495   let Defs = [NZCV];
1496 }
1497
1498 def CCMNwi : A64I_condcmpimmImpl<0b0, 0b0, GPR32, "ccmn">;
1499 def CCMNxi : A64I_condcmpimmImpl<0b1, 0b0, GPR64, "ccmn">;
1500 def CCMPwi : A64I_condcmpimmImpl<0b0, 0b1, GPR32, "ccmp">;
1501 def CCMPxi : A64I_condcmpimmImpl<0b1, 0b1, GPR64, "ccmp">;
1502
1503 //===----------------------------------------------------------------------===//
1504 // Conditional compare (register) instructions
1505 //===----------------------------------------------------------------------===//
1506 // Contains: CCMN, CCMP
1507
1508 class A64I_condcmpregImpl<bit sf, bit op, RegisterClass GPR, string asmop>
1509   : A64I_condcmpreg<sf, op, 0b0, 0b0, 0b1,
1510                     (outs),
1511                     (ins GPR:$Rn, GPR:$Rm, uimm4:$NZCVImm, cond_code_op:$Cond),
1512                     !strconcat(asmop, "\t$Rn, $Rm, $NZCVImm, $Cond"),
1513                     [], NoItinerary> {
1514   let Defs = [NZCV];
1515 }
1516
1517 def CCMNww : A64I_condcmpregImpl<0b0, 0b0, GPR32, "ccmn">;
1518 def CCMNxx : A64I_condcmpregImpl<0b1, 0b0, GPR64, "ccmn">;
1519 def CCMPww : A64I_condcmpregImpl<0b0, 0b1, GPR32, "ccmp">;
1520 def CCMPxx : A64I_condcmpregImpl<0b1, 0b1, GPR64, "ccmp">;
1521
1522 //===----------------------------------------------------------------------===//
1523 // Conditional select instructions
1524 //===----------------------------------------------------------------------===//
1525 // Contains: CSEL, CSINC, CSINV, CSNEG + aliases CSET, CSETM, CINC, CINV, CNEG
1526
1527 // Condition code which is encoded as the inversion (semantically rather than
1528 // bitwise) in the instruction.
1529 def inv_cond_code_op_asmoperand : AsmOperandClass {
1530   let Name = "InvCondCodeOp";
1531   let RenderMethod = "addInvCondCodeOperands";
1532   let PredicateMethod = "isCondCode";
1533   let ParserMethod = "ParseCondCodeOperand";
1534   let DiagnosticType = "CondCode";
1535 }
1536
1537 def inv_cond_code_op : Operand<i32> {
1538   let ParserMatchClass = inv_cond_code_op_asmoperand;
1539 }
1540
1541 // Having a separate operand for the selectable use-case is debatable, but gives
1542 // consistency with cond_code.
1543 def inv_cond_XFORM : SDNodeXForm<imm, [{
1544   A64CC::CondCodes CC = static_cast<A64CC::CondCodes>(N->getZExtValue());
1545   return CurDAG->getTargetConstant(A64InvertCondCode(CC), MVT::i32);
1546 }]>;
1547
1548 def inv_cond_code
1549   : ImmLeaf<i32, [{ return Imm >= 0 && Imm <= 15; }], inv_cond_XFORM>;
1550
1551
1552 multiclass A64I_condselSizes<bit op, bits<2> op2, string asmop,
1553                              SDPatternOperator select> {
1554   let Uses = [NZCV] in {
1555     def wwwc : A64I_condsel<0b0, op, 0b0, op2,
1556                             (outs GPR32:$Rd),
1557                             (ins GPR32:$Rn, GPR32:$Rm, cond_code_op:$Cond),
1558                             !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Cond"),
1559                             [(set i32:$Rd, (select i32:$Rn, i32:$Rm))],
1560                             NoItinerary>;
1561
1562
1563     def xxxc : A64I_condsel<0b1, op, 0b0, op2,
1564                             (outs GPR64:$Rd),
1565                             (ins GPR64:$Rn, GPR64:$Rm, cond_code_op:$Cond),
1566                             !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Cond"),
1567                             [(set i64:$Rd, (select i64:$Rn, i64:$Rm))],
1568                             NoItinerary>;
1569   }
1570 }
1571
1572 def simple_select
1573   : PatFrag<(ops node:$lhs, node:$rhs),
1574             (A64select_cc NZCV, node:$lhs, node:$rhs, (i32 imm:$Cond))>;
1575
1576 class complex_select<SDPatternOperator opnode>
1577   : PatFrag<(ops node:$lhs, node:$rhs),
1578         (A64select_cc NZCV, node:$lhs, (opnode node:$rhs), (i32 imm:$Cond))>;
1579
1580
1581 defm CSEL : A64I_condselSizes<0b0, 0b00, "csel", simple_select>;
1582 defm CSINC : A64I_condselSizes<0b0, 0b01, "csinc",
1583                                complex_select<PatFrag<(ops node:$val),
1584                                                       (add node:$val, 1)>>>;
1585 defm CSINV : A64I_condselSizes<0b1, 0b00, "csinv", complex_select<not>>;
1586 defm CSNEG : A64I_condselSizes<0b1, 0b01, "csneg", complex_select<ineg>>;
1587
1588 // Now the instruction aliases, which fit nicely into LLVM's model:
1589
1590 def : InstAlias<"cset $Rd, $Cond",
1591                 (CSINCwwwc GPR32:$Rd, WZR, WZR, inv_cond_code_op:$Cond)>;
1592 def : InstAlias<"cset $Rd, $Cond",
1593                 (CSINCxxxc GPR64:$Rd, XZR, XZR, inv_cond_code_op:$Cond)>;
1594 def : InstAlias<"csetm $Rd, $Cond",
1595                 (CSINVwwwc GPR32:$Rd, WZR, WZR, inv_cond_code_op:$Cond)>;
1596 def : InstAlias<"csetm $Rd, $Cond",
1597                 (CSINVxxxc GPR64:$Rd, XZR, XZR, inv_cond_code_op:$Cond)>;
1598 def : InstAlias<"cinc $Rd, $Rn, $Cond",
1599            (CSINCwwwc GPR32:$Rd, GPR32:$Rn, GPR32:$Rn, inv_cond_code_op:$Cond)>;
1600 def : InstAlias<"cinc $Rd, $Rn, $Cond",
1601            (CSINCxxxc GPR64:$Rd, GPR64:$Rn, GPR64:$Rn, inv_cond_code_op:$Cond)>;
1602 def : InstAlias<"cinv $Rd, $Rn, $Cond",
1603            (CSINVwwwc GPR32:$Rd, GPR32:$Rn, GPR32:$Rn, inv_cond_code_op:$Cond)>;
1604 def : InstAlias<"cinv $Rd, $Rn, $Cond",
1605            (CSINVxxxc GPR64:$Rd, GPR64:$Rn, GPR64:$Rn, inv_cond_code_op:$Cond)>;
1606 def : InstAlias<"cneg $Rd, $Rn, $Cond",
1607            (CSNEGwwwc GPR32:$Rd, GPR32:$Rn, GPR32:$Rn, inv_cond_code_op:$Cond)>;
1608 def : InstAlias<"cneg $Rd, $Rn, $Cond",
1609            (CSNEGxxxc GPR64:$Rd, GPR64:$Rn, GPR64:$Rn, inv_cond_code_op:$Cond)>;
1610
1611 // Finally some helper patterns.
1612
1613 // For CSET (a.k.a. zero-extension of icmp)
1614 def : Pat<(A64select_cc NZCV, 0, 1, cond_code:$Cond),
1615           (CSINCwwwc WZR, WZR, cond_code:$Cond)>;
1616 def : Pat<(A64select_cc NZCV, 1, 0, inv_cond_code:$Cond),
1617           (CSINCwwwc WZR, WZR, inv_cond_code:$Cond)>;
1618
1619 def : Pat<(A64select_cc NZCV, 0, 1, cond_code:$Cond),
1620           (CSINCxxxc XZR, XZR, cond_code:$Cond)>;
1621 def : Pat<(A64select_cc NZCV, 1, 0, inv_cond_code:$Cond),
1622           (CSINCxxxc XZR, XZR, inv_cond_code:$Cond)>;
1623
1624 // For CSETM (a.k.a. sign-extension of icmp)
1625 def : Pat<(A64select_cc NZCV, 0, -1, cond_code:$Cond),
1626           (CSINVwwwc WZR, WZR, cond_code:$Cond)>;
1627 def : Pat<(A64select_cc NZCV, -1, 0, inv_cond_code:$Cond),
1628           (CSINVwwwc WZR, WZR, inv_cond_code:$Cond)>;
1629
1630 def : Pat<(A64select_cc NZCV, 0, -1, cond_code:$Cond),
1631           (CSINVxxxc XZR, XZR, cond_code:$Cond)>;
1632 def : Pat<(A64select_cc NZCV, -1, 0, inv_cond_code:$Cond),
1633           (CSINVxxxc XZR, XZR, inv_cond_code:$Cond)>;
1634
1635 // CINC, CINV and CNEG get dealt with automatically, which leaves the issue of
1636 // commutativity. The instructions are to complex for isCommutable to be used,
1637 // so we have to create the patterns manually:
1638
1639 // No commutable pattern for CSEL since the commuted version is isomorphic.
1640
1641 // CSINC
1642 def :Pat<(A64select_cc NZCV, (add i32:$Rm, 1), i32:$Rn, inv_cond_code:$Cond),
1643          (CSINCwwwc $Rn, $Rm, inv_cond_code:$Cond)>;
1644 def :Pat<(A64select_cc NZCV, (add i64:$Rm, 1), i64:$Rn, inv_cond_code:$Cond),
1645          (CSINCxxxc $Rn, $Rm, inv_cond_code:$Cond)>;
1646
1647 // CSINV
1648 def :Pat<(A64select_cc NZCV, (not i32:$Rm), i32:$Rn, inv_cond_code:$Cond),
1649          (CSINVwwwc $Rn, $Rm, inv_cond_code:$Cond)>;
1650 def :Pat<(A64select_cc NZCV, (not i64:$Rm), i64:$Rn, inv_cond_code:$Cond),
1651          (CSINVxxxc $Rn, $Rm, inv_cond_code:$Cond)>;
1652
1653 // CSNEG
1654 def :Pat<(A64select_cc NZCV, (ineg i32:$Rm), i32:$Rn, inv_cond_code:$Cond),
1655          (CSNEGwwwc $Rn, $Rm, inv_cond_code:$Cond)>;
1656 def :Pat<(A64select_cc NZCV, (ineg i64:$Rm), i64:$Rn, inv_cond_code:$Cond),
1657          (CSNEGxxxc $Rn, $Rm, inv_cond_code:$Cond)>;
1658
1659 //===----------------------------------------------------------------------===//
1660 // Data Processing (1 source) instructions
1661 //===----------------------------------------------------------------------===//
1662 // Contains: RBIT, REV16, REV, REV32, CLZ, CLS.
1663
1664 // We define an unary operator which always fails. We will use this to
1665 // define unary operators that cannot be matched.
1666
1667 class A64I_dp_1src_impl<bit sf, bits<6> opcode, string asmop,
1668                    list<dag> patterns, RegisterClass GPRrc,
1669                    InstrItinClass itin>:
1670       A64I_dp_1src<sf,
1671                    0,
1672                    0b00000,
1673                    opcode,
1674                    !strconcat(asmop, "\t$Rd, $Rn"),
1675                    (outs GPRrc:$Rd),
1676                    (ins GPRrc:$Rn),
1677                    patterns,
1678                    itin>;
1679
1680 multiclass A64I_dp_1src <bits<6> opcode, string asmop> {
1681   let hasSideEffects = 0 in {
1682     def ww : A64I_dp_1src_impl<0b0, opcode, asmop, [], GPR32, NoItinerary>;
1683     def xx : A64I_dp_1src_impl<0b1, opcode, asmop, [], GPR64, NoItinerary>;
1684   }
1685 }
1686
1687 defm RBIT  : A64I_dp_1src<0b000000, "rbit">;
1688 defm CLS   : A64I_dp_1src<0b000101, "cls">;
1689 defm CLZ   : A64I_dp_1src<0b000100, "clz">;
1690
1691 def : Pat<(ctlz i32:$Rn), (CLZww $Rn)>;
1692 def : Pat<(ctlz i64:$Rn), (CLZxx $Rn)>;
1693 def : Pat<(ctlz_zero_undef i32:$Rn), (CLZww $Rn)>;
1694 def : Pat<(ctlz_zero_undef i64:$Rn), (CLZxx $Rn)>;
1695
1696 def : Pat<(cttz i32:$Rn), (CLZww (RBITww $Rn))>;
1697 def : Pat<(cttz i64:$Rn), (CLZxx (RBITxx $Rn))>;
1698 def : Pat<(cttz_zero_undef i32:$Rn), (CLZww (RBITww $Rn))>;
1699 def : Pat<(cttz_zero_undef i64:$Rn), (CLZxx (RBITxx $Rn))>;
1700
1701
1702 def REVww : A64I_dp_1src_impl<0b0, 0b000010, "rev",
1703                               [(set i32:$Rd, (bswap i32:$Rn))],
1704                               GPR32, NoItinerary>;
1705 def REVxx : A64I_dp_1src_impl<0b1, 0b000011, "rev",
1706                               [(set i64:$Rd, (bswap i64:$Rn))],
1707                               GPR64, NoItinerary>;
1708 def REV32xx : A64I_dp_1src_impl<0b1, 0b000010, "rev32",
1709                           [(set i64:$Rd, (bswap (rotr i64:$Rn, (i64 32))))],
1710                           GPR64, NoItinerary>;
1711 def REV16ww : A64I_dp_1src_impl<0b0, 0b000001, "rev16",
1712                           [(set i32:$Rd, (bswap (rotr i32:$Rn, (i64 16))))],
1713                           GPR32,
1714                           NoItinerary>;
1715 def REV16xx : A64I_dp_1src_impl<0b1, 0b000001, "rev16", [], GPR64, NoItinerary>;
1716
1717 //===----------------------------------------------------------------------===//
1718 // Data Processing (2 sources) instructions
1719 //===----------------------------------------------------------------------===//
1720 // Contains: CRC32C?[BHWX], UDIV, SDIV, LSLV, LSRV, ASRV, RORV + aliases LSL,
1721 //           LSR, ASR, ROR
1722
1723
1724 class dp_2src_impl<bit sf, bits<6> opcode, string asmop, list<dag> patterns,
1725                    RegisterClass GPRsp,
1726                    InstrItinClass itin>:
1727       A64I_dp_2src<sf,
1728                    opcode,
1729                    0,
1730                    !strconcat(asmop, "\t$Rd, $Rn, $Rm"),
1731                    (outs GPRsp:$Rd),
1732                    (ins GPRsp:$Rn, GPRsp:$Rm),
1733                    patterns,
1734                    itin>;
1735
1736 multiclass dp_2src_crc<bit c, string asmop> {
1737   def B_www : dp_2src_impl<0b0, {0, 1, 0, c, 0, 0},
1738                            !strconcat(asmop, "b"), [], GPR32, NoItinerary>;
1739   def H_www : dp_2src_impl<0b0, {0, 1, 0, c, 0, 1},
1740                            !strconcat(asmop, "h"), [], GPR32, NoItinerary>;
1741   def W_www : dp_2src_impl<0b0, {0, 1, 0, c, 1, 0},
1742                            !strconcat(asmop, "w"), [], GPR32, NoItinerary>;
1743   def X_wwx : A64I_dp_2src<0b1, {0, 1, 0, c, 1, 1}, 0b0,
1744                            !strconcat(asmop, "x\t$Rd, $Rn, $Rm"),
1745                            (outs GPR32:$Rd), (ins GPR32:$Rn, GPR64:$Rm), [],
1746                            NoItinerary>;
1747 }
1748
1749 multiclass dp_2src_zext <bits<6> opcode, string asmop, SDPatternOperator op> {
1750    def www : dp_2src_impl<0b0,
1751                          opcode,
1752                          asmop,
1753                          [(set i32:$Rd,
1754                                (op i32:$Rn, (i64 (zext i32:$Rm))))],
1755                          GPR32,
1756                          NoItinerary>;
1757    def xxx : dp_2src_impl<0b1,
1758                          opcode,
1759                          asmop,
1760                          [(set i64:$Rd, (op i64:$Rn, i64:$Rm))],
1761                          GPR64,
1762                          NoItinerary>;
1763 }
1764
1765
1766 multiclass dp_2src <bits<6> opcode, string asmop, SDPatternOperator op> {
1767     def www : dp_2src_impl<0b0,
1768                          opcode,
1769                          asmop,
1770                          [(set i32:$Rd, (op i32:$Rn, i32:$Rm))],
1771                          GPR32,
1772                          NoItinerary>;
1773    def xxx : dp_2src_impl<0b1,
1774                          opcode,
1775                          asmop,
1776                          [(set i64:$Rd, (op i64:$Rn, i64:$Rm))],
1777                          GPR64,
1778                          NoItinerary>;
1779 }
1780
1781 // Here we define the data processing 2 source instructions.
1782 defm CRC32  : dp_2src_crc<0b0, "crc32">;
1783 defm CRC32C : dp_2src_crc<0b1, "crc32c">;
1784
1785 defm UDIV : dp_2src<0b000010, "udiv", udiv>;
1786 defm SDIV : dp_2src<0b000011, "sdiv", sdiv>;
1787
1788 defm LSLV : dp_2src_zext<0b001000, "lsl", shl>;
1789 defm LSRV : dp_2src_zext<0b001001, "lsr", srl>;
1790 defm ASRV : dp_2src_zext<0b001010, "asr", sra>;
1791 defm RORV : dp_2src_zext<0b001011, "ror", rotr>;
1792
1793 // Extra patterns for an incoming 64-bit value for a 32-bit
1794 // operation. Since the LLVM operations are undefined (as in C) if the
1795 // RHS is out of range, it's perfectly permissible to discard the high
1796 // bits of the GPR64.
1797 def : Pat<(shl i32:$Rn, i64:$Rm),
1798           (LSLVwww $Rn, (EXTRACT_SUBREG $Rm, sub_32))>;
1799 def : Pat<(srl i32:$Rn, i64:$Rm),
1800           (LSRVwww $Rn, (EXTRACT_SUBREG $Rm, sub_32))>;
1801 def : Pat<(sra i32:$Rn, i64:$Rm),
1802           (ASRVwww $Rn, (EXTRACT_SUBREG $Rm, sub_32))>;
1803 def : Pat<(rotr i32:$Rn, i64:$Rm),
1804           (RORVwww $Rn, (EXTRACT_SUBREG $Rm, sub_32))>;
1805
1806 // Here we define the aliases for the data processing 2 source instructions.
1807 def LSL_mnemonic : MnemonicAlias<"lslv", "lsl">;
1808 def LSR_mnemonic : MnemonicAlias<"lsrv", "lsr">;
1809 def ASR_menmonic : MnemonicAlias<"asrv", "asr">;
1810 def ROR_menmonic : MnemonicAlias<"rorv", "ror">;
1811
1812 //===----------------------------------------------------------------------===//
1813 // Data Processing (3 sources) instructions
1814 //===----------------------------------------------------------------------===//
1815 // Contains: MADD, MSUB, SMADDL, SMSUBL, SMULH, UMADDL, UMSUBL, UMULH
1816 //    + aliases MUL, MNEG, SMULL, SMNEGL, UMULL, UMNEGL
1817
1818 class A64I_dp3_4operand<bit sf, bits<6> opcode, RegisterClass AccReg,
1819                         ValueType AccTy, RegisterClass SrcReg,
1820                         string asmop, dag pattern>
1821   : A64I_dp3<sf, opcode,
1822              (outs AccReg:$Rd), (ins SrcReg:$Rn, SrcReg:$Rm, AccReg:$Ra),
1823              !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Ra"),
1824              [(set AccTy:$Rd, pattern)], NoItinerary> {
1825   RegisterClass AccGPR = AccReg;
1826   RegisterClass SrcGPR = SrcReg;
1827 }
1828
1829 def MADDwwww : A64I_dp3_4operand<0b0, 0b000000, GPR32, i32, GPR32, "madd",
1830                                  (add i32:$Ra, (mul i32:$Rn, i32:$Rm))>;
1831 def MADDxxxx : A64I_dp3_4operand<0b1, 0b000000, GPR64, i64, GPR64, "madd",
1832                                  (add i64:$Ra, (mul i64:$Rn, i64:$Rm))>;
1833
1834 def MSUBwwww : A64I_dp3_4operand<0b0, 0b000001, GPR32, i32, GPR32, "msub",
1835                                  (sub i32:$Ra, (mul i32:$Rn, i32:$Rm))>;
1836 def MSUBxxxx : A64I_dp3_4operand<0b1, 0b000001, GPR64, i64, GPR64, "msub",
1837                                  (sub i64:$Ra, (mul i64:$Rn, i64:$Rm))>;
1838
1839 def SMADDLxwwx : A64I_dp3_4operand<0b1, 0b000010, GPR64, i64, GPR32, "smaddl",
1840                      (add i64:$Ra, (mul (i64 (sext i32:$Rn)), (sext i32:$Rm)))>;
1841 def SMSUBLxwwx : A64I_dp3_4operand<0b1, 0b000011, GPR64, i64, GPR32, "smsubl",
1842                      (sub i64:$Ra, (mul (i64 (sext i32:$Rn)), (sext i32:$Rm)))>;
1843
1844 def UMADDLxwwx : A64I_dp3_4operand<0b1, 0b001010, GPR64, i64, GPR32, "umaddl",
1845                      (add i64:$Ra, (mul (i64 (zext i32:$Rn)), (zext i32:$Rm)))>;
1846 def UMSUBLxwwx : A64I_dp3_4operand<0b1, 0b001011, GPR64, i64, GPR32, "umsubl",
1847                      (sub i64:$Ra, (mul (i64 (zext i32:$Rn)), (zext i32:$Rm)))>;
1848
1849 let isCommutable = 1, PostEncoderMethod = "fixMulHigh" in {
1850   def UMULHxxx : A64I_dp3<0b1, 0b001100, (outs GPR64:$Rd),
1851                           (ins GPR64:$Rn, GPR64:$Rm),
1852                           "umulh\t$Rd, $Rn, $Rm",
1853                           [(set i64:$Rd, (mulhu i64:$Rn, i64:$Rm))],
1854                           NoItinerary>;
1855
1856   def SMULHxxx : A64I_dp3<0b1, 0b000100, (outs GPR64:$Rd),
1857                           (ins GPR64:$Rn, GPR64:$Rm),
1858                           "smulh\t$Rd, $Rn, $Rm",
1859                           [(set i64:$Rd, (mulhs i64:$Rn, i64:$Rm))],
1860                           NoItinerary>;
1861 }
1862
1863 multiclass A64I_dp3_3operand<string asmop, A64I_dp3_4operand INST,
1864                              Register ZR, dag pattern> {
1865   def : InstAlias<asmop # " $Rd, $Rn, $Rm",
1866                   (INST INST.AccGPR:$Rd, INST.SrcGPR:$Rn, INST.SrcGPR:$Rm, ZR)>;
1867
1868   def : Pat<pattern, (INST $Rn, $Rm, ZR)>;
1869 }
1870
1871 defm : A64I_dp3_3operand<"mul", MADDwwww, WZR, (mul i32:$Rn, i32:$Rm)>;
1872 defm : A64I_dp3_3operand<"mul", MADDxxxx, XZR, (mul i64:$Rn, i64:$Rm)>;
1873
1874 defm : A64I_dp3_3operand<"mneg", MSUBwwww, WZR,
1875                          (sub 0, (mul i32:$Rn, i32:$Rm))>;
1876 defm : A64I_dp3_3operand<"mneg", MSUBxxxx, XZR,
1877                          (sub 0, (mul i64:$Rn, i64:$Rm))>;
1878
1879 defm : A64I_dp3_3operand<"smull", SMADDLxwwx, XZR,
1880                          (mul (i64 (sext i32:$Rn)), (sext i32:$Rm))>;
1881 defm : A64I_dp3_3operand<"smnegl", SMSUBLxwwx, XZR,
1882                        (sub 0, (mul (i64 (sext i32:$Rn)), (sext i32:$Rm)))>;
1883
1884 defm : A64I_dp3_3operand<"umull", UMADDLxwwx, XZR,
1885                          (mul (i64 (zext i32:$Rn)), (zext i32:$Rm))>;
1886 defm : A64I_dp3_3operand<"umnegl", UMSUBLxwwx, XZR,
1887                        (sub 0, (mul (i64 (zext i32:$Rn)), (zext i32:$Rm)))>;
1888
1889
1890 //===----------------------------------------------------------------------===//
1891 // Exception generation
1892 //===----------------------------------------------------------------------===//
1893 // Contains: SVC, HVC, SMC, BRK, HLT, DCPS1, DCPS2, DCPS3
1894
1895 def uimm16_asmoperand : AsmOperandClass {
1896   let Name = "UImm16";
1897   let PredicateMethod = "isUImm<16>";
1898   let RenderMethod = "addImmOperands";
1899   let DiagnosticType = "UImm16";
1900 }
1901
1902 def uimm16 : Operand<i32> {
1903   let ParserMatchClass = uimm16_asmoperand;
1904 }
1905
1906 class A64I_exceptImpl<bits<3> opc, bits<2> ll, string asmop>
1907   : A64I_exception<opc, 0b000, ll, (outs), (ins uimm16:$UImm16),
1908                    !strconcat(asmop, "\t$UImm16"), [], NoItinerary> {
1909   let isBranch = 1;
1910   let isTerminator = 1;
1911 }
1912
1913 def SVCi : A64I_exceptImpl<0b000, 0b01, "svc">;
1914 def HVCi : A64I_exceptImpl<0b000, 0b10, "hvc">;
1915 def SMCi : A64I_exceptImpl<0b000, 0b11, "smc">;
1916 def BRKi : A64I_exceptImpl<0b001, 0b00, "brk">;
1917 def HLTi : A64I_exceptImpl<0b010, 0b00, "hlt">;
1918
1919 def DCPS1i : A64I_exceptImpl<0b101, 0b01, "dcps1">;
1920 def DCPS2i : A64I_exceptImpl<0b101, 0b10, "dcps2">;
1921 def DCPS3i : A64I_exceptImpl<0b101, 0b11, "dcps3">;
1922
1923 // The immediate is optional for the DCPS instructions, defaulting to 0.
1924 def : InstAlias<"dcps1", (DCPS1i 0)>;
1925 def : InstAlias<"dcps2", (DCPS2i 0)>;
1926 def : InstAlias<"dcps3", (DCPS3i 0)>;
1927
1928 //===----------------------------------------------------------------------===//
1929 // Extract (immediate)
1930 //===----------------------------------------------------------------------===//
1931 // Contains: EXTR + alias ROR
1932
1933 def EXTRwwwi : A64I_extract<0b0, 0b000, 0b0,
1934                             (outs GPR32:$Rd),
1935                             (ins GPR32:$Rn, GPR32:$Rm, bitfield32_imm:$LSB),
1936                             "extr\t$Rd, $Rn, $Rm, $LSB",
1937                             [(set i32:$Rd,
1938                                   (A64Extr i32:$Rn, i32:$Rm, imm:$LSB))],
1939                             NoItinerary>;
1940 def EXTRxxxi : A64I_extract<0b1, 0b000, 0b1,
1941                             (outs GPR64:$Rd),
1942                             (ins GPR64:$Rn, GPR64:$Rm, bitfield64_imm:$LSB),
1943                             "extr\t$Rd, $Rn, $Rm, $LSB",
1944                             [(set i64:$Rd,
1945                                   (A64Extr i64:$Rn, i64:$Rm, imm:$LSB))],
1946                             NoItinerary>;
1947
1948 def : InstAlias<"ror $Rd, $Rs, $LSB",
1949                (EXTRwwwi GPR32:$Rd, GPR32:$Rs, GPR32:$Rs, bitfield32_imm:$LSB)>;
1950 def : InstAlias<"ror $Rd, $Rs, $LSB",
1951                (EXTRxxxi GPR64:$Rd, GPR64:$Rs, GPR64:$Rs, bitfield64_imm:$LSB)>;
1952
1953 def : Pat<(rotr i32:$Rn, bitfield32_imm:$LSB),
1954           (EXTRwwwi $Rn, $Rn, bitfield32_imm:$LSB)>;
1955 def : Pat<(rotr i64:$Rn, bitfield64_imm:$LSB),
1956           (EXTRxxxi $Rn, $Rn, bitfield64_imm:$LSB)>;
1957
1958 //===----------------------------------------------------------------------===//
1959 // Floating-point compare instructions
1960 //===----------------------------------------------------------------------===//
1961 // Contains: FCMP, FCMPE
1962
1963 def fpzero_asmoperand : AsmOperandClass {
1964   let Name = "FPZero";
1965   let ParserMethod = "ParseFPImmOperand";
1966   let DiagnosticType = "FPZero";
1967 }
1968
1969 def fpz32 : Operand<f32>,
1970             ComplexPattern<f32, 1, "SelectFPZeroOperand", [fpimm]> {
1971   let ParserMatchClass = fpzero_asmoperand;
1972   let PrintMethod = "printFPZeroOperand";
1973   let DecoderMethod = "DecodeFPZeroOperand";
1974 }
1975
1976 def fpz64 : Operand<f64>,
1977             ComplexPattern<f64, 1, "SelectFPZeroOperand", [fpimm]> {
1978   let ParserMatchClass = fpzero_asmoperand;
1979   let PrintMethod = "printFPZeroOperand";
1980   let DecoderMethod = "DecodeFPZeroOperand";
1981 }
1982
1983 def fpz64movi : Operand<i64>,
1984             ComplexPattern<f64, 1, "SelectFPZeroOperand", [fpimm]> {
1985   let ParserMatchClass = fpzero_asmoperand;
1986   let PrintMethod = "printFPZeroOperand";
1987   let DecoderMethod = "DecodeFPZeroOperand";
1988 }
1989
1990 multiclass A64I_fpcmpSignal<bits<2> type, bit imm, dag ins, dag pattern> {
1991   def _quiet : A64I_fpcmp<0b0, 0b0, type, 0b00, {0b0, imm, 0b0, 0b0, 0b0},
1992                           (outs), ins, "fcmp\t$Rn, $Rm", [pattern],
1993                           NoItinerary> {
1994     let Defs = [NZCV];
1995   }
1996
1997   def _sig : A64I_fpcmp<0b0, 0b0, type, 0b00, {0b1, imm, 0b0, 0b0, 0b0},
1998                         (outs), ins, "fcmpe\t$Rn, $Rm", [], NoItinerary> {
1999     let Defs = [NZCV];
2000   }
2001 }
2002
2003 defm FCMPss : A64I_fpcmpSignal<0b00, 0b0, (ins FPR32:$Rn, FPR32:$Rm),
2004                                (set NZCV, (A64cmp f32:$Rn, f32:$Rm))>;
2005 defm FCMPdd : A64I_fpcmpSignal<0b01, 0b0, (ins FPR64:$Rn, FPR64:$Rm),
2006                                (set NZCV, (A64cmp f64:$Rn, f64:$Rm))>;
2007
2008 // What would be Rm should be written as 0; note that even though it's called
2009 // "$Rm" here to fit in with the InstrFormats, it's actually an immediate.
2010 defm FCMPsi : A64I_fpcmpSignal<0b00, 0b1, (ins FPR32:$Rn, fpz32:$Rm),
2011                                (set NZCV, (A64cmp f32:$Rn, fpz32:$Rm))>;
2012
2013 defm FCMPdi : A64I_fpcmpSignal<0b01, 0b1, (ins FPR64:$Rn, fpz64:$Rm),
2014                                (set NZCV, (A64cmp f64:$Rn, fpz64:$Rm))>;
2015
2016
2017 //===----------------------------------------------------------------------===//
2018 // Floating-point conditional compare instructions
2019 //===----------------------------------------------------------------------===//
2020 // Contains: FCCMP, FCCMPE
2021
2022 class A64I_fpccmpImpl<bits<2> type, bit op, RegisterClass FPR, string asmop>
2023   : A64I_fpccmp<0b0, 0b0, type, op,
2024                 (outs),
2025                 (ins FPR:$Rn, FPR:$Rm, uimm4:$NZCVImm, cond_code_op:$Cond),
2026                 !strconcat(asmop, "\t$Rn, $Rm, $NZCVImm, $Cond"),
2027                 [], NoItinerary> {
2028   let Defs = [NZCV];
2029 }
2030
2031 def FCCMPss : A64I_fpccmpImpl<0b00, 0b0, FPR32, "fccmp">;
2032 def FCCMPEss : A64I_fpccmpImpl<0b00, 0b1, FPR32, "fccmpe">;
2033 def FCCMPdd : A64I_fpccmpImpl<0b01, 0b0, FPR64, "fccmp">;
2034 def FCCMPEdd : A64I_fpccmpImpl<0b01, 0b1, FPR64, "fccmpe">;
2035
2036 //===----------------------------------------------------------------------===//
2037 // Floating-point conditional select instructions
2038 //===----------------------------------------------------------------------===//
2039 // Contains: FCSEL
2040
2041 let Uses = [NZCV] in {
2042   def FCSELsssc : A64I_fpcondsel<0b0, 0b0, 0b00, (outs FPR32:$Rd),
2043                                  (ins FPR32:$Rn, FPR32:$Rm, cond_code_op:$Cond),
2044                                  "fcsel\t$Rd, $Rn, $Rm, $Cond",
2045                                  [(set f32:$Rd, 
2046                                        (simple_select f32:$Rn, f32:$Rm))],
2047                                  NoItinerary>;
2048
2049
2050   def FCSELdddc : A64I_fpcondsel<0b0, 0b0, 0b01, (outs FPR64:$Rd),
2051                                  (ins FPR64:$Rn, FPR64:$Rm, cond_code_op:$Cond),
2052                                  "fcsel\t$Rd, $Rn, $Rm, $Cond",
2053                                  [(set f64:$Rd,
2054                                        (simple_select f64:$Rn, f64:$Rm))],
2055                                  NoItinerary>;
2056 }
2057
2058 //===----------------------------------------------------------------------===//
2059 // Floating-point data-processing (1 source)
2060 //===----------------------------------------------------------------------===//
2061 // Contains: FMOV, FABS, FNEG, FSQRT, FCVT, FRINT[NPMZAXI].
2062
2063 def FPNoUnop : PatFrag<(ops node:$val), (fneg node:$val),
2064                        [{ (void)N; return false; }]>;
2065
2066 // First we do the fairly trivial bunch with uniform "OP s, s" and "OP d, d"
2067 // syntax. Default to no pattern because most are odd enough not to have one.
2068 multiclass A64I_fpdp1sizes<bits<6> opcode, string asmstr,
2069                            SDPatternOperator opnode = FPNoUnop> {
2070   def ss : A64I_fpdp1<0b0, 0b0, 0b00, opcode, (outs FPR32:$Rd), (ins FPR32:$Rn),
2071                      !strconcat(asmstr, "\t$Rd, $Rn"),
2072                      [(set f32:$Rd, (opnode f32:$Rn))],
2073                      NoItinerary>;
2074
2075   def dd : A64I_fpdp1<0b0, 0b0, 0b01, opcode, (outs FPR64:$Rd), (ins FPR64:$Rn),
2076                      !strconcat(asmstr, "\t$Rd, $Rn"),
2077                      [(set f64:$Rd, (opnode f64:$Rn))],
2078                      NoItinerary>;
2079 }
2080
2081 defm FMOV   : A64I_fpdp1sizes<0b000000, "fmov">;
2082 defm FABS   : A64I_fpdp1sizes<0b000001, "fabs", fabs>;
2083 defm FNEG   : A64I_fpdp1sizes<0b000010, "fneg", fneg>;
2084 defm FSQRT  : A64I_fpdp1sizes<0b000011, "fsqrt", fsqrt>;
2085
2086 defm FRINTN : A64I_fpdp1sizes<0b001000, "frintn">;
2087 defm FRINTP : A64I_fpdp1sizes<0b001001, "frintp", fceil>;
2088 defm FRINTM : A64I_fpdp1sizes<0b001010, "frintm", ffloor>;
2089 defm FRINTZ : A64I_fpdp1sizes<0b001011, "frintz", ftrunc>;
2090 defm FRINTA : A64I_fpdp1sizes<0b001100, "frinta">;
2091 defm FRINTX : A64I_fpdp1sizes<0b001110, "frintx", frint>;
2092 defm FRINTI : A64I_fpdp1sizes<0b001111, "frinti", fnearbyint>;
2093
2094 // The FCVT instrucitons have different source and destination register-types,
2095 // but the fields are uniform everywhere a D-register (say) crops up. Package
2096 // this information in a Record.
2097 class FCVTRegType<RegisterClass rc, bits<2> fld, ValueType vt> {
2098     RegisterClass Class = rc;
2099     ValueType VT = vt;
2100     bit t1 = fld{1};
2101     bit t0 = fld{0};
2102 }
2103
2104 def FCVT16 : FCVTRegType<FPR16, 0b11, f16>;
2105 def FCVT32 : FCVTRegType<FPR32, 0b00, f32>;
2106 def FCVT64 : FCVTRegType<FPR64, 0b01, f64>;
2107
2108 class A64I_fpdp1_fcvt<FCVTRegType DestReg, FCVTRegType SrcReg, SDNode opnode>
2109   : A64I_fpdp1<0b0, 0b0, {SrcReg.t1, SrcReg.t0},
2110                {0,0,0,1, DestReg.t1, DestReg.t0},
2111                (outs DestReg.Class:$Rd), (ins SrcReg.Class:$Rn),
2112                "fcvt\t$Rd, $Rn",
2113                [(set DestReg.VT:$Rd, (opnode SrcReg.VT:$Rn))], NoItinerary>;
2114
2115 def FCVTds : A64I_fpdp1_fcvt<FCVT64, FCVT32, fextend>;
2116 def FCVThs : A64I_fpdp1_fcvt<FCVT16, FCVT32, fround>;
2117 def FCVTsd : A64I_fpdp1_fcvt<FCVT32, FCVT64, fround>;
2118 def FCVThd : A64I_fpdp1_fcvt<FCVT16, FCVT64, fround>;
2119 def FCVTsh : A64I_fpdp1_fcvt<FCVT32, FCVT16, fextend>;
2120 def FCVTdh : A64I_fpdp1_fcvt<FCVT64, FCVT16, fextend>;
2121
2122
2123 //===----------------------------------------------------------------------===//
2124 // Floating-point data-processing (2 sources) instructions
2125 //===----------------------------------------------------------------------===//
2126 // Contains: FMUL, FDIV, FADD, FSUB, FMAX, FMIN, FMAXNM, FMINNM, FNMUL
2127
2128 def FPNoBinop : PatFrag<(ops node:$lhs, node:$rhs), (fadd node:$lhs, node:$rhs),
2129                       [{ (void)N; return false; }]>;
2130
2131 multiclass A64I_fpdp2sizes<bits<4> opcode, string asmstr,
2132                            SDPatternOperator opnode> {
2133   def sss : A64I_fpdp2<0b0, 0b0, 0b00, opcode,
2134                       (outs FPR32:$Rd),
2135                       (ins FPR32:$Rn, FPR32:$Rm),
2136                       !strconcat(asmstr, "\t$Rd, $Rn, $Rm"),
2137                       [(set f32:$Rd, (opnode f32:$Rn, f32:$Rm))],
2138                       NoItinerary>;
2139
2140   def ddd : A64I_fpdp2<0b0, 0b0, 0b01, opcode,
2141                       (outs FPR64:$Rd),
2142                       (ins FPR64:$Rn, FPR64:$Rm),
2143                       !strconcat(asmstr, "\t$Rd, $Rn, $Rm"),
2144                       [(set f64:$Rd, (opnode f64:$Rn, f64:$Rm))],
2145                       NoItinerary>;
2146 }
2147
2148 let isCommutable = 1 in {
2149   defm FMUL   : A64I_fpdp2sizes<0b0000, "fmul", fmul>;
2150   defm FADD   : A64I_fpdp2sizes<0b0010, "fadd", fadd>;
2151
2152   // No patterns for these.
2153   defm FMAX   : A64I_fpdp2sizes<0b0100, "fmax", FPNoBinop>;
2154   defm FMIN   : A64I_fpdp2sizes<0b0101, "fmin", FPNoBinop>;
2155   defm FMAXNM : A64I_fpdp2sizes<0b0110, "fmaxnm", FPNoBinop>;
2156   defm FMINNM : A64I_fpdp2sizes<0b0111, "fminnm", FPNoBinop>;
2157
2158   defm FNMUL  : A64I_fpdp2sizes<0b1000, "fnmul",
2159                                 PatFrag<(ops node:$lhs, node:$rhs),
2160                                         (fneg (fmul node:$lhs, node:$rhs))> >;
2161 }
2162
2163 defm FDIV : A64I_fpdp2sizes<0b0001, "fdiv", fdiv>;
2164 defm FSUB : A64I_fpdp2sizes<0b0011, "fsub", fsub>;
2165
2166 //===----------------------------------------------------------------------===//
2167 // Floating-point data-processing (3 sources) instructions
2168 //===----------------------------------------------------------------------===//
2169 // Contains: FMADD, FMSUB, FNMADD, FNMSUB
2170
2171 def fmsub : PatFrag<(ops node:$Rn, node:$Rm, node:$Ra),
2172                     (fma (fneg node:$Rn),  node:$Rm, node:$Ra)>;
2173 def fnmadd : PatFrag<(ops node:$Rn, node:$Rm, node:$Ra),
2174                      (fma node:$Rn,  node:$Rm, (fneg node:$Ra))>;
2175 def fnmsub : PatFrag<(ops node:$Rn, node:$Rm, node:$Ra),
2176                      (fma (fneg node:$Rn),  node:$Rm, (fneg node:$Ra))>;
2177
2178 class A64I_fpdp3Impl<string asmop, RegisterClass FPR, ValueType VT,
2179                      bits<2> type, bit o1, bit o0, SDPatternOperator fmakind>
2180   : A64I_fpdp3<0b0, 0b0, type, o1, o0, (outs FPR:$Rd),
2181                (ins FPR:$Rn, FPR:$Rm, FPR:$Ra),
2182                !strconcat(asmop,"\t$Rd, $Rn, $Rm, $Ra"),
2183                [(set VT:$Rd, (fmakind VT:$Rn, VT:$Rm, VT:$Ra))],
2184                NoItinerary>;
2185
2186 def FMADDssss  : A64I_fpdp3Impl<"fmadd",  FPR32, f32, 0b00, 0b0, 0b0, fma>;
2187 def FMSUBssss  : A64I_fpdp3Impl<"fmsub",  FPR32, f32, 0b00, 0b0, 0b1, fmsub>;
2188 def FNMADDssss : A64I_fpdp3Impl<"fnmadd", FPR32, f32, 0b00, 0b1, 0b0, fnmadd>;
2189 def FNMSUBssss : A64I_fpdp3Impl<"fnmsub", FPR32, f32, 0b00, 0b1, 0b1, fnmsub>;
2190
2191 def FMADDdddd  : A64I_fpdp3Impl<"fmadd",  FPR64, f64, 0b01, 0b0, 0b0, fma>;
2192 def FMSUBdddd  : A64I_fpdp3Impl<"fmsub",  FPR64, f64, 0b01, 0b0, 0b1, fmsub>;
2193 def FNMADDdddd : A64I_fpdp3Impl<"fnmadd", FPR64, f64, 0b01, 0b1, 0b0, fnmadd>;
2194 def FNMSUBdddd : A64I_fpdp3Impl<"fnmsub", FPR64, f64, 0b01, 0b1, 0b1, fnmsub>;
2195
2196 // Extra patterns for when we're allowed to optimise separate multiplication and
2197 // addition.
2198 let Predicates = [UseFusedMAC] in {
2199 def : Pat<(f32 (fadd FPR32:$Ra, (f32 (fmul FPR32:$Rn, FPR32:$Rm)))),
2200           (FMADDssss FPR32:$Rn, FPR32:$Rm, FPR32:$Ra)>;
2201 def : Pat<(f32 (fsub FPR32:$Ra, (f32 (fmul FPR32:$Rn, FPR32:$Rm)))),
2202           (FMSUBssss FPR32:$Rn, FPR32:$Rm, FPR32:$Ra)>;
2203 def : Pat<(f32 (fsub (f32 (fmul FPR32:$Rn, FPR32:$Rm)), FPR32:$Ra)),
2204           (FNMADDssss FPR32:$Rn, FPR32:$Rm, FPR32:$Ra)>;
2205 def : Pat<(f32 (fsub (f32 (fneg FPR32:$Ra)), (f32 (fmul FPR32:$Rn, FPR32:$Rm)))),
2206           (FNMSUBssss FPR32:$Rn, FPR32:$Rm, FPR32:$Ra)>;
2207
2208 def : Pat<(f64 (fadd FPR64:$Ra, (f64 (fmul FPR64:$Rn, FPR64:$Rm)))),
2209           (FMADDdddd FPR64:$Rn, FPR64:$Rm, FPR64:$Ra)>;
2210 def : Pat<(f64 (fsub FPR64:$Ra, (f64 (fmul FPR64:$Rn, FPR64:$Rm)))),
2211           (FMSUBdddd FPR64:$Rn, FPR64:$Rm, FPR64:$Ra)>;
2212 def : Pat<(f64 (fsub (f64 (fmul FPR64:$Rn, FPR64:$Rm)), FPR64:$Ra)),
2213           (FNMADDdddd FPR64:$Rn, FPR64:$Rm, FPR64:$Ra)>;
2214 def : Pat<(f64 (fsub (f64 (fneg FPR64:$Ra)), (f64 (fmul FPR64:$Rn, FPR64:$Rm)))),
2215           (FNMSUBdddd FPR64:$Rn, FPR64:$Rm, FPR64:$Ra)>;
2216 }
2217
2218
2219 //===----------------------------------------------------------------------===//
2220 // Floating-point <-> fixed-point conversion instructions
2221 //===----------------------------------------------------------------------===//
2222 // Contains: FCVTZS, FCVTZU, SCVTF, UCVTF
2223
2224 // #1-#32 allowed, encoded as "64 - <specified imm>
2225 def fixedpos_asmoperand_i32 : AsmOperandClass {
2226   let Name = "CVTFixedPos32";
2227   let RenderMethod = "addCVTFixedPosOperands";
2228   let PredicateMethod = "isCVTFixedPos<32>";
2229   let DiagnosticType = "CVTFixedPos32";
2230 }
2231
2232 // Also encoded as "64 - <specified imm>" but #1-#64 allowed.
2233 def fixedpos_asmoperand_i64 : AsmOperandClass {
2234   let Name = "CVTFixedPos64";
2235   let RenderMethod = "addCVTFixedPosOperands";
2236   let PredicateMethod = "isCVTFixedPos<64>";
2237   let DiagnosticType = "CVTFixedPos64";
2238 }
2239
2240 // We need the cartesian product of f32/f64 i32/i64 operands for
2241 // conversions:
2242 //   + Selection needs to use operands of correct floating type
2243 //   + Assembly parsing and decoding depend on integer width
2244 class cvtfix_i32_op<ValueType FloatVT>
2245   : Operand<FloatVT>,
2246     ComplexPattern<FloatVT, 1, "SelectCVTFixedPosOperand<32>", [fpimm]> {
2247   let ParserMatchClass = fixedpos_asmoperand_i32;
2248   let DecoderMethod = "DecodeCVT32FixedPosOperand";
2249   let PrintMethod = "printCVTFixedPosOperand";
2250 }
2251
2252 class cvtfix_i64_op<ValueType FloatVT>
2253   : Operand<FloatVT>,
2254     ComplexPattern<FloatVT, 1, "SelectCVTFixedPosOperand<64>", [fpimm]> {
2255   let ParserMatchClass = fixedpos_asmoperand_i64;
2256   let PrintMethod = "printCVTFixedPosOperand";
2257 }
2258
2259 // Because of the proliferation of weird operands, it's not really
2260 // worth going for a multiclass here. Oh well.
2261
2262 class A64I_fptofix<bit sf, bits<2> type, bits<3> opcode,
2263                    RegisterClass GPR, RegisterClass FPR, 
2264                    ValueType DstTy, ValueType SrcTy, 
2265                    Operand scale_op, string asmop, SDNode cvtop>
2266   : A64I_fpfixed<sf, 0b0, type, 0b11, opcode,
2267                  (outs GPR:$Rd), (ins FPR:$Rn, scale_op:$Scale),
2268                  !strconcat(asmop, "\t$Rd, $Rn, $Scale"),
2269                  [(set DstTy:$Rd, (cvtop (fmul SrcTy:$Rn, scale_op:$Scale)))],
2270                  NoItinerary>;
2271
2272 def FCVTZSwsi : A64I_fptofix<0b0, 0b00, 0b000, GPR32, FPR32, i32, f32,
2273                              cvtfix_i32_op<f32>, "fcvtzs", fp_to_sint>;
2274 def FCVTZSxsi : A64I_fptofix<0b1, 0b00, 0b000, GPR64, FPR32, i64, f32,
2275                              cvtfix_i64_op<f32>, "fcvtzs", fp_to_sint>;
2276 def FCVTZUwsi : A64I_fptofix<0b0, 0b00, 0b001, GPR32, FPR32, i32, f32,
2277                              cvtfix_i32_op<f32>, "fcvtzu", fp_to_uint>;
2278 def FCVTZUxsi : A64I_fptofix<0b1, 0b00, 0b001, GPR64, FPR32, i64, f32,
2279                              cvtfix_i64_op<f32>, "fcvtzu", fp_to_uint>;
2280
2281 def FCVTZSwdi : A64I_fptofix<0b0, 0b01, 0b000, GPR32, FPR64, i32, f64,
2282                              cvtfix_i32_op<f64>, "fcvtzs", fp_to_sint>;
2283 def FCVTZSxdi : A64I_fptofix<0b1, 0b01, 0b000, GPR64, FPR64, i64, f64,
2284                              cvtfix_i64_op<f64>, "fcvtzs", fp_to_sint>;
2285 def FCVTZUwdi : A64I_fptofix<0b0, 0b01, 0b001, GPR32, FPR64, i32, f64,
2286                              cvtfix_i32_op<f64>, "fcvtzu", fp_to_uint>;
2287 def FCVTZUxdi : A64I_fptofix<0b1, 0b01, 0b001, GPR64, FPR64, i64, f64,
2288                              cvtfix_i64_op<f64>, "fcvtzu", fp_to_uint>;
2289
2290
2291 class A64I_fixtofp<bit sf, bits<2> type, bits<3> opcode,
2292                    RegisterClass FPR, RegisterClass GPR,
2293                    ValueType DstTy, ValueType SrcTy,
2294                    Operand scale_op, string asmop, SDNode cvtop>
2295   : A64I_fpfixed<sf, 0b0, type, 0b00, opcode,
2296                  (outs FPR:$Rd), (ins GPR:$Rn, scale_op:$Scale),
2297                  !strconcat(asmop, "\t$Rd, $Rn, $Scale"),
2298                  [(set DstTy:$Rd, (fdiv (cvtop SrcTy:$Rn), scale_op:$Scale))],
2299                  NoItinerary>;
2300
2301 def SCVTFswi : A64I_fixtofp<0b0, 0b00, 0b010, FPR32, GPR32, f32, i32,
2302                             cvtfix_i32_op<f32>, "scvtf", sint_to_fp>;
2303 def SCVTFsxi : A64I_fixtofp<0b1, 0b00, 0b010, FPR32, GPR64, f32, i64,
2304                             cvtfix_i64_op<f32>, "scvtf", sint_to_fp>;
2305 def UCVTFswi : A64I_fixtofp<0b0, 0b00, 0b011, FPR32, GPR32, f32, i32,
2306                             cvtfix_i32_op<f32>, "ucvtf", uint_to_fp>;
2307 def UCVTFsxi : A64I_fixtofp<0b1, 0b00, 0b011, FPR32, GPR64, f32, i64,
2308                             cvtfix_i64_op<f32>, "ucvtf", uint_to_fp>;
2309 def SCVTFdwi : A64I_fixtofp<0b0, 0b01, 0b010, FPR64, GPR32, f64, i32,
2310                             cvtfix_i32_op<f64>, "scvtf", sint_to_fp>;
2311 def SCVTFdxi : A64I_fixtofp<0b1, 0b01, 0b010, FPR64, GPR64, f64, i64,
2312                             cvtfix_i64_op<f64>, "scvtf", sint_to_fp>;
2313 def UCVTFdwi : A64I_fixtofp<0b0, 0b01, 0b011, FPR64, GPR32, f64, i32,
2314                             cvtfix_i32_op<f64>, "ucvtf", uint_to_fp>;
2315 def UCVTFdxi : A64I_fixtofp<0b1, 0b01, 0b011, FPR64, GPR64, f64, i64,
2316                             cvtfix_i64_op<f64>, "ucvtf", uint_to_fp>;
2317
2318 //===----------------------------------------------------------------------===//
2319 // Floating-point <-> integer conversion instructions
2320 //===----------------------------------------------------------------------===//
2321 // Contains: FCVTZS, FCVTZU, SCVTF, UCVTF
2322
2323 class A64I_fpintI<bit sf, bits<2> type, bits<2> rmode, bits<3> opcode,
2324                    RegisterClass DestPR, RegisterClass SrcPR, string asmop>
2325   : A64I_fpint<sf, 0b0, type, rmode, opcode, (outs DestPR:$Rd), (ins SrcPR:$Rn),
2326                !strconcat(asmop, "\t$Rd, $Rn"), [], NoItinerary>;
2327
2328 multiclass A64I_fptointRM<bits<2> rmode, bit o2, string asmop> {
2329   def Sws : A64I_fpintI<0b0, 0b00, rmode, {o2, 0, 0},
2330                         GPR32, FPR32, asmop # "s">;
2331   def Sxs : A64I_fpintI<0b1, 0b00, rmode, {o2, 0, 0},
2332                         GPR64, FPR32, asmop # "s">;
2333   def Uws : A64I_fpintI<0b0, 0b00, rmode, {o2, 0, 1},
2334                         GPR32, FPR32, asmop # "u">;
2335   def Uxs : A64I_fpintI<0b1, 0b00, rmode, {o2, 0, 1},
2336                         GPR64, FPR32, asmop # "u">;
2337
2338   def Swd : A64I_fpintI<0b0, 0b01, rmode, {o2, 0, 0},
2339                         GPR32, FPR64, asmop # "s">;
2340   def Sxd : A64I_fpintI<0b1, 0b01, rmode, {o2, 0, 0},
2341                         GPR64, FPR64, asmop # "s">;
2342   def Uwd : A64I_fpintI<0b0, 0b01, rmode, {o2, 0, 1},
2343                         GPR32, FPR64, asmop # "u">;
2344   def Uxd : A64I_fpintI<0b1, 0b01, rmode, {o2, 0, 1},
2345                         GPR64, FPR64, asmop # "u">;
2346 }
2347
2348 defm FCVTN : A64I_fptointRM<0b00, 0b0, "fcvtn">;
2349 defm FCVTP : A64I_fptointRM<0b01, 0b0, "fcvtp">;
2350 defm FCVTM : A64I_fptointRM<0b10, 0b0, "fcvtm">;
2351 defm FCVTZ : A64I_fptointRM<0b11, 0b0, "fcvtz">;
2352 defm FCVTA : A64I_fptointRM<0b00, 0b1, "fcvta">;
2353
2354 def : Pat<(i32 (fp_to_sint f32:$Rn)), (FCVTZSws $Rn)>;
2355 def : Pat<(i64 (fp_to_sint f32:$Rn)), (FCVTZSxs $Rn)>;
2356 def : Pat<(i32 (fp_to_uint f32:$Rn)), (FCVTZUws $Rn)>;
2357 def : Pat<(i64 (fp_to_uint f32:$Rn)), (FCVTZUxs $Rn)>;
2358 def : Pat<(i32 (fp_to_sint f64:$Rn)), (FCVTZSwd $Rn)>;
2359 def : Pat<(i64 (fp_to_sint f64:$Rn)), (FCVTZSxd $Rn)>;
2360 def : Pat<(i32 (fp_to_uint f64:$Rn)), (FCVTZUwd $Rn)>;
2361 def : Pat<(i64 (fp_to_uint f64:$Rn)), (FCVTZUxd $Rn)>;
2362
2363 multiclass A64I_inttofp<bit o0, string asmop> {
2364   def CVTFsw : A64I_fpintI<0b0, 0b00, 0b00, {0, 1, o0}, FPR32, GPR32, asmop>;
2365   def CVTFsx : A64I_fpintI<0b1, 0b00, 0b00, {0, 1, o0}, FPR32, GPR64, asmop>;
2366   def CVTFdw : A64I_fpintI<0b0, 0b01, 0b00, {0, 1, o0}, FPR64, GPR32, asmop>;
2367   def CVTFdx : A64I_fpintI<0b1, 0b01, 0b00, {0, 1, o0}, FPR64, GPR64, asmop>;
2368 }
2369
2370 defm S : A64I_inttofp<0b0, "scvtf">;
2371 defm U : A64I_inttofp<0b1, "ucvtf">;
2372
2373 def : Pat<(f32 (sint_to_fp i32:$Rn)), (SCVTFsw $Rn)>;
2374 def : Pat<(f32 (sint_to_fp i64:$Rn)), (SCVTFsx $Rn)>;
2375 def : Pat<(f64 (sint_to_fp i32:$Rn)), (SCVTFdw $Rn)>;
2376 def : Pat<(f64 (sint_to_fp i64:$Rn)), (SCVTFdx $Rn)>;
2377 def : Pat<(f32 (uint_to_fp i32:$Rn)), (UCVTFsw $Rn)>;
2378 def : Pat<(f32 (uint_to_fp i64:$Rn)), (UCVTFsx $Rn)>;
2379 def : Pat<(f64 (uint_to_fp i32:$Rn)), (UCVTFdw $Rn)>;
2380 def : Pat<(f64 (uint_to_fp i64:$Rn)), (UCVTFdx $Rn)>;
2381
2382 def FMOVws : A64I_fpintI<0b0, 0b00, 0b00, 0b110, GPR32, FPR32, "fmov">;
2383 def FMOVsw : A64I_fpintI<0b0, 0b00, 0b00, 0b111, FPR32, GPR32, "fmov">;
2384 def FMOVxd : A64I_fpintI<0b1, 0b01, 0b00, 0b110, GPR64, FPR64, "fmov">;
2385 def FMOVdx : A64I_fpintI<0b1, 0b01, 0b00, 0b111, FPR64, GPR64, "fmov">;
2386
2387 def : Pat<(i32 (bitconvert f32:$Rn)), (FMOVws $Rn)>;
2388 def : Pat<(f32 (bitconvert i32:$Rn)), (FMOVsw $Rn)>;
2389 def : Pat<(i64 (bitconvert f64:$Rn)), (FMOVxd $Rn)>;
2390 def : Pat<(f64 (bitconvert i64:$Rn)), (FMOVdx $Rn)>;
2391
2392 def lane1_asmoperand : AsmOperandClass {
2393   let Name = "Lane1";
2394   let RenderMethod = "addImmOperands";
2395   let DiagnosticType = "Lane1";
2396 }
2397
2398 def lane1 : Operand<i32> {
2399   let ParserMatchClass = lane1_asmoperand;
2400   let PrintMethod = "printBareImmOperand";
2401 }
2402
2403 let DecoderMethod =  "DecodeFMOVLaneInstruction" in {
2404   def FMOVxv : A64I_fpint<0b1, 0b0, 0b10, 0b01, 0b110,
2405                           (outs GPR64:$Rd), (ins VPR128:$Rn, lane1:$Lane),
2406                           "fmov\t$Rd, $Rn.d[$Lane]", [], NoItinerary>;
2407
2408   def FMOVvx : A64I_fpint<0b1, 0b0, 0b10, 0b01, 0b111,
2409                           (outs VPR128:$Rd), (ins GPR64:$Rn, lane1:$Lane),
2410                           "fmov\t$Rd.d[$Lane], $Rn", [], NoItinerary>;
2411 }
2412
2413 def : InstAlias<"fmov $Rd, $Rn.2d[$Lane]",
2414                 (FMOVxv GPR64:$Rd, VPR128:$Rn, lane1:$Lane), 0b0>;
2415
2416 def : InstAlias<"fmov $Rd.2d[$Lane], $Rn",
2417                 (FMOVvx VPR128:$Rd, GPR64:$Rn, lane1:$Lane), 0b0>;
2418
2419 //===----------------------------------------------------------------------===//
2420 // Floating-point immediate instructions
2421 //===----------------------------------------------------------------------===//
2422 // Contains: FMOV
2423
2424 def fpimm_asmoperand : AsmOperandClass {
2425   let Name = "FMOVImm";
2426   let ParserMethod = "ParseFPImmOperand";
2427   let DiagnosticType = "FPImm";
2428 }
2429
2430 // The MCOperand for these instructions are the encoded 8-bit values.
2431 def SDXF_fpimm : SDNodeXForm<fpimm, [{
2432   uint32_t Imm8;
2433   A64Imms::isFPImm(N->getValueAPF(), Imm8);
2434   return CurDAG->getTargetConstant(Imm8, MVT::i32);
2435 }]>;
2436
2437 class fmov_operand<ValueType FT>
2438   : Operand<i32>,
2439     PatLeaf<(FT fpimm), [{ return A64Imms::isFPImm(N->getValueAPF()); }],
2440             SDXF_fpimm> {
2441   let PrintMethod = "printFPImmOperand";
2442   let ParserMatchClass = fpimm_asmoperand;
2443 }
2444
2445 def fmov32_operand : fmov_operand<f32>;
2446 def fmov64_operand : fmov_operand<f64>;
2447
2448 class A64I_fpimm_impl<bits<2> type, RegisterClass Reg, ValueType VT,
2449                       Operand fmov_operand>
2450   : A64I_fpimm<0b0, 0b0, type, 0b00000,
2451                (outs Reg:$Rd),
2452                (ins fmov_operand:$Imm8),
2453                "fmov\t$Rd, $Imm8",
2454                [(set VT:$Rd, fmov_operand:$Imm8)],
2455                NoItinerary>;
2456
2457 def FMOVsi : A64I_fpimm_impl<0b00, FPR32, f32, fmov32_operand>;
2458 def FMOVdi : A64I_fpimm_impl<0b01, FPR64, f64, fmov64_operand>;
2459
2460 //===----------------------------------------------------------------------===//
2461 // Load-register (literal) instructions
2462 //===----------------------------------------------------------------------===//
2463 // Contains: LDR, LDRSW, PRFM
2464
2465 def ldrlit_label_asmoperand : AsmOperandClass {
2466   let Name = "LoadLitLabel";
2467   let RenderMethod = "addLabelOperands<19, 4>";
2468   let DiagnosticType = "Label";
2469 }
2470
2471 def ldrlit_label : Operand<i64> {
2472   let EncoderMethod = "getLoadLitLabelOpValue";
2473
2474   // This label is a 19-bit offset from PC, scaled by the instruction-width: 4.
2475   let PrintMethod = "printLabelOperand<19, 4>";
2476   let ParserMatchClass = ldrlit_label_asmoperand;
2477   let OperandType = "OPERAND_PCREL";
2478 }
2479
2480 // Various instructions take an immediate value (which can always be used),
2481 // where some numbers have a symbolic name to make things easier. These operands
2482 // and the associated functions abstract away the differences.
2483 multiclass namedimm<string prefix, string mapper> {
2484   def _asmoperand : AsmOperandClass {
2485     let Name = "NamedImm" # prefix;
2486     let PredicateMethod = "isUImm";
2487     let RenderMethod = "addImmOperands";
2488     let ParserMethod = "ParseNamedImmOperand<" # mapper # ">";
2489     let DiagnosticType = "NamedImm_" # prefix;
2490   }
2491
2492   def _op : Operand<i32> {
2493     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_asmoperand");
2494     let PrintMethod = "printNamedImmOperand<" # mapper # ">";
2495     let DecoderMethod = "DecodeNamedImmOperand<" # mapper # ">";
2496   }
2497 }
2498
2499 defm prefetch : namedimm<"prefetch", "A64PRFM::PRFMMapper">;
2500
2501 class A64I_LDRlitSimple<bits<2> opc, bit v, RegisterClass OutReg,
2502                       list<dag> patterns = []>
2503    : A64I_LDRlit<opc, v, (outs OutReg:$Rt), (ins ldrlit_label:$Imm19),
2504                  "ldr\t$Rt, $Imm19", patterns, NoItinerary>;
2505
2506 let mayLoad = 1 in {
2507   def LDRw_lit : A64I_LDRlitSimple<0b00, 0b0, GPR32>;
2508   def LDRx_lit : A64I_LDRlitSimple<0b01, 0b0, GPR64>;
2509 }
2510
2511 def LDRs_lit  : A64I_LDRlitSimple<0b00, 0b1, FPR32>;
2512 def LDRd_lit  : A64I_LDRlitSimple<0b01, 0b1, FPR64>;
2513
2514 let mayLoad = 1 in {
2515   def LDRq_lit : A64I_LDRlitSimple<0b10, 0b1, FPR128>;
2516
2517
2518   def LDRSWx_lit : A64I_LDRlit<0b10, 0b0,
2519                                (outs GPR64:$Rt),
2520                                (ins ldrlit_label:$Imm19),
2521                                "ldrsw\t$Rt, $Imm19",
2522                                [], NoItinerary>;
2523
2524   def PRFM_lit : A64I_LDRlit<0b11, 0b0,
2525                              (outs), (ins prefetch_op:$Rt, ldrlit_label:$Imm19),
2526                              "prfm\t$Rt, $Imm19",
2527                              [], NoItinerary>;
2528 }
2529
2530 //===----------------------------------------------------------------------===//
2531 // Load-store exclusive instructions
2532 //===----------------------------------------------------------------------===//
2533 // Contains: STXRB, STXRH, STXR, LDXRB, LDXRH, LDXR. STXP, LDXP, STLXRB,
2534 //           STLXRH, STLXR, LDAXRB, LDAXRH, LDAXR, STLXP, LDAXP, STLRB,
2535 //           STLRH, STLR, LDARB, LDARH, LDAR
2536
2537 // Since these instructions have the undefined register bits set to 1 in
2538 // their canonical form, we need a post encoder method to set those bits
2539 // to 1 when encoding these instructions. We do this using the
2540 // fixLoadStoreExclusive function. This function has template parameters:
2541 //
2542 // fixLoadStoreExclusive<int hasRs, int hasRt2>
2543 //
2544 // hasRs indicates that the instruction uses the Rs field, so we won't set
2545 // it to 1 (and the same for Rt2). We don't need template parameters for
2546 // the other register fiels since Rt and Rn are always used.
2547
2548 // This operand parses a GPR64xsp register, followed by an optional immediate
2549 // #0.
2550 def GPR64xsp0_asmoperand : AsmOperandClass {
2551   let Name = "GPR64xsp0";
2552   let PredicateMethod = "isWrappedReg";
2553   let RenderMethod = "addRegOperands";
2554   let ParserMethod = "ParseLSXAddressOperand";
2555   // Diagnostics are provided by ParserMethod
2556 }
2557
2558 def GPR64xsp0 : RegisterOperand<GPR64xsp> {
2559   let ParserMatchClass = GPR64xsp0_asmoperand;
2560 }
2561
2562 //===----------------------------------
2563 // Store-exclusive (releasing & normal)
2564 //===----------------------------------
2565
2566 class A64I_SRexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2567                         dag ins, list<dag> pat,
2568                         InstrItinClass itin> :
2569        A64I_LDSTex_stn <size,
2570                         opcode{2}, 0, opcode{1}, opcode{0},
2571                         outs, ins,
2572                         !strconcat(asm, "\t$Rs, $Rt, [$Rn]"),
2573                         pat, itin> {
2574   let mayStore = 1;
2575   let PostEncoderMethod = "fixLoadStoreExclusive<1,0>";
2576 }
2577
2578 multiclass A64I_SRex<string asmstr, bits<3> opcode, string prefix> {
2579   def _byte:  A64I_SRexs_impl<0b00, opcode, !strconcat(asmstr, "b"),
2580                               (outs GPR32:$Rs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2581                               [], NoItinerary>;
2582
2583   def _hword:  A64I_SRexs_impl<0b01, opcode, !strconcat(asmstr, "h"),
2584                                (outs GPR32:$Rs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2585                                [],NoItinerary>;
2586
2587   def _word:  A64I_SRexs_impl<0b10, opcode, asmstr,
2588                               (outs GPR32:$Rs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2589                               [], NoItinerary>;
2590
2591   def _dword: A64I_SRexs_impl<0b11, opcode, asmstr,
2592                               (outs GPR32:$Rs), (ins GPR64:$Rt, GPR64xsp0:$Rn),
2593                               [], NoItinerary>;
2594 }
2595
2596 defm STXR  : A64I_SRex<"stxr",  0b000, "STXR">;
2597 defm STLXR : A64I_SRex<"stlxr", 0b001, "STLXR">;
2598
2599 //===----------------------------------
2600 // Loads
2601 //===----------------------------------
2602
2603 class A64I_LRexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2604                         dag ins, list<dag> pat,
2605                         InstrItinClass itin> :
2606         A64I_LDSTex_tn <size,
2607                         opcode{2}, 1, opcode{1}, opcode{0},
2608                         outs, ins,
2609                         !strconcat(asm, "\t$Rt, [$Rn]"),
2610                         pat, itin> {
2611   let mayLoad = 1;
2612   let PostEncoderMethod = "fixLoadStoreExclusive<0,0>";
2613 }
2614
2615 multiclass A64I_LRex<string asmstr, bits<3> opcode> {
2616   def _byte:  A64I_LRexs_impl<0b00, opcode, !strconcat(asmstr, "b"),
2617                             (outs GPR32:$Rt), (ins GPR64xsp0:$Rn),
2618                             [], NoItinerary>;
2619
2620   def _hword:  A64I_LRexs_impl<0b01, opcode, !strconcat(asmstr, "h"),
2621                             (outs GPR32:$Rt), (ins GPR64xsp0:$Rn),
2622                             [], NoItinerary>;
2623
2624   def _word:  A64I_LRexs_impl<0b10, opcode, asmstr,
2625                             (outs GPR32:$Rt), (ins GPR64xsp0:$Rn),
2626                             [], NoItinerary>;
2627
2628   def _dword: A64I_LRexs_impl<0b11, opcode, asmstr,
2629                             (outs GPR64:$Rt), (ins GPR64xsp0:$Rn),
2630                             [], NoItinerary>;
2631 }
2632
2633 defm LDXR  : A64I_LRex<"ldxr",  0b000>;
2634 defm LDAXR : A64I_LRex<"ldaxr", 0b001>;
2635 defm LDAR  : A64I_LRex<"ldar",  0b101>;
2636
2637 class acquiring_load<PatFrag base>
2638   : PatFrag<(ops node:$ptr), (base node:$ptr), [{
2639   AtomicOrdering Ordering = cast<AtomicSDNode>(N)->getOrdering();
2640   return Ordering == Acquire || Ordering == SequentiallyConsistent;
2641 }]>;
2642
2643 def atomic_load_acquire_8  : acquiring_load<atomic_load_8>;
2644 def atomic_load_acquire_16 : acquiring_load<atomic_load_16>;
2645 def atomic_load_acquire_32 : acquiring_load<atomic_load_32>;
2646 def atomic_load_acquire_64 : acquiring_load<atomic_load_64>;
2647
2648 def : Pat<(atomic_load_acquire_8  i64:$Rn), (LDAR_byte  $Rn)>;
2649 def : Pat<(atomic_load_acquire_16 i64:$Rn), (LDAR_hword $Rn)>;
2650 def : Pat<(atomic_load_acquire_32 i64:$Rn), (LDAR_word  $Rn)>;
2651 def : Pat<(atomic_load_acquire_64 i64:$Rn), (LDAR_dword $Rn)>;
2652
2653 //===----------------------------------
2654 // Store-release (no exclusivity)
2655 //===----------------------------------
2656
2657 class A64I_SLexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2658                         dag ins, list<dag> pat,
2659                         InstrItinClass itin> :
2660         A64I_LDSTex_tn <size,
2661                         opcode{2}, 0, opcode{1}, opcode{0},
2662                         outs, ins,
2663                         !strconcat(asm, "\t$Rt, [$Rn]"),
2664                         pat, itin> {
2665   let mayStore = 1;
2666   let PostEncoderMethod = "fixLoadStoreExclusive<0,0>";
2667 }
2668
2669 class releasing_store<PatFrag base>
2670   : PatFrag<(ops node:$ptr, node:$val), (base node:$ptr, node:$val), [{
2671   AtomicOrdering Ordering = cast<AtomicSDNode>(N)->getOrdering();
2672   return Ordering == Release || Ordering == SequentiallyConsistent;
2673 }]>;
2674
2675 def atomic_store_release_8  : releasing_store<atomic_store_8>;
2676 def atomic_store_release_16 : releasing_store<atomic_store_16>;
2677 def atomic_store_release_32 : releasing_store<atomic_store_32>;
2678 def atomic_store_release_64 : releasing_store<atomic_store_64>;
2679
2680 multiclass A64I_SLex<string asmstr, bits<3> opcode, string prefix> {
2681   def _byte:  A64I_SLexs_impl<0b00, opcode, !strconcat(asmstr, "b"),
2682                             (outs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2683                             [(atomic_store_release_8 i64:$Rn, i32:$Rt)],
2684                             NoItinerary>;
2685
2686   def _hword:  A64I_SLexs_impl<0b01, opcode, !strconcat(asmstr, "h"),
2687                            (outs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2688                            [(atomic_store_release_16 i64:$Rn, i32:$Rt)],
2689                            NoItinerary>;
2690
2691   def _word:  A64I_SLexs_impl<0b10, opcode, asmstr,
2692                            (outs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2693                            [(atomic_store_release_32 i64:$Rn, i32:$Rt)],
2694                            NoItinerary>;
2695
2696   def _dword: A64I_SLexs_impl<0b11, opcode, asmstr,
2697                            (outs), (ins GPR64:$Rt, GPR64xsp0:$Rn),
2698                            [(atomic_store_release_64 i64:$Rn, i64:$Rt)],
2699                            NoItinerary>;
2700 }
2701
2702 defm STLR  : A64I_SLex<"stlr", 0b101, "STLR">;
2703
2704 //===----------------------------------
2705 // Store-exclusive pair (releasing & normal)
2706 //===----------------------------------
2707
2708 class A64I_SPexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2709                         dag ins, list<dag> pat,
2710                         InstrItinClass itin> :
2711      A64I_LDSTex_stt2n <size,
2712                         opcode{2}, 0, opcode{1}, opcode{0},
2713                         outs, ins,
2714                         !strconcat(asm, "\t$Rs, $Rt, $Rt2, [$Rn]"),
2715                         pat, itin> {
2716   let mayStore = 1;
2717 }
2718
2719
2720 multiclass A64I_SPex<string asmstr, bits<3> opcode> {
2721   def _word:  A64I_SPexs_impl<0b10, opcode, asmstr, (outs),
2722                             (ins GPR32:$Rs, GPR32:$Rt, GPR32:$Rt2,
2723                                  GPR64xsp0:$Rn),
2724                             [], NoItinerary>;
2725
2726   def _dword: A64I_SPexs_impl<0b11, opcode, asmstr, (outs),
2727                             (ins GPR32:$Rs, GPR64:$Rt, GPR64:$Rt2,
2728                                             GPR64xsp0:$Rn),
2729                             [], NoItinerary>;
2730 }
2731
2732 defm STXP  : A64I_SPex<"stxp", 0b010>;
2733 defm STLXP : A64I_SPex<"stlxp", 0b011>;
2734
2735 //===----------------------------------
2736 // Load-exclusive pair (acquiring & normal)
2737 //===----------------------------------
2738
2739 class A64I_LPexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2740                         dag ins, list<dag> pat,
2741                         InstrItinClass itin> :
2742       A64I_LDSTex_tt2n <size,
2743                         opcode{2}, 1, opcode{1}, opcode{0},
2744                         outs, ins,
2745                         !strconcat(asm, "\t$Rt, $Rt2, [$Rn]"),
2746                         pat, itin>{
2747   let mayLoad = 1;
2748   let DecoderMethod = "DecodeLoadPairExclusiveInstruction";
2749   let PostEncoderMethod = "fixLoadStoreExclusive<0,1>";
2750 }
2751
2752 multiclass A64I_LPex<string asmstr, bits<3> opcode> {
2753   def _word:  A64I_LPexs_impl<0b10, opcode, asmstr,
2754                             (outs GPR32:$Rt, GPR32:$Rt2),
2755                             (ins GPR64xsp0:$Rn),
2756                             [], NoItinerary>;
2757
2758   def _dword: A64I_LPexs_impl<0b11, opcode, asmstr,
2759                             (outs GPR64:$Rt, GPR64:$Rt2),
2760                             (ins GPR64xsp0:$Rn),
2761                             [], NoItinerary>;
2762 }
2763
2764 defm LDXP  : A64I_LPex<"ldxp", 0b010>;
2765 defm LDAXP : A64I_LPex<"ldaxp", 0b011>;
2766
2767 //===----------------------------------------------------------------------===//
2768 // Load-store register (unscaled immediate) instructions
2769 //===----------------------------------------------------------------------===//
2770 // Contains: LDURB, LDURH, LDRUSB, LDRUSH, LDRUSW, STUR, STURB, STURH and PRFUM
2771 //
2772 // and
2773 //
2774 //===----------------------------------------------------------------------===//
2775 // Load-store register (register offset) instructions
2776 //===----------------------------------------------------------------------===//
2777 // Contains: LDRB, LDRH, LDRSB, LDRSH, LDRSW, STR, STRB, STRH and PRFM
2778 //
2779 // and
2780 //
2781 //===----------------------------------------------------------------------===//
2782 // Load-store register (unsigned immediate) instructions
2783 //===----------------------------------------------------------------------===//
2784 // Contains: LDRB, LDRH, LDRSB, LDRSH, LDRSW, STR, STRB, STRH and PRFM
2785 //
2786 // and
2787 //
2788 //===----------------------------------------------------------------------===//
2789 // Load-store register (immediate post-indexed) instructions
2790 //===----------------------------------------------------------------------===//
2791 // Contains: STRB, STRH, STR, LDRB, LDRH, LDR, LDRSB, LDRSH, LDRSW
2792 //
2793 // and
2794 //
2795 //===----------------------------------------------------------------------===//
2796 // Load-store register (immediate pre-indexed) instructions
2797 //===----------------------------------------------------------------------===//
2798 // Contains: STRB, STRH, STR, LDRB, LDRH, LDR, LDRSB, LDRSH, LDRSW
2799
2800 // Note that patterns are much later on in a completely separate section (they
2801 // need ADRPxi to be defined).
2802
2803 //===-------------------------------
2804 // 1. Various operands needed
2805 //===-------------------------------
2806
2807 //===-------------------------------
2808 // 1.1 Unsigned 12-bit immediate operands
2809 //===-------------------------------
2810 // The addressing mode for these instructions consists of an unsigned 12-bit
2811 // immediate which is scaled by the size of the memory access.
2812 //
2813 // We represent this in the MC layer by two operands:
2814 //     1. A base register.
2815 //     2. A 12-bit immediate: not multiplied by access size, so "LDR x0,[x0,#8]"
2816 //        would have '1' in this field.
2817 // This means that separate functions are needed for converting representations
2818 // which *are* aware of the intended access size.
2819
2820 // Anything that creates an MCInst (Decoding, selection and AsmParsing) has to
2821 // know the access size via some means. An isolated operand does not have this
2822 // information unless told from here, which means we need separate tablegen
2823 // Operands for each access size. This multiclass takes care of instantiating
2824 // the correct template functions in the rest of the backend.
2825
2826 //===-------------------------------
2827 // 1.1 Unsigned 12-bit immediate operands
2828 //===-------------------------------
2829
2830 multiclass offsets_uimm12<int MemSize, string prefix> {
2831   def uimm12_asmoperand : AsmOperandClass {
2832     let Name = "OffsetUImm12_" # MemSize;
2833     let PredicateMethod = "isOffsetUImm12<" # MemSize # ">";
2834     let RenderMethod = "addOffsetUImm12Operands<" # MemSize # ">";
2835     let DiagnosticType = "LoadStoreUImm12_" # MemSize;
2836   }
2837
2838   // Pattern is really no more than an ImmLeaf, but predicated on MemSize which
2839   // complicates things beyond TableGen's ken.
2840   def uimm12 : Operand<i64>,
2841                ComplexPattern<i64, 1, "SelectOffsetUImm12<" # MemSize # ">"> {
2842     let ParserMatchClass
2843       = !cast<AsmOperandClass>(prefix # uimm12_asmoperand);
2844
2845     let PrintMethod = "printOffsetUImm12Operand<" # MemSize # ">";
2846     let EncoderMethod = "getOffsetUImm12OpValue<" # MemSize # ">";
2847   }
2848 }
2849
2850 defm byte_  : offsets_uimm12<1, "byte_">;
2851 defm hword_ : offsets_uimm12<2, "hword_">;
2852 defm word_  : offsets_uimm12<4, "word_">;
2853 defm dword_ : offsets_uimm12<8, "dword_">;
2854 defm qword_ : offsets_uimm12<16, "qword_">;
2855
2856 //===-------------------------------
2857 // 1.1 Signed 9-bit immediate operands
2858 //===-------------------------------
2859
2860 // The MCInst is expected to store the bit-wise encoding of the value,
2861 // which amounts to lopping off the extended sign bits.
2862 def SDXF_simm9 : SDNodeXForm<imm, [{
2863   return CurDAG->getTargetConstant(N->getZExtValue() & 0x1ff, MVT::i32);
2864 }]>;
2865
2866 def simm9_asmoperand : AsmOperandClass {
2867   let Name = "SImm9";
2868   let PredicateMethod = "isSImm<9>";
2869   let RenderMethod = "addSImmOperands<9>";
2870   let DiagnosticType = "LoadStoreSImm9";
2871 }
2872
2873 def simm9 : Operand<i64>,
2874             ImmLeaf<i64, [{ return Imm >= -0x100 && Imm <= 0xff; }],
2875             SDXF_simm9> {
2876   let PrintMethod = "printOffsetSImm9Operand";
2877   let ParserMatchClass = simm9_asmoperand;
2878 }
2879
2880
2881 //===-------------------------------
2882 // 1.3 Register offset extensions
2883 //===-------------------------------
2884
2885 // The assembly-syntax for these addressing-modes is:
2886 //    [<Xn|SP>, <R><m> {, <extend> {<amount>}}]
2887 //
2888 // The essential semantics are:
2889 //     + <amount> is a shift: #<log(transfer size)> or #0
2890 //     + <R> can be W or X.
2891 //     + If <R> is W, <extend> can be UXTW or SXTW
2892 //     + If <R> is X, <extend> can be LSL or SXTX
2893 //
2894 // The trickiest of those constraints is that Rm can be either GPR32 or GPR64,
2895 // which will need separate instructions for LLVM type-consistency. We'll also
2896 // need separate operands, of course.
2897 multiclass regexts<int MemSize, int RmSize, RegisterClass GPR,
2898                    string Rm, string prefix> {
2899   def regext_asmoperand : AsmOperandClass {
2900     let Name = "AddrRegExtend_" # MemSize # "_" #  Rm;
2901     let PredicateMethod = "isAddrRegExtend<" # MemSize # "," # RmSize # ">";
2902     let RenderMethod = "addAddrRegExtendOperands<" # MemSize # ">";
2903     let DiagnosticType = "LoadStoreExtend" # RmSize # "_" # MemSize;
2904   }
2905
2906   def regext : Operand<i64> {
2907     let PrintMethod
2908       = "printAddrRegExtendOperand<" # MemSize # ", " # RmSize # ">";
2909
2910     let DecoderMethod = "DecodeAddrRegExtendOperand";
2911     let ParserMatchClass
2912       = !cast<AsmOperandClass>(prefix # regext_asmoperand);
2913   }
2914 }
2915
2916 multiclass regexts_wx<int MemSize, string prefix> {
2917   // Rm is an X-register if LSL or SXTX are specified as the shift.
2918   defm Xm_ : regexts<MemSize, 64, GPR64, "Xm", prefix # "Xm_">;
2919
2920   // Rm is a W-register if UXTW or SXTW are specified as the shift.
2921   defm Wm_ : regexts<MemSize, 32, GPR32, "Wm", prefix # "Wm_">;
2922 }
2923
2924 defm byte_  : regexts_wx<1, "byte_">;
2925 defm hword_ : regexts_wx<2, "hword_">;
2926 defm word_  : regexts_wx<4, "word_">;
2927 defm dword_ : regexts_wx<8, "dword_">;
2928 defm qword_ : regexts_wx<16, "qword_">;
2929
2930
2931 //===------------------------------
2932 // 2. The instructions themselves.
2933 //===------------------------------
2934
2935 // We have the following instructions to implement:
2936 // |                 | B     | H     | W     | X      |
2937 // |-----------------+-------+-------+-------+--------|
2938 // | unsigned str    | STRB  | STRH  | STR   | STR    |
2939 // | unsigned ldr    | LDRB  | LDRH  | LDR   | LDR    |
2940 // | signed ldr to W | LDRSB | LDRSH | -     | -      |
2941 // | signed ldr to X | LDRSB | LDRSH | LDRSW | (PRFM) |
2942
2943 // This will instantiate the LDR/STR instructions you'd expect to use for an
2944 // unsigned datatype (first two rows above) or floating-point register, which is
2945 // reasonably uniform across all access sizes.
2946
2947
2948 //===------------------------------
2949 // 2.1 Regular instructions
2950 //===------------------------------
2951
2952 // This class covers the basic unsigned or irrelevantly-signed loads and stores,
2953 // to general-purpose and floating-point registers.
2954
2955 class AddrParams<string prefix> {
2956   Operand uimm12 = !cast<Operand>(prefix # "_uimm12");
2957
2958   Operand regextWm = !cast<Operand>(prefix # "_Wm_regext");
2959   Operand regextXm = !cast<Operand>(prefix # "_Xm_regext");
2960 }
2961
2962 def byte_addrparams : AddrParams<"byte">;
2963 def hword_addrparams : AddrParams<"hword">;
2964 def word_addrparams : AddrParams<"word">;
2965 def dword_addrparams : AddrParams<"dword">;
2966 def qword_addrparams : AddrParams<"qword">;
2967
2968 multiclass A64I_LDRSTR_unsigned<string prefix, bits<2> size, bit v,
2969                                 bit high_opc, string asmsuffix,
2970                                 RegisterClass GPR, AddrParams params> {
2971   // Unsigned immediate
2972   def _STR : A64I_LSunsigimm<size, v, {high_opc, 0b0},
2973                      (outs), (ins GPR:$Rt, GPR64xsp:$Rn, params.uimm12:$UImm12),
2974                      "str" # asmsuffix # "\t$Rt, [$Rn, $UImm12]",
2975                      [], NoItinerary> {
2976     let mayStore = 1;
2977   }
2978   def : InstAlias<"str" # asmsuffix # " $Rt, [$Rn]",
2979                 (!cast<Instruction>(prefix # "_STR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
2980
2981   def _LDR : A64I_LSunsigimm<size, v, {high_opc, 0b1},
2982                       (outs GPR:$Rt), (ins GPR64xsp:$Rn, params.uimm12:$UImm12),
2983                       "ldr" #  asmsuffix # "\t$Rt, [$Rn, $UImm12]",
2984                       [], NoItinerary> {
2985     let mayLoad = 1;
2986   }
2987   def : InstAlias<"ldr" # asmsuffix # " $Rt, [$Rn]",
2988                 (!cast<Instruction>(prefix # "_LDR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
2989
2990   // Register offset (four of these: load/store and Wm/Xm).
2991   let mayLoad = 1 in {
2992     def _Wm_RegOffset_LDR : A64I_LSregoff<size, v, {high_opc, 0b1}, 0b0,
2993                             (outs GPR:$Rt),
2994                             (ins GPR64xsp:$Rn, GPR32:$Rm, params.regextWm:$Ext),
2995                             "ldr" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
2996                             [], NoItinerary>;
2997
2998     def _Xm_RegOffset_LDR : A64I_LSregoff<size, v, {high_opc, 0b1}, 0b1,
2999                             (outs GPR:$Rt),
3000                             (ins GPR64xsp:$Rn, GPR64:$Rm, params.regextXm:$Ext),
3001                             "ldr" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
3002                             [], NoItinerary>;
3003   }
3004   def : InstAlias<"ldr" # asmsuffix # " $Rt, [$Rn, $Rm]",
3005         (!cast<Instruction>(prefix # "_Xm_RegOffset_LDR") GPR:$Rt, GPR64xsp:$Rn,
3006                                                           GPR64:$Rm, 2)>;
3007
3008   let mayStore = 1 in {
3009     def _Wm_RegOffset_STR : A64I_LSregoff<size, v, {high_opc, 0b0}, 0b0,
3010                                   (outs), (ins GPR:$Rt, GPR64xsp:$Rn, GPR32:$Rm,
3011                                                params.regextWm:$Ext),
3012                                   "str" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
3013                                   [], NoItinerary>;
3014
3015     def _Xm_RegOffset_STR : A64I_LSregoff<size, v, {high_opc, 0b0}, 0b1,
3016                                   (outs), (ins GPR:$Rt, GPR64xsp:$Rn, GPR64:$Rm,
3017                                                params.regextXm:$Ext),
3018                                   "str" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
3019                                   [], NoItinerary>;
3020   }
3021   def : InstAlias<"str" # asmsuffix # " $Rt, [$Rn, $Rm]",
3022       (!cast<Instruction>(prefix # "_Xm_RegOffset_STR") GPR:$Rt, GPR64xsp:$Rn,
3023                                                         GPR64:$Rm, 2)>;
3024
3025   // Unaligned immediate
3026   def _STUR : A64I_LSunalimm<size, v, {high_opc, 0b0},
3027                              (outs), (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3028                              "stur" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
3029                              [], NoItinerary> {
3030     let mayStore = 1;
3031   }
3032   def : InstAlias<"stur" # asmsuffix # " $Rt, [$Rn]",
3033                (!cast<Instruction>(prefix # "_STUR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
3034
3035   def _LDUR : A64I_LSunalimm<size, v, {high_opc, 0b1},
3036                              (outs GPR:$Rt), (ins GPR64xsp:$Rn, simm9:$SImm9),
3037                              "ldur" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
3038                              [], NoItinerary> {
3039     let mayLoad = 1;
3040   }
3041   def : InstAlias<"ldur" # asmsuffix # " $Rt, [$Rn]",
3042                (!cast<Instruction>(prefix # "_LDUR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
3043
3044   // Post-indexed
3045   def _PostInd_STR : A64I_LSpostind<size, v, {high_opc, 0b0},
3046                                (outs GPR64xsp:$Rn_wb),
3047                                (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3048                                "str" # asmsuffix # "\t$Rt, [$Rn], $SImm9",
3049                                [], NoItinerary> {
3050     let Constraints = "$Rn = $Rn_wb";
3051     let mayStore = 1;
3052
3053     // Decoder only needed for unpredictability checking (FIXME).
3054     let DecoderMethod = "DecodeSingleIndexedInstruction";
3055   }
3056
3057   def _PostInd_LDR : A64I_LSpostind<size, v, {high_opc, 0b1},
3058                                     (outs GPR:$Rt, GPR64xsp:$Rn_wb),
3059                                     (ins GPR64xsp:$Rn, simm9:$SImm9),
3060                                     "ldr" # asmsuffix # "\t$Rt, [$Rn], $SImm9",
3061                                     [], NoItinerary> {
3062     let mayLoad = 1;
3063     let Constraints = "$Rn = $Rn_wb";
3064     let DecoderMethod = "DecodeSingleIndexedInstruction";
3065   }
3066
3067   // Pre-indexed
3068   def _PreInd_STR : A64I_LSpreind<size, v, {high_opc, 0b0},
3069                                (outs GPR64xsp:$Rn_wb),
3070                                (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3071                                "str" # asmsuffix # "\t$Rt, [$Rn, $SImm9]!",
3072                                [], NoItinerary> {
3073     let Constraints = "$Rn = $Rn_wb";
3074     let mayStore = 1;
3075
3076     // Decoder only needed for unpredictability checking (FIXME).
3077     let DecoderMethod = "DecodeSingleIndexedInstruction";
3078   }
3079
3080   def _PreInd_LDR : A64I_LSpreind<size, v, {high_opc, 0b1},
3081                                     (outs GPR:$Rt, GPR64xsp:$Rn_wb),
3082                                     (ins GPR64xsp:$Rn, simm9:$SImm9),
3083                                     "ldr" # asmsuffix # "\t$Rt, [$Rn, $SImm9]!",
3084                                     [], NoItinerary> {
3085     let mayLoad = 1;
3086     let Constraints = "$Rn = $Rn_wb";
3087     let DecoderMethod = "DecodeSingleIndexedInstruction";
3088   }
3089
3090 }
3091
3092 // STRB/LDRB: First define the instructions
3093 defm LS8
3094   : A64I_LDRSTR_unsigned<"LS8", 0b00, 0b0, 0b0, "b", GPR32, byte_addrparams>;
3095
3096 // STRH/LDRH
3097 defm LS16
3098   : A64I_LDRSTR_unsigned<"LS16", 0b01, 0b0, 0b0, "h", GPR32, hword_addrparams>;
3099
3100
3101 // STR/LDR to/from a W register
3102 defm LS32
3103   : A64I_LDRSTR_unsigned<"LS32", 0b10, 0b0, 0b0, "", GPR32, word_addrparams>;
3104
3105 // STR/LDR to/from an X register
3106 defm LS64
3107   : A64I_LDRSTR_unsigned<"LS64", 0b11, 0b0, 0b0, "", GPR64, dword_addrparams>;
3108
3109 // STR/LDR to/from a B register
3110 defm LSFP8
3111   : A64I_LDRSTR_unsigned<"LSFP8", 0b00, 0b1, 0b0, "", FPR8, byte_addrparams>;
3112
3113 // STR/LDR to/from an H register
3114 defm LSFP16
3115   : A64I_LDRSTR_unsigned<"LSFP16", 0b01, 0b1, 0b0, "", FPR16, hword_addrparams>;
3116
3117 // STR/LDR to/from an S register
3118 defm LSFP32
3119   : A64I_LDRSTR_unsigned<"LSFP32", 0b10, 0b1, 0b0, "", FPR32, word_addrparams>;
3120 // STR/LDR to/from a D register
3121 defm LSFP64
3122   : A64I_LDRSTR_unsigned<"LSFP64", 0b11, 0b1, 0b0, "", FPR64, dword_addrparams>;
3123 // STR/LDR to/from a Q register
3124 defm LSFP128
3125   : A64I_LDRSTR_unsigned<"LSFP128", 0b00, 0b1, 0b1, "", FPR128,
3126                          qword_addrparams>;
3127
3128 //===------------------------------
3129 // 2.3 Signed loads
3130 //===------------------------------
3131
3132 // Byte and half-word signed loads can both go into either an X or a W register,
3133 // so it's worth factoring out. Signed word loads don't fit because there is no
3134 // W version.
3135 multiclass A64I_LDR_signed<bits<2> size, string asmopcode, AddrParams params,
3136                            string prefix> {
3137   // Unsigned offset
3138   def w : A64I_LSunsigimm<size, 0b0, 0b11,
3139                           (outs GPR32:$Rt),
3140                           (ins GPR64xsp:$Rn, params.uimm12:$UImm12),
3141                           "ldrs" # asmopcode # "\t$Rt, [$Rn, $UImm12]",
3142                           [], NoItinerary> {
3143     let mayLoad = 1;
3144   }
3145   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn]",
3146                   (!cast<Instruction>(prefix # w) GPR32:$Rt, GPR64xsp:$Rn, 0)>;
3147
3148   def x : A64I_LSunsigimm<size, 0b0, 0b10,
3149                           (outs GPR64:$Rt),
3150                           (ins GPR64xsp:$Rn, params.uimm12:$UImm12),
3151                           "ldrs" # asmopcode # "\t$Rt, [$Rn, $UImm12]",
3152                           [], NoItinerary> {
3153     let mayLoad = 1;
3154   }
3155   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn]",
3156                   (!cast<Instruction>(prefix # x) GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3157
3158   // Register offset
3159   let mayLoad = 1 in {
3160     def w_Wm_RegOffset : A64I_LSregoff<size, 0b0, 0b11, 0b0,
3161                             (outs GPR32:$Rt),
3162                             (ins GPR64xsp:$Rn, GPR32:$Rm, params.regextWm:$Ext),
3163                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3164                             [], NoItinerary>;
3165
3166     def w_Xm_RegOffset : A64I_LSregoff<size, 0b0, 0b11, 0b1,
3167                             (outs GPR32:$Rt),
3168                             (ins GPR64xsp:$Rn, GPR64:$Rm, params.regextXm:$Ext),
3169                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3170                             [], NoItinerary>;
3171
3172     def x_Wm_RegOffset : A64I_LSregoff<size, 0b0, 0b10, 0b0,
3173                             (outs GPR64:$Rt),
3174                             (ins GPR64xsp:$Rn, GPR32:$Rm, params.regextWm:$Ext),
3175                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3176                             [], NoItinerary>;
3177
3178     def x_Xm_RegOffset : A64I_LSregoff<size, 0b0, 0b10, 0b1,
3179                             (outs GPR64:$Rt),
3180                             (ins GPR64xsp:$Rn, GPR64:$Rm, params.regextXm:$Ext),
3181                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3182                             [], NoItinerary>;
3183   }
3184   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn, $Rm]",
3185         (!cast<Instruction>(prefix # "w_Xm_RegOffset") GPR32:$Rt, GPR64xsp:$Rn,
3186                                                        GPR64:$Rm, 2)>;
3187
3188   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn, $Rm]",
3189         (!cast<Instruction>(prefix # "x_Xm_RegOffset") GPR64:$Rt, GPR64xsp:$Rn,
3190                                                        GPR64:$Rm, 2)>;
3191
3192
3193   let mayLoad = 1 in {
3194     // Unaligned offset
3195     def w_U : A64I_LSunalimm<size, 0b0, 0b11,
3196                              (outs GPR32:$Rt),
3197                              (ins GPR64xsp:$Rn, simm9:$SImm9),
3198                              "ldurs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3199                              [], NoItinerary>;
3200
3201     def x_U : A64I_LSunalimm<size, 0b0, 0b10,
3202                              (outs GPR64:$Rt),
3203                              (ins GPR64xsp:$Rn, simm9:$SImm9),
3204                              "ldurs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3205                              [], NoItinerary>;
3206
3207
3208     // Post-indexed
3209     def w_PostInd : A64I_LSpostind<size, 0b0, 0b11,
3210                                  (outs GPR32:$Rt, GPR64xsp:$Rn_wb),
3211                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3212                                  "ldrs" # asmopcode # "\t$Rt, [$Rn], $SImm9",
3213                                  [], NoItinerary> {
3214       let Constraints = "$Rn = $Rn_wb";
3215       let DecoderMethod = "DecodeSingleIndexedInstruction";
3216     }
3217
3218     def x_PostInd : A64I_LSpostind<size, 0b0, 0b10,
3219                                    (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3220                                    (ins GPR64xsp:$Rn, simm9:$SImm9),
3221                                    "ldrs" # asmopcode # "\t$Rt, [$Rn], $SImm9",
3222                                    [], NoItinerary> {
3223       let Constraints = "$Rn = $Rn_wb";
3224       let DecoderMethod = "DecodeSingleIndexedInstruction";
3225     }
3226
3227     // Pre-indexed
3228     def w_PreInd : A64I_LSpreind<size, 0b0, 0b11,
3229                                  (outs GPR32:$Rt, GPR64xsp:$Rn_wb),
3230                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3231                                  "ldrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]!",
3232                                  [], NoItinerary> {
3233       let Constraints = "$Rn = $Rn_wb";
3234       let DecoderMethod = "DecodeSingleIndexedInstruction";
3235     }
3236
3237     def x_PreInd : A64I_LSpreind<size, 0b0, 0b10,
3238                                  (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3239                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3240                                  "ldrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]!",
3241                                  [], NoItinerary> {
3242       let Constraints = "$Rn = $Rn_wb";
3243       let DecoderMethod = "DecodeSingleIndexedInstruction";
3244     }
3245   } // let mayLoad = 1
3246 }
3247
3248 // LDRSB
3249 defm LDRSB : A64I_LDR_signed<0b00, "b", byte_addrparams, "LDRSB">;
3250 // LDRSH
3251 defm LDRSH : A64I_LDR_signed<0b01, "h", hword_addrparams, "LDRSH">;
3252
3253 // LDRSW: load a 32-bit register, sign-extending to 64-bits.
3254 def LDRSWx
3255     : A64I_LSunsigimm<0b10, 0b0, 0b10,
3256                     (outs GPR64:$Rt),
3257                     (ins GPR64xsp:$Rn, word_uimm12:$UImm12),
3258                     "ldrsw\t$Rt, [$Rn, $UImm12]",
3259                     [], NoItinerary> {
3260   let mayLoad = 1;
3261 }
3262 def : InstAlias<"ldrsw $Rt, [$Rn]", (LDRSWx GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3263
3264 let mayLoad = 1 in {
3265   def LDRSWx_Wm_RegOffset : A64I_LSregoff<0b10, 0b0, 0b10, 0b0,
3266                              (outs GPR64:$Rt),
3267                              (ins GPR64xsp:$Rn, GPR32:$Rm, word_Wm_regext:$Ext),
3268                              "ldrsw\t$Rt, [$Rn, $Rm, $Ext]",
3269                              [], NoItinerary>;
3270
3271   def LDRSWx_Xm_RegOffset : A64I_LSregoff<0b10, 0b0, 0b10, 0b1,
3272                              (outs GPR64:$Rt),
3273                              (ins GPR64xsp:$Rn, GPR64:$Rm, word_Xm_regext:$Ext),
3274                              "ldrsw\t$Rt, [$Rn, $Rm, $Ext]",
3275                              [], NoItinerary>;
3276 }
3277 def : InstAlias<"ldrsw $Rt, [$Rn, $Rm]",
3278                 (LDRSWx_Xm_RegOffset GPR64:$Rt, GPR64xsp:$Rn, GPR64:$Rm, 2)>;
3279
3280
3281 def LDURSWx
3282     : A64I_LSunalimm<0b10, 0b0, 0b10,
3283                     (outs GPR64:$Rt),
3284                     (ins GPR64xsp:$Rn, simm9:$SImm9),
3285                     "ldursw\t$Rt, [$Rn, $SImm9]",
3286                     [], NoItinerary> {
3287   let mayLoad = 1;
3288 }
3289 def : InstAlias<"ldursw $Rt, [$Rn]", (LDURSWx GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3290
3291 def LDRSWx_PostInd
3292     : A64I_LSpostind<0b10, 0b0, 0b10,
3293                     (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3294                     (ins GPR64xsp:$Rn, simm9:$SImm9),
3295                     "ldrsw\t$Rt, [$Rn], $SImm9",
3296                     [], NoItinerary> {
3297   let mayLoad = 1;
3298   let Constraints = "$Rn = $Rn_wb";
3299   let DecoderMethod = "DecodeSingleIndexedInstruction";
3300 }
3301
3302 def LDRSWx_PreInd : A64I_LSpreind<0b10, 0b0, 0b10,
3303                                  (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3304                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3305                                  "ldrsw\t$Rt, [$Rn, $SImm9]!",
3306                                  [], NoItinerary> {
3307   let mayLoad = 1;
3308   let Constraints = "$Rn = $Rn_wb";
3309   let DecoderMethod = "DecodeSingleIndexedInstruction";
3310 }
3311
3312 //===------------------------------
3313 // 2.4 Prefetch operations
3314 //===------------------------------
3315
3316 def PRFM : A64I_LSunsigimm<0b11, 0b0, 0b10, (outs),
3317                  (ins prefetch_op:$Rt, GPR64xsp:$Rn, dword_uimm12:$UImm12),
3318                  "prfm\t$Rt, [$Rn, $UImm12]",
3319                  [], NoItinerary> {
3320   let mayLoad = 1;
3321 }
3322 def : InstAlias<"prfm $Rt, [$Rn]",
3323                 (PRFM prefetch_op:$Rt, GPR64xsp:$Rn, 0)>;
3324
3325 let mayLoad = 1 in {
3326   def PRFM_Wm_RegOffset : A64I_LSregoff<0b11, 0b0, 0b10, 0b0, (outs),
3327                                         (ins prefetch_op:$Rt, GPR64xsp:$Rn,
3328                                              GPR32:$Rm, dword_Wm_regext:$Ext),
3329                                         "prfm\t$Rt, [$Rn, $Rm, $Ext]",
3330                                         [], NoItinerary>;
3331   def PRFM_Xm_RegOffset : A64I_LSregoff<0b11, 0b0, 0b10, 0b1, (outs),
3332                                         (ins prefetch_op:$Rt, GPR64xsp:$Rn,
3333                                              GPR64:$Rm, dword_Xm_regext:$Ext),
3334                                         "prfm\t$Rt, [$Rn, $Rm, $Ext]",
3335                                         [], NoItinerary>;
3336 }
3337
3338 def : InstAlias<"prfm $Rt, [$Rn, $Rm]",
3339                 (PRFM_Xm_RegOffset prefetch_op:$Rt, GPR64xsp:$Rn,
3340                                    GPR64:$Rm, 2)>;
3341
3342
3343 def PRFUM : A64I_LSunalimm<0b11, 0b0, 0b10, (outs),
3344                          (ins prefetch_op:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3345                          "prfum\t$Rt, [$Rn, $SImm9]",
3346                          [], NoItinerary> {
3347   let mayLoad = 1;
3348 }
3349 def : InstAlias<"prfum $Rt, [$Rn]",
3350                 (PRFUM prefetch_op:$Rt, GPR64xsp:$Rn, 0)>;
3351
3352 //===----------------------------------------------------------------------===//
3353 // Load-store register (unprivileged) instructions
3354 //===----------------------------------------------------------------------===//
3355 // Contains: LDTRB, LDTRH, LDTRSB, LDTRSH, LDTRSW, STTR, STTRB and STTRH
3356
3357 // These instructions very much mirror the "unscaled immediate" loads, but since
3358 // there are no floating-point variants we need to split them out into their own
3359 // section to avoid instantiation of "ldtr d0, [sp]" etc.
3360
3361 multiclass A64I_LDTRSTTR<bits<2> size, string asmsuffix, RegisterClass GPR,
3362                          string prefix> {
3363   def _UnPriv_STR : A64I_LSunpriv<size, 0b0, 0b00,
3364                               (outs), (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3365                               "sttr" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
3366                               [], NoItinerary> {
3367     let mayStore = 1;
3368   }
3369
3370   def : InstAlias<"sttr" # asmsuffix # " $Rt, [$Rn]",
3371          (!cast<Instruction>(prefix # "_UnPriv_STR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
3372
3373   def _UnPriv_LDR : A64I_LSunpriv<size, 0b0, 0b01,
3374                                (outs GPR:$Rt), (ins GPR64xsp:$Rn, simm9:$SImm9),
3375                                "ldtr" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
3376                                [], NoItinerary> {
3377     let mayLoad = 1;
3378   }
3379
3380   def : InstAlias<"ldtr" # asmsuffix # " $Rt, [$Rn]",
3381          (!cast<Instruction>(prefix # "_UnPriv_LDR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
3382
3383 }
3384
3385 // STTRB/LDTRB: First define the instructions
3386 defm LS8 : A64I_LDTRSTTR<0b00, "b", GPR32, "LS8">;
3387
3388 // STTRH/LDTRH
3389 defm LS16 : A64I_LDTRSTTR<0b01, "h", GPR32, "LS16">;
3390
3391 // STTR/LDTR to/from a W register
3392 defm LS32 : A64I_LDTRSTTR<0b10, "", GPR32, "LS32">;
3393
3394 // STTR/LDTR to/from an X register
3395 defm LS64 : A64I_LDTRSTTR<0b11, "", GPR64, "LS64">;
3396
3397 // Now a class for the signed instructions that can go to either 32 or 64
3398 // bits...
3399 multiclass A64I_LDTR_signed<bits<2> size, string asmopcode, string prefix> {
3400   let mayLoad = 1 in {
3401     def w : A64I_LSunpriv<size, 0b0, 0b11,
3402                           (outs GPR32:$Rt),
3403                           (ins GPR64xsp:$Rn, simm9:$SImm9),
3404                           "ldtrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3405                           [], NoItinerary>;
3406
3407     def x : A64I_LSunpriv<size, 0b0, 0b10,
3408                           (outs GPR64:$Rt),
3409                           (ins GPR64xsp:$Rn, simm9:$SImm9),
3410                           "ldtrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3411                           [], NoItinerary>;
3412   }
3413
3414   def : InstAlias<"ldtrs" # asmopcode # " $Rt, [$Rn]",
3415                  (!cast<Instruction>(prefix # "w") GPR32:$Rt, GPR64xsp:$Rn, 0)>;
3416
3417   def : InstAlias<"ldtrs" # asmopcode # " $Rt, [$Rn]",
3418                  (!cast<Instruction>(prefix # "x") GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3419
3420 }
3421
3422 // LDTRSB
3423 defm LDTRSB : A64I_LDTR_signed<0b00, "b", "LDTRSB">;
3424 // LDTRSH
3425 defm LDTRSH : A64I_LDTR_signed<0b01, "h", "LDTRSH">;
3426
3427 // And finally LDTRSW which only goes to 64 bits.
3428 def LDTRSWx : A64I_LSunpriv<0b10, 0b0, 0b10,
3429                             (outs GPR64:$Rt),
3430                             (ins GPR64xsp:$Rn, simm9:$SImm9),
3431                             "ldtrsw\t$Rt, [$Rn, $SImm9]",
3432                             [], NoItinerary> {
3433   let mayLoad = 1;
3434 }
3435 def : InstAlias<"ldtrsw $Rt, [$Rn]", (LDTRSWx GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3436
3437 //===----------------------------------------------------------------------===//
3438 // Load-store register pair (offset) instructions
3439 //===----------------------------------------------------------------------===//
3440 //
3441 // and
3442 //
3443 //===----------------------------------------------------------------------===//
3444 // Load-store register pair (post-indexed) instructions
3445 //===----------------------------------------------------------------------===//
3446 // Contains: STP, LDP, LDPSW
3447 //
3448 // and
3449 //
3450 //===----------------------------------------------------------------------===//
3451 // Load-store register pair (pre-indexed) instructions
3452 //===----------------------------------------------------------------------===//
3453 // Contains: STP, LDP, LDPSW
3454 //
3455 // and
3456 //
3457 //===----------------------------------------------------------------------===//
3458 // Load-store non-temporal register pair (offset) instructions
3459 //===----------------------------------------------------------------------===//
3460 // Contains: STNP, LDNP
3461
3462
3463 // Anything that creates an MCInst (Decoding, selection and AsmParsing) has to
3464 // know the access size via some means. An isolated operand does not have this
3465 // information unless told from here, which means we need separate tablegen
3466 // Operands for each access size. This multiclass takes care of instantiating
3467 // the correct template functions in the rest of the backend.
3468
3469 multiclass offsets_simm7<string MemSize, string prefix> {
3470   // The bare signed 7-bit immediate is used in post-indexed instructions, but
3471   // because of the scaling performed a generic "simm7" operand isn't
3472   // appropriate here either.
3473   def simm7_asmoperand : AsmOperandClass {
3474     let Name = "SImm7_Scaled" # MemSize;
3475     let PredicateMethod = "isSImm7Scaled<" # MemSize # ">";
3476     let RenderMethod = "addSImm7ScaledOperands<" # MemSize # ">";
3477     let DiagnosticType = "LoadStoreSImm7_" # MemSize;
3478   }
3479
3480   def simm7 : Operand<i64> {
3481     let PrintMethod = "printSImm7ScaledOperand<" # MemSize # ">";
3482     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "simm7_asmoperand");
3483   }
3484 }
3485
3486 defm word_  : offsets_simm7<"4", "word_">;
3487 defm dword_ : offsets_simm7<"8", "dword_">;
3488 defm qword_ : offsets_simm7<"16", "qword_">;
3489
3490 multiclass A64I_LSPsimple<bits<2> opc, bit v, RegisterClass SomeReg,
3491                           Operand simm7, string prefix> {
3492   def _STR : A64I_LSPoffset<opc, v, 0b0, (outs),
3493                     (ins SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn, simm7:$SImm7),
3494                     "stp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3495     let mayStore = 1;
3496     let DecoderMethod = "DecodeLDSTPairInstruction";
3497   }
3498   def : InstAlias<"stp $Rt, $Rt2, [$Rn]",
3499                   (!cast<Instruction>(prefix # "_STR") SomeReg:$Rt,
3500                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3501
3502   def _LDR : A64I_LSPoffset<opc, v, 0b1,
3503                             (outs SomeReg:$Rt, SomeReg:$Rt2),
3504                             (ins GPR64xsp:$Rn, simm7:$SImm7),
3505                             "ldp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3506     let mayLoad = 1;
3507     let DecoderMethod = "DecodeLDSTPairInstruction";
3508   }
3509   def : InstAlias<"ldp $Rt, $Rt2, [$Rn]",
3510                   (!cast<Instruction>(prefix # "_LDR") SomeReg:$Rt,
3511                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3512
3513   def _PostInd_STR : A64I_LSPpostind<opc, v, 0b0,
3514                                (outs GPR64xsp:$Rn_wb),
3515                                (ins SomeReg:$Rt, SomeReg:$Rt2,
3516                                     GPR64xsp:$Rn,
3517                                     simm7:$SImm7),
3518                                "stp\t$Rt, $Rt2, [$Rn], $SImm7",
3519                                [], NoItinerary> {
3520     let mayStore = 1;
3521     let Constraints = "$Rn = $Rn_wb";
3522
3523     // Decoder only needed for unpredictability checking (FIXME).
3524     let DecoderMethod = "DecodeLDSTPairInstruction";
3525   }
3526
3527   def _PostInd_LDR : A64I_LSPpostind<opc, v, 0b1,
3528                         (outs SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn_wb),
3529                         (ins GPR64xsp:$Rn, simm7:$SImm7),
3530                         "ldp\t$Rt, $Rt2, [$Rn], $SImm7",
3531                         [], NoItinerary> {
3532     let mayLoad = 1;
3533     let Constraints = "$Rn = $Rn_wb";
3534     let DecoderMethod = "DecodeLDSTPairInstruction";
3535   }
3536
3537   def _PreInd_STR : A64I_LSPpreind<opc, v, 0b0, (outs GPR64xsp:$Rn_wb),
3538                     (ins SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn, simm7:$SImm7),
3539                     "stp\t$Rt, $Rt2, [$Rn, $SImm7]!",
3540                     [], NoItinerary> {
3541     let mayStore = 1;
3542     let Constraints = "$Rn = $Rn_wb";
3543     let DecoderMethod = "DecodeLDSTPairInstruction";
3544   }
3545
3546   def _PreInd_LDR : A64I_LSPpreind<opc, v, 0b1,
3547                               (outs SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn_wb),
3548                               (ins GPR64xsp:$Rn, simm7:$SImm7),
3549                               "ldp\t$Rt, $Rt2, [$Rn, $SImm7]!",
3550                               [], NoItinerary> {
3551     let mayLoad = 1;
3552     let Constraints = "$Rn = $Rn_wb";
3553     let DecoderMethod = "DecodeLDSTPairInstruction";
3554   }
3555
3556   def _NonTemp_STR : A64I_LSPnontemp<opc, v, 0b0, (outs),
3557                     (ins SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn, simm7:$SImm7),
3558                     "stnp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3559     let mayStore = 1;
3560     let DecoderMethod = "DecodeLDSTPairInstruction";
3561   }
3562   def : InstAlias<"stnp $Rt, $Rt2, [$Rn]",
3563                   (!cast<Instruction>(prefix # "_NonTemp_STR") SomeReg:$Rt,
3564                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3565
3566   def _NonTemp_LDR : A64I_LSPnontemp<opc, v, 0b1,
3567                             (outs SomeReg:$Rt, SomeReg:$Rt2),
3568                             (ins GPR64xsp:$Rn, simm7:$SImm7),
3569                             "ldnp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3570     let mayLoad = 1;
3571     let DecoderMethod = "DecodeLDSTPairInstruction";
3572   }
3573   def : InstAlias<"ldnp $Rt, $Rt2, [$Rn]",
3574                   (!cast<Instruction>(prefix # "_NonTemp_LDR") SomeReg:$Rt,
3575                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3576
3577 }
3578
3579
3580 defm LSPair32 : A64I_LSPsimple<0b00, 0b0, GPR32, word_simm7, "LSPair32">;
3581 defm LSPair64 : A64I_LSPsimple<0b10, 0b0, GPR64, dword_simm7, "LSPair64">;
3582 defm LSFPPair32 : A64I_LSPsimple<0b00, 0b1, FPR32, word_simm7, "LSFPPair32">;
3583 defm LSFPPair64 : A64I_LSPsimple<0b01, 0b1, FPR64,  dword_simm7, "LSFPPair64">;
3584 defm LSFPPair128 : A64I_LSPsimple<0b10, 0b1, FPR128, qword_simm7,
3585                                   "LSFPPair128">;
3586
3587
3588 def LDPSWx : A64I_LSPoffset<0b01, 0b0, 0b1,
3589                            (outs GPR64:$Rt, GPR64:$Rt2),
3590                            (ins GPR64xsp:$Rn, word_simm7:$SImm7),
3591                            "ldpsw\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3592   let mayLoad = 1;
3593   let DecoderMethod = "DecodeLDSTPairInstruction";
3594 }
3595 def : InstAlias<"ldpsw $Rt, $Rt2, [$Rn]",
3596                 (LDPSWx GPR64:$Rt, GPR64:$Rt2, GPR64xsp:$Rn, 0)>;
3597
3598 def LDPSWx_PostInd : A64I_LSPpostind<0b01, 0b0, 0b1,
3599                                   (outs GPR64:$Rt, GPR64:$Rt2, GPR64:$Rn_wb),
3600                                   (ins GPR64xsp:$Rn, word_simm7:$SImm7),
3601                                   "ldpsw\t$Rt, $Rt2, [$Rn], $SImm7",
3602                                   [], NoItinerary> {
3603   let mayLoad = 1;
3604   let Constraints = "$Rn = $Rn_wb";
3605   let DecoderMethod = "DecodeLDSTPairInstruction";
3606 }
3607
3608 def LDPSWx_PreInd : A64I_LSPpreind<0b01, 0b0, 0b1,
3609                                    (outs GPR64:$Rt, GPR64:$Rt2, GPR64:$Rn_wb),
3610                                    (ins GPR64xsp:$Rn, word_simm7:$SImm7),
3611                                    "ldpsw\t$Rt, $Rt2, [$Rn, $SImm7]!",
3612                                    [], NoItinerary> {
3613   let mayLoad = 1;
3614   let Constraints = "$Rn = $Rn_wb";
3615   let DecoderMethod = "DecodeLDSTPairInstruction";
3616 }
3617
3618 //===----------------------------------------------------------------------===//
3619 // Logical (immediate) instructions
3620 //===----------------------------------------------------------------------===//
3621 // Contains: AND, ORR, EOR, ANDS, + aliases TST, MOV
3622
3623 multiclass logical_imm_operands<string prefix, string note,
3624                                 int size, ValueType VT> {
3625   def _asmoperand : AsmOperandClass {
3626     let Name = "LogicalImm" # note # size;
3627     let PredicateMethod = "isLogicalImm" # note # "<" # size # ">";
3628     let RenderMethod = "addLogicalImmOperands<" # size # ">";
3629     let DiagnosticType = "LogicalSecondSource";
3630   }
3631
3632   def _operand
3633         : Operand<VT>, ComplexPattern<VT, 1, "SelectLogicalImm", [imm]> {
3634     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_asmoperand");
3635     let PrintMethod = "printLogicalImmOperand<" # size # ">";
3636     let DecoderMethod = "DecodeLogicalImmOperand<" # size # ">";
3637   }
3638 }
3639
3640 defm logical_imm32 : logical_imm_operands<"logical_imm32", "", 32, i32>;
3641 defm logical_imm64 : logical_imm_operands<"logical_imm64", "", 64, i64>;
3642
3643 // The mov versions only differ in assembly parsing, where they
3644 // exclude values representable with either MOVZ or MOVN.
3645 defm logical_imm32_mov
3646   : logical_imm_operands<"logical_imm32_mov", "MOV", 32, i32>;
3647 defm logical_imm64_mov
3648   : logical_imm_operands<"logical_imm64_mov", "MOV", 64, i64>;
3649
3650
3651 multiclass A64I_logimmSizes<bits<2> opc, string asmop, SDNode opnode> {
3652   def wwi : A64I_logicalimm<0b0, opc, (outs GPR32wsp:$Rd),
3653                          (ins GPR32:$Rn, logical_imm32_operand:$Imm),
3654                          !strconcat(asmop, "\t$Rd, $Rn, $Imm"),
3655                          [(set i32:$Rd,
3656                                (opnode i32:$Rn, logical_imm32_operand:$Imm))],
3657                          NoItinerary>;
3658
3659   def xxi : A64I_logicalimm<0b1, opc, (outs GPR64xsp:$Rd),
3660                          (ins GPR64:$Rn, logical_imm64_operand:$Imm),
3661                          !strconcat(asmop, "\t$Rd, $Rn, $Imm"),
3662                          [(set i64:$Rd,
3663                                (opnode i64:$Rn, logical_imm64_operand:$Imm))],
3664                          NoItinerary>;
3665 }
3666
3667 defm AND : A64I_logimmSizes<0b00, "and", and>;
3668 defm ORR : A64I_logimmSizes<0b01, "orr", or>;
3669 defm EOR : A64I_logimmSizes<0b10, "eor", xor>;
3670
3671 let Defs = [NZCV] in {
3672   def ANDSwwi : A64I_logicalimm<0b0, 0b11, (outs GPR32:$Rd),
3673                                 (ins GPR32:$Rn, logical_imm32_operand:$Imm),
3674                                 "ands\t$Rd, $Rn, $Imm",
3675                                 [], NoItinerary>;
3676
3677   def ANDSxxi : A64I_logicalimm<0b1, 0b11, (outs GPR64:$Rd),
3678                                 (ins GPR64:$Rn, logical_imm64_operand:$Imm),
3679                                 "ands\t$Rd, $Rn, $Imm",
3680                                 [], NoItinerary>;
3681 }
3682
3683
3684 def : InstAlias<"tst $Rn, $Imm",
3685                 (ANDSwwi WZR, GPR32:$Rn, logical_imm32_operand:$Imm)>;
3686 def : InstAlias<"tst $Rn, $Imm",
3687                 (ANDSxxi XZR, GPR64:$Rn, logical_imm64_operand:$Imm)>;
3688 def : InstAlias<"mov $Rd, $Imm",
3689                 (ORRwwi GPR32wsp:$Rd, WZR, logical_imm32_mov_operand:$Imm)>;
3690 def : InstAlias<"mov $Rd, $Imm",
3691                 (ORRxxi GPR64xsp:$Rd, XZR, logical_imm64_mov_operand:$Imm)>;
3692
3693 //===----------------------------------------------------------------------===//
3694 // Logical (shifted register) instructions
3695 //===----------------------------------------------------------------------===//
3696 // Contains: AND, BIC, ORR, ORN, EOR, EON, ANDS, BICS + aliases TST, MVN, MOV
3697
3698 // Operand for optimizing (icmp (and LHS, RHS), 0, SomeCode). In theory "ANDS"
3699 // behaves differently for unsigned comparisons, so we defensively only allow
3700 // signed or n/a as the operand. In practice "unsigned greater than 0" is "not
3701 // equal to 0" and LLVM gives us this.
3702 def signed_cond : PatLeaf<(cond), [{
3703   return !isUnsignedIntSetCC(N->get());
3704 }]>;
3705
3706
3707 // These instructions share their "shift" operands with add/sub (shifted
3708 // register instructions). They are defined there.
3709
3710 // N.b. the commutable parameter is just !N. It will be first against the wall
3711 // when the revolution comes.
3712 multiclass logical_shifts<string prefix, bit sf, bits<2> opc,
3713                           bit N, bit commutable,
3714                           string asmop, SDPatternOperator opfrag, ValueType ty,
3715                           RegisterClass GPR, list<Register> defs> {
3716   let isCommutable = commutable, Defs = defs in {
3717   def _lsl : A64I_logicalshift<sf, opc, 0b00, N,
3718                        (outs GPR:$Rd),
3719                        (ins GPR:$Rn, GPR:$Rm,
3720                             !cast<Operand>("lsl_operand_" # ty):$Imm6),
3721                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3722                        [(set ty:$Rd, (opfrag ty:$Rn, (shl ty:$Rm,
3723                             !cast<Operand>("lsl_operand_" # ty):$Imm6))
3724                        )],
3725                        NoItinerary>;
3726
3727   def _lsr : A64I_logicalshift<sf, opc, 0b01, N,
3728                        (outs GPR:$Rd),
3729                        (ins GPR:$Rn, GPR:$Rm,
3730                             !cast<Operand>("lsr_operand_" # ty):$Imm6),
3731                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3732                        [(set ty:$Rd, (opfrag ty:$Rn, (srl ty:$Rm,
3733                             !cast<Operand>("lsr_operand_" # ty):$Imm6))
3734                        )],
3735                        NoItinerary>;
3736
3737   def _asr : A64I_logicalshift<sf, opc, 0b10, N,
3738                        (outs GPR:$Rd),
3739                        (ins GPR:$Rn, GPR:$Rm,
3740                             !cast<Operand>("asr_operand_" # ty):$Imm6),
3741                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3742                        [(set ty:$Rd, (opfrag ty:$Rn, (sra ty:$Rm,
3743                             !cast<Operand>("asr_operand_" # ty):$Imm6))
3744                        )],
3745                        NoItinerary>;
3746
3747   def _ror : A64I_logicalshift<sf, opc, 0b11, N,
3748                        (outs GPR:$Rd),
3749                        (ins GPR:$Rn, GPR:$Rm,
3750                             !cast<Operand>("ror_operand_" # ty):$Imm6),
3751                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3752                        [(set ty:$Rd, (opfrag ty:$Rn, (rotr ty:$Rm,
3753                             !cast<Operand>("ror_operand_" # ty):$Imm6))
3754                        )],
3755                        NoItinerary>;
3756   }
3757
3758   def _noshift
3759       : InstAlias<!strconcat(asmop, " $Rd, $Rn, $Rm"),
3760                  (!cast<Instruction>(prefix # "_lsl") GPR:$Rd, GPR:$Rn,
3761                                                       GPR:$Rm, 0)>;
3762
3763   def : Pat<(opfrag ty:$Rn, ty:$Rm),
3764             (!cast<Instruction>(prefix # "_lsl") $Rn, $Rm, 0)>;
3765 }
3766
3767 multiclass logical_sizes<string prefix, bits<2> opc, bit N, bit commutable,
3768                          string asmop, SDPatternOperator opfrag,
3769                          list<Register> defs> {
3770   defm xxx : logical_shifts<prefix # "xxx", 0b1, opc, N,
3771                             commutable, asmop, opfrag, i64, GPR64, defs>;
3772   defm www : logical_shifts<prefix # "www", 0b0, opc, N,
3773                             commutable, asmop, opfrag, i32, GPR32, defs>;
3774 }
3775
3776
3777 defm AND : logical_sizes<"AND", 0b00, 0b0, 0b1, "and", and, []>;
3778 defm ORR : logical_sizes<"ORR", 0b01, 0b0, 0b1, "orr", or, []>;
3779 defm EOR : logical_sizes<"EOR", 0b10, 0b0, 0b1, "eor", xor, []>;
3780 defm ANDS : logical_sizes<"ANDS", 0b11, 0b0, 0b1, "ands",
3781              PatFrag<(ops node:$lhs, node:$rhs), (and node:$lhs, node:$rhs),
3782                      [{ (void)N; return false; }]>,
3783              [NZCV]>;
3784
3785 defm BIC : logical_sizes<"BIC", 0b00, 0b1, 0b0, "bic",
3786                          PatFrag<(ops node:$lhs, node:$rhs),
3787                                  (and node:$lhs, (not node:$rhs))>, []>;
3788 defm ORN : logical_sizes<"ORN", 0b01, 0b1, 0b0, "orn",
3789                          PatFrag<(ops node:$lhs, node:$rhs),
3790                                  (or node:$lhs, (not node:$rhs))>, []>;
3791 defm EON : logical_sizes<"EON", 0b10, 0b1, 0b0, "eon",
3792                          PatFrag<(ops node:$lhs, node:$rhs),
3793                                  (xor node:$lhs, (not node:$rhs))>, []>;
3794 defm BICS : logical_sizes<"BICS", 0b11, 0b1, 0b0, "bics",
3795                           PatFrag<(ops node:$lhs, node:$rhs),
3796                                   (and node:$lhs, (not node:$rhs)),
3797                                   [{ (void)N; return false; }]>,
3798                           [NZCV]>;
3799
3800 multiclass tst_shifts<string prefix, bit sf, ValueType ty, RegisterClass GPR> {
3801   let isCommutable = 1, Rd = 0b11111, Defs = [NZCV] in {
3802   def _lsl : A64I_logicalshift<sf, 0b11, 0b00, 0b0,
3803                        (outs),
3804                        (ins GPR:$Rn, GPR:$Rm,
3805                             !cast<Operand>("lsl_operand_" # ty):$Imm6),
3806                        "tst\t$Rn, $Rm, $Imm6",
3807                        [(set NZCV, (A64setcc (and ty:$Rn, (shl ty:$Rm,
3808                            !cast<Operand>("lsl_operand_" # ty):$Imm6)),
3809                                           0, signed_cond))],
3810                        NoItinerary>;
3811
3812
3813   def _lsr : A64I_logicalshift<sf, 0b11, 0b01, 0b0,
3814                        (outs),
3815                        (ins GPR:$Rn, GPR:$Rm,
3816                             !cast<Operand>("lsr_operand_" # ty):$Imm6),
3817                        "tst\t$Rn, $Rm, $Imm6",
3818                        [(set NZCV, (A64setcc (and ty:$Rn, (srl ty:$Rm,
3819                            !cast<Operand>("lsr_operand_" # ty):$Imm6)),
3820                                           0, signed_cond))],
3821                        NoItinerary>;
3822
3823   def _asr : A64I_logicalshift<sf, 0b11, 0b10, 0b0,
3824                        (outs),
3825                        (ins GPR:$Rn, GPR:$Rm,
3826                             !cast<Operand>("asr_operand_" # ty):$Imm6),
3827                        "tst\t$Rn, $Rm, $Imm6",
3828                        [(set NZCV, (A64setcc (and ty:$Rn, (sra ty:$Rm,
3829                            !cast<Operand>("asr_operand_" # ty):$Imm6)),
3830                                           0, signed_cond))],
3831                        NoItinerary>;
3832
3833   def _ror : A64I_logicalshift<sf, 0b11, 0b11, 0b0,
3834                        (outs),
3835                        (ins GPR:$Rn, GPR:$Rm,
3836                             !cast<Operand>("ror_operand_" # ty):$Imm6),
3837                        "tst\t$Rn, $Rm, $Imm6",
3838                        [(set NZCV, (A64setcc (and ty:$Rn, (rotr ty:$Rm,
3839                            !cast<Operand>("ror_operand_" # ty):$Imm6)),
3840                                           0, signed_cond))],
3841                        NoItinerary>;
3842   }
3843
3844   def _noshift : InstAlias<"tst $Rn, $Rm",
3845                      (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
3846
3847   def : Pat<(A64setcc (and ty:$Rn, ty:$Rm), 0, signed_cond),
3848             (!cast<Instruction>(prefix # "_lsl") $Rn, $Rm, 0)>;
3849 }
3850
3851 defm TSTxx : tst_shifts<"TSTxx", 0b1, i64, GPR64>;
3852 defm TSTww : tst_shifts<"TSTww", 0b0, i32, GPR32>;
3853
3854
3855 multiclass mvn_shifts<string prefix, bit sf, ValueType ty, RegisterClass GPR> {
3856   let isCommutable = 0, Rn = 0b11111 in {
3857   def _lsl : A64I_logicalshift<sf, 0b01, 0b00, 0b1,
3858                        (outs GPR:$Rd),
3859                        (ins GPR:$Rm,
3860                             !cast<Operand>("lsl_operand_" # ty):$Imm6),
3861                        "mvn\t$Rd, $Rm, $Imm6",
3862                        [(set ty:$Rd, (not (shl ty:$Rm,
3863                          !cast<Operand>("lsl_operand_" # ty):$Imm6)))],
3864                        NoItinerary>;
3865
3866
3867   def _lsr : A64I_logicalshift<sf, 0b01, 0b01, 0b1,
3868                        (outs GPR:$Rd),
3869                        (ins GPR:$Rm,
3870                             !cast<Operand>("lsr_operand_" # ty):$Imm6),
3871                        "mvn\t$Rd, $Rm, $Imm6",
3872                        [(set ty:$Rd, (not (srl ty:$Rm,
3873                          !cast<Operand>("lsr_operand_" # ty):$Imm6)))],
3874                        NoItinerary>;
3875
3876   def _asr : A64I_logicalshift<sf, 0b01, 0b10, 0b1,
3877                        (outs GPR:$Rd),
3878                        (ins GPR:$Rm,
3879                             !cast<Operand>("asr_operand_" # ty):$Imm6),
3880                        "mvn\t$Rd, $Rm, $Imm6",
3881                        [(set ty:$Rd, (not (sra ty:$Rm,
3882                          !cast<Operand>("asr_operand_" # ty):$Imm6)))],
3883                        NoItinerary>;
3884
3885   def _ror : A64I_logicalshift<sf, 0b01, 0b11, 0b1,
3886                        (outs GPR:$Rd),
3887                        (ins GPR:$Rm,
3888                             !cast<Operand>("ror_operand_" # ty):$Imm6),
3889                        "mvn\t$Rd, $Rm, $Imm6",
3890                        [(set ty:$Rd, (not (rotr ty:$Rm,
3891                          !cast<Operand>("lsl_operand_" # ty):$Imm6)))],
3892                        NoItinerary>;
3893   }
3894
3895   def _noshift : InstAlias<"mvn $Rn, $Rm",
3896                      (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
3897
3898   def : Pat<(not ty:$Rm),
3899             (!cast<Instruction>(prefix # "_lsl") $Rm, 0)>;
3900 }
3901
3902 defm MVNxx : mvn_shifts<"MVNxx", 0b1, i64, GPR64>;
3903 defm MVNww : mvn_shifts<"MVNww", 0b0, i32, GPR32>;
3904
3905 def MOVxx :InstAlias<"mov $Rd, $Rm", (ORRxxx_lsl GPR64:$Rd, XZR, GPR64:$Rm, 0)>;
3906 def MOVww :InstAlias<"mov $Rd, $Rm", (ORRwww_lsl GPR32:$Rd, WZR, GPR32:$Rm, 0)>;
3907
3908 //===----------------------------------------------------------------------===//
3909 // Move wide (immediate) instructions
3910 //===----------------------------------------------------------------------===//
3911 // Contains: MOVN, MOVZ, MOVK + MOV aliases
3912
3913 // A wide variety of different relocations are needed for variants of these
3914 // instructions, so it turns out that we need a different operand for all of
3915 // them.
3916 multiclass movw_operands<string prefix, string instname, int width> {
3917   def _imm_asmoperand : AsmOperandClass {
3918     let Name = instname # width # "Shifted" # shift;
3919     let PredicateMethod = "is" # instname # width # "Imm";
3920     let RenderMethod = "addMoveWideImmOperands";
3921     let ParserMethod = "ParseImmWithLSLOperand";
3922     let DiagnosticType = "MOVWUImm16";
3923   }
3924
3925   def _imm : Operand<i64> {
3926     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_imm_asmoperand");
3927     let PrintMethod = "printMoveWideImmOperand";
3928     let EncoderMethod = "getMoveWideImmOpValue";
3929     let DecoderMethod = "DecodeMoveWideImmOperand<" # width # ">";
3930
3931     let MIOperandInfo = (ops uimm16:$UImm16, imm:$Shift);
3932   }
3933 }
3934
3935 defm movn32 : movw_operands<"movn32", "MOVN", 32>;
3936 defm movn64 : movw_operands<"movn64", "MOVN", 64>;
3937 defm movz32 : movw_operands<"movz32", "MOVZ", 32>;
3938 defm movz64 : movw_operands<"movz64", "MOVZ", 64>;
3939 defm movk32 : movw_operands<"movk32", "MOVK", 32>;
3940 defm movk64 : movw_operands<"movk64", "MOVK", 64>;
3941
3942 multiclass A64I_movwSizes<bits<2> opc, string asmop, dag ins32bit,
3943                           dag ins64bit> {
3944
3945   def wii : A64I_movw<0b0, opc, (outs GPR32:$Rd), ins32bit,
3946                       !strconcat(asmop, "\t$Rd, $FullImm"),
3947                       [], NoItinerary> {
3948     bits<18> FullImm;
3949     let UImm16 = FullImm{15-0};
3950     let Shift = FullImm{17-16};
3951   }
3952
3953   def xii : A64I_movw<0b1, opc, (outs GPR64:$Rd), ins64bit,
3954                       !strconcat(asmop, "\t$Rd, $FullImm"),
3955                       [], NoItinerary> {
3956     bits<18> FullImm;
3957     let UImm16 = FullImm{15-0};
3958     let Shift = FullImm{17-16};
3959   }
3960 }
3961
3962 let isMoveImm = 1, isReMaterializable = 1,
3963     isAsCheapAsAMove = 1, hasSideEffects = 0 in {
3964   defm MOVN : A64I_movwSizes<0b00, "movn",
3965                              (ins movn32_imm:$FullImm),
3966                              (ins movn64_imm:$FullImm)>;
3967
3968   // Some relocations are able to convert between a MOVZ and a MOVN. If these
3969   // are applied the instruction must be emitted with the corresponding bits as
3970   // 0, which means a MOVZ needs to override that bit from the default.
3971   let PostEncoderMethod = "fixMOVZ" in
3972   defm MOVZ : A64I_movwSizes<0b10, "movz",
3973                              (ins movz32_imm:$FullImm),
3974                              (ins movz64_imm:$FullImm)>;
3975 }
3976
3977 let Constraints = "$src = $Rd" in
3978 defm MOVK : A64I_movwSizes<0b11, "movk",
3979                            (ins GPR32:$src, movk32_imm:$FullImm),
3980                            (ins GPR64:$src, movk64_imm:$FullImm)>;
3981
3982
3983 // And now the "MOV" aliases. These also need their own operands because what
3984 // they accept is completely different to what the base instructions accept.
3985 multiclass movalias_operand<string prefix, string basename,
3986                             string immpredicate, int width> {
3987   def _asmoperand : AsmOperandClass {
3988     let Name = basename # width # "MovAlias";
3989     let PredicateMethod
3990           = "isMoveWideMovAlias<" # width # ", A64Imms::" # immpredicate # ">";
3991     let RenderMethod
3992       = "addMoveWideMovAliasOperands<" # width # ", "
3993                                        # "A64Imms::" # immpredicate # ">";
3994   }
3995
3996   def _movimm : Operand<i64> {
3997     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_asmoperand");
3998
3999     let MIOperandInfo = (ops uimm16:$UImm16, imm:$Shift);
4000   }
4001 }
4002
4003 defm movz32 : movalias_operand<"movz32", "MOVZ", "isMOVZImm", 32>;
4004 defm movz64 : movalias_operand<"movz64", "MOVZ", "isMOVZImm", 64>;
4005 defm movn32 : movalias_operand<"movn32", "MOVN", "isOnlyMOVNImm", 32>;
4006 defm movn64 : movalias_operand<"movn64", "MOVN", "isOnlyMOVNImm", 64>;
4007
4008 // FIXME: these are officially canonical aliases, but TableGen is too limited to
4009 // print them at the moment. I believe in this case an "AliasPredicate" method
4010 // will need to be implemented. to allow it, as well as the more generally
4011 // useful handling of non-register, non-constant operands.
4012 class movalias<Instruction INST, RegisterClass GPR, Operand operand>
4013   : InstAlias<"mov $Rd, $FullImm", (INST GPR:$Rd, operand:$FullImm)>;
4014
4015 def : movalias<MOVZwii, GPR32, movz32_movimm>;
4016 def : movalias<MOVZxii, GPR64, movz64_movimm>;
4017 def : movalias<MOVNwii, GPR32, movn32_movimm>;
4018 def : movalias<MOVNxii, GPR64, movn64_movimm>;
4019
4020 def movw_addressref_g0 : ComplexPattern<i64, 2, "SelectMOVWAddressRef<0>">;
4021 def movw_addressref_g1 : ComplexPattern<i64, 2, "SelectMOVWAddressRef<1>">;
4022 def movw_addressref_g2 : ComplexPattern<i64, 2, "SelectMOVWAddressRef<2>">;
4023 def movw_addressref_g3 : ComplexPattern<i64, 2, "SelectMOVWAddressRef<3>">;
4024
4025 def : Pat<(A64WrapperLarge movw_addressref_g3:$G3, movw_addressref_g2:$G2,
4026                            movw_addressref_g1:$G1, movw_addressref_g0:$G0),
4027           (MOVKxii (MOVKxii (MOVKxii (MOVZxii movw_addressref_g3:$G3),
4028                                      movw_addressref_g2:$G2),
4029                             movw_addressref_g1:$G1),
4030                    movw_addressref_g0:$G0)>;
4031
4032 //===----------------------------------------------------------------------===//
4033 // PC-relative addressing instructions
4034 //===----------------------------------------------------------------------===//
4035 // Contains: ADR, ADRP
4036
4037 def adr_label : Operand<i64> {
4038   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_adr_prel>";
4039
4040   // This label is a 21-bit offset from PC, unscaled
4041   let PrintMethod = "printLabelOperand<21, 1>";
4042   let ParserMatchClass = label_asmoperand<21, 1>;
4043   let OperandType = "OPERAND_PCREL";
4044 }
4045
4046 def adrp_label_asmoperand : AsmOperandClass {
4047   let Name = "AdrpLabel";
4048   let RenderMethod = "addLabelOperands<21, 4096>";
4049   let DiagnosticType = "Label";
4050 }
4051
4052 def adrp_label : Operand<i64> {
4053   let EncoderMethod = "getAdrpLabelOpValue";
4054
4055   // This label is a 21-bit offset from PC, scaled by the page-size: 4096.
4056   let PrintMethod = "printLabelOperand<21, 4096>";
4057   let ParserMatchClass = adrp_label_asmoperand;
4058   let OperandType = "OPERAND_PCREL";
4059 }
4060
4061 let hasSideEffects = 0 in {
4062   def ADRxi : A64I_PCADR<0b0, (outs GPR64:$Rd), (ins adr_label:$Label),
4063                          "adr\t$Rd, $Label", [], NoItinerary>;
4064
4065   def ADRPxi : A64I_PCADR<0b1, (outs GPR64:$Rd), (ins adrp_label:$Label),
4066                           "adrp\t$Rd, $Label", [], NoItinerary>;
4067 }
4068
4069 //===----------------------------------------------------------------------===//
4070 // System instructions
4071 //===----------------------------------------------------------------------===//
4072 // Contains: HINT, CLREX, DSB, DMB, ISB, MSR, SYS, SYSL, MRS
4073 //    + aliases IC, DC, AT, TLBI, NOP, YIELD, WFE, WFI, SEV, SEVL
4074
4075 // Op1 and Op2 fields are sometimes simple 3-bit unsigned immediate values.
4076 def uimm3_asmoperand : AsmOperandClass {
4077   let Name = "UImm3";
4078   let PredicateMethod = "isUImm<3>";
4079   let RenderMethod = "addImmOperands";
4080   let DiagnosticType = "UImm3";
4081 }
4082
4083 def uimm3 : Operand<i32> {
4084   let ParserMatchClass = uimm3_asmoperand;
4085 }
4086
4087 // The HINT alias can accept a simple unsigned 7-bit immediate.
4088 def uimm7_asmoperand : AsmOperandClass {
4089   let Name = "UImm7";
4090   let PredicateMethod = "isUImm<7>";
4091   let RenderMethod = "addImmOperands";
4092   let DiagnosticType = "UImm7";
4093 }
4094
4095 def uimm7 : Operand<i32> {
4096   let ParserMatchClass = uimm7_asmoperand;
4097 }
4098
4099 // Multiclass namedimm is defined with the prefetch operands. Most of these fit
4100 // into the NamedImmMapper scheme well: they either accept a named operand or
4101 // any immediate under a particular value (which may be 0, implying no immediate
4102 // is allowed).
4103 defm dbarrier : namedimm<"dbarrier", "A64DB::DBarrierMapper">;
4104 defm isb : namedimm<"isb", "A64ISB::ISBMapper">;
4105 defm ic : namedimm<"ic", "A64IC::ICMapper">;
4106 defm dc : namedimm<"dc", "A64DC::DCMapper">;
4107 defm at : namedimm<"at", "A64AT::ATMapper">;
4108 defm tlbi : namedimm<"tlbi", "A64TLBI::TLBIMapper">;
4109
4110 // However, MRS and MSR are more complicated for a few reasons:
4111 //   * There are ~1000 generic names S3_<op1>_<CRn>_<CRm>_<Op2> which have an
4112 //     implementation-defined effect
4113 //   * Most registers are shared, but some are read-only or write-only.
4114 //   * There is a variant of MSR which accepts the same register name (SPSel),
4115 //     but which would have a different encoding.
4116
4117 // In principle these could be resolved in with more complicated subclasses of
4118 // NamedImmMapper, however that imposes an overhead on other "named
4119 // immediates". Both in concrete terms with virtual tables and in unnecessary
4120 // abstraction.
4121
4122 // The solution adopted here is to take the MRS/MSR Mappers out of the usual
4123 // hierarchy (they're not derived from NamedImmMapper) and to add logic for
4124 // their special situation.
4125 def mrs_asmoperand : AsmOperandClass {
4126   let Name = "MRS";
4127   let ParserMethod = "ParseSysRegOperand";
4128   let DiagnosticType = "MRS";
4129 }
4130
4131 def mrs_op : Operand<i32> {
4132   let ParserMatchClass = mrs_asmoperand;
4133   let PrintMethod = "printMRSOperand";
4134   let DecoderMethod = "DecodeMRSOperand";
4135 }
4136
4137 def msr_asmoperand : AsmOperandClass {
4138   let Name = "MSRWithReg";
4139
4140   // Note that SPSel is valid for both this and the pstate operands, but with
4141   // different immediate encodings. This is why these operands provide a string
4142   // AArch64Operand rather than an immediate. The overlap is small enough that
4143   // it could be resolved with hackery now, but who can say in future?
4144   let ParserMethod = "ParseSysRegOperand";
4145   let DiagnosticType = "MSR";
4146 }
4147
4148 def msr_op : Operand<i32> {
4149   let ParserMatchClass = msr_asmoperand;
4150   let PrintMethod = "printMSROperand";
4151   let DecoderMethod = "DecodeMSROperand";
4152 }
4153
4154 def pstate_asmoperand : AsmOperandClass {
4155   let Name = "MSRPState";
4156   // See comment above about parser.
4157   let ParserMethod = "ParseSysRegOperand";
4158   let DiagnosticType = "MSR";
4159 }
4160
4161 def pstate_op : Operand<i32> {
4162   let ParserMatchClass = pstate_asmoperand;
4163   let PrintMethod = "printNamedImmOperand<A64PState::PStateMapper>";
4164   let DecoderMethod = "DecodeNamedImmOperand<A64PState::PStateMapper>";
4165 }
4166
4167 // When <CRn> is specified, an assembler should accept something like "C4", not
4168 // the usual "#4" immediate.
4169 def CRx_asmoperand : AsmOperandClass {
4170   let Name = "CRx";
4171   let PredicateMethod = "isUImm<4>";
4172   let RenderMethod = "addImmOperands";
4173   let ParserMethod = "ParseCRxOperand";
4174   // Diagnostics are handled in all cases by ParseCRxOperand.
4175 }
4176
4177 def CRx : Operand<i32> {
4178   let ParserMatchClass = CRx_asmoperand;
4179   let PrintMethod = "printCRxOperand";
4180 }
4181
4182
4183 // Finally, we can start defining the instructions.
4184
4185 // HINT is straightforward, with a few aliases.
4186 def HINTi : A64I_system<0b0, (outs), (ins uimm7:$UImm7), "hint\t$UImm7",
4187                         [], NoItinerary> {
4188   bits<7> UImm7;
4189   let CRm = UImm7{6-3};
4190   let Op2 = UImm7{2-0};
4191
4192   let Op0 = 0b00;
4193   let Op1 = 0b011;
4194   let CRn = 0b0010;
4195   let Rt = 0b11111;
4196 }
4197
4198 def : InstAlias<"nop", (HINTi 0)>;
4199 def : InstAlias<"yield", (HINTi 1)>;
4200 def : InstAlias<"wfe", (HINTi 2)>;
4201 def : InstAlias<"wfi", (HINTi 3)>;
4202 def : InstAlias<"sev", (HINTi 4)>;
4203 def : InstAlias<"sevl", (HINTi 5)>;
4204
4205 // Quite a few instructions then follow a similar pattern of fixing common
4206 // fields in the bitpattern, we'll define a helper-class for them.
4207 class simple_sys<bits<2> op0, bits<3> op1, bits<4> crn, bits<3> op2,
4208                  Operand operand, string asmop>
4209   : A64I_system<0b0, (outs), (ins operand:$CRm), !strconcat(asmop, "\t$CRm"),
4210                 [], NoItinerary> {
4211   let Op0 = op0;
4212   let Op1 = op1;
4213   let CRn = crn;
4214   let Op2 = op2;
4215   let Rt = 0b11111;
4216 }
4217
4218
4219 def CLREXi : simple_sys<0b00, 0b011, 0b0011, 0b010, uimm4, "clrex">;
4220 def DSBi : simple_sys<0b00, 0b011, 0b0011, 0b100, dbarrier_op, "dsb">;
4221 def DMBi : simple_sys<0b00, 0b011, 0b0011, 0b101, dbarrier_op, "dmb">;
4222 def ISBi : simple_sys<0b00, 0b011, 0b0011, 0b110, isb_op, "isb">;
4223
4224 def : InstAlias<"clrex", (CLREXi 0b1111)>;
4225 def : InstAlias<"isb", (ISBi 0b1111)>;
4226
4227 // (DMBi 0xb) is a "DMB ISH" instruciton, appropriate for Linux SMP
4228 // configurations at least.
4229 def : Pat<(atomic_fence imm, imm), (DMBi 0xb)>;
4230
4231 // Any SYS bitpattern can be represented with a complex and opaque "SYS"
4232 // instruction.
4233 def SYSiccix : A64I_system<0b0, (outs),
4234                            (ins uimm3:$Op1, CRx:$CRn, CRx:$CRm,
4235                                 uimm3:$Op2, GPR64:$Rt),
4236                            "sys\t$Op1, $CRn, $CRm, $Op2, $Rt",
4237                            [], NoItinerary> {
4238   let Op0 = 0b01;
4239 }
4240
4241 // You can skip the Xt argument whether it makes sense or not for the generic
4242 // SYS instruction.
4243 def : InstAlias<"sys $Op1, $CRn, $CRm, $Op2",
4244                 (SYSiccix uimm3:$Op1, CRx:$CRn, CRx:$CRm, uimm3:$Op2, XZR)>;
4245
4246
4247 // But many have aliases, which obviously don't fit into
4248 class SYSalias<dag ins, string asmstring>
4249   : A64I_system<0b0, (outs), ins, asmstring, [], NoItinerary> {
4250   let isAsmParserOnly = 1;
4251
4252   bits<14> SysOp;
4253   let Op0 = 0b01;
4254   let Op1 = SysOp{13-11};
4255   let CRn = SysOp{10-7};
4256   let CRm = SysOp{6-3};
4257   let Op2 = SysOp{2-0};
4258 }
4259
4260 def ICix : SYSalias<(ins ic_op:$SysOp, GPR64:$Rt), "ic\t$SysOp, $Rt">;
4261
4262 def ICi : SYSalias<(ins ic_op:$SysOp), "ic\t$SysOp"> {
4263   let Rt = 0b11111;
4264 }
4265
4266 def DCix : SYSalias<(ins dc_op:$SysOp, GPR64:$Rt), "dc\t$SysOp, $Rt">;
4267 def ATix : SYSalias<(ins at_op:$SysOp, GPR64:$Rt), "at\t$SysOp, $Rt">;
4268
4269 def TLBIix : SYSalias<(ins tlbi_op:$SysOp, GPR64:$Rt), "tlbi\t$SysOp, $Rt">;
4270
4271 def TLBIi : SYSalias<(ins tlbi_op:$SysOp), "tlbi\t$SysOp"> {
4272   let Rt = 0b11111;
4273 }
4274
4275
4276 def SYSLxicci : A64I_system<0b1, (outs GPR64:$Rt),
4277                             (ins uimm3:$Op1, CRx:$CRn, CRx:$CRm, uimm3:$Op2),
4278                             "sysl\t$Rt, $Op1, $CRn, $CRm, $Op2",
4279                             [], NoItinerary> {
4280   let Op0 = 0b01;
4281 }
4282
4283 // The instructions themselves are rather simple for MSR and MRS.
4284 def MSRix : A64I_system<0b0, (outs), (ins msr_op:$SysReg, GPR64:$Rt),
4285                         "msr\t$SysReg, $Rt", [], NoItinerary> {
4286   bits<16> SysReg;
4287   let Op0 = SysReg{15-14};
4288   let Op1 = SysReg{13-11};
4289   let CRn = SysReg{10-7};
4290   let CRm = SysReg{6-3};
4291   let Op2 = SysReg{2-0};
4292 }
4293
4294 def MRSxi : A64I_system<0b1, (outs GPR64:$Rt), (ins mrs_op:$SysReg),
4295                         "mrs\t$Rt, $SysReg", [], NoItinerary> {
4296   bits<16> SysReg;
4297   let Op0 = SysReg{15-14};
4298   let Op1 = SysReg{13-11};
4299   let CRn = SysReg{10-7};
4300   let CRm = SysReg{6-3};
4301   let Op2 = SysReg{2-0};
4302 }
4303
4304 def MSRii : A64I_system<0b0, (outs), (ins pstate_op:$PState, uimm4:$CRm),
4305                         "msr\t$PState, $CRm", [], NoItinerary> {
4306   bits<6> PState;
4307
4308   let Op0 = 0b00;
4309   let Op1 = PState{5-3};
4310   let CRn = 0b0100;
4311   let Op2 = PState{2-0};
4312   let Rt = 0b11111;
4313 }
4314
4315 //===----------------------------------------------------------------------===//
4316 // Test & branch (immediate) instructions
4317 //===----------------------------------------------------------------------===//
4318 // Contains: TBZ, TBNZ
4319
4320 // The bit to test is a simple unsigned 6-bit immediate in the X-register
4321 // versions.
4322 def uimm6 : Operand<i64> {
4323   let ParserMatchClass = uimm6_asmoperand;
4324 }
4325
4326 def label_wid14_scal4_asmoperand : label_asmoperand<14, 4>;
4327
4328 def tbimm_target : Operand<OtherVT> {
4329   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_tstbr>";
4330
4331   // This label is a 14-bit offset from PC, scaled by the instruction-width: 4.
4332   let PrintMethod = "printLabelOperand<14, 4>";
4333   let ParserMatchClass = label_wid14_scal4_asmoperand;
4334
4335   let OperandType = "OPERAND_PCREL";
4336 }
4337
4338 def A64eq : ImmLeaf<i32, [{ return Imm == A64CC::EQ; }]>;
4339 def A64ne : ImmLeaf<i32, [{ return Imm == A64CC::NE; }]>;
4340
4341 // These instructions correspond to patterns involving "and" with a power of
4342 // two, which we need to be able to select.
4343 def tstb64_pat : ComplexPattern<i64, 1, "SelectTSTBOperand<64>">;
4344 def tstb32_pat : ComplexPattern<i32, 1, "SelectTSTBOperand<32>">;
4345
4346 let isBranch = 1, isTerminator = 1 in {
4347   def TBZxii : A64I_TBimm<0b0, (outs),
4348                         (ins GPR64:$Rt, uimm6:$Imm, tbimm_target:$Label),
4349                         "tbz\t$Rt, $Imm, $Label",
4350                         [(A64br_cc (A64cmp (and i64:$Rt, tstb64_pat:$Imm), 0),
4351                                    A64eq, bb:$Label)],
4352                         NoItinerary>;
4353
4354   def TBNZxii : A64I_TBimm<0b1, (outs),
4355                         (ins GPR64:$Rt, uimm6:$Imm, tbimm_target:$Label),
4356                         "tbnz\t$Rt, $Imm, $Label",
4357                         [(A64br_cc (A64cmp (and i64:$Rt, tstb64_pat:$Imm), 0),
4358                                    A64ne, bb:$Label)],
4359                         NoItinerary>;
4360
4361
4362   // Note, these instructions overlap with the above 64-bit patterns. This is
4363   // intentional, "tbz x3, #1, somewhere" and "tbz w3, #1, somewhere" would both
4364   // do the same thing and are both permitted assembly. They also both have
4365   // sensible DAG patterns.
4366   def TBZwii : A64I_TBimm<0b0, (outs),
4367                         (ins GPR32:$Rt, uimm5:$Imm, tbimm_target:$Label),
4368                         "tbz\t$Rt, $Imm, $Label",
4369                         [(A64br_cc (A64cmp (and i32:$Rt, tstb32_pat:$Imm), 0),
4370                                    A64eq, bb:$Label)],
4371                         NoItinerary> {
4372     let Imm{5} = 0b0;
4373   }
4374
4375   def TBNZwii : A64I_TBimm<0b1, (outs),
4376                         (ins GPR32:$Rt, uimm5:$Imm, tbimm_target:$Label),
4377                         "tbnz\t$Rt, $Imm, $Label",
4378                         [(A64br_cc (A64cmp (and i32:$Rt, tstb32_pat:$Imm), 0),
4379                                    A64ne, bb:$Label)],
4380                         NoItinerary> {
4381     let Imm{5} = 0b0;
4382   }
4383 }
4384
4385 //===----------------------------------------------------------------------===//
4386 // Unconditional branch (immediate) instructions
4387 //===----------------------------------------------------------------------===//
4388 // Contains: B, BL
4389
4390 def label_wid26_scal4_asmoperand : label_asmoperand<26, 4>;
4391
4392 def bimm_target : Operand<OtherVT> {
4393   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_uncondbr>";
4394
4395   // This label is a 26-bit offset from PC, scaled by the instruction-width: 4.
4396   let PrintMethod = "printLabelOperand<26, 4>";
4397   let ParserMatchClass = label_wid26_scal4_asmoperand;
4398
4399   let OperandType = "OPERAND_PCREL";
4400 }
4401
4402 def blimm_target : Operand<i64> {
4403   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_call>";
4404
4405   // This label is a 26-bit offset from PC, scaled by the instruction-width: 4.
4406   let PrintMethod = "printLabelOperand<26, 4>";
4407   let ParserMatchClass = label_wid26_scal4_asmoperand;
4408
4409   let OperandType = "OPERAND_PCREL";
4410 }
4411
4412 class A64I_BimmImpl<bit op, string asmop, list<dag> patterns, Operand lbl_type>
4413   : A64I_Bimm<op, (outs), (ins lbl_type:$Label),
4414               !strconcat(asmop, "\t$Label"), patterns,
4415               NoItinerary>;
4416
4417 let isBranch = 1 in {
4418   def Bimm : A64I_BimmImpl<0b0, "b", [(br bb:$Label)], bimm_target> {
4419     let isTerminator = 1;
4420     let isBarrier = 1;
4421   }
4422
4423   def BLimm : A64I_BimmImpl<0b1, "bl",
4424                             [(AArch64Call tglobaladdr:$Label)], blimm_target> {
4425     let isCall = 1;
4426     let Defs = [X30];
4427   }
4428 }
4429
4430 def : Pat<(AArch64Call texternalsym:$Label), (BLimm texternalsym:$Label)>;
4431
4432 //===----------------------------------------------------------------------===//
4433 // Unconditional branch (register) instructions
4434 //===----------------------------------------------------------------------===//
4435 // Contains: BR, BLR, RET, ERET, DRP.
4436
4437 // Most of the notional opcode fields in the A64I_Breg format are fixed in A64
4438 // at the moment.
4439 class A64I_BregImpl<bits<4> opc,
4440                     dag outs, dag ins, string asmstr, list<dag> patterns,
4441                     InstrItinClass itin = NoItinerary>
4442   : A64I_Breg<opc, 0b11111, 0b000000, 0b00000,
4443               outs, ins, asmstr, patterns, itin> {
4444   let isBranch         = 1;
4445   let isIndirectBranch = 1;
4446 }
4447
4448 // Note that these are not marked isCall or isReturn because as far as LLVM is
4449 // concerned they're not. "ret" is just another jump unless it has been selected
4450 // by LLVM as the function's return.
4451
4452 let isBranch = 1 in {
4453   def BRx : A64I_BregImpl<0b0000,(outs), (ins GPR64:$Rn),
4454                           "br\t$Rn", [(brind i64:$Rn)]> {
4455     let isBarrier = 1;
4456     let isTerminator = 1;
4457   }
4458
4459   def BLRx : A64I_BregImpl<0b0001, (outs), (ins GPR64:$Rn),
4460                            "blr\t$Rn", [(AArch64Call i64:$Rn)]> {
4461     let isBarrier = 0;
4462     let isCall = 1;
4463     let Defs = [X30];
4464   }
4465
4466   def RETx : A64I_BregImpl<0b0010, (outs), (ins GPR64:$Rn),
4467                            "ret\t$Rn", []> {
4468     let isBarrier = 1;
4469     let isTerminator = 1;
4470     let isReturn = 1;
4471   }
4472
4473   // Create a separate pseudo-instruction for codegen to use so that we don't
4474   // flag x30 as used in every function. It'll be restored before the RET by the
4475   // epilogue if it's legitimately used.
4476   def RET : A64PseudoExpand<(outs), (ins), [(A64ret)], (RETx (ops X30))> {
4477     let isTerminator = 1;
4478     let isBarrier = 1;
4479     let isReturn = 1;
4480   }
4481
4482   def ERET : A64I_BregImpl<0b0100, (outs), (ins), "eret", []> {
4483     let Rn = 0b11111;
4484     let isBarrier = 1;
4485     let isTerminator = 1;
4486     let isReturn = 1;
4487   }
4488
4489   def DRPS : A64I_BregImpl<0b0101, (outs), (ins), "drps", []> {
4490     let Rn = 0b11111;
4491     let isBarrier = 1;
4492   }
4493 }
4494
4495 def RETAlias : InstAlias<"ret", (RETx X30)>;
4496
4497
4498 //===----------------------------------------------------------------------===//
4499 // Address generation patterns
4500 //===----------------------------------------------------------------------===//
4501
4502 // Primary method of address generation for the small/absolute memory model is
4503 // an ADRP/ADR pair:
4504 //     ADRP x0, some_variable
4505 //     ADD x0, x0, #:lo12:some_variable
4506 //
4507 // The load/store elision of the ADD is accomplished when selecting
4508 // addressing-modes. This just mops up the cases where that doesn't work and we
4509 // really need an address in some register.
4510
4511 // This wrapper applies a LO12 modifier to the address. Otherwise we could just
4512 // use the same address.
4513
4514 class ADRP_ADD<SDNode Wrapper, SDNode addrop>
4515  : Pat<(Wrapper addrop:$Hi, addrop:$Lo12, (i32 imm)),
4516        (ADDxxi_lsl0_s (ADRPxi addrop:$Hi), addrop:$Lo12)>;
4517
4518 def : ADRP_ADD<A64WrapperSmall, tblockaddress>;
4519 def : ADRP_ADD<A64WrapperSmall, texternalsym>;
4520 def : ADRP_ADD<A64WrapperSmall, tglobaladdr>;
4521 def : ADRP_ADD<A64WrapperSmall, tglobaltlsaddr>;
4522 def : ADRP_ADD<A64WrapperSmall, tjumptable>;
4523
4524 //===----------------------------------------------------------------------===//
4525 // GOT access patterns
4526 //===----------------------------------------------------------------------===//
4527
4528 class GOTLoadSmall<SDNode addrfrag>
4529   : Pat<(A64GOTLoad (A64WrapperSmall addrfrag:$Hi, addrfrag:$Lo12, 8)),
4530         (LS64_LDR (ADRPxi addrfrag:$Hi), addrfrag:$Lo12)>;
4531
4532 def : GOTLoadSmall<texternalsym>;
4533 def : GOTLoadSmall<tglobaladdr>;
4534 def : GOTLoadSmall<tglobaltlsaddr>;
4535
4536 //===----------------------------------------------------------------------===//
4537 // Tail call handling
4538 //===----------------------------------------------------------------------===//
4539
4540 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, Uses = [XSP] in {
4541   def TC_RETURNdi
4542     : PseudoInst<(outs), (ins i64imm:$dst, i32imm:$FPDiff),
4543                  [(AArch64tcret tglobaladdr:$dst, (i32 timm:$FPDiff))]>;
4544
4545   def TC_RETURNxi
4546     : PseudoInst<(outs), (ins tcGPR64:$dst, i32imm:$FPDiff),
4547                  [(AArch64tcret i64:$dst, (i32 timm:$FPDiff))]>;
4548 }
4549
4550 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1,
4551     Uses = [XSP] in {
4552   def TAIL_Bimm : A64PseudoExpand<(outs), (ins bimm_target:$Label), [],
4553                                   (Bimm bimm_target:$Label)>;
4554
4555   def TAIL_BRx : A64PseudoExpand<(outs), (ins tcGPR64:$Rd), [],
4556                                  (BRx GPR64:$Rd)>;
4557 }
4558
4559
4560 def : Pat<(AArch64tcret texternalsym:$dst, (i32 timm:$FPDiff)),
4561           (TC_RETURNdi texternalsym:$dst, imm:$FPDiff)>;
4562
4563 //===----------------------------------------------------------------------===//
4564 // Thread local storage
4565 //===----------------------------------------------------------------------===//
4566
4567 // This is a pseudo-instruction representing the ".tlsdesccall" directive in
4568 // assembly. Its effect is to insert an R_AARCH64_TLSDESC_CALL relocation at the
4569 // current location. It should always be immediately followed by a BLR
4570 // instruction, and is intended solely for relaxation by the linker.
4571
4572 def : Pat<(A64threadpointer), (MRSxi 0xde82)>;
4573
4574 def TLSDESCCALL : PseudoInst<(outs), (ins i64imm:$Lbl), []> {
4575   let hasSideEffects = 1;
4576 }
4577
4578 def TLSDESC_BLRx : PseudoInst<(outs), (ins GPR64:$Rn, i64imm:$Var),
4579                             [(A64tlsdesc_blr i64:$Rn, tglobaltlsaddr:$Var)]> {
4580   let isCall = 1;
4581   let Defs = [X30];
4582 }
4583
4584 def : Pat<(A64tlsdesc_blr i64:$Rn, texternalsym:$Var),
4585           (TLSDESC_BLRx $Rn, texternalsym:$Var)>;
4586
4587 //===----------------------------------------------------------------------===//
4588 // Bitfield patterns
4589 //===----------------------------------------------------------------------===//
4590
4591 def bfi32_lsb_to_immr : SDNodeXForm<imm, [{
4592   return CurDAG->getTargetConstant((32 - N->getZExtValue()) % 32, MVT::i64);
4593 }]>;
4594
4595 def bfi64_lsb_to_immr : SDNodeXForm<imm, [{
4596   return CurDAG->getTargetConstant((64 - N->getZExtValue()) % 64, MVT::i64);
4597 }]>;
4598
4599 def bfi_width_to_imms : SDNodeXForm<imm, [{
4600   return CurDAG->getTargetConstant(N->getZExtValue() - 1, MVT::i64);
4601 }]>;
4602
4603
4604 // The simpler patterns deal with cases where no AND mask is actually needed
4605 // (either all bits are used or the low 32 bits are used).
4606 let AddedComplexity = 10 in {
4607
4608 def : Pat<(A64Bfi i64:$src, i64:$Rn, imm:$ImmR, imm:$ImmS),
4609            (BFIxxii $src, $Rn,
4610                     (bfi64_lsb_to_immr (i64 imm:$ImmR)),
4611                     (bfi_width_to_imms (i64 imm:$ImmS)))>;
4612
4613 def : Pat<(A64Bfi i32:$src, i32:$Rn, imm:$ImmR, imm:$ImmS),
4614           (BFIwwii $src, $Rn,
4615                    (bfi32_lsb_to_immr (i64 imm:$ImmR)),
4616                    (bfi_width_to_imms (i64 imm:$ImmS)))>;
4617
4618
4619 def : Pat<(and (A64Bfi i64:$src, i64:$Rn, imm:$ImmR, imm:$ImmS),
4620                (i64 4294967295)),
4621           (SUBREG_TO_REG (i64 0),
4622                          (BFIwwii (EXTRACT_SUBREG $src, sub_32),
4623                                   (EXTRACT_SUBREG $Rn, sub_32),
4624                                   (bfi32_lsb_to_immr (i64 imm:$ImmR)),
4625                                   (bfi_width_to_imms (i64 imm:$ImmS))),
4626                          sub_32)>;
4627
4628 }
4629
4630 //===----------------------------------------------------------------------===//
4631 // Miscellaneous patterns
4632 //===----------------------------------------------------------------------===//
4633
4634 // Truncation from 64 to 32-bits just involves renaming your register.
4635 def : Pat<(i32 (trunc i64:$val)), (EXTRACT_SUBREG $val, sub_32)>;
4636
4637 // Similarly, extension where we don't care about the high bits is
4638 // just a rename.
4639 def : Pat<(i64 (anyext i32:$val)),
4640           (INSERT_SUBREG (IMPLICIT_DEF), $val, sub_32)>;
4641
4642 // SELECT instructions providing f128 types need to be handled by a
4643 // pseudo-instruction since the eventual code will need to introduce basic
4644 // blocks and control flow.
4645 def F128CSEL : PseudoInst<(outs FPR128:$Rd),
4646                          (ins FPR128:$Rn, FPR128:$Rm, cond_code_op:$Cond),
4647                          [(set f128:$Rd, (simple_select f128:$Rn, f128:$Rm))]> {
4648   let Uses = [NZCV];
4649   let usesCustomInserter = 1;
4650 }
4651
4652 //===----------------------------------------------------------------------===//
4653 // Load/store patterns
4654 //===----------------------------------------------------------------------===//
4655
4656 // There are lots of patterns here, because we need to allow at least three
4657 // parameters to vary independently.
4658 //   1. Instruction: "ldrb w9, [sp]", "ldrh w9, [sp]", ...
4659 //   2. LLVM source: zextloadi8, anyextloadi8, ...
4660 //   3. Address-generation: A64Wrapper, (add BASE, OFFSET), ...
4661 //
4662 // The biggest problem turns out to be the address-generation variable. At the
4663 // point of instantiation we need to produce two DAGs, one for the pattern and
4664 // one for the instruction. Doing this at the lowest level of classes doesn't
4665 // work.
4666 //
4667 // Consider the simple uimm12 addressing mode, and the desire to match both (add
4668 // GPR64xsp:$Rn, uimm12:$Offset) and GPR64xsp:$Rn, particularly on the
4669 // instruction side. We'd need to insert either "GPR64xsp" and "uimm12" or
4670 // "GPR64xsp" and "0" into an unknown dag. !subst is not capable of this
4671 // operation, and PatFrags are for selection not output.
4672 //
4673 // As a result, the address-generation patterns are the final
4674 // instantiations. However, we do still need to vary the operand for the address
4675 // further down (At the point we're deciding A64WrapperSmall, we don't know
4676 // the memory width of the operation).
4677
4678 //===------------------------------
4679 // 1. Basic infrastructural defs
4680 //===------------------------------
4681
4682 // First, some simple classes for !foreach and !subst to use:
4683 class Decls {
4684   dag pattern;
4685 }
4686
4687 def decls : Decls;
4688 def ALIGN;
4689 def INST;
4690 def OFFSET;
4691 def SHIFT;
4692
4693 // You can't use !subst on an actual immediate, but you *can* use it on an
4694 // operand record that happens to match a single immediate. So we do.
4695 def imm_eq0 : ImmLeaf<i64, [{ return Imm == 0; }]>;
4696 def imm_eq1 : ImmLeaf<i64, [{ return Imm == 1; }]>;
4697 def imm_eq2 : ImmLeaf<i64, [{ return Imm == 2; }]>;
4698 def imm_eq3 : ImmLeaf<i64, [{ return Imm == 3; }]>;
4699 def imm_eq4 : ImmLeaf<i64, [{ return Imm == 4; }]>;
4700
4701 // If the low bits of a pointer are known to be 0 then an "or" is just as good
4702 // as addition for computing an offset. This fragment forwards that check for
4703 // TableGen's use.
4704 def add_like_or : PatFrag<(ops node:$lhs, node:$rhs), (or node:$lhs, node:$rhs),
4705 [{
4706   return CurDAG->isBaseWithConstantOffset(SDValue(N, 0));
4707 }]>;
4708
4709 // Load/store (unsigned immediate) operations with relocations against global
4710 // symbols (for lo12) are only valid if those symbols have correct alignment
4711 // (since the immediate offset is divided by the access scale, it can't have a
4712 // remainder).
4713 //
4714 // The guaranteed alignment is provided as part of the WrapperSmall
4715 // operation, and checked against one of these.
4716 def any_align   : ImmLeaf<i32, [{ (void)Imm; return true; }]>;
4717 def min_align2  : ImmLeaf<i32, [{ return Imm >= 2; }]>;
4718 def min_align4  : ImmLeaf<i32, [{ return Imm >= 4; }]>;
4719 def min_align8  : ImmLeaf<i32, [{ return Imm >= 8; }]>;
4720 def min_align16 : ImmLeaf<i32, [{ return Imm >= 16; }]>;
4721
4722 // "Normal" load/store instructions can be used on atomic operations, provided
4723 // the ordering parameter is at most "monotonic". Anything above that needs
4724 // special handling with acquire/release instructions.
4725 class simple_load<PatFrag base>
4726   : PatFrag<(ops node:$ptr), (base node:$ptr), [{
4727   return cast<AtomicSDNode>(N)->getOrdering() <= Monotonic;
4728 }]>;
4729
4730 def atomic_load_simple_i8  : simple_load<atomic_load_8>;
4731 def atomic_load_simple_i16 : simple_load<atomic_load_16>;
4732 def atomic_load_simple_i32 : simple_load<atomic_load_32>;
4733 def atomic_load_simple_i64 : simple_load<atomic_load_64>;
4734
4735 class simple_store<PatFrag base>
4736   : PatFrag<(ops node:$ptr, node:$val), (base node:$ptr, node:$val), [{
4737   return cast<AtomicSDNode>(N)->getOrdering() <= Monotonic;
4738 }]>;
4739
4740 def atomic_store_simple_i8  : simple_store<atomic_store_8>;
4741 def atomic_store_simple_i16 : simple_store<atomic_store_16>;
4742 def atomic_store_simple_i32 : simple_store<atomic_store_32>;
4743 def atomic_store_simple_i64 : simple_store<atomic_store_64>;
4744
4745 //===------------------------------
4746 // 2. UImm12 and SImm9
4747 //===------------------------------
4748
4749 // These instructions have two operands providing the address so they can be
4750 // treated similarly for most purposes.
4751
4752 //===------------------------------
4753 // 2.1 Base patterns covering extend/truncate semantics
4754 //===------------------------------
4755
4756 // Atomic patterns can be shared between integer operations of all sizes, a
4757 // quick multiclass here allows reuse.
4758 multiclass ls_atomic_pats<Instruction LOAD, Instruction STORE, dag Base,
4759                           dag Offset, dag address, ValueType transty,
4760                           ValueType sty> {
4761   def : Pat<(!cast<PatFrag>("atomic_load_simple_" # sty) address),
4762             (LOAD Base, Offset)>;
4763
4764   def : Pat<(!cast<PatFrag>("atomic_store_simple_" # sty) address, transty:$Rt),
4765             (STORE $Rt, Base, Offset)>;
4766 }
4767
4768 // Instructions accessing a memory chunk smaller than a register (or, in a
4769 // pinch, the same size) have a characteristic set of patterns they want to
4770 // match: extending loads and truncating stores. This class deals with the
4771 // sign-neutral version of those patterns.
4772 //
4773 // It will be instantiated across multiple addressing-modes.
4774 multiclass ls_small_pats<Instruction LOAD, Instruction STORE,
4775                          dag Base, dag Offset,
4776                          dag address, ValueType sty>
4777   : ls_atomic_pats<LOAD, STORE, Base, Offset, address, i32, sty> {
4778   def : Pat<(!cast<SDNode>(zextload # sty) address), (LOAD Base, Offset)>;
4779
4780   def : Pat<(!cast<SDNode>(extload # sty) address), (LOAD Base, Offset)>;
4781
4782   // For zero-extension to 64-bits we have to tell LLVM that the whole 64-bit
4783   // register was actually set.
4784   def : Pat<(i64 (!cast<SDNode>(zextload # sty) address)),
4785             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset), sub_32)>;
4786
4787   def : Pat<(i64 (!cast<SDNode>(extload # sty) address)),
4788             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset), sub_32)>;
4789
4790   def : Pat<(!cast<SDNode>(truncstore # sty) i32:$Rt, address),
4791             (STORE $Rt, Base, Offset)>;
4792
4793   // For truncating store from 64-bits, we have to manually tell LLVM to
4794   // ignore the high bits of the x register.
4795   def : Pat<(!cast<SDNode>(truncstore # sty) i64:$Rt, address),
4796             (STORE (EXTRACT_SUBREG $Rt, sub_32), Base, Offset)>;
4797 }
4798
4799 // Next come patterns for sign-extending loads.
4800 multiclass load_signed_pats<string T, string U, dag Base, dag Offset,
4801                             dag address, ValueType sty> {
4802   def : Pat<(i32 (!cast<SDNode>("sextload" # sty) address)),
4803             (!cast<Instruction>("LDRS" # T # "w" # U) Base, Offset)>;
4804
4805   def : Pat<(i64 (!cast<SDNode>("sextload" # sty) address)),
4806             (!cast<Instruction>("LDRS" # T # "x" # U) Base, Offset)>;
4807
4808 }
4809
4810 // and finally "natural-width" loads and stores come next.
4811 multiclass ls_neutral_pats<Instruction LOAD, Instruction STORE, dag Base,
4812                            dag Offset, dag address, ValueType sty> {
4813   def : Pat<(sty (load address)), (LOAD Base, Offset)>;
4814   def : Pat<(store sty:$Rt, address), (STORE $Rt, Base, Offset)>;
4815 }
4816
4817 // Integer operations also get atomic instructions to select for.
4818 multiclass ls_int_neutral_pats<Instruction LOAD, Instruction STORE, dag Base,
4819                            dag Offset, dag address, ValueType sty>
4820   : ls_neutral_pats<LOAD, STORE, Base, Offset, address, sty>,
4821     ls_atomic_pats<LOAD, STORE, Base, Offset, address, sty, sty>;
4822
4823 //===------------------------------
4824 // 2.2. Addressing-mode instantiations
4825 //===------------------------------
4826
4827 multiclass uimm12_pats<dag address, dag Base, dag Offset> {
4828   defm : ls_small_pats<LS8_LDR, LS8_STR, Base,
4829                        !foreach(decls.pattern, Offset,
4830                                 !subst(OFFSET, byte_uimm12, decls.pattern)),
4831                        !foreach(decls.pattern, address,
4832                                 !subst(OFFSET, byte_uimm12,
4833                                 !subst(ALIGN, any_align, decls.pattern))),
4834                        i8>;
4835   defm : ls_small_pats<LS16_LDR, LS16_STR, Base,
4836                        !foreach(decls.pattern, Offset,
4837                                 !subst(OFFSET, hword_uimm12, decls.pattern)),
4838                        !foreach(decls.pattern, address,
4839                                 !subst(OFFSET, hword_uimm12,
4840                                 !subst(ALIGN, min_align2, decls.pattern))),
4841                        i16>;
4842   defm : ls_small_pats<LS32_LDR, LS32_STR, Base,
4843                        !foreach(decls.pattern, Offset,
4844                                 !subst(OFFSET, word_uimm12, decls.pattern)),
4845                        !foreach(decls.pattern, address,
4846                                 !subst(OFFSET, word_uimm12,
4847                                 !subst(ALIGN, min_align4, decls.pattern))),
4848                        i32>;
4849
4850   defm : ls_int_neutral_pats<LS32_LDR, LS32_STR, Base,
4851                           !foreach(decls.pattern, Offset,
4852                                    !subst(OFFSET, word_uimm12, decls.pattern)),
4853                           !foreach(decls.pattern, address,
4854                                    !subst(OFFSET, word_uimm12,
4855                                    !subst(ALIGN, min_align4, decls.pattern))),
4856                           i32>;
4857
4858   defm : ls_int_neutral_pats<LS64_LDR, LS64_STR, Base,
4859                           !foreach(decls.pattern, Offset,
4860                                    !subst(OFFSET, dword_uimm12, decls.pattern)),
4861                           !foreach(decls.pattern, address,
4862                                    !subst(OFFSET, dword_uimm12,
4863                                    !subst(ALIGN, min_align8, decls.pattern))),
4864                           i64>;
4865
4866   defm : ls_neutral_pats<LSFP16_LDR, LSFP16_STR, Base,
4867                           !foreach(decls.pattern, Offset,
4868                                    !subst(OFFSET, hword_uimm12, decls.pattern)),
4869                           !foreach(decls.pattern, address,
4870                                    !subst(OFFSET, hword_uimm12,
4871                                    !subst(ALIGN, min_align2, decls.pattern))),
4872                           f16>;
4873
4874   defm : ls_neutral_pats<LSFP32_LDR, LSFP32_STR, Base,
4875                           !foreach(decls.pattern, Offset,
4876                                    !subst(OFFSET, word_uimm12, decls.pattern)),
4877                           !foreach(decls.pattern, address,
4878                                    !subst(OFFSET, word_uimm12,
4879                                    !subst(ALIGN, min_align4, decls.pattern))),
4880                           f32>;
4881
4882   defm : ls_neutral_pats<LSFP64_LDR, LSFP64_STR, Base,
4883                           !foreach(decls.pattern, Offset,
4884                                    !subst(OFFSET, dword_uimm12, decls.pattern)),
4885                           !foreach(decls.pattern, address,
4886                                    !subst(OFFSET, dword_uimm12,
4887                                    !subst(ALIGN, min_align8, decls.pattern))),
4888                           f64>;
4889
4890   defm : ls_neutral_pats<LSFP128_LDR, LSFP128_STR, Base,
4891                           !foreach(decls.pattern, Offset,
4892                                    !subst(OFFSET, qword_uimm12, decls.pattern)),
4893                           !foreach(decls.pattern, address,
4894                                    !subst(OFFSET, qword_uimm12,
4895                                    !subst(ALIGN, min_align16, decls.pattern))),
4896                           f128>;
4897
4898   defm : load_signed_pats<"B", "", Base,
4899                           !foreach(decls.pattern, Offset,
4900                                    !subst(OFFSET, byte_uimm12, decls.pattern)),
4901                           !foreach(decls.pattern, address,
4902                                    !subst(OFFSET, byte_uimm12,
4903                                    !subst(ALIGN, any_align, decls.pattern))),
4904                           i8>;
4905
4906   defm : load_signed_pats<"H", "", Base,
4907                           !foreach(decls.pattern, Offset,
4908                                    !subst(OFFSET, hword_uimm12, decls.pattern)),
4909                           !foreach(decls.pattern, address,
4910                                    !subst(OFFSET, hword_uimm12,
4911                                    !subst(ALIGN, min_align2, decls.pattern))),
4912                           i16>;
4913
4914   def : Pat<(sextloadi32 !foreach(decls.pattern, address,
4915                                   !subst(OFFSET, word_uimm12,
4916                                   !subst(ALIGN, min_align4, decls.pattern)))),
4917             (LDRSWx Base, !foreach(decls.pattern, Offset,
4918                                   !subst(OFFSET, word_uimm12, decls.pattern)))>;
4919 }
4920
4921 // Straightforward patterns of last resort: a pointer with or without an
4922 // appropriate offset.
4923 defm : uimm12_pats<(i64 i64:$Rn), (i64 i64:$Rn), (i64 0)>;
4924 defm : uimm12_pats<(add i64:$Rn, OFFSET:$UImm12),
4925                    (i64 i64:$Rn), (i64 OFFSET:$UImm12)>;
4926
4927 // The offset could be hidden behind an "or", of course:
4928 defm : uimm12_pats<(add_like_or i64:$Rn, OFFSET:$UImm12),
4929                    (i64 i64:$Rn), (i64 OFFSET:$UImm12)>;
4930
4931 // Global addresses under the small-absolute model should use these
4932 // instructions. There are ELF relocations specifically for it.
4933 defm : uimm12_pats<(A64WrapperSmall tglobaladdr:$Hi, tglobaladdr:$Lo12, ALIGN),
4934                    (ADRPxi tglobaladdr:$Hi), (i64 tglobaladdr:$Lo12)>;
4935
4936 defm : uimm12_pats<(A64WrapperSmall tglobaltlsaddr:$Hi, tglobaltlsaddr:$Lo12,
4937                                     ALIGN),
4938                    (ADRPxi tglobaltlsaddr:$Hi), (i64 tglobaltlsaddr:$Lo12)>;
4939
4940 // External symbols that make it this far should also get standard relocations.
4941 defm : uimm12_pats<(A64WrapperSmall texternalsym:$Hi, texternalsym:$Lo12,
4942                                     ALIGN),
4943                    (ADRPxi texternalsym:$Hi), (i64 texternalsym:$Lo12)>;
4944
4945 defm : uimm12_pats<(A64WrapperSmall tconstpool:$Hi, tconstpool:$Lo12, ALIGN),
4946                    (ADRPxi tconstpool:$Hi), (i64 tconstpool:$Lo12)>;
4947
4948 // We also want to use uimm12 instructions for local variables at the moment.
4949 def tframeindex_XFORM : SDNodeXForm<frameindex, [{
4950   int FI = cast<FrameIndexSDNode>(N)->getIndex();
4951   return CurDAG->getTargetFrameIndex(FI, MVT::i64);
4952 }]>;
4953
4954 defm : uimm12_pats<(i64 frameindex:$Rn),
4955                    (tframeindex_XFORM tframeindex:$Rn), (i64 0)>;
4956
4957 // These can be much simpler than uimm12 because we don't to change the operand
4958 // type (e.g. LDURB and LDURH take the same operands).
4959 multiclass simm9_pats<dag address, dag Base, dag Offset> {
4960   defm : ls_small_pats<LS8_LDUR, LS8_STUR, Base, Offset, address, i8>;
4961   defm : ls_small_pats<LS16_LDUR, LS16_STUR, Base, Offset, address, i16>;
4962
4963   defm : ls_int_neutral_pats<LS32_LDUR, LS32_STUR, Base, Offset, address, i32>;
4964   defm : ls_int_neutral_pats<LS64_LDUR, LS64_STUR, Base, Offset, address, i64>;
4965
4966   defm : ls_neutral_pats<LSFP16_LDUR, LSFP16_STUR, Base, Offset, address, f16>;
4967   defm : ls_neutral_pats<LSFP32_LDUR, LSFP32_STUR, Base, Offset, address, f32>;
4968   defm : ls_neutral_pats<LSFP64_LDUR, LSFP64_STUR, Base, Offset, address, f64>;
4969   defm : ls_neutral_pats<LSFP128_LDUR, LSFP128_STUR, Base, Offset, address,
4970                          f128>;
4971
4972   def : Pat<(i64 (zextloadi32 address)),
4973             (SUBREG_TO_REG (i64 0), (LS32_LDUR Base, Offset), sub_32)>;
4974
4975   def : Pat<(truncstorei32 i64:$Rt, address),
4976             (LS32_STUR (EXTRACT_SUBREG $Rt, sub_32), Base, Offset)>;
4977
4978   defm : load_signed_pats<"B", "_U", Base, Offset, address, i8>;
4979   defm : load_signed_pats<"H", "_U", Base, Offset, address, i16>;
4980   def : Pat<(sextloadi32 address), (LDURSWx Base, Offset)>;
4981 }
4982
4983 defm : simm9_pats<(add i64:$Rn, simm9:$SImm9),
4984                   (i64 $Rn), (SDXF_simm9 simm9:$SImm9)>;
4985
4986 defm : simm9_pats<(add_like_or i64:$Rn, simm9:$SImm9),
4987                   (i64 $Rn), (SDXF_simm9 simm9:$SImm9)>;
4988
4989
4990 //===------------------------------
4991 // 3. Register offset patterns
4992 //===------------------------------
4993
4994 // Atomic patterns can be shared between integer operations of all sizes, a
4995 // quick multiclass here allows reuse.
4996 multiclass ro_atomic_pats<Instruction LOAD, Instruction STORE, dag Base,
4997                           dag Offset, dag Extend, dag address,
4998                           ValueType transty, ValueType sty> {
4999   def : Pat<(!cast<PatFrag>("atomic_load_simple_" # sty) address),
5000             (LOAD Base, Offset, Extend)>;
5001
5002   def : Pat<(!cast<PatFrag>("atomic_store_simple_" # sty) address, transty:$Rt),
5003             (STORE $Rt, Base, Offset, Extend)>;
5004 }
5005
5006 // The register offset instructions take three operands giving the instruction,
5007 // and have an annoying split between instructions where Rm is 32-bit and
5008 // 64-bit. So we need a special hierarchy to describe them. Other than that the
5009 // same operations should be supported as for simm9 and uimm12 addressing.
5010
5011 multiclass ro_small_pats<Instruction LOAD, Instruction STORE,
5012                          dag Base, dag Offset, dag Extend,
5013                          dag address, ValueType sty>
5014   : ro_atomic_pats<LOAD, STORE, Base, Offset, Extend, address, i32, sty> {
5015   def : Pat<(!cast<SDNode>(zextload # sty) address),
5016             (LOAD Base, Offset, Extend)>;
5017
5018   def : Pat<(!cast<SDNode>(extload # sty) address),
5019             (LOAD Base, Offset, Extend)>;
5020
5021   // For zero-extension to 64-bits we have to tell LLVM that the whole 64-bit
5022   // register was actually set.
5023   def : Pat<(i64 (!cast<SDNode>(zextload # sty) address)),
5024             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset, Extend), sub_32)>;
5025
5026   def : Pat<(i64 (!cast<SDNode>(extload # sty) address)),
5027             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset, Extend), sub_32)>;
5028
5029   def : Pat<(!cast<SDNode>(truncstore # sty) i32:$Rt, address),
5030             (STORE $Rt, Base, Offset, Extend)>;
5031
5032   // For truncating store from 64-bits, we have to manually tell LLVM to
5033   // ignore the high bits of the x register.
5034   def : Pat<(!cast<SDNode>(truncstore # sty) i64:$Rt, address),
5035             (STORE (EXTRACT_SUBREG $Rt, sub_32), Base, Offset, Extend)>;
5036
5037 }
5038
5039 // Next come patterns for sign-extending loads.
5040 multiclass ro_signed_pats<string T, string Rm, dag Base, dag Offset, dag Extend,
5041                           dag address, ValueType sty> {
5042   def : Pat<(i32 (!cast<SDNode>("sextload" # sty) address)),
5043             (!cast<Instruction>("LDRS" # T # "w_" # Rm # "_RegOffset")
5044               Base, Offset, Extend)>;
5045
5046   def : Pat<(i64 (!cast<SDNode>("sextload" # sty) address)),
5047             (!cast<Instruction>("LDRS" # T # "x_" # Rm # "_RegOffset")
5048               Base, Offset, Extend)>;
5049 }
5050
5051 // and finally "natural-width" loads and stores come next.
5052 multiclass ro_neutral_pats<Instruction LOAD, Instruction STORE,
5053                            dag Base, dag Offset, dag Extend, dag address,
5054                            ValueType sty> {
5055   def : Pat<(sty (load address)), (LOAD Base, Offset, Extend)>;
5056   def : Pat<(store sty:$Rt, address),
5057             (STORE $Rt, Base, Offset, Extend)>;
5058 }
5059
5060 multiclass ro_int_neutral_pats<Instruction LOAD, Instruction STORE,
5061                                dag Base, dag Offset, dag Extend, dag address,
5062                                ValueType sty>
5063   : ro_neutral_pats<LOAD, STORE, Base, Offset, Extend, address, sty>,
5064     ro_atomic_pats<LOAD, STORE, Base, Offset, Extend, address, sty, sty>;
5065
5066 multiclass regoff_pats<string Rm, dag address, dag Base, dag Offset,
5067                        dag Extend> {
5068   defm : ro_small_pats<!cast<Instruction>("LS8_" # Rm # "_RegOffset_LDR"),
5069                        !cast<Instruction>("LS8_" # Rm # "_RegOffset_STR"),
5070                        Base, Offset, Extend,
5071                        !foreach(decls.pattern, address,
5072                                 !subst(SHIFT, imm_eq0, decls.pattern)),
5073                        i8>;
5074   defm : ro_small_pats<!cast<Instruction>("LS16_" # Rm # "_RegOffset_LDR"),
5075                        !cast<Instruction>("LS16_" # Rm # "_RegOffset_STR"),
5076                        Base, Offset, Extend,
5077                        !foreach(decls.pattern, address,
5078                                 !subst(SHIFT, imm_eq1, decls.pattern)),
5079                        i16>;
5080   defm : ro_small_pats<!cast<Instruction>("LS32_" # Rm # "_RegOffset_LDR"),
5081                        !cast<Instruction>("LS32_" # Rm # "_RegOffset_STR"),
5082                        Base, Offset, Extend,
5083                        !foreach(decls.pattern, address,
5084                                 !subst(SHIFT, imm_eq2, decls.pattern)),
5085                        i32>;
5086
5087   defm : ro_int_neutral_pats<
5088                             !cast<Instruction>("LS32_" # Rm # "_RegOffset_LDR"),
5089                             !cast<Instruction>("LS32_" # Rm # "_RegOffset_STR"),
5090                             Base, Offset, Extend,
5091                             !foreach(decls.pattern, address,
5092                                      !subst(SHIFT, imm_eq2, decls.pattern)),
5093                             i32>;
5094
5095   defm : ro_int_neutral_pats<
5096                             !cast<Instruction>("LS64_" # Rm # "_RegOffset_LDR"),
5097                             !cast<Instruction>("LS64_" # Rm # "_RegOffset_STR"),
5098                             Base, Offset, Extend,
5099                             !foreach(decls.pattern, address,
5100                                      !subst(SHIFT, imm_eq3, decls.pattern)),
5101                             i64>;
5102
5103   defm : ro_neutral_pats<!cast<Instruction>("LSFP16_" # Rm # "_RegOffset_LDR"),
5104                          !cast<Instruction>("LSFP16_" # Rm # "_RegOffset_STR"),
5105                          Base, Offset, Extend,
5106                          !foreach(decls.pattern, address,
5107                                   !subst(SHIFT, imm_eq1, decls.pattern)),
5108                          f16>;
5109
5110   defm : ro_neutral_pats<!cast<Instruction>("LSFP32_" # Rm # "_RegOffset_LDR"),
5111                          !cast<Instruction>("LSFP32_" # Rm # "_RegOffset_STR"),
5112                          Base, Offset, Extend,
5113                          !foreach(decls.pattern, address,
5114                                   !subst(SHIFT, imm_eq2, decls.pattern)),
5115                          f32>;
5116
5117   defm : ro_neutral_pats<!cast<Instruction>("LSFP64_" # Rm # "_RegOffset_LDR"),
5118                          !cast<Instruction>("LSFP64_" # Rm # "_RegOffset_STR"),
5119                          Base, Offset, Extend,
5120                          !foreach(decls.pattern, address,
5121                                   !subst(SHIFT, imm_eq3, decls.pattern)),
5122                          f64>;
5123
5124   defm : ro_neutral_pats<!cast<Instruction>("LSFP128_" # Rm # "_RegOffset_LDR"),
5125                          !cast<Instruction>("LSFP128_" # Rm # "_RegOffset_STR"),
5126                          Base, Offset, Extend,
5127                          !foreach(decls.pattern, address,
5128                                   !subst(SHIFT, imm_eq4, decls.pattern)),
5129                          f128>;
5130
5131   defm : ro_signed_pats<"B", Rm, Base, Offset, Extend,
5132                         !foreach(decls.pattern, address,
5133                                  !subst(SHIFT, imm_eq0, decls.pattern)),
5134                         i8>;
5135
5136   defm : ro_signed_pats<"H", Rm, Base, Offset, Extend,
5137                         !foreach(decls.pattern, address,
5138                                  !subst(SHIFT, imm_eq1, decls.pattern)),
5139                         i16>;
5140
5141   def : Pat<(sextloadi32 !foreach(decls.pattern, address,
5142                                   !subst(SHIFT, imm_eq2, decls.pattern))),
5143             (!cast<Instruction>("LDRSWx_" # Rm # "_RegOffset")
5144               Base, Offset, Extend)>;
5145 }
5146
5147
5148 // Finally we're in a position to tell LLVM exactly what addresses are reachable
5149 // using register-offset instructions. Essentially a base plus a possibly
5150 // extended, possibly shifted (by access size) offset.
5151
5152 defm : regoff_pats<"Wm", (add i64:$Rn, (sext i32:$Rm)),
5153                    (i64 i64:$Rn), (i32 i32:$Rm), (i64 6)>;
5154
5155 defm : regoff_pats<"Wm", (add i64:$Rn, (shl (sext i32:$Rm), SHIFT)),
5156                    (i64 i64:$Rn), (i32 i32:$Rm), (i64 7)>;
5157
5158 defm : regoff_pats<"Wm", (add i64:$Rn, (zext i32:$Rm)),
5159                    (i64 i64:$Rn), (i32 i32:$Rm), (i64 2)>;
5160
5161 defm : regoff_pats<"Wm", (add i64:$Rn, (shl (zext i32:$Rm), SHIFT)),
5162                    (i64 i64:$Rn), (i32 i32:$Rm), (i64 3)>;
5163
5164 defm : regoff_pats<"Xm", (add i64:$Rn, i64:$Rm),
5165                    (i64 i64:$Rn), (i64 i64:$Rm), (i64 2)>;
5166
5167 defm : regoff_pats<"Xm", (add i64:$Rn, (shl i64:$Rm, SHIFT)),
5168                    (i64 i64:$Rn), (i64 i64:$Rm), (i64 3)>;
5169
5170 //===----------------------------------------------------------------------===//
5171 // Advanced SIMD (NEON) Support
5172 //
5173
5174 include "AArch64InstrNEON.td"