a9ff02ae0a197a6d663942e110ffdd2ae1e81fc9
[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: CRC32C?[BHWX], UDIV, SDIV, LSLV, LSRV, ASRV, RORV + aliases LSL,
1662 //           LSR, ASR, ROR
1663
1664
1665 class dp_2src_impl<bit sf, bits<6> opcode, string asmop, list<dag> patterns,
1666                    RegisterClass GPRsp,
1667                    InstrItinClass itin>:
1668       A64I_dp_2src<sf,
1669                    opcode,
1670                    0,
1671                    !strconcat(asmop, "\t$Rd, $Rn, $Rm"),
1672                    (outs GPRsp:$Rd),
1673                    (ins GPRsp:$Rn, GPRsp:$Rm),
1674                    patterns,
1675                    itin>;
1676
1677 multiclass dp_2src_crc<bit c, string asmop> {
1678   def B_www : dp_2src_impl<0b0, {0, 1, 0, c, 0, 0},
1679                            !strconcat(asmop, "b"), [], GPR32, NoItinerary>;
1680   def H_www : dp_2src_impl<0b0, {0, 1, 0, c, 0, 1},
1681                            !strconcat(asmop, "h"), [], GPR32, NoItinerary>;
1682   def W_www : dp_2src_impl<0b0, {0, 1, 0, c, 1, 0},
1683                            !strconcat(asmop, "w"), [], GPR32, NoItinerary>;
1684   def X_wwx : A64I_dp_2src<0b1, {0, 1, 0, c, 1, 1}, 0b0,
1685                            !strconcat(asmop, "x\t$Rd, $Rn, $Rm"),
1686                            (outs GPR32:$Rd), (ins GPR32:$Rn, GPR64:$Rm), [],
1687                            NoItinerary>;
1688 }
1689
1690 multiclass dp_2src_zext <bits<6> opcode, string asmop, SDPatternOperator op> {
1691    def www : dp_2src_impl<0b0,
1692                          opcode,
1693                          asmop,
1694                          [(set GPR32:$Rd,
1695                                (op GPR32:$Rn, (i64 (zext GPR32:$Rm))))],
1696                          GPR32,
1697                          NoItinerary>;
1698    def xxx : dp_2src_impl<0b1,
1699                          opcode,
1700                          asmop,
1701                          [(set GPR64:$Rd, (op GPR64:$Rn, GPR64:$Rm))],
1702                          GPR64,
1703                          NoItinerary>;
1704 }
1705
1706
1707 multiclass dp_2src <bits<6> opcode, string asmop, SDPatternOperator op> {
1708     def www : dp_2src_impl<0b0,
1709                          opcode,
1710                          asmop,
1711                          [(set GPR32:$Rd, (op GPR32:$Rn, GPR32:$Rm))],
1712                          GPR32,
1713                          NoItinerary>;
1714    def xxx : dp_2src_impl<0b1,
1715                          opcode,
1716                          asmop,
1717                          [(set GPR64:$Rd, (op GPR64:$Rn, GPR64:$Rm))],
1718                          GPR64,
1719                          NoItinerary>;
1720 }
1721
1722 // Here we define the data processing 2 source instructions.
1723 defm CRC32  : dp_2src_crc<0b0, "crc32">;
1724 defm CRC32C : dp_2src_crc<0b1, "crc32c">;
1725
1726 defm UDIV : dp_2src<0b000010, "udiv", udiv>;
1727 defm SDIV : dp_2src<0b000011, "sdiv", sdiv>;
1728
1729 defm LSLV : dp_2src_zext<0b001000, "lsl", shl>;
1730 defm LSRV : dp_2src_zext<0b001001, "lsr", srl>;
1731 defm ASRV : dp_2src_zext<0b001010, "asr", sra>;
1732 defm RORV : dp_2src_zext<0b001011, "ror", rotr>;
1733
1734 // Extra patterns for an incoming 64-bit value for a 32-bit
1735 // operation. Since the LLVM operations are undefined (as in C) if the
1736 // RHS is out of range, it's perfectly permissible to discard the high
1737 // bits of the GPR64.
1738 def : Pat<(shl GPR32:$Rn, GPR64:$Rm),
1739           (LSLVwww GPR32:$Rn, (EXTRACT_SUBREG GPR64:$Rm, sub_32))>;
1740 def : Pat<(srl GPR32:$Rn, GPR64:$Rm),
1741           (LSRVwww GPR32:$Rn, (EXTRACT_SUBREG GPR64:$Rm, sub_32))>;
1742 def : Pat<(sra GPR32:$Rn, GPR64:$Rm),
1743           (ASRVwww GPR32:$Rn, (EXTRACT_SUBREG GPR64:$Rm, sub_32))>;
1744 def : Pat<(rotr GPR32:$Rn, GPR64:$Rm),
1745           (RORVwww GPR32:$Rn, (EXTRACT_SUBREG GPR64:$Rm, sub_32))>;
1746
1747 // Here we define the aliases for the data processing 2 source instructions.
1748 def LSL_mnemonic : MnemonicAlias<"lslv", "lsl">;
1749 def LSR_mnemonic : MnemonicAlias<"lsrv", "lsr">;
1750 def ASR_menmonic : MnemonicAlias<"asrv", "asr">;
1751 def ROR_menmonic : MnemonicAlias<"rorv", "ror">;
1752
1753 //===----------------------------------------------------------------------===//
1754 // Data Processing (3 sources) instructions
1755 //===----------------------------------------------------------------------===//
1756 // Contains: MADD, MSUB, SMADDL, SMSUBL, SMULH, UMADDL, UMSUBL, UMULH
1757 //    + aliases MUL, MNEG, SMULL, SMNEGL, UMULL, UMNEGL
1758
1759 class A64I_dp3_4operand<bit sf, bits<6> opcode, RegisterClass AccReg,
1760                         RegisterClass SrcReg, string asmop, dag pattern>
1761   : A64I_dp3<sf, opcode,
1762              (outs AccReg:$Rd), (ins SrcReg:$Rn, SrcReg:$Rm, AccReg:$Ra),
1763              !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Ra"),
1764              [(set AccReg:$Rd, pattern)], NoItinerary> {
1765   RegisterClass AccGPR = AccReg;
1766   RegisterClass SrcGPR = SrcReg;
1767 }
1768
1769 def MADDwwww : A64I_dp3_4operand<0b0, 0b000000, GPR32, GPR32, "madd",
1770                                  (add GPR32:$Ra, (mul GPR32:$Rn, GPR32:$Rm))>;
1771 def MADDxxxx : A64I_dp3_4operand<0b1, 0b000000, GPR64, GPR64, "madd",
1772                                  (add GPR64:$Ra, (mul GPR64:$Rn, GPR64:$Rm))>;
1773
1774 def MSUBwwww : A64I_dp3_4operand<0b0, 0b000001, GPR32, GPR32, "msub",
1775                                  (sub GPR32:$Ra, (mul GPR32:$Rn, GPR32:$Rm))>;
1776 def MSUBxxxx : A64I_dp3_4operand<0b1, 0b000001, GPR64, GPR64, "msub",
1777                                  (sub GPR64:$Ra, (mul GPR64:$Rn, GPR64:$Rm))>;
1778
1779 def SMADDLxwwx : A64I_dp3_4operand<0b1, 0b000010, GPR64, GPR32, "smaddl",
1780                (add GPR64:$Ra, (mul (i64 (sext GPR32:$Rn)), (sext GPR32:$Rm)))>;
1781 def SMSUBLxwwx : A64I_dp3_4operand<0b1, 0b000011, GPR64, GPR32, "smsubl",
1782                (sub GPR64:$Ra, (mul (i64 (sext GPR32:$Rn)), (sext GPR32:$Rm)))>;
1783
1784 def UMADDLxwwx : A64I_dp3_4operand<0b1, 0b001010, GPR64, GPR32, "umaddl",
1785                (add GPR64:$Ra, (mul (i64 (zext GPR32:$Rn)), (zext GPR32:$Rm)))>;
1786 def UMSUBLxwwx : A64I_dp3_4operand<0b1, 0b001011, GPR64, GPR32, "umsubl",
1787                (sub GPR64:$Ra, (mul (i64 (zext GPR32:$Rn)), (zext GPR32:$Rm)))>;
1788
1789 let isCommutable = 1, PostEncoderMethod = "fixMulHigh" in {
1790   def UMULHxxx : A64I_dp3<0b1, 0b001100, (outs GPR64:$Rd),
1791                           (ins GPR64:$Rn, GPR64:$Rm),
1792                           "umulh\t$Rd, $Rn, $Rm",
1793                           [(set GPR64:$Rd, (mulhu GPR64:$Rn, GPR64:$Rm))],
1794                           NoItinerary>;
1795
1796   def SMULHxxx : A64I_dp3<0b1, 0b000100, (outs GPR64:$Rd),
1797                           (ins GPR64:$Rn, GPR64:$Rm),
1798                           "smulh\t$Rd, $Rn, $Rm",
1799                           [(set GPR64:$Rd, (mulhs GPR64:$Rn, GPR64:$Rm))],
1800                           NoItinerary>;
1801 }
1802
1803 multiclass A64I_dp3_3operand<string asmop, A64I_dp3_4operand INST,
1804                              Register ZR, dag pattern> {
1805   def : InstAlias<asmop # " $Rd, $Rn, $Rm",
1806                   (INST INST.AccGPR:$Rd, INST.SrcGPR:$Rn, INST.SrcGPR:$Rm, ZR)>;
1807
1808   def : Pat<pattern, (INST INST.SrcGPR:$Rn, INST.SrcGPR:$Rm, ZR)>;
1809 }
1810
1811 defm : A64I_dp3_3operand<"mul", MADDwwww, WZR, (mul GPR32:$Rn, GPR32:$Rm)>;
1812 defm : A64I_dp3_3operand<"mul", MADDxxxx, XZR, (mul GPR64:$Rn, GPR64:$Rm)>;
1813
1814 defm : A64I_dp3_3operand<"mneg", MSUBwwww, WZR,
1815                          (sub 0, (mul GPR32:$Rn, GPR32:$Rm))>;
1816 defm : A64I_dp3_3operand<"mneg", MSUBxxxx, XZR,
1817                          (sub 0, (mul GPR64:$Rn, GPR64:$Rm))>;
1818
1819 defm : A64I_dp3_3operand<"smull", SMADDLxwwx, XZR,
1820                          (mul (i64 (sext GPR32:$Rn)), (sext GPR32:$Rm))>;
1821 defm : A64I_dp3_3operand<"smnegl", SMSUBLxwwx, XZR,
1822                        (sub 0, (mul (i64 (sext GPR32:$Rn)), (sext GPR32:$Rm)))>;
1823
1824 defm : A64I_dp3_3operand<"umull", UMADDLxwwx, XZR,
1825                          (mul (i64 (zext GPR32:$Rn)), (zext GPR32:$Rm))>;
1826 defm : A64I_dp3_3operand<"umnegl", UMSUBLxwwx, XZR,
1827                        (sub 0, (mul (i64 (zext GPR32:$Rn)), (zext GPR32:$Rm)))>;
1828
1829
1830 //===----------------------------------------------------------------------===//
1831 // Exception generation
1832 //===----------------------------------------------------------------------===//
1833 // Contains: SVC, HVC, SMC, BRK, HLT, DCPS1, DCPS2, DCPS3
1834
1835 def uimm16_asmoperand : AsmOperandClass {
1836   let Name = "UImm16";
1837   let PredicateMethod = "isUImm<16>";
1838   let RenderMethod = "addImmOperands";
1839 }
1840
1841 def uimm16 : Operand<i32> {
1842   let ParserMatchClass = uimm16_asmoperand;
1843 }
1844
1845 class A64I_exceptImpl<bits<3> opc, bits<2> ll, string asmop>
1846   : A64I_exception<opc, 0b000, ll, (outs), (ins uimm16:$UImm16),
1847                    !strconcat(asmop, "\t$UImm16"), [], NoItinerary> {
1848   let isBranch = 1;
1849   let isTerminator = 1;
1850 }
1851
1852 def SVCi : A64I_exceptImpl<0b000, 0b01, "svc">;
1853 def HVCi : A64I_exceptImpl<0b000, 0b10, "hvc">;
1854 def SMCi : A64I_exceptImpl<0b000, 0b11, "smc">;
1855 def BRKi : A64I_exceptImpl<0b001, 0b00, "brk">;
1856 def HLTi : A64I_exceptImpl<0b010, 0b00, "hlt">;
1857
1858 def DCPS1i : A64I_exceptImpl<0b101, 0b01, "dcps1">;
1859 def DCPS2i : A64I_exceptImpl<0b101, 0b10, "dcps2">;
1860 def DCPS3i : A64I_exceptImpl<0b101, 0b11, "dcps3">;
1861
1862 // The immediate is optional for the DCPS instructions, defaulting to 0.
1863 def : InstAlias<"dcps1", (DCPS1i 0)>;
1864 def : InstAlias<"dcps2", (DCPS2i 0)>;
1865 def : InstAlias<"dcps3", (DCPS3i 0)>;
1866
1867 //===----------------------------------------------------------------------===//
1868 // Extract (immediate)
1869 //===----------------------------------------------------------------------===//
1870 // Contains: EXTR + alias ROR
1871
1872 def EXTRwwwi : A64I_extract<0b0, 0b000, 0b0,
1873                             (outs GPR32:$Rd),
1874                             (ins GPR32:$Rn, GPR32:$Rm, bitfield32_imm:$LSB),
1875                             "extr\t$Rd, $Rn, $Rm, $LSB",
1876                             [(set GPR32:$Rd,
1877                                   (A64Extr GPR32:$Rn, GPR32:$Rm, imm:$LSB))],
1878                             NoItinerary>;
1879 def EXTRxxxi : A64I_extract<0b1, 0b000, 0b1,
1880                             (outs GPR64:$Rd),
1881                             (ins GPR64:$Rn, GPR64:$Rm, bitfield64_imm:$LSB),
1882                             "extr\t$Rd, $Rn, $Rm, $LSB",
1883                             [(set GPR64:$Rd,
1884                                   (A64Extr GPR64:$Rn, GPR64:$Rm, imm:$LSB))],
1885                             NoItinerary>;
1886
1887 def : InstAlias<"ror $Rd, $Rs, $LSB",
1888                (EXTRwwwi GPR32:$Rd, GPR32:$Rs, GPR32:$Rs, bitfield32_imm:$LSB)>;
1889 def : InstAlias<"ror $Rd, $Rs, $LSB",
1890                (EXTRxxxi GPR64:$Rd, GPR64:$Rs, GPR64:$Rs, bitfield64_imm:$LSB)>;
1891
1892 def : Pat<(rotr GPR32:$Rn, bitfield32_imm:$LSB),
1893           (EXTRwwwi GPR32:$Rn, GPR32:$Rn, bitfield32_imm:$LSB)>;
1894 def : Pat<(rotr GPR64:$Rn, bitfield64_imm:$LSB),
1895           (EXTRxxxi GPR64:$Rn, GPR64:$Rn, bitfield64_imm:$LSB)>;
1896
1897 //===----------------------------------------------------------------------===//
1898 // Floating-point compare instructions
1899 //===----------------------------------------------------------------------===//
1900 // Contains: FCMP, FCMPE
1901
1902 def fpzero_asmoperand : AsmOperandClass {
1903   let Name = "FPZero";
1904   let ParserMethod = "ParseFPImmOperand";
1905 }
1906
1907 def fpz32 : Operand<f32>,
1908             ComplexPattern<f32, 1, "SelectFPZeroOperand", [fpimm]> {
1909   let ParserMatchClass = fpzero_asmoperand;
1910   let PrintMethod = "printFPZeroOperand";
1911 }
1912
1913 def fpz64 : Operand<f64>,
1914             ComplexPattern<f64, 1, "SelectFPZeroOperand", [fpimm]> {
1915   let ParserMatchClass = fpzero_asmoperand;
1916   let PrintMethod = "printFPZeroOperand";
1917 }
1918
1919 multiclass A64I_fpcmpSignal<bits<2> type, bit imm, dag ins, string asmop2,
1920                             dag pattern> {
1921   def _quiet : A64I_fpcmp<0b0, 0b0, type, 0b00, {0b0, imm, 0b0, 0b0, 0b0},
1922                           (outs), ins, !strconcat("fcmp\t$Rn, ", asmop2),
1923                           [pattern], NoItinerary> {
1924     let Defs = [NZCV];
1925   }
1926
1927   def _sig : A64I_fpcmp<0b0, 0b0, type, 0b00, {0b1, imm, 0b0, 0b0, 0b0},
1928                         (outs), ins, !strconcat("fcmpe\t$Rn, ", asmop2),
1929                         [], NoItinerary> {
1930     let Defs = [NZCV];
1931   }
1932 }
1933
1934 defm FCMPss : A64I_fpcmpSignal<0b00, 0b0, (ins FPR32:$Rn, FPR32:$Rm), "$Rm",
1935                                (set NZCV, (A64cmp (f32 FPR32:$Rn), FPR32:$Rm))>;
1936 defm FCMPdd : A64I_fpcmpSignal<0b01, 0b0, (ins FPR64:$Rn, FPR64:$Rm), "$Rm",
1937                                (set NZCV, (A64cmp (f64 FPR64:$Rn), FPR64:$Rm))>;
1938
1939 // What would be Rm should be written as 0, but anything is valid for
1940 // disassembly so we can't set the bits
1941 let PostEncoderMethod = "fixFCMPImm" in {
1942   defm FCMPsi : A64I_fpcmpSignal<0b00, 0b1, (ins FPR32:$Rn, fpz32:$Imm), "$Imm",
1943                               (set NZCV, (A64cmp (f32 FPR32:$Rn), fpz32:$Imm))>;
1944
1945   defm FCMPdi : A64I_fpcmpSignal<0b01, 0b1, (ins FPR64:$Rn, fpz64:$Imm), "$Imm",
1946                               (set NZCV, (A64cmp (f64 FPR64:$Rn), fpz64:$Imm))>;
1947 }
1948
1949
1950 //===----------------------------------------------------------------------===//
1951 // Floating-point conditional compare instructions
1952 //===----------------------------------------------------------------------===//
1953 // Contains: FCCMP, FCCMPE
1954
1955 class A64I_fpccmpImpl<bits<2> type, bit op, RegisterClass FPR, string asmop>
1956   : A64I_fpccmp<0b0, 0b0, type, op,
1957                 (outs),
1958                 (ins FPR:$Rn, FPR:$Rm, uimm4:$NZCVImm, cond_code_op:$Cond),
1959                 !strconcat(asmop, "\t$Rn, $Rm, $NZCVImm, $Cond"),
1960                 [], NoItinerary> {
1961   let Defs = [NZCV];
1962 }
1963
1964 def FCCMPss : A64I_fpccmpImpl<0b00, 0b0, FPR32, "fccmp">;
1965 def FCCMPEss : A64I_fpccmpImpl<0b00, 0b1, FPR32, "fccmpe">;
1966 def FCCMPdd : A64I_fpccmpImpl<0b01, 0b0, FPR64, "fccmp">;
1967 def FCCMPEdd : A64I_fpccmpImpl<0b01, 0b1, FPR64, "fccmpe">;
1968
1969 //===----------------------------------------------------------------------===//
1970 // Floating-point conditional select instructions
1971 //===----------------------------------------------------------------------===//
1972 // Contains: FCSEL
1973
1974 let Uses = [NZCV] in {
1975   def FCSELsssc : A64I_fpcondsel<0b0, 0b0, 0b00, (outs FPR32:$Rd),
1976                                  (ins FPR32:$Rn, FPR32:$Rm, cond_code_op:$Cond),
1977                                  "fcsel\t$Rd, $Rn, $Rm, $Cond",
1978                                  [(set FPR32:$Rd,
1979                                        (simple_select (f32 FPR32:$Rn),
1980                                                       FPR32:$Rm))],
1981                                  NoItinerary>;
1982
1983
1984   def FCSELdddc : A64I_fpcondsel<0b0, 0b0, 0b01, (outs FPR64:$Rd),
1985                                  (ins FPR64:$Rn, FPR64:$Rm, cond_code_op:$Cond),
1986                                  "fcsel\t$Rd, $Rn, $Rm, $Cond",
1987                                  [(set FPR64:$Rd,
1988                                        (simple_select (f64 FPR64:$Rn),
1989                                                       FPR64:$Rm))],
1990                                  NoItinerary>;
1991 }
1992
1993 //===----------------------------------------------------------------------===//
1994 // Floating-point data-processing (1 source)
1995 //===----------------------------------------------------------------------===//
1996 // Contains: FMOV, FABS, FNEG, FSQRT, FCVT, FRINT[NPMZAXI].
1997
1998 def FPNoUnop : PatFrag<(ops node:$val), (fneg node:$val),
1999                        [{ (void)N; return false; }]>;
2000
2001 // First we do the fairly trivial bunch with uniform "OP s, s" and "OP d, d"
2002 // syntax. Default to no pattern because most are odd enough not to have one.
2003 multiclass A64I_fpdp1sizes<bits<6> opcode, string asmstr,
2004                            SDPatternOperator opnode = FPNoUnop> {
2005   def ss : A64I_fpdp1<0b0, 0b0, 0b00, opcode, (outs FPR32:$Rd), (ins FPR32:$Rn),
2006                      !strconcat(asmstr, "\t$Rd, $Rn"),
2007                      [(set (f32 FPR32:$Rd), (opnode FPR32:$Rn))],
2008                      NoItinerary>;
2009
2010   def dd : A64I_fpdp1<0b0, 0b0, 0b01, opcode, (outs FPR64:$Rd), (ins FPR64:$Rn),
2011                      !strconcat(asmstr, "\t$Rd, $Rn"),
2012                      [(set (f64 FPR64:$Rd), (opnode FPR64:$Rn))],
2013                      NoItinerary>;
2014 }
2015
2016 defm FMOV   : A64I_fpdp1sizes<0b000000, "fmov">;
2017 defm FABS   : A64I_fpdp1sizes<0b000001, "fabs", fabs>;
2018 defm FNEG   : A64I_fpdp1sizes<0b000010, "fneg", fneg>;
2019 defm FSQRT  : A64I_fpdp1sizes<0b000011, "fsqrt", fsqrt>;
2020
2021 defm FRINTN : A64I_fpdp1sizes<0b001000, "frintn">;
2022 defm FRINTP : A64I_fpdp1sizes<0b001001, "frintp", fceil>;
2023 defm FRINTM : A64I_fpdp1sizes<0b001010, "frintm", ffloor>;
2024 defm FRINTZ : A64I_fpdp1sizes<0b001011, "frintz", ftrunc>;
2025 defm FRINTA : A64I_fpdp1sizes<0b001100, "frinta">;
2026 defm FRINTX : A64I_fpdp1sizes<0b001110, "frintx", frint>;
2027 defm FRINTI : A64I_fpdp1sizes<0b001111, "frinti", fnearbyint>;
2028
2029 // The FCVT instrucitons have different source and destination register-types,
2030 // but the fields are uniform everywhere a D-register (say) crops up. Package
2031 // this information in a Record.
2032 class FCVTRegType<RegisterClass rc, bits<2> fld, ValueType vt> {
2033     RegisterClass Class = rc;
2034     ValueType VT = vt;
2035     bit t1 = fld{1};
2036     bit t0 = fld{0};
2037 }
2038
2039 def FCVT16 : FCVTRegType<FPR16, 0b11, f16>;
2040 def FCVT32 : FCVTRegType<FPR32, 0b00, f32>;
2041 def FCVT64 : FCVTRegType<FPR64, 0b01, f64>;
2042
2043 class A64I_fpdp1_fcvt<FCVTRegType DestReg, FCVTRegType SrcReg, SDNode opnode>
2044   : A64I_fpdp1<0b0, 0b0, {SrcReg.t1, SrcReg.t0},
2045                {0,0,0,1, DestReg.t1, DestReg.t0},
2046                (outs DestReg.Class:$Rd), (ins SrcReg.Class:$Rn),
2047                "fcvt\t$Rd, $Rn",
2048                [(set (DestReg.VT DestReg.Class:$Rd),
2049                      (opnode (SrcReg.VT SrcReg.Class:$Rn)))], NoItinerary>;
2050
2051 def FCVTds : A64I_fpdp1_fcvt<FCVT64, FCVT32, fextend>;
2052 def FCVThs : A64I_fpdp1_fcvt<FCVT16, FCVT32, fround>;
2053 def FCVTsd : A64I_fpdp1_fcvt<FCVT32, FCVT64, fround>;
2054 def FCVThd : A64I_fpdp1_fcvt<FCVT16, FCVT64, fround>;
2055 def FCVTsh : A64I_fpdp1_fcvt<FCVT32, FCVT16, fextend>;
2056 def FCVTdh : A64I_fpdp1_fcvt<FCVT64, FCVT16, fextend>;
2057
2058
2059 //===----------------------------------------------------------------------===//
2060 // Floating-point data-processing (2 sources) instructions
2061 //===----------------------------------------------------------------------===//
2062 // Contains: FMUL, FDIV, FADD, FSUB, FMAX, FMIN, FMAXNM, FMINNM, FNMUL
2063
2064 def FPNoBinop : PatFrag<(ops node:$lhs, node:$rhs), (fadd node:$lhs, node:$rhs),
2065                       [{ (void)N; return false; }]>;
2066
2067 multiclass A64I_fpdp2sizes<bits<4> opcode, string asmstr,
2068                            SDPatternOperator opnode> {
2069   def sss : A64I_fpdp2<0b0, 0b0, 0b00, opcode,
2070                       (outs FPR32:$Rd),
2071                       (ins FPR32:$Rn, FPR32:$Rm),
2072                       !strconcat(asmstr, "\t$Rd, $Rn, $Rm"),
2073                       [(set (f32 FPR32:$Rd), (opnode FPR32:$Rn, FPR32:$Rm))],
2074                       NoItinerary>;
2075
2076   def ddd : A64I_fpdp2<0b0, 0b0, 0b01, opcode,
2077                       (outs FPR64:$Rd),
2078                       (ins FPR64:$Rn, FPR64:$Rm),
2079                       !strconcat(asmstr, "\t$Rd, $Rn, $Rm"),
2080                       [(set (f64 FPR64:$Rd), (opnode FPR64:$Rn, FPR64:$Rm))],
2081                       NoItinerary>;
2082 }
2083
2084 let isCommutable = 1 in {
2085   defm FMUL   : A64I_fpdp2sizes<0b0000, "fmul", fmul>;
2086   defm FADD   : A64I_fpdp2sizes<0b0010, "fadd", fadd>;
2087
2088   // No patterns for these.
2089   defm FMAX   : A64I_fpdp2sizes<0b0100, "fmax", FPNoBinop>;
2090   defm FMIN   : A64I_fpdp2sizes<0b0101, "fmin", FPNoBinop>;
2091   defm FMAXNM : A64I_fpdp2sizes<0b0110, "fmaxnm", FPNoBinop>;
2092   defm FMINNM : A64I_fpdp2sizes<0b0111, "fminnm", FPNoBinop>;
2093
2094   defm FNMUL  : A64I_fpdp2sizes<0b1000, "fnmul",
2095                                 PatFrag<(ops node:$lhs, node:$rhs),
2096                                         (fneg (fmul node:$lhs, node:$rhs))> >;
2097 }
2098
2099 defm FDIV : A64I_fpdp2sizes<0b0001, "fdiv", fdiv>;
2100 defm FSUB : A64I_fpdp2sizes<0b0011, "fsub", fsub>;
2101
2102 //===----------------------------------------------------------------------===//
2103 // Floating-point data-processing (3 sources) instructions
2104 //===----------------------------------------------------------------------===//
2105 // Contains: FMADD, FMSUB, FNMADD, FNMSUB
2106
2107 def fmsub : PatFrag<(ops node:$Rn, node:$Rm, node:$Ra),
2108                     (fma (fneg node:$Rn),  node:$Rm, node:$Ra)>;
2109 def fnmadd : PatFrag<(ops node:$Rn, node:$Rm, node:$Ra),
2110                      (fma node:$Rn,  node:$Rm, (fneg node:$Ra))>;
2111 def fnmsub : PatFrag<(ops node:$Rn, node:$Rm, node:$Ra),
2112                      (fma (fneg node:$Rn),  node:$Rm, (fneg node:$Ra))>;
2113
2114 class A64I_fpdp3Impl<string asmop, RegisterClass FPR, ValueType VT,
2115                      bits<2> type, bit o1, bit o0, SDPatternOperator fmakind>
2116   : A64I_fpdp3<0b0, 0b0, type, o1, o0, (outs FPR:$Rd),
2117                (ins FPR:$Rn, FPR:$Rm, FPR:$Ra),
2118                !strconcat(asmop,"\t$Rd, $Rn, $Rm, $Ra"),
2119                [(set FPR:$Rd, (fmakind (VT FPR:$Rn), FPR:$Rm, FPR:$Ra))],
2120                NoItinerary>;
2121
2122 def FMADDssss  : A64I_fpdp3Impl<"fmadd",  FPR32, f32, 0b00, 0b0, 0b0, fma>;
2123 def FMSUBssss  : A64I_fpdp3Impl<"fmsub",  FPR32, f32, 0b00, 0b0, 0b1, fmsub>;
2124 def FNMADDssss : A64I_fpdp3Impl<"fnmadd", FPR32, f32, 0b00, 0b1, 0b0, fnmadd>;
2125 def FNMSUBssss : A64I_fpdp3Impl<"fnmsub", FPR32, f32, 0b00, 0b1, 0b1, fnmsub>;
2126
2127 def FMADDdddd  : A64I_fpdp3Impl<"fmadd",  FPR64, f64, 0b01, 0b0, 0b0, fma>;
2128 def FMSUBdddd  : A64I_fpdp3Impl<"fmsub",  FPR64, f64, 0b01, 0b0, 0b1, fmsub>;
2129 def FNMADDdddd : A64I_fpdp3Impl<"fnmadd", FPR64, f64, 0b01, 0b1, 0b0, fnmadd>;
2130 def FNMSUBdddd : A64I_fpdp3Impl<"fnmsub", FPR64, f64, 0b01, 0b1, 0b1, fnmsub>;
2131
2132 //===----------------------------------------------------------------------===//
2133 // Floating-point <-> fixed-point conversion instructions
2134 //===----------------------------------------------------------------------===//
2135 // Contains: FCVTZS, FCVTZU, SCVTF, UCVTF
2136
2137 // #1-#32 allowed, encoded as "64 - <specified imm>
2138 def fixedpos_asmoperand_i32 : AsmOperandClass {
2139   let Name = "CVTFixedPos32";
2140   let RenderMethod = "addCVTFixedPosOperands";
2141   let PredicateMethod = "isCVTFixedPos<32>";
2142 }
2143
2144 // Also encoded as "64 - <specified imm>" but #1-#64 allowed.
2145 def fixedpos_asmoperand_i64 : AsmOperandClass {
2146   let Name = "CVTFixedPos64";
2147   let RenderMethod = "addCVTFixedPosOperands";
2148   let PredicateMethod = "isCVTFixedPos<64>";
2149 }
2150
2151 // We need the cartesian product of f32/f64 i32/i64 operands for
2152 // conversions:
2153 //   + Selection needs to use operands of correct floating type
2154 //   + Assembly parsing and decoding depend on integer width
2155 class cvtfix_i32_op<ValueType FloatVT>
2156   : Operand<FloatVT>,
2157     ComplexPattern<FloatVT, 1, "SelectCVTFixedPosOperand<32>", [fpimm]> {
2158   let ParserMatchClass = fixedpos_asmoperand_i32;
2159   let DecoderMethod = "DecodeCVT32FixedPosOperand";
2160   let PrintMethod = "printCVTFixedPosOperand";
2161 }
2162
2163 class cvtfix_i64_op<ValueType FloatVT>
2164   : Operand<FloatVT>,
2165     ComplexPattern<FloatVT, 1, "SelectCVTFixedPosOperand<64>", [fpimm]> {
2166   let ParserMatchClass = fixedpos_asmoperand_i64;
2167   let PrintMethod = "printCVTFixedPosOperand";
2168 }
2169
2170 // Because of the proliferation of weird operands, it's not really
2171 // worth going for a multiclass here. Oh well.
2172
2173 class A64I_fptofix<bit sf, bits<2> type, bits<3> opcode,
2174                    RegisterClass GPR, RegisterClass FPR, Operand scale_op,
2175                    string asmop, SDNode cvtop>
2176   : A64I_fpfixed<sf, 0b0, type, 0b11, opcode,
2177                  (outs GPR:$Rd), (ins FPR:$Rn, scale_op:$Scale),
2178                  !strconcat(asmop, "\t$Rd, $Rn, $Scale"),
2179                  [(set GPR:$Rd, (cvtop (fmul FPR:$Rn, scale_op:$Scale)))],
2180                  NoItinerary>;
2181
2182 def FCVTZSwsi : A64I_fptofix<0b0, 0b00, 0b000, GPR32, FPR32,
2183                              cvtfix_i32_op<f32>, "fcvtzs", fp_to_sint>;
2184 def FCVTZSxsi : A64I_fptofix<0b1, 0b00, 0b000, GPR64, FPR32,
2185                              cvtfix_i64_op<f32>, "fcvtzs", fp_to_sint>;
2186 def FCVTZUwsi : A64I_fptofix<0b0, 0b00, 0b001, GPR32, FPR32,
2187                              cvtfix_i32_op<f32>, "fcvtzu", fp_to_uint>;
2188 def FCVTZUxsi : A64I_fptofix<0b1, 0b00, 0b001, GPR64, FPR32,
2189                              cvtfix_i64_op<f32>, "fcvtzu", fp_to_uint>;
2190
2191 def FCVTZSwdi : A64I_fptofix<0b0, 0b01, 0b000, GPR32, FPR64,
2192                              cvtfix_i32_op<f64>, "fcvtzs", fp_to_sint>;
2193 def FCVTZSxdi : A64I_fptofix<0b1, 0b01, 0b000, GPR64, FPR64,
2194                              cvtfix_i64_op<f64>, "fcvtzs", fp_to_sint>;
2195 def FCVTZUwdi : A64I_fptofix<0b0, 0b01, 0b001, GPR32, FPR64,
2196                              cvtfix_i32_op<f64>, "fcvtzu", fp_to_uint>;
2197 def FCVTZUxdi : A64I_fptofix<0b1, 0b01, 0b001, GPR64, FPR64,
2198                              cvtfix_i64_op<f64>, "fcvtzu", fp_to_uint>;
2199
2200
2201 class A64I_fixtofp<bit sf, bits<2> type, bits<3> opcode,
2202                    RegisterClass FPR, RegisterClass GPR, Operand scale_op,
2203                    string asmop, SDNode cvtop>
2204   : A64I_fpfixed<sf, 0b0, type, 0b00, opcode,
2205                  (outs FPR:$Rd), (ins GPR:$Rn, scale_op:$Scale),
2206                  !strconcat(asmop, "\t$Rd, $Rn, $Scale"),
2207                  [(set FPR:$Rd, (fdiv (cvtop GPR:$Rn), scale_op:$Scale))],
2208                  NoItinerary>;
2209
2210 def SCVTFswi : A64I_fixtofp<0b0, 0b00, 0b010, FPR32, GPR32,
2211                             cvtfix_i32_op<f32>, "scvtf", sint_to_fp>;
2212 def SCVTFsxi : A64I_fixtofp<0b1, 0b00, 0b010, FPR32, GPR64,
2213                             cvtfix_i64_op<f32>, "scvtf", sint_to_fp>;
2214 def UCVTFswi : A64I_fixtofp<0b0, 0b00, 0b011, FPR32, GPR32,
2215                             cvtfix_i32_op<f32>, "ucvtf", uint_to_fp>;
2216 def UCVTFsxi : A64I_fixtofp<0b1, 0b00, 0b011, FPR32, GPR64,
2217                             cvtfix_i64_op<f32>, "ucvtf", uint_to_fp>;
2218 def SCVTFdwi : A64I_fixtofp<0b0, 0b01, 0b010, FPR64, GPR32,
2219                             cvtfix_i32_op<f64>, "scvtf", sint_to_fp>;
2220 def SCVTFdxi : A64I_fixtofp<0b1, 0b01, 0b010, FPR64, GPR64,
2221                             cvtfix_i64_op<f64>, "scvtf", sint_to_fp>;
2222 def UCVTFdwi : A64I_fixtofp<0b0, 0b01, 0b011, FPR64, GPR32,
2223                             cvtfix_i32_op<f64>, "ucvtf", uint_to_fp>;
2224 def UCVTFdxi : A64I_fixtofp<0b1, 0b01, 0b011, FPR64, GPR64,
2225                             cvtfix_i64_op<f64>, "ucvtf", uint_to_fp>;
2226
2227 //===----------------------------------------------------------------------===//
2228 // Floating-point <-> integer conversion instructions
2229 //===----------------------------------------------------------------------===//
2230 // Contains: FCVTZS, FCVTZU, SCVTF, UCVTF
2231
2232 class A64I_fpintI<bit sf, bits<2> type, bits<2> rmode, bits<3> opcode,
2233                    RegisterClass DestPR, RegisterClass SrcPR, string asmop>
2234   : A64I_fpint<sf, 0b0, type, rmode, opcode, (outs DestPR:$Rd), (ins SrcPR:$Rn),
2235                !strconcat(asmop, "\t$Rd, $Rn"), [], NoItinerary>;
2236
2237 multiclass A64I_fptointRM<bits<2> rmode, bit o2, string asmop> {
2238   def Sws : A64I_fpintI<0b0, 0b00, rmode, {o2, 0, 0},
2239                         GPR32, FPR32, asmop # "s">;
2240   def Sxs : A64I_fpintI<0b1, 0b00, rmode, {o2, 0, 0},
2241                         GPR64, FPR32, asmop # "s">;
2242   def Uws : A64I_fpintI<0b0, 0b00, rmode, {o2, 0, 1},
2243                         GPR32, FPR32, asmop # "u">;
2244   def Uxs : A64I_fpintI<0b1, 0b00, rmode, {o2, 0, 1},
2245                         GPR64, FPR32, asmop # "u">;
2246
2247   def Swd : A64I_fpintI<0b0, 0b01, rmode, {o2, 0, 0},
2248                         GPR32, FPR64, asmop # "s">;
2249   def Sxd : A64I_fpintI<0b1, 0b01, rmode, {o2, 0, 0},
2250                         GPR64, FPR64, asmop # "s">;
2251   def Uwd : A64I_fpintI<0b0, 0b01, rmode, {o2, 0, 1},
2252                         GPR32, FPR64, asmop # "u">;
2253   def Uxd : A64I_fpintI<0b1, 0b01, rmode, {o2, 0, 1},
2254                         GPR64, FPR64, asmop # "u">;
2255 }
2256
2257 defm FCVTN : A64I_fptointRM<0b00, 0b0, "fcvtn">;
2258 defm FCVTP : A64I_fptointRM<0b01, 0b0, "fcvtp">;
2259 defm FCVTM : A64I_fptointRM<0b10, 0b0, "fcvtm">;
2260 defm FCVTZ : A64I_fptointRM<0b11, 0b0, "fcvtz">;
2261 defm FCVTA : A64I_fptointRM<0b00, 0b1, "fcvta">;
2262
2263 def : Pat<(i32 (fp_to_sint FPR32:$Rn)), (FCVTZSws FPR32:$Rn)>;
2264 def : Pat<(i64 (fp_to_sint FPR32:$Rn)), (FCVTZSxs FPR32:$Rn)>;
2265 def : Pat<(i32 (fp_to_uint FPR32:$Rn)), (FCVTZUws FPR32:$Rn)>;
2266 def : Pat<(i64 (fp_to_uint FPR32:$Rn)), (FCVTZUxs FPR32:$Rn)>;
2267 def : Pat<(i32 (fp_to_sint (f64 FPR64:$Rn))), (FCVTZSwd FPR64:$Rn)>;
2268 def : Pat<(i64 (fp_to_sint (f64 FPR64:$Rn))), (FCVTZSxd FPR64:$Rn)>;
2269 def : Pat<(i32 (fp_to_uint (f64 FPR64:$Rn))), (FCVTZUwd FPR64:$Rn)>;
2270 def : Pat<(i64 (fp_to_uint (f64 FPR64:$Rn))), (FCVTZUxd FPR64:$Rn)>;
2271
2272 multiclass A64I_inttofp<bit o0, string asmop> {
2273   def CVTFsw : A64I_fpintI<0b0, 0b00, 0b00, {0, 1, o0}, FPR32, GPR32, asmop>;
2274   def CVTFsx : A64I_fpintI<0b1, 0b00, 0b00, {0, 1, o0}, FPR32, GPR64, asmop>;
2275   def CVTFdw : A64I_fpintI<0b0, 0b01, 0b00, {0, 1, o0}, FPR64, GPR32, asmop>;
2276   def CVTFdx : A64I_fpintI<0b1, 0b01, 0b00, {0, 1, o0}, FPR64, GPR64, asmop>;
2277 }
2278
2279 defm S : A64I_inttofp<0b0, "scvtf">;
2280 defm U : A64I_inttofp<0b1, "ucvtf">;
2281
2282 def : Pat<(f32 (sint_to_fp GPR32:$Rn)), (SCVTFsw GPR32:$Rn)>;
2283 def : Pat<(f32 (sint_to_fp GPR64:$Rn)), (SCVTFsx GPR64:$Rn)>;
2284 def : Pat<(f64 (sint_to_fp GPR32:$Rn)), (SCVTFdw GPR32:$Rn)>;
2285 def : Pat<(f64 (sint_to_fp GPR64:$Rn)), (SCVTFdx GPR64:$Rn)>;
2286 def : Pat<(f32 (uint_to_fp GPR32:$Rn)), (UCVTFsw GPR32:$Rn)>;
2287 def : Pat<(f32 (uint_to_fp GPR64:$Rn)), (UCVTFsx GPR64:$Rn)>;
2288 def : Pat<(f64 (uint_to_fp GPR32:$Rn)), (UCVTFdw GPR32:$Rn)>;
2289 def : Pat<(f64 (uint_to_fp GPR64:$Rn)), (UCVTFdx GPR64:$Rn)>;
2290
2291 def FMOVws : A64I_fpintI<0b0, 0b00, 0b00, 0b110, GPR32, FPR32, "fmov">;
2292 def FMOVsw : A64I_fpintI<0b0, 0b00, 0b00, 0b111, FPR32, GPR32, "fmov">;
2293 def FMOVxd : A64I_fpintI<0b1, 0b01, 0b00, 0b110, GPR64, FPR64, "fmov">;
2294 def FMOVdx : A64I_fpintI<0b1, 0b01, 0b00, 0b111, FPR64, GPR64, "fmov">;
2295
2296 def : Pat<(i32 (bitconvert (f32 FPR32:$Rn))), (FMOVws FPR32:$Rn)>;
2297 def : Pat<(f32 (bitconvert (i32 GPR32:$Rn))), (FMOVsw GPR32:$Rn)>;
2298 def : Pat<(i64 (bitconvert (f64 FPR64:$Rn))), (FMOVxd FPR64:$Rn)>;
2299 def : Pat<(f64 (bitconvert (i64 GPR64:$Rn))), (FMOVdx GPR64:$Rn)>;
2300
2301 def lane1_asmoperand : AsmOperandClass {
2302   let Name = "Lane1";
2303   let RenderMethod = "addImmOperands";
2304 }
2305
2306 def lane1 : Operand<i32> {
2307   let ParserMatchClass = lane1_asmoperand;
2308   let PrintMethod = "printBareImmOperand";
2309 }
2310
2311 let DecoderMethod =  "DecodeFMOVLaneInstruction" in {
2312   def FMOVxv : A64I_fpint<0b1, 0b0, 0b10, 0b01, 0b110,
2313                           (outs GPR64:$Rd), (ins VPR128:$Rn, lane1:$Lane),
2314                           "fmov\t$Rd, $Rn.d[$Lane]", [], NoItinerary>;
2315
2316   def FMOVvx : A64I_fpint<0b1, 0b0, 0b10, 0b01, 0b111,
2317                           (outs VPR128:$Rd), (ins GPR64:$Rn, lane1:$Lane),
2318                           "fmov\t$Rd.d[$Lane], $Rn", [], NoItinerary>;
2319 }
2320
2321 def : InstAlias<"fmov $Rd, $Rn.2d[$Lane]",
2322                 (FMOVxv GPR64:$Rd, VPR128:$Rn, lane1:$Lane), 0b0>;
2323
2324 def : InstAlias<"fmov $Rd.2d[$Lane], $Rn",
2325                 (FMOVvx VPR128:$Rd, GPR64:$Rn, lane1:$Lane), 0b0>;
2326
2327 //===----------------------------------------------------------------------===//
2328 // Floating-point immediate instructions
2329 //===----------------------------------------------------------------------===//
2330 // Contains: FMOV
2331
2332 def fpimm_asmoperand : AsmOperandClass {
2333   let Name = "FMOVImm";
2334   let ParserMethod = "ParseFPImmOperand";
2335 }
2336
2337 // The MCOperand for these instructions are the encoded 8-bit values.
2338 def SDXF_fpimm : SDNodeXForm<fpimm, [{
2339   uint32_t Imm8;
2340   A64Imms::isFPImm(N->getValueAPF(), Imm8);
2341   return CurDAG->getTargetConstant(Imm8, MVT::i32);
2342 }]>;
2343
2344 class fmov_operand<ValueType FT>
2345   : Operand<i32>,
2346     PatLeaf<(FT fpimm), [{ return A64Imms::isFPImm(N->getValueAPF()); }],
2347             SDXF_fpimm> {
2348   let PrintMethod = "printFPImmOperand";
2349   let ParserMatchClass = fpimm_asmoperand;
2350 }
2351
2352 def fmov32_operand : fmov_operand<f32>;
2353 def fmov64_operand : fmov_operand<f64>;
2354
2355 class A64I_fpimm_impl<bits<2> type, RegisterClass Reg, ValueType VT,
2356                       Operand fmov_operand>
2357   : A64I_fpimm<0b0, 0b0, type, 0b00000,
2358                (outs Reg:$Rd),
2359                (ins fmov_operand:$Imm8),
2360                "fmov\t$Rd, $Imm8",
2361                [(set (VT Reg:$Rd), fmov_operand:$Imm8)],
2362                NoItinerary>;
2363
2364 def FMOVsi : A64I_fpimm_impl<0b00, FPR32, f32, fmov32_operand>;
2365 def FMOVdi : A64I_fpimm_impl<0b01, FPR64, f64, fmov64_operand>;
2366
2367 //===----------------------------------------------------------------------===//
2368 // Load-register (literal) instructions
2369 //===----------------------------------------------------------------------===//
2370 // Contains: LDR, LDRSW, PRFM
2371
2372 def ldrlit_label_asmoperand : AsmOperandClass {
2373   let Name = "LoadLitLabel";
2374   let RenderMethod = "addLabelOperands<19, 4>";
2375 }
2376
2377 def ldrlit_label : Operand<i64> {
2378   let EncoderMethod = "getLoadLitLabelOpValue";
2379
2380   // This label is a 19-bit offset from PC, scaled by the instruction-width: 4.
2381   let PrintMethod = "printLabelOperand<19, 4>";
2382   let ParserMatchClass = ldrlit_label_asmoperand;
2383   let OperandType = "OPERAND_PCREL";
2384 }
2385
2386 // Various instructions take an immediate value (which can always be used),
2387 // where some numbers have a symbolic name to make things easier. These operands
2388 // and the associated functions abstract away the differences.
2389 multiclass namedimm<string prefix, string mapper> {
2390   def _asmoperand : AsmOperandClass {
2391     let Name = "NamedImm" # prefix;
2392     let PredicateMethod = "isUImm";
2393     let RenderMethod = "addImmOperands";
2394     let ParserMethod = "ParseNamedImmOperand<" # mapper # ">";
2395   }
2396
2397   def _op : Operand<i32> {
2398     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_asmoperand");
2399     let PrintMethod = "printNamedImmOperand<" # mapper # ">";
2400     let DecoderMethod = "DecodeNamedImmOperand<" # mapper # ">";
2401   }
2402 }
2403
2404 defm prefetch : namedimm<"prefetch", "A64PRFM::PRFMMapper">;
2405
2406 class A64I_LDRlitSimple<bits<2> opc, bit v, RegisterClass OutReg,
2407                       list<dag> patterns = []>
2408    : A64I_LDRlit<opc, v, (outs OutReg:$Rt), (ins ldrlit_label:$Imm19),
2409                  "ldr\t$Rt, $Imm19", patterns, NoItinerary>;
2410
2411 let mayLoad = 1 in {
2412   def LDRw_lit : A64I_LDRlitSimple<0b00, 0b0, GPR32,
2413                               [(set (i32 GPR32:$Rt), (load constpool:$Imm19))]>;
2414   def LDRx_lit : A64I_LDRlitSimple<0b01, 0b0, GPR64,
2415                               [(set (i64 GPR64:$Rt), (load constpool:$Imm19))]>;
2416 }
2417
2418 def LDRs_lit  : A64I_LDRlitSimple<0b00, 0b1, FPR32,
2419                               [(set (f32 FPR32:$Rt), (load constpool:$Imm19))]>;
2420 def LDRd_lit  : A64I_LDRlitSimple<0b01, 0b1, FPR64,
2421                               [(set (f64 FPR64:$Rt), (load constpool:$Imm19))]>;
2422
2423 let mayLoad = 1 in {
2424   def LDRq_lit : A64I_LDRlitSimple<0b10, 0b1, FPR128>;
2425
2426
2427   def LDRSWx_lit : A64I_LDRlit<0b10, 0b0,
2428                                (outs GPR64:$Rt),
2429                                (ins ldrlit_label:$Imm19),
2430                                "ldrsw\t$Rt, $Imm19",
2431                                [], NoItinerary>;
2432
2433   def PRFM_lit : A64I_LDRlit<0b11, 0b0,
2434                              (outs), (ins prefetch_op:$Rt, ldrlit_label:$Imm19),
2435                              "prfm\t$Rt, $Imm19",
2436                              [], NoItinerary>;
2437 }
2438
2439 //===----------------------------------------------------------------------===//
2440 // Load-store exclusive instructions
2441 //===----------------------------------------------------------------------===//
2442 // Contains: STXRB, STXRH, STXR, LDXRB, LDXRH, LDXR. STXP, LDXP, STLXRB,
2443 //           STLXRH, STLXR, LDAXRB, LDAXRH, LDAXR, STLXP, LDAXP, STLRB,
2444 //           STLRH, STLR, LDARB, LDARH, LDAR
2445
2446 // Since these instructions have the undefined register bits set to 1 in
2447 // their canonical form, we need a post encoder method to set those bits
2448 // to 1 when encoding these instructions. We do this using the
2449 // fixLoadStoreExclusive function. This function has template parameters:
2450 //
2451 // fixLoadStoreExclusive<int hasRs, int hasRt2>
2452 //
2453 // hasRs indicates that the instruction uses the Rs field, so we won't set
2454 // it to 1 (and the same for Rt2). We don't need template parameters for
2455 // the other register fiels since Rt and Rn are always used.
2456
2457 // This operand parses a GPR64xsp register, followed by an optional immediate
2458 // #0.
2459 def GPR64xsp0_asmoperand : AsmOperandClass {
2460   let Name = "GPR64xsp0";
2461   let PredicateMethod = "isWrappedReg";
2462   let RenderMethod = "addRegOperands";
2463   let ParserMethod = "ParseLSXAddressOperand";
2464 }
2465
2466 def GPR64xsp0 : RegisterOperand<GPR64xsp> {
2467   let ParserMatchClass = GPR64xsp0_asmoperand;
2468 }
2469
2470 //===----------------------------------
2471 // Store-exclusive (releasing & normal)
2472 //===----------------------------------
2473
2474 class A64I_SRexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2475                         dag ins, list<dag> pat,
2476                         InstrItinClass itin> :
2477        A64I_LDSTex_stn <size,
2478                         opcode{2}, 0, opcode{1}, opcode{0},
2479                         outs, ins,
2480                         !strconcat(asm, "\t$Rs, $Rt, [$Rn]"),
2481                         pat, itin> {
2482   let mayStore = 1;
2483   let PostEncoderMethod = "fixLoadStoreExclusive<1,0>";
2484 }
2485
2486 multiclass A64I_SRex<string asmstr, bits<3> opcode, string prefix> {
2487   def _byte:  A64I_SRexs_impl<0b00, opcode, !strconcat(asmstr, "b"),
2488                               (outs GPR32:$Rs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2489                               [], NoItinerary>;
2490
2491   def _hword:  A64I_SRexs_impl<0b01, opcode, !strconcat(asmstr, "h"),
2492                                (outs GPR32:$Rs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2493                                [],NoItinerary>;
2494
2495   def _word:  A64I_SRexs_impl<0b10, opcode, asmstr,
2496                               (outs GPR32:$Rs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2497                               [], NoItinerary>;
2498
2499   def _dword: A64I_SRexs_impl<0b11, opcode, asmstr,
2500                               (outs GPR32:$Rs), (ins GPR64:$Rt, GPR64xsp0:$Rn),
2501                               [], NoItinerary>;
2502 }
2503
2504 defm STXR  : A64I_SRex<"stxr",  0b000, "STXR">;
2505 defm STLXR : A64I_SRex<"stlxr", 0b001, "STLXR">;
2506
2507 //===----------------------------------
2508 // Loads
2509 //===----------------------------------
2510
2511 class A64I_LRexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2512                         dag ins, list<dag> pat,
2513                         InstrItinClass itin> :
2514         A64I_LDSTex_tn <size,
2515                         opcode{2}, 1, opcode{1}, opcode{0},
2516                         outs, ins,
2517                         !strconcat(asm, "\t$Rt, [$Rn]"),
2518                         pat, itin> {
2519   let mayLoad = 1;
2520   let PostEncoderMethod = "fixLoadStoreExclusive<0,0>";
2521 }
2522
2523 multiclass A64I_LRex<string asmstr, bits<3> opcode> {
2524   def _byte:  A64I_LRexs_impl<0b00, opcode, !strconcat(asmstr, "b"),
2525                             (outs GPR32:$Rt), (ins GPR64xsp0:$Rn),
2526                             [], NoItinerary>;
2527
2528   def _hword:  A64I_LRexs_impl<0b01, opcode, !strconcat(asmstr, "h"),
2529                             (outs GPR32:$Rt), (ins GPR64xsp0:$Rn),
2530                             [], NoItinerary>;
2531
2532   def _word:  A64I_LRexs_impl<0b10, opcode, asmstr,
2533                             (outs GPR32:$Rt), (ins GPR64xsp0:$Rn),
2534                             [], NoItinerary>;
2535
2536   def _dword: A64I_LRexs_impl<0b11, opcode, asmstr,
2537                             (outs GPR64:$Rt), (ins GPR64xsp0:$Rn),
2538                             [], NoItinerary>;
2539 }
2540
2541 defm LDXR  : A64I_LRex<"ldxr",  0b000>;
2542 defm LDAXR : A64I_LRex<"ldaxr", 0b001>;
2543 defm LDAR  : A64I_LRex<"ldar",  0b101>;
2544
2545 class acquiring_load<PatFrag base>
2546   : PatFrag<(ops node:$ptr), (base node:$ptr), [{
2547   return cast<AtomicSDNode>(N)->getOrdering() == Acquire;
2548 }]>;
2549
2550 def atomic_load_acquire_8  : acquiring_load<atomic_load_8>;
2551 def atomic_load_acquire_16 : acquiring_load<atomic_load_16>;
2552 def atomic_load_acquire_32 : acquiring_load<atomic_load_32>;
2553 def atomic_load_acquire_64 : acquiring_load<atomic_load_64>;
2554
2555 def : Pat<(atomic_load_acquire_8  GPR64xsp:$Rn), (LDAR_byte  GPR64xsp0:$Rn)>;
2556 def : Pat<(atomic_load_acquire_16 GPR64xsp:$Rn), (LDAR_hword GPR64xsp0:$Rn)>;
2557 def : Pat<(atomic_load_acquire_32 GPR64xsp:$Rn), (LDAR_word  GPR64xsp0:$Rn)>;
2558 def : Pat<(atomic_load_acquire_64 GPR64xsp:$Rn), (LDAR_dword GPR64xsp0:$Rn)>;
2559
2560 //===----------------------------------
2561 // Store-release (no exclusivity)
2562 //===----------------------------------
2563
2564 class A64I_SLexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2565                         dag ins, list<dag> pat,
2566                         InstrItinClass itin> :
2567         A64I_LDSTex_tn <size,
2568                         opcode{2}, 0, opcode{1}, opcode{0},
2569                         outs, ins,
2570                         !strconcat(asm, "\t$Rt, [$Rn]"),
2571                         pat, itin> {
2572   let mayStore = 1;
2573   let PostEncoderMethod = "fixLoadStoreExclusive<0,0>";
2574 }
2575
2576 class releasing_store<PatFrag base>
2577   : PatFrag<(ops node:$ptr, node:$val), (base node:$ptr, node:$val), [{
2578   return cast<AtomicSDNode>(N)->getOrdering() == Release;
2579 }]>;
2580
2581 def atomic_store_release_8  : releasing_store<atomic_store_8>;
2582 def atomic_store_release_16 : releasing_store<atomic_store_16>;
2583 def atomic_store_release_32 : releasing_store<atomic_store_32>;
2584 def atomic_store_release_64 : releasing_store<atomic_store_64>;
2585
2586 multiclass A64I_SLex<string asmstr, bits<3> opcode, string prefix> {
2587   def _byte:  A64I_SLexs_impl<0b00, opcode, !strconcat(asmstr, "b"),
2588                             (outs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2589                             [(atomic_store_release_8 GPR64xsp0:$Rn, GPR32:$Rt)],
2590                             NoItinerary>;
2591
2592   def _hword:  A64I_SLexs_impl<0b01, opcode, !strconcat(asmstr, "h"),
2593                            (outs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2594                            [(atomic_store_release_16 GPR64xsp0:$Rn, GPR32:$Rt)],
2595                            NoItinerary>;
2596
2597   def _word:  A64I_SLexs_impl<0b10, opcode, asmstr,
2598                            (outs), (ins GPR32:$Rt, GPR64xsp0:$Rn),
2599                            [(atomic_store_release_32 GPR64xsp0:$Rn, GPR32:$Rt)],
2600                            NoItinerary>;
2601
2602   def _dword: A64I_SLexs_impl<0b11, opcode, asmstr,
2603                            (outs), (ins GPR64:$Rt, GPR64xsp0:$Rn),
2604                            [(atomic_store_release_64 GPR64xsp0:$Rn, GPR64:$Rt)],
2605                            NoItinerary>;
2606 }
2607
2608 defm STLR  : A64I_SLex<"stlr", 0b101, "STLR">;
2609
2610 //===----------------------------------
2611 // Store-exclusive pair (releasing & normal)
2612 //===----------------------------------
2613
2614 class A64I_SPexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2615                         dag ins, list<dag> pat,
2616                         InstrItinClass itin> :
2617      A64I_LDSTex_stt2n <size,
2618                         opcode{2}, 0, opcode{1}, opcode{0},
2619                         outs, ins,
2620                         !strconcat(asm, "\t$Rs, $Rt, $Rt2, [$Rn]"),
2621                         pat, itin> {
2622   let mayStore = 1;
2623 }
2624
2625
2626 multiclass A64I_SPex<string asmstr, bits<3> opcode> {
2627   def _word:  A64I_SPexs_impl<0b10, opcode, asmstr, (outs),
2628                             (ins GPR32:$Rs, GPR32:$Rt, GPR32:$Rt2,
2629                                  GPR64xsp0:$Rn),
2630                             [], NoItinerary>;
2631
2632   def _dword: A64I_SPexs_impl<0b11, opcode, asmstr, (outs),
2633                             (ins GPR32:$Rs, GPR64:$Rt, GPR64:$Rt2,
2634                                             GPR64xsp0:$Rn),
2635                             [], NoItinerary>;
2636 }
2637
2638 defm STXP  : A64I_SPex<"stxp", 0b010>;
2639 defm STLXP : A64I_SPex<"stlxp", 0b011>;
2640
2641 //===----------------------------------
2642 // Load-exclusive pair (acquiring & normal)
2643 //===----------------------------------
2644
2645 class A64I_LPexs_impl<bits<2> size, bits<3> opcode, string asm, dag outs,
2646                         dag ins, list<dag> pat,
2647                         InstrItinClass itin> :
2648       A64I_LDSTex_tt2n <size,
2649                         opcode{2}, 1, opcode{1}, opcode{0},
2650                         outs, ins,
2651                         !strconcat(asm, "\t$Rt, $Rt2, [$Rn]"),
2652                         pat, itin>{
2653   let mayLoad = 1;
2654   let DecoderMethod = "DecodeLoadPairExclusiveInstruction";
2655   let PostEncoderMethod = "fixLoadStoreExclusive<0,1>";
2656 }
2657
2658 multiclass A64I_LPex<string asmstr, bits<3> opcode> {
2659   def _word:  A64I_LPexs_impl<0b10, opcode, asmstr,
2660                             (outs GPR32:$Rt, GPR32:$Rt2),
2661                             (ins GPR64xsp0:$Rn),
2662                             [], NoItinerary>;
2663
2664   def _dword: A64I_LPexs_impl<0b11, opcode, asmstr,
2665                             (outs GPR64:$Rt, GPR64:$Rt2),
2666                             (ins GPR64xsp0:$Rn),
2667                             [], NoItinerary>;
2668 }
2669
2670 defm LDXP  : A64I_LPex<"ldxp", 0b010>;
2671 defm LDAXP : A64I_LPex<"ldaxp", 0b011>;
2672
2673 //===----------------------------------------------------------------------===//
2674 // Load-store register (unscaled immediate) instructions
2675 //===----------------------------------------------------------------------===//
2676 // Contains: LDURB, LDURH, LDRUSB, LDRUSH, LDRUSW, STUR, STURB, STURH and PRFUM
2677 //
2678 // and
2679 //
2680 //===----------------------------------------------------------------------===//
2681 // Load-store register (register offset) instructions
2682 //===----------------------------------------------------------------------===//
2683 // Contains: LDRB, LDRH, LDRSB, LDRSH, LDRSW, STR, STRB, STRH and PRFM
2684 //
2685 // and
2686 //
2687 //===----------------------------------------------------------------------===//
2688 // Load-store register (unsigned immediate) instructions
2689 //===----------------------------------------------------------------------===//
2690 // Contains: LDRB, LDRH, LDRSB, LDRSH, LDRSW, STR, STRB, STRH and PRFM
2691 //
2692 // and
2693 //
2694 //===----------------------------------------------------------------------===//
2695 // Load-store register (immediate post-indexed) instructions
2696 //===----------------------------------------------------------------------===//
2697 // Contains: STRB, STRH, STR, LDRB, LDRH, LDR, LDRSB, LDRSH, LDRSW
2698 //
2699 // and
2700 //
2701 //===----------------------------------------------------------------------===//
2702 // Load-store register (immediate pre-indexed) instructions
2703 //===----------------------------------------------------------------------===//
2704 // Contains: STRB, STRH, STR, LDRB, LDRH, LDR, LDRSB, LDRSH, LDRSW
2705
2706 // Note that patterns are much later on in a completely separate section (they
2707 // need ADRPxi to be defined).
2708
2709 //===-------------------------------
2710 // 1. Various operands needed
2711 //===-------------------------------
2712
2713 //===-------------------------------
2714 // 1.1 Unsigned 12-bit immediate operands
2715 //===-------------------------------
2716 // The addressing mode for these instructions consists of an unsigned 12-bit
2717 // immediate which is scaled by the size of the memory access.
2718 //
2719 // We represent this in the MC layer by two operands:
2720 //     1. A base register.
2721 //     2. A 12-bit immediate: not multiplied by access size, so "LDR x0,[x0,#8]"
2722 //        would have '1' in this field.
2723 // This means that separate functions are needed for converting representations
2724 // which *are* aware of the intended access size.
2725
2726 // Anything that creates an MCInst (Decoding, selection and AsmParsing) has to
2727 // know the access size via some means. An isolated operand does not have this
2728 // information unless told from here, which means we need separate tablegen
2729 // Operands for each access size. This multiclass takes care of instantiating
2730 // the correct template functions in the rest of the backend.
2731
2732 //===-------------------------------
2733 // 1.1 Unsigned 12-bit immediate operands
2734 //===-------------------------------
2735
2736 multiclass offsets_uimm12<int MemSize, string prefix> {
2737   def uimm12_asmoperand : AsmOperandClass {
2738     let Name = "OffsetUImm12_" # MemSize;
2739     let PredicateMethod = "isOffsetUImm12<" # MemSize # ">";
2740     let RenderMethod = "addOffsetUImm12Operands<" # MemSize # ">";
2741   }
2742
2743   // Pattern is really no more than an ImmLeaf, but predicated on MemSize which
2744   // complicates things beyond TableGen's ken.
2745   def uimm12 : Operand<i64>,
2746                ComplexPattern<i64, 1, "SelectOffsetUImm12<" # MemSize # ">"> {
2747     let ParserMatchClass
2748       = !cast<AsmOperandClass>(prefix # uimm12_asmoperand);
2749
2750     let PrintMethod = "printOffsetUImm12Operand<" # MemSize # ">";
2751     let EncoderMethod = "getOffsetUImm12OpValue<" # MemSize # ">";
2752   }
2753 }
2754
2755 defm byte_  : offsets_uimm12<1, "byte_">;
2756 defm hword_ : offsets_uimm12<2, "hword_">;
2757 defm word_  : offsets_uimm12<4, "word_">;
2758 defm dword_ : offsets_uimm12<8, "dword_">;
2759 defm qword_ : offsets_uimm12<16, "qword_">;
2760
2761 //===-------------------------------
2762 // 1.1 Signed 9-bit immediate operands
2763 //===-------------------------------
2764
2765 // The MCInst is expected to store the bit-wise encoding of the value,
2766 // which amounts to lopping off the extended sign bits.
2767 def SDXF_simm9 : SDNodeXForm<imm, [{
2768   return CurDAG->getTargetConstant(N->getZExtValue() & 0x1ff, MVT::i32);
2769 }]>;
2770
2771 def simm9_asmoperand : AsmOperandClass {
2772   let Name = "SImm9";
2773   let PredicateMethod = "isSImm<9>";
2774   let RenderMethod = "addSImmOperands<9>";
2775 }
2776
2777 def simm9 : Operand<i64>,
2778             ImmLeaf<i64, [{ return Imm >= -0x100 && Imm <= 0xff; }],
2779             SDXF_simm9> {
2780   let PrintMethod = "printOffsetSImm9Operand";
2781   let ParserMatchClass = simm9_asmoperand;
2782 }
2783
2784
2785 //===-------------------------------
2786 // 1.3 Register offset extensions
2787 //===-------------------------------
2788
2789 // The assembly-syntax for these addressing-modes is:
2790 //    [<Xn|SP>, <R><m> {, <extend> {<amount>}}]
2791 //
2792 // The essential semantics are:
2793 //     + <amount> is a shift: #<log(transfer size)> or #0
2794 //     + <R> can be W or X.
2795 //     + If <R> is W, <extend> can be UXTW or SXTW
2796 //     + If <R> is X, <extend> can be LSL or SXTX
2797 //
2798 // The trickiest of those constraints is that Rm can be either GPR32 or GPR64,
2799 // which will need separate instructions for LLVM type-consistency. We'll also
2800 // need separate operands, of course.
2801 multiclass regexts<int MemSize, int RmSize, RegisterClass GPR,
2802                    string Rm, string prefix> {
2803   def regext_asmoperand : AsmOperandClass {
2804     let Name = "AddrRegExtend_" # MemSize # "_" #  Rm;
2805     let PredicateMethod = "isAddrRegExtend<" # MemSize # "," # RmSize # ">";
2806     let RenderMethod = "addAddrRegExtendOperands<" # MemSize # ">";
2807   }
2808
2809   def regext : Operand<i64> {
2810     let PrintMethod
2811       = "printAddrRegExtendOperand<" # MemSize # ", " # RmSize # ">";
2812
2813     let DecoderMethod = "DecodeAddrRegExtendOperand";
2814     let ParserMatchClass
2815       = !cast<AsmOperandClass>(prefix # regext_asmoperand);
2816   }
2817 }
2818
2819 multiclass regexts_wx<int MemSize, string prefix> {
2820   // Rm is an X-register if LSL or SXTX are specified as the shift.
2821   defm Xm_ : regexts<MemSize, 64, GPR64, "Xm", prefix # "Xm_">;
2822
2823   // Rm is a W-register if UXTW or SXTW are specified as the shift.
2824   defm Wm_ : regexts<MemSize, 32, GPR32, "Wm", prefix # "Wm_">;
2825 }
2826
2827 defm byte_  : regexts_wx<1, "byte_">;
2828 defm hword_ : regexts_wx<2, "hword_">;
2829 defm word_  : regexts_wx<4, "word_">;
2830 defm dword_ : regexts_wx<8, "dword_">;
2831 defm qword_ : regexts_wx<16, "qword_">;
2832
2833
2834 //===------------------------------
2835 // 2. The instructions themselves.
2836 //===------------------------------
2837
2838 // We have the following instructions to implement:
2839 // |                 | B     | H     | W     | X      |
2840 // |-----------------+-------+-------+-------+--------|
2841 // | unsigned str    | STRB  | STRH  | STR   | STR    |
2842 // | unsigned ldr    | LDRB  | LDRH  | LDR   | LDR    |
2843 // | signed ldr to W | LDRSB | LDRSH | -     | -      |
2844 // | signed ldr to X | LDRSB | LDRSH | LDRSW | (PRFM) |
2845
2846 // This will instantiate the LDR/STR instructions you'd expect to use for an
2847 // unsigned datatype (first two rows above) or floating-point register, which is
2848 // reasonably uniform across all access sizes.
2849
2850
2851 //===------------------------------
2852 // 2.1 Regular instructions
2853 //===------------------------------
2854
2855 // This class covers the basic unsigned or irrelevantly-signed loads and stores,
2856 // to general-purpose and floating-point registers.
2857
2858 class AddrParams<string prefix> {
2859   Operand uimm12 = !cast<Operand>(prefix # "_uimm12");
2860
2861   Operand regextWm = !cast<Operand>(prefix # "_Wm_regext");
2862   Operand regextXm = !cast<Operand>(prefix # "_Xm_regext");
2863 }
2864
2865 def byte_addrparams : AddrParams<"byte">;
2866 def hword_addrparams : AddrParams<"hword">;
2867 def word_addrparams : AddrParams<"word">;
2868 def dword_addrparams : AddrParams<"dword">;
2869 def qword_addrparams : AddrParams<"qword">;
2870
2871 multiclass A64I_LDRSTR_unsigned<string prefix, bits<2> size, bit v,
2872                                 bit high_opc, string asmsuffix,
2873                                 RegisterClass GPR, AddrParams params> {
2874   // Unsigned immediate
2875   def _STR : A64I_LSunsigimm<size, v, {high_opc, 0b0},
2876                      (outs), (ins GPR:$Rt, GPR64xsp:$Rn, params.uimm12:$UImm12),
2877                      "str" # asmsuffix # "\t$Rt, [$Rn, $UImm12]",
2878                      [], NoItinerary> {
2879     let mayStore = 1;
2880   }
2881   def : InstAlias<"str" # asmsuffix # " $Rt, [$Rn]",
2882                 (!cast<Instruction>(prefix # "_STR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
2883
2884   def _LDR : A64I_LSunsigimm<size, v, {high_opc, 0b1},
2885                       (outs GPR:$Rt), (ins GPR64xsp:$Rn, params.uimm12:$UImm12),
2886                       "ldr" #  asmsuffix # "\t$Rt, [$Rn, $UImm12]",
2887                       [], NoItinerary> {
2888     let mayLoad = 1;
2889   }
2890   def : InstAlias<"ldr" # asmsuffix # " $Rt, [$Rn]",
2891                 (!cast<Instruction>(prefix # "_LDR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
2892
2893   // Register offset (four of these: load/store and Wm/Xm).
2894   let mayLoad = 1 in {
2895     def _Wm_RegOffset_LDR : A64I_LSregoff<size, v, {high_opc, 0b1}, 0b0,
2896                             (outs GPR:$Rt),
2897                             (ins GPR64xsp:$Rn, GPR32:$Rm, params.regextWm:$Ext),
2898                             "ldr" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
2899                             [], NoItinerary>;
2900
2901     def _Xm_RegOffset_LDR : A64I_LSregoff<size, v, {high_opc, 0b1}, 0b1,
2902                             (outs GPR:$Rt),
2903                             (ins GPR64xsp:$Rn, GPR64:$Rm, params.regextXm:$Ext),
2904                             "ldr" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
2905                             [], NoItinerary>;
2906   }
2907   def : InstAlias<"ldr" # asmsuffix # " $Rt, [$Rn, $Rm]",
2908         (!cast<Instruction>(prefix # "_Xm_RegOffset_LDR") GPR:$Rt, GPR64xsp:$Rn,
2909                                                           GPR64:$Rm, 2)>;
2910
2911   let mayStore = 1 in {
2912     def _Wm_RegOffset_STR : A64I_LSregoff<size, v, {high_opc, 0b0}, 0b0,
2913                                   (outs), (ins GPR:$Rt, GPR64xsp:$Rn, GPR32:$Rm,
2914                                                params.regextWm:$Ext),
2915                                   "str" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
2916                                   [], NoItinerary>;
2917
2918     def _Xm_RegOffset_STR : A64I_LSregoff<size, v, {high_opc, 0b0}, 0b1,
2919                                   (outs), (ins GPR:$Rt, GPR64xsp:$Rn, GPR64:$Rm,
2920                                                params.regextXm:$Ext),
2921                                   "str" # asmsuffix # "\t$Rt, [$Rn, $Rm, $Ext]",
2922                                   [], NoItinerary>;
2923   }
2924   def : InstAlias<"str" # asmsuffix # " $Rt, [$Rn, $Rm]",
2925       (!cast<Instruction>(prefix # "_Xm_RegOffset_STR") GPR:$Rt, GPR64xsp:$Rn,
2926                                                         GPR64:$Rm, 2)>;
2927
2928   // Unaligned immediate
2929   def _STUR : A64I_LSunalimm<size, v, {high_opc, 0b0},
2930                              (outs), (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
2931                              "stur" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
2932                              [], NoItinerary> {
2933     let mayStore = 1;
2934   }
2935   def : InstAlias<"stur" # asmsuffix # " $Rt, [$Rn]",
2936                (!cast<Instruction>(prefix # "_STUR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
2937
2938   def _LDUR : A64I_LSunalimm<size, v, {high_opc, 0b1},
2939                              (outs GPR:$Rt), (ins GPR64xsp:$Rn, simm9:$SImm9),
2940                              "ldur" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
2941                              [], NoItinerary> {
2942     let mayLoad = 1;
2943   }
2944   def : InstAlias<"ldur" # asmsuffix # " $Rt, [$Rn]",
2945                (!cast<Instruction>(prefix # "_LDUR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
2946
2947   // Post-indexed
2948   def _PostInd_STR : A64I_LSpostind<size, v, {high_opc, 0b0},
2949                                (outs GPR64xsp:$Rn_wb),
2950                                (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
2951                                "str" # asmsuffix # "\t$Rt, [$Rn], $SImm9",
2952                                [], NoItinerary> {
2953     let Constraints = "$Rn = $Rn_wb";
2954     let mayStore = 1;
2955
2956     // Decoder only needed for unpredictability checking (FIXME).
2957     let DecoderMethod = "DecodeSingleIndexedInstruction";
2958   }
2959
2960   def _PostInd_LDR : A64I_LSpostind<size, v, {high_opc, 0b1},
2961                                     (outs GPR:$Rt, GPR64xsp:$Rn_wb),
2962                                     (ins GPR64xsp:$Rn, simm9:$SImm9),
2963                                     "ldr" # asmsuffix # "\t$Rt, [$Rn], $SImm9",
2964                                     [], NoItinerary> {
2965     let mayLoad = 1;
2966     let Constraints = "$Rn = $Rn_wb";
2967     let DecoderMethod = "DecodeSingleIndexedInstruction";
2968   }
2969
2970   // Pre-indexed
2971   def _PreInd_STR : A64I_LSpreind<size, v, {high_opc, 0b0},
2972                                (outs GPR64xsp:$Rn_wb),
2973                                (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
2974                                "str" # asmsuffix # "\t$Rt, [$Rn, $SImm9]!",
2975                                [], NoItinerary> {
2976     let Constraints = "$Rn = $Rn_wb";
2977     let mayStore = 1;
2978
2979     // Decoder only needed for unpredictability checking (FIXME).
2980     let DecoderMethod = "DecodeSingleIndexedInstruction";
2981   }
2982
2983   def _PreInd_LDR : A64I_LSpreind<size, v, {high_opc, 0b1},
2984                                     (outs GPR:$Rt, GPR64xsp:$Rn_wb),
2985                                     (ins GPR64xsp:$Rn, simm9:$SImm9),
2986                                     "ldr" # asmsuffix # "\t$Rt, [$Rn, $SImm9]!",
2987                                     [], NoItinerary> {
2988     let mayLoad = 1;
2989     let Constraints = "$Rn = $Rn_wb";
2990     let DecoderMethod = "DecodeSingleIndexedInstruction";
2991   }
2992
2993 }
2994
2995 // STRB/LDRB: First define the instructions
2996 defm LS8
2997   : A64I_LDRSTR_unsigned<"LS8", 0b00, 0b0, 0b0, "b", GPR32, byte_addrparams>;
2998
2999 // STRH/LDRH
3000 defm LS16
3001   : A64I_LDRSTR_unsigned<"LS16", 0b01, 0b0, 0b0, "h", GPR32, hword_addrparams>;
3002
3003
3004 // STR/LDR to/from a W register
3005 defm LS32
3006   : A64I_LDRSTR_unsigned<"LS32", 0b10, 0b0, 0b0, "", GPR32, word_addrparams>;
3007
3008 // STR/LDR to/from an X register
3009 defm LS64
3010   : A64I_LDRSTR_unsigned<"LS64", 0b11, 0b0, 0b0, "", GPR64, dword_addrparams>;
3011
3012 // STR/LDR to/from a B register
3013 defm LSFP8
3014   : A64I_LDRSTR_unsigned<"LSFP8", 0b00, 0b1, 0b0, "", FPR8, byte_addrparams>;
3015
3016 // STR/LDR to/from an H register
3017 defm LSFP16
3018   : A64I_LDRSTR_unsigned<"LSFP16", 0b01, 0b1, 0b0, "", FPR16, hword_addrparams>;
3019
3020 // STR/LDR to/from an S register
3021 defm LSFP32
3022   : A64I_LDRSTR_unsigned<"LSFP32", 0b10, 0b1, 0b0, "", FPR32, word_addrparams>;
3023 // STR/LDR to/from a D register
3024 defm LSFP64
3025   : A64I_LDRSTR_unsigned<"LSFP64", 0b11, 0b1, 0b0, "", FPR64, dword_addrparams>;
3026 // STR/LDR to/from a Q register
3027 defm LSFP128
3028   : A64I_LDRSTR_unsigned<"LSFP128", 0b00, 0b1, 0b1, "", FPR128,
3029                          qword_addrparams>;
3030
3031 //===------------------------------
3032 // 2.3 Signed loads
3033 //===------------------------------
3034
3035 // Byte and half-word signed loads can both go into either an X or a W register,
3036 // so it's worth factoring out. Signed word loads don't fit because there is no
3037 // W version.
3038 multiclass A64I_LDR_signed<bits<2> size, string asmopcode, AddrParams params,
3039                            string prefix> {
3040   // Unsigned offset
3041   def w : A64I_LSunsigimm<size, 0b0, 0b11,
3042                           (outs GPR32:$Rt),
3043                           (ins GPR64xsp:$Rn, params.uimm12:$UImm12),
3044                           "ldrs" # asmopcode # "\t$Rt, [$Rn, $UImm12]",
3045                           [], NoItinerary> {
3046     let mayLoad = 1;
3047   }
3048   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn]",
3049                   (!cast<Instruction>(prefix # w) GPR32:$Rt, GPR64xsp:$Rn, 0)>;
3050
3051   def x : A64I_LSunsigimm<size, 0b0, 0b10,
3052                           (outs GPR64:$Rt),
3053                           (ins GPR64xsp:$Rn, params.uimm12:$UImm12),
3054                           "ldrs" # asmopcode # "\t$Rt, [$Rn, $UImm12]",
3055                           [], NoItinerary> {
3056     let mayLoad = 1;
3057   }
3058   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn]",
3059                   (!cast<Instruction>(prefix # x) GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3060
3061   // Register offset
3062   let mayLoad = 1 in {
3063     def w_Wm_RegOffset : A64I_LSregoff<size, 0b0, 0b11, 0b0,
3064                             (outs GPR32:$Rt),
3065                             (ins GPR64xsp:$Rn, GPR32:$Rm, params.regextWm:$Ext),
3066                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3067                             [], NoItinerary>;
3068
3069     def w_Xm_RegOffset : A64I_LSregoff<size, 0b0, 0b11, 0b1,
3070                             (outs GPR32:$Rt),
3071                             (ins GPR64xsp:$Rn, GPR64:$Rm, params.regextXm:$Ext),
3072                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3073                             [], NoItinerary>;
3074
3075     def x_Wm_RegOffset : A64I_LSregoff<size, 0b0, 0b10, 0b0,
3076                             (outs GPR64:$Rt),
3077                             (ins GPR64xsp:$Rn, GPR32:$Rm, params.regextWm:$Ext),
3078                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3079                             [], NoItinerary>;
3080
3081     def x_Xm_RegOffset : A64I_LSregoff<size, 0b0, 0b10, 0b1,
3082                             (outs GPR64:$Rt),
3083                             (ins GPR64xsp:$Rn, GPR64:$Rm, params.regextXm:$Ext),
3084                             "ldrs" # asmopcode # "\t$Rt, [$Rn, $Rm, $Ext]",
3085                             [], NoItinerary>;
3086   }
3087   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn, $Rm]",
3088         (!cast<Instruction>(prefix # "w_Xm_RegOffset") GPR32:$Rt, GPR64xsp:$Rn,
3089                                                        GPR64:$Rm, 2)>;
3090
3091   def : InstAlias<"ldrs" # asmopcode # " $Rt, [$Rn, $Rm]",
3092         (!cast<Instruction>(prefix # "x_Xm_RegOffset") GPR64:$Rt, GPR64xsp:$Rn,
3093                                                        GPR64:$Rm, 2)>;
3094
3095
3096   let mayLoad = 1 in {
3097     // Unaligned offset
3098     def w_U : A64I_LSunalimm<size, 0b0, 0b11,
3099                              (outs GPR32:$Rt),
3100                              (ins GPR64xsp:$Rn, simm9:$SImm9),
3101                              "ldurs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3102                              [], NoItinerary>;
3103
3104     def x_U : A64I_LSunalimm<size, 0b0, 0b10,
3105                              (outs GPR64:$Rt),
3106                              (ins GPR64xsp:$Rn, simm9:$SImm9),
3107                              "ldurs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3108                              [], NoItinerary>;
3109
3110
3111     // Post-indexed
3112     def w_PostInd : A64I_LSpostind<size, 0b0, 0b11,
3113                                  (outs GPR32:$Rt, GPR64xsp:$Rn_wb),
3114                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3115                                  "ldrs" # asmopcode # "\t$Rt, [$Rn], $SImm9",
3116                                  [], NoItinerary> {
3117       let Constraints = "$Rn = $Rn_wb";
3118       let DecoderMethod = "DecodeSingleIndexedInstruction";
3119     }
3120
3121     def x_PostInd : A64I_LSpostind<size, 0b0, 0b10,
3122                                    (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3123                                    (ins GPR64xsp:$Rn, simm9:$SImm9),
3124                                    "ldrs" # asmopcode # "\t$Rt, [$Rn], $SImm9",
3125                                    [], NoItinerary> {
3126       let Constraints = "$Rn = $Rn_wb";
3127       let DecoderMethod = "DecodeSingleIndexedInstruction";
3128     }
3129
3130     // Pre-indexed
3131     def w_PreInd : A64I_LSpreind<size, 0b0, 0b11,
3132                                  (outs GPR32:$Rt, GPR64xsp:$Rn_wb),
3133                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3134                                  "ldrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]!",
3135                                  [], NoItinerary> {
3136       let Constraints = "$Rn = $Rn_wb";
3137       let DecoderMethod = "DecodeSingleIndexedInstruction";
3138     }
3139
3140     def x_PreInd : A64I_LSpreind<size, 0b0, 0b10,
3141                                  (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3142                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3143                                  "ldrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]!",
3144                                  [], NoItinerary> {
3145       let Constraints = "$Rn = $Rn_wb";
3146       let DecoderMethod = "DecodeSingleIndexedInstruction";
3147     }
3148   } // let mayLoad = 1
3149 }
3150
3151 // LDRSB
3152 defm LDRSB : A64I_LDR_signed<0b00, "b", byte_addrparams, "LDRSB">;
3153 // LDRSH
3154 defm LDRSH : A64I_LDR_signed<0b01, "h", hword_addrparams, "LDRSH">;
3155
3156 // LDRSW: load a 32-bit register, sign-extending to 64-bits.
3157 def LDRSWx
3158     : A64I_LSunsigimm<0b10, 0b0, 0b10,
3159                     (outs GPR64:$Rt),
3160                     (ins GPR64xsp:$Rn, word_uimm12:$UImm12),
3161                     "ldrsw\t$Rt, [$Rn, $UImm12]",
3162                     [], NoItinerary> {
3163   let mayLoad = 1;
3164 }
3165 def : InstAlias<"ldrsw $Rt, [$Rn]", (LDRSWx GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3166
3167 let mayLoad = 1 in {
3168   def LDRSWx_Wm_RegOffset : A64I_LSregoff<0b10, 0b0, 0b10, 0b0,
3169                              (outs GPR64:$Rt),
3170                              (ins GPR64xsp:$Rn, GPR32:$Rm, word_Wm_regext:$Ext),
3171                              "ldrsw\t$Rt, [$Rn, $Rm, $Ext]",
3172                              [], NoItinerary>;
3173
3174   def LDRSWx_Xm_RegOffset : A64I_LSregoff<0b10, 0b0, 0b10, 0b1,
3175                              (outs GPR64:$Rt),
3176                              (ins GPR64xsp:$Rn, GPR64:$Rm, word_Xm_regext:$Ext),
3177                              "ldrsw\t$Rt, [$Rn, $Rm, $Ext]",
3178                              [], NoItinerary>;
3179 }
3180 def : InstAlias<"ldrsw $Rt, [$Rn, $Rm]",
3181                 (LDRSWx_Xm_RegOffset GPR64:$Rt, GPR64xsp:$Rn, GPR64:$Rm, 2)>;
3182
3183
3184 def LDURSWx
3185     : A64I_LSunalimm<0b10, 0b0, 0b10,
3186                     (outs GPR64:$Rt),
3187                     (ins GPR64xsp:$Rn, simm9:$SImm9),
3188                     "ldursw\t$Rt, [$Rn, $SImm9]",
3189                     [], NoItinerary> {
3190   let mayLoad = 1;
3191 }
3192 def : InstAlias<"ldursw $Rt, [$Rn]", (LDURSWx GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3193
3194 def LDRSWx_PostInd
3195     : A64I_LSpostind<0b10, 0b0, 0b10,
3196                     (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3197                     (ins GPR64xsp:$Rn, simm9:$SImm9),
3198                     "ldrsw\t$Rt, [$Rn], $SImm9",
3199                     [], NoItinerary> {
3200   let mayLoad = 1;
3201   let Constraints = "$Rn = $Rn_wb";
3202   let DecoderMethod = "DecodeSingleIndexedInstruction";
3203 }
3204
3205 def LDRSWx_PreInd : A64I_LSpreind<0b10, 0b0, 0b10,
3206                                  (outs GPR64:$Rt, GPR64xsp:$Rn_wb),
3207                                  (ins GPR64xsp:$Rn, simm9:$SImm9),
3208                                  "ldrsw\t$Rt, [$Rn, $SImm9]!",
3209                                  [], NoItinerary> {
3210   let mayLoad = 1;
3211   let Constraints = "$Rn = $Rn_wb";
3212   let DecoderMethod = "DecodeSingleIndexedInstruction";
3213 }
3214
3215 //===------------------------------
3216 // 2.4 Prefetch operations
3217 //===------------------------------
3218
3219 def PRFM : A64I_LSunsigimm<0b11, 0b0, 0b10, (outs),
3220                  (ins prefetch_op:$Rt, GPR64xsp:$Rn, dword_uimm12:$UImm12),
3221                  "prfm\t$Rt, [$Rn, $UImm12]",
3222                  [], NoItinerary> {
3223   let mayLoad = 1;
3224 }
3225 def : InstAlias<"prfm $Rt, [$Rn]",
3226                 (PRFM prefetch_op:$Rt, GPR64xsp:$Rn, 0)>;
3227
3228 let mayLoad = 1 in {
3229   def PRFM_Wm_RegOffset : A64I_LSregoff<0b11, 0b0, 0b10, 0b0, (outs),
3230                                         (ins prefetch_op:$Rt, GPR64xsp:$Rn,
3231                                              GPR32:$Rm, dword_Wm_regext:$Ext),
3232                                         "prfm\t$Rt, [$Rn, $Rm, $Ext]",
3233                                         [], NoItinerary>;
3234   def PRFM_Xm_RegOffset : A64I_LSregoff<0b11, 0b0, 0b10, 0b1, (outs),
3235                                         (ins prefetch_op:$Rt, GPR64xsp:$Rn,
3236                                              GPR64:$Rm, dword_Xm_regext:$Ext),
3237                                         "prfm\t$Rt, [$Rn, $Rm, $Ext]",
3238                                         [], NoItinerary>;
3239 }
3240
3241 def : InstAlias<"prfm $Rt, [$Rn, $Rm]",
3242                 (PRFM_Xm_RegOffset prefetch_op:$Rt, GPR64xsp:$Rn,
3243                                    GPR64:$Rm, 2)>;
3244
3245
3246 def PRFUM : A64I_LSunalimm<0b11, 0b0, 0b10, (outs),
3247                          (ins prefetch_op:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3248                          "prfum\t$Rt, [$Rn, $SImm9]",
3249                          [], NoItinerary> {
3250   let mayLoad = 1;
3251 }
3252 def : InstAlias<"prfum $Rt, [$Rn]",
3253                 (PRFUM prefetch_op:$Rt, GPR64xsp:$Rn, 0)>;
3254
3255 //===----------------------------------------------------------------------===//
3256 // Load-store register (unprivileged) instructions
3257 //===----------------------------------------------------------------------===//
3258 // Contains: LDTRB, LDTRH, LDTRSB, LDTRSH, LDTRSW, STTR, STTRB and STTRH
3259
3260 // These instructions very much mirror the "unscaled immediate" loads, but since
3261 // there are no floating-point variants we need to split them out into their own
3262 // section to avoid instantiation of "ldtr d0, [sp]" etc.
3263
3264 multiclass A64I_LDTRSTTR<bits<2> size, string asmsuffix, RegisterClass GPR,
3265                          string prefix> {
3266   def _UnPriv_STR : A64I_LSunpriv<size, 0b0, 0b00,
3267                               (outs), (ins GPR:$Rt, GPR64xsp:$Rn, simm9:$SImm9),
3268                               "sttr" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
3269                               [], NoItinerary> {
3270     let mayStore = 1;
3271   }
3272
3273   def : InstAlias<"sttr" # asmsuffix # " $Rt, [$Rn]",
3274          (!cast<Instruction>(prefix # "_UnPriv_STR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
3275
3276   def _UnPriv_LDR : A64I_LSunpriv<size, 0b0, 0b01,
3277                                (outs GPR:$Rt), (ins GPR64xsp:$Rn, simm9:$SImm9),
3278                                "ldtr" # asmsuffix # "\t$Rt, [$Rn, $SImm9]",
3279                                [], NoItinerary> {
3280     let mayLoad = 1;
3281   }
3282
3283   def : InstAlias<"ldtr" # asmsuffix # " $Rt, [$Rn]",
3284          (!cast<Instruction>(prefix # "_UnPriv_LDR") GPR:$Rt, GPR64xsp:$Rn, 0)>;
3285
3286 }
3287
3288 // STTRB/LDTRB: First define the instructions
3289 defm LS8 : A64I_LDTRSTTR<0b00, "b", GPR32, "LS8">;
3290
3291 // STTRH/LDTRH
3292 defm LS16 : A64I_LDTRSTTR<0b01, "h", GPR32, "LS16">;
3293
3294 // STTR/LDTR to/from a W register
3295 defm LS32 : A64I_LDTRSTTR<0b10, "", GPR32, "LS32">;
3296
3297 // STTR/LDTR to/from an X register
3298 defm LS64 : A64I_LDTRSTTR<0b11, "", GPR64, "LS64">;
3299
3300 // Now a class for the signed instructions that can go to either 32 or 64
3301 // bits...
3302 multiclass A64I_LDTR_signed<bits<2> size, string asmopcode, string prefix> {
3303   let mayLoad = 1 in {
3304     def w : A64I_LSunpriv<size, 0b0, 0b11,
3305                           (outs GPR32:$Rt),
3306                           (ins GPR64xsp:$Rn, simm9:$SImm9),
3307                           "ldtrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3308                           [], NoItinerary>;
3309
3310     def x : A64I_LSunpriv<size, 0b0, 0b10,
3311                           (outs GPR64:$Rt),
3312                           (ins GPR64xsp:$Rn, simm9:$SImm9),
3313                           "ldtrs" # asmopcode # "\t$Rt, [$Rn, $SImm9]",
3314                           [], NoItinerary>;
3315   }
3316
3317   def : InstAlias<"ldtrs" # asmopcode # " $Rt, [$Rn]",
3318                  (!cast<Instruction>(prefix # "w") GPR32:$Rt, GPR64xsp:$Rn, 0)>;
3319
3320   def : InstAlias<"ldtrs" # asmopcode # " $Rt, [$Rn]",
3321                  (!cast<Instruction>(prefix # "x") GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3322
3323 }
3324
3325 // LDTRSB
3326 defm LDTRSB : A64I_LDTR_signed<0b00, "b", "LDTRSB">;
3327 // LDTRSH
3328 defm LDTRSH : A64I_LDTR_signed<0b01, "h", "LDTRSH">;
3329
3330 // And finally LDTRSW which only goes to 64 bits.
3331 def LDTRSWx : A64I_LSunpriv<0b10, 0b0, 0b10,
3332                             (outs GPR64:$Rt),
3333                             (ins GPR64xsp:$Rn, simm9:$SImm9),
3334                             "ldtrsw\t$Rt, [$Rn, $SImm9]",
3335                             [], NoItinerary> {
3336   let mayLoad = 1;
3337 }
3338 def : InstAlias<"ldtrsw $Rt, [$Rn]", (LDTRSWx GPR64:$Rt, GPR64xsp:$Rn, 0)>;
3339
3340 //===----------------------------------------------------------------------===//
3341 // Load-store register pair (offset) instructions
3342 //===----------------------------------------------------------------------===//
3343 //
3344 // and
3345 //
3346 //===----------------------------------------------------------------------===//
3347 // Load-store register pair (post-indexed) instructions
3348 //===----------------------------------------------------------------------===//
3349 // Contains: STP, LDP, LDPSW
3350 //
3351 // and
3352 //
3353 //===----------------------------------------------------------------------===//
3354 // Load-store register pair (pre-indexed) instructions
3355 //===----------------------------------------------------------------------===//
3356 // Contains: STP, LDP, LDPSW
3357 //
3358 // and
3359 //
3360 //===----------------------------------------------------------------------===//
3361 // Load-store non-temporal register pair (offset) instructions
3362 //===----------------------------------------------------------------------===//
3363 // Contains: STNP, LDNP
3364
3365
3366 // Anything that creates an MCInst (Decoding, selection and AsmParsing) has to
3367 // know the access size via some means. An isolated operand does not have this
3368 // information unless told from here, which means we need separate tablegen
3369 // Operands for each access size. This multiclass takes care of instantiating
3370 // the correct template functions in the rest of the backend.
3371
3372 multiclass offsets_simm7<string MemSize, string prefix> {
3373   // The bare signed 7-bit immediate is used in post-indexed instructions, but
3374   // because of the scaling performed a generic "simm7" operand isn't
3375   // appropriate here either.
3376   def simm7_asmoperand : AsmOperandClass {
3377     let Name = "SImm7_Scaled" # MemSize;
3378     let PredicateMethod = "isSImm7Scaled<" # MemSize # ">";
3379     let RenderMethod = "addSImm7ScaledOperands<" # MemSize # ">";
3380   }
3381
3382   def simm7 : Operand<i64> {
3383     let PrintMethod = "printSImm7ScaledOperand<" # MemSize # ">";
3384     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "simm7_asmoperand");
3385   }
3386 }
3387
3388 defm word_  : offsets_simm7<"4", "word_">;
3389 defm dword_ : offsets_simm7<"8", "dword_">;
3390 defm qword_ : offsets_simm7<"16", "qword_">;
3391
3392 multiclass A64I_LSPsimple<bits<2> opc, bit v, RegisterClass SomeReg,
3393                           Operand simm7, string prefix> {
3394   def _STR : A64I_LSPoffset<opc, v, 0b0, (outs),
3395                     (ins SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn, simm7:$SImm7),
3396                     "stp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3397     let mayStore = 1;
3398     let DecoderMethod = "DecodeLDSTPairInstruction";
3399   }
3400   def : InstAlias<"stp $Rt, $Rt2, [$Rn]",
3401                   (!cast<Instruction>(prefix # "_STR") SomeReg:$Rt,
3402                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3403
3404   def _LDR : A64I_LSPoffset<opc, v, 0b1,
3405                             (outs SomeReg:$Rt, SomeReg:$Rt2),
3406                             (ins GPR64xsp:$Rn, simm7:$SImm7),
3407                             "ldp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3408     let mayLoad = 1;
3409     let DecoderMethod = "DecodeLDSTPairInstruction";
3410   }
3411   def : InstAlias<"ldp $Rt, $Rt2, [$Rn]",
3412                   (!cast<Instruction>(prefix # "_LDR") SomeReg:$Rt,
3413                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3414
3415   def _PostInd_STR : A64I_LSPpostind<opc, v, 0b0,
3416                                (outs GPR64xsp:$Rn_wb),
3417                                (ins SomeReg:$Rt, SomeReg:$Rt2,
3418                                     GPR64xsp:$Rn,
3419                                     simm7:$SImm7),
3420                                "stp\t$Rt, $Rt2, [$Rn], $SImm7",
3421                                [], NoItinerary> {
3422     let mayStore = 1;
3423     let Constraints = "$Rn = $Rn_wb";
3424
3425     // Decoder only needed for unpredictability checking (FIXME).
3426     let DecoderMethod = "DecodeLDSTPairInstruction";
3427   }
3428
3429   def _PostInd_LDR : A64I_LSPpostind<opc, v, 0b1,
3430                         (outs SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn_wb),
3431                         (ins GPR64xsp:$Rn, simm7:$SImm7),
3432                         "ldp\t$Rt, $Rt2, [$Rn], $SImm7",
3433                         [], NoItinerary> {
3434     let mayLoad = 1;
3435     let Constraints = "$Rn = $Rn_wb";
3436     let DecoderMethod = "DecodeLDSTPairInstruction";
3437   }
3438
3439   def _PreInd_STR : A64I_LSPpreind<opc, v, 0b0, (outs GPR64xsp:$Rn_wb),
3440                     (ins SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn, simm7:$SImm7),
3441                     "stp\t$Rt, $Rt2, [$Rn, $SImm7]!",
3442                     [], NoItinerary> {
3443     let mayStore = 1;
3444     let Constraints = "$Rn = $Rn_wb";
3445     let DecoderMethod = "DecodeLDSTPairInstruction";
3446   }
3447
3448   def _PreInd_LDR : A64I_LSPpreind<opc, v, 0b1,
3449                               (outs SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn_wb),
3450                               (ins GPR64xsp:$Rn, simm7:$SImm7),
3451                               "ldp\t$Rt, $Rt2, [$Rn, $SImm7]!",
3452                               [], NoItinerary> {
3453     let mayLoad = 1;
3454     let Constraints = "$Rn = $Rn_wb";
3455     let DecoderMethod = "DecodeLDSTPairInstruction";
3456   }
3457
3458   def _NonTemp_STR : A64I_LSPnontemp<opc, v, 0b0, (outs),
3459                     (ins SomeReg:$Rt, SomeReg:$Rt2, GPR64xsp:$Rn, simm7:$SImm7),
3460                     "stnp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3461     let mayStore = 1;
3462     let DecoderMethod = "DecodeLDSTPairInstruction";
3463   }
3464   def : InstAlias<"stnp $Rt, $Rt2, [$Rn]",
3465                   (!cast<Instruction>(prefix # "_NonTemp_STR") SomeReg:$Rt,
3466                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3467
3468   def _NonTemp_LDR : A64I_LSPnontemp<opc, v, 0b1,
3469                             (outs SomeReg:$Rt, SomeReg:$Rt2),
3470                             (ins GPR64xsp:$Rn, simm7:$SImm7),
3471                             "ldnp\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3472     let mayLoad = 1;
3473     let DecoderMethod = "DecodeLDSTPairInstruction";
3474   }
3475   def : InstAlias<"ldnp $Rt, $Rt2, [$Rn]",
3476                   (!cast<Instruction>(prefix # "_NonTemp_LDR") SomeReg:$Rt,
3477                                                 SomeReg:$Rt2, GPR64xsp:$Rn, 0)>;
3478
3479 }
3480
3481
3482 defm LSPair32 : A64I_LSPsimple<0b00, 0b0, GPR32, word_simm7, "LSPair32">;
3483 defm LSPair64 : A64I_LSPsimple<0b10, 0b0, GPR64, dword_simm7, "LSPair64">;
3484 defm LSFPPair32 : A64I_LSPsimple<0b00, 0b1, FPR32, word_simm7, "LSFPPair32">;
3485 defm LSFPPair64 : A64I_LSPsimple<0b01, 0b1, FPR64,  dword_simm7, "LSFPPair64">;
3486 defm LSFPPair128 : A64I_LSPsimple<0b10, 0b1, FPR128, qword_simm7,
3487                                   "LSFPPair128">;
3488
3489
3490 def LDPSWx : A64I_LSPoffset<0b01, 0b0, 0b1,
3491                            (outs GPR64:$Rt, GPR64:$Rt2),
3492                            (ins GPR64xsp:$Rn, word_simm7:$SImm7),
3493                            "ldpsw\t$Rt, $Rt2, [$Rn, $SImm7]", [], NoItinerary> {
3494   let mayLoad = 1;
3495   let DecoderMethod = "DecodeLDSTPairInstruction";
3496 }
3497 def : InstAlias<"ldpsw $Rt, $Rt2, [$Rn]",
3498                 (LDPSWx GPR64:$Rt, GPR64:$Rt2, GPR64xsp:$Rn, 0)>;
3499
3500 def LDPSWx_PostInd : A64I_LSPpostind<0b01, 0b0, 0b1,
3501                                   (outs GPR64:$Rt, GPR64:$Rt2, GPR64:$Rn_wb),
3502                                   (ins GPR64xsp:$Rn, word_simm7:$SImm7),
3503                                   "ldpsw\t$Rt, $Rt2, [$Rn], $SImm7",
3504                                   [], NoItinerary> {
3505   let mayLoad = 1;
3506   let Constraints = "$Rn = $Rn_wb";
3507   let DecoderMethod = "DecodeLDSTPairInstruction";
3508 }
3509
3510 def LDPSWx_PreInd : A64I_LSPpreind<0b01, 0b0, 0b1,
3511                                    (outs GPR64:$Rt, GPR64:$Rt2, GPR64:$Rn_wb),
3512                                    (ins GPR64xsp:$Rn, word_simm7:$SImm7),
3513                                    "ldpsw\t$Rt, $Rt2, [$Rn, $SImm7]!",
3514                                    [], NoItinerary> {
3515   let mayLoad = 1;
3516   let Constraints = "$Rn = $Rn_wb";
3517   let DecoderMethod = "DecodeLDSTPairInstruction";
3518 }
3519
3520 //===----------------------------------------------------------------------===//
3521 // Logical (immediate) instructions
3522 //===----------------------------------------------------------------------===//
3523 // Contains: AND, ORR, EOR, ANDS, + aliases TST, MOV
3524
3525 multiclass logical_imm_operands<string prefix, string note,
3526                                 int size, ValueType VT> {
3527   def _asmoperand : AsmOperandClass {
3528     let Name = "LogicalImm" # note # size;
3529     let PredicateMethod = "isLogicalImm" # note # "<" # size # ">";
3530     let RenderMethod = "addLogicalImmOperands<" # size # ">";
3531   }
3532
3533   def _operand
3534         : Operand<VT>, ComplexPattern<VT, 1, "SelectLogicalImm", [imm]> {
3535     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_asmoperand");
3536     let PrintMethod = "printLogicalImmOperand<" # size # ">";
3537     let DecoderMethod = "DecodeLogicalImmOperand<" # size # ">";
3538   }
3539 }
3540
3541 defm logical_imm32 : logical_imm_operands<"logical_imm32", "", 32, i32>;
3542 defm logical_imm64 : logical_imm_operands<"logical_imm64", "", 64, i64>;
3543
3544 // The mov versions only differ in assembly parsing, where they
3545 // exclude values representable with either MOVZ or MOVN.
3546 defm logical_imm32_mov
3547   : logical_imm_operands<"logical_imm32_mov", "MOV", 32, i32>;
3548 defm logical_imm64_mov
3549   : logical_imm_operands<"logical_imm64_mov", "MOV", 64, i64>;
3550
3551
3552 multiclass A64I_logimmSizes<bits<2> opc, string asmop, SDNode opnode> {
3553   def wwi : A64I_logicalimm<0b0, opc, (outs GPR32wsp:$Rd),
3554                          (ins GPR32:$Rn, logical_imm32_operand:$Imm),
3555                          !strconcat(asmop, "\t$Rd, $Rn, $Imm"),
3556                          [(set GPR32wsp:$Rd,
3557                                (opnode GPR32:$Rn, logical_imm32_operand:$Imm))],
3558                          NoItinerary>;
3559
3560   def xxi : A64I_logicalimm<0b1, opc, (outs GPR64xsp:$Rd),
3561                          (ins GPR64:$Rn, logical_imm64_operand:$Imm),
3562                          !strconcat(asmop, "\t$Rd, $Rn, $Imm"),
3563                          [(set GPR64xsp:$Rd,
3564                                (opnode GPR64:$Rn, logical_imm64_operand:$Imm))],
3565                          NoItinerary>;
3566 }
3567
3568 defm AND : A64I_logimmSizes<0b00, "and", and>;
3569 defm ORR : A64I_logimmSizes<0b01, "orr", or>;
3570 defm EOR : A64I_logimmSizes<0b10, "eor", xor>;
3571
3572 let Defs = [NZCV] in {
3573   def ANDSwwi : A64I_logicalimm<0b0, 0b11, (outs GPR32:$Rd),
3574                                 (ins GPR32:$Rn, logical_imm32_operand:$Imm),
3575                                 "ands\t$Rd, $Rn, $Imm",
3576                                 [], NoItinerary>;
3577
3578   def ANDSxxi : A64I_logicalimm<0b1, 0b11, (outs GPR64:$Rd),
3579                                 (ins GPR64:$Rn, logical_imm64_operand:$Imm),
3580                                 "ands\t$Rd, $Rn, $Imm",
3581                                 [], NoItinerary>;
3582 }
3583
3584
3585 def : InstAlias<"tst $Rn, $Imm",
3586                 (ANDSwwi WZR, GPR32:$Rn, logical_imm32_operand:$Imm)>;
3587 def : InstAlias<"tst $Rn, $Imm",
3588                 (ANDSxxi XZR, GPR64:$Rn, logical_imm64_operand:$Imm)>;
3589 def : InstAlias<"mov $Rd, $Imm",
3590                 (ORRwwi GPR32wsp:$Rd, WZR, logical_imm32_mov_operand:$Imm)>;
3591 def : InstAlias<"mov $Rd, $Imm",
3592                 (ORRxxi GPR64xsp:$Rd, XZR, logical_imm64_mov_operand:$Imm)>;
3593
3594 //===----------------------------------------------------------------------===//
3595 // Logical (shifted register) instructions
3596 //===----------------------------------------------------------------------===//
3597 // Contains: AND, BIC, ORR, ORN, EOR, EON, ANDS, BICS + aliases TST, MVN, MOV
3598
3599 // Operand for optimizing (icmp (and LHS, RHS), 0, SomeCode). In theory "ANDS"
3600 // behaves differently for unsigned comparisons, so we defensively only allow
3601 // signed or n/a as the operand. In practice "unsigned greater than 0" is "not
3602 // equal to 0" and LLVM gives us this.
3603 def signed_cond : PatLeaf<(cond), [{
3604   return !isUnsignedIntSetCC(N->get());
3605 }]>;
3606
3607
3608 // These instructions share their "shift" operands with add/sub (shifted
3609 // register instructions). They are defined there.
3610
3611 // N.b. the commutable parameter is just !N. It will be first against the wall
3612 // when the revolution comes.
3613 multiclass logical_shifts<string prefix, bit sf, bits<2> opc,
3614                           bit N, bit commutable,
3615                           string asmop, SDPatternOperator opfrag, string sty,
3616                           RegisterClass GPR, list<Register> defs> {
3617   let isCommutable = commutable, Defs = defs in {
3618   def _lsl : A64I_logicalshift<sf, opc, 0b00, N,
3619                        (outs GPR:$Rd),
3620                        (ins GPR:$Rn, GPR:$Rm,
3621                             !cast<Operand>("lsl_operand_" # sty):$Imm6),
3622                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3623                        [(set GPR:$Rd, (opfrag GPR:$Rn, (shl GPR:$Rm,
3624                             !cast<Operand>("lsl_operand_" # sty):$Imm6))
3625                        )],
3626                        NoItinerary>;
3627
3628   def _lsr : A64I_logicalshift<sf, opc, 0b01, N,
3629                        (outs GPR:$Rd),
3630                        (ins GPR:$Rn, GPR:$Rm,
3631                             !cast<Operand>("lsr_operand_" # sty):$Imm6),
3632                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3633                        [(set GPR:$Rd, (opfrag GPR:$Rn, (srl GPR:$Rm,
3634                             !cast<Operand>("lsr_operand_" # sty):$Imm6))
3635                        )],
3636                        NoItinerary>;
3637
3638   def _asr : A64I_logicalshift<sf, opc, 0b10, N,
3639                        (outs GPR:$Rd),
3640                        (ins GPR:$Rn, GPR:$Rm,
3641                             !cast<Operand>("asr_operand_" # sty):$Imm6),
3642                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3643                        [(set GPR:$Rd, (opfrag GPR:$Rn, (sra GPR:$Rm,
3644                             !cast<Operand>("asr_operand_" # sty):$Imm6))
3645                        )],
3646                        NoItinerary>;
3647
3648   def _ror : A64I_logicalshift<sf, opc, 0b11, N,
3649                        (outs GPR:$Rd),
3650                        (ins GPR:$Rn, GPR:$Rm,
3651                             !cast<Operand>("ror_operand_" # sty):$Imm6),
3652                        !strconcat(asmop, "\t$Rd, $Rn, $Rm, $Imm6"),
3653                        [(set GPR:$Rd, (opfrag GPR:$Rn, (rotr GPR:$Rm,
3654                             !cast<Operand>("ror_operand_" # sty):$Imm6))
3655                        )],
3656                        NoItinerary>;
3657   }
3658
3659   def _noshift
3660       : InstAlias<!strconcat(asmop, " $Rd, $Rn, $Rm"),
3661                  (!cast<Instruction>(prefix # "_lsl") GPR:$Rd, GPR:$Rn,
3662                                                       GPR:$Rm, 0)>;
3663
3664   def : Pat<(opfrag GPR:$Rn, GPR:$Rm),
3665             (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
3666 }
3667
3668 multiclass logical_sizes<string prefix, bits<2> opc, bit N, bit commutable,
3669                          string asmop, SDPatternOperator opfrag,
3670                          list<Register> defs> {
3671   defm xxx : logical_shifts<prefix # "xxx", 0b1, opc, N,
3672                             commutable, asmop, opfrag, "i64", GPR64, defs>;
3673   defm www : logical_shifts<prefix # "www", 0b0, opc, N,
3674                             commutable, asmop, opfrag, "i32", GPR32, defs>;
3675 }
3676
3677
3678 defm AND : logical_sizes<"AND", 0b00, 0b0, 0b1, "and", and, []>;
3679 defm ORR : logical_sizes<"ORR", 0b01, 0b0, 0b1, "orr", or, []>;
3680 defm EOR : logical_sizes<"EOR", 0b10, 0b0, 0b1, "eor", xor, []>;
3681 defm ANDS : logical_sizes<"ANDS", 0b11, 0b0, 0b1, "ands",
3682              PatFrag<(ops node:$lhs, node:$rhs), (and node:$lhs, node:$rhs),
3683                      [{ (void)N; return false; }]>,
3684              [NZCV]>;
3685
3686 defm BIC : logical_sizes<"BIC", 0b00, 0b1, 0b0, "bic",
3687                          PatFrag<(ops node:$lhs, node:$rhs),
3688                                  (and node:$lhs, (not node:$rhs))>, []>;
3689 defm ORN : logical_sizes<"ORN", 0b01, 0b1, 0b0, "orn",
3690                          PatFrag<(ops node:$lhs, node:$rhs),
3691                                  (or node:$lhs, (not node:$rhs))>, []>;
3692 defm EON : logical_sizes<"EON", 0b10, 0b1, 0b0, "eon",
3693                          PatFrag<(ops node:$lhs, node:$rhs),
3694                                  (xor node:$lhs, (not node:$rhs))>, []>;
3695 defm BICS : logical_sizes<"BICS", 0b11, 0b1, 0b0, "bics",
3696                           PatFrag<(ops node:$lhs, node:$rhs),
3697                                   (and node:$lhs, (not node:$rhs)),
3698                                   [{ (void)N; return false; }]>,
3699                           [NZCV]>;
3700
3701 multiclass tst_shifts<string prefix, bit sf, string sty, RegisterClass GPR> {
3702   let isCommutable = 1, Rd = 0b11111, Defs = [NZCV] in {
3703   def _lsl : A64I_logicalshift<sf, 0b11, 0b00, 0b0,
3704                        (outs),
3705                        (ins GPR:$Rn, GPR:$Rm,
3706                             !cast<Operand>("lsl_operand_" # sty):$Imm6),
3707                        "tst\t$Rn, $Rm, $Imm6",
3708                        [(set NZCV, (A64setcc (and GPR:$Rn, (shl GPR:$Rm,
3709                            !cast<Operand>("lsl_operand_" # sty):$Imm6)),
3710                                           0, signed_cond))],
3711                        NoItinerary>;
3712
3713
3714   def _lsr : A64I_logicalshift<sf, 0b11, 0b01, 0b0,
3715                        (outs),
3716                        (ins GPR:$Rn, GPR:$Rm,
3717                             !cast<Operand>("lsr_operand_" # sty):$Imm6),
3718                        "tst\t$Rn, $Rm, $Imm6",
3719                        [(set NZCV, (A64setcc (and GPR:$Rn, (srl GPR:$Rm,
3720                            !cast<Operand>("lsr_operand_" # sty):$Imm6)),
3721                                           0, signed_cond))],
3722                        NoItinerary>;
3723
3724   def _asr : A64I_logicalshift<sf, 0b11, 0b10, 0b0,
3725                        (outs),
3726                        (ins GPR:$Rn, GPR:$Rm,
3727                             !cast<Operand>("asr_operand_" # sty):$Imm6),
3728                        "tst\t$Rn, $Rm, $Imm6",
3729                        [(set NZCV, (A64setcc (and GPR:$Rn, (sra GPR:$Rm,
3730                            !cast<Operand>("asr_operand_" # sty):$Imm6)),
3731                                           0, signed_cond))],
3732                        NoItinerary>;
3733
3734   def _ror : A64I_logicalshift<sf, 0b11, 0b11, 0b0,
3735                        (outs),
3736                        (ins GPR:$Rn, GPR:$Rm,
3737                             !cast<Operand>("ror_operand_" # sty):$Imm6),
3738                        "tst\t$Rn, $Rm, $Imm6",
3739                        [(set NZCV, (A64setcc (and GPR:$Rn, (rotr GPR:$Rm,
3740                            !cast<Operand>("ror_operand_" # sty):$Imm6)),
3741                                           0, signed_cond))],
3742                        NoItinerary>;
3743   }
3744
3745   def _noshift : InstAlias<"tst $Rn, $Rm",
3746                      (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
3747
3748   def : Pat<(A64setcc (and GPR:$Rn, GPR:$Rm), 0, signed_cond),
3749             (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
3750 }
3751
3752 defm TSTxx : tst_shifts<"TSTxx", 0b1, "i64", GPR64>;
3753 defm TSTww : tst_shifts<"TSTww", 0b0, "i32", GPR32>;
3754
3755
3756 multiclass mvn_shifts<string prefix, bit sf, string sty, RegisterClass GPR> {
3757   let isCommutable = 0, Rn = 0b11111 in {
3758   def _lsl : A64I_logicalshift<sf, 0b01, 0b00, 0b1,
3759                        (outs GPR:$Rd),
3760                        (ins GPR:$Rm,
3761                             !cast<Operand>("lsl_operand_" # sty):$Imm6),
3762                        "mvn\t$Rd, $Rm, $Imm6",
3763                        [(set GPR:$Rd, (not (shl GPR:$Rm,
3764                          !cast<Operand>("lsl_operand_" # sty):$Imm6)))],
3765                        NoItinerary>;
3766
3767
3768   def _lsr : A64I_logicalshift<sf, 0b01, 0b01, 0b1,
3769                        (outs GPR:$Rd),
3770                        (ins GPR:$Rm,
3771                             !cast<Operand>("lsr_operand_" # sty):$Imm6),
3772                        "mvn\t$Rd, $Rm, $Imm6",
3773                        [(set GPR:$Rd, (not (srl GPR:$Rm,
3774                          !cast<Operand>("lsr_operand_" # sty):$Imm6)))],
3775                        NoItinerary>;
3776
3777   def _asr : A64I_logicalshift<sf, 0b01, 0b10, 0b1,
3778                        (outs GPR:$Rd),
3779                        (ins GPR:$Rm,
3780                             !cast<Operand>("asr_operand_" # sty):$Imm6),
3781                        "mvn\t$Rd, $Rm, $Imm6",
3782                        [(set GPR:$Rd, (not (sra GPR:$Rm,
3783                          !cast<Operand>("asr_operand_" # sty):$Imm6)))],
3784                        NoItinerary>;
3785
3786   def _ror : A64I_logicalshift<sf, 0b01, 0b11, 0b1,
3787                        (outs GPR:$Rd),
3788                        (ins GPR:$Rm,
3789                             !cast<Operand>("ror_operand_" # sty):$Imm6),
3790                        "mvn\t$Rd, $Rm, $Imm6",
3791                        [(set GPR:$Rd, (not (rotr GPR:$Rm,
3792                          !cast<Operand>("lsl_operand_" # sty):$Imm6)))],
3793                        NoItinerary>;
3794   }
3795
3796   def _noshift : InstAlias<"mvn $Rn, $Rm",
3797                      (!cast<Instruction>(prefix # "_lsl") GPR:$Rn, GPR:$Rm, 0)>;
3798
3799   def : Pat<(not GPR:$Rm),
3800             (!cast<Instruction>(prefix # "_lsl") GPR:$Rm, 0)>;
3801 }
3802
3803 defm MVNxx : mvn_shifts<"MVNxx", 0b1, "i64", GPR64>;
3804 defm MVNww : mvn_shifts<"MVNww", 0b0, "i32", GPR32>;
3805
3806 def MOVxx :InstAlias<"mov $Rd, $Rm", (ORRxxx_lsl GPR64:$Rd, XZR, GPR64:$Rm, 0)>;
3807 def MOVww :InstAlias<"mov $Rd, $Rm", (ORRwww_lsl GPR32:$Rd, WZR, GPR32:$Rm, 0)>;
3808
3809 //===----------------------------------------------------------------------===//
3810 // Move wide (immediate) instructions
3811 //===----------------------------------------------------------------------===//
3812 // Contains: MOVN, MOVZ, MOVK + MOV aliases
3813
3814 // A wide variety of different relocations are needed for variants of these
3815 // instructions, so it turns out that we need a different operand for all of
3816 // them.
3817 multiclass movw_operands<string prefix, string instname, int width> {
3818   def _imm_asmoperand : AsmOperandClass {
3819     let Name = instname # width # "Shifted" # shift;
3820     let PredicateMethod = "is" # instname # width # "Imm";
3821     let RenderMethod = "addMoveWideImmOperands";
3822
3823     let ParserMethod = "ParseImmWithLSLOperand";
3824   }
3825
3826   def _imm : Operand<i32> {
3827     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_imm_asmoperand");
3828     let PrintMethod = "printMoveWideImmOperand";
3829     let EncoderMethod = "getMoveWideImmOpValue";
3830     let DecoderMethod = "DecodeMoveWideImmOperand<" # width # ">";
3831
3832     let MIOperandInfo = (ops uimm16:$UImm16, imm:$Shift);
3833   }
3834 }
3835
3836 defm movn32 : movw_operands<"movn32", "MOVN", 32>;
3837 defm movn64 : movw_operands<"movn64", "MOVN", 64>;
3838 defm movz32 : movw_operands<"movz32", "MOVZ", 32>;
3839 defm movz64 : movw_operands<"movz64", "MOVZ", 64>;
3840 defm movk32 : movw_operands<"movk32", "MOVK", 32>;
3841 defm movk64 : movw_operands<"movk64", "MOVK", 64>;
3842
3843 multiclass A64I_movwSizes<bits<2> opc, string asmop, dag ins32bit,
3844                           dag ins64bit> {
3845
3846   def wii : A64I_movw<0b0, opc, (outs GPR32:$Rd), ins32bit,
3847                       !strconcat(asmop, "\t$Rd, $FullImm"),
3848                       [], NoItinerary> {
3849     bits<18> FullImm;
3850     let UImm16 = FullImm{15-0};
3851     let Shift = FullImm{17-16};
3852   }
3853
3854   def xii : A64I_movw<0b1, opc, (outs GPR64:$Rd), ins64bit,
3855                       !strconcat(asmop, "\t$Rd, $FullImm"),
3856                       [], NoItinerary> {
3857     bits<18> FullImm;
3858     let UImm16 = FullImm{15-0};
3859     let Shift = FullImm{17-16};
3860   }
3861 }
3862
3863 let isMoveImm = 1, isReMaterializable = 1,
3864     isAsCheapAsAMove = 1, neverHasSideEffects = 1 in {
3865   defm MOVN : A64I_movwSizes<0b00, "movn",
3866                              (ins movn32_imm:$FullImm),
3867                              (ins movn64_imm:$FullImm)>;
3868
3869   // Some relocations are able to convert between a MOVZ and a MOVN. If these
3870   // are applied the instruction must be emitted with the corresponding bits as
3871   // 0, which means a MOVZ needs to override that bit from the default.
3872   let PostEncoderMethod = "fixMOVZ" in
3873   defm MOVZ : A64I_movwSizes<0b10, "movz",
3874                              (ins movz32_imm:$FullImm),
3875                              (ins movz64_imm:$FullImm)>;
3876 }
3877
3878 let Constraints = "$src = $Rd" in
3879 defm MOVK : A64I_movwSizes<0b11, "movk",
3880                            (ins GPR32:$src, movk32_imm:$FullImm),
3881                            (ins GPR64:$src, movk64_imm:$FullImm)>;
3882
3883
3884 // And now the "MOV" aliases. These also need their own operands because what
3885 // they accept is completely different to what the base instructions accept.
3886 multiclass movalias_operand<string prefix, string basename,
3887                             string immpredicate, int width> {
3888   def _asmoperand : AsmOperandClass {
3889     let Name = basename # width # "MovAlias";
3890     let PredicateMethod
3891           = "isMoveWideMovAlias<" # width # ", A64Imms::" # immpredicate # ">";
3892     let RenderMethod
3893       = "addMoveWideMovAliasOperands<" # width # ", "
3894                                        # "A64Imms::" # immpredicate # ">";
3895   }
3896
3897   def _movimm : Operand<i32> {
3898     let ParserMatchClass = !cast<AsmOperandClass>(prefix # "_asmoperand");
3899
3900     let MIOperandInfo = (ops uimm16:$UImm16, imm:$Shift);
3901   }
3902 }
3903
3904 defm movz32 : movalias_operand<"movz32", "MOVZ", "isMOVZImm", 32>;
3905 defm movz64 : movalias_operand<"movz64", "MOVZ", "isMOVZImm", 64>;
3906 defm movn32 : movalias_operand<"movn32", "MOVN", "isOnlyMOVNImm", 32>;
3907 defm movn64 : movalias_operand<"movn64", "MOVN", "isOnlyMOVNImm", 64>;
3908
3909 // FIXME: these are officially canonical aliases, but TableGen is too limited to
3910 // print them at the moment. I believe in this case an "AliasPredicate" method
3911 // will need to be implemented. to allow it, as well as the more generally
3912 // useful handling of non-register, non-constant operands.
3913 class movalias<Instruction INST, RegisterClass GPR, Operand operand>
3914   : InstAlias<"mov $Rd, $FullImm", (INST GPR:$Rd, operand:$FullImm)>;
3915
3916 def : movalias<MOVZwii, GPR32, movz32_movimm>;
3917 def : movalias<MOVZxii, GPR64, movz64_movimm>;
3918 def : movalias<MOVNwii, GPR32, movn32_movimm>;
3919 def : movalias<MOVNxii, GPR64, movn64_movimm>;
3920
3921 //===----------------------------------------------------------------------===//
3922 // PC-relative addressing instructions
3923 //===----------------------------------------------------------------------===//
3924 // Contains: ADR, ADRP
3925
3926 def adr_label : Operand<i64> {
3927   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_adr_prel>";
3928
3929   // This label is a 21-bit offset from PC, unscaled
3930   let PrintMethod = "printLabelOperand<21, 1>";
3931   let ParserMatchClass = label_asmoperand<21, 1>;
3932   let OperandType = "OPERAND_PCREL";
3933 }
3934
3935 def adrp_label_asmoperand : AsmOperandClass {
3936   let Name = "AdrpLabel";
3937   let RenderMethod = "addLabelOperands<21, 4096>";
3938 }
3939
3940 def adrp_label : Operand<i64> {
3941   let EncoderMethod = "getAdrpLabelOpValue";
3942
3943   // This label is a 21-bit offset from PC, scaled by the page-size: 4096.
3944   let PrintMethod = "printLabelOperand<21, 4096>";
3945   let ParserMatchClass = adrp_label_asmoperand;
3946   let OperandType = "OPERAND_PCREL";
3947 }
3948
3949 let neverHasSideEffects = 1 in {
3950   def ADRxi : A64I_PCADR<0b0, (outs GPR64:$Rd), (ins adr_label:$Label),
3951                          "adr\t$Rd, $Label", [], NoItinerary>;
3952
3953   def ADRPxi : A64I_PCADR<0b1, (outs GPR64:$Rd), (ins adrp_label:$Label),
3954                           "adrp\t$Rd, $Label", [], NoItinerary>;
3955 }
3956
3957 //===----------------------------------------------------------------------===//
3958 // System instructions
3959 //===----------------------------------------------------------------------===//
3960 // Contains: HINT, CLREX, DSB, DMB, ISB, MSR, SYS, SYSL, MRS
3961 //    + aliases IC, DC, AT, TLBI, NOP, YIELD, WFE, WFI, SEV, SEVL
3962
3963 // Op1 and Op2 fields are sometimes simple 3-bit unsigned immediate values.
3964 def uimm3_asmoperand : AsmOperandClass {
3965   let Name = "UImm3";
3966   let PredicateMethod = "isUImm<3>";
3967   let RenderMethod = "addImmOperands";
3968 }
3969
3970 def uimm3 : Operand<i32> {
3971   let ParserMatchClass = uimm3_asmoperand;
3972 }
3973
3974 // The HINT alias can accept a simple unsigned 7-bit immediate.
3975 def uimm7_asmoperand : AsmOperandClass {
3976   let Name = "UImm7";
3977   let PredicateMethod = "isUImm<7>";
3978   let RenderMethod = "addImmOperands";
3979 }
3980
3981 def uimm7 : Operand<i32> {
3982   let ParserMatchClass = uimm7_asmoperand;
3983 }
3984
3985 // Multiclass namedimm is defined with the prefetch operands. Most of these fit
3986 // into the NamedImmMapper scheme well: they either accept a named operand or
3987 // any immediate under a particular value (which may be 0, implying no immediate
3988 // is allowed).
3989 defm dbarrier : namedimm<"dbarrier", "A64DB::DBarrierMapper">;
3990 defm isb : namedimm<"isb", "A64ISB::ISBMapper">;
3991 defm ic : namedimm<"ic", "A64IC::ICMapper">;
3992 defm dc : namedimm<"dc", "A64DC::DCMapper">;
3993 defm at : namedimm<"at", "A64AT::ATMapper">;
3994 defm tlbi : namedimm<"tlbi", "A64TLBI::TLBIMapper">;
3995
3996 // However, MRS and MSR are more complicated for a few reasons:
3997 //   * There are ~1000 generic names S3_<op1>_<CRn>_<CRm>_<Op2> which have an
3998 //     implementation-defined effect
3999 //   * Most registers are shared, but some are read-only or write-only.
4000 //   * There is a variant of MSR which accepts the same register name (SPSel),
4001 //     but which would have a different encoding.
4002
4003 // In principle these could be resolved in with more complicated subclasses of
4004 // NamedImmMapper, however that imposes an overhead on other "named
4005 // immediates". Both in concrete terms with virtual tables and in unnecessary
4006 // abstraction.
4007
4008 // The solution adopted here is to take the MRS/MSR Mappers out of the usual
4009 // hierarchy (they're not derived from NamedImmMapper) and to add logic for
4010 // their special situation.
4011 def mrs_asmoperand : AsmOperandClass {
4012   let Name = "MRS";
4013   let ParserMethod = "ParseSysRegOperand";
4014 }
4015
4016 def mrs_op : Operand<i32> {
4017   let ParserMatchClass = mrs_asmoperand;
4018   let PrintMethod = "printMRSOperand";
4019   let DecoderMethod = "DecodeMRSOperand";
4020 }
4021
4022 def msr_asmoperand : AsmOperandClass {
4023   let Name = "MSRWithReg";
4024
4025   // Note that SPSel is valid for both this and the pstate operands, but with
4026   // different immediate encodings. This is why these operands provide a string
4027   // AArch64Operand rather than an immediate. The overlap is small enough that
4028   // it could be resolved with hackery now, but who can say in future?
4029   let ParserMethod = "ParseSysRegOperand";
4030 }
4031
4032 def msr_op : Operand<i32> {
4033   let ParserMatchClass = msr_asmoperand;
4034   let PrintMethod = "printMSROperand";
4035   let DecoderMethod = "DecodeMSROperand";
4036 }
4037
4038 def pstate_asmoperand : AsmOperandClass {
4039   let Name = "MSRPState";
4040   // See comment above about parser.
4041   let ParserMethod = "ParseSysRegOperand";
4042 }
4043
4044 def pstate_op : Operand<i32> {
4045   let ParserMatchClass = pstate_asmoperand;
4046   let PrintMethod = "printNamedImmOperand<A64PState::PStateMapper>";
4047   let DecoderMethod = "DecodeNamedImmOperand<A64PState::PStateMapper>";
4048 }
4049
4050 // When <CRn> is specified, an assembler should accept something like "C4", not
4051 // the usual "#4" immediate.
4052 def CRx_asmoperand : AsmOperandClass {
4053   let Name = "CRx";
4054   let PredicateMethod = "isUImm<4>";
4055   let RenderMethod = "addImmOperands";
4056   let ParserMethod = "ParseCRxOperand";
4057 }
4058
4059 def CRx : Operand<i32> {
4060   let ParserMatchClass = CRx_asmoperand;
4061   let PrintMethod = "printCRxOperand";
4062 }
4063
4064
4065 // Finally, we can start defining the instructions.
4066
4067 // HINT is straightforward, with a few aliases.
4068 def HINTi : A64I_system<0b0, (outs), (ins uimm7:$UImm7), "hint\t$UImm7",
4069                         [], NoItinerary> {
4070   bits<7> UImm7;
4071   let CRm = UImm7{6-3};
4072   let Op2 = UImm7{2-0};
4073
4074   let Op0 = 0b00;
4075   let Op1 = 0b011;
4076   let CRn = 0b0010;
4077   let Rt = 0b11111;
4078 }
4079
4080 def : InstAlias<"nop", (HINTi 0)>;
4081 def : InstAlias<"yield", (HINTi 1)>;
4082 def : InstAlias<"wfe", (HINTi 2)>;
4083 def : InstAlias<"wfi", (HINTi 3)>;
4084 def : InstAlias<"sev", (HINTi 4)>;
4085 def : InstAlias<"sevl", (HINTi 5)>;
4086
4087 // Quite a few instructions then follow a similar pattern of fixing common
4088 // fields in the bitpattern, we'll define a helper-class for them.
4089 class simple_sys<bits<2> op0, bits<3> op1, bits<4> crn, bits<3> op2,
4090                  Operand operand, string asmop>
4091   : A64I_system<0b0, (outs), (ins operand:$CRm), !strconcat(asmop, "\t$CRm"),
4092                 [], NoItinerary> {
4093   let Op0 = op0;
4094   let Op1 = op1;
4095   let CRn = crn;
4096   let Op2 = op2;
4097   let Rt = 0b11111;
4098 }
4099
4100
4101 def CLREXi : simple_sys<0b00, 0b011, 0b0011, 0b010, uimm4, "clrex">;
4102 def DSBi : simple_sys<0b00, 0b011, 0b0011, 0b100, dbarrier_op, "dsb">;
4103 def DMBi : simple_sys<0b00, 0b011, 0b0011, 0b101, dbarrier_op, "dmb">;
4104 def ISBi : simple_sys<0b00, 0b011, 0b0011, 0b110, isb_op, "isb">;
4105
4106 def : InstAlias<"clrex", (CLREXi 0b1111)>;
4107 def : InstAlias<"isb", (ISBi 0b1111)>;
4108
4109 // (DMBi 0xb) is a "DMB ISH" instruciton, appropriate for Linux SMP
4110 // configurations at least.
4111 def : Pat<(atomic_fence imm, imm), (DMBi 0xb)>;
4112
4113 // Any SYS bitpattern can be represented with a complex and opaque "SYS"
4114 // instruction.
4115 def SYSiccix : A64I_system<0b0, (outs),
4116                            (ins uimm3:$Op1, CRx:$CRn, CRx:$CRm,
4117                                 uimm3:$Op2, GPR64:$Rt),
4118                            "sys\t$Op1, $CRn, $CRm, $Op2, $Rt",
4119                            [], NoItinerary> {
4120   let Op0 = 0b01;
4121 }
4122
4123 // You can skip the Xt argument whether it makes sense or not for the generic
4124 // SYS instruction.
4125 def : InstAlias<"sys $Op1, $CRn, $CRm, $Op2",
4126                 (SYSiccix uimm3:$Op1, CRx:$CRn, CRx:$CRm, uimm3:$Op2, XZR)>;
4127
4128
4129 // But many have aliases, which obviously don't fit into
4130 class SYSalias<dag ins, string asmstring>
4131   : A64I_system<0b0, (outs), ins, asmstring, [], NoItinerary> {
4132   let isAsmParserOnly = 1;
4133
4134   bits<14> SysOp;
4135   let Op0 = 0b01;
4136   let Op1 = SysOp{13-11};
4137   let CRn = SysOp{10-7};
4138   let CRm = SysOp{6-3};
4139   let Op2 = SysOp{2-0};
4140 }
4141
4142 def ICix : SYSalias<(ins ic_op:$SysOp, GPR64:$Rt), "ic\t$SysOp, $Rt">;
4143
4144 def ICi : SYSalias<(ins ic_op:$SysOp), "ic\t$SysOp"> {
4145   let Rt = 0b11111;
4146 }
4147
4148 def DCix : SYSalias<(ins dc_op:$SysOp, GPR64:$Rt), "dc\t$SysOp, $Rt">;
4149 def ATix : SYSalias<(ins at_op:$SysOp, GPR64:$Rt), "at\t$SysOp, $Rt">;
4150
4151 def TLBIix : SYSalias<(ins tlbi_op:$SysOp, GPR64:$Rt), "tlbi\t$SysOp, $Rt">;
4152
4153 def TLBIi : SYSalias<(ins tlbi_op:$SysOp), "tlbi\t$SysOp"> {
4154   let Rt = 0b11111;
4155 }
4156
4157
4158 def SYSLxicci : A64I_system<0b1, (outs GPR64:$Rt),
4159                             (ins uimm3:$Op1, CRx:$CRn, CRx:$CRm, uimm3:$Op2),
4160                             "sysl\t$Rt, $Op1, $CRn, $CRm, $Op2",
4161                             [], NoItinerary> {
4162   let Op0 = 0b01;
4163 }
4164
4165 // The instructions themselves are rather simple for MSR and MRS.
4166 def MSRix : A64I_system<0b0, (outs), (ins msr_op:$SysReg, GPR64:$Rt),
4167                         "msr\t$SysReg, $Rt", [], NoItinerary> {
4168   bits<16> SysReg;
4169   let Op0 = SysReg{15-14};
4170   let Op1 = SysReg{13-11};
4171   let CRn = SysReg{10-7};
4172   let CRm = SysReg{6-3};
4173   let Op2 = SysReg{2-0};
4174 }
4175
4176 def MRSxi : A64I_system<0b1, (outs GPR64:$Rt), (ins mrs_op:$SysReg),
4177                         "mrs\t$Rt, $SysReg", [], NoItinerary> {
4178   bits<16> SysReg;
4179   let Op0 = SysReg{15-14};
4180   let Op1 = SysReg{13-11};
4181   let CRn = SysReg{10-7};
4182   let CRm = SysReg{6-3};
4183   let Op2 = SysReg{2-0};
4184 }
4185
4186 def MSRii : A64I_system<0b0, (outs), (ins pstate_op:$PState, uimm4:$CRm),
4187                         "msr\t$PState, $CRm", [], NoItinerary> {
4188   bits<6> PState;
4189
4190   let Op0 = 0b00;
4191   let Op1 = PState{5-3};
4192   let CRn = 0b0100;
4193   let Op2 = PState{2-0};
4194   let Rt = 0b11111;
4195 }
4196
4197 //===----------------------------------------------------------------------===//
4198 // Test & branch (immediate) instructions
4199 //===----------------------------------------------------------------------===//
4200 // Contains: TBZ, TBNZ
4201
4202 // The bit to test is a simple unsigned 6-bit immediate in the X-register
4203 // versions.
4204 def uimm6 : Operand<i64> {
4205   let ParserMatchClass = uimm6_asmoperand;
4206 }
4207
4208 def label_wid14_scal4_asmoperand : label_asmoperand<14, 4>;
4209
4210 def tbimm_target : Operand<OtherVT> {
4211   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_tstbr>";
4212
4213   // This label is a 14-bit offset from PC, scaled by the instruction-width: 4.
4214   let PrintMethod = "printLabelOperand<14, 4>";
4215   let ParserMatchClass = label_wid14_scal4_asmoperand;
4216
4217   let OperandType = "OPERAND_PCREL";
4218 }
4219
4220 def A64eq : ImmLeaf<i32, [{ return Imm == A64CC::EQ; }]>;
4221 def A64ne : ImmLeaf<i32, [{ return Imm == A64CC::NE; }]>;
4222
4223 // These instructions correspond to patterns involving "and" with a power of
4224 // two, which we need to be able to select.
4225 def tstb64_pat : ComplexPattern<i64, 1, "SelectTSTBOperand<64>">;
4226 def tstb32_pat : ComplexPattern<i32, 1, "SelectTSTBOperand<32>">;
4227
4228 let isBranch = 1, isTerminator = 1 in {
4229   def TBZxii : A64I_TBimm<0b0, (outs),
4230                         (ins GPR64:$Rt, uimm6:$Imm, tbimm_target:$Label),
4231                         "tbz\t$Rt, $Imm, $Label",
4232                         [(A64br_cc (A64cmp (and GPR64:$Rt, tstb64_pat:$Imm), 0),
4233                                    A64eq, bb:$Label)],
4234                         NoItinerary>;
4235
4236   def TBNZxii : A64I_TBimm<0b1, (outs),
4237                         (ins GPR64:$Rt, uimm6:$Imm, tbimm_target:$Label),
4238                         "tbnz\t$Rt, $Imm, $Label",
4239                         [(A64br_cc (A64cmp (and GPR64:$Rt, tstb64_pat:$Imm), 0),
4240                                    A64ne, bb:$Label)],
4241                         NoItinerary>;
4242
4243
4244   // Note, these instructions overlap with the above 64-bit patterns. This is
4245   // intentional, "tbz x3, #1, somewhere" and "tbz w3, #1, somewhere" would both
4246   // do the same thing and are both permitted assembly. They also both have
4247   // sensible DAG patterns.
4248   def TBZwii : A64I_TBimm<0b0, (outs),
4249                         (ins GPR32:$Rt, uimm5:$Imm, tbimm_target:$Label),
4250                         "tbz\t$Rt, $Imm, $Label",
4251                         [(A64br_cc (A64cmp (and GPR32:$Rt, tstb32_pat:$Imm), 0),
4252                                    A64eq, bb:$Label)],
4253                         NoItinerary> {
4254     let Imm{5} = 0b0;
4255   }
4256
4257   def TBNZwii : A64I_TBimm<0b1, (outs),
4258                         (ins GPR32:$Rt, uimm5:$Imm, tbimm_target:$Label),
4259                         "tbnz\t$Rt, $Imm, $Label",
4260                         [(A64br_cc (A64cmp (and GPR32:$Rt, tstb32_pat:$Imm), 0),
4261                                    A64ne, bb:$Label)],
4262                         NoItinerary> {
4263     let Imm{5} = 0b0;
4264   }
4265 }
4266
4267 //===----------------------------------------------------------------------===//
4268 // Unconditional branch (immediate) instructions
4269 //===----------------------------------------------------------------------===//
4270 // Contains: B, BL
4271
4272 def label_wid26_scal4_asmoperand : label_asmoperand<26, 4>;
4273
4274 def bimm_target : Operand<OtherVT> {
4275   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_uncondbr>";
4276
4277   // This label is a 26-bit offset from PC, scaled by the instruction-width: 4.
4278   let PrintMethod = "printLabelOperand<26, 4>";
4279   let ParserMatchClass = label_wid26_scal4_asmoperand;
4280
4281   let OperandType = "OPERAND_PCREL";
4282 }
4283
4284 def blimm_target : Operand<i64> {
4285   let EncoderMethod = "getLabelOpValue<AArch64::fixup_a64_call>";
4286
4287   // This label is a 26-bit offset from PC, scaled by the instruction-width: 4.
4288   let PrintMethod = "printLabelOperand<26, 4>";
4289   let ParserMatchClass = label_wid26_scal4_asmoperand;
4290
4291   let OperandType = "OPERAND_PCREL";
4292 }
4293
4294 class A64I_BimmImpl<bit op, string asmop, list<dag> patterns, Operand lbl_type>
4295   : A64I_Bimm<op, (outs), (ins lbl_type:$Label),
4296               !strconcat(asmop, "\t$Label"), patterns,
4297               NoItinerary>;
4298
4299 let isBranch = 1 in {
4300   def Bimm : A64I_BimmImpl<0b0, "b", [(br bb:$Label)], bimm_target> {
4301     let isTerminator = 1;
4302     let isBarrier = 1;
4303   }
4304
4305   def BLimm : A64I_BimmImpl<0b1, "bl",
4306                             [(AArch64Call tglobaladdr:$Label)], blimm_target> {
4307     let isCall = 1;
4308     let Defs = [X30];
4309   }
4310 }
4311
4312 def : Pat<(AArch64Call texternalsym:$Label), (BLimm texternalsym:$Label)>;
4313
4314 //===----------------------------------------------------------------------===//
4315 // Unconditional branch (register) instructions
4316 //===----------------------------------------------------------------------===//
4317 // Contains: BR, BLR, RET, ERET, DRP.
4318
4319 // Most of the notional opcode fields in the A64I_Breg format are fixed in A64
4320 // at the moment.
4321 class A64I_BregImpl<bits<4> opc,
4322                     dag outs, dag ins, string asmstr, list<dag> patterns,
4323                     InstrItinClass itin = NoItinerary>
4324   : A64I_Breg<opc, 0b11111, 0b000000, 0b00000,
4325               outs, ins, asmstr, patterns, itin> {
4326   let isBranch         = 1;
4327   let isIndirectBranch = 1;
4328 }
4329
4330 // Note that these are not marked isCall or isReturn because as far as LLVM is
4331 // concerned they're not. "ret" is just another jump unless it has been selected
4332 // by LLVM as the function's return.
4333
4334 let isBranch = 1 in {
4335   def BRx : A64I_BregImpl<0b0000,(outs), (ins GPR64:$Rn),
4336                           "br\t$Rn", [(brind GPR64:$Rn)]> {
4337     let isBarrier = 1;
4338     let isTerminator = 1;
4339   }
4340
4341   def BLRx : A64I_BregImpl<0b0001, (outs), (ins GPR64:$Rn),
4342                            "blr\t$Rn", [(AArch64Call GPR64:$Rn)]> {
4343     let isBarrier = 0;
4344     let isCall = 1;
4345     let Defs = [X30];
4346   }
4347
4348   def RETx : A64I_BregImpl<0b0010, (outs), (ins GPR64:$Rn),
4349                            "ret\t$Rn", []> {
4350     let isBarrier = 1;
4351     let isTerminator = 1;
4352     let isReturn = 1;
4353   }
4354
4355   // Create a separate pseudo-instruction for codegen to use so that we don't
4356   // flag x30 as used in every function. It'll be restored before the RET by the
4357   // epilogue if it's legitimately used.
4358   def RET : A64PseudoExpand<(outs), (ins), [(A64ret)], (RETx (ops X30))> {
4359     let isTerminator = 1;
4360     let isBarrier = 1;
4361     let isReturn = 1;
4362   }
4363
4364   def ERET : A64I_BregImpl<0b0100, (outs), (ins), "eret", []> {
4365     let Rn = 0b11111;
4366     let isBarrier = 1;
4367     let isTerminator = 1;
4368     let isReturn = 1;
4369   }
4370
4371   def DRPS : A64I_BregImpl<0b0101, (outs), (ins), "drps", []> {
4372     let Rn = 0b11111;
4373     let isBarrier = 1;
4374   }
4375 }
4376
4377 def RETAlias : InstAlias<"ret", (RETx X30)>;
4378
4379
4380 //===----------------------------------------------------------------------===//
4381 // Address generation patterns
4382 //===----------------------------------------------------------------------===//
4383
4384 // Primary method of address generation for the small/absolute memory model is
4385 // an ADRP/ADR pair:
4386 //     ADRP x0, some_variable
4387 //     ADD x0, x0, #:lo12:some_variable
4388 //
4389 // The load/store elision of the ADD is accomplished when selecting
4390 // addressing-modes. This just mops up the cases where that doesn't work and we
4391 // really need an address in some register.
4392
4393 // This wrapper applies a LO12 modifier to the address. Otherwise we could just
4394 // use the same address.
4395
4396 class ADRP_ADD<SDNode Wrapper, SDNode addrop>
4397  : Pat<(Wrapper addrop:$Hi, addrop:$Lo12, (i32 imm)),
4398        (ADDxxi_lsl0_s (ADRPxi addrop:$Hi), addrop:$Lo12)>;
4399
4400 def : ADRP_ADD<A64WrapperSmall, tblockaddress>;
4401 def : ADRP_ADD<A64WrapperSmall, texternalsym>;
4402 def : ADRP_ADD<A64WrapperSmall, tglobaladdr>;
4403 def : ADRP_ADD<A64WrapperSmall, tglobaltlsaddr>;
4404 def : ADRP_ADD<A64WrapperSmall, tjumptable>;
4405
4406 //===----------------------------------------------------------------------===//
4407 // GOT access patterns
4408 //===----------------------------------------------------------------------===//
4409
4410 // FIXME: Wibble
4411
4412 class GOTLoadSmall<SDNode addrfrag>
4413   : Pat<(A64GOTLoad (A64WrapperSmall addrfrag:$Hi, addrfrag:$Lo12, 8)),
4414         (LS64_LDR (ADRPxi addrfrag:$Hi), addrfrag:$Lo12)>;
4415
4416 def : GOTLoadSmall<texternalsym>;
4417 def : GOTLoadSmall<tglobaladdr>;
4418 def : GOTLoadSmall<tglobaltlsaddr>;
4419
4420 //===----------------------------------------------------------------------===//
4421 // Tail call handling
4422 //===----------------------------------------------------------------------===//
4423
4424 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, Uses = [XSP] in {
4425   def TC_RETURNdi
4426     : PseudoInst<(outs), (ins i64imm:$dst, i32imm:$FPDiff),
4427                  [(AArch64tcret tglobaladdr:$dst, (i32 timm:$FPDiff))]>;
4428
4429   def TC_RETURNxi
4430     : PseudoInst<(outs), (ins tcGPR64:$dst, i32imm:$FPDiff),
4431                  [(AArch64tcret tcGPR64:$dst, (i32 timm:$FPDiff))]>;
4432 }
4433
4434 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1,
4435     Uses = [XSP] in {
4436   def TAIL_Bimm : A64PseudoExpand<(outs), (ins bimm_target:$Label), [],
4437                                   (Bimm bimm_target:$Label)>;
4438
4439   def TAIL_BRx : A64PseudoExpand<(outs), (ins tcGPR64:$Rd), [],
4440                                  (BRx GPR64:$Rd)>;
4441 }
4442
4443
4444 def : Pat<(AArch64tcret texternalsym:$dst, (i32 timm:$FPDiff)),
4445           (TC_RETURNdi texternalsym:$dst, imm:$FPDiff)>;
4446
4447 //===----------------------------------------------------------------------===//
4448 // Thread local storage
4449 //===----------------------------------------------------------------------===//
4450
4451 // This is a pseudo-instruction representing the ".tlsdesccall" directive in
4452 // assembly. Its effect is to insert an R_AARCH64_TLSDESC_CALL relocation at the
4453 // current location. It should always be immediately followed by a BLR
4454 // instruction, and is intended solely for relaxation by the linker.
4455
4456 def : Pat<(A64threadpointer), (MRSxi 0xde82)>;
4457
4458 def TLSDESCCALL : PseudoInst<(outs), (ins i64imm:$Lbl), []> {
4459   let hasSideEffects = 1;
4460 }
4461
4462 def TLSDESC_BLRx : PseudoInst<(outs), (ins GPR64:$Rn, i64imm:$Var),
4463                             [(A64tlsdesc_blr GPR64:$Rn, tglobaltlsaddr:$Var)]> {
4464   let isCall = 1;
4465   let Defs = [X30];
4466 }
4467
4468 def : Pat<(A64tlsdesc_blr GPR64:$Rn, texternalsym:$Var),
4469           (TLSDESC_BLRx GPR64:$Rn, texternalsym:$Var)>;
4470
4471 //===----------------------------------------------------------------------===//
4472 // Bitfield patterns
4473 //===----------------------------------------------------------------------===//
4474
4475 def bfi32_lsb_to_immr : SDNodeXForm<imm, [{
4476   return CurDAG->getTargetConstant((32 - N->getZExtValue()) % 32, MVT::i64);
4477 }]>;
4478
4479 def bfi64_lsb_to_immr : SDNodeXForm<imm, [{
4480   return CurDAG->getTargetConstant((64 - N->getZExtValue()) % 64, MVT::i64);
4481 }]>;
4482
4483 def bfi_width_to_imms : SDNodeXForm<imm, [{
4484   return CurDAG->getTargetConstant(N->getZExtValue() - 1, MVT::i64);
4485 }]>;
4486
4487
4488 // The simpler patterns deal with cases where no AND mask is actually needed
4489 // (either all bits are used or the low 32 bits are used).
4490 let AddedComplexity = 10 in {
4491
4492 def : Pat<(A64Bfi GPR64:$src, GPR64:$Rn, imm:$ImmR, imm:$ImmS),
4493            (BFIxxii GPR64:$src, GPR64:$Rn,
4494                     (bfi64_lsb_to_immr (i64 imm:$ImmR)),
4495                     (bfi_width_to_imms (i64 imm:$ImmS)))>;
4496
4497 def : Pat<(A64Bfi GPR32:$src, GPR32:$Rn, imm:$ImmR, imm:$ImmS),
4498           (BFIwwii GPR32:$src, GPR32:$Rn,
4499                    (bfi32_lsb_to_immr (i64 imm:$ImmR)),
4500                    (bfi_width_to_imms (i64 imm:$ImmS)))>;
4501
4502
4503 def : Pat<(and (A64Bfi GPR64:$src, GPR64:$Rn, imm:$ImmR, imm:$ImmS),
4504                (i64 4294967295)),
4505           (SUBREG_TO_REG (i64 0),
4506                          (BFIwwii (EXTRACT_SUBREG GPR64:$src, sub_32),
4507                                   (EXTRACT_SUBREG GPR64:$Rn, sub_32),
4508                                   (bfi32_lsb_to_immr (i64 imm:$ImmR)),
4509                                   (bfi_width_to_imms (i64 imm:$ImmS))),
4510                          sub_32)>;
4511
4512 }
4513
4514 //===----------------------------------------------------------------------===//
4515 // Constant island entries
4516 //===----------------------------------------------------------------------===//
4517
4518 // The constant island pass needs to create "instructions" in the middle of the
4519 // instruction stream to reresent its constants.
4520
4521 def cpinst_operand : Operand<i32>;
4522
4523 def CONSTPOOL_ENTRY : PseudoInst<(outs), (ins cpinst_operand:$instid,
4524                                               cpinst_operand:$cpidx,
4525                                               i32imm:$size), []> {
4526   let neverHasSideEffects = 1;
4527   let isNotDuplicable = 1;
4528 }
4529
4530 //===----------------------------------------------------------------------===//
4531 // Miscellaneous patterns
4532 //===----------------------------------------------------------------------===//
4533
4534 // Truncation from 64 to 32-bits just involves renaming your register.
4535 def : Pat<(i32 (trunc (i64 GPR64:$val))), (EXTRACT_SUBREG GPR64:$val, sub_32)>;
4536
4537 // Similarly, extension where we don't care about the high bits is
4538 // just a rename.
4539 def : Pat<(i64 (anyext (i32 GPR32:$val))),
4540           (INSERT_SUBREG (IMPLICIT_DEF), GPR32:$val, sub_32)>;
4541
4542 // SELECT instructions providing f128 types need to be handled by a
4543 // pseudo-instruction since the eventual code will need to introduce basic
4544 // blocks and control flow.
4545 def F128CSEL : PseudoInst<(outs FPR128:$Rd),
4546                           (ins FPR128:$Rn, FPR128:$Rm, cond_code_op:$Cond),
4547                           [(set FPR128:$Rd, (simple_select (f128 FPR128:$Rn),
4548                                                            FPR128:$Rm))]> {
4549   let Uses = [NZCV];
4550   let usesCustomInserter = 1;
4551 }
4552
4553 //===----------------------------------------------------------------------===//
4554 // Load/store patterns
4555 //===----------------------------------------------------------------------===//
4556
4557 // There are lots of patterns here, because we need to allow at least three
4558 // parameters to vary independently.
4559 //   1. Instruction: "ldrb w9, [sp]", "ldrh w9, [sp]", ...
4560 //   2. LLVM source: zextloadi8, anyextloadi8, ...
4561 //   3. Address-generation: A64Wrapper, (add BASE, OFFSET), ...
4562 //
4563 // The biggest problem turns out to be the address-generation variable. At the
4564 // point of instantiation we need to produce two DAGs, one for the pattern and
4565 // one for the instruction. Doing this at the lowest level of classes doesn't
4566 // work.
4567 //
4568 // Consider the simple uimm12 addressing mode, and the desire to match both (add
4569 // GPR64xsp:$Rn, uimm12:$Offset) and GPR64xsp:$Rn, particularly on the
4570 // instruction side. We'd need to insert either "GPR64xsp" and "uimm12" or
4571 // "GPR64xsp" and "0" into an unknown dag. !subst is not capable of this
4572 // operation, and PatFrags are for selection not output.
4573 //
4574 // As a result, the address-generation patterns are the final
4575 // instantiations. However, we do still need to vary the operand for the address
4576 // further down (At the point we're deciding A64WrapperSmall, we don't know
4577 // the memory width of the operation).
4578
4579 //===------------------------------
4580 // 1. Basic infrastructural defs
4581 //===------------------------------
4582
4583 // First, some simple classes for !foreach and !subst to use:
4584 class Decls {
4585   dag pattern;
4586 }
4587
4588 def decls : Decls;
4589 def ALIGN;
4590 def INST;
4591 def OFFSET;
4592 def SHIFT;
4593
4594 // You can't use !subst on an actual immediate, but you *can* use it on an
4595 // operand record that happens to match a single immediate. So we do.
4596 def imm_eq0 : ImmLeaf<i64, [{ return Imm == 0; }]>;
4597 def imm_eq1 : ImmLeaf<i64, [{ return Imm == 1; }]>;
4598 def imm_eq2 : ImmLeaf<i64, [{ return Imm == 2; }]>;
4599 def imm_eq3 : ImmLeaf<i64, [{ return Imm == 3; }]>;
4600 def imm_eq4 : ImmLeaf<i64, [{ return Imm == 4; }]>;
4601
4602 // If the low bits of a pointer are known to be 0 then an "or" is just as good
4603 // as addition for computing an offset. This fragment forwards that check for
4604 // TableGen's use.
4605 def add_like_or : PatFrag<(ops node:$lhs, node:$rhs), (or node:$lhs, node:$rhs),
4606 [{
4607   return CurDAG->isBaseWithConstantOffset(SDValue(N, 0));
4608 }]>;
4609
4610 // Load/store (unsigned immediate) operations with relocations against global
4611 // symbols (for lo12) are only valid if those symbols have correct alignment
4612 // (since the immediate offset is divided by the access scale, it can't have a
4613 // remainder).
4614 //
4615 // The guaranteed alignment is provided as part of the WrapperSmall
4616 // operation, and checked against one of these.
4617 def any_align   : ImmLeaf<i32, [{ (void)Imm; return true; }]>;
4618 def min_align2  : ImmLeaf<i32, [{ return Imm >= 2; }]>;
4619 def min_align4  : ImmLeaf<i32, [{ return Imm >= 4; }]>;
4620 def min_align8  : ImmLeaf<i32, [{ return Imm >= 8; }]>;
4621 def min_align16 : ImmLeaf<i32, [{ return Imm >= 16; }]>;
4622
4623 // "Normal" load/store instructions can be used on atomic operations, provided
4624 // the ordering parameter is at most "monotonic". Anything above that needs
4625 // special handling with acquire/release instructions.
4626 class simple_load<PatFrag base>
4627   : PatFrag<(ops node:$ptr), (base node:$ptr), [{
4628   return cast<AtomicSDNode>(N)->getOrdering() <= Monotonic;
4629 }]>;
4630
4631 def atomic_load_simple_i8  : simple_load<atomic_load_8>;
4632 def atomic_load_simple_i16 : simple_load<atomic_load_16>;
4633 def atomic_load_simple_i32 : simple_load<atomic_load_32>;
4634 def atomic_load_simple_i64 : simple_load<atomic_load_64>;
4635
4636 class simple_store<PatFrag base>
4637   : PatFrag<(ops node:$ptr, node:$val), (base node:$ptr, node:$val), [{
4638   return cast<AtomicSDNode>(N)->getOrdering() <= Monotonic;
4639 }]>;
4640
4641 def atomic_store_simple_i8  : simple_store<atomic_store_8>;
4642 def atomic_store_simple_i16 : simple_store<atomic_store_16>;
4643 def atomic_store_simple_i32 : simple_store<atomic_store_32>;
4644 def atomic_store_simple_i64 : simple_store<atomic_store_64>;
4645
4646 //===------------------------------
4647 // 2. UImm12 and SImm9
4648 //===------------------------------
4649
4650 // These instructions have two operands providing the address so they can be
4651 // treated similarly for most purposes.
4652
4653 //===------------------------------
4654 // 2.1 Base patterns covering extend/truncate semantics
4655 //===------------------------------
4656
4657 // Atomic patterns can be shared between integer operations of all sizes, a
4658 // quick multiclass here allows reuse.
4659 multiclass ls_atomic_pats<Instruction LOAD, Instruction STORE, dag Base,
4660                           dag Offset, dag address, RegisterClass TPR,
4661                           ValueType sty> {
4662   def : Pat<(!cast<PatFrag>("atomic_load_simple_" # sty) address),
4663             (LOAD Base, Offset)>;
4664
4665   def : Pat<(!cast<PatFrag>("atomic_store_simple_" # sty) address, TPR:$Rt),
4666             (STORE TPR:$Rt, Base, Offset)>;
4667 }
4668
4669 // Instructions accessing a memory chunk smaller than a register (or, in a
4670 // pinch, the same size) have a characteristic set of patterns they want to
4671 // match: extending loads and truncating stores. This class deals with the
4672 // sign-neutral version of those patterns.
4673 //
4674 // It will be instantiated across multiple addressing-modes.
4675 multiclass ls_small_pats<Instruction LOAD, Instruction STORE,
4676                          dag Base, dag Offset,
4677                          dag address, ValueType sty>
4678   : ls_atomic_pats<LOAD, STORE, Base, Offset, address, GPR32, sty> {
4679   def : Pat<(!cast<SDNode>(zextload # sty) address), (LOAD Base, Offset)>;
4680
4681   def : Pat<(!cast<SDNode>(extload # sty) address), (LOAD Base, Offset)>;
4682
4683   // For zero-extension to 64-bits we have to tell LLVM that the whole 64-bit
4684   // register was actually set.
4685   def : Pat<(i64 (!cast<SDNode>(zextload # sty) address)),
4686             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset), sub_32)>;
4687
4688   def : Pat<(i64 (!cast<SDNode>(extload # sty) address)),
4689             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset), sub_32)>;
4690
4691   def : Pat<(!cast<SDNode>(truncstore # sty) GPR32:$Rt, address),
4692             (STORE GPR32:$Rt, Base, Offset)>;
4693
4694   // For truncating store from 64-bits, we have to manually tell LLVM to
4695   // ignore the high bits of the x register.
4696   def : Pat<(!cast<SDNode>(truncstore # sty) GPR64:$Rt, address),
4697             (STORE (EXTRACT_SUBREG GPR64:$Rt, sub_32), Base, Offset)>;
4698 }
4699
4700 // Next come patterns for sign-extending loads.
4701 multiclass load_signed_pats<string T, string U, dag Base, dag Offset,
4702                             dag address, ValueType sty> {
4703   def : Pat<(i32 (!cast<SDNode>("sextload" # sty) address)),
4704             (!cast<Instruction>("LDRS" # T # "w" # U) Base, Offset)>;
4705
4706   def : Pat<(i64 (!cast<SDNode>("sextload" # sty) address)),
4707             (!cast<Instruction>("LDRS" # T # "x" # U) Base, Offset)>;
4708
4709 }
4710
4711 // and finally "natural-width" loads and stores come next.
4712 multiclass ls_neutral_pats<Instruction LOAD, Instruction STORE, dag Base,
4713                            dag Offset, dag address, RegisterClass TPR,
4714                            ValueType sty> {
4715   def : Pat<(sty (load address)), (LOAD Base, Offset)>;
4716   def : Pat<(store (sty TPR:$Rt), address), (STORE TPR:$Rt, Base, Offset)>;
4717 }
4718
4719 // Integer operations also get atomic instructions to select for.
4720 multiclass ls_int_neutral_pats<Instruction LOAD, Instruction STORE, dag Base,
4721                            dag Offset, dag address, RegisterClass TPR,
4722                            ValueType sty>
4723   : ls_neutral_pats<LOAD, STORE, Base, Offset, address, TPR, sty>,
4724     ls_atomic_pats<LOAD, STORE, Base, Offset, address, TPR, sty>;
4725
4726 //===------------------------------
4727 // 2.2. Addressing-mode instantiations
4728 //===------------------------------
4729
4730 multiclass uimm12_pats<dag address, dag Base, dag Offset> {
4731   defm : ls_small_pats<LS8_LDR, LS8_STR, Base,
4732                        !foreach(decls.pattern, Offset,
4733                                 !subst(OFFSET, byte_uimm12, decls.pattern)),
4734                        !foreach(decls.pattern, address,
4735                                 !subst(OFFSET, byte_uimm12,
4736                                 !subst(ALIGN, any_align, decls.pattern))),
4737                        i8>;
4738   defm : ls_small_pats<LS16_LDR, LS16_STR, Base,
4739                        !foreach(decls.pattern, Offset,
4740                                 !subst(OFFSET, hword_uimm12, decls.pattern)),
4741                        !foreach(decls.pattern, address,
4742                                 !subst(OFFSET, hword_uimm12,
4743                                 !subst(ALIGN, min_align2, decls.pattern))),
4744                        i16>;
4745   defm : ls_small_pats<LS32_LDR, LS32_STR, Base,
4746                        !foreach(decls.pattern, Offset,
4747                                 !subst(OFFSET, word_uimm12, decls.pattern)),
4748                        !foreach(decls.pattern, address,
4749                                 !subst(OFFSET, word_uimm12,
4750                                 !subst(ALIGN, min_align4, decls.pattern))),
4751                        i32>;
4752
4753   defm : ls_int_neutral_pats<LS32_LDR, LS32_STR, Base,
4754                           !foreach(decls.pattern, Offset,
4755                                    !subst(OFFSET, word_uimm12, decls.pattern)),
4756                           !foreach(decls.pattern, address,
4757                                    !subst(OFFSET, word_uimm12,
4758                                    !subst(ALIGN, min_align4, decls.pattern))),
4759                           GPR32, i32>;
4760
4761   defm : ls_int_neutral_pats<LS64_LDR, LS64_STR, Base,
4762                           !foreach(decls.pattern, Offset,
4763                                    !subst(OFFSET, dword_uimm12, decls.pattern)),
4764                           !foreach(decls.pattern, address,
4765                                    !subst(OFFSET, dword_uimm12,
4766                                    !subst(ALIGN, min_align8, decls.pattern))),
4767                           GPR64, i64>;
4768
4769   defm : ls_neutral_pats<LSFP16_LDR, LSFP16_STR, Base,
4770                           !foreach(decls.pattern, Offset,
4771                                    !subst(OFFSET, hword_uimm12, decls.pattern)),
4772                           !foreach(decls.pattern, address,
4773                                    !subst(OFFSET, hword_uimm12,
4774                                    !subst(ALIGN, min_align2, decls.pattern))),
4775                           FPR16, f16>;
4776
4777   defm : ls_neutral_pats<LSFP32_LDR, LSFP32_STR, Base,
4778                           !foreach(decls.pattern, Offset,
4779                                    !subst(OFFSET, word_uimm12, decls.pattern)),
4780                           !foreach(decls.pattern, address,
4781                                    !subst(OFFSET, word_uimm12,
4782                                    !subst(ALIGN, min_align4, decls.pattern))),
4783                           FPR32, f32>;
4784
4785   defm : ls_neutral_pats<LSFP64_LDR, LSFP64_STR, Base,
4786                           !foreach(decls.pattern, Offset,
4787                                    !subst(OFFSET, dword_uimm12, decls.pattern)),
4788                           !foreach(decls.pattern, address,
4789                                    !subst(OFFSET, dword_uimm12,
4790                                    !subst(ALIGN, min_align8, decls.pattern))),
4791                           FPR64, f64>;
4792
4793   defm : ls_neutral_pats<LSFP128_LDR, LSFP128_STR, Base,
4794                           !foreach(decls.pattern, Offset,
4795                                    !subst(OFFSET, qword_uimm12, decls.pattern)),
4796                           !foreach(decls.pattern, address,
4797                                    !subst(OFFSET, qword_uimm12,
4798                                    !subst(ALIGN, min_align16, decls.pattern))),
4799                           FPR128, f128>;
4800
4801   defm : load_signed_pats<"B", "", Base,
4802                           !foreach(decls.pattern, Offset,
4803                                    !subst(OFFSET, byte_uimm12, decls.pattern)),
4804                           !foreach(decls.pattern, address,
4805                                    !subst(OFFSET, byte_uimm12,
4806                                    !subst(ALIGN, any_align, decls.pattern))),
4807                           i8>;
4808
4809   defm : load_signed_pats<"H", "", Base,
4810                           !foreach(decls.pattern, Offset,
4811                                    !subst(OFFSET, hword_uimm12, decls.pattern)),
4812                           !foreach(decls.pattern, address,
4813                                    !subst(OFFSET, hword_uimm12,
4814                                    !subst(ALIGN, min_align2, decls.pattern))),
4815                           i16>;
4816
4817   def : Pat<(sextloadi32 !foreach(decls.pattern, address,
4818                                   !subst(OFFSET, word_uimm12,
4819                                   !subst(ALIGN, min_align4, decls.pattern)))),
4820             (LDRSWx Base, !foreach(decls.pattern, Offset,
4821                                   !subst(OFFSET, word_uimm12, decls.pattern)))>;
4822 }
4823
4824 // Straightforward patterns of last resort: a pointer with or without an
4825 // appropriate offset.
4826 defm : uimm12_pats<(i64 GPR64xsp:$Rn), (i64 GPR64xsp:$Rn), (i64 0)>;
4827 defm : uimm12_pats<(add GPR64xsp:$Rn, OFFSET:$UImm12),
4828                    (i64 GPR64xsp:$Rn), (i64 OFFSET:$UImm12)>;
4829
4830 // The offset could be hidden behind an "or", of course:
4831 defm : uimm12_pats<(add_like_or GPR64xsp:$Rn, OFFSET:$UImm12),
4832                    (i64 GPR64xsp:$Rn), (i64 OFFSET:$UImm12)>;
4833
4834 // Global addresses under the small-absolute model should use these
4835 // instructions. There are ELF relocations specifically for it.
4836 defm : uimm12_pats<(A64WrapperSmall tglobaladdr:$Hi, tglobaladdr:$Lo12, ALIGN),
4837                    (ADRPxi tglobaladdr:$Hi), (i64 tglobaladdr:$Lo12)>;
4838
4839 defm : uimm12_pats<(A64WrapperSmall tglobaltlsaddr:$Hi, tglobaltlsaddr:$Lo12,
4840                                     ALIGN),
4841                    (ADRPxi tglobaltlsaddr:$Hi), (i64 tglobaltlsaddr:$Lo12)>;
4842
4843 // External symbols that make it this far should also get standard relocations.
4844 defm : uimm12_pats<(A64WrapperSmall texternalsym:$Hi, texternalsym:$Lo12,
4845                                     ALIGN),
4846                    (ADRPxi texternalsym:$Hi), (i64 texternalsym:$Lo12)>;
4847
4848
4849 // We also want to use uimm12 instructions for local variables at the moment.
4850 def tframeindex_XFORM : SDNodeXForm<frameindex, [{
4851   int FI = cast<FrameIndexSDNode>(N)->getIndex();
4852   return CurDAG->getTargetFrameIndex(FI, MVT::i64);
4853 }]>;
4854
4855 defm : uimm12_pats<(i64 frameindex:$Rn),
4856                    (tframeindex_XFORM tframeindex:$Rn), (i64 0)>;
4857
4858 // These can be much simpler than uimm12 because we don't to change the operand
4859 // type (e.g. LDURB and LDURH take the same operands).
4860 multiclass simm9_pats<dag address, dag Base, dag Offset> {
4861   defm : ls_small_pats<LS8_LDUR, LS8_STUR, Base, Offset, address, i8>;
4862   defm : ls_small_pats<LS16_LDUR, LS16_STUR, Base, Offset, address, i16>;
4863
4864   defm : ls_int_neutral_pats<LS32_LDUR, LS32_STUR, Base, Offset, address,
4865                              GPR32, i32>;
4866   defm : ls_int_neutral_pats<LS64_LDUR, LS64_STUR, Base, Offset, address,
4867                              GPR64, i64>;
4868
4869   defm : ls_neutral_pats<LSFP16_LDUR, LSFP16_STUR, Base, Offset, address,
4870                          FPR16, f16>;
4871   defm : ls_neutral_pats<LSFP32_LDUR, LSFP32_STUR, Base, Offset, address,
4872                          FPR32, f32>;
4873   defm : ls_neutral_pats<LSFP64_LDUR, LSFP64_STUR, Base, Offset, address,
4874                          FPR64, f64>;
4875   defm : ls_neutral_pats<LSFP128_LDUR, LSFP128_STUR, Base, Offset, address,
4876                          FPR128, f128>;
4877
4878   def : Pat<(i64 (zextloadi32 address)),
4879             (SUBREG_TO_REG (i64 0), (LS32_LDUR Base, Offset), sub_32)>;
4880
4881   def : Pat<(truncstorei32 GPR64:$Rt, address),
4882             (LS32_STUR (EXTRACT_SUBREG GPR64:$Rt, sub_32), Base, Offset)>;
4883
4884   defm : load_signed_pats<"B", "_U", Base, Offset, address, i8>;
4885   defm : load_signed_pats<"H", "_U", Base, Offset, address, i16>;
4886   def : Pat<(sextloadi32 address), (LDURSWx Base, Offset)>;
4887 }
4888
4889 defm : simm9_pats<(add GPR64xsp:$Rn, simm9:$SImm9),
4890                   (i64 GPR64xsp:$Rn), (SDXF_simm9 simm9:$SImm9)>;
4891
4892 defm : simm9_pats<(add_like_or GPR64xsp:$Rn, simm9:$SImm9),
4893                   (i64 GPR64xsp:$Rn), (SDXF_simm9 simm9:$SImm9)>;
4894
4895
4896 //===------------------------------
4897 // 3. Register offset patterns
4898 //===------------------------------
4899
4900 // Atomic patterns can be shared between integer operations of all sizes, a
4901 // quick multiclass here allows reuse.
4902 multiclass ro_atomic_pats<Instruction LOAD, Instruction STORE, dag Base,
4903                           dag Offset, dag Extend, dag address,
4904                           RegisterClass TPR, ValueType sty> {
4905   def : Pat<(!cast<PatFrag>("atomic_load_simple_" # sty) address),
4906             (LOAD Base, Offset, Extend)>;
4907
4908   def : Pat<(!cast<PatFrag>("atomic_store_simple_" # sty) address, TPR:$Rt),
4909             (STORE TPR:$Rt, Base, Offset, Extend)>;
4910 }
4911
4912 // The register offset instructions take three operands giving the instruction,
4913 // and have an annoying split between instructions where Rm is 32-bit and
4914 // 64-bit. So we need a special hierarchy to describe them. Other than that the
4915 // same operations should be supported as for simm9 and uimm12 addressing.
4916
4917 multiclass ro_small_pats<Instruction LOAD, Instruction STORE,
4918                          dag Base, dag Offset, dag Extend,
4919                          dag address, ValueType sty>
4920   : ro_atomic_pats<LOAD, STORE, Base, Offset, Extend, address, GPR32, sty> {
4921   def : Pat<(!cast<SDNode>(zextload # sty) address),
4922             (LOAD Base, Offset, Extend)>;
4923
4924   def : Pat<(!cast<SDNode>(extload # sty) address),
4925             (LOAD Base, Offset, Extend)>;
4926
4927   // For zero-extension to 64-bits we have to tell LLVM that the whole 64-bit
4928   // register was actually set.
4929   def : Pat<(i64 (!cast<SDNode>(zextload # sty) address)),
4930             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset, Extend), sub_32)>;
4931
4932   def : Pat<(i64 (!cast<SDNode>(extload # sty) address)),
4933             (SUBREG_TO_REG (i64 0), (LOAD Base, Offset, Extend), sub_32)>;
4934
4935   def : Pat<(!cast<SDNode>(truncstore # sty) GPR32:$Rt, address),
4936             (STORE GPR32:$Rt, Base, Offset, Extend)>;
4937
4938   // For truncating store from 64-bits, we have to manually tell LLVM to
4939   // ignore the high bits of the x register.
4940   def : Pat<(!cast<SDNode>(truncstore # sty) GPR64:$Rt, address),
4941             (STORE (EXTRACT_SUBREG GPR64:$Rt, sub_32), Base, Offset, Extend)>;
4942
4943 }
4944
4945 // Next come patterns for sign-extending loads.
4946 multiclass ro_signed_pats<string T, string Rm, dag Base, dag Offset, dag Extend,
4947                           dag address, ValueType sty> {
4948   def : Pat<(i32 (!cast<SDNode>("sextload" # sty) address)),
4949             (!cast<Instruction>("LDRS" # T # "w_" # Rm # "_RegOffset")
4950               Base, Offset, Extend)>;
4951
4952   def : Pat<(i64 (!cast<SDNode>("sextload" # sty) address)),
4953             (!cast<Instruction>("LDRS" # T # "x_" # Rm # "_RegOffset")
4954               Base, Offset, Extend)>;
4955 }
4956
4957 // and finally "natural-width" loads and stores come next.
4958 multiclass ro_neutral_pats<Instruction LOAD, Instruction STORE,
4959                            dag Base, dag Offset, dag Extend, dag address,
4960                            RegisterClass TPR, ValueType sty> {
4961   def : Pat<(sty (load address)), (LOAD Base, Offset, Extend)>;
4962   def : Pat<(store (sty TPR:$Rt), address),
4963             (STORE TPR:$Rt, Base, Offset, Extend)>;
4964 }
4965
4966 multiclass ro_int_neutral_pats<Instruction LOAD, Instruction STORE,
4967                                dag Base, dag Offset, dag Extend, dag address,
4968                                RegisterClass TPR, ValueType sty>
4969   : ro_neutral_pats<LOAD, STORE, Base, Offset, Extend, address, TPR, sty>,
4970     ro_atomic_pats<LOAD, STORE, Base, Offset, Extend, address, TPR, sty>;
4971
4972 multiclass regoff_pats<string Rm, dag address, dag Base, dag Offset,
4973                        dag Extend> {
4974   defm : ro_small_pats<!cast<Instruction>("LS8_" # Rm # "_RegOffset_LDR"),
4975                        !cast<Instruction>("LS8_" # Rm # "_RegOffset_STR"),
4976                        Base, Offset, Extend,
4977                        !foreach(decls.pattern, address,
4978                                 !subst(SHIFT, imm_eq0, decls.pattern)),
4979                        i8>;
4980   defm : ro_small_pats<!cast<Instruction>("LS16_" # Rm # "_RegOffset_LDR"),
4981                        !cast<Instruction>("LS16_" # Rm # "_RegOffset_STR"),
4982                        Base, Offset, Extend,
4983                        !foreach(decls.pattern, address,
4984                                 !subst(SHIFT, imm_eq1, decls.pattern)),
4985                        i16>;
4986   defm : ro_small_pats<!cast<Instruction>("LS32_" # Rm # "_RegOffset_LDR"),
4987                        !cast<Instruction>("LS32_" # Rm # "_RegOffset_STR"),
4988                        Base, Offset, Extend,
4989                        !foreach(decls.pattern, address,
4990                                 !subst(SHIFT, imm_eq2, decls.pattern)),
4991                        i32>;
4992
4993   defm : ro_int_neutral_pats<
4994                             !cast<Instruction>("LS32_" # Rm # "_RegOffset_LDR"),
4995                             !cast<Instruction>("LS32_" # Rm # "_RegOffset_STR"),
4996                             Base, Offset, Extend,
4997                             !foreach(decls.pattern, address,
4998                                      !subst(SHIFT, imm_eq2, decls.pattern)),
4999                             GPR32, i32>;
5000
5001   defm : ro_int_neutral_pats<
5002                             !cast<Instruction>("LS64_" # Rm # "_RegOffset_LDR"),
5003                             !cast<Instruction>("LS64_" # Rm # "_RegOffset_STR"),
5004                             Base, Offset, Extend,
5005                             !foreach(decls.pattern, address,
5006                                      !subst(SHIFT, imm_eq3, decls.pattern)),
5007                             GPR64, i64>;
5008
5009   defm : ro_neutral_pats<!cast<Instruction>("LSFP16_" # Rm # "_RegOffset_LDR"),
5010                          !cast<Instruction>("LSFP16_" # Rm # "_RegOffset_STR"),
5011                          Base, Offset, Extend,
5012                          !foreach(decls.pattern, address,
5013                                   !subst(SHIFT, imm_eq1, decls.pattern)),
5014                          FPR16, f16>;
5015
5016   defm : ro_neutral_pats<!cast<Instruction>("LSFP32_" # Rm # "_RegOffset_LDR"),
5017                          !cast<Instruction>("LSFP32_" # Rm # "_RegOffset_STR"),
5018                          Base, Offset, Extend,
5019                          !foreach(decls.pattern, address,
5020                                   !subst(SHIFT, imm_eq2, decls.pattern)),
5021                          FPR32, f32>;
5022
5023   defm : ro_neutral_pats<!cast<Instruction>("LSFP64_" # Rm # "_RegOffset_LDR"),
5024                          !cast<Instruction>("LSFP64_" # Rm # "_RegOffset_STR"),
5025                          Base, Offset, Extend,
5026                          !foreach(decls.pattern, address,
5027                                   !subst(SHIFT, imm_eq3, decls.pattern)),
5028                          FPR64, f64>;
5029
5030   defm : ro_neutral_pats<!cast<Instruction>("LSFP128_" # Rm # "_RegOffset_LDR"),
5031                          !cast<Instruction>("LSFP128_" # Rm # "_RegOffset_STR"),
5032                          Base, Offset, Extend,
5033                          !foreach(decls.pattern, address,
5034                                   !subst(SHIFT, imm_eq4, decls.pattern)),
5035                          FPR128, f128>;
5036
5037   defm : ro_signed_pats<"B", Rm, Base, Offset, Extend,
5038                           !foreach(decls.pattern, address,
5039                                    !subst(SHIFT, imm_eq0, decls.pattern)),
5040                           i8>;
5041
5042   defm : ro_signed_pats<"H", Rm, Base, Offset, Extend,
5043                           !foreach(decls.pattern, address,
5044                                    !subst(SHIFT, imm_eq1, decls.pattern)),
5045                           i16>;
5046
5047   def : Pat<(sextloadi32 !foreach(decls.pattern, address,
5048                                   !subst(SHIFT, imm_eq2, decls.pattern))),
5049             (!cast<Instruction>("LDRSWx_" # Rm # "_RegOffset")
5050               Base, Offset, Extend)>;
5051 }
5052
5053
5054 // Finally we're in a position to tell LLVM exactly what addresses are reachable
5055 // using register-offset instructions. Essentially a base plus a possibly
5056 // extended, possibly shifted (by access size) offset.
5057
5058 defm : regoff_pats<"Wm", (add GPR64xsp:$Rn, (sext GPR32:$Rm)),
5059                    (i64 GPR64xsp:$Rn), (i32 GPR32:$Rm), (i64 6)>;
5060
5061 defm : regoff_pats<"Wm", (add GPR64xsp:$Rn, (shl (sext GPR32:$Rm), SHIFT)),
5062                    (i64 GPR64xsp:$Rn), (i32 GPR32:$Rm), (i64 7)>;
5063
5064 defm : regoff_pats<"Wm", (add GPR64xsp:$Rn, (zext GPR32:$Rm)),
5065                    (i64 GPR64xsp:$Rn), (i32 GPR32:$Rm), (i64 2)>;
5066
5067 defm : regoff_pats<"Wm", (add GPR64xsp:$Rn, (shl (zext GPR32:$Rm), SHIFT)),
5068                    (i64 GPR64xsp:$Rn), (i32 GPR32:$Rm), (i64 3)>;
5069
5070 defm : regoff_pats<"Xm", (add GPR64xsp:$Rn, GPR64:$Rm),
5071                    (i64 GPR64xsp:$Rn), (i64 GPR64:$Rm), (i64 2)>;
5072
5073 defm : regoff_pats<"Xm", (add GPR64xsp:$Rn, (shl GPR64:$Rm, SHIFT)),
5074                    (i64 GPR64xsp:$Rn), (i64 GPR64:$Rm), (i64 3)>;