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