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