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