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