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