xref: /freebsd/contrib/llvm-project/llvm/lib/Target/ARM/ARMInstrThumb2.td (revision 4d3fc8b0570b29fb0d6ee9525f104d52176ff0d4)
1//===-- ARMInstrThumb2.td - Thumb2 support for ARM ---------*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file describes the Thumb2 instruction set.
10//
11//===----------------------------------------------------------------------===//
12
13// IT block predicate field
14def it_pred_asmoperand : AsmOperandClass {
15  let Name = "ITCondCode";
16  let ParserMethod = "parseITCondCode";
17}
18def it_pred : Operand<i32> {
19  let PrintMethod = "printMandatoryPredicateOperand";
20  let ParserMatchClass = it_pred_asmoperand;
21}
22
23// IT block condition mask
24def it_mask_asmoperand : AsmOperandClass { let Name = "ITMask"; }
25def it_mask : Operand<i32> {
26  let PrintMethod = "printThumbITMask";
27  let ParserMatchClass = it_mask_asmoperand;
28  let EncoderMethod = "getITMaskOpValue";
29}
30
31// t2_shift_imm: An integer that encodes a shift amount and the type of shift
32// (asr or lsl). The 6-bit immediate encodes as:
33//    {5}     0 ==> lsl
34//            1     asr
35//    {4-0}   imm5 shift amount.
36//            asr #32 not allowed
37def t2_shift_imm : Operand<i32> {
38  let PrintMethod = "printShiftImmOperand";
39  let ParserMatchClass = ShifterImmAsmOperand;
40  let DecoderMethod = "DecodeT2ShifterImmOperand";
41}
42
43def mve_shift_imm : AsmOperandClass {
44  let Name = "MVELongShift";
45  let RenderMethod = "addImmOperands";
46  let DiagnosticString = "operand must be an immediate in the range [1,32]";
47}
48def long_shift : Operand<i32>,
49                 ImmLeaf<i32, [{ return Imm > 0 && Imm <= 32; }]> {
50  let ParserMatchClass = mve_shift_imm;
51  let DecoderMethod = "DecodeLongShiftOperand";
52}
53
54// Shifted operands. No register controlled shifts for Thumb2.
55// Note: We do not support rrx shifted operands yet.
56def t2_so_reg : Operand<i32>,    // reg imm
57                ComplexPattern<i32, 2, "SelectShiftImmShifterOperand",
58                               [shl,srl,sra,rotr]> {
59  let EncoderMethod = "getT2SORegOpValue";
60  let PrintMethod = "printT2SOOperand";
61  let DecoderMethod = "DecodeSORegImmOperand";
62  let ParserMatchClass = ShiftedImmAsmOperand;
63  let MIOperandInfo = (ops rGPR, i32imm);
64}
65
66// Same as above, but only matching on a single use node.
67def t2_so_reg_oneuse : Operand<i32>,
68                       ComplexPattern<i32, 2,
69                                      "SelectShiftImmShifterOperandOneUse",
70                                      [shl,srl,sra,rotr]>;
71
72// t2_so_imm_not_XFORM - Return the complement of a t2_so_imm value
73def t2_so_imm_not_XFORM : SDNodeXForm<imm, [{
74  return CurDAG->getTargetConstant(~((uint32_t)N->getZExtValue()), SDLoc(N),
75                                   MVT::i32);
76}]>;
77
78// t2_so_imm_neg_XFORM - Return the negation of a t2_so_imm value
79def t2_so_imm_neg_XFORM : SDNodeXForm<imm, [{
80  return CurDAG->getTargetConstant(-((int)N->getZExtValue()), SDLoc(N),
81                                   MVT::i32);
82}]>;
83
84// so_imm_notSext_XFORM - Return a so_imm value packed into the format
85// described for so_imm_notSext def below, with sign extension from 16
86// bits.
87def t2_so_imm_notSext16_XFORM : SDNodeXForm<imm, [{
88  APInt apIntN = N->getAPIntValue();
89  unsigned N16bitSignExt = apIntN.trunc(16).sext(32).getZExtValue();
90  return CurDAG->getTargetConstant(~N16bitSignExt, SDLoc(N), MVT::i32);
91}]>;
92
93// t2_so_imm - Match a 32-bit immediate operand, which is an
94// 8-bit immediate rotated by an arbitrary number of bits, or an 8-bit
95// immediate splatted into multiple bytes of the word.
96def t2_so_imm_asmoperand : AsmOperandClass {
97  let Name = "T2SOImm";
98  let RenderMethod = "addImmOperands";
99
100}
101def t2_so_imm : Operand<i32>, ImmLeaf<i32, [{
102    return ARM_AM::getT2SOImmVal(Imm) != -1;
103  }]> {
104  let ParserMatchClass = t2_so_imm_asmoperand;
105  let EncoderMethod = "getT2SOImmOpValue";
106  let DecoderMethod = "DecodeT2SOImm";
107}
108
109// t2_so_imm_not - Match an immediate that is a complement
110// of a t2_so_imm.
111// Note: this pattern doesn't require an encoder method and such, as it's
112// only used on aliases (Pat<> and InstAlias<>). The actual encoding
113// is handled by the destination instructions, which use t2_so_imm.
114def t2_so_imm_not_asmoperand : AsmOperandClass { let Name = "T2SOImmNot"; }
115def t2_so_imm_not : Operand<i32>, PatLeaf<(imm), [{
116  return ARM_AM::getT2SOImmVal(~((uint32_t)N->getZExtValue())) != -1;
117}], t2_so_imm_not_XFORM> {
118  let ParserMatchClass = t2_so_imm_not_asmoperand;
119}
120
121// t2_so_imm_notSext - match an immediate that is a complement of a t2_so_imm
122// if the upper 16 bits are zero.
123def t2_so_imm_notSext : Operand<i32>, PatLeaf<(imm), [{
124    APInt apIntN = N->getAPIntValue();
125    if (!apIntN.isIntN(16)) return false;
126    unsigned N16bitSignExt = apIntN.trunc(16).sext(32).getZExtValue();
127    return ARM_AM::getT2SOImmVal(~N16bitSignExt) != -1;
128  }], t2_so_imm_notSext16_XFORM> {
129  let ParserMatchClass = t2_so_imm_not_asmoperand;
130}
131
132// t2_so_imm_neg - Match an immediate that is a negation of a t2_so_imm.
133def t2_so_imm_neg_asmoperand : AsmOperandClass { let Name = "T2SOImmNeg"; }
134def t2_so_imm_neg : Operand<i32>, ImmLeaf<i32, [{
135  return Imm && ARM_AM::getT2SOImmVal(-(uint32_t)Imm) != -1;
136}], t2_so_imm_neg_XFORM> {
137  let ParserMatchClass = t2_so_imm_neg_asmoperand;
138}
139
140/// imm0_4095 predicate - True if the 32-bit immediate is in the range [0,4095].
141def imm0_4095_asmoperand: ImmAsmOperand<0,4095> { let Name = "Imm0_4095"; }
142def imm0_4095 : Operand<i32>, ImmLeaf<i32, [{
143  return Imm >= 0 && Imm < 4096;
144}]> {
145  let ParserMatchClass = imm0_4095_asmoperand;
146}
147
148def imm0_4095_neg_asmoperand: AsmOperandClass { let Name = "Imm0_4095Neg"; }
149def imm0_4095_neg : Operand<i32>, PatLeaf<(i32 imm), [{
150 return (uint32_t)(-N->getZExtValue()) < 4096;
151}], imm_neg_XFORM> {
152  let ParserMatchClass = imm0_4095_neg_asmoperand;
153}
154
155def imm1_255_neg : PatLeaf<(i32 imm), [{
156  uint32_t Val = -N->getZExtValue();
157  return (Val > 0 && Val < 255);
158}], imm_neg_XFORM>;
159
160def imm0_255_not : PatLeaf<(i32 imm), [{
161  return (uint32_t)(~N->getZExtValue()) < 255;
162}], imm_not_XFORM>;
163
164def lo5AllOne : PatLeaf<(i32 imm), [{
165  // Returns true if all low 5-bits are 1.
166  return (((uint32_t)N->getZExtValue()) & 0x1FUL) == 0x1FUL;
167}]>;
168
169// Define Thumb2 specific addressing modes.
170
171// t2_addr_offset_none := reg
172def MemNoOffsetT2AsmOperand
173  : AsmOperandClass { let Name = "MemNoOffsetT2"; }
174def t2_addr_offset_none : MemOperand {
175  let PrintMethod = "printAddrMode7Operand";
176  let DecoderMethod = "DecodeGPRnopcRegisterClass";
177  let ParserMatchClass = MemNoOffsetT2AsmOperand;
178  let MIOperandInfo = (ops GPRnopc:$base);
179}
180
181// t2_nosp_addr_offset_none := reg
182def MemNoOffsetT2NoSpAsmOperand
183  : AsmOperandClass { let Name = "MemNoOffsetT2NoSp"; }
184def t2_nosp_addr_offset_none : MemOperand {
185  let PrintMethod = "printAddrMode7Operand";
186  let DecoderMethod = "DecoderGPRRegisterClass";
187  let ParserMatchClass = MemNoOffsetT2NoSpAsmOperand;
188  let MIOperandInfo = (ops rGPR:$base);
189}
190
191// t2addrmode_imm12  := reg + imm12
192def t2addrmode_imm12_asmoperand : AsmOperandClass {let Name="MemUImm12Offset";}
193def t2addrmode_imm12 : MemOperand,
194                       ComplexPattern<i32, 2, "SelectT2AddrModeImm12", []> {
195  let PrintMethod = "printAddrModeImm12Operand<false>";
196  let EncoderMethod = "getAddrModeImm12OpValue";
197  let DecoderMethod = "DecodeT2AddrModeImm12";
198  let ParserMatchClass = t2addrmode_imm12_asmoperand;
199  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
200}
201
202// t2ldrlabel  := imm12
203def t2ldrlabel : MemOperand {
204  let EncoderMethod = "getAddrModeImm12OpValue";
205  let PrintMethod = "printThumbLdrLabelOperand";
206}
207
208def t2ldr_pcrel_imm12_asmoperand : AsmOperandClass {let Name = "MemPCRelImm12";}
209def t2ldr_pcrel_imm12 : Operand<i32> {
210  let ParserMatchClass = t2ldr_pcrel_imm12_asmoperand;
211  // used for assembler pseudo instruction and maps to t2ldrlabel, so
212  // doesn't need encoder or print methods of its own.
213}
214
215// ADR instruction labels.
216def t2adrlabel : Operand<i32> {
217  let EncoderMethod = "getT2AdrLabelOpValue";
218  let PrintMethod = "printAdrLabelOperand<0>";
219}
220
221// t2addrmode_posimm8  := reg + imm8
222def MemPosImm8OffsetAsmOperand : AsmOperandClass {
223  let Name="MemPosImm8Offset";
224  let RenderMethod = "addMemImmOffsetOperands";
225}
226def t2addrmode_posimm8 : MemOperand {
227  let PrintMethod = "printT2AddrModeImm8Operand<false>";
228  let EncoderMethod = "getT2AddrModeImmOpValue<8,0>";
229  let DecoderMethod = "DecodeT2AddrModeImm8";
230  let ParserMatchClass = MemPosImm8OffsetAsmOperand;
231  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
232}
233
234// t2addrmode_negimm8  := reg - imm8
235def MemNegImm8OffsetAsmOperand : AsmOperandClass {
236  let Name="MemNegImm8Offset";
237  let RenderMethod = "addMemImmOffsetOperands";
238}
239def t2addrmode_negimm8 : MemOperand,
240                      ComplexPattern<i32, 2, "SelectT2AddrModeImm8", []> {
241  let PrintMethod = "printT2AddrModeImm8Operand<false>";
242  let EncoderMethod = "getT2AddrModeImmOpValue<8,0>";
243  let DecoderMethod = "DecodeT2AddrModeImm8";
244  let ParserMatchClass = MemNegImm8OffsetAsmOperand;
245  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
246}
247
248// t2addrmode_imm8  := reg +/- imm8
249def MemImm8OffsetAsmOperand : AsmOperandClass {
250  let Name = "MemImm8Offset";
251  let RenderMethod = "addMemImmOffsetOperands";
252}
253class T2AddrMode_Imm8 : MemOperand,
254                        ComplexPattern<i32, 2, "SelectT2AddrModeImm8", []> {
255  let EncoderMethod = "getT2AddrModeImmOpValue<8,0>";
256  let DecoderMethod = "DecodeT2AddrModeImm8";
257  let ParserMatchClass = MemImm8OffsetAsmOperand;
258  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
259}
260
261def t2addrmode_imm8 : T2AddrMode_Imm8 {
262  let PrintMethod = "printT2AddrModeImm8Operand<false>";
263}
264
265def t2addrmode_imm8_pre : T2AddrMode_Imm8 {
266  let PrintMethod = "printT2AddrModeImm8Operand<true>";
267}
268
269def t2am_imm8_offset : MemOperand,
270                       ComplexPattern<i32, 1, "SelectT2AddrModeImm8Offset",
271                                      [], [SDNPWantRoot]> {
272  let PrintMethod = "printT2AddrModeImm8OffsetOperand";
273  let EncoderMethod = "getT2AddrModeImm8OffsetOpValue";
274  let DecoderMethod = "DecodeT2Imm8";
275}
276
277// t2addrmode_imm8s4  := reg +/- (imm8 << 2)
278def MemImm8s4OffsetAsmOperand : AsmOperandClass {let Name = "MemImm8s4Offset";}
279class T2AddrMode_Imm8s4 : MemOperand,
280                          ComplexPattern<i32, 2, "SelectT2AddrModeImm8<2>", []> {
281  let EncoderMethod = "getT2AddrModeImm8s4OpValue";
282  let DecoderMethod = "DecodeT2AddrModeImm8s4";
283  let ParserMatchClass = MemImm8s4OffsetAsmOperand;
284  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
285}
286
287def t2addrmode_imm8s4 : T2AddrMode_Imm8s4 {
288  let PrintMethod = "printT2AddrModeImm8s4Operand<false>";
289}
290
291def t2addrmode_imm8s4_pre : T2AddrMode_Imm8s4 {
292  let PrintMethod = "printT2AddrModeImm8s4Operand<true>";
293}
294
295def t2am_imm8s4_offset_asmoperand : AsmOperandClass { let Name = "Imm8s4"; }
296def t2am_imm8s4_offset : MemOperand {
297  let PrintMethod = "printT2AddrModeImm8s4OffsetOperand";
298  let EncoderMethod = "getT2ScaledImmOpValue<8,2>";
299  let DecoderMethod = "DecodeT2Imm8S4";
300}
301
302// t2addrmode_imm7s4  := reg +/- (imm7 << 2)
303def MemImm7s4OffsetAsmOperand : AsmOperandClass {let Name = "MemImm7s4Offset";}
304class T2AddrMode_Imm7s4 : MemOperand {
305  let EncoderMethod = "getT2AddrModeImm7s4OpValue";
306  let DecoderMethod = "DecodeT2AddrModeImm7<2,0>";
307  let ParserMatchClass = MemImm7s4OffsetAsmOperand;
308  let MIOperandInfo = (ops GPRnopc:$base, i32imm:$offsimm);
309}
310
311def t2addrmode_imm7s4 : T2AddrMode_Imm7s4 {
312  // They are printed the same way as the imm8 version
313  let PrintMethod = "printT2AddrModeImm8s4Operand<false>";
314}
315
316def t2addrmode_imm7s4_pre : T2AddrMode_Imm7s4 {
317  // They are printed the same way as the imm8 version
318  let PrintMethod = "printT2AddrModeImm8s4Operand<true>";
319}
320
321def t2am_imm7s4_offset_asmoperand : AsmOperandClass { let Name = "Imm7s4"; }
322def t2am_imm7s4_offset : MemOperand {
323  // They are printed the same way as the imm8 version
324  let PrintMethod = "printT2AddrModeImm8s4OffsetOperand";
325  let ParserMatchClass = t2am_imm7s4_offset_asmoperand;
326  let EncoderMethod = "getT2ScaledImmOpValue<7,2>";
327  let DecoderMethod = "DecodeT2Imm7S4";
328}
329
330// t2addrmode_imm0_1020s4  := reg + (imm8 << 2)
331def MemImm0_1020s4OffsetAsmOperand : AsmOperandClass {
332  let Name = "MemImm0_1020s4Offset";
333}
334def t2addrmode_imm0_1020s4 : MemOperand,
335                         ComplexPattern<i32, 2, "SelectT2AddrModeExclusive"> {
336  let PrintMethod = "printT2AddrModeImm0_1020s4Operand";
337  let EncoderMethod = "getT2AddrModeImm0_1020s4OpValue";
338  let DecoderMethod = "DecodeT2AddrModeImm0_1020s4";
339  let ParserMatchClass = MemImm0_1020s4OffsetAsmOperand;
340  let MIOperandInfo = (ops GPRnopc:$base, i32imm:$offsimm);
341}
342
343// t2addrmode_so_reg  := reg + (reg << imm2)
344def t2addrmode_so_reg_asmoperand : AsmOperandClass {let Name="T2MemRegOffset";}
345def t2addrmode_so_reg : MemOperand,
346                        ComplexPattern<i32, 3, "SelectT2AddrModeSoReg", []> {
347  let PrintMethod = "printT2AddrModeSoRegOperand";
348  let EncoderMethod = "getT2AddrModeSORegOpValue";
349  let DecoderMethod = "DecodeT2AddrModeSOReg";
350  let ParserMatchClass = t2addrmode_so_reg_asmoperand;
351  let MIOperandInfo = (ops GPRnopc:$base, rGPR:$offsreg, i32imm:$offsimm);
352}
353
354// Addresses for the TBB/TBH instructions.
355def addrmode_tbb_asmoperand : AsmOperandClass { let Name = "MemTBB"; }
356def addrmode_tbb : MemOperand {
357  let PrintMethod = "printAddrModeTBB";
358  let ParserMatchClass = addrmode_tbb_asmoperand;
359  let MIOperandInfo = (ops GPR:$Rn, rGPR:$Rm);
360}
361def addrmode_tbh_asmoperand : AsmOperandClass { let Name = "MemTBH"; }
362def addrmode_tbh : MemOperand {
363  let PrintMethod = "printAddrModeTBH";
364  let ParserMatchClass = addrmode_tbh_asmoperand;
365  let MIOperandInfo = (ops GPR:$Rn, rGPR:$Rm);
366}
367
368// Define ARMv8.1-M specific addressing modes.
369
370// Label operands for BF/BFL/WLS/DLS/LE
371class BFLabelOp<string signed, string isNeg, string zeroPermitted, string size,
372                string fixup>
373  : Operand<OtherVT> {
374  let EncoderMethod = !strconcat("getBFTargetOpValue<", isNeg, ", ",
375                                 fixup, ">");
376  let OperandType = "OPERAND_PCREL";
377  let DecoderMethod = !strconcat("DecodeBFLabelOperand<", signed, ", ",
378                                 isNeg, ", ", zeroPermitted, ", ", size, ">");
379}
380def bflabel_u4  : BFLabelOp<"false", "false", "false", "4",  "ARM::fixup_bf_branch">;
381def bflabel_s12 : BFLabelOp<"true",  "false", "true",  "12", "ARM::fixup_bfc_target">;
382def bflabel_s16 : BFLabelOp<"true",  "false", "true",  "16", "ARM::fixup_bf_target">;
383def bflabel_s18 : BFLabelOp<"true",  "false", "true",  "18", "ARM::fixup_bfl_target">;
384
385def wlslabel_u11_asmoperand : AsmOperandClass {
386  let Name = "WLSLabel";
387  let RenderMethod = "addImmOperands";
388  let PredicateMethod = "isUnsignedOffset<11, 1>";
389  let DiagnosticString =
390    "loop end is out of range or not a positive multiple of 2";
391}
392def wlslabel_u11 : BFLabelOp<"false", "false", "true",  "11", "ARM::fixup_wls"> {
393  let ParserMatchClass = wlslabel_u11_asmoperand;
394}
395def lelabel_u11_asmoperand : AsmOperandClass {
396  let Name = "LELabel";
397  let RenderMethod = "addImmOperands";
398  let PredicateMethod = "isLEOffset";
399  let DiagnosticString =
400    "loop start is out of range or not a negative multiple of 2";
401}
402def lelabel_u11 : BFLabelOp<"false", "true",  "true",  "11", "ARM::fixup_le"> {
403  let ParserMatchClass = lelabel_u11_asmoperand;
404}
405
406def bfafter_target : Operand<OtherVT> {
407    let EncoderMethod = "getBFAfterTargetOpValue";
408    let OperandType = "OPERAND_PCREL";
409    let DecoderMethod = "DecodeBFAfterTargetOperand";
410}
411
412// pred operand excluding AL
413def pred_noal_asmoperand : AsmOperandClass {
414  let Name = "CondCodeNoAL";
415  let RenderMethod = "addITCondCodeOperands";
416  let PredicateMethod = "isITCondCodeNoAL";
417  let ParserMethod = "parseITCondCode";
418}
419def pred_noal : Operand<i32> {
420  let PrintMethod = "printMandatoryPredicateOperand";
421  let ParserMatchClass = pred_noal_asmoperand;
422  let DecoderMethod = "DecodePredNoALOperand";
423}
424
425
426// CSEL aliases inverted predicate
427def pred_noal_inv_asmoperand : AsmOperandClass {
428  let Name = "CondCodeNoALInv";
429  let RenderMethod = "addITCondCodeInvOperands";
430  let PredicateMethod = "isITCondCodeNoAL";
431  let ParserMethod = "parseITCondCode";
432}
433def pred_noal_inv : Operand<i32> {
434  let PrintMethod = "printMandatoryInvertedPredicateOperand";
435  let ParserMatchClass = pred_noal_inv_asmoperand;
436}
437//===----------------------------------------------------------------------===//
438// Multiclass helpers...
439//
440
441
442class T2OneRegImm<dag oops, dag iops, InstrItinClass itin,
443           string opc, string asm, list<dag> pattern>
444  : T2I<oops, iops, itin, opc, asm, pattern> {
445  bits<4> Rd;
446  bits<12> imm;
447
448  let Inst{11-8}  = Rd;
449  let Inst{26}    = imm{11};
450  let Inst{14-12} = imm{10-8};
451  let Inst{7-0}   = imm{7-0};
452}
453
454
455class T2sOneRegImm<dag oops, dag iops, InstrItinClass itin,
456           string opc, string asm, list<dag> pattern>
457  : T2sI<oops, iops, itin, opc, asm, pattern> {
458  bits<4> Rd;
459  bits<4> Rn;
460  bits<12> imm;
461
462  let Inst{11-8}  = Rd;
463  let Inst{26}    = imm{11};
464  let Inst{14-12} = imm{10-8};
465  let Inst{7-0}   = imm{7-0};
466}
467
468class T2OneRegCmpImm<dag oops, dag iops, InstrItinClass itin,
469           string opc, string asm, list<dag> pattern>
470  : T2I<oops, iops, itin, opc, asm, pattern> {
471  bits<4> Rn;
472  bits<12> imm;
473
474  let Inst{19-16}  = Rn;
475  let Inst{26}    = imm{11};
476  let Inst{14-12} = imm{10-8};
477  let Inst{7-0}   = imm{7-0};
478}
479
480
481class T2OneRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
482           string opc, string asm, list<dag> pattern>
483  : T2I<oops, iops, itin, opc, asm, pattern> {
484  bits<4> Rd;
485  bits<12> ShiftedRm;
486
487  let Inst{11-8}  = Rd;
488  let Inst{3-0}   = ShiftedRm{3-0};
489  let Inst{5-4}   = ShiftedRm{6-5};
490  let Inst{14-12} = ShiftedRm{11-9};
491  let Inst{7-6}   = ShiftedRm{8-7};
492}
493
494class T2sOneRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
495           string opc, string asm, list<dag> pattern>
496  : T2sI<oops, iops, itin, opc, asm, pattern> {
497  bits<4> Rd;
498  bits<12> ShiftedRm;
499
500  let Inst{11-8}  = Rd;
501  let Inst{3-0}   = ShiftedRm{3-0};
502  let Inst{5-4}   = ShiftedRm{6-5};
503  let Inst{14-12} = ShiftedRm{11-9};
504  let Inst{7-6}   = ShiftedRm{8-7};
505}
506
507class T2OneRegCmpShiftedReg<dag oops, dag iops, InstrItinClass itin,
508           string opc, string asm, list<dag> pattern>
509  : T2I<oops, iops, itin, opc, asm, pattern> {
510  bits<4> Rn;
511  bits<12> ShiftedRm;
512
513  let Inst{19-16} = Rn;
514  let Inst{3-0}   = ShiftedRm{3-0};
515  let Inst{5-4}   = ShiftedRm{6-5};
516  let Inst{14-12} = ShiftedRm{11-9};
517  let Inst{7-6}   = ShiftedRm{8-7};
518}
519
520class T2TwoReg<dag oops, dag iops, InstrItinClass itin,
521           string opc, string asm, list<dag> pattern>
522  : T2I<oops, iops, itin, opc, asm, pattern> {
523  bits<4> Rd;
524  bits<4> Rm;
525
526  let Inst{11-8}  = Rd;
527  let Inst{3-0}   = Rm;
528}
529
530class T2sTwoReg<dag oops, dag iops, InstrItinClass itin,
531           string opc, string asm, list<dag> pattern>
532  : T2sI<oops, iops, itin, opc, asm, pattern> {
533  bits<4> Rd;
534  bits<4> Rm;
535
536  let Inst{11-8}  = Rd;
537  let Inst{3-0}   = Rm;
538}
539
540class T2TwoRegCmp<dag oops, dag iops, InstrItinClass itin,
541           string opc, string asm, list<dag> pattern>
542  : T2I<oops, iops, itin, opc, asm, pattern> {
543  bits<4> Rn;
544  bits<4> Rm;
545
546  let Inst{19-16} = Rn;
547  let Inst{3-0}   = Rm;
548}
549
550
551class T2TwoRegImm<dag oops, dag iops, InstrItinClass itin,
552           string opc, string asm, list<dag> pattern>
553  : T2I<oops, iops, itin, opc, asm, pattern> {
554  bits<4> Rd;
555  bits<4> Rn;
556  bits<12> imm;
557
558  let Inst{11-8}  = Rd;
559  let Inst{19-16} = Rn;
560  let Inst{26}    = imm{11};
561  let Inst{14-12} = imm{10-8};
562  let Inst{7-0}   = imm{7-0};
563}
564
565class T2sTwoRegImm<dag oops, dag iops, InstrItinClass itin,
566           string opc, string asm, list<dag> pattern>
567  : T2sI<oops, iops, itin, opc, asm, pattern> {
568  bits<4> Rd;
569  bits<4> Rn;
570  bits<12> imm;
571
572  let Inst{11-8}  = Rd;
573  let Inst{19-16} = Rn;
574  let Inst{26}    = imm{11};
575  let Inst{14-12} = imm{10-8};
576  let Inst{7-0}   = imm{7-0};
577}
578
579class T2TwoRegShiftImm<dag oops, dag iops, InstrItinClass itin,
580           string opc, string asm, list<dag> pattern>
581  : T2I<oops, iops, itin, opc, asm, pattern> {
582  bits<4> Rd;
583  bits<4> Rm;
584  bits<5> imm;
585
586  let Inst{11-8}  = Rd;
587  let Inst{3-0}   = Rm;
588  let Inst{14-12} = imm{4-2};
589  let Inst{7-6}   = imm{1-0};
590}
591
592class T2sTwoRegShiftImm<dag oops, dag iops, InstrItinClass itin,
593           string opc, string asm, list<dag> pattern>
594  : T2sI<oops, iops, itin, opc, asm, pattern> {
595  bits<4> Rd;
596  bits<4> Rm;
597  bits<5> imm;
598
599  let Inst{11-8}  = Rd;
600  let Inst{3-0}   = Rm;
601  let Inst{14-12} = imm{4-2};
602  let Inst{7-6}   = imm{1-0};
603}
604
605class T2ThreeReg<dag oops, dag iops, InstrItinClass itin,
606           string opc, string asm, list<dag> pattern>
607  : T2I<oops, iops, itin, opc, asm, pattern> {
608  bits<4> Rd;
609  bits<4> Rn;
610  bits<4> Rm;
611
612  let Inst{11-8}  = Rd;
613  let Inst{19-16} = Rn;
614  let Inst{3-0}   = Rm;
615}
616
617class T2ThreeRegNoP<dag oops, dag iops, InstrItinClass itin,
618           string asm, list<dag> pattern>
619  : T2XI<oops, iops, itin, asm, pattern> {
620  bits<4> Rd;
621  bits<4> Rn;
622  bits<4> Rm;
623
624  let Inst{11-8}  = Rd;
625  let Inst{19-16} = Rn;
626  let Inst{3-0}   = Rm;
627}
628
629class T2sThreeReg<dag oops, dag iops, InstrItinClass itin,
630           string opc, string asm, list<dag> pattern>
631  : T2sI<oops, iops, itin, opc, asm, pattern> {
632  bits<4> Rd;
633  bits<4> Rn;
634  bits<4> Rm;
635
636  let Inst{11-8}  = Rd;
637  let Inst{19-16} = Rn;
638  let Inst{3-0}   = Rm;
639}
640
641class T2TwoRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
642           string opc, string asm, list<dag> pattern>
643  : T2I<oops, iops, itin, opc, asm, pattern> {
644  bits<4> Rd;
645  bits<4> Rn;
646  bits<12> ShiftedRm;
647
648  let Inst{11-8}  = Rd;
649  let Inst{19-16} = Rn;
650  let Inst{3-0}   = ShiftedRm{3-0};
651  let Inst{5-4}   = ShiftedRm{6-5};
652  let Inst{14-12} = ShiftedRm{11-9};
653  let Inst{7-6}   = ShiftedRm{8-7};
654}
655
656class T2sTwoRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
657           string opc, string asm, list<dag> pattern>
658  : T2sI<oops, iops, itin, opc, asm, pattern> {
659  bits<4> Rd;
660  bits<4> Rn;
661  bits<12> ShiftedRm;
662
663  let Inst{11-8}  = Rd;
664  let Inst{19-16} = Rn;
665  let Inst{3-0}   = ShiftedRm{3-0};
666  let Inst{5-4}   = ShiftedRm{6-5};
667  let Inst{14-12} = ShiftedRm{11-9};
668  let Inst{7-6}   = ShiftedRm{8-7};
669}
670
671class T2FourReg<dag oops, dag iops, InstrItinClass itin,
672           string opc, string asm, list<dag> pattern>
673  : T2I<oops, iops, itin, opc, asm, pattern> {
674  bits<4> Rd;
675  bits<4> Rn;
676  bits<4> Rm;
677  bits<4> Ra;
678
679  let Inst{19-16} = Rn;
680  let Inst{15-12} = Ra;
681  let Inst{11-8}  = Rd;
682  let Inst{3-0}   = Rm;
683}
684
685class T2MulLong<bits<3> opc22_20, bits<4> opc7_4,
686                string opc, list<dag> pattern>
687  : T2I<(outs rGPR:$RdLo, rGPR:$RdHi), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL64,
688         opc, "\t$RdLo, $RdHi, $Rn, $Rm", pattern>,
689    Sched<[WriteMUL64Lo, WriteMUL64Hi, ReadMUL, ReadMUL]> {
690  bits<4> RdLo;
691  bits<4> RdHi;
692  bits<4> Rn;
693  bits<4> Rm;
694
695  let Inst{31-23} = 0b111110111;
696  let Inst{22-20} = opc22_20;
697  let Inst{19-16} = Rn;
698  let Inst{15-12} = RdLo;
699  let Inst{11-8}  = RdHi;
700  let Inst{7-4}   = opc7_4;
701  let Inst{3-0}   = Rm;
702}
703class T2MlaLong<bits<3> opc22_20, bits<4> opc7_4, string opc>
704  : T2I<(outs rGPR:$RdLo, rGPR:$RdHi),
705        (ins rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi), IIC_iMAC64,
706        opc, "\t$RdLo, $RdHi, $Rn, $Rm", []>,
707        RegConstraint<"$RLo = $RdLo, $RHi = $RdHi">,
708    Sched<[WriteMAC64Lo, WriteMAC64Hi, ReadMUL, ReadMUL, ReadMAC, ReadMAC]> {
709  bits<4> RdLo;
710  bits<4> RdHi;
711  bits<4> Rn;
712  bits<4> Rm;
713
714  let Inst{31-23} = 0b111110111;
715  let Inst{22-20} = opc22_20;
716  let Inst{19-16} = Rn;
717  let Inst{15-12} = RdLo;
718  let Inst{11-8}  = RdHi;
719  let Inst{7-4}   = opc7_4;
720  let Inst{3-0}   = Rm;
721}
722
723
724/// T2I_bin_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns for a
725/// binary operation that produces a value. These are predicable and can be
726/// changed to modify CPSR.
727multiclass T2I_bin_irs<bits<4> opcod, string opc,
728                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
729                     SDPatternOperator opnode, bit Commutable = 0,
730                     string wide = ""> {
731   // shifted imm
732   def ri : T2sTwoRegImm<
733                (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm), iii,
734                 opc, "\t$Rd, $Rn, $imm",
735                 [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_imm:$imm))]>,
736                 Sched<[WriteALU, ReadALU]> {
737     let Inst{31-27} = 0b11110;
738     let Inst{25} = 0;
739     let Inst{24-21} = opcod;
740     let Inst{15} = 0;
741   }
742   // register
743   def rr : T2sThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), iir,
744                 opc, !strconcat(wide, "\t$Rd, $Rn, $Rm"),
745                 [(set rGPR:$Rd, (opnode rGPR:$Rn, rGPR:$Rm))]>,
746                 Sched<[WriteALU, ReadALU, ReadALU]> {
747     let isCommutable = Commutable;
748     let Inst{31-27} = 0b11101;
749     let Inst{26-25} = 0b01;
750     let Inst{24-21} = opcod;
751     let Inst{15} = 0b0;
752     // In most of these instructions, and most versions of the Arm
753     // architecture, bit 15 of this encoding is listed as (0) rather
754     // than 0, i.e. setting it to 1 is UNPREDICTABLE or a soft-fail
755     // rather than a hard failure. In v8.1-M, this requirement is
756     // upgraded to a hard one for ORR, so that the encodings with 1
757     // in this bit can be reused for other instructions (such as
758     // CSEL). Setting Unpredictable{15} = 1 here would reintroduce
759     // that encoding clash in the auto- generated MC decoder, so I
760     // comment it out.
761     let Unpredictable{15} = !if(!eq(opcod, 0b0010), 0b0, 0b1);
762     let Inst{14-12} = 0b000; // imm3
763     let Inst{7-6} = 0b00; // imm2
764     let Inst{5-4} = 0b00; // type
765   }
766   // shifted register
767   def rs : T2sTwoRegShiftedReg<
768                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm), iis,
769                 opc, !strconcat(wide, "\t$Rd, $Rn, $ShiftedRm"),
770                 [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_reg:$ShiftedRm))]>,
771                 Sched<[WriteALUsi, ReadALU]>  {
772     let Inst{31-27} = 0b11101;
773     let Inst{26-25} = 0b01;
774     let Inst{24-21} = opcod;
775     let Inst{15} = 0;
776     let Unpredictable{15} = !if(!eq(opcod, 0b0010), 0b0, 0b1); // see above
777   }
778  // Assembly aliases for optional destination operand when it's the same
779  // as the source operand.
780  def : t2InstAlias<!strconcat(opc, "${s}${p} $Rdn, $imm"),
781     (!cast<Instruction>(NAME#"ri") rGPR:$Rdn, rGPR:$Rdn,
782                                                    t2_so_imm:$imm, pred:$p,
783                                                    cc_out:$s)>;
784  def : t2InstAlias<!strconcat(opc, "${s}${p}", wide, " $Rdn, $Rm"),
785     (!cast<Instruction>(NAME#"rr") rGPR:$Rdn, rGPR:$Rdn,
786                                                    rGPR:$Rm, pred:$p,
787                                                    cc_out:$s)>;
788  def : t2InstAlias<!strconcat(opc, "${s}${p}", wide, " $Rdn, $shift"),
789     (!cast<Instruction>(NAME#"rs") rGPR:$Rdn, rGPR:$Rdn,
790                                                    t2_so_reg:$shift, pred:$p,
791                                                    cc_out:$s)>;
792}
793
794/// T2I_bin_w_irs - Same as T2I_bin_irs except these operations need
795//  the ".w" suffix to indicate that they are wide.
796multiclass T2I_bin_w_irs<bits<4> opcod, string opc,
797                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
798                     SDPatternOperator opnode, bit Commutable = 0> :
799    T2I_bin_irs<opcod, opc, iii, iir, iis, opnode, Commutable, ".w"> {
800  // Assembler aliases w/ the ".w" suffix.
801  def : t2InstAlias<!strconcat(opc, "${s}${p}.w", " $Rd, $Rn, $imm"),
802     (!cast<Instruction>(NAME#"ri") rGPR:$Rd, rGPR:$Rn, t2_so_imm:$imm, pred:$p,
803                                    cc_out:$s)>;
804  // Assembler aliases w/o the ".w" suffix.
805  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $Rm"),
806     (!cast<Instruction>(NAME#"rr") rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p,
807                                    cc_out:$s)>;
808  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $shift"),
809     (!cast<Instruction>(NAME#"rs") rGPR:$Rd, rGPR:$Rn, t2_so_reg:$shift,
810                                    pred:$p, cc_out:$s)>;
811
812  // and with the optional destination operand, too.
813  def : t2InstAlias<!strconcat(opc, "${s}${p}.w", " $Rdn, $imm"),
814     (!cast<Instruction>(NAME#"ri") rGPR:$Rdn, rGPR:$Rdn, t2_so_imm:$imm,
815                                    pred:$p, cc_out:$s)>;
816  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $Rm"),
817     (!cast<Instruction>(NAME#"rr") rGPR:$Rdn, rGPR:$Rdn, rGPR:$Rm, pred:$p,
818                                    cc_out:$s)>;
819  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $shift"),
820     (!cast<Instruction>(NAME#"rs") rGPR:$Rdn, rGPR:$Rdn, t2_so_reg:$shift,
821                                    pred:$p, cc_out:$s)>;
822}
823
824/// T2I_rbin_is - Same as T2I_bin_irs except the order of operands are
825/// reversed.  The 'rr' form is only defined for the disassembler; for codegen
826/// it is equivalent to the T2I_bin_irs counterpart.
827multiclass T2I_rbin_irs<bits<4> opcod, string opc, SDNode opnode> {
828   // shifted imm
829   def ri : T2sTwoRegImm<
830                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm), IIC_iALUi,
831                 opc, ".w\t$Rd, $Rn, $imm",
832                 [(set rGPR:$Rd, (opnode t2_so_imm:$imm, rGPR:$Rn))]>,
833                 Sched<[WriteALU, ReadALU]> {
834     let Inst{31-27} = 0b11110;
835     let Inst{25} = 0;
836     let Inst{24-21} = opcod;
837     let Inst{15} = 0;
838   }
839   // register
840   def rr : T2sThreeReg<
841                 (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUr,
842                 opc, "\t$Rd, $Rn, $Rm",
843                 [/* For disassembly only; pattern left blank */]>,
844                 Sched<[WriteALU, ReadALU, ReadALU]> {
845     let Inst{31-27} = 0b11101;
846     let Inst{26-25} = 0b01;
847     let Inst{24-21} = opcod;
848     let Inst{14-12} = 0b000; // imm3
849     let Inst{7-6} = 0b00; // imm2
850     let Inst{5-4} = 0b00; // type
851   }
852   // shifted register
853   def rs : T2sTwoRegShiftedReg<
854                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm),
855                 IIC_iALUsir, opc, "\t$Rd, $Rn, $ShiftedRm",
856                 [(set rGPR:$Rd, (opnode t2_so_reg:$ShiftedRm, rGPR:$Rn))]>,
857                 Sched<[WriteALUsi, ReadALU]> {
858     let Inst{31-27} = 0b11101;
859     let Inst{26-25} = 0b01;
860     let Inst{24-21} = opcod;
861   }
862}
863
864/// T2I_bin_s_irs - Similar to T2I_bin_irs except it sets the 's' bit so the
865/// instruction modifies the CPSR register.
866///
867/// These opcodes will be converted to the real non-S opcodes by
868/// AdjustInstrPostInstrSelection after giving then an optional CPSR operand.
869let hasPostISelHook = 1, Defs = [CPSR] in {
870multiclass T2I_bin_s_irs<InstrItinClass iii, InstrItinClass iir,
871                         InstrItinClass iis, SDNode opnode,
872                         bit Commutable = 0> {
873   // shifted imm
874   def ri : t2PseudoInst<(outs rGPR:$Rd),
875                         (ins GPRnopc:$Rn, t2_so_imm:$imm, pred:$p),
876                         4, iii,
877                         [(set rGPR:$Rd, CPSR, (opnode GPRnopc:$Rn,
878                                                t2_so_imm:$imm))]>,
879            Sched<[WriteALU, ReadALU]>;
880   // register
881   def rr : t2PseudoInst<(outs rGPR:$Rd), (ins GPRnopc:$Rn, rGPR:$Rm, pred:$p),
882                         4, iir,
883                         [(set rGPR:$Rd, CPSR, (opnode GPRnopc:$Rn,
884                                                rGPR:$Rm))]>,
885            Sched<[WriteALU, ReadALU, ReadALU]> {
886     let isCommutable = Commutable;
887   }
888   // shifted register
889   def rs : t2PseudoInst<(outs rGPR:$Rd),
890                         (ins GPRnopc:$Rn, t2_so_reg:$ShiftedRm, pred:$p),
891                         4, iis,
892                         [(set rGPR:$Rd, CPSR, (opnode GPRnopc:$Rn,
893                                                t2_so_reg:$ShiftedRm))]>,
894            Sched<[WriteALUsi, ReadALUsr]>;
895}
896}
897
898/// T2I_rbin_s_is -  Same as T2I_bin_s_irs, except selection DAG
899/// operands are reversed.
900let hasPostISelHook = 1, Defs = [CPSR] in {
901multiclass T2I_rbin_s_is<SDNode opnode> {
902   // shifted imm
903   def ri : t2PseudoInst<(outs rGPR:$Rd),
904                         (ins rGPR:$Rn, t2_so_imm:$imm, pred:$p),
905                         4, IIC_iALUi,
906                         [(set rGPR:$Rd, CPSR, (opnode t2_so_imm:$imm,
907                                                rGPR:$Rn))]>,
908            Sched<[WriteALU, ReadALU]>;
909   // shifted register
910   def rs : t2PseudoInst<(outs rGPR:$Rd),
911                         (ins rGPR:$Rn, t2_so_reg:$ShiftedRm, pred:$p),
912                         4, IIC_iALUsi,
913                         [(set rGPR:$Rd, CPSR, (opnode t2_so_reg:$ShiftedRm,
914                                                rGPR:$Rn))]>,
915            Sched<[WriteALUsi, ReadALU]>;
916}
917}
918
919/// T2I_bin_ii12rs - Defines a set of (op reg, {so_imm|imm0_4095|r|so_reg})
920/// patterns for a binary operation that produces a value.
921multiclass T2I_bin_ii12rs<bits<3> op23_21, string opc, SDNode opnode,
922                          bit Commutable = 0> {
923   // shifted imm
924   // The register-immediate version is re-materializable. This is useful
925   // in particular for taking the address of a local.
926   let isReMaterializable = 1 in {
927    def spImm : T2sTwoRegImm<
928              (outs GPRsp:$Rd), (ins GPRsp:$Rn, t2_so_imm:$imm), IIC_iALUi,
929              opc, ".w\t$Rd, $Rn, $imm",
930              []>,
931              Sched<[WriteALU, ReadALU]> {
932    let  Rn = 13;
933    let  Rd = 13;
934
935    let Inst{31-27} = 0b11110;
936    let Inst{25-24} = 0b01;
937    let Inst{23-21} = op23_21;
938    let Inst{15}    = 0;
939
940    let DecoderMethod = "DecodeT2AddSubSPImm";
941   }
942
943   def ri : T2sTwoRegImm<
944               (outs rGPR:$Rd), (ins GPRnopc:$Rn, t2_so_imm:$imm), IIC_iALUi,
945               opc, ".w\t$Rd, $Rn, $imm",
946               [(set rGPR:$Rd, (opnode GPRnopc:$Rn, t2_so_imm:$imm))]>,
947               Sched<[WriteALU, ReadALU]> {
948     let Inst{31-27} = 0b11110;
949     let Inst{25} = 0;
950     let Inst{24} = 1;
951     let Inst{23-21} = op23_21;
952     let Inst{15} = 0;
953   }
954   }
955   // 12-bit imm
956   def ri12 : T2I<
957                  (outs rGPR:$Rd), (ins GPR:$Rn, imm0_4095:$imm), IIC_iALUi,
958                  !strconcat(opc, "w"), "\t$Rd, $Rn, $imm",
959                  [(set rGPR:$Rd, (opnode GPR:$Rn, imm0_4095:$imm))]>,
960                  Sched<[WriteALU, ReadALU]> {
961     bits<4> Rd;
962     bits<4> Rn;
963     bits<12> imm;
964     let Inst{31-27} = 0b11110;
965     let Inst{26} = imm{11};
966     let Inst{25-24} = 0b10;
967     let Inst{23-21} = op23_21;
968     let Inst{20} = 0; // The S bit.
969     let Inst{19-16} = Rn;
970     let Inst{15} = 0;
971     let Inst{14-12} = imm{10-8};
972     let Inst{11-8} = Rd;
973     let Inst{7-0} = imm{7-0};
974   }
975     def spImm12 : T2I<
976                    (outs GPRsp:$Rd), (ins GPRsp:$Rn, imm0_4095:$imm), IIC_iALUi,
977                    !strconcat(opc, "w"), "\t$Rd, $Rn, $imm",
978                    []>,
979                    Sched<[WriteALU, ReadALU]> {
980       bits<4> Rd = 13;
981       bits<4> Rn = 13;
982       bits<12> imm;
983       let Inst{31-27} = 0b11110;
984       let Inst{26} = imm{11};
985       let Inst{25-24} = 0b10;
986       let Inst{23-21} = op23_21;
987       let Inst{20} = 0; // The S bit.
988       let Inst{19-16} = Rn;
989       let Inst{15} = 0;
990       let Inst{14-12} = imm{10-8};
991       let Inst{11-8} = Rd;
992       let Inst{7-0} = imm{7-0};
993       let DecoderMethod = "DecodeT2AddSubSPImm";
994     }
995   // register
996   def rr : T2sThreeReg<(outs GPRnopc:$Rd), (ins GPRnopc:$Rn, rGPR:$Rm),
997                 IIC_iALUr, opc, ".w\t$Rd, $Rn, $Rm",
998                 [(set GPRnopc:$Rd, (opnode GPRnopc:$Rn, rGPR:$Rm))]>,
999                 Sched<[WriteALU, ReadALU, ReadALU]> {
1000     let isCommutable = Commutable;
1001     let Inst{31-27} = 0b11101;
1002     let Inst{26-25} = 0b01;
1003     let Inst{24} = 1;
1004     let Inst{23-21} = op23_21;
1005     let Inst{14-12} = 0b000; // imm3
1006     let Inst{7-6} = 0b00; // imm2
1007     let Inst{5-4} = 0b00; // type
1008   }
1009   // shifted register
1010   def rs : T2sTwoRegShiftedReg<
1011                 (outs GPRnopc:$Rd), (ins GPRnopc:$Rn, t2_so_reg:$ShiftedRm),
1012                 IIC_iALUsi, opc, ".w\t$Rd, $Rn, $ShiftedRm",
1013              [(set GPRnopc:$Rd, (opnode GPRnopc:$Rn, t2_so_reg:$ShiftedRm))]>,
1014              Sched<[WriteALUsi, ReadALU]> {
1015     let Inst{31-27} = 0b11101;
1016     let Inst{26-25} = 0b01;
1017     let Inst{24} = 1;
1018     let Inst{23-21} = op23_21;
1019   }
1020}
1021
1022/// T2I_adde_sube_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns
1023/// for a binary operation that produces a value and use the carry
1024/// bit. It's not predicable.
1025let Defs = [CPSR], Uses = [CPSR] in {
1026multiclass T2I_adde_sube_irs<bits<4> opcod, string opc, SDNode opnode,
1027                             bit Commutable = 0> {
1028   // shifted imm
1029   def ri : T2sTwoRegImm<(outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm),
1030                 IIC_iALUi, opc, "\t$Rd, $Rn, $imm",
1031               [(set rGPR:$Rd, CPSR, (opnode rGPR:$Rn, t2_so_imm:$imm, CPSR))]>,
1032                 Requires<[IsThumb2]>, Sched<[WriteALU, ReadALU]> {
1033     let Inst{31-27} = 0b11110;
1034     let Inst{25} = 0;
1035     let Inst{24-21} = opcod;
1036     let Inst{15} = 0;
1037   }
1038   // register
1039   def rr : T2sThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUr,
1040                 opc, ".w\t$Rd, $Rn, $Rm",
1041                 [(set rGPR:$Rd, CPSR, (opnode rGPR:$Rn, rGPR:$Rm, CPSR))]>,
1042                 Requires<[IsThumb2]>, Sched<[WriteALU, ReadALU, ReadALU]> {
1043     let isCommutable = Commutable;
1044     let Inst{31-27} = 0b11101;
1045     let Inst{26-25} = 0b01;
1046     let Inst{24-21} = opcod;
1047     let Inst{14-12} = 0b000; // imm3
1048     let Inst{7-6} = 0b00; // imm2
1049     let Inst{5-4} = 0b00; // type
1050   }
1051   // shifted register
1052   def rs : T2sTwoRegShiftedReg<
1053                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm),
1054                 IIC_iALUsi, opc, ".w\t$Rd, $Rn, $ShiftedRm",
1055         [(set rGPR:$Rd, CPSR, (opnode rGPR:$Rn, t2_so_reg:$ShiftedRm, CPSR))]>,
1056                 Requires<[IsThumb2]>, Sched<[WriteALUsi, ReadALU]> {
1057     let Inst{31-27} = 0b11101;
1058     let Inst{26-25} = 0b01;
1059     let Inst{24-21} = opcod;
1060   }
1061}
1062}
1063
1064/// T2I_sh_ir - Defines a set of (op reg, {so_imm|r}) patterns for a shift /
1065//  rotate operation that produces a value.
1066multiclass T2I_sh_ir<bits<2> opcod, string opc, Operand ty, SDNode opnode> {
1067   // 5-bit imm
1068   def ri : T2sTwoRegShiftImm<
1069                 (outs rGPR:$Rd), (ins rGPR:$Rm, ty:$imm), IIC_iMOVsi,
1070                 opc, ".w\t$Rd, $Rm, $imm",
1071                 [(set rGPR:$Rd, (opnode rGPR:$Rm, (i32 ty:$imm)))]>,
1072                 Sched<[WriteALU]> {
1073     let Inst{31-27} = 0b11101;
1074     let Inst{26-21} = 0b010010;
1075     let Inst{19-16} = 0b1111; // Rn
1076     let Inst{15}    = 0b0;
1077     let Inst{5-4} = opcod;
1078   }
1079   // register
1080   def rr : T2sThreeReg<
1081                 (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMOVsr,
1082                 opc, ".w\t$Rd, $Rn, $Rm",
1083                 [(set rGPR:$Rd, (opnode rGPR:$Rn, rGPR:$Rm))]>,
1084                 Sched<[WriteALU]> {
1085     let Inst{31-27} = 0b11111;
1086     let Inst{26-23} = 0b0100;
1087     let Inst{22-21} = opcod;
1088     let Inst{15-12} = 0b1111;
1089     let Inst{7-4} = 0b0000;
1090   }
1091
1092  // Optional destination register
1093  def : t2InstAlias<!strconcat(opc, "${s}${p}", ".w $Rdn, $imm"),
1094     (!cast<Instruction>(NAME#"ri") rGPR:$Rdn, rGPR:$Rdn, ty:$imm, pred:$p,
1095                                    cc_out:$s)>;
1096  def : t2InstAlias<!strconcat(opc, "${s}${p}", ".w $Rdn, $Rm"),
1097     (!cast<Instruction>(NAME#"rr") rGPR:$Rdn, rGPR:$Rdn, rGPR:$Rm, pred:$p,
1098                                    cc_out:$s)>;
1099
1100  // Assembler aliases w/o the ".w" suffix.
1101  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $imm"),
1102     (!cast<Instruction>(NAME#"ri") rGPR:$Rd, rGPR:$Rn, ty:$imm, pred:$p,
1103                                    cc_out:$s)>;
1104  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $Rm"),
1105     (!cast<Instruction>(NAME#"rr") rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p,
1106                                    cc_out:$s)>;
1107
1108  // and with the optional destination operand, too.
1109  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $imm"),
1110     (!cast<Instruction>(NAME#"ri") rGPR:$Rdn, rGPR:$Rdn, ty:$imm, pred:$p,
1111                                    cc_out:$s)>;
1112  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $Rm"),
1113     (!cast<Instruction>(NAME#"rr") rGPR:$Rdn, rGPR:$Rdn, rGPR:$Rm, pred:$p,
1114                                    cc_out:$s)>;
1115}
1116
1117/// T2I_cmp_irs - Defines a set of (op r, {so_imm|r|so_reg}) cmp / test
1118/// patterns. Similar to T2I_bin_irs except the instruction does not produce
1119/// a explicit result, only implicitly set CPSR.
1120multiclass T2I_cmp_irs<bits<4> opcod, string opc, RegisterClass LHSGPR,
1121                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
1122                     SDPatternOperator opnode> {
1123let isCompare = 1, Defs = [CPSR] in {
1124   // shifted imm
1125   def ri : T2OneRegCmpImm<
1126                (outs), (ins LHSGPR:$Rn, t2_so_imm:$imm), iii,
1127                opc, ".w\t$Rn, $imm",
1128                [(opnode LHSGPR:$Rn, t2_so_imm:$imm)]>, Sched<[WriteCMP]> {
1129     let Inst{31-27} = 0b11110;
1130     let Inst{25} = 0;
1131     let Inst{24-21} = opcod;
1132     let Inst{20} = 1; // The S bit.
1133     let Inst{15} = 0;
1134     let Inst{11-8} = 0b1111; // Rd
1135   }
1136   // register
1137   def rr : T2TwoRegCmp<
1138                (outs), (ins LHSGPR:$Rn, rGPR:$Rm), iir,
1139                opc, ".w\t$Rn, $Rm",
1140                [(opnode LHSGPR:$Rn, rGPR:$Rm)]>, Sched<[WriteCMP]> {
1141     let Inst{31-27} = 0b11101;
1142     let Inst{26-25} = 0b01;
1143     let Inst{24-21} = opcod;
1144     let Inst{20} = 1; // The S bit.
1145     let Inst{14-12} = 0b000; // imm3
1146     let Inst{11-8} = 0b1111; // Rd
1147     let Inst{7-6} = 0b00; // imm2
1148     let Inst{5-4} = 0b00; // type
1149   }
1150   // shifted register
1151   def rs : T2OneRegCmpShiftedReg<
1152                (outs), (ins LHSGPR:$Rn, t2_so_reg:$ShiftedRm), iis,
1153                opc, ".w\t$Rn, $ShiftedRm",
1154                [(opnode LHSGPR:$Rn, t2_so_reg:$ShiftedRm)]>,
1155                Sched<[WriteCMPsi]> {
1156     let Inst{31-27} = 0b11101;
1157     let Inst{26-25} = 0b01;
1158     let Inst{24-21} = opcod;
1159     let Inst{20} = 1; // The S bit.
1160     let Inst{11-8} = 0b1111; // Rd
1161   }
1162}
1163
1164  // Assembler aliases w/o the ".w" suffix.
1165  // No alias here for 'rr' version as not all instantiations of this
1166  // multiclass want one (CMP in particular, does not).
1167  def : t2InstAlias<!strconcat(opc, "${p}", " $Rn, $imm"),
1168     (!cast<Instruction>(NAME#"ri") LHSGPR:$Rn, t2_so_imm:$imm, pred:$p)>;
1169  def : t2InstAlias<!strconcat(opc, "${p}", " $Rn, $shift"),
1170     (!cast<Instruction>(NAME#"rs") LHSGPR:$Rn, t2_so_reg:$shift, pred:$p)>;
1171}
1172
1173/// T2I_ld - Defines a set of (op r, {imm12|imm8|so_reg}) load patterns.
1174multiclass T2I_ld<bit signed, bits<2> opcod, string opc,
1175                  InstrItinClass iii, InstrItinClass iis, RegisterClass target,
1176                  PatFrag opnode> {
1177  def i12 : T2Ii12<(outs target:$Rt), (ins t2addrmode_imm12:$addr), iii,
1178                   opc, ".w\t$Rt, $addr",
1179                   [(set target:$Rt, (opnode t2addrmode_imm12:$addr))]>,
1180            Sched<[WriteLd]> {
1181    bits<4> Rt;
1182    bits<17> addr;
1183    let Inst{31-25} = 0b1111100;
1184    let Inst{24} = signed;
1185    let Inst{23} = 1;
1186    let Inst{22-21} = opcod;
1187    let Inst{20} = 1; // load
1188    let Inst{19-16} = addr{16-13}; // Rn
1189    let Inst{15-12} = Rt;
1190    let Inst{11-0}  = addr{11-0};  // imm
1191
1192    let DecoderMethod = "DecodeT2LoadImm12";
1193  }
1194  def i8  : T2Ii8n <(outs target:$Rt), (ins t2addrmode_negimm8:$addr), iii,
1195                    opc, "\t$Rt, $addr",
1196                    [(set target:$Rt, (opnode t2addrmode_negimm8:$addr))]>,
1197            Sched<[WriteLd]> {
1198    bits<4> Rt;
1199    bits<13> addr;
1200    let Inst{31-27} = 0b11111;
1201    let Inst{26-25} = 0b00;
1202    let Inst{24} = signed;
1203    let Inst{23} = 0;
1204    let Inst{22-21} = opcod;
1205    let Inst{20} = 1; // load
1206    let Inst{19-16} = addr{12-9}; // Rn
1207    let Inst{15-12} = Rt;
1208    let Inst{11} = 1;
1209    // Offset: index==TRUE, wback==FALSE
1210    let Inst{10} = 1; // The P bit.
1211    let Inst{9}     = addr{8};    // U
1212    let Inst{8} = 0; // The W bit.
1213    let Inst{7-0}   = addr{7-0};  // imm
1214
1215    let DecoderMethod = "DecodeT2LoadImm8";
1216  }
1217  def s   : T2Iso <(outs target:$Rt), (ins t2addrmode_so_reg:$addr), iis,
1218                   opc, ".w\t$Rt, $addr",
1219                   [(set target:$Rt, (opnode t2addrmode_so_reg:$addr))]>,
1220            Sched<[WriteLd]> {
1221    let Inst{31-27} = 0b11111;
1222    let Inst{26-25} = 0b00;
1223    let Inst{24} = signed;
1224    let Inst{23} = 0;
1225    let Inst{22-21} = opcod;
1226    let Inst{20} = 1; // load
1227    let Inst{11-6} = 0b000000;
1228
1229    bits<4> Rt;
1230    let Inst{15-12} = Rt;
1231
1232    bits<10> addr;
1233    let Inst{19-16} = addr{9-6}; // Rn
1234    let Inst{3-0}   = addr{5-2}; // Rm
1235    let Inst{5-4}   = addr{1-0}; // imm
1236
1237    let DecoderMethod = "DecodeT2LoadShift";
1238  }
1239
1240  // pci variant is very similar to i12, but supports negative offsets
1241  // from the PC.
1242  def pci : T2Ipc <(outs target:$Rt), (ins t2ldrlabel:$addr), iii,
1243                   opc, ".w\t$Rt, $addr",
1244                   [(set target:$Rt, (opnode (ARMWrapper tconstpool:$addr)))]>,
1245            Sched<[WriteLd]> {
1246    let isReMaterializable = 1;
1247    let Inst{31-27} = 0b11111;
1248    let Inst{26-25} = 0b00;
1249    let Inst{24} = signed;
1250    let Inst{22-21} = opcod;
1251    let Inst{20} = 1; // load
1252    let Inst{19-16} = 0b1111; // Rn
1253
1254    bits<4> Rt;
1255    let Inst{15-12} = Rt{3-0};
1256
1257    bits<13> addr;
1258    let Inst{23} = addr{12}; // add = (U == '1')
1259    let Inst{11-0}  = addr{11-0};
1260
1261    let DecoderMethod = "DecodeT2LoadLabel";
1262  }
1263}
1264
1265/// T2I_st - Defines a set of (op r, {imm12|imm8|so_reg}) store patterns.
1266multiclass T2I_st<bits<2> opcod, string opc,
1267                  InstrItinClass iii, InstrItinClass iis, RegisterClass target,
1268                  PatFrag opnode> {
1269  def i12 : T2Ii12<(outs), (ins target:$Rt, t2addrmode_imm12:$addr), iii,
1270                   opc, ".w\t$Rt, $addr",
1271                   [(opnode target:$Rt, t2addrmode_imm12:$addr)]>,
1272            Sched<[WriteST]> {
1273    let Inst{31-27} = 0b11111;
1274    let Inst{26-23} = 0b0001;
1275    let Inst{22-21} = opcod;
1276    let Inst{20} = 0; // !load
1277
1278    bits<4> Rt;
1279    let Inst{15-12} = Rt;
1280
1281    bits<17> addr;
1282    let addr{12}    = 1;           // add = TRUE
1283    let Inst{19-16} = addr{16-13}; // Rn
1284    let Inst{23}    = addr{12};    // U
1285    let Inst{11-0}  = addr{11-0};  // imm
1286  }
1287  def i8  : T2Ii8n <(outs), (ins target:$Rt, t2addrmode_negimm8:$addr), iii,
1288                    opc, "\t$Rt, $addr",
1289                    [(opnode target:$Rt, t2addrmode_negimm8:$addr)]>,
1290            Sched<[WriteST]> {
1291    let Inst{31-27} = 0b11111;
1292    let Inst{26-23} = 0b0000;
1293    let Inst{22-21} = opcod;
1294    let Inst{20} = 0; // !load
1295    let Inst{11} = 1;
1296    // Offset: index==TRUE, wback==FALSE
1297    let Inst{10} = 1; // The P bit.
1298    let Inst{8} = 0; // The W bit.
1299
1300    bits<4> Rt;
1301    let Inst{15-12} = Rt;
1302
1303    bits<13> addr;
1304    let Inst{19-16} = addr{12-9}; // Rn
1305    let Inst{9}     = addr{8};    // U
1306    let Inst{7-0}   = addr{7-0};  // imm
1307  }
1308  def s   : T2Iso <(outs), (ins target:$Rt, t2addrmode_so_reg:$addr), iis,
1309                   opc, ".w\t$Rt, $addr",
1310                   [(opnode target:$Rt, t2addrmode_so_reg:$addr)]>,
1311            Sched<[WriteST]> {
1312    let Inst{31-27} = 0b11111;
1313    let Inst{26-23} = 0b0000;
1314    let Inst{22-21} = opcod;
1315    let Inst{20} = 0; // !load
1316    let Inst{11-6} = 0b000000;
1317
1318    bits<4> Rt;
1319    let Inst{15-12} = Rt;
1320
1321    bits<10> addr;
1322    let Inst{19-16}   = addr{9-6}; // Rn
1323    let Inst{3-0} = addr{5-2}; // Rm
1324    let Inst{5-4}   = addr{1-0}; // imm
1325  }
1326}
1327
1328/// T2I_ext_rrot - A unary operation with two forms: one whose operand is a
1329/// register and one whose operand is a register rotated by 8/16/24.
1330class T2I_ext_rrot_base<bits<3> opcod, dag iops, dag oops,
1331                        string opc, string oprs,
1332                        list<dag> pattern>
1333  : T2TwoReg<iops, oops, IIC_iEXTr, opc, oprs, pattern> {
1334  bits<2> rot;
1335  let Inst{31-27} = 0b11111;
1336  let Inst{26-23} = 0b0100;
1337  let Inst{22-20} = opcod;
1338  let Inst{19-16} = 0b1111; // Rn
1339  let Inst{15-12} = 0b1111;
1340  let Inst{7} = 1;
1341  let Inst{5-4} = rot; // rotate
1342}
1343
1344class T2I_ext_rrot<bits<3> opcod, string opc>
1345  : T2I_ext_rrot_base<opcod,
1346                      (outs rGPR:$Rd),
1347                      (ins rGPR:$Rm, rot_imm:$rot),
1348                      opc, ".w\t$Rd, $Rm$rot", []>,
1349                      Requires<[IsThumb2]>,
1350                      Sched<[WriteALU, ReadALU]>;
1351
1352// UXTB16, SXTB16 - Requires HasDSP, does not need the .w qualifier.
1353class T2I_ext_rrot_xtb16<bits<3> opcod, string opc>
1354  : T2I_ext_rrot_base<opcod,
1355                      (outs rGPR:$Rd),
1356                      (ins rGPR:$Rm, rot_imm:$rot),
1357                      opc, "\t$Rd, $Rm$rot", []>,
1358                      Requires<[HasDSP, IsThumb2]>,
1359                      Sched<[WriteALU, ReadALU]>;
1360
1361/// T2I_exta_rrot - A binary operation with two forms: one whose operand is a
1362/// register and one whose operand is a register rotated by 8/16/24.
1363class T2I_exta_rrot<bits<3> opcod, string opc>
1364  : T2ThreeReg<(outs rGPR:$Rd),
1365               (ins rGPR:$Rn, rGPR:$Rm, rot_imm:$rot),
1366               IIC_iEXTAsr, opc, "\t$Rd, $Rn, $Rm$rot", []>,
1367               Requires<[HasDSP, IsThumb2]>,
1368               Sched<[WriteALU, ReadALU]> {
1369  bits<2> rot;
1370  let Inst{31-27} = 0b11111;
1371  let Inst{26-23} = 0b0100;
1372  let Inst{22-20} = opcod;
1373  let Inst{15-12} = 0b1111;
1374  let Inst{7} = 1;
1375  let Inst{5-4} = rot;
1376}
1377
1378//===----------------------------------------------------------------------===//
1379// Instructions
1380//===----------------------------------------------------------------------===//
1381
1382//===----------------------------------------------------------------------===//
1383//  Miscellaneous Instructions.
1384//
1385
1386class T2PCOneRegImm<dag oops, dag iops, InstrItinClass itin,
1387           string asm, list<dag> pattern>
1388  : T2XI<oops, iops, itin, asm, pattern> {
1389  bits<4> Rd;
1390  bits<12> label;
1391
1392  let Inst{11-8}  = Rd;
1393  let Inst{26}    = label{11};
1394  let Inst{14-12} = label{10-8};
1395  let Inst{7-0}   = label{7-0};
1396}
1397
1398// LEApcrel - Load a pc-relative address into a register without offending the
1399// assembler.
1400def t2ADR : T2PCOneRegImm<(outs rGPR:$Rd),
1401              (ins t2adrlabel:$addr, pred:$p),
1402              IIC_iALUi, "adr{$p}.w\t$Rd, $addr", []>,
1403              Sched<[WriteALU, ReadALU]> {
1404  let Inst{31-27} = 0b11110;
1405  let Inst{25-24} = 0b10;
1406  // Inst{23:21} = '11' (add = FALSE) or '00' (add = TRUE)
1407  let Inst{22} = 0;
1408  let Inst{20} = 0;
1409  let Inst{19-16} = 0b1111; // Rn
1410  let Inst{15} = 0;
1411
1412  bits<4> Rd;
1413  bits<13> addr;
1414  let Inst{11-8} = Rd;
1415  let Inst{23}    = addr{12};
1416  let Inst{21}    = addr{12};
1417  let Inst{26}    = addr{11};
1418  let Inst{14-12} = addr{10-8};
1419  let Inst{7-0}   = addr{7-0};
1420
1421  let DecoderMethod = "DecodeT2Adr";
1422}
1423
1424let hasSideEffects = 0, isReMaterializable = 1 in
1425def t2LEApcrel   : t2PseudoInst<(outs rGPR:$Rd), (ins i32imm:$label, pred:$p),
1426                                4, IIC_iALUi, []>, Sched<[WriteALU, ReadALU]>;
1427let hasSideEffects = 1 in
1428def t2LEApcrelJT : t2PseudoInst<(outs rGPR:$Rd),
1429                                (ins i32imm:$label, pred:$p),
1430                                4, IIC_iALUi,
1431                                []>, Sched<[WriteALU, ReadALU]>;
1432
1433
1434//===----------------------------------------------------------------------===//
1435//  Load / store Instructions.
1436//
1437
1438// Load
1439let canFoldAsLoad = 1, isReMaterializable = 1  in
1440defm t2LDR   : T2I_ld<0, 0b10, "ldr", IIC_iLoad_i, IIC_iLoad_si, GPR, load>;
1441
1442// Loads with zero extension
1443defm t2LDRH  : T2I_ld<0, 0b01, "ldrh", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1444                      GPRnopc, zextloadi16>;
1445defm t2LDRB  : T2I_ld<0, 0b00, "ldrb", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1446                      GPRnopc, zextloadi8>;
1447
1448// Loads with sign extension
1449defm t2LDRSH : T2I_ld<1, 0b01, "ldrsh", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1450                      GPRnopc, sextloadi16>;
1451defm t2LDRSB : T2I_ld<1, 0b00, "ldrsb", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1452                      GPRnopc, sextloadi8>;
1453
1454let mayLoad = 1, hasSideEffects = 0, hasExtraDefRegAllocReq = 1 in {
1455// Load doubleword
1456def t2LDRDi8  : T2Ii8s4<1, 0, 1, (outs rGPR:$Rt, rGPR:$Rt2),
1457                        (ins t2addrmode_imm8s4:$addr),
1458                        IIC_iLoad_d_i, "ldrd", "\t$Rt, $Rt2, $addr", "",
1459                        [(set rGPR:$Rt, rGPR:$Rt2, (ARMldrd t2addrmode_imm8s4:$addr))]>,
1460                 Sched<[WriteLd]>;
1461} // mayLoad = 1, hasSideEffects = 0, hasExtraDefRegAllocReq = 1
1462
1463// zextload i1 -> zextload i8
1464def : T2Pat<(zextloadi1 t2addrmode_imm12:$addr),
1465            (t2LDRBi12  t2addrmode_imm12:$addr)>;
1466def : T2Pat<(zextloadi1 t2addrmode_negimm8:$addr),
1467            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
1468def : T2Pat<(zextloadi1 t2addrmode_so_reg:$addr),
1469            (t2LDRBs    t2addrmode_so_reg:$addr)>;
1470def : T2Pat<(zextloadi1 (ARMWrapper tconstpool:$addr)),
1471            (t2LDRBpci  tconstpool:$addr)>;
1472
1473// extload -> zextload
1474// FIXME: Reduce the number of patterns by legalizing extload to zextload
1475// earlier?
1476def : T2Pat<(extloadi1  t2addrmode_imm12:$addr),
1477            (t2LDRBi12  t2addrmode_imm12:$addr)>;
1478def : T2Pat<(extloadi1  t2addrmode_negimm8:$addr),
1479            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
1480def : T2Pat<(extloadi1  t2addrmode_so_reg:$addr),
1481            (t2LDRBs    t2addrmode_so_reg:$addr)>;
1482def : T2Pat<(extloadi1  (ARMWrapper tconstpool:$addr)),
1483            (t2LDRBpci  tconstpool:$addr)>;
1484
1485def : T2Pat<(extloadi8  t2addrmode_imm12:$addr),
1486            (t2LDRBi12  t2addrmode_imm12:$addr)>;
1487def : T2Pat<(extloadi8  t2addrmode_negimm8:$addr),
1488            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
1489def : T2Pat<(extloadi8  t2addrmode_so_reg:$addr),
1490            (t2LDRBs    t2addrmode_so_reg:$addr)>;
1491def : T2Pat<(extloadi8  (ARMWrapper tconstpool:$addr)),
1492            (t2LDRBpci  tconstpool:$addr)>;
1493
1494def : T2Pat<(extloadi16 t2addrmode_imm12:$addr),
1495            (t2LDRHi12  t2addrmode_imm12:$addr)>;
1496def : T2Pat<(extloadi16 t2addrmode_negimm8:$addr),
1497            (t2LDRHi8   t2addrmode_negimm8:$addr)>;
1498def : T2Pat<(extloadi16 t2addrmode_so_reg:$addr),
1499            (t2LDRHs    t2addrmode_so_reg:$addr)>;
1500def : T2Pat<(extloadi16 (ARMWrapper tconstpool:$addr)),
1501            (t2LDRHpci  tconstpool:$addr)>;
1502
1503// FIXME: The destination register of the loads and stores can't be PC, but
1504//        can be SP. We need another regclass (similar to rGPR) to represent
1505//        that. Not a pressing issue since these are selected manually,
1506//        not via pattern.
1507
1508// Indexed loads
1509
1510let mayLoad = 1, hasSideEffects = 0 in {
1511def t2LDR_PRE  : T2Ipreldst<0, 0b10, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1512                            (ins t2addrmode_imm8_pre:$addr),
1513                            AddrModeT2_i8, IndexModePre, IIC_iLoad_iu,
1514                            "ldr", "\t$Rt, $addr!", "$addr.base = $Rn_wb", []>,
1515                 Sched<[WriteLd]>;
1516
1517def t2LDR_POST : T2Ipostldst<0, 0b10, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1518                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1519                          AddrModeT2_i8, IndexModePost, IIC_iLoad_iu,
1520                          "ldr", "\t$Rt, $Rn$offset", "$Rn = $Rn_wb", []>,
1521                  Sched<[WriteLd]>;
1522
1523def t2LDRB_PRE : T2Ipreldst<0, 0b00, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1524                            (ins t2addrmode_imm8_pre:$addr),
1525                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1526                            "ldrb", "\t$Rt, $addr!", "$addr.base = $Rn_wb", []>,
1527                 Sched<[WriteLd]>;
1528
1529def t2LDRB_POST : T2Ipostldst<0, 0b00, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1530                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1531                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1532                          "ldrb", "\t$Rt, $Rn$offset", "$Rn = $Rn_wb", []>,
1533                  Sched<[WriteLd]>;
1534
1535def t2LDRH_PRE : T2Ipreldst<0, 0b01, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1536                            (ins t2addrmode_imm8_pre:$addr),
1537                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1538                            "ldrh", "\t$Rt, $addr!", "$addr.base = $Rn_wb", []>,
1539                Sched<[WriteLd]>;
1540
1541def t2LDRH_POST : T2Ipostldst<0, 0b01, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1542                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1543                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1544                          "ldrh", "\t$Rt, $Rn$offset", "$Rn = $Rn_wb", []>,
1545                  Sched<[WriteLd]>;
1546
1547def t2LDRSB_PRE : T2Ipreldst<1, 0b00, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1548                            (ins t2addrmode_imm8_pre:$addr),
1549                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1550                            "ldrsb", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1551                            []>, Sched<[WriteLd]>;
1552
1553def t2LDRSB_POST : T2Ipostldst<1, 0b00, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1554                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1555                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1556                          "ldrsb", "\t$Rt, $Rn$offset", "$Rn = $Rn_wb", []>,
1557                   Sched<[WriteLd]>;
1558
1559def t2LDRSH_PRE : T2Ipreldst<1, 0b01, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1560                            (ins t2addrmode_imm8_pre:$addr),
1561                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1562                            "ldrsh", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1563                            []>, Sched<[WriteLd]>;
1564
1565def t2LDRSH_POST : T2Ipostldst<1, 0b01, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1566                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1567                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1568                          "ldrsh", "\t$Rt, $Rn$offset", "$Rn = $Rn_wb", []>,
1569                  Sched<[WriteLd]>;
1570} // mayLoad = 1, hasSideEffects = 0
1571
1572// F5.1.72 LDR (immediate) T4
1573// .w suffixes; Constraints can't be used on t2InstAlias to describe
1574// "$Rn =  $Rn_wb" on POST or "$addr.base = $Rn_wb" on PRE.
1575def t2LDR_PRE_imm : t2AsmPseudo<"ldr${p}.w $Rt, $addr!",
1576                         (ins GPR:$Rt, t2addrmode_imm8_pre:$addr, pred:$p)>;
1577def t2LDR_POST_imm : t2AsmPseudo<"ldr${p}.w $Rt, $Rn, $imm",
1578                         (ins GPR:$Rt, addr_offset_none:$Rn, t2am_imm8_offset:$imm, pred:$p)>;
1579
1580// LDRT, LDRBT, LDRHT, LDRSBT, LDRSHT all have offset mode (PUW=0b110).
1581// Ref: A8.6.57 LDR (immediate, Thumb) Encoding T4
1582class T2IldT<bit signed, bits<2> type, string opc, InstrItinClass ii>
1583  : T2Ii8p<(outs rGPR:$Rt), (ins t2addrmode_posimm8:$addr), ii, opc,
1584           "\t$Rt, $addr", []>, Sched<[WriteLd]> {
1585  bits<4> Rt;
1586  bits<13> addr;
1587  let Inst{31-27} = 0b11111;
1588  let Inst{26-25} = 0b00;
1589  let Inst{24} = signed;
1590  let Inst{23} = 0;
1591  let Inst{22-21} = type;
1592  let Inst{20} = 1; // load
1593  let Inst{19-16} = addr{12-9};
1594  let Inst{15-12} = Rt;
1595  let Inst{11} = 1;
1596  let Inst{10-8} = 0b110; // PUW.
1597  let Inst{7-0} = addr{7-0};
1598
1599  let DecoderMethod = "DecodeT2LoadT";
1600}
1601
1602def t2LDRT   : T2IldT<0, 0b10, "ldrt", IIC_iLoad_i>;
1603def t2LDRBT  : T2IldT<0, 0b00, "ldrbt", IIC_iLoad_bh_i>;
1604def t2LDRHT  : T2IldT<0, 0b01, "ldrht", IIC_iLoad_bh_i>;
1605def t2LDRSBT : T2IldT<1, 0b00, "ldrsbt", IIC_iLoad_bh_i>;
1606def t2LDRSHT : T2IldT<1, 0b01, "ldrsht", IIC_iLoad_bh_i>;
1607
1608class T2Ildacq<bits<4> bits23_20, bits<2> bit54, dag oops, dag iops,
1609               string opc, string asm, list<dag> pattern>
1610  : Thumb2I<oops, iops, AddrModeNone, 4, NoItinerary,
1611            opc, asm, "", pattern>, Requires<[IsThumb, HasAcquireRelease]> {
1612  bits<4> Rt;
1613  bits<4> addr;
1614
1615  let Inst{31-27} = 0b11101;
1616  let Inst{26-24} = 0b000;
1617  let Inst{23-20} = bits23_20;
1618  let Inst{11-6} = 0b111110;
1619  let Inst{5-4} = bit54;
1620  let Inst{3-0} = 0b1111;
1621
1622  // Encode instruction operands
1623  let Inst{19-16} = addr;
1624  let Inst{15-12} = Rt;
1625}
1626
1627def t2LDA : T2Ildacq<0b1101, 0b10, (outs rGPR:$Rt),
1628                     (ins addr_offset_none:$addr), "lda", "\t$Rt, $addr", []>,
1629            Sched<[WriteLd]>;
1630def t2LDAB : T2Ildacq<0b1101, 0b00, (outs rGPR:$Rt),
1631                      (ins addr_offset_none:$addr), "ldab", "\t$Rt, $addr", []>,
1632            Sched<[WriteLd]>;
1633def t2LDAH : T2Ildacq<0b1101, 0b01, (outs rGPR:$Rt),
1634                      (ins addr_offset_none:$addr), "ldah", "\t$Rt, $addr", []>,
1635            Sched<[WriteLd]>;
1636
1637// Store
1638defm t2STR :T2I_st<0b10,"str", IIC_iStore_i, IIC_iStore_si, GPR, store>;
1639defm t2STRB:T2I_st<0b00,"strb", IIC_iStore_bh_i, IIC_iStore_bh_si,
1640                   rGPR, truncstorei8>;
1641defm t2STRH:T2I_st<0b01,"strh", IIC_iStore_bh_i, IIC_iStore_bh_si,
1642                   rGPR, truncstorei16>;
1643
1644// Store doubleword
1645let mayStore = 1, hasSideEffects = 0, hasExtraSrcRegAllocReq = 1 in
1646def t2STRDi8 : T2Ii8s4<1, 0, 0, (outs),
1647                       (ins rGPR:$Rt, rGPR:$Rt2, t2addrmode_imm8s4:$addr),
1648               IIC_iStore_d_r, "strd", "\t$Rt, $Rt2, $addr", "",
1649               [(ARMstrd rGPR:$Rt, rGPR:$Rt2, t2addrmode_imm8s4:$addr)]>,
1650               Sched<[WriteST]>;
1651
1652// Indexed stores
1653
1654let mayStore = 1, hasSideEffects = 0 in {
1655def t2STR_PRE  : T2Ipreldst<0, 0b10, 0, 1, (outs GPRnopc:$Rn_wb),
1656                            (ins GPRnopc:$Rt, t2addrmode_imm8_pre:$addr),
1657                            AddrModeT2_i8, IndexModePre, IIC_iStore_iu,
1658                            "str", "\t$Rt, $addr!",
1659                            "$addr.base = $Rn_wb,@earlyclobber $Rn_wb", []>,
1660                 Sched<[WriteST]>;
1661
1662def t2STRH_PRE  : T2Ipreldst<0, 0b01, 0, 1, (outs GPRnopc:$Rn_wb),
1663                            (ins rGPR:$Rt, t2addrmode_imm8_pre:$addr),
1664                            AddrModeT2_i8, IndexModePre, IIC_iStore_iu,
1665                        "strh", "\t$Rt, $addr!",
1666                        "$addr.base = $Rn_wb,@earlyclobber $Rn_wb", []>,
1667                  Sched<[WriteST]>;
1668
1669def t2STRB_PRE  : T2Ipreldst<0, 0b00, 0, 1, (outs GPRnopc:$Rn_wb),
1670                            (ins rGPR:$Rt, t2addrmode_imm8_pre:$addr),
1671                            AddrModeT2_i8, IndexModePre, IIC_iStore_bh_iu,
1672                        "strb", "\t$Rt, $addr!",
1673                        "$addr.base = $Rn_wb,@earlyclobber $Rn_wb", []>,
1674            Sched<[WriteST]>;
1675} // mayStore = 1, hasSideEffects = 0
1676
1677def t2STR_POST : T2Ipostldst<0, 0b10, 0, 0, (outs GPRnopc:$Rn_wb),
1678                            (ins GPRnopc:$Rt, addr_offset_none:$Rn,
1679                                 t2am_imm8_offset:$offset),
1680                            AddrModeT2_i8, IndexModePost, IIC_iStore_iu,
1681                          "str", "\t$Rt, $Rn$offset",
1682                          "$Rn = $Rn_wb,@earlyclobber $Rn_wb",
1683             [(set GPRnopc:$Rn_wb,
1684                  (post_store GPRnopc:$Rt, addr_offset_none:$Rn,
1685                              t2am_imm8_offset:$offset))]>,
1686            Sched<[WriteST]>;
1687
1688def t2STRH_POST : T2Ipostldst<0, 0b01, 0, 0, (outs GPRnopc:$Rn_wb),
1689                            (ins rGPR:$Rt, addr_offset_none:$Rn,
1690                                 t2am_imm8_offset:$offset),
1691                            AddrModeT2_i8, IndexModePost, IIC_iStore_bh_iu,
1692                         "strh", "\t$Rt, $Rn$offset",
1693                         "$Rn = $Rn_wb,@earlyclobber $Rn_wb",
1694       [(set GPRnopc:$Rn_wb,
1695             (post_truncsti16 rGPR:$Rt, addr_offset_none:$Rn,
1696                              t2am_imm8_offset:$offset))]>,
1697            Sched<[WriteST]>;
1698
1699def t2STRB_POST : T2Ipostldst<0, 0b00, 0, 0, (outs GPRnopc:$Rn_wb),
1700                            (ins rGPR:$Rt, addr_offset_none:$Rn,
1701                                 t2am_imm8_offset:$offset),
1702                            AddrModeT2_i8, IndexModePost, IIC_iStore_bh_iu,
1703                         "strb", "\t$Rt, $Rn$offset",
1704                         "$Rn = $Rn_wb,@earlyclobber $Rn_wb",
1705        [(set GPRnopc:$Rn_wb,
1706              (post_truncsti8 rGPR:$Rt, addr_offset_none:$Rn,
1707                              t2am_imm8_offset:$offset))]>,
1708            Sched<[WriteST]>;
1709
1710// Pseudo-instructions for pattern matching the pre-indexed stores. We can't
1711// put the patterns on the instruction definitions directly as ISel wants
1712// the address base and offset to be separate operands, not a single
1713// complex operand like we represent the instructions themselves. The
1714// pseudos map between the two.
1715let usesCustomInserter = 1,
1716    Constraints = "$Rn = $Rn_wb,@earlyclobber $Rn_wb" in {
1717def t2STR_preidx: t2PseudoInst<(outs GPRnopc:$Rn_wb),
1718               (ins rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset, pred:$p),
1719               4, IIC_iStore_ru,
1720      [(set GPRnopc:$Rn_wb,
1721            (pre_store rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset))]>,
1722            Sched<[WriteST]>;
1723def t2STRB_preidx: t2PseudoInst<(outs GPRnopc:$Rn_wb),
1724               (ins rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset, pred:$p),
1725               4, IIC_iStore_ru,
1726      [(set GPRnopc:$Rn_wb,
1727            (pre_truncsti8 rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset))]>,
1728            Sched<[WriteST]>;
1729def t2STRH_preidx: t2PseudoInst<(outs GPRnopc:$Rn_wb),
1730               (ins rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset, pred:$p),
1731               4, IIC_iStore_ru,
1732      [(set GPRnopc:$Rn_wb,
1733            (pre_truncsti16 rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset))]>,
1734            Sched<[WriteST]>;
1735}
1736
1737// F5.1.229 STR (immediate) T4
1738// .w suffixes; Constraints can't be used on t2InstAlias to describe
1739// "$Rn =  $Rn_wb,@earlyclobber $Rn_wb" on POST or
1740// "$addr.base = $Rn_wb,@earlyclobber $Rn_wb" on PRE.
1741def t2STR_PRE_imm : t2AsmPseudo<"str${p}.w $Rt, $addr!",
1742  (ins GPR:$Rt, t2addrmode_imm8_pre:$addr, pred:$p)>;
1743def t2STR_POST_imm : t2AsmPseudo<"str${p}.w $Rt, $Rn, $imm",
1744  (ins GPR:$Rt, addr_offset_none:$Rn, t2am_imm8_offset:$imm, pred:$p)>;
1745
1746// STRT, STRBT, STRHT all have offset mode (PUW=0b110) and are for disassembly
1747// only.
1748// Ref: A8.6.193 STR (immediate, Thumb) Encoding T4
1749class T2IstT<bits<2> type, string opc, InstrItinClass ii>
1750  : T2Ii8p<(outs), (ins rGPR:$Rt, t2addrmode_posimm8:$addr), ii, opc,
1751           "\t$Rt, $addr", []>, Sched<[WriteST]> {
1752  let Inst{31-27} = 0b11111;
1753  let Inst{26-25} = 0b00;
1754  let Inst{24} = 0; // not signed
1755  let Inst{23} = 0;
1756  let Inst{22-21} = type;
1757  let Inst{20} = 0; // store
1758  let Inst{11} = 1;
1759  let Inst{10-8} = 0b110; // PUW
1760
1761  bits<4> Rt;
1762  bits<13> addr;
1763  let Inst{15-12} = Rt;
1764  let Inst{19-16} = addr{12-9};
1765  let Inst{7-0}   = addr{7-0};
1766}
1767
1768def t2STRT   : T2IstT<0b10, "strt", IIC_iStore_i>;
1769def t2STRBT  : T2IstT<0b00, "strbt", IIC_iStore_bh_i>;
1770def t2STRHT  : T2IstT<0b01, "strht", IIC_iStore_bh_i>;
1771
1772// ldrd / strd pre / post variants
1773
1774let mayLoad = 1, hasSideEffects = 0 in
1775def t2LDRD_PRE  : T2Ii8s4<1, 1, 1, (outs rGPR:$Rt, rGPR:$Rt2, GPR:$wb),
1776                 (ins t2addrmode_imm8s4_pre:$addr), IIC_iLoad_d_ru,
1777                 "ldrd", "\t$Rt, $Rt2, $addr!", "$addr.base = $wb", []>,
1778                 Sched<[WriteLd]> {
1779  let DecoderMethod = "DecodeT2LDRDPreInstruction";
1780}
1781
1782let mayLoad = 1, hasSideEffects = 0 in
1783def t2LDRD_POST : T2Ii8s4post<0, 1, 1, (outs rGPR:$Rt, rGPR:$Rt2, GPR:$wb),
1784                 (ins addr_offset_none:$addr, t2am_imm8s4_offset:$imm),
1785                 IIC_iLoad_d_ru, "ldrd", "\t$Rt, $Rt2, $addr$imm",
1786                 "$addr.base = $wb", []>, Sched<[WriteLd]>;
1787
1788let mayStore = 1, hasSideEffects = 0 in
1789def t2STRD_PRE  : T2Ii8s4<1, 1, 0, (outs GPR:$wb),
1790                 (ins rGPR:$Rt, rGPR:$Rt2, t2addrmode_imm8s4_pre:$addr),
1791                 IIC_iStore_d_ru, "strd", "\t$Rt, $Rt2, $addr!",
1792                 "$addr.base = $wb", []>, Sched<[WriteST]> {
1793  let DecoderMethod = "DecodeT2STRDPreInstruction";
1794}
1795
1796let mayStore = 1, hasSideEffects = 0 in
1797def t2STRD_POST : T2Ii8s4post<0, 1, 0, (outs GPR:$wb),
1798                 (ins rGPR:$Rt, rGPR:$Rt2, addr_offset_none:$addr,
1799                      t2am_imm8s4_offset:$imm),
1800                 IIC_iStore_d_ru, "strd", "\t$Rt, $Rt2, $addr$imm",
1801                 "$addr.base = $wb", []>, Sched<[WriteST]>;
1802
1803class T2Istrrel<bits<2> bit54, dag oops, dag iops,
1804                string opc, string asm, list<dag> pattern>
1805  : Thumb2I<oops, iops, AddrModeNone, 4, NoItinerary, opc,
1806            asm, "", pattern>, Requires<[IsThumb, HasAcquireRelease]>,
1807    Sched<[WriteST]> {
1808  bits<4> Rt;
1809  bits<4> addr;
1810
1811  let Inst{31-27} = 0b11101;
1812  let Inst{26-20} = 0b0001100;
1813  let Inst{11-6} = 0b111110;
1814  let Inst{5-4} = bit54;
1815  let Inst{3-0} = 0b1111;
1816
1817  // Encode instruction operands
1818  let Inst{19-16} = addr;
1819  let Inst{15-12} = Rt;
1820}
1821
1822def t2STL  : T2Istrrel<0b10, (outs), (ins rGPR:$Rt, addr_offset_none:$addr),
1823                       "stl", "\t$Rt, $addr", []>;
1824def t2STLB : T2Istrrel<0b00, (outs), (ins rGPR:$Rt, addr_offset_none:$addr),
1825                       "stlb", "\t$Rt, $addr", []>;
1826def t2STLH : T2Istrrel<0b01, (outs), (ins rGPR:$Rt, addr_offset_none:$addr),
1827                       "stlh", "\t$Rt, $addr", []>;
1828
1829// T2Ipl (Preload Data/Instruction) signals the memory system of possible future
1830// data/instruction access.
1831// instr_write is inverted for Thumb mode: (prefetch 3) -> (preload 0),
1832// (prefetch 1) -> (preload 2),  (prefetch 2) -> (preload 1).
1833multiclass T2Ipl<bits<1> write, bits<1> instr, string opc> {
1834
1835  def i12 : T2Ii12<(outs), (ins t2addrmode_imm12:$addr), IIC_Preload, opc,
1836                "\t$addr",
1837              [(ARMPreload t2addrmode_imm12:$addr, (i32 write), (i32 instr))]>,
1838              Sched<[WritePreLd]> {
1839    let Inst{31-25} = 0b1111100;
1840    let Inst{24} = instr;
1841    let Inst{23} = 1;
1842    let Inst{22} = 0;
1843    let Inst{21} = write;
1844    let Inst{20} = 1;
1845    let Inst{15-12} = 0b1111;
1846
1847    bits<17> addr;
1848    let Inst{19-16} = addr{16-13}; // Rn
1849    let Inst{11-0}  = addr{11-0};  // imm12
1850
1851    let DecoderMethod = "DecodeT2LoadImm12";
1852  }
1853
1854  def i8 : T2Ii8n<(outs), (ins t2addrmode_negimm8:$addr), IIC_Preload, opc,
1855                 "\t$addr",
1856            [(ARMPreload t2addrmode_negimm8:$addr, (i32 write), (i32 instr))]>,
1857            Sched<[WritePreLd]> {
1858    let Inst{31-25} = 0b1111100;
1859    let Inst{24} = instr;
1860    let Inst{23} = 0; // U = 0
1861    let Inst{22} = 0;
1862    let Inst{21} = write;
1863    let Inst{20} = 1;
1864    let Inst{15-12} = 0b1111;
1865    let Inst{11-8} = 0b1100;
1866
1867    bits<13> addr;
1868    let Inst{19-16} = addr{12-9}; // Rn
1869    let Inst{7-0}   = addr{7-0};  // imm8
1870
1871    let DecoderMethod = "DecodeT2LoadImm8";
1872  }
1873
1874  def s : T2Iso<(outs), (ins t2addrmode_so_reg:$addr), IIC_Preload, opc,
1875               "\t$addr",
1876             [(ARMPreload t2addrmode_so_reg:$addr, (i32 write), (i32 instr))]>,
1877             Sched<[WritePreLd]> {
1878    let Inst{31-25} = 0b1111100;
1879    let Inst{24} = instr;
1880    let Inst{23} = 0; // add = TRUE for T1
1881    let Inst{22} = 0;
1882    let Inst{21} = write;
1883    let Inst{20} = 1;
1884    let Inst{15-12} = 0b1111;
1885    let Inst{11-6} = 0b000000;
1886
1887    bits<10> addr;
1888    let Inst{19-16} = addr{9-6}; // Rn
1889    let Inst{3-0}   = addr{5-2}; // Rm
1890    let Inst{5-4}   = addr{1-0}; // imm2
1891
1892    let DecoderMethod = "DecodeT2LoadShift";
1893  }
1894}
1895
1896defm t2PLD    : T2Ipl<0, 0, "pld">,  Requires<[IsThumb2]>;
1897defm t2PLDW   : T2Ipl<1, 0, "pldw">, Requires<[IsThumb2,HasV7,HasMP]>;
1898defm t2PLI    : T2Ipl<0, 1, "pli">,  Requires<[IsThumb2,HasV7]>;
1899
1900// PLD/PLDW/PLI aliases w/ the optional .w suffix
1901def : t2InstAlias<"pld${p}.w\t$addr",
1902                 (t2PLDi12  t2addrmode_imm12:$addr, pred:$p)>;
1903def : t2InstAlias<"pld${p}.w\t$addr",
1904                 (t2PLDi8   t2addrmode_negimm8:$addr, pred:$p)>;
1905def : t2InstAlias<"pld${p}.w\t$addr",
1906                 (t2PLDs    t2addrmode_so_reg:$addr, pred:$p)>;
1907
1908def : InstAlias<"pldw${p}.w\t$addr",
1909                 (t2PLDWi12  t2addrmode_imm12:$addr, pred:$p), 0>,
1910      Requires<[IsThumb2,HasV7,HasMP]>;
1911def : InstAlias<"pldw${p}.w\t$addr",
1912                 (t2PLDWi8   t2addrmode_negimm8:$addr, pred:$p), 0>,
1913      Requires<[IsThumb2,HasV7,HasMP]>;
1914def : InstAlias<"pldw${p}.w\t$addr",
1915                 (t2PLDWs    t2addrmode_so_reg:$addr, pred:$p), 0>,
1916      Requires<[IsThumb2,HasV7,HasMP]>;
1917
1918def : InstAlias<"pli${p}.w\t$addr",
1919                 (t2PLIi12  t2addrmode_imm12:$addr, pred:$p), 0>,
1920      Requires<[IsThumb2,HasV7]>;
1921def : InstAlias<"pli${p}.w\t$addr",
1922                 (t2PLIi8   t2addrmode_negimm8:$addr, pred:$p), 0>,
1923      Requires<[IsThumb2,HasV7]>;
1924def : InstAlias<"pli${p}.w\t$addr",
1925                 (t2PLIs    t2addrmode_so_reg:$addr, pred:$p), 0>,
1926      Requires<[IsThumb2,HasV7]>;
1927
1928// pci variant is very similar to i12, but supports negative offsets
1929// from the PC. Only PLD and PLI have pci variants (not PLDW)
1930class T2Iplpci<bits<1> inst, string opc> : T2Ipc<(outs), (ins t2ldrlabel:$addr),
1931               IIC_Preload, opc, "\t$addr",
1932               [(ARMPreload (ARMWrapper tconstpool:$addr),
1933                (i32 0), (i32 inst))]>, Sched<[WritePreLd]> {
1934  let Inst{31-25} = 0b1111100;
1935  let Inst{24} = inst;
1936  let Inst{22-20} = 0b001;
1937  let Inst{19-16} = 0b1111;
1938  let Inst{15-12} = 0b1111;
1939
1940  bits<13> addr;
1941  let Inst{23}   = addr{12};   // add = (U == '1')
1942  let Inst{11-0} = addr{11-0}; // imm12
1943
1944  let DecoderMethod = "DecodeT2LoadLabel";
1945}
1946
1947def t2PLDpci : T2Iplpci<0, "pld">,  Requires<[IsThumb2]>;
1948def t2PLIpci : T2Iplpci<1, "pli">,  Requires<[IsThumb2,HasV7]>;
1949
1950def : t2InstAlias<"pld${p}.w $addr",
1951                  (t2PLDpci t2ldrlabel:$addr, pred:$p)>;
1952def : InstAlias<"pli${p}.w $addr",
1953                 (t2PLIpci  t2ldrlabel:$addr, pred:$p), 0>,
1954      Requires<[IsThumb2,HasV7]>;
1955
1956// PLD/PLI with alternate literal form.
1957def : t2InstAlias<"pld${p} $addr",
1958                  (t2PLDpci t2ldr_pcrel_imm12:$addr, pred:$p)>;
1959def : InstAlias<"pli${p} $addr",
1960                 (t2PLIpci  t2ldr_pcrel_imm12:$addr, pred:$p), 0>,
1961      Requires<[IsThumb2,HasV7]>;
1962def : t2InstAlias<"pld${p}.w $addr",
1963                  (t2PLDpci t2ldr_pcrel_imm12:$addr, pred:$p)>;
1964def : InstAlias<"pli${p}.w $addr",
1965                 (t2PLIpci  t2ldr_pcrel_imm12:$addr, pred:$p), 0>,
1966      Requires<[IsThumb2,HasV7]>;
1967
1968//===----------------------------------------------------------------------===//
1969//  Load / store multiple Instructions.
1970//
1971
1972multiclass thumb2_ld_mult<string asm, InstrItinClass itin,
1973                            InstrItinClass itin_upd, bit L_bit> {
1974  def IA :
1975    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1976         itin, !strconcat(asm, "${p}.w\t$Rn, $regs"), []> {
1977    bits<4>  Rn;
1978    bits<16> regs;
1979
1980    let Inst{31-27} = 0b11101;
1981    let Inst{26-25} = 0b00;
1982    let Inst{24-23} = 0b01;     // Increment After
1983    let Inst{22}    = 0;
1984    let Inst{21}    = 0;        // No writeback
1985    let Inst{20}    = L_bit;
1986    let Inst{19-16} = Rn;
1987    let Inst{15-0}  = regs;
1988  }
1989  def IA_UPD :
1990    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1991          itin_upd, !strconcat(asm, "${p}.w\t$Rn!, $regs"), "$Rn = $wb", []> {
1992    bits<4>  Rn;
1993    bits<16> regs;
1994
1995    let Inst{31-27} = 0b11101;
1996    let Inst{26-25} = 0b00;
1997    let Inst{24-23} = 0b01;     // Increment After
1998    let Inst{22}    = 0;
1999    let Inst{21}    = 1;        // Writeback
2000    let Inst{20}    = L_bit;
2001    let Inst{19-16} = Rn;
2002    let Inst{15-0}  = regs;
2003  }
2004  def DB :
2005    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
2006         itin, !strconcat(asm, "db${p}\t$Rn, $regs"), []> {
2007    bits<4>  Rn;
2008    bits<16> regs;
2009
2010    let Inst{31-27} = 0b11101;
2011    let Inst{26-25} = 0b00;
2012    let Inst{24-23} = 0b10;     // Decrement Before
2013    let Inst{22}    = 0;
2014    let Inst{21}    = 0;        // No writeback
2015    let Inst{20}    = L_bit;
2016    let Inst{19-16} = Rn;
2017    let Inst{15-0}  = regs;
2018  }
2019  def DB_UPD :
2020    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
2021          itin_upd, !strconcat(asm, "db${p}\t$Rn!, $regs"), "$Rn = $wb", []> {
2022    bits<4>  Rn;
2023    bits<16> regs;
2024
2025    let Inst{31-27} = 0b11101;
2026    let Inst{26-25} = 0b00;
2027    let Inst{24-23} = 0b10;     // Decrement Before
2028    let Inst{22}    = 0;
2029    let Inst{21}    = 1;        // Writeback
2030    let Inst{20}    = L_bit;
2031    let Inst{19-16} = Rn;
2032    let Inst{15-0}  = regs;
2033  }
2034}
2035
2036let hasSideEffects = 0 in {
2037
2038let mayLoad = 1, hasExtraDefRegAllocReq = 1, variadicOpsAreDefs = 1 in
2039defm t2LDM : thumb2_ld_mult<"ldm", IIC_iLoad_m, IIC_iLoad_mu, 1>;
2040
2041multiclass thumb2_st_mult<string asm, InstrItinClass itin,
2042                            InstrItinClass itin_upd, bit L_bit> {
2043  def IA :
2044    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
2045         itin, !strconcat(asm, "${p}.w\t$Rn, $regs"), []> {
2046    bits<4>  Rn;
2047    bits<16> regs;
2048
2049    let Inst{31-27} = 0b11101;
2050    let Inst{26-25} = 0b00;
2051    let Inst{24-23} = 0b01;     // Increment After
2052    let Inst{22}    = 0;
2053    let Inst{21}    = 0;        // No writeback
2054    let Inst{20}    = L_bit;
2055    let Inst{19-16} = Rn;
2056    let Inst{15}    = 0;
2057    let Inst{14}    = regs{14};
2058    let Inst{13}    = 0;
2059    let Inst{12-0}  = regs{12-0};
2060  }
2061  def IA_UPD :
2062    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
2063          itin_upd, !strconcat(asm, "${p}.w\t$Rn!, $regs"), "$Rn = $wb", []> {
2064    bits<4>  Rn;
2065    bits<16> regs;
2066
2067    let Inst{31-27} = 0b11101;
2068    let Inst{26-25} = 0b00;
2069    let Inst{24-23} = 0b01;     // Increment After
2070    let Inst{22}    = 0;
2071    let Inst{21}    = 1;        // Writeback
2072    let Inst{20}    = L_bit;
2073    let Inst{19-16} = Rn;
2074    let Inst{15}    = 0;
2075    let Inst{14}    = regs{14};
2076    let Inst{13}    = 0;
2077    let Inst{12-0}  = regs{12-0};
2078  }
2079  def DB :
2080    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
2081         itin, !strconcat(asm, "db${p}\t$Rn, $regs"), []> {
2082    bits<4>  Rn;
2083    bits<16> regs;
2084
2085    let Inst{31-27} = 0b11101;
2086    let Inst{26-25} = 0b00;
2087    let Inst{24-23} = 0b10;     // Decrement Before
2088    let Inst{22}    = 0;
2089    let Inst{21}    = 0;        // No writeback
2090    let Inst{20}    = L_bit;
2091    let Inst{19-16} = Rn;
2092    let Inst{15}    = 0;
2093    let Inst{14}    = regs{14};
2094    let Inst{13}    = 0;
2095    let Inst{12-0}  = regs{12-0};
2096  }
2097  def DB_UPD :
2098    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
2099          itin_upd, !strconcat(asm, "db${p}\t$Rn!, $regs"), "$Rn = $wb", []> {
2100    bits<4>  Rn;
2101    bits<16> regs;
2102
2103    let Inst{31-27} = 0b11101;
2104    let Inst{26-25} = 0b00;
2105    let Inst{24-23} = 0b10;     // Decrement Before
2106    let Inst{22}    = 0;
2107    let Inst{21}    = 1;        // Writeback
2108    let Inst{20}    = L_bit;
2109    let Inst{19-16} = Rn;
2110    let Inst{15}    = 0;
2111    let Inst{14}    = regs{14};
2112    let Inst{13}    = 0;
2113    let Inst{12-0}  = regs{12-0};
2114  }
2115}
2116
2117
2118let mayStore = 1, hasExtraSrcRegAllocReq = 1 in
2119defm t2STM : thumb2_st_mult<"stm", IIC_iStore_m, IIC_iStore_mu, 0>;
2120
2121} // hasSideEffects
2122
2123
2124//===----------------------------------------------------------------------===//
2125//  Move Instructions.
2126//
2127
2128let hasSideEffects = 0 in
2129def t2MOVr : T2sTwoReg<(outs GPRnopc:$Rd), (ins GPRnopc:$Rm), IIC_iMOVr,
2130                   "mov", ".w\t$Rd, $Rm", []>, Sched<[WriteALU]> {
2131  let Inst{31-27} = 0b11101;
2132  let Inst{26-25} = 0b01;
2133  let Inst{24-21} = 0b0010;
2134  let Inst{19-16} = 0b1111; // Rn
2135  let Inst{15} = 0b0;
2136  let Inst{14-12} = 0b000;
2137  let Inst{7-4} = 0b0000;
2138}
2139def : t2InstAlias<"mov${p}.w $Rd, $Rm", (t2MOVr GPRnopc:$Rd, GPRnopc:$Rm,
2140                                                pred:$p, zero_reg)>;
2141def : t2InstAlias<"movs${p}.w $Rd, $Rm", (t2MOVr GPRnopc:$Rd, GPRnopc:$Rm,
2142                                                 pred:$p, CPSR)>;
2143def : t2InstAlias<"movs${p} $Rd, $Rm", (t2MOVr GPRnopc:$Rd, GPRnopc:$Rm,
2144                                               pred:$p, CPSR)>;
2145
2146// AddedComplexity to ensure isel tries t2MOVi before t2MOVi16.
2147let isReMaterializable = 1, isAsCheapAsAMove = 1, isMoveImm = 1,
2148    AddedComplexity = 1 in
2149def t2MOVi : T2sOneRegImm<(outs rGPR:$Rd), (ins t2_so_imm:$imm), IIC_iMOVi,
2150                   "mov", ".w\t$Rd, $imm",
2151                   [(set rGPR:$Rd, t2_so_imm:$imm)]>, Sched<[WriteALU]> {
2152  let Inst{31-27} = 0b11110;
2153  let Inst{25} = 0;
2154  let Inst{24-21} = 0b0010;
2155  let Inst{19-16} = 0b1111; // Rn
2156  let Inst{15} = 0;
2157}
2158
2159// cc_out is handled as part of the explicit mnemonic in the parser for 'mov'.
2160// Use aliases to get that to play nice here.
2161def : t2InstAlias<"movs${p}.w $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
2162                                                pred:$p, CPSR)>;
2163def : t2InstAlias<"movs${p} $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
2164                                                pred:$p, CPSR)>;
2165
2166def : t2InstAlias<"mov${p}.w $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
2167                                                 pred:$p, zero_reg)>;
2168def : t2InstAlias<"mov${p} $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
2169                                               pred:$p, zero_reg)>;
2170
2171let isReMaterializable = 1, isAsCheapAsAMove = 1, isMoveImm = 1 in
2172def t2MOVi16 : T2I<(outs rGPR:$Rd), (ins imm0_65535_expr:$imm), IIC_iMOVi,
2173                   "movw", "\t$Rd, $imm",
2174                   [(set rGPR:$Rd, imm0_65535:$imm)]>, Sched<[WriteALU]>,
2175                   Requires<[IsThumb, HasV8MBaseline]> {
2176  let Inst{31-27} = 0b11110;
2177  let Inst{25} = 1;
2178  let Inst{24-21} = 0b0010;
2179  let Inst{20} = 0; // The S bit.
2180  let Inst{15} = 0;
2181
2182  bits<4> Rd;
2183  bits<16> imm;
2184
2185  let Inst{11-8}  = Rd;
2186  let Inst{19-16} = imm{15-12};
2187  let Inst{26}    = imm{11};
2188  let Inst{14-12} = imm{10-8};
2189  let Inst{7-0}   = imm{7-0};
2190  let DecoderMethod = "DecodeT2MOVTWInstruction";
2191}
2192
2193def : InstAlias<"mov${p} $Rd, $imm",
2194                (t2MOVi16 rGPR:$Rd, imm256_65535_expr:$imm, pred:$p), 0>,
2195                Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteALU]>;
2196
2197// This gets lowered to a single 4-byte instructions
2198let Size = 4 in
2199def t2MOVi16_ga_pcrel : PseudoInst<(outs rGPR:$Rd),
2200                                (ins i32imm:$addr, pclabel:$id), IIC_iMOVi, []>,
2201                        Sched<[WriteALU]>;
2202
2203let Constraints = "$src = $Rd" in {
2204def t2MOVTi16 : T2I<(outs rGPR:$Rd),
2205                    (ins rGPR:$src, imm0_65535_expr:$imm), IIC_iMOVi,
2206                    "movt", "\t$Rd, $imm",
2207                    [(set rGPR:$Rd,
2208                          (or (and rGPR:$src, 0xffff), lo16AllZero:$imm))]>,
2209                          Sched<[WriteALU]>,
2210                          Requires<[IsThumb, HasV8MBaseline]> {
2211  let Inst{31-27} = 0b11110;
2212  let Inst{25} = 1;
2213  let Inst{24-21} = 0b0110;
2214  let Inst{20} = 0; // The S bit.
2215  let Inst{15} = 0;
2216
2217  bits<4> Rd;
2218  bits<16> imm;
2219
2220  let Inst{11-8}  = Rd;
2221  let Inst{19-16} = imm{15-12};
2222  let Inst{26}    = imm{11};
2223  let Inst{14-12} = imm{10-8};
2224  let Inst{7-0}   = imm{7-0};
2225  let DecoderMethod = "DecodeT2MOVTWInstruction";
2226}
2227
2228// This gets lowered to a single 4-byte instructions
2229let Size = 4 in
2230def t2MOVTi16_ga_pcrel : PseudoInst<(outs rGPR:$Rd),
2231                     (ins rGPR:$src, i32imm:$addr, pclabel:$id), IIC_iMOVi, []>,
2232                     Sched<[WriteALU]>, Requires<[IsThumb, HasV8MBaseline]>;
2233} // Constraints
2234
2235def : T2Pat<(or rGPR:$src, 0xffff0000), (t2MOVTi16 rGPR:$src, 0xffff)>;
2236
2237//===----------------------------------------------------------------------===//
2238//  Extend Instructions.
2239//
2240
2241// Sign extenders
2242
2243def t2SXTB  : T2I_ext_rrot<0b100, "sxtb">;
2244def t2SXTH  : T2I_ext_rrot<0b000, "sxth">;
2245def t2SXTB16 : T2I_ext_rrot_xtb16<0b010, "sxtb16">;
2246
2247def t2SXTAB : T2I_exta_rrot<0b100, "sxtab">;
2248def t2SXTAH : T2I_exta_rrot<0b000, "sxtah">;
2249def t2SXTAB16 : T2I_exta_rrot<0b010, "sxtab16">;
2250
2251def : T2Pat<(sext_inreg (rotr rGPR:$Rn, rot_imm:$rot), i8),
2252            (t2SXTB rGPR:$Rn, rot_imm:$rot)>;
2253def : T2Pat<(sext_inreg (rotr rGPR:$Rn, rot_imm:$rot), i16),
2254            (t2SXTH rGPR:$Rn, rot_imm:$rot)>;
2255def : Thumb2DSPPat<(add rGPR:$Rn,
2256                            (sext_inreg (rotr rGPR:$Rm, rot_imm:$rot), i8)),
2257            (t2SXTAB rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2258def : Thumb2DSPPat<(add rGPR:$Rn,
2259                            (sext_inreg (rotr rGPR:$Rm, rot_imm:$rot), i16)),
2260            (t2SXTAH rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2261def : Thumb2DSPPat<(int_arm_sxtb16 rGPR:$Rn),
2262                   (t2SXTB16 rGPR:$Rn, 0)>;
2263def : Thumb2DSPPat<(int_arm_sxtab16 rGPR:$Rn, rGPR:$Rm),
2264                   (t2SXTAB16 rGPR:$Rn, rGPR:$Rm, 0)>;
2265def : Thumb2DSPPat<(int_arm_sxtb16 (rotr rGPR:$Rn, rot_imm:$rot)),
2266                   (t2SXTB16 rGPR:$Rn, rot_imm:$rot)>;
2267def : Thumb2DSPPat<(int_arm_sxtab16 rGPR:$Rn, (rotr rGPR:$Rm, rot_imm:$rot)),
2268                   (t2SXTAB16 rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2269
2270
2271// A simple right-shift can also be used in most cases (the exception is the
2272// SXTH operations with a rotate of 24: there the non-contiguous bits are
2273// relevant).
2274def : Thumb2DSPPat<(add rGPR:$Rn, (sext_inreg
2275                                        (srl rGPR:$Rm, rot_imm:$rot), i8)),
2276                       (t2SXTAB rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2277def : Thumb2DSPPat<(add rGPR:$Rn, (sext_inreg
2278                                        (srl rGPR:$Rm, imm8_or_16:$rot), i16)),
2279                       (t2SXTAH rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2280def : Thumb2DSPPat<(add rGPR:$Rn, (sext_inreg
2281                                        (rotr rGPR:$Rm, (i32 24)), i16)),
2282                       (t2SXTAH rGPR:$Rn, rGPR:$Rm, (i32 3))>;
2283def : Thumb2DSPPat<(add rGPR:$Rn, (sext_inreg
2284                                        (or (srl rGPR:$Rm, (i32 24)),
2285                                              (shl rGPR:$Rm, (i32 8))), i16)),
2286                       (t2SXTAH rGPR:$Rn, rGPR:$Rm, (i32 3))>;
2287
2288// Zero extenders
2289
2290let AddedComplexity = 16 in {
2291def t2UXTB   : T2I_ext_rrot<0b101, "uxtb">;
2292def t2UXTH   : T2I_ext_rrot<0b001, "uxth">;
2293def t2UXTB16 : T2I_ext_rrot_xtb16<0b011, "uxtb16">;
2294
2295def : Thumb2DSPPat<(and (rotr rGPR:$Rm, rot_imm:$rot), 0x000000FF),
2296                       (t2UXTB rGPR:$Rm, rot_imm:$rot)>;
2297def : Thumb2DSPPat<(and (rotr rGPR:$Rm, rot_imm:$rot), 0x0000FFFF),
2298                       (t2UXTH rGPR:$Rm, rot_imm:$rot)>;
2299def : Thumb2DSPPat<(and (rotr rGPR:$Rm, rot_imm:$rot), 0x00FF00FF),
2300                       (t2UXTB16 rGPR:$Rm, rot_imm:$rot)>;
2301
2302def : Thumb2DSPPat<(int_arm_uxtb16 rGPR:$Rm),
2303                   (t2UXTB16 rGPR:$Rm, 0)>;
2304def : Thumb2DSPPat<(int_arm_uxtb16 (rotr rGPR:$Rn, rot_imm:$rot)),
2305                   (t2UXTB16 rGPR:$Rn, rot_imm:$rot)>;
2306
2307// FIXME: This pattern incorrectly assumes the shl operator is a rotate.
2308//        The transformation should probably be done as a combiner action
2309//        instead so we can include a check for masking back in the upper
2310//        eight bits of the source into the lower eight bits of the result.
2311//def : T2Pat<(and (shl rGPR:$Src, (i32 8)), 0xFF00FF),
2312//            (t2UXTB16 rGPR:$Src, 3)>,
2313//          Requires<[HasDSP, IsThumb2]>;
2314def : T2Pat<(and (srl rGPR:$Src, (i32 8)), 0xFF00FF),
2315            (t2UXTB16 rGPR:$Src, 1)>,
2316        Requires<[HasDSP, IsThumb2]>;
2317
2318def t2UXTAB : T2I_exta_rrot<0b101, "uxtab">;
2319def t2UXTAH : T2I_exta_rrot<0b001, "uxtah">;
2320def t2UXTAB16 : T2I_exta_rrot<0b011, "uxtab16">;
2321
2322def : Thumb2DSPPat<(add rGPR:$Rn, (and (rotr rGPR:$Rm, rot_imm:$rot),
2323                                            0x00FF)),
2324                       (t2UXTAB rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2325def : Thumb2DSPPat<(add rGPR:$Rn, (and (rotr rGPR:$Rm, rot_imm:$rot),
2326                                            0xFFFF)),
2327                       (t2UXTAH rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2328def : Thumb2DSPPat<(add rGPR:$Rn, (and (srl rGPR:$Rm, rot_imm:$rot),
2329                                           0xFF)),
2330                       (t2UXTAB rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2331def : Thumb2DSPPat<(add rGPR:$Rn, (and (srl rGPR:$Rm, imm8_or_16:$rot),
2332                                            0xFFFF)),
2333                       (t2UXTAH rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2334def : Thumb2DSPPat<(int_arm_uxtab16 rGPR:$Rn, rGPR:$Rm),
2335                      (t2UXTAB16 rGPR:$Rn, rGPR:$Rm, 0)>;
2336def : Thumb2DSPPat<(int_arm_uxtab16 rGPR:$Rn, (rotr rGPR:$Rm, rot_imm:$rot)),
2337                   (t2UXTAB16 rGPR:$Rn, rGPR:$Rm, rot_imm:$rot)>;
2338}
2339
2340
2341//===----------------------------------------------------------------------===//
2342//  Arithmetic Instructions.
2343//
2344
2345let isAdd = 1 in
2346defm t2ADD  : T2I_bin_ii12rs<0b000, "add", add, 1>;
2347defm t2SUB  : T2I_bin_ii12rs<0b101, "sub", sub>;
2348
2349// ADD and SUB with 's' bit set. No 12-bit immediate (T4) variants.
2350//
2351// Currently, t2ADDS/t2SUBS are pseudo opcodes that exist only in the
2352// selection DAG. They are "lowered" to real t2ADD/t2SUB opcodes by
2353// AdjustInstrPostInstrSelection where we determine whether or not to
2354// set the "s" bit based on CPSR liveness.
2355//
2356// FIXME: Eliminate t2ADDS/t2SUBS pseudo opcodes after adding tablegen
2357// support for an optional CPSR definition that corresponds to the DAG
2358// node's second value. We can then eliminate the implicit def of CPSR.
2359defm t2ADDS : T2I_bin_s_irs <IIC_iALUi, IIC_iALUr, IIC_iALUsi, ARMaddc, 1>;
2360defm t2SUBS : T2I_bin_s_irs <IIC_iALUi, IIC_iALUr, IIC_iALUsi, ARMsubc>;
2361
2362def : T2Pat<(ARMsubs GPRnopc:$Rn, t2_so_imm:$imm),
2363            (t2SUBSri $Rn, t2_so_imm:$imm)>;
2364def : T2Pat<(ARMsubs GPRnopc:$Rn, rGPR:$Rm), (t2SUBSrr $Rn, $Rm)>;
2365def : T2Pat<(ARMsubs GPRnopc:$Rn, t2_so_reg:$ShiftedRm),
2366            (t2SUBSrs $Rn, t2_so_reg:$ShiftedRm)>;
2367
2368let hasPostISelHook = 1 in {
2369defm t2ADC  : T2I_adde_sube_irs<0b1010, "adc", ARMadde, 1>;
2370defm t2SBC  : T2I_adde_sube_irs<0b1011, "sbc", ARMsube>;
2371}
2372
2373def : t2InstSubst<"adc${s}${p} $rd, $rn, $imm",
2374                 (t2SBCri rGPR:$rd, rGPR:$rn, t2_so_imm_not:$imm, pred:$p, s_cc_out:$s)>;
2375def : t2InstSubst<"sbc${s}${p} $rd, $rn, $imm",
2376                 (t2ADCri rGPR:$rd, rGPR:$rn, t2_so_imm_not:$imm, pred:$p, s_cc_out:$s)>;
2377
2378def : t2InstSubst<"add${s}${p}.w $rd, $rn, $imm",
2379                 (t2SUBri rGPR:$rd, GPRnopc:$rn, t2_so_imm_neg:$imm, pred:$p, s_cc_out:$s)>;
2380def : t2InstSubst<"sub${s}${p}.w $rd, $rn, $imm",
2381                 (t2ADDri rGPR:$rd, GPRnopc:$rn, t2_so_imm_neg:$imm, pred:$p, s_cc_out:$s)>;
2382def : t2InstSubst<"subw${p} $Rd, $Rn, $imm",
2383                 (t2ADDri12 rGPR:$Rd, GPR:$Rn, imm0_4095_neg:$imm, pred:$p)>;
2384def : t2InstSubst<"sub${s}${p} $rd, $rn, $imm",
2385                 (t2ADDri rGPR:$rd, GPRnopc:$rn, t2_so_imm_neg:$imm, pred:$p, s_cc_out:$s)>;
2386def : t2InstSubst<"sub${p} $rd, $rn, $imm",
2387                 (t2ADDri12 rGPR:$rd, GPR:$rn, imm0_4095_neg:$imm, pred:$p)>;
2388
2389// SP to SP alike
2390def : t2InstSubst<"add${s}${p}.w $rd, $rn, $imm",
2391                 (t2SUBspImm GPRsp:$rd, GPRsp:$rn, t2_so_imm_neg:$imm, pred:$p, s_cc_out:$s)>;
2392def : t2InstSubst<"sub${s}${p}.w $rd, $rn, $imm",
2393                 (t2ADDspImm GPRsp:$rd, GPRsp:$rn, t2_so_imm_neg:$imm, pred:$p, s_cc_out:$s)>;
2394def : t2InstSubst<"subw${p} $Rd, $Rn, $imm",
2395                 (t2ADDspImm12 GPRsp:$Rd, GPRsp:$Rn, imm0_4095_neg:$imm, pred:$p)>;
2396def : t2InstSubst<"sub${s}${p} $rd, $rn, $imm",
2397                 (t2ADDspImm GPRsp:$rd, GPRsp:$rn, t2_so_imm_neg:$imm, pred:$p, s_cc_out:$s)>;
2398def : t2InstSubst<"sub${p} $rd, $rn, $imm",
2399                 (t2ADDspImm12 GPRsp:$rd, GPRsp:$rn, imm0_4095_neg:$imm, pred:$p)>;
2400
2401
2402// RSB
2403defm t2RSB  : T2I_rbin_irs  <0b1110, "rsb", sub>;
2404
2405// FIXME: Eliminate them if we can write def : Pat patterns which defines
2406// CPSR and the implicit def of CPSR is not needed.
2407defm t2RSBS : T2I_rbin_s_is <ARMsubc>;
2408
2409// (sub X, imm) gets canonicalized to (add X, -imm).  Match this form.
2410// The assume-no-carry-in form uses the negation of the input since add/sub
2411// assume opposite meanings of the carry flag (i.e., carry == !borrow).
2412// See the definition of AddWithCarry() in the ARM ARM A2.2.1 for the gory
2413// details.
2414// The AddedComplexity preferences the first variant over the others since
2415// it can be shrunk to a 16-bit wide encoding, while the others cannot.
2416let AddedComplexity = 1 in
2417def : T2Pat<(add        rGPR:$src, imm1_255_neg:$imm),
2418            (t2SUBri    rGPR:$src, imm1_255_neg:$imm)>;
2419def : T2Pat<(add        rGPR:$src, t2_so_imm_neg:$imm),
2420            (t2SUBri    rGPR:$src, t2_so_imm_neg:$imm)>;
2421def : T2Pat<(add        rGPR:$src, imm0_4095_neg:$imm),
2422            (t2SUBri12  rGPR:$src, imm0_4095_neg:$imm)>;
2423def : T2Pat<(add        GPR:$src, imm0_65535_neg:$imm),
2424            (t2SUBrr    GPR:$src, (t2MOVi16 (imm_neg_XFORM imm:$imm)))>;
2425
2426// Do the same for v8m targets since they support movw with a 16-bit value.
2427def : T1Pat<(add tGPR:$src, imm0_65535_neg:$imm),
2428             (tSUBrr tGPR:$src, (t2MOVi16 (imm_neg_XFORM imm:$imm)))>,
2429             Requires<[HasV8MBaseline]>;
2430
2431let AddedComplexity = 1 in
2432def : T2Pat<(ARMaddc    rGPR:$src, imm1_255_neg:$imm),
2433            (t2SUBSri   rGPR:$src, imm1_255_neg:$imm)>;
2434def : T2Pat<(ARMaddc    rGPR:$src, t2_so_imm_neg:$imm),
2435            (t2SUBSri   rGPR:$src, t2_so_imm_neg:$imm)>;
2436def : T2Pat<(ARMaddc    rGPR:$src, imm0_65535_neg:$imm),
2437            (t2SUBSrr   rGPR:$src, (t2MOVi16 (imm_neg_XFORM imm:$imm)))>;
2438// The with-carry-in form matches bitwise not instead of the negation.
2439// Effectively, the inverse interpretation of the carry flag already accounts
2440// for part of the negation.
2441let AddedComplexity = 1 in
2442def : T2Pat<(ARMadde    rGPR:$src, imm0_255_not:$imm, CPSR),
2443            (t2SBCri    rGPR:$src, imm0_255_not:$imm)>;
2444def : T2Pat<(ARMadde    rGPR:$src, t2_so_imm_not:$imm, CPSR),
2445            (t2SBCri    rGPR:$src, t2_so_imm_not:$imm)>;
2446def : T2Pat<(ARMadde    rGPR:$src, imm0_65535_neg:$imm, CPSR),
2447            (t2SBCrr    rGPR:$src, (t2MOVi16 (imm_not_XFORM imm:$imm)))>;
2448
2449def t2SEL : T2ThreeReg<(outs GPR:$Rd), (ins GPR:$Rn, GPR:$Rm),
2450                NoItinerary, "sel", "\t$Rd, $Rn, $Rm",
2451                [(set GPR:$Rd, (int_arm_sel GPR:$Rn, GPR:$Rm))]>,
2452          Requires<[IsThumb2, HasDSP]> {
2453  let Inst{31-27} = 0b11111;
2454  let Inst{26-24} = 0b010;
2455  let Inst{23} = 0b1;
2456  let Inst{22-20} = 0b010;
2457  let Inst{15-12} = 0b1111;
2458  let Inst{7} = 0b1;
2459  let Inst{6-4} = 0b000;
2460}
2461
2462// A6.3.13, A6.3.14, A6.3.15 Parallel addition and subtraction (signed/unsigned)
2463// And Miscellaneous operations -- for disassembly only
2464class T2I_pam<bits<3> op22_20, bits<4> op7_4, string opc,
2465              list<dag> pat, dag iops, string asm>
2466  : T2I<(outs rGPR:$Rd), iops, NoItinerary, opc, asm, pat>,
2467    Requires<[IsThumb2, HasDSP]> {
2468  let Inst{31-27} = 0b11111;
2469  let Inst{26-23} = 0b0101;
2470  let Inst{22-20} = op22_20;
2471  let Inst{15-12} = 0b1111;
2472  let Inst{7-4} = op7_4;
2473
2474  bits<4> Rd;
2475  bits<4> Rn;
2476  bits<4> Rm;
2477
2478  let Inst{11-8}  = Rd;
2479  let Inst{19-16} = Rn;
2480  let Inst{3-0}   = Rm;
2481}
2482
2483class T2I_pam_intrinsics<bits<3> op22_20, bits<4> op7_4, string opc,
2484                         Intrinsic intrinsic>
2485  : T2I_pam<op22_20, op7_4, opc,
2486    [(set rGPR:$Rd, (intrinsic rGPR:$Rn, rGPR:$Rm))],
2487    (ins rGPR:$Rn, rGPR:$Rm), "\t$Rd, $Rn, $Rm">;
2488
2489class T2I_pam_intrinsics_rev<bits<3> op22_20, bits<4> op7_4, string opc>
2490  : T2I_pam<op22_20, op7_4, opc, [],
2491    (ins rGPR:$Rm, rGPR:$Rn), "\t$Rd, $Rm, $Rn">;
2492
2493// Saturating add/subtract
2494def t2QADD16  : T2I_pam_intrinsics<0b001, 0b0001, "qadd16", int_arm_qadd16>;
2495def t2QADD8   : T2I_pam_intrinsics<0b000, 0b0001, "qadd8", int_arm_qadd8>;
2496def t2QASX    : T2I_pam_intrinsics<0b010, 0b0001, "qasx", int_arm_qasx>;
2497def t2UQSUB8  : T2I_pam_intrinsics<0b100, 0b0101, "uqsub8", int_arm_uqsub8>;
2498def t2QSAX    : T2I_pam_intrinsics<0b110, 0b0001, "qsax", int_arm_qsax>;
2499def t2QSUB16  : T2I_pam_intrinsics<0b101, 0b0001, "qsub16", int_arm_qsub16>;
2500def t2QSUB8   : T2I_pam_intrinsics<0b100, 0b0001, "qsub8", int_arm_qsub8>;
2501def t2UQADD16 : T2I_pam_intrinsics<0b001, 0b0101, "uqadd16", int_arm_uqadd16>;
2502def t2UQADD8  : T2I_pam_intrinsics<0b000, 0b0101, "uqadd8", int_arm_uqadd8>;
2503def t2UQASX   : T2I_pam_intrinsics<0b010, 0b0101, "uqasx", int_arm_uqasx>;
2504def t2UQSAX   : T2I_pam_intrinsics<0b110, 0b0101, "uqsax", int_arm_uqsax>;
2505def t2UQSUB16 : T2I_pam_intrinsics<0b101, 0b0101, "uqsub16", int_arm_uqsub16>;
2506def t2QADD    : T2I_pam_intrinsics_rev<0b000, 0b1000, "qadd">;
2507def t2QSUB    : T2I_pam_intrinsics_rev<0b000, 0b1010, "qsub">;
2508def t2QDADD   : T2I_pam_intrinsics_rev<0b000, 0b1001, "qdadd">;
2509def t2QDSUB   : T2I_pam_intrinsics_rev<0b000, 0b1011, "qdsub">;
2510
2511def : Thumb2DSPPat<(int_arm_qadd rGPR:$Rm, rGPR:$Rn),
2512                   (t2QADD rGPR:$Rm, rGPR:$Rn)>;
2513def : Thumb2DSPPat<(int_arm_qsub rGPR:$Rm, rGPR:$Rn),
2514                   (t2QSUB rGPR:$Rm, rGPR:$Rn)>;
2515def : Thumb2DSPPat<(int_arm_qadd rGPR:$Rm, (int_arm_qadd rGPR:$Rn, rGPR:$Rn)),
2516                   (t2QDADD rGPR:$Rm, rGPR:$Rn)>;
2517def : Thumb2DSPPat<(int_arm_qsub rGPR:$Rm, (int_arm_qadd rGPR:$Rn, rGPR:$Rn)),
2518                   (t2QDSUB rGPR:$Rm, rGPR:$Rn)>;
2519
2520def : Thumb2DSPPat<(saddsat rGPR:$Rm, rGPR:$Rn),
2521                   (t2QADD rGPR:$Rm, rGPR:$Rn)>;
2522def : Thumb2DSPPat<(ssubsat rGPR:$Rm, rGPR:$Rn),
2523                   (t2QSUB rGPR:$Rm, rGPR:$Rn)>;
2524def : Thumb2DSPPat<(saddsat rGPR:$Rm, (saddsat rGPR:$Rn, rGPR:$Rn)),
2525                   (t2QDADD rGPR:$Rm, rGPR:$Rn)>;
2526def : Thumb2DSPPat<(ssubsat rGPR:$Rm, (saddsat rGPR:$Rn, rGPR:$Rn)),
2527                   (t2QDSUB rGPR:$Rm, rGPR:$Rn)>;
2528
2529def : Thumb2DSPPat<(ARMqadd8b rGPR:$Rm, rGPR:$Rn),
2530                   (t2QADD8 rGPR:$Rm, rGPR:$Rn)>;
2531def : Thumb2DSPPat<(ARMqsub8b rGPR:$Rm, rGPR:$Rn),
2532                   (t2QSUB8 rGPR:$Rm, rGPR:$Rn)>;
2533def : Thumb2DSPPat<(ARMqadd16b rGPR:$Rm, rGPR:$Rn),
2534                   (t2QADD16 rGPR:$Rm, rGPR:$Rn)>;
2535def : Thumb2DSPPat<(ARMqsub16b rGPR:$Rm, rGPR:$Rn),
2536                   (t2QSUB16 rGPR:$Rm, rGPR:$Rn)>;
2537
2538def : Thumb2DSPPat<(ARMuqadd8b rGPR:$Rm, rGPR:$Rn),
2539                   (t2UQADD8 rGPR:$Rm, rGPR:$Rn)>;
2540def : Thumb2DSPPat<(ARMuqsub8b rGPR:$Rm, rGPR:$Rn),
2541                   (t2UQSUB8 rGPR:$Rm, rGPR:$Rn)>;
2542def : Thumb2DSPPat<(ARMuqadd16b rGPR:$Rm, rGPR:$Rn),
2543                   (t2UQADD16 rGPR:$Rm, rGPR:$Rn)>;
2544def : Thumb2DSPPat<(ARMuqsub16b rGPR:$Rm, rGPR:$Rn),
2545                   (t2UQSUB16 rGPR:$Rm, rGPR:$Rn)>;
2546
2547// Signed/Unsigned add/subtract
2548
2549def t2SASX    : T2I_pam_intrinsics<0b010, 0b0000, "sasx", int_arm_sasx>;
2550def t2SADD16  : T2I_pam_intrinsics<0b001, 0b0000, "sadd16", int_arm_sadd16>;
2551def t2SADD8   : T2I_pam_intrinsics<0b000, 0b0000, "sadd8", int_arm_sadd8>;
2552def t2SSAX    : T2I_pam_intrinsics<0b110, 0b0000, "ssax", int_arm_ssax>;
2553def t2SSUB16  : T2I_pam_intrinsics<0b101, 0b0000, "ssub16", int_arm_ssub16>;
2554def t2SSUB8   : T2I_pam_intrinsics<0b100, 0b0000, "ssub8", int_arm_ssub8>;
2555def t2UASX    : T2I_pam_intrinsics<0b010, 0b0100, "uasx", int_arm_uasx>;
2556def t2UADD16  : T2I_pam_intrinsics<0b001, 0b0100, "uadd16", int_arm_uadd16>;
2557def t2UADD8   : T2I_pam_intrinsics<0b000, 0b0100, "uadd8", int_arm_uadd8>;
2558def t2USAX    : T2I_pam_intrinsics<0b110, 0b0100, "usax", int_arm_usax>;
2559def t2USUB16  : T2I_pam_intrinsics<0b101, 0b0100, "usub16", int_arm_usub16>;
2560def t2USUB8   : T2I_pam_intrinsics<0b100, 0b0100, "usub8", int_arm_usub8>;
2561
2562// Signed/Unsigned halving add/subtract
2563
2564def t2SHASX   : T2I_pam_intrinsics<0b010, 0b0010, "shasx", int_arm_shasx>;
2565def t2SHADD16 : T2I_pam_intrinsics<0b001, 0b0010, "shadd16", int_arm_shadd16>;
2566def t2SHADD8  : T2I_pam_intrinsics<0b000, 0b0010, "shadd8", int_arm_shadd8>;
2567def t2SHSAX   : T2I_pam_intrinsics<0b110, 0b0010, "shsax", int_arm_shsax>;
2568def t2SHSUB16 : T2I_pam_intrinsics<0b101, 0b0010, "shsub16", int_arm_shsub16>;
2569def t2SHSUB8  : T2I_pam_intrinsics<0b100, 0b0010, "shsub8", int_arm_shsub8>;
2570def t2UHASX   : T2I_pam_intrinsics<0b010, 0b0110, "uhasx", int_arm_uhasx>;
2571def t2UHADD16 : T2I_pam_intrinsics<0b001, 0b0110, "uhadd16", int_arm_uhadd16>;
2572def t2UHADD8  : T2I_pam_intrinsics<0b000, 0b0110, "uhadd8", int_arm_uhadd8>;
2573def t2UHSAX   : T2I_pam_intrinsics<0b110, 0b0110, "uhsax", int_arm_uhsax>;
2574def t2UHSUB16 : T2I_pam_intrinsics<0b101, 0b0110, "uhsub16", int_arm_uhsub16>;
2575def t2UHSUB8  : T2I_pam_intrinsics<0b100, 0b0110, "uhsub8", int_arm_uhsub8>;
2576
2577// Helper class for disassembly only
2578// A6.3.16 & A6.3.17
2579// T2Imac - Thumb2 multiply [accumulate, and absolute difference] instructions.
2580class T2ThreeReg_mac<bit long, bits<3> op22_20, bits<4> op7_4, dag oops,
2581  dag iops, InstrItinClass itin, string opc, string asm, list<dag> pattern>
2582  : T2ThreeReg<oops, iops, itin, opc, asm, pattern> {
2583  let Inst{31-27} = 0b11111;
2584  let Inst{26-24} = 0b011;
2585  let Inst{23}    = long;
2586  let Inst{22-20} = op22_20;
2587  let Inst{7-4}   = op7_4;
2588}
2589
2590class T2FourReg_mac<bit long, bits<3> op22_20, bits<4> op7_4, dag oops,
2591  dag iops, InstrItinClass itin, string opc, string asm, list<dag> pattern>
2592  : T2FourReg<oops, iops, itin, opc, asm, pattern> {
2593  let Inst{31-27} = 0b11111;
2594  let Inst{26-24} = 0b011;
2595  let Inst{23}    = long;
2596  let Inst{22-20} = op22_20;
2597  let Inst{7-4}   = op7_4;
2598}
2599
2600// Unsigned Sum of Absolute Differences [and Accumulate].
2601def t2USAD8   : T2ThreeReg_mac<0, 0b111, 0b0000, (outs rGPR:$Rd),
2602                                           (ins rGPR:$Rn, rGPR:$Rm),
2603                        NoItinerary, "usad8", "\t$Rd, $Rn, $Rm",
2604                        [(set rGPR:$Rd, (int_arm_usad8 rGPR:$Rn, rGPR:$Rm))]>,
2605          Requires<[IsThumb2, HasDSP]> {
2606  let Inst{15-12} = 0b1111;
2607}
2608def t2USADA8  : T2FourReg_mac<0, 0b111, 0b0000, (outs rGPR:$Rd),
2609                       (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), NoItinerary,
2610                        "usada8", "\t$Rd, $Rn, $Rm, $Ra",
2611          [(set rGPR:$Rd, (int_arm_usada8 rGPR:$Rn, rGPR:$Rm, rGPR:$Ra))]>,
2612          Requires<[IsThumb2, HasDSP]>;
2613
2614// Signed/Unsigned saturate.
2615class T2SatI<dag iops, string opc, string asm>
2616  : T2I<(outs rGPR:$Rd), iops, NoItinerary, opc, asm, []> {
2617  bits<4> Rd;
2618  bits<4> Rn;
2619  bits<5> sat_imm;
2620  bits<6> sh;
2621
2622  let Inst{31-24} = 0b11110011;
2623  let Inst{21} = sh{5};
2624  let Inst{20} = 0;
2625  let Inst{19-16} = Rn;
2626  let Inst{15} = 0;
2627  let Inst{14-12} = sh{4-2};
2628  let Inst{11-8}  = Rd;
2629  let Inst{7-6} = sh{1-0};
2630  let Inst{5} = 0;
2631  let Inst{4-0}   = sat_imm;
2632}
2633
2634def t2SSAT: T2SatI<(ins imm1_32:$sat_imm, rGPR:$Rn, t2_shift_imm:$sh),
2635                   "ssat", "\t$Rd, $sat_imm, $Rn$sh">,
2636                   Requires<[IsThumb2]>, Sched<[WriteALU]> {
2637  let Inst{23-22} = 0b00;
2638  let Inst{5}  = 0;
2639}
2640
2641def t2SSAT16: T2SatI<(ins imm1_16:$sat_imm, rGPR:$Rn),
2642                     "ssat16", "\t$Rd, $sat_imm, $Rn">,
2643                     Requires<[IsThumb2, HasDSP]>, Sched<[WriteALU]> {
2644  let Inst{23-22} = 0b00;
2645  let sh = 0b100000;
2646  let Inst{4} = 0;
2647}
2648
2649def t2USAT: T2SatI<(ins imm0_31:$sat_imm, rGPR:$Rn, t2_shift_imm:$sh),
2650                    "usat", "\t$Rd, $sat_imm, $Rn$sh">,
2651                    Requires<[IsThumb2]>, Sched<[WriteALU]> {
2652  let Inst{23-22} = 0b10;
2653}
2654
2655def t2USAT16: T2SatI<(ins imm0_15:$sat_imm, rGPR:$Rn),
2656                     "usat16", "\t$Rd, $sat_imm, $Rn">,
2657                     Requires<[IsThumb2, HasDSP]>, Sched<[WriteALU]> {
2658  let Inst{23-22} = 0b10;
2659  let sh = 0b100000;
2660  let Inst{4} = 0;
2661}
2662
2663def : T2Pat<(ARMssat GPRnopc:$Rn, imm0_31:$imm),
2664             (t2SSAT imm0_31:$imm, GPRnopc:$Rn, 0)>;
2665def : T2Pat<(ARMusat GPRnopc:$Rn, imm0_31:$imm),
2666             (t2USAT imm0_31:$imm, GPRnopc:$Rn, 0)>;
2667def : T2Pat<(int_arm_ssat GPR:$a, imm1_32:$pos),
2668            (t2SSAT imm1_32:$pos, GPR:$a, 0)>;
2669def : T2Pat<(int_arm_usat GPR:$a, imm0_31:$pos),
2670            (t2USAT imm0_31:$pos, GPR:$a, 0)>;
2671def : T2Pat<(int_arm_ssat16 GPR:$a, imm1_16:$pos),
2672            (t2SSAT16 imm1_16:$pos, GPR:$a)>;
2673def : T2Pat<(int_arm_usat16 GPR:$a, imm0_15:$pos),
2674            (t2USAT16 imm0_15:$pos, GPR:$a)>;
2675def : T2Pat<(int_arm_ssat (shl GPRnopc:$a, imm0_31:$shft), imm1_32:$pos),
2676            (t2SSAT imm1_32:$pos, GPRnopc:$a, imm0_31:$shft)>;
2677def : T2Pat<(int_arm_ssat (sra GPRnopc:$a, asr_imm:$shft), imm1_32:$pos),
2678            (t2SSAT imm1_32:$pos, GPRnopc:$a, asr_imm:$shft)>;
2679def : T2Pat<(int_arm_usat (shl GPRnopc:$a, imm0_31:$shft), imm0_31:$pos),
2680            (t2USAT imm0_31:$pos, GPRnopc:$a, imm0_31:$shft)>;
2681def : T2Pat<(int_arm_usat (sra GPRnopc:$a, asr_imm:$shft), imm0_31:$pos),
2682            (t2USAT imm0_31:$pos, GPRnopc:$a, asr_imm:$shft)>;
2683def : T2Pat<(ARMssat (shl GPRnopc:$a, imm0_31:$shft), imm0_31:$pos),
2684            (t2SSAT imm0_31:$pos, GPRnopc:$a, imm0_31:$shft)>;
2685def : T2Pat<(ARMssat (sra GPRnopc:$Rn, asr_imm:$shft), imm0_31:$pos),
2686            (t2SSAT imm0_31:$pos, GPRnopc:$Rn, asr_imm:$shft)>;
2687def : T2Pat<(ARMusat (shl GPRnopc:$a, imm0_31:$shft), imm0_31:$pos),
2688            (t2USAT imm0_31:$pos, GPRnopc:$a, imm0_31:$shft)>;
2689def : T2Pat<(ARMusat (sra GPRnopc:$Rn, asr_imm:$shft), imm0_31:$pos),
2690            (t2USAT imm0_31:$pos, GPRnopc:$Rn, asr_imm:$shft)>;
2691
2692
2693//===----------------------------------------------------------------------===//
2694//  Shift and rotate Instructions.
2695//
2696
2697defm t2LSL  : T2I_sh_ir<0b00, "lsl", imm1_31, shl>;
2698defm t2LSR  : T2I_sh_ir<0b01, "lsr", imm_sr,  srl>;
2699defm t2ASR  : T2I_sh_ir<0b10, "asr", imm_sr,  sra>;
2700defm t2ROR  : T2I_sh_ir<0b11, "ror", imm1_31, rotr>;
2701
2702// LSL #0 is actually MOV, and has slightly different permitted registers to
2703// LSL with non-zero shift
2704def : t2InstAlias<"lsl${s}${p} $Rd, $Rm, #0",
2705                  (t2MOVr GPRnopc:$Rd, GPRnopc:$Rm, pred:$p, cc_out:$s)>;
2706def : t2InstAlias<"lsl${s}${p}.w $Rd, $Rm, #0",
2707                  (t2MOVr GPRnopc:$Rd, GPRnopc:$Rm, pred:$p, cc_out:$s)>;
2708
2709// (rotr x, (and y, 0x...1f)) ==> (ROR x, y)
2710def : T2Pat<(rotr rGPR:$lhs, (and rGPR:$rhs, lo5AllOne)),
2711            (t2RORrr rGPR:$lhs, rGPR:$rhs)>;
2712
2713let Uses = [CPSR] in {
2714def t2RRX : T2sTwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iMOVsi,
2715                   "rrx", "\t$Rd, $Rm",
2716                   [(set rGPR:$Rd, (ARMrrx rGPR:$Rm))]>, Sched<[WriteALU]> {
2717  let Inst{31-27} = 0b11101;
2718  let Inst{26-25} = 0b01;
2719  let Inst{24-21} = 0b0010;
2720  let Inst{19-16} = 0b1111; // Rn
2721  let Inst{15} = 0b0;
2722  let Unpredictable{15} = 0b1;
2723  let Inst{14-12} = 0b000;
2724  let Inst{7-4} = 0b0011;
2725}
2726}
2727
2728let isCodeGenOnly = 1, Defs = [CPSR] in {
2729def t2MOVsrl_flag : T2TwoRegShiftImm<
2730                        (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iMOVsi,
2731                        "lsrs", ".w\t$Rd, $Rm, #1",
2732                        [(set rGPR:$Rd, (ARMsrl_flag rGPR:$Rm))]>,
2733                        Sched<[WriteALU]> {
2734  let Inst{31-27} = 0b11101;
2735  let Inst{26-25} = 0b01;
2736  let Inst{24-21} = 0b0010;
2737  let Inst{20} = 1; // The S bit.
2738  let Inst{19-16} = 0b1111; // Rn
2739  let Inst{5-4} = 0b01; // Shift type.
2740  // Shift amount = Inst{14-12:7-6} = 1.
2741  let Inst{14-12} = 0b000;
2742  let Inst{7-6} = 0b01;
2743}
2744def t2MOVsra_flag : T2TwoRegShiftImm<
2745                        (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iMOVsi,
2746                        "asrs", ".w\t$Rd, $Rm, #1",
2747                        [(set rGPR:$Rd, (ARMsra_flag rGPR:$Rm))]>,
2748                        Sched<[WriteALU]> {
2749  let Inst{31-27} = 0b11101;
2750  let Inst{26-25} = 0b01;
2751  let Inst{24-21} = 0b0010;
2752  let Inst{20} = 1; // The S bit.
2753  let Inst{19-16} = 0b1111; // Rn
2754  let Inst{5-4} = 0b10; // Shift type.
2755  // Shift amount = Inst{14-12:7-6} = 1.
2756  let Inst{14-12} = 0b000;
2757  let Inst{7-6} = 0b01;
2758}
2759}
2760
2761//===----------------------------------------------------------------------===//
2762//  Bitwise Instructions.
2763//
2764
2765defm t2AND  : T2I_bin_w_irs<0b0000, "and",
2766                            IIC_iBITi, IIC_iBITr, IIC_iBITsi, and, 1>;
2767defm t2ORR  : T2I_bin_w_irs<0b0010, "orr",
2768                            IIC_iBITi, IIC_iBITr, IIC_iBITsi, or, 1>;
2769defm t2EOR  : T2I_bin_w_irs<0b0100, "eor",
2770                            IIC_iBITi, IIC_iBITr, IIC_iBITsi, xor, 1>;
2771
2772defm t2BIC  : T2I_bin_w_irs<0b0001, "bic",
2773                            IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2774                            BinOpFrag<(and node:$LHS, (not node:$RHS))>>;
2775
2776class T2BitFI<dag oops, dag iops, InstrItinClass itin,
2777              string opc, string asm, list<dag> pattern>
2778    : T2I<oops, iops, itin, opc, asm, pattern> {
2779  bits<4> Rd;
2780  bits<5> msb;
2781  bits<5> lsb;
2782
2783  let Inst{11-8}  = Rd;
2784  let Inst{4-0}   = msb{4-0};
2785  let Inst{14-12} = lsb{4-2};
2786  let Inst{7-6}   = lsb{1-0};
2787}
2788
2789class T2TwoRegBitFI<dag oops, dag iops, InstrItinClass itin,
2790              string opc, string asm, list<dag> pattern>
2791    : T2BitFI<oops, iops, itin, opc, asm, pattern> {
2792  bits<4> Rn;
2793
2794  let Inst{19-16} = Rn;
2795}
2796
2797let Constraints = "$src = $Rd" in
2798def t2BFC : T2BitFI<(outs rGPR:$Rd), (ins rGPR:$src, bf_inv_mask_imm:$imm),
2799                IIC_iUNAsi, "bfc", "\t$Rd, $imm",
2800                [(set rGPR:$Rd, (and rGPR:$src, bf_inv_mask_imm:$imm))]>, Sched<[WriteALU]> {
2801  let Inst{31-27} = 0b11110;
2802  let Inst{26} = 0; // should be 0.
2803  let Inst{25} = 1;
2804  let Inst{24-20} = 0b10110;
2805  let Inst{19-16} = 0b1111; // Rn
2806  let Inst{15} = 0;
2807  let Inst{5} = 0; // should be 0.
2808
2809  bits<10> imm;
2810  let msb{4-0} = imm{9-5};
2811  let lsb{4-0} = imm{4-0};
2812}
2813
2814def t2SBFX: T2TwoRegBitFI<
2815                (outs rGPR:$Rd), (ins rGPR:$Rn, imm0_31:$lsb, imm1_32:$msb),
2816                 IIC_iUNAsi, "sbfx", "\t$Rd, $Rn, $lsb, $msb", []>, Sched<[WriteALU]> {
2817  let Inst{31-27} = 0b11110;
2818  let Inst{25} = 1;
2819  let Inst{24-20} = 0b10100;
2820  let Inst{15} = 0;
2821
2822  let hasSideEffects = 0;
2823}
2824
2825def t2UBFX: T2TwoRegBitFI<
2826                (outs rGPR:$Rd), (ins rGPR:$Rn, imm0_31:$lsb, imm1_32:$msb),
2827                 IIC_iUNAsi, "ubfx", "\t$Rd, $Rn, $lsb, $msb", []>, Sched<[WriteALU]> {
2828  let Inst{31-27} = 0b11110;
2829  let Inst{25} = 1;
2830  let Inst{24-20} = 0b11100;
2831  let Inst{15} = 0;
2832
2833  let hasSideEffects = 0;
2834}
2835
2836// A8.8.247  UDF - Undefined (Encoding T2)
2837def t2UDF : T2XI<(outs), (ins imm0_65535:$imm16), IIC_Br, "udf.w\t$imm16",
2838                 [(int_arm_undefined imm0_65535:$imm16)]> {
2839  bits<16> imm16;
2840  let Inst{31-29} = 0b111;
2841  let Inst{28-27} = 0b10;
2842  let Inst{26-20} = 0b1111111;
2843  let Inst{19-16} = imm16{15-12};
2844  let Inst{15} = 0b1;
2845  let Inst{14-12} = 0b010;
2846  let Inst{11-0} = imm16{11-0};
2847}
2848
2849// A8.6.18  BFI - Bitfield insert (Encoding T1)
2850let Constraints = "$src = $Rd" in {
2851  def t2BFI : T2TwoRegBitFI<(outs rGPR:$Rd),
2852                  (ins rGPR:$src, rGPR:$Rn, bf_inv_mask_imm:$imm),
2853                  IIC_iBITi, "bfi", "\t$Rd, $Rn, $imm",
2854                  [(set rGPR:$Rd, (ARMbfi rGPR:$src, rGPR:$Rn,
2855                                   bf_inv_mask_imm:$imm))]>, Sched<[WriteALU]> {
2856    let Inst{31-27} = 0b11110;
2857    let Inst{26} = 0; // should be 0.
2858    let Inst{25} = 1;
2859    let Inst{24-20} = 0b10110;
2860    let Inst{15} = 0;
2861    let Inst{5} = 0; // should be 0.
2862
2863    bits<10> imm;
2864    let msb{4-0} = imm{9-5};
2865    let lsb{4-0} = imm{4-0};
2866  }
2867}
2868
2869defm t2ORN  : T2I_bin_irs<0b0011, "orn",
2870                          IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2871                          BinOpFrag<(or node:$LHS, (not node:$RHS))>, 0, "">;
2872def : t2InstAlias<"orn${s}${p}.w $Rd, $Rn, $imm",
2873   (t2ORNri rGPR:$Rd, rGPR:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
2874def : t2InstAlias<"orn${s}${p}.w $Rd, $Rn, $Rm",
2875   (t2ORNrr rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
2876def : t2InstAlias<"orn${s}${p}.w $Rd, $Rn, $ShiftedRm",
2877   (t2ORNrs rGPR:$Rd, rGPR:$Rn, t2_so_reg:$ShiftedRm, pred:$p, cc_out:$s)>;
2878
2879/// T2I_un_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns for a
2880/// unary operation that produces a value. These are predicable and can be
2881/// changed to modify CPSR.
2882multiclass T2I_un_irs<bits<4> opcod, string opc,
2883                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
2884                      PatFrag opnode,
2885                      bit Cheap = 0, bit ReMat = 0, bit MoveImm = 0> {
2886   // shifted imm
2887   def i : T2sOneRegImm<(outs rGPR:$Rd), (ins t2_so_imm:$imm), iii,
2888                opc, "\t$Rd, $imm",
2889                [(set rGPR:$Rd, (opnode t2_so_imm:$imm))]>, Sched<[WriteALU]> {
2890     let isAsCheapAsAMove = Cheap;
2891     let isReMaterializable = ReMat;
2892     let isMoveImm = MoveImm;
2893     let Inst{31-27} = 0b11110;
2894     let Inst{25} = 0;
2895     let Inst{24-21} = opcod;
2896     let Inst{19-16} = 0b1111; // Rn
2897     let Inst{15} = 0;
2898   }
2899   // register
2900   def r : T2sTwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm), iir,
2901                opc, ".w\t$Rd, $Rm",
2902                [(set rGPR:$Rd, (opnode rGPR:$Rm))]>, Sched<[WriteALU]> {
2903     let Inst{31-27} = 0b11101;
2904     let Inst{26-25} = 0b01;
2905     let Inst{24-21} = opcod;
2906     let Inst{19-16} = 0b1111; // Rn
2907     let Inst{14-12} = 0b000; // imm3
2908     let Inst{7-6} = 0b00; // imm2
2909     let Inst{5-4} = 0b00; // type
2910   }
2911   // shifted register
2912   def s : T2sOneRegShiftedReg<(outs rGPR:$Rd), (ins t2_so_reg:$ShiftedRm), iis,
2913                opc, ".w\t$Rd, $ShiftedRm",
2914                [(set rGPR:$Rd, (opnode t2_so_reg:$ShiftedRm))]>,
2915                Sched<[WriteALU]> {
2916     let Inst{31-27} = 0b11101;
2917     let Inst{26-25} = 0b01;
2918     let Inst{24-21} = opcod;
2919     let Inst{19-16} = 0b1111; // Rn
2920   }
2921}
2922
2923// Prefer over of t2EORri ra, rb, -1 because mvn has 16-bit version
2924let AddedComplexity = 1 in
2925defm t2MVN  : T2I_un_irs <0b0011, "mvn",
2926                          IIC_iMVNi, IIC_iMVNr, IIC_iMVNsi,
2927                          not, 1, 1, 1>;
2928
2929let AddedComplexity = 1 in
2930def : T2Pat<(and     rGPR:$src, t2_so_imm_not:$imm),
2931            (t2BICri rGPR:$src, t2_so_imm_not:$imm)>;
2932
2933// so_imm_notSext is needed instead of so_imm_not, as the value of imm
2934// will match the extended, not the original bitWidth for $src.
2935def : T2Pat<(and top16Zero:$src, t2_so_imm_notSext:$imm),
2936            (t2BICri rGPR:$src, t2_so_imm_notSext:$imm)>;
2937
2938// FIXME: Disable this pattern on Darwin to workaround an assembler bug.
2939def : T2Pat<(or      rGPR:$src, t2_so_imm_not:$imm),
2940            (t2ORNri rGPR:$src, t2_so_imm_not:$imm)>,
2941            Requires<[IsThumb2]>;
2942
2943def : T2Pat<(t2_so_imm_not:$src),
2944            (t2MVNi t2_so_imm_not:$src)>;
2945
2946// There are shorter Thumb encodings for ADD than ORR, so to increase
2947// Thumb2SizeReduction's chances later on we select a t2ADD for an or where
2948// possible.
2949def : T2Pat<(or AddLikeOrOp:$Rn, t2_so_imm:$imm),
2950            (t2ADDri rGPR:$Rn, t2_so_imm:$imm)>;
2951
2952def : T2Pat<(or AddLikeOrOp:$Rn, imm0_4095:$Rm),
2953            (t2ADDri12 rGPR:$Rn, imm0_4095:$Rm)>;
2954
2955def : T2Pat<(or AddLikeOrOp:$Rn, non_imm32:$Rm),
2956            (t2ADDrr $Rn, $Rm)>;
2957
2958//===----------------------------------------------------------------------===//
2959//  Multiply Instructions.
2960//
2961let isCommutable = 1 in
2962def t2MUL: T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL32,
2963                "mul", "\t$Rd, $Rn, $Rm",
2964                [(set rGPR:$Rd, (mul rGPR:$Rn, rGPR:$Rm))]>,
2965           Sched<[WriteMUL32, ReadMUL, ReadMUL]> {
2966  let Inst{31-27} = 0b11111;
2967  let Inst{26-23} = 0b0110;
2968  let Inst{22-20} = 0b000;
2969  let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2970  let Inst{7-4} = 0b0000; // Multiply
2971}
2972
2973class T2FourRegMLA<bits<4> op7_4, string opc, list<dag> pattern>
2974  : T2FourReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2975               opc, "\t$Rd, $Rn, $Rm, $Ra", pattern>,
2976               Requires<[IsThumb2, UseMulOps]>,
2977    Sched<[WriteMAC32, ReadMUL, ReadMUL, ReadMAC]>  {
2978  let Inst{31-27} = 0b11111;
2979  let Inst{26-23} = 0b0110;
2980  let Inst{22-20} = 0b000;
2981  let Inst{7-4} = op7_4;
2982}
2983
2984def t2MLA : T2FourRegMLA<0b0000, "mla",
2985                         [(set rGPR:$Rd, (add (mul rGPR:$Rn, rGPR:$Rm),
2986                                               rGPR:$Ra))]>;
2987def t2MLS: T2FourRegMLA<0b0001, "mls",
2988                        [(set rGPR:$Rd, (sub rGPR:$Ra, (mul rGPR:$Rn,
2989                                                            rGPR:$Rm)))]>;
2990
2991// Extra precision multiplies with low / high results
2992let hasSideEffects = 0 in {
2993let isCommutable = 1 in {
2994def t2SMULL : T2MulLong<0b000, 0b0000, "smull",
2995                        [(set rGPR:$RdLo, rGPR:$RdHi,
2996                              (smullohi rGPR:$Rn, rGPR:$Rm))]>;
2997def t2UMULL : T2MulLong<0b010, 0b0000, "umull",
2998                        [(set rGPR:$RdLo, rGPR:$RdHi,
2999                              (umullohi rGPR:$Rn, rGPR:$Rm))]>;
3000} // isCommutable
3001
3002// Multiply + accumulate
3003def t2SMLAL : T2MlaLong<0b100, 0b0000, "smlal">;
3004def t2UMLAL : T2MlaLong<0b110, 0b0000, "umlal">;
3005def t2UMAAL : T2MlaLong<0b110, 0b0110, "umaal">, Requires<[IsThumb2, HasDSP]>;
3006} // hasSideEffects
3007
3008// Rounding variants of the below included for disassembly only
3009
3010// Most significant word multiply
3011class T2SMMUL<bits<4> op7_4, string opc, list<dag> pattern>
3012  : T2ThreeReg<(outs rGPR:$Rd),
3013               (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL32,
3014               opc, "\t$Rd, $Rn, $Rm", pattern>,
3015               Requires<[IsThumb2, HasDSP]>,
3016    Sched<[WriteMUL32, ReadMUL, ReadMUL]> {
3017  let Inst{31-27} = 0b11111;
3018  let Inst{26-23} = 0b0110;
3019  let Inst{22-20} = 0b101;
3020  let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
3021  let Inst{7-4} = op7_4;
3022}
3023def t2SMMUL : T2SMMUL<0b0000, "smmul", [(set rGPR:$Rd, (mulhs rGPR:$Rn,
3024                                                              rGPR:$Rm))]>;
3025def t2SMMULR :
3026  T2SMMUL<0b0001, "smmulr",
3027          [(set rGPR:$Rd, (ARMsmmlar rGPR:$Rn, rGPR:$Rm, (i32 0)))]>;
3028
3029class T2FourRegSMMLA<bits<3> op22_20, bits<4> op7_4, string opc,
3030                     list<dag> pattern>
3031  : T2FourReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
3032              opc, "\t$Rd, $Rn, $Rm, $Ra", pattern>,
3033              Requires<[IsThumb2, HasDSP, UseMulOps]>,
3034    Sched<[WriteMAC32, ReadMUL, ReadMUL, ReadMAC]> {
3035  let Inst{31-27} = 0b11111;
3036  let Inst{26-23} = 0b0110;
3037  let Inst{22-20} = op22_20;
3038  let Inst{7-4} = op7_4;
3039}
3040
3041def t2SMMLA :   T2FourRegSMMLA<0b101, 0b0000, "smmla",
3042                [(set rGPR:$Rd, (add (mulhs rGPR:$Rm, rGPR:$Rn), rGPR:$Ra))]>;
3043def t2SMMLAR:   T2FourRegSMMLA<0b101, 0b0001, "smmlar",
3044                [(set rGPR:$Rd, (ARMsmmlar rGPR:$Rn, rGPR:$Rm, rGPR:$Ra))]>;
3045def t2SMMLS:    T2FourRegSMMLA<0b110, 0b0000, "smmls", []>;
3046def t2SMMLSR:   T2FourRegSMMLA<0b110, 0b0001, "smmlsr",
3047                [(set rGPR:$Rd, (ARMsmmlsr rGPR:$Rn, rGPR:$Rm, rGPR:$Ra))]>;
3048
3049class T2ThreeRegSMUL<bits<3> op22_20, bits<2> op5_4, string opc,
3050                     list<dag> pattern>
3051  : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16, opc,
3052               "\t$Rd, $Rn, $Rm", pattern>,
3053    Requires<[IsThumb2, HasDSP]>,
3054    Sched<[WriteMUL16, ReadMUL, ReadMUL]> {
3055    let Inst{31-27} = 0b11111;
3056    let Inst{26-23} = 0b0110;
3057    let Inst{22-20} = op22_20;
3058    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
3059    let Inst{7-6} = 0b00;
3060    let Inst{5-4} = op5_4;
3061}
3062
3063def t2SMULBB : T2ThreeRegSMUL<0b001, 0b00, "smulbb",
3064             [(set rGPR:$Rd, (bb_mul rGPR:$Rn, rGPR:$Rm))]>;
3065def t2SMULBT : T2ThreeRegSMUL<0b001, 0b01, "smulbt",
3066             [(set rGPR:$Rd, (bt_mul rGPR:$Rn, rGPR:$Rm))]>;
3067def t2SMULTB : T2ThreeRegSMUL<0b001, 0b10, "smultb",
3068             [(set rGPR:$Rd, (tb_mul rGPR:$Rn, rGPR:$Rm))]>;
3069def t2SMULTT : T2ThreeRegSMUL<0b001, 0b11, "smultt",
3070             [(set rGPR:$Rd, (tt_mul rGPR:$Rn, rGPR:$Rm))]>;
3071def t2SMULWB : T2ThreeRegSMUL<0b011, 0b00, "smulwb",
3072             [(set rGPR:$Rd, (ARMsmulwb rGPR:$Rn, rGPR:$Rm))]>;
3073def t2SMULWT : T2ThreeRegSMUL<0b011, 0b01, "smulwt",
3074             [(set rGPR:$Rd, (ARMsmulwt rGPR:$Rn, rGPR:$Rm))]>;
3075
3076def : Thumb2DSPPat<(mul sext_16_node:$Rn, (sext_bottom_16 rGPR:$Rm)),
3077                   (t2SMULBB rGPR:$Rn, rGPR:$Rm)>;
3078def : Thumb2DSPPat<(mul sext_16_node:$Rn, (sext_top_16 rGPR:$Rm)),
3079                   (t2SMULBT rGPR:$Rn, rGPR:$Rm)>;
3080def : Thumb2DSPPat<(mul (sext_top_16 rGPR:$Rn), sext_16_node:$Rm),
3081                   (t2SMULTB rGPR:$Rn, rGPR:$Rm)>;
3082
3083def : Thumb2DSPPat<(int_arm_smulbb rGPR:$Rn, rGPR:$Rm),
3084                   (t2SMULBB rGPR:$Rn, rGPR:$Rm)>;
3085def : Thumb2DSPPat<(int_arm_smulbt rGPR:$Rn, rGPR:$Rm),
3086                   (t2SMULBT rGPR:$Rn, rGPR:$Rm)>;
3087def : Thumb2DSPPat<(int_arm_smultb rGPR:$Rn, rGPR:$Rm),
3088                   (t2SMULTB rGPR:$Rn, rGPR:$Rm)>;
3089def : Thumb2DSPPat<(int_arm_smultt rGPR:$Rn, rGPR:$Rm),
3090                   (t2SMULTT rGPR:$Rn, rGPR:$Rm)>;
3091def : Thumb2DSPPat<(int_arm_smulwb rGPR:$Rn, rGPR:$Rm),
3092                   (t2SMULWB rGPR:$Rn, rGPR:$Rm)>;
3093def : Thumb2DSPPat<(int_arm_smulwt rGPR:$Rn, rGPR:$Rm),
3094                   (t2SMULWT rGPR:$Rn, rGPR:$Rm)>;
3095
3096class T2FourRegSMLA<bits<3> op22_20, bits<2> op5_4, string opc,
3097                    list<dag> pattern>
3098  : T2FourReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMUL16,
3099               opc, "\t$Rd, $Rn, $Rm, $Ra", pattern>,
3100    Requires<[IsThumb2, HasDSP, UseMulOps]>,
3101    Sched<[WriteMAC16, ReadMUL, ReadMUL, ReadMAC]>  {
3102    let Inst{31-27} = 0b11111;
3103    let Inst{26-23} = 0b0110;
3104    let Inst{22-20} = op22_20;
3105    let Inst{7-6} = 0b00;
3106    let Inst{5-4} = op5_4;
3107}
3108
3109def t2SMLABB : T2FourRegSMLA<0b001, 0b00, "smlabb",
3110             [(set rGPR:$Rd, (add rGPR:$Ra, (bb_mul rGPR:$Rn, rGPR:$Rm)))]>;
3111def t2SMLABT : T2FourRegSMLA<0b001, 0b01, "smlabt",
3112             [(set rGPR:$Rd, (add rGPR:$Ra, (bt_mul rGPR:$Rn, rGPR:$Rm)))]>;
3113def t2SMLATB : T2FourRegSMLA<0b001, 0b10, "smlatb",
3114             [(set rGPR:$Rd, (add rGPR:$Ra, (tb_mul rGPR:$Rn, rGPR:$Rm)))]>;
3115def t2SMLATT : T2FourRegSMLA<0b001, 0b11, "smlatt",
3116             [(set rGPR:$Rd, (add rGPR:$Ra, (tt_mul rGPR:$Rn, rGPR:$Rm)))]>;
3117def t2SMLAWB : T2FourRegSMLA<0b011, 0b00, "smlawb",
3118             [(set rGPR:$Rd, (add rGPR:$Ra, (ARMsmulwb rGPR:$Rn, rGPR:$Rm)))]>;
3119def t2SMLAWT : T2FourRegSMLA<0b011, 0b01, "smlawt",
3120             [(set rGPR:$Rd, (add rGPR:$Ra, (ARMsmulwt rGPR:$Rn, rGPR:$Rm)))]>;
3121
3122def : Thumb2DSPMulPat<(add rGPR:$Ra, (mul sext_16_node:$Rn, sext_16_node:$Rm)),
3123                      (t2SMLABB rGPR:$Rn, rGPR:$Rm, rGPR:$Ra)>;
3124def : Thumb2DSPMulPat<(add rGPR:$Ra, (mul sext_16_node:$Rn,
3125                                          (sext_bottom_16 rGPR:$Rm))),
3126                      (t2SMLABB rGPR:$Rn, rGPR:$Rm, rGPR:$Ra)>;
3127def : Thumb2DSPMulPat<(add rGPR:$Ra, (mul sext_16_node:$Rn,
3128                                          (sext_top_16 rGPR:$Rm))),
3129                      (t2SMLABT rGPR:$Rn, rGPR:$Rm, rGPR:$Ra)>;
3130def : Thumb2DSPMulPat<(add rGPR:$Ra, (mul (sext_top_16 rGPR:$Rn),
3131                                          sext_16_node:$Rm)),
3132                      (t2SMLATB rGPR:$Rn, rGPR:$Rm, rGPR:$Ra)>;
3133
3134def : Thumb2DSPPat<(int_arm_smlabb GPR:$a, GPR:$b, GPR:$acc),
3135                   (t2SMLABB GPR:$a, GPR:$b, GPR:$acc)>;
3136def : Thumb2DSPPat<(int_arm_smlabt GPR:$a, GPR:$b, GPR:$acc),
3137                   (t2SMLABT GPR:$a, GPR:$b, GPR:$acc)>;
3138def : Thumb2DSPPat<(int_arm_smlatb GPR:$a, GPR:$b, GPR:$acc),
3139                   (t2SMLATB GPR:$a, GPR:$b, GPR:$acc)>;
3140def : Thumb2DSPPat<(int_arm_smlatt GPR:$a, GPR:$b, GPR:$acc),
3141                   (t2SMLATT GPR:$a, GPR:$b, GPR:$acc)>;
3142def : Thumb2DSPPat<(int_arm_smlawb GPR:$a, GPR:$b, GPR:$acc),
3143                   (t2SMLAWB GPR:$a, GPR:$b, GPR:$acc)>;
3144def : Thumb2DSPPat<(int_arm_smlawt GPR:$a, GPR:$b, GPR:$acc),
3145                   (t2SMLAWT GPR:$a, GPR:$b, GPR:$acc)>;
3146
3147// Halfword multiple accumulate long: SMLAL<x><y>
3148def t2SMLALBB : T2MlaLong<0b100, 0b1000, "smlalbb">,
3149                          Requires<[IsThumb2, HasDSP]>;
3150def t2SMLALBT : T2MlaLong<0b100, 0b1001, "smlalbt">,
3151                          Requires<[IsThumb2, HasDSP]>;
3152def t2SMLALTB : T2MlaLong<0b100, 0b1010, "smlaltb">,
3153                          Requires<[IsThumb2, HasDSP]>;
3154def t2SMLALTT : T2MlaLong<0b100, 0b1011, "smlaltt">,
3155                          Requires<[IsThumb2, HasDSP]>;
3156
3157def : Thumb2DSPPat<(ARMsmlalbb GPR:$Rn, GPR:$Rm, GPR:$RLo, GPR:$RHi),
3158                   (t2SMLALBB $Rn, $Rm, $RLo, $RHi)>;
3159def : Thumb2DSPPat<(ARMsmlalbt GPR:$Rn, GPR:$Rm, GPR:$RLo, GPR:$RHi),
3160                   (t2SMLALBT $Rn, $Rm, $RLo, $RHi)>;
3161def : Thumb2DSPPat<(ARMsmlaltb GPR:$Rn, GPR:$Rm, GPR:$RLo, GPR:$RHi),
3162                   (t2SMLALTB $Rn, $Rm, $RLo, $RHi)>;
3163def : Thumb2DSPPat<(ARMsmlaltt GPR:$Rn, GPR:$Rm, GPR:$RLo, GPR:$RHi),
3164                   (t2SMLALTT $Rn, $Rm, $RLo, $RHi)>;
3165
3166class T2DualHalfMul<bits<3> op22_20, bits<4> op7_4, string opc,
3167                    Intrinsic intrinsic>
3168  : T2ThreeReg_mac<0, op22_20, op7_4,
3169                   (outs rGPR:$Rd),
3170                   (ins rGPR:$Rn, rGPR:$Rm),
3171                   IIC_iMAC32, opc, "\t$Rd, $Rn, $Rm",
3172                   [(set rGPR:$Rd, (intrinsic rGPR:$Rn, rGPR:$Rm))]>,
3173                   Requires<[IsThumb2, HasDSP]>,
3174   Sched<[WriteMAC32, ReadMUL, ReadMUL, ReadMAC]> {
3175  let Inst{15-12} = 0b1111;
3176}
3177
3178// Dual halfword multiple: SMUAD, SMUSD, SMLAD, SMLSD, SMLALD, SMLSLD
3179def t2SMUAD: T2DualHalfMul<0b010, 0b0000, "smuad", int_arm_smuad>;
3180def t2SMUADX: T2DualHalfMul<0b010, 0b0001, "smuadx", int_arm_smuadx>;
3181def t2SMUSD: T2DualHalfMul<0b100, 0b0000, "smusd", int_arm_smusd>;
3182def t2SMUSDX: T2DualHalfMul<0b100, 0b0001, "smusdx", int_arm_smusdx>;
3183
3184class T2DualHalfMulAdd<bits<3> op22_20, bits<4> op7_4, string opc,
3185                       Intrinsic intrinsic>
3186  : T2FourReg_mac<0, op22_20, op7_4,
3187                  (outs rGPR:$Rd),
3188                  (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra),
3189                  IIC_iMAC32, opc, "\t$Rd, $Rn, $Rm, $Ra",
3190                  [(set rGPR:$Rd, (intrinsic rGPR:$Rn, rGPR:$Rm, rGPR:$Ra))]>,
3191                  Requires<[IsThumb2, HasDSP]>;
3192
3193def t2SMLAD   : T2DualHalfMulAdd<0b010, 0b0000, "smlad", int_arm_smlad>;
3194def t2SMLADX  : T2DualHalfMulAdd<0b010, 0b0001, "smladx", int_arm_smladx>;
3195def t2SMLSD   : T2DualHalfMulAdd<0b100, 0b0000, "smlsd", int_arm_smlsd>;
3196def t2SMLSDX  : T2DualHalfMulAdd<0b100, 0b0001, "smlsdx", int_arm_smlsdx>;
3197
3198class T2DualHalfMulAddLong<bits<3> op22_20, bits<4> op7_4, string opc>
3199  : T2FourReg_mac<1, op22_20, op7_4,
3200                  (outs rGPR:$Ra, rGPR:$Rd),
3201                  (ins rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi),
3202                  IIC_iMAC64, opc, "\t$Ra, $Rd, $Rn, $Rm", []>,
3203                  RegConstraint<"$Ra = $RLo, $Rd = $RHi">,
3204                  Requires<[IsThumb2, HasDSP]>,
3205    Sched<[WriteMAC64Lo, WriteMAC64Hi, ReadMUL, ReadMUL, ReadMAC, ReadMAC]>;
3206
3207def t2SMLALD  : T2DualHalfMulAddLong<0b100, 0b1100, "smlald">;
3208def t2SMLALDX : T2DualHalfMulAddLong<0b100, 0b1101, "smlaldx">;
3209def t2SMLSLD  : T2DualHalfMulAddLong<0b101, 0b1100, "smlsld">;
3210def t2SMLSLDX : T2DualHalfMulAddLong<0b101, 0b1101, "smlsldx">;
3211
3212def : Thumb2DSPPat<(ARMSmlald rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi),
3213                   (t2SMLALD rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi)>;
3214def : Thumb2DSPPat<(ARMSmlaldx rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi),
3215                   (t2SMLALDX rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi)>;
3216def : Thumb2DSPPat<(ARMSmlsld rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi),
3217                   (t2SMLSLD rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi)>;
3218def : Thumb2DSPPat<(ARMSmlsldx rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi),
3219                   (t2SMLSLDX rGPR:$Rn, rGPR:$Rm, rGPR:$RLo, rGPR:$RHi)>;
3220
3221//===----------------------------------------------------------------------===//
3222//  Division Instructions.
3223//  Signed and unsigned division on v7-M
3224//
3225def t2SDIV : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iDIV,
3226                 "sdiv", "\t$Rd, $Rn, $Rm",
3227                 [(set rGPR:$Rd, (sdiv rGPR:$Rn, rGPR:$Rm))]>,
3228                 Requires<[HasDivideInThumb, IsThumb, HasV8MBaseline]>,
3229             Sched<[WriteDIV]> {
3230  let Inst{31-27} = 0b11111;
3231  let Inst{26-21} = 0b011100;
3232  let Inst{20} = 0b1;
3233  let Inst{15-12} = 0b1111;
3234  let Inst{7-4} = 0b1111;
3235}
3236
3237def t2UDIV : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iDIV,
3238                 "udiv", "\t$Rd, $Rn, $Rm",
3239                 [(set rGPR:$Rd, (udiv rGPR:$Rn, rGPR:$Rm))]>,
3240                 Requires<[HasDivideInThumb, IsThumb, HasV8MBaseline]>,
3241             Sched<[WriteDIV]> {
3242  let Inst{31-27} = 0b11111;
3243  let Inst{26-21} = 0b011101;
3244  let Inst{20} = 0b1;
3245  let Inst{15-12} = 0b1111;
3246  let Inst{7-4} = 0b1111;
3247}
3248
3249//===----------------------------------------------------------------------===//
3250//  Misc. Arithmetic Instructions.
3251//
3252
3253class T2I_misc<bits<2> op1, bits<2> op2, dag oops, dag iops,
3254      InstrItinClass itin, string opc, string asm, list<dag> pattern>
3255  : T2ThreeReg<oops, iops, itin, opc, asm, pattern> {
3256  let Inst{31-27} = 0b11111;
3257  let Inst{26-22} = 0b01010;
3258  let Inst{21-20} = op1;
3259  let Inst{15-12} = 0b1111;
3260  let Inst{7-6} = 0b10;
3261  let Inst{5-4} = op2;
3262  let Rn{3-0} = Rm;
3263}
3264
3265def t2CLZ : T2I_misc<0b11, 0b00, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
3266                    "clz", "\t$Rd, $Rm", [(set rGPR:$Rd, (ctlz rGPR:$Rm))]>,
3267                    Sched<[WriteALU]>;
3268
3269def t2RBIT : T2I_misc<0b01, 0b10, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
3270                      "rbit", "\t$Rd, $Rm",
3271                      [(set rGPR:$Rd, (bitreverse rGPR:$Rm))]>,
3272                      Sched<[WriteALU]>;
3273
3274def t2REV : T2I_misc<0b01, 0b00, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
3275                 "rev", ".w\t$Rd, $Rm", [(set rGPR:$Rd, (bswap rGPR:$Rm))]>,
3276                 Sched<[WriteALU]>;
3277
3278def t2REV16 : T2I_misc<0b01, 0b01, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
3279                       "rev16", ".w\t$Rd, $Rm",
3280                [(set rGPR:$Rd, (rotr (bswap rGPR:$Rm), (i32 16)))]>,
3281                Sched<[WriteALU]>;
3282
3283def : T2Pat<(srl (bswap top16Zero:$Rn), (i32 16)),
3284            (t2REV16 rGPR:$Rn)>;
3285
3286def t2REVSH : T2I_misc<0b01, 0b11, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
3287                       "revsh", ".w\t$Rd, $Rm",
3288                 [(set rGPR:$Rd, (sra (bswap rGPR:$Rm), (i32 16)))]>,
3289                 Sched<[WriteALU]>;
3290
3291def : T2Pat<(or (sra (shl rGPR:$Rm, (i32 24)), (i32 16)),
3292                (and (srl rGPR:$Rm, (i32 8)), 0xFF)),
3293            (t2REVSH rGPR:$Rm)>;
3294
3295def t2PKHBT : T2ThreeReg<
3296            (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, pkh_lsl_amt:$sh),
3297                  IIC_iBITsi, "pkhbt", "\t$Rd, $Rn, $Rm$sh",
3298                  [(set rGPR:$Rd, (or (and rGPR:$Rn, 0xFFFF),
3299                                      (and (shl rGPR:$Rm, pkh_lsl_amt:$sh),
3300                                           0xFFFF0000)))]>,
3301                  Requires<[HasDSP, IsThumb2]>,
3302                  Sched<[WriteALUsi, ReadALU]> {
3303  let Inst{31-27} = 0b11101;
3304  let Inst{26-25} = 0b01;
3305  let Inst{24-20} = 0b01100;
3306  let Inst{5} = 0; // BT form
3307  let Inst{4} = 0;
3308
3309  bits<5> sh;
3310  let Inst{14-12} = sh{4-2};
3311  let Inst{7-6}   = sh{1-0};
3312}
3313
3314// Alternate cases for PKHBT where identities eliminate some nodes.
3315def : T2Pat<(or (and rGPR:$src1, 0xFFFF), (and rGPR:$src2, 0xFFFF0000)),
3316            (t2PKHBT rGPR:$src1, rGPR:$src2, 0)>,
3317            Requires<[HasDSP, IsThumb2]>;
3318def : T2Pat<(or (and rGPR:$src1, 0xFFFF), (shl rGPR:$src2, imm16_31:$sh)),
3319            (t2PKHBT rGPR:$src1, rGPR:$src2, imm16_31:$sh)>,
3320            Requires<[HasDSP, IsThumb2]>;
3321
3322// Note: Shifts of 1-15 bits will be transformed to srl instead of sra and
3323// will match the pattern below.
3324def t2PKHTB : T2ThreeReg<
3325                  (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, pkh_asr_amt:$sh),
3326                  IIC_iBITsi, "pkhtb", "\t$Rd, $Rn, $Rm$sh",
3327                  [(set rGPR:$Rd, (or (and rGPR:$Rn, 0xFFFF0000),
3328                                       (and (sra rGPR:$Rm, pkh_asr_amt:$sh),
3329                                            0xFFFF)))]>,
3330                  Requires<[HasDSP, IsThumb2]>,
3331                  Sched<[WriteALUsi, ReadALU]> {
3332  let Inst{31-27} = 0b11101;
3333  let Inst{26-25} = 0b01;
3334  let Inst{24-20} = 0b01100;
3335  let Inst{5} = 1; // TB form
3336  let Inst{4} = 0;
3337
3338  bits<5> sh;
3339  let Inst{14-12} = sh{4-2};
3340  let Inst{7-6}   = sh{1-0};
3341}
3342
3343// Alternate cases for PKHTB where identities eliminate some nodes.  Note that
3344// a shift amount of 0 is *not legal* here, it is PKHBT instead.
3345// We also can not replace a srl (17..31) by an arithmetic shift we would use in
3346// pkhtb src1, src2, asr (17..31).
3347def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000), (srl rGPR:$src2, imm16:$sh)),
3348            (t2PKHTB rGPR:$src1, rGPR:$src2, imm16:$sh)>,
3349            Requires<[HasDSP, IsThumb2]>;
3350def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000), (sra rGPR:$src2, imm16_31:$sh)),
3351            (t2PKHTB rGPR:$src1, rGPR:$src2, imm16_31:$sh)>,
3352            Requires<[HasDSP, IsThumb2]>;
3353def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000),
3354                (and (srl rGPR:$src2, imm1_15:$sh), 0xFFFF)),
3355            (t2PKHTB rGPR:$src1, rGPR:$src2, imm1_15:$sh)>,
3356            Requires<[HasDSP, IsThumb2]>;
3357
3358//===----------------------------------------------------------------------===//
3359// CRC32 Instructions
3360//
3361// Polynomials:
3362// + CRC32{B,H,W}       0x04C11DB7
3363// + CRC32C{B,H,W}      0x1EDC6F41
3364//
3365
3366class T2I_crc32<bit C, bits<2> sz, string suffix, SDPatternOperator builtin>
3367  : T2ThreeRegNoP<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), NoItinerary,
3368               !strconcat("crc32", suffix, "\t$Rd, $Rn, $Rm"),
3369               [(set rGPR:$Rd, (builtin rGPR:$Rn, rGPR:$Rm))]>,
3370               Requires<[IsThumb2, HasV8, HasCRC]> {
3371  let Inst{31-27} = 0b11111;
3372  let Inst{26-21} = 0b010110;
3373  let Inst{20}    = C;
3374  let Inst{15-12} = 0b1111;
3375  let Inst{7-6}   = 0b10;
3376  let Inst{5-4}   = sz;
3377}
3378
3379def t2CRC32B  : T2I_crc32<0, 0b00, "b", int_arm_crc32b>;
3380def t2CRC32CB : T2I_crc32<1, 0b00, "cb", int_arm_crc32cb>;
3381def t2CRC32H  : T2I_crc32<0, 0b01, "h", int_arm_crc32h>;
3382def t2CRC32CH : T2I_crc32<1, 0b01, "ch", int_arm_crc32ch>;
3383def t2CRC32W  : T2I_crc32<0, 0b10, "w", int_arm_crc32w>;
3384def t2CRC32CW : T2I_crc32<1, 0b10, "cw", int_arm_crc32cw>;
3385
3386//===----------------------------------------------------------------------===//
3387//  Comparison Instructions...
3388//
3389defm t2CMP  : T2I_cmp_irs<0b1101, "cmp", GPRnopc,
3390                          IIC_iCMPi, IIC_iCMPr, IIC_iCMPsi, ARMcmp>;
3391
3392def : T2Pat<(ARMcmpZ  GPRnopc:$lhs, t2_so_imm:$imm),
3393            (t2CMPri  GPRnopc:$lhs, t2_so_imm:$imm)>;
3394def : T2Pat<(ARMcmpZ  GPRnopc:$lhs, rGPR:$rhs),
3395            (t2CMPrr  GPRnopc:$lhs, rGPR:$rhs)>;
3396def : T2Pat<(ARMcmpZ  GPRnopc:$lhs, t2_so_reg_oneuse:$rhs),
3397            (t2CMPrs  GPRnopc:$lhs, t2_so_reg_oneuse:$rhs)>;
3398
3399let isCompare = 1, Defs = [CPSR] in {
3400   // shifted imm
3401   def t2CMNri : T2OneRegCmpImm<
3402                (outs), (ins GPRnopc:$Rn, t2_so_imm:$imm), IIC_iCMPi,
3403                "cmn", ".w\t$Rn, $imm",
3404                [(ARMcmn GPRnopc:$Rn, (ineg t2_so_imm:$imm))]>,
3405                Sched<[WriteCMP, ReadALU]> {
3406     let Inst{31-27} = 0b11110;
3407     let Inst{25} = 0;
3408     let Inst{24-21} = 0b1000;
3409     let Inst{20} = 1; // The S bit.
3410     let Inst{15} = 0;
3411     let Inst{11-8} = 0b1111; // Rd
3412   }
3413   // register
3414   def t2CMNzrr : T2TwoRegCmp<
3415                (outs), (ins GPRnopc:$Rn, rGPR:$Rm), IIC_iCMPr,
3416                "cmn", ".w\t$Rn, $Rm",
3417                [(BinOpFrag<(ARMcmpZ node:$LHS,(ineg node:$RHS))>
3418                  GPRnopc:$Rn, rGPR:$Rm)]>, Sched<[WriteCMP, ReadALU, ReadALU]> {
3419     let Inst{31-27} = 0b11101;
3420     let Inst{26-25} = 0b01;
3421     let Inst{24-21} = 0b1000;
3422     let Inst{20} = 1; // The S bit.
3423     let Inst{14-12} = 0b000; // imm3
3424     let Inst{11-8} = 0b1111; // Rd
3425     let Inst{7-6} = 0b00; // imm2
3426     let Inst{5-4} = 0b00; // type
3427   }
3428   // shifted register
3429   def t2CMNzrs : T2OneRegCmpShiftedReg<
3430                (outs), (ins GPRnopc:$Rn, t2_so_reg:$ShiftedRm), IIC_iCMPsi,
3431                "cmn", ".w\t$Rn, $ShiftedRm",
3432                [(BinOpFrag<(ARMcmpZ node:$LHS,(ineg node:$RHS))>
3433                  GPRnopc:$Rn, t2_so_reg:$ShiftedRm)]>,
3434                  Sched<[WriteCMPsi, ReadALU, ReadALU]> {
3435     let Inst{31-27} = 0b11101;
3436     let Inst{26-25} = 0b01;
3437     let Inst{24-21} = 0b1000;
3438     let Inst{20} = 1; // The S bit.
3439     let Inst{11-8} = 0b1111; // Rd
3440   }
3441}
3442
3443// Assembler aliases w/o the ".w" suffix.
3444// No alias here for 'rr' version as not all instantiations of this multiclass
3445// want one (CMP in particular, does not).
3446def : t2InstAlias<"cmn${p} $Rn, $imm",
3447   (t2CMNri GPRnopc:$Rn, t2_so_imm:$imm, pred:$p)>;
3448def : t2InstAlias<"cmn${p} $Rn, $shift",
3449   (t2CMNzrs GPRnopc:$Rn, t2_so_reg:$shift, pred:$p)>;
3450
3451def : T2Pat<(ARMcmp  GPR:$src, t2_so_imm_neg:$imm),
3452            (t2CMNri GPR:$src, t2_so_imm_neg:$imm)>;
3453
3454def : T2Pat<(ARMcmpZ GPRnopc:$src, t2_so_imm_neg:$imm),
3455            (t2CMNri GPRnopc:$src, t2_so_imm_neg:$imm)>;
3456
3457defm t2TST  : T2I_cmp_irs<0b0000, "tst", rGPR,
3458                          IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
3459                         BinOpFrag<(ARMcmpZ (and_su node:$LHS, node:$RHS), 0)>>;
3460defm t2TEQ  : T2I_cmp_irs<0b0100, "teq", rGPR,
3461                          IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
3462                         BinOpFrag<(ARMcmpZ (xor_su node:$LHS, node:$RHS), 0)>>;
3463
3464// Conditional moves
3465let hasSideEffects = 0 in {
3466
3467let isCommutable = 1, isSelect = 1 in
3468def t2MOVCCr : t2PseudoInst<(outs rGPR:$Rd),
3469                            (ins rGPR:$false, rGPR:$Rm, cmovpred:$p),
3470                            4, IIC_iCMOVr,
3471                            [(set rGPR:$Rd, (ARMcmov rGPR:$false, rGPR:$Rm,
3472                                                     cmovpred:$p))]>,
3473               RegConstraint<"$false = $Rd">, Sched<[WriteALU]>;
3474
3475let isMoveImm = 1 in
3476def t2MOVCCi
3477    : t2PseudoInst<(outs rGPR:$Rd),
3478                   (ins rGPR:$false, t2_so_imm:$imm, cmovpred:$p),
3479                   4, IIC_iCMOVi,
3480                   [(set rGPR:$Rd, (ARMcmov rGPR:$false,t2_so_imm:$imm,
3481                                            cmovpred:$p))]>,
3482      RegConstraint<"$false = $Rd">, Sched<[WriteALU]>;
3483
3484let isCodeGenOnly = 1 in {
3485let isMoveImm = 1 in
3486def t2MOVCCi16
3487    : t2PseudoInst<(outs rGPR:$Rd),
3488                   (ins  rGPR:$false, imm0_65535_expr:$imm, cmovpred:$p),
3489                   4, IIC_iCMOVi,
3490                   [(set rGPR:$Rd, (ARMcmov rGPR:$false, imm0_65535:$imm,
3491                                            cmovpred:$p))]>,
3492      RegConstraint<"$false = $Rd">, Sched<[WriteALU]>;
3493
3494let isMoveImm = 1 in
3495def t2MVNCCi
3496    : t2PseudoInst<(outs rGPR:$Rd),
3497                   (ins rGPR:$false, t2_so_imm:$imm, cmovpred:$p),
3498                   4, IIC_iCMOVi,
3499                   [(set rGPR:$Rd,
3500                         (ARMcmov rGPR:$false, t2_so_imm_not:$imm,
3501                                  cmovpred:$p))]>,
3502      RegConstraint<"$false = $Rd">, Sched<[WriteALU]>;
3503
3504class MOVCCShPseudo<SDPatternOperator opnode, Operand ty>
3505    : t2PseudoInst<(outs rGPR:$Rd),
3506                   (ins rGPR:$false, rGPR:$Rm, i32imm:$imm, cmovpred:$p),
3507                   4, IIC_iCMOVsi,
3508                   [(set rGPR:$Rd, (ARMcmov rGPR:$false,
3509                                            (opnode rGPR:$Rm, (i32 ty:$imm)),
3510                                            cmovpred:$p))]>,
3511      RegConstraint<"$false = $Rd">, Sched<[WriteALU]>;
3512
3513def t2MOVCClsl : MOVCCShPseudo<shl,  imm0_31>;
3514def t2MOVCClsr : MOVCCShPseudo<srl,  imm_sr>;
3515def t2MOVCCasr : MOVCCShPseudo<sra,  imm_sr>;
3516def t2MOVCCror : MOVCCShPseudo<rotr, imm0_31>;
3517
3518let isMoveImm = 1 in
3519def t2MOVCCi32imm
3520    : t2PseudoInst<(outs rGPR:$dst),
3521                   (ins rGPR:$false, i32imm:$src, cmovpred:$p),
3522                   8, IIC_iCMOVix2,
3523                   [(set rGPR:$dst, (ARMcmov rGPR:$false, imm:$src,
3524                                             cmovpred:$p))]>,
3525      RegConstraint<"$false = $dst">;
3526} // isCodeGenOnly = 1
3527
3528} // hasSideEffects
3529
3530//===----------------------------------------------------------------------===//
3531// Atomic operations intrinsics
3532//
3533
3534// memory barriers protect the atomic sequences
3535let hasSideEffects = 1 in {
3536def t2DMB : T2I<(outs), (ins memb_opt:$opt), NoItinerary,
3537                "dmb", "\t$opt", [(int_arm_dmb (i32 imm0_15:$opt))]>,
3538                Requires<[IsThumb, HasDB]> {
3539  bits<4> opt;
3540  let Inst{31-4} = 0xf3bf8f5;
3541  let Inst{3-0} = opt;
3542}
3543
3544def t2DSB : T2I<(outs), (ins memb_opt:$opt), NoItinerary,
3545                "dsb", "\t$opt", [(int_arm_dsb (i32 imm0_15:$opt))]>,
3546                Requires<[IsThumb, HasDB]> {
3547  bits<4> opt;
3548  let Inst{31-4} = 0xf3bf8f4;
3549  let Inst{3-0} = opt;
3550}
3551
3552def t2ISB : T2I<(outs), (ins instsyncb_opt:$opt), NoItinerary,
3553                "isb", "\t$opt", [(int_arm_isb (i32 imm0_15:$opt))]>,
3554                Requires<[IsThumb, HasDB]> {
3555  bits<4> opt;
3556  let Inst{31-4} = 0xf3bf8f6;
3557  let Inst{3-0} = opt;
3558}
3559
3560let hasNoSchedulingInfo = 1 in
3561def t2TSB : T2I<(outs), (ins tsb_opt:$opt), NoItinerary,
3562                "tsb", "\t$opt", []>, Requires<[IsThumb, HasV8_4a]> {
3563  let Inst{31-0} = 0xf3af8012;
3564  let DecoderMethod = "DecodeTSBInstruction";
3565}
3566}
3567
3568// Armv8.5-A speculation barrier
3569def t2SB : Thumb2XI<(outs), (ins), AddrModeNone, 4, NoItinerary, "sb", "", []>,
3570           Requires<[IsThumb2, HasSB]>, Sched<[]> {
3571  let Inst{31-0} = 0xf3bf8f70;
3572  let Unpredictable = 0x000f2f0f;
3573  let hasSideEffects = 1;
3574}
3575
3576class T2I_ldrex<bits<4> opcod, dag oops, dag iops, AddrMode am, int sz,
3577                InstrItinClass itin, string opc, string asm, string cstr,
3578                list<dag> pattern, bits<4> rt2 = 0b1111>
3579  : Thumb2I<oops, iops, am, sz, itin, opc, asm, cstr, pattern> {
3580  let Inst{31-27} = 0b11101;
3581  let Inst{26-20} = 0b0001101;
3582  let Inst{11-8} = rt2;
3583  let Inst{7-4} = opcod;
3584  let Inst{3-0} = 0b1111;
3585
3586  bits<4> addr;
3587  bits<4> Rt;
3588  let Inst{19-16} = addr;
3589  let Inst{15-12} = Rt;
3590}
3591class T2I_strex<bits<4> opcod, dag oops, dag iops, AddrMode am, int sz,
3592                InstrItinClass itin, string opc, string asm, string cstr,
3593                list<dag> pattern, bits<4> rt2 = 0b1111>
3594  : Thumb2I<oops, iops, am, sz, itin, opc, asm, cstr, pattern> {
3595  let Inst{31-27} = 0b11101;
3596  let Inst{26-20} = 0b0001100;
3597  let Inst{11-8} = rt2;
3598  let Inst{7-4} = opcod;
3599
3600  bits<4> Rd;
3601  bits<4> addr;
3602  bits<4> Rt;
3603  let Inst{3-0}  = Rd;
3604  let Inst{19-16} = addr;
3605  let Inst{15-12} = Rt;
3606}
3607
3608let mayLoad = 1 in {
3609def t2LDREXB : T2I_ldrex<0b0100, (outs rGPR:$Rt), (ins addr_offset_none:$addr),
3610                         AddrModeNone, 4, NoItinerary,
3611                         "ldrexb", "\t$Rt, $addr", "",
3612                         [(set rGPR:$Rt, (ldrex_1 addr_offset_none:$addr))]>,
3613               Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteLd]>;
3614def t2LDREXH : T2I_ldrex<0b0101, (outs rGPR:$Rt), (ins addr_offset_none:$addr),
3615                         AddrModeNone, 4, NoItinerary,
3616                         "ldrexh", "\t$Rt, $addr", "",
3617                         [(set rGPR:$Rt, (ldrex_2 addr_offset_none:$addr))]>,
3618               Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteLd]>;
3619def t2LDREX  : Thumb2I<(outs rGPR:$Rt), (ins t2addrmode_imm0_1020s4:$addr),
3620                       AddrModeT2_ldrex, 4, NoItinerary,
3621                       "ldrex", "\t$Rt, $addr", "",
3622                     [(set rGPR:$Rt, (ldrex_4 t2addrmode_imm0_1020s4:$addr))]>,
3623               Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteLd]> {
3624  bits<4> Rt;
3625  bits<12> addr;
3626  let Inst{31-27} = 0b11101;
3627  let Inst{26-20} = 0b0000101;
3628  let Inst{19-16} = addr{11-8};
3629  let Inst{15-12} = Rt;
3630  let Inst{11-8} = 0b1111;
3631  let Inst{7-0} = addr{7-0};
3632}
3633let hasExtraDefRegAllocReq = 1 in
3634def t2LDREXD : T2I_ldrex<0b0111, (outs rGPR:$Rt, rGPR:$Rt2),
3635                         (ins addr_offset_none:$addr),
3636                         AddrModeNone, 4, NoItinerary,
3637                         "ldrexd", "\t$Rt, $Rt2, $addr", "",
3638                         [], {?, ?, ?, ?}>,
3639               Requires<[IsThumb2, IsNotMClass]>, Sched<[WriteLd]> {
3640  bits<4> Rt2;
3641  let Inst{11-8} = Rt2;
3642}
3643def t2LDAEXB : T2I_ldrex<0b1100, (outs rGPR:$Rt), (ins addr_offset_none:$addr),
3644                         AddrModeNone, 4, NoItinerary,
3645                         "ldaexb", "\t$Rt, $addr", "",
3646                         [(set rGPR:$Rt, (ldaex_1 addr_offset_none:$addr))]>,
3647               Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>, Sched<[WriteLd]>;
3648def t2LDAEXH : T2I_ldrex<0b1101, (outs rGPR:$Rt), (ins addr_offset_none:$addr),
3649                         AddrModeNone, 4, NoItinerary,
3650                         "ldaexh", "\t$Rt, $addr", "",
3651                         [(set rGPR:$Rt, (ldaex_2 addr_offset_none:$addr))]>,
3652               Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>, Sched<[WriteLd]>;
3653def t2LDAEX  : Thumb2I<(outs rGPR:$Rt), (ins addr_offset_none:$addr),
3654                       AddrModeNone, 4, NoItinerary,
3655                       "ldaex", "\t$Rt, $addr", "",
3656                         [(set rGPR:$Rt, (ldaex_4 addr_offset_none:$addr))]>,
3657               Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>, Sched<[WriteLd]> {
3658  bits<4> Rt;
3659  bits<4> addr;
3660  let Inst{31-27} = 0b11101;
3661  let Inst{26-20} = 0b0001101;
3662  let Inst{19-16} = addr;
3663  let Inst{15-12} = Rt;
3664  let Inst{11-8} = 0b1111;
3665  let Inst{7-0} = 0b11101111;
3666}
3667let hasExtraDefRegAllocReq = 1 in
3668def t2LDAEXD : T2I_ldrex<0b1111, (outs rGPR:$Rt, rGPR:$Rt2),
3669                         (ins addr_offset_none:$addr),
3670                         AddrModeNone, 4, NoItinerary,
3671                         "ldaexd", "\t$Rt, $Rt2, $addr", "",
3672                         [], {?, ?, ?, ?}>, Requires<[IsThumb,
3673                         HasAcquireRelease, HasV7Clrex, IsNotMClass]>, Sched<[WriteLd]> {
3674  bits<4> Rt2;
3675  let Inst{11-8} = Rt2;
3676
3677  let Inst{7} = 1;
3678}
3679}
3680
3681let mayStore = 1, Constraints = "@earlyclobber $Rd" in {
3682def t2STREXB : T2I_strex<0b0100, (outs rGPR:$Rd),
3683                         (ins rGPR:$Rt, addr_offset_none:$addr),
3684                         AddrModeNone, 4, NoItinerary,
3685                         "strexb", "\t$Rd, $Rt, $addr", "",
3686                         [(set rGPR:$Rd,
3687                               (strex_1 rGPR:$Rt, addr_offset_none:$addr))]>,
3688               Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteST]>;
3689def t2STREXH : T2I_strex<0b0101, (outs rGPR:$Rd),
3690                         (ins rGPR:$Rt, addr_offset_none:$addr),
3691                         AddrModeNone, 4, NoItinerary,
3692                         "strexh", "\t$Rd, $Rt, $addr", "",
3693                         [(set rGPR:$Rd,
3694                               (strex_2 rGPR:$Rt, addr_offset_none:$addr))]>,
3695               Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteST]>;
3696
3697def t2STREX  : Thumb2I<(outs rGPR:$Rd), (ins rGPR:$Rt,
3698                             t2addrmode_imm0_1020s4:$addr),
3699                  AddrModeT2_ldrex, 4, NoItinerary,
3700                  "strex", "\t$Rd, $Rt, $addr", "",
3701                  [(set rGPR:$Rd,
3702                        (strex_4 rGPR:$Rt, t2addrmode_imm0_1020s4:$addr))]>,
3703               Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteST]> {
3704  bits<4> Rd;
3705  bits<4> Rt;
3706  bits<12> addr;
3707  let Inst{31-27} = 0b11101;
3708  let Inst{26-20} = 0b0000100;
3709  let Inst{19-16} = addr{11-8};
3710  let Inst{15-12} = Rt;
3711  let Inst{11-8}  = Rd;
3712  let Inst{7-0} = addr{7-0};
3713}
3714let hasExtraSrcRegAllocReq = 1 in
3715def t2STREXD : T2I_strex<0b0111, (outs rGPR:$Rd),
3716                         (ins rGPR:$Rt, rGPR:$Rt2, addr_offset_none:$addr),
3717                         AddrModeNone, 4, NoItinerary,
3718                         "strexd", "\t$Rd, $Rt, $Rt2, $addr", "", [],
3719                         {?, ?, ?, ?}>,
3720               Requires<[IsThumb2, IsNotMClass]>, Sched<[WriteST]> {
3721  bits<4> Rt2;
3722  let Inst{11-8} = Rt2;
3723}
3724def t2STLEXB : T2I_strex<0b1100, (outs rGPR:$Rd),
3725                         (ins rGPR:$Rt, addr_offset_none:$addr),
3726                         AddrModeNone, 4, NoItinerary,
3727                         "stlexb", "\t$Rd, $Rt, $addr", "",
3728                         [(set rGPR:$Rd,
3729                               (stlex_1 rGPR:$Rt, addr_offset_none:$addr))]>,
3730                         Requires<[IsThumb, HasAcquireRelease,
3731                                   HasV7Clrex]>, Sched<[WriteST]>;
3732
3733def t2STLEXH : T2I_strex<0b1101, (outs rGPR:$Rd),
3734                         (ins rGPR:$Rt, addr_offset_none:$addr),
3735                         AddrModeNone, 4, NoItinerary,
3736                         "stlexh", "\t$Rd, $Rt, $addr", "",
3737                         [(set rGPR:$Rd,
3738                               (stlex_2 rGPR:$Rt, addr_offset_none:$addr))]>,
3739                         Requires<[IsThumb, HasAcquireRelease,
3740                                   HasV7Clrex]>, Sched<[WriteST]>;
3741
3742def t2STLEX  : Thumb2I<(outs rGPR:$Rd), (ins rGPR:$Rt,
3743                             addr_offset_none:$addr),
3744                  AddrModeNone, 4, NoItinerary,
3745                  "stlex", "\t$Rd, $Rt, $addr", "",
3746                  [(set rGPR:$Rd,
3747                        (stlex_4 rGPR:$Rt, addr_offset_none:$addr))]>,
3748                  Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>,
3749                  Sched<[WriteST]> {
3750  bits<4> Rd;
3751  bits<4> Rt;
3752  bits<4> addr;
3753  let Inst{31-27} = 0b11101;
3754  let Inst{26-20} = 0b0001100;
3755  let Inst{19-16} = addr;
3756  let Inst{15-12} = Rt;
3757  let Inst{11-4}  = 0b11111110;
3758  let Inst{3-0}   = Rd;
3759}
3760let hasExtraSrcRegAllocReq = 1 in
3761def t2STLEXD : T2I_strex<0b1111, (outs rGPR:$Rd),
3762                         (ins rGPR:$Rt, rGPR:$Rt2, addr_offset_none:$addr),
3763                         AddrModeNone, 4, NoItinerary,
3764                         "stlexd", "\t$Rd, $Rt, $Rt2, $addr", "", [],
3765                         {?, ?, ?, ?}>, Requires<[IsThumb, HasAcquireRelease,
3766                         HasV7Clrex, IsNotMClass]>, Sched<[WriteST]> {
3767  bits<4> Rt2;
3768  let Inst{11-8} = Rt2;
3769}
3770}
3771
3772def t2CLREX : T2I<(outs), (ins), NoItinerary, "clrex", "", [(int_arm_clrex)]>,
3773            Requires<[IsThumb, HasV7Clrex]>  {
3774  let Inst{31-16} = 0xf3bf;
3775  let Inst{15-14} = 0b10;
3776  let Inst{13} = 0;
3777  let Inst{12} = 0;
3778  let Inst{11-8} = 0b1111;
3779  let Inst{7-4} = 0b0010;
3780  let Inst{3-0} = 0b1111;
3781}
3782
3783def : T2Pat<(and (ldrex_1 addr_offset_none:$addr), 0xff),
3784            (t2LDREXB addr_offset_none:$addr)>,
3785            Requires<[IsThumb, HasV8MBaseline]>;
3786def : T2Pat<(and (ldrex_2 addr_offset_none:$addr), 0xffff),
3787            (t2LDREXH addr_offset_none:$addr)>,
3788            Requires<[IsThumb, HasV8MBaseline]>;
3789def : T2Pat<(strex_1 (and GPR:$Rt, 0xff), addr_offset_none:$addr),
3790            (t2STREXB GPR:$Rt, addr_offset_none:$addr)>,
3791            Requires<[IsThumb, HasV8MBaseline]>;
3792def : T2Pat<(strex_2 (and GPR:$Rt, 0xffff), addr_offset_none:$addr),
3793            (t2STREXH GPR:$Rt, addr_offset_none:$addr)>,
3794            Requires<[IsThumb, HasV8MBaseline]>;
3795
3796def : T2Pat<(and (ldaex_1 addr_offset_none:$addr), 0xff),
3797            (t2LDAEXB addr_offset_none:$addr)>,
3798            Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>;
3799def : T2Pat<(and (ldaex_2 addr_offset_none:$addr), 0xffff),
3800            (t2LDAEXH addr_offset_none:$addr)>,
3801            Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>;
3802def : T2Pat<(stlex_1 (and GPR:$Rt, 0xff), addr_offset_none:$addr),
3803            (t2STLEXB GPR:$Rt, addr_offset_none:$addr)>,
3804            Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>;
3805def : T2Pat<(stlex_2 (and GPR:$Rt, 0xffff), addr_offset_none:$addr),
3806            (t2STLEXH GPR:$Rt, addr_offset_none:$addr)>,
3807            Requires<[IsThumb, HasAcquireRelease, HasV7Clrex]>;
3808
3809//===----------------------------------------------------------------------===//
3810// SJLJ Exception handling intrinsics
3811//   eh_sjlj_setjmp() is an instruction sequence to store the return
3812//   address and save #0 in R0 for the non-longjmp case.
3813//   Since by its nature we may be coming from some other function to get
3814//   here, and we're using the stack frame for the containing function to
3815//   save/restore registers, we can't keep anything live in regs across
3816//   the eh_sjlj_setjmp(), else it will almost certainly have been tromped upon
3817//   when we get here from a longjmp(). We force everything out of registers
3818//   except for our own input by listing the relevant registers in Defs. By
3819//   doing so, we also cause the prologue/epilogue code to actively preserve
3820//   all of the callee-saved registers, which is exactly what we want.
3821//   $val is a scratch register for our use.
3822// This gets lowered to an instruction sequence of 12 bytes
3823let Defs =
3824  [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7,  R8,  R9,  R10, R11, R12, LR, CPSR,
3825    Q0, Q1, Q2, Q3, Q8, Q9, Q10, Q11, Q12, Q13, Q14, Q15],
3826  hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1, Size = 12,
3827  usesCustomInserter = 1 in {
3828  def t2Int_eh_sjlj_setjmp : Thumb2XI<(outs), (ins tGPR:$src, tGPR:$val),
3829                               AddrModeNone, 0, NoItinerary, "", "",
3830                          [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>,
3831                             Requires<[IsThumb2, HasVFP2]>;
3832}
3833
3834// This gets lowered to an instruction sequence of 12 bytes
3835let Defs =
3836  [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7,  R8,  R9,  R10, R11, R12, LR, CPSR ],
3837  hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1, Size = 12,
3838  usesCustomInserter = 1 in {
3839  def t2Int_eh_sjlj_setjmp_nofp : Thumb2XI<(outs), (ins tGPR:$src, tGPR:$val),
3840                               AddrModeNone, 0, NoItinerary, "", "",
3841                          [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>,
3842                                  Requires<[IsThumb2, NoVFP]>;
3843}
3844
3845
3846//===----------------------------------------------------------------------===//
3847// Control-Flow Instructions
3848//
3849
3850// FIXME: remove when we have a way to marking a MI with these properties.
3851// FIXME: Should pc be an implicit operand like PICADD, etc?
3852let isReturn = 1, isTerminator = 1, isBarrier = 1, mayLoad = 1,
3853    hasExtraDefRegAllocReq = 1, isCodeGenOnly = 1 in
3854def t2LDMIA_RET: t2PseudoExpand<(outs GPR:$wb), (ins GPR:$Rn, pred:$p,
3855                                                   reglist:$regs, variable_ops),
3856                              4, IIC_iLoad_mBr, [],
3857            (t2LDMIA_UPD GPR:$wb, GPR:$Rn, pred:$p, reglist:$regs)>,
3858                         RegConstraint<"$Rn = $wb">;
3859
3860let isBranch = 1, isTerminator = 1, isBarrier = 1 in {
3861let isPredicable = 1 in
3862def t2B   : T2I<(outs), (ins thumb_br_target:$target), IIC_Br,
3863                 "b", ".w\t$target",
3864                 [(br bb:$target)]>, Sched<[WriteBr]>,
3865                 Requires<[IsThumb, HasV8MBaseline]> {
3866  let Inst{31-27} = 0b11110;
3867  let Inst{15-14} = 0b10;
3868  let Inst{12} = 1;
3869
3870  bits<24> target;
3871  let Inst{26} = target{23};
3872  let Inst{13} = target{22};
3873  let Inst{11} = target{21};
3874  let Inst{25-16} = target{20-11};
3875  let Inst{10-0} = target{10-0};
3876  let DecoderMethod = "DecodeT2BInstruction";
3877  let AsmMatchConverter = "cvtThumbBranches";
3878}
3879
3880let Size = 4, isNotDuplicable = 1, isBranch = 1, isTerminator = 1,
3881    isBarrier = 1, isIndirectBranch = 1 in {
3882
3883// available in both v8-M.Baseline and Thumb2 targets
3884def t2BR_JT : t2basePseudoInst<(outs),
3885          (ins GPR:$target, GPR:$index, i32imm:$jt),
3886           0, IIC_Br,
3887          [(ARMbr2jt GPR:$target, GPR:$index, tjumptable:$jt)]>,
3888          Sched<[WriteBr]>;
3889
3890// FIXME: Add a case that can be predicated.
3891def t2TBB_JT : t2PseudoInst<(outs),
3892        (ins GPR:$base, GPR:$index, i32imm:$jt, i32imm:$pclbl), 0, IIC_Br, []>,
3893        Sched<[WriteBr]>;
3894
3895def t2TBH_JT : t2PseudoInst<(outs),
3896        (ins GPR:$base, GPR:$index, i32imm:$jt, i32imm:$pclbl), 0, IIC_Br, []>,
3897        Sched<[WriteBr]>;
3898
3899def t2TBB : T2I<(outs), (ins addrmode_tbb:$addr), IIC_Br,
3900                    "tbb", "\t$addr", []>, Sched<[WriteBrTbl]> {
3901  bits<4> Rn;
3902  bits<4> Rm;
3903  let Inst{31-20} = 0b111010001101;
3904  let Inst{19-16} = Rn;
3905  let Inst{15-5} = 0b11110000000;
3906  let Inst{4} = 0; // B form
3907  let Inst{3-0} = Rm;
3908
3909  let DecoderMethod = "DecodeThumbTableBranch";
3910}
3911
3912def t2TBH : T2I<(outs), (ins addrmode_tbh:$addr), IIC_Br,
3913                   "tbh", "\t$addr", []>, Sched<[WriteBrTbl]> {
3914  bits<4> Rn;
3915  bits<4> Rm;
3916  let Inst{31-20} = 0b111010001101;
3917  let Inst{19-16} = Rn;
3918  let Inst{15-5} = 0b11110000000;
3919  let Inst{4} = 1; // H form
3920  let Inst{3-0} = Rm;
3921
3922  let DecoderMethod = "DecodeThumbTableBranch";
3923}
3924} // isNotDuplicable, isIndirectBranch
3925
3926} // isBranch, isTerminator, isBarrier
3927
3928// FIXME: should be able to write a pattern for ARMBrcond, but can't use
3929// a two-value operand where a dag node expects ", "two operands. :(
3930let isBranch = 1, isTerminator = 1 in
3931def t2Bcc : T2I<(outs), (ins brtarget:$target), IIC_Br,
3932                "b", ".w\t$target",
3933                [/*(ARMbrcond bb:$target, imm:$cc)*/]>, Sched<[WriteBr]> {
3934  let Inst{31-27} = 0b11110;
3935  let Inst{15-14} = 0b10;
3936  let Inst{12} = 0;
3937
3938  bits<4> p;
3939  let Inst{25-22} = p;
3940
3941  bits<21> target;
3942  let Inst{26} = target{20};
3943  let Inst{11} = target{19};
3944  let Inst{13} = target{18};
3945  let Inst{21-16} = target{17-12};
3946  let Inst{10-0} = target{11-1};
3947
3948  let DecoderMethod = "DecodeThumb2BCCInstruction";
3949  let AsmMatchConverter = "cvtThumbBranches";
3950}
3951
3952// Tail calls. The MachO version of thumb tail calls uses a t2 branch, so
3953// it goes here.
3954// Windows SEH unwinding also needs a strict t2 branch for tail calls.
3955let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in {
3956  // IOS version.
3957  let Uses = [SP] in
3958  def tTAILJMPd: tPseudoExpand<(outs),
3959                   (ins thumb_br_target:$dst, pred:$p),
3960                   4, IIC_Br, [],
3961                   (t2B thumb_br_target:$dst, pred:$p)>,
3962                 Requires<[IsThumb2]>, Sched<[WriteBr]>;
3963}
3964
3965// IT block
3966let Defs = [ITSTATE] in
3967def t2IT : Thumb2XI<(outs), (ins it_pred:$cc, it_mask:$mask),
3968                    AddrModeNone, 2,  IIC_iALUx,
3969                    "it$mask\t$cc", "", []> {
3970  // 16-bit instruction.
3971  let Inst{31-16} = 0x0000;
3972  let Inst{15-8} = 0b10111111;
3973
3974  bits<4> cc;
3975  bits<4> mask;
3976  let Inst{7-4} = cc;
3977  let Inst{3-0} = mask;
3978
3979  let DecoderMethod = "DecodeIT";
3980}
3981
3982// Branch and Exchange Jazelle -- for disassembly only
3983// Rm = Inst{19-16}
3984let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in
3985def t2BXJ : T2I<(outs), (ins GPRnopc:$func), NoItinerary, "bxj", "\t$func", []>,
3986    Sched<[WriteBr]>, Requires<[IsThumb2, IsNotMClass]> {
3987  bits<4> func;
3988  let Inst{31-27} = 0b11110;
3989  let Inst{26} = 0;
3990  let Inst{25-20} = 0b111100;
3991  let Inst{19-16} = func;
3992  let Inst{15-0} = 0b1000111100000000;
3993}
3994
3995def : t2InstAlias<"bl${p}.w $func", (tBL pred:$p, thumb_bl_target:$func), 0>;
3996
3997// Compare and branch on zero / non-zero
3998let isBranch = 1, isTerminator = 1 in {
3999  def tCBZ  : T1I<(outs), (ins tGPR:$Rn, thumb_cb_target:$target), IIC_Br,
4000                  "cbz\t$Rn, $target", []>,
4001              T1Misc<{0,0,?,1,?,?,?}>,
4002              Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteBr]> {
4003    // A8.6.27
4004    bits<6> target;
4005    bits<3> Rn;
4006    let Inst{9}   = target{5};
4007    let Inst{7-3} = target{4-0};
4008    let Inst{2-0} = Rn;
4009  }
4010
4011  def tCBNZ : T1I<(outs), (ins tGPR:$Rn, thumb_cb_target:$target), IIC_Br,
4012                  "cbnz\t$Rn, $target", []>,
4013              T1Misc<{1,0,?,1,?,?,?}>,
4014              Requires<[IsThumb, HasV8MBaseline]>, Sched<[WriteBr]> {
4015    // A8.6.27
4016    bits<6> target;
4017    bits<3> Rn;
4018    let Inst{9}   = target{5};
4019    let Inst{7-3} = target{4-0};
4020    let Inst{2-0} = Rn;
4021  }
4022}
4023
4024
4025// Change Processor State is a system instruction.
4026// FIXME: Since the asm parser has currently no clean way to handle optional
4027// operands, create 3 versions of the same instruction. Once there's a clean
4028// framework to represent optional operands, change this behavior.
4029class t2CPS<dag iops, string asm_op> : T2XI<(outs), iops, NoItinerary,
4030            !strconcat("cps", asm_op), []>,
4031          Requires<[IsThumb2, IsNotMClass]> {
4032  bits<2> imod;
4033  bits<3> iflags;
4034  bits<5> mode;
4035  bit M;
4036
4037  let Inst{31-11} = 0b111100111010111110000;
4038  let Inst{10-9}  = imod;
4039  let Inst{8}     = M;
4040  let Inst{7-5}   = iflags;
4041  let Inst{4-0}   = mode;
4042  let DecoderMethod = "DecodeT2CPSInstruction";
4043}
4044
4045let M = 1 in
4046  def t2CPS3p : t2CPS<(ins imod_op:$imod, iflags_op:$iflags, i32imm:$mode),
4047                      "$imod\t$iflags, $mode">;
4048let mode = 0, M = 0 in
4049  def t2CPS2p : t2CPS<(ins imod_op:$imod, iflags_op:$iflags),
4050                      "$imod.w\t$iflags">;
4051let imod = 0, iflags = 0, M = 1 in
4052  def t2CPS1p : t2CPS<(ins imm0_31:$mode), "\t$mode">;
4053
4054def : t2InstAlias<"cps$imod.w $iflags, $mode",
4055                   (t2CPS3p imod_op:$imod, iflags_op:$iflags, i32imm:$mode), 0>;
4056def : t2InstAlias<"cps.w $mode", (t2CPS1p imm0_31:$mode), 0>;
4057
4058// A6.3.4 Branches and miscellaneous control
4059// Table A6-14 Change Processor State, and hint instructions
4060def t2HINT : T2I<(outs), (ins imm0_239:$imm), NoItinerary, "hint", ".w\t$imm",
4061                  [(int_arm_hint imm0_239:$imm)]> {
4062  bits<8> imm;
4063  let Inst{31-3} = 0b11110011101011111000000000000;
4064  let Inst{7-0} = imm;
4065
4066  let DecoderMethod = "DecodeT2HintSpaceInstruction";
4067}
4068
4069def : t2InstAlias<"hint$p $imm", (t2HINT imm0_239:$imm, pred:$p), 0>;
4070def : t2InstAlias<"nop$p.w", (t2HINT 0, pred:$p), 1>;
4071def : t2InstAlias<"yield$p.w", (t2HINT 1, pred:$p), 1>;
4072def : t2InstAlias<"wfe$p.w", (t2HINT 2, pred:$p), 1>;
4073def : t2InstAlias<"wfi$p.w", (t2HINT 3, pred:$p), 1>;
4074def : t2InstAlias<"sev$p.w", (t2HINT 4, pred:$p), 1>;
4075def : t2InstAlias<"sevl$p.w", (t2HINT 5, pred:$p), 1> {
4076  let Predicates = [IsThumb2, HasV8];
4077}
4078def : t2InstAlias<"esb$p.w", (t2HINT 16, pred:$p), 1> {
4079  let Predicates = [IsThumb2, HasRAS];
4080}
4081def : t2InstAlias<"esb$p", (t2HINT 16, pred:$p), 0> {
4082  let Predicates = [IsThumb2, HasRAS];
4083}
4084def : t2InstAlias<"csdb$p.w", (t2HINT 20, pred:$p), 0>;
4085def : t2InstAlias<"csdb$p",   (t2HINT 20, pred:$p), 1>;
4086
4087def : t2InstAlias<"pacbti$p r12,lr,sp", (t2HINT 13, pred:$p), 1>;
4088def : t2InstAlias<"bti$p", (t2HINT 15, pred:$p), 1>;
4089def : t2InstAlias<"pac$p r12,lr,sp", (t2HINT 29, pred:$p), 1>;
4090def : t2InstAlias<"aut$p r12,lr,sp", (t2HINT 45, pred:$p), 1>;
4091
4092def t2DBG : T2I<(outs), (ins imm0_15:$opt), NoItinerary, "dbg", "\t$opt",
4093                [(int_arm_dbg imm0_15:$opt)]> {
4094  bits<4> opt;
4095  let Inst{31-20} = 0b111100111010;
4096  let Inst{19-16} = 0b1111;
4097  let Inst{15-8} = 0b10000000;
4098  let Inst{7-4} = 0b1111;
4099  let Inst{3-0} = opt;
4100}
4101def : t2InstAlias<"dbg${p}.w $opt", (t2DBG imm0_15:$opt, pred:$p), 0>;
4102
4103// Secure Monitor Call is a system instruction.
4104// Option = Inst{19-16}
4105let isCall = 1, Uses = [SP] in
4106def t2SMC : T2I<(outs), (ins imm0_15:$opt), NoItinerary, "smc", "\t$opt",
4107                []>, Requires<[IsThumb2, HasTrustZone]> {
4108  let Inst{31-27} = 0b11110;
4109  let Inst{26-20} = 0b1111111;
4110  let Inst{15-12} = 0b1000;
4111
4112  bits<4> opt;
4113  let Inst{19-16} = opt;
4114}
4115
4116class T2DCPS<bits<2> opt, string opc>
4117  : T2I<(outs), (ins), NoItinerary, opc, "", []>, Requires<[IsThumb2, HasV8]> {
4118  let Inst{31-27} = 0b11110;
4119  let Inst{26-20} = 0b1111000;
4120  let Inst{19-16} = 0b1111;
4121  let Inst{15-12} = 0b1000;
4122  let Inst{11-2} = 0b0000000000;
4123  let Inst{1-0} = opt;
4124}
4125
4126def t2DCPS1 : T2DCPS<0b01, "dcps1">;
4127def t2DCPS2 : T2DCPS<0b10, "dcps2">;
4128def t2DCPS3 : T2DCPS<0b11, "dcps3">;
4129
4130class T2SRS<bits<2> Op, bit W, dag oops, dag iops, InstrItinClass itin,
4131            string opc, string asm, list<dag> pattern>
4132  : T2I<oops, iops, itin, opc, asm, pattern>,
4133    Requires<[IsThumb2,IsNotMClass]> {
4134  bits<5> mode;
4135  let Inst{31-25} = 0b1110100;
4136  let Inst{24-23} = Op;
4137  let Inst{22} = 0;
4138  let Inst{21} = W;
4139  let Inst{20-16} = 0b01101;
4140  let Inst{15-5} = 0b11000000000;
4141  let Inst{4-0} = mode{4-0};
4142}
4143
4144// Store Return State is a system instruction.
4145def t2SRSDB_UPD : T2SRS<0b00, 1, (outs), (ins imm0_31:$mode), NoItinerary,
4146                        "srsdb", "\tsp!, $mode", []>;
4147def t2SRSDB  : T2SRS<0b00, 0, (outs), (ins imm0_31:$mode), NoItinerary,
4148                     "srsdb","\tsp, $mode", []>;
4149def t2SRSIA_UPD : T2SRS<0b11, 1, (outs), (ins imm0_31:$mode), NoItinerary,
4150                        "srsia","\tsp!, $mode", []>;
4151def t2SRSIA  : T2SRS<0b11, 0, (outs), (ins imm0_31:$mode), NoItinerary,
4152                     "srsia","\tsp, $mode", []>;
4153
4154
4155def : t2InstAlias<"srsdb${p} $mode", (t2SRSDB imm0_31:$mode, pred:$p)>;
4156def : t2InstAlias<"srsdb${p} $mode!", (t2SRSDB_UPD imm0_31:$mode, pred:$p)>;
4157
4158def : t2InstAlias<"srsia${p} $mode", (t2SRSIA imm0_31:$mode, pred:$p)>;
4159def : t2InstAlias<"srsia${p} $mode!", (t2SRSIA_UPD imm0_31:$mode, pred:$p)>;
4160
4161// Return From Exception is a system instruction.
4162let isReturn = 1, isBarrier = 1, isTerminator = 1, Defs = [PC] in
4163class T2RFE<bits<12> op31_20, dag oops, dag iops, InstrItinClass itin,
4164          string opc, string asm, list<dag> pattern>
4165  : T2I<oops, iops, itin, opc, asm, pattern>,
4166    Requires<[IsThumb2,IsNotMClass]> {
4167  let Inst{31-20} = op31_20{11-0};
4168
4169  bits<4> Rn;
4170  let Inst{19-16} = Rn;
4171  let Inst{15-0} = 0xc000;
4172}
4173
4174def t2RFEDBW : T2RFE<0b111010000011,
4175                   (outs), (ins GPR:$Rn), NoItinerary, "rfedb", "\t$Rn!",
4176                   [/* For disassembly only; pattern left blank */]>;
4177def t2RFEDB  : T2RFE<0b111010000001,
4178                   (outs), (ins GPR:$Rn), NoItinerary, "rfedb", "\t$Rn",
4179                   [/* For disassembly only; pattern left blank */]>;
4180def t2RFEIAW : T2RFE<0b111010011011,
4181                   (outs), (ins GPR:$Rn), NoItinerary, "rfeia", "\t$Rn!",
4182                   [/* For disassembly only; pattern left blank */]>;
4183def t2RFEIA  : T2RFE<0b111010011001,
4184                   (outs), (ins GPR:$Rn), NoItinerary, "rfeia", "\t$Rn",
4185                   [/* For disassembly only; pattern left blank */]>;
4186
4187// B9.3.19 SUBS PC, LR, #imm (Thumb2) system instruction.
4188// Exception return instruction is "subs pc, lr, #imm".
4189let isReturn = 1, isBarrier = 1, isTerminator = 1, Defs = [PC] in
4190def t2SUBS_PC_LR : T2I <(outs), (ins imm0_255:$imm), NoItinerary,
4191                        "subs", "\tpc, lr, $imm",
4192                        [(ARMintretflag imm0_255:$imm)]>,
4193                   Requires<[IsThumb2,IsNotMClass]> {
4194  let Inst{31-8} = 0b111100111101111010001111;
4195
4196  bits<8> imm;
4197  let Inst{7-0} = imm;
4198}
4199
4200// B9.3.19 SUBS PC, LR (Thumb)
4201// In the Thumb instruction set, MOVS{<c>}{<q>} PC, LR is a pseudo-instruction
4202// for SUBS{<c>}{<q>} PC, LR, #0.
4203def : t2InstAlias<"movs${p}\tpc, lr", (t2SUBS_PC_LR 0, pred:$p)>;
4204def : t2InstAlias<"movs${p}.w\tpc, lr", (t2SUBS_PC_LR 0, pred:$p)>;
4205
4206// ERET - Return from exception in Hypervisor mode.
4207// B9.3.3, B9.3.20: ERET is an alias for "SUBS PC, LR, #0" in an implementation that
4208// includes virtualization extensions.
4209def t2ERET : InstAlias<"eret${p}", (t2SUBS_PC_LR 0, pred:$p), 1>,
4210             Requires<[IsThumb2, HasVirtualization]>;
4211
4212// Hypervisor Call is a system instruction.
4213let isCall = 1 in {
4214def t2HVC : T2XI <(outs), (ins imm0_65535:$imm16), IIC_Br, "hvc.w\t$imm16", []>,
4215      Requires<[IsThumb2, HasVirtualization]>, Sched<[WriteBr]> {
4216    bits<16> imm16;
4217    let Inst{31-20} = 0b111101111110;
4218    let Inst{19-16} = imm16{15-12};
4219    let Inst{15-12} = 0b1000;
4220    let Inst{11-0} = imm16{11-0};
4221}
4222}
4223
4224// Alias for HVC without the ".w" optional width specifier
4225def : t2InstAlias<"hvc\t$imm16", (t2HVC imm0_65535:$imm16)>;
4226
4227//===----------------------------------------------------------------------===//
4228// Non-Instruction Patterns
4229//
4230
4231// 32-bit immediate using movw + movt.
4232// This is a single pseudo instruction to make it re-materializable.
4233// FIXME: Remove this when we can do generalized remat.
4234let isReMaterializable = 1, isMoveImm = 1, Size = 8 in
4235def t2MOVi32imm : PseudoInst<(outs rGPR:$dst), (ins i32imm:$src), IIC_iMOVix2,
4236                            [(set rGPR:$dst, (i32 imm:$src))]>,
4237                            Requires<[IsThumb, UseMovt]>;
4238
4239// Pseudo instruction that combines movw + movt + add pc (if pic).
4240// It also makes it possible to rematerialize the instructions.
4241// FIXME: Remove this when we can do generalized remat and when machine licm
4242// can properly the instructions.
4243let isReMaterializable = 1 in {
4244def t2MOV_ga_pcrel : PseudoInst<(outs rGPR:$dst), (ins i32imm:$addr),
4245                                IIC_iMOVix2addpc,
4246                          [(set rGPR:$dst, (ARMWrapperPIC tglobaladdr:$addr))]>,
4247                          Requires<[IsThumb, HasV8MBaseline, UseMovtInPic]>;
4248
4249}
4250
4251def : T2Pat<(ARMWrapperPIC tglobaltlsaddr :$dst),
4252            (t2MOV_ga_pcrel tglobaltlsaddr:$dst)>,
4253      Requires<[IsThumb2, UseMovtInPic]>;
4254def : T2Pat<(ARMWrapper tglobaltlsaddr:$dst),
4255            (t2MOVi32imm tglobaltlsaddr:$dst)>,
4256      Requires<[IsThumb2, UseMovt]>;
4257
4258// ConstantPool, GlobalAddress, and JumpTable
4259def : T2Pat<(ARMWrapper tconstpool :$dst), (t2LEApcrel tconstpool :$dst)>;
4260def : T2Pat<(ARMWrapper texternalsym :$dst), (t2MOVi32imm texternalsym :$dst)>,
4261    Requires<[IsThumb, HasV8MBaseline, UseMovt]>;
4262def : T2Pat<(ARMWrapper tglobaladdr :$dst), (t2MOVi32imm tglobaladdr :$dst)>,
4263    Requires<[IsThumb, HasV8MBaseline, UseMovt]>;
4264
4265def : T2Pat<(ARMWrapperJT tjumptable:$dst), (t2LEApcrelJT tjumptable:$dst)>;
4266
4267let hasNoSchedulingInfo = 1 in {
4268def t2LDRLIT_ga_pcrel : PseudoInst<(outs rGPR:$dst), (ins i32imm:$addr),
4269                                  IIC_iLoadiALU,
4270                                  [(set rGPR:$dst,
4271                                        (ARMWrapperPIC tglobaladdr:$addr))]>,
4272                       Requires<[IsThumb, HasV8MBaseline, DontUseMovtInPic]>;
4273}
4274
4275// TLS globals
4276def : Pat<(ARMWrapperPIC tglobaltlsaddr:$addr),
4277          (t2LDRLIT_ga_pcrel tglobaltlsaddr:$addr)>,
4278      Requires<[IsThumb, HasV8MBaseline, DontUseMovtInPic]>;
4279
4280// Pseudo instruction that combines ldr from constpool and add pc. This should
4281// be expanded into two instructions late to allow if-conversion and
4282// scheduling.
4283let canFoldAsLoad = 1, isReMaterializable = 1 in
4284def t2LDRpci_pic : PseudoInst<(outs rGPR:$dst), (ins i32imm:$addr, pclabel:$cp),
4285                   IIC_iLoadiALU,
4286              [(set rGPR:$dst, (ARMpic_add (load (ARMWrapper tconstpool:$addr)),
4287                                           imm:$cp))]>,
4288               Requires<[IsThumb2]>;
4289
4290// Pseudo instruction that combines movs + predicated rsbmi
4291// to implement integer ABS
4292let usesCustomInserter = 1, Defs = [CPSR], hasNoSchedulingInfo = 1 in {
4293def t2ABS : PseudoInst<(outs rGPR:$dst), (ins rGPR:$src),
4294                       NoItinerary, []>, Requires<[IsThumb2]>;
4295}
4296
4297//===----------------------------------------------------------------------===//
4298// Coprocessor load/store -- for disassembly only
4299//
4300class T2CI<bits<4> op31_28, dag oops, dag iops, string opc, string asm,
4301           list<dag> pattern, AddrMode am = AddrModeNone>
4302  : T2I<oops, iops, NoItinerary, opc, asm, pattern, am> {
4303  let Inst{31-28} = op31_28;
4304  let Inst{27-25} = 0b110;
4305}
4306
4307multiclass t2LdStCop<bits<4> op31_28, bit load, bit Dbit, string asm, list<dag> pattern> {
4308  def _OFFSET : T2CI<op31_28,
4309                     (outs), (ins p_imm:$cop, c_imm:$CRd, addrmode5:$addr),
4310                     asm, "\t$cop, $CRd, $addr", pattern, AddrMode5> {
4311    bits<13> addr;
4312    bits<4> cop;
4313    bits<4> CRd;
4314    let Inst{24} = 1; // P = 1
4315    let Inst{23} = addr{8};
4316    let Inst{22} = Dbit;
4317    let Inst{21} = 0; // W = 0
4318    let Inst{20} = load;
4319    let Inst{19-16} = addr{12-9};
4320    let Inst{15-12} = CRd;
4321    let Inst{11-8} = cop;
4322    let Inst{7-0} = addr{7-0};
4323    let DecoderMethod = "DecodeCopMemInstruction";
4324  }
4325  def _PRE : T2CI<op31_28,
4326                  (outs), (ins p_imm:$cop, c_imm:$CRd, addrmode5_pre:$addr),
4327                  asm, "\t$cop, $CRd, $addr!", []> {
4328    bits<13> addr;
4329    bits<4> cop;
4330    bits<4> CRd;
4331    let Inst{24} = 1; // P = 1
4332    let Inst{23} = addr{8};
4333    let Inst{22} = Dbit;
4334    let Inst{21} = 1; // W = 1
4335    let Inst{20} = load;
4336    let Inst{19-16} = addr{12-9};
4337    let Inst{15-12} = CRd;
4338    let Inst{11-8} = cop;
4339    let Inst{7-0} = addr{7-0};
4340    let DecoderMethod = "DecodeCopMemInstruction";
4341  }
4342  def _POST: T2CI<op31_28,
4343                  (outs), (ins p_imm:$cop, c_imm:$CRd, addr_offset_none:$addr,
4344                               postidx_imm8s4:$offset),
4345                 asm, "\t$cop, $CRd, $addr, $offset", []> {
4346    bits<9> offset;
4347    bits<4> addr;
4348    bits<4> cop;
4349    bits<4> CRd;
4350    let Inst{24} = 0; // P = 0
4351    let Inst{23} = offset{8};
4352    let Inst{22} = Dbit;
4353    let Inst{21} = 1; // W = 1
4354    let Inst{20} = load;
4355    let Inst{19-16} = addr;
4356    let Inst{15-12} = CRd;
4357    let Inst{11-8} = cop;
4358    let Inst{7-0} = offset{7-0};
4359    let DecoderMethod = "DecodeCopMemInstruction";
4360  }
4361  def _OPTION : T2CI<op31_28, (outs),
4362                     (ins p_imm:$cop, c_imm:$CRd, addr_offset_none:$addr,
4363                          coproc_option_imm:$option),
4364      asm, "\t$cop, $CRd, $addr, $option", []> {
4365    bits<8> option;
4366    bits<4> addr;
4367    bits<4> cop;
4368    bits<4> CRd;
4369    let Inst{24} = 0; // P = 0
4370    let Inst{23} = 1; // U = 1
4371    let Inst{22} = Dbit;
4372    let Inst{21} = 0; // W = 0
4373    let Inst{20} = load;
4374    let Inst{19-16} = addr;
4375    let Inst{15-12} = CRd;
4376    let Inst{11-8} = cop;
4377    let Inst{7-0} = option;
4378    let DecoderMethod = "DecodeCopMemInstruction";
4379  }
4380}
4381
4382let DecoderNamespace = "Thumb2CoProc" in {
4383defm t2LDC   : t2LdStCop<0b1110, 1, 0, "ldc", [(int_arm_ldc timm:$cop, timm:$CRd, addrmode5:$addr)]>;
4384defm t2LDCL  : t2LdStCop<0b1110, 1, 1, "ldcl", [(int_arm_ldcl timm:$cop, timm:$CRd, addrmode5:$addr)]>;
4385defm t2LDC2  : t2LdStCop<0b1111, 1, 0, "ldc2", [(int_arm_ldc2 timm:$cop, timm:$CRd, addrmode5:$addr)]>, Requires<[PreV8,IsThumb2]>;
4386defm t2LDC2L : t2LdStCop<0b1111, 1, 1, "ldc2l", [(int_arm_ldc2l timm:$cop, timm:$CRd, addrmode5:$addr)]>, Requires<[PreV8,IsThumb2]>;
4387
4388defm t2STC   : t2LdStCop<0b1110, 0, 0, "stc", [(int_arm_stc timm:$cop, timm:$CRd, addrmode5:$addr)]>;
4389defm t2STCL  : t2LdStCop<0b1110, 0, 1, "stcl", [(int_arm_stcl timm:$cop, timm:$CRd, addrmode5:$addr)]>;
4390defm t2STC2  : t2LdStCop<0b1111, 0, 0, "stc2", [(int_arm_stc2 timm:$cop, timm:$CRd, addrmode5:$addr)]>, Requires<[PreV8,IsThumb2]>;
4391defm t2STC2L : t2LdStCop<0b1111, 0, 1, "stc2l", [(int_arm_stc2l timm:$cop, timm:$CRd, addrmode5:$addr)]>, Requires<[PreV8,IsThumb2]>;
4392}
4393
4394
4395//===----------------------------------------------------------------------===//
4396// Move between special register and ARM core register -- for disassembly only
4397//
4398// Move to ARM core register from Special Register
4399
4400// A/R class MRS.
4401//
4402// A/R class can only move from CPSR or SPSR.
4403def t2MRS_AR : T2I<(outs GPR:$Rd), (ins), NoItinerary, "mrs", "\t$Rd, apsr",
4404                  []>, Requires<[IsThumb2,IsNotMClass]> {
4405  bits<4> Rd;
4406  let Inst{31-12} = 0b11110011111011111000;
4407  let Inst{11-8} = Rd;
4408  let Inst{7-0} = 0b00000000;
4409}
4410
4411def : t2InstAlias<"mrs${p} $Rd, cpsr", (t2MRS_AR GPR:$Rd, pred:$p)>;
4412
4413def t2MRSsys_AR: T2I<(outs GPR:$Rd), (ins), NoItinerary, "mrs", "\t$Rd, spsr",
4414                   []>, Requires<[IsThumb2,IsNotMClass]> {
4415  bits<4> Rd;
4416  let Inst{31-12} = 0b11110011111111111000;
4417  let Inst{11-8} = Rd;
4418  let Inst{7-0} = 0b00000000;
4419}
4420
4421def t2MRSbanked : T2I<(outs rGPR:$Rd), (ins banked_reg:$banked),
4422                      NoItinerary, "mrs", "\t$Rd, $banked", []>,
4423                  Requires<[IsThumb, HasVirtualization]> {
4424  bits<6> banked;
4425  bits<4> Rd;
4426
4427  let Inst{31-21} = 0b11110011111;
4428  let Inst{20} = banked{5}; // R bit
4429  let Inst{19-16} = banked{3-0};
4430  let Inst{15-12} = 0b1000;
4431  let Inst{11-8} = Rd;
4432  let Inst{7-5} = 0b001;
4433  let Inst{4} = banked{4};
4434  let Inst{3-0} = 0b0000;
4435}
4436
4437
4438// M class MRS.
4439//
4440// This MRS has a mask field in bits 7-0 and can take more values than
4441// the A/R class (a full msr_mask).
4442def t2MRS_M : T2I<(outs rGPR:$Rd), (ins msr_mask:$SYSm), NoItinerary,
4443                  "mrs", "\t$Rd, $SYSm", []>,
4444              Requires<[IsThumb,IsMClass]> {
4445  bits<4> Rd;
4446  bits<8> SYSm;
4447  let Inst{31-12} = 0b11110011111011111000;
4448  let Inst{11-8} = Rd;
4449  let Inst{7-0} = SYSm;
4450
4451  let Unpredictable{20-16} = 0b11111;
4452  let Unpredictable{13} = 0b1;
4453}
4454
4455
4456// Move from ARM core register to Special Register
4457//
4458// A/R class MSR.
4459//
4460// No need to have both system and application versions, the encodings are the
4461// same and the assembly parser has no way to distinguish between them. The mask
4462// operand contains the special register (R Bit) in bit 4 and bits 3-0 contains
4463// the mask with the fields to be accessed in the special register.
4464let Defs = [CPSR] in
4465def t2MSR_AR : T2I<(outs), (ins msr_mask:$mask, rGPR:$Rn),
4466                   NoItinerary, "msr", "\t$mask, $Rn", []>,
4467               Requires<[IsThumb2,IsNotMClass]> {
4468  bits<5> mask;
4469  bits<4> Rn;
4470  let Inst{31-21} = 0b11110011100;
4471  let Inst{20}    = mask{4}; // R Bit
4472  let Inst{19-16} = Rn;
4473  let Inst{15-12} = 0b1000;
4474  let Inst{11-8}  = mask{3-0};
4475  let Inst{7-0}   = 0;
4476}
4477
4478// However, the MSR (banked register) system instruction (ARMv7VE) *does* have a
4479// separate encoding (distinguished by bit 5.
4480def t2MSRbanked : T2I<(outs), (ins banked_reg:$banked, rGPR:$Rn),
4481                      NoItinerary, "msr", "\t$banked, $Rn", []>,
4482                  Requires<[IsThumb, HasVirtualization]> {
4483  bits<6> banked;
4484  bits<4> Rn;
4485
4486  let Inst{31-21} = 0b11110011100;
4487  let Inst{20} = banked{5}; // R bit
4488  let Inst{19-16} = Rn;
4489  let Inst{15-12} = 0b1000;
4490  let Inst{11-8} = banked{3-0};
4491  let Inst{7-5} = 0b001;
4492  let Inst{4} = banked{4};
4493  let Inst{3-0} = 0b0000;
4494}
4495
4496
4497// M class MSR.
4498//
4499// Move from ARM core register to Special Register
4500let Defs = [CPSR] in
4501def t2MSR_M : T2I<(outs), (ins msr_mask:$SYSm, rGPR:$Rn),
4502                  NoItinerary, "msr", "\t$SYSm, $Rn", []>,
4503              Requires<[IsThumb,IsMClass]> {
4504  bits<12> SYSm;
4505  bits<4> Rn;
4506  let Inst{31-21} = 0b11110011100;
4507  let Inst{20}    = 0b0;
4508  let Inst{19-16} = Rn;
4509  let Inst{15-12} = 0b1000;
4510  let Inst{11-10} = SYSm{11-10};
4511  let Inst{9-8}   = 0b00;
4512  let Inst{7-0}   = SYSm{7-0};
4513
4514  let Unpredictable{20} = 0b1;
4515  let Unpredictable{13} = 0b1;
4516  let Unpredictable{9-8} = 0b11;
4517}
4518
4519
4520//===----------------------------------------------------------------------===//
4521// Move between coprocessor and ARM core register
4522//
4523
4524class t2MovRCopro<bits<4> Op, string opc, bit direction, dag oops, dag iops,
4525                  list<dag> pattern>
4526  : T2Cop<Op, oops, iops, opc, "\t$cop, $opc1, $Rt, $CRn, $CRm, $opc2",
4527          pattern> {
4528  let Inst{27-24} = 0b1110;
4529  let Inst{20} = direction;
4530  let Inst{4} = 1;
4531
4532  bits<4> Rt;
4533  bits<4> cop;
4534  bits<3> opc1;
4535  bits<3> opc2;
4536  bits<4> CRm;
4537  bits<4> CRn;
4538
4539  let Inst{15-12} = Rt;
4540  let Inst{11-8}  = cop;
4541  let Inst{23-21} = opc1;
4542  let Inst{7-5}   = opc2;
4543  let Inst{3-0}   = CRm;
4544  let Inst{19-16} = CRn;
4545
4546  let DecoderNamespace = "Thumb2CoProc";
4547}
4548
4549class t2MovRRCopro<bits<4> Op, string opc, bit direction, dag oops, dag iops,
4550                   list<dag> pattern = []>
4551  : T2Cop<Op, oops, iops, opc, "\t$cop, $opc1, $Rt, $Rt2, $CRm", pattern> {
4552  let Inst{27-24} = 0b1100;
4553  let Inst{23-21} = 0b010;
4554  let Inst{20} = direction;
4555
4556  bits<4> Rt;
4557  bits<4> Rt2;
4558  bits<4> cop;
4559  bits<4> opc1;
4560  bits<4> CRm;
4561
4562  let Inst{15-12} = Rt;
4563  let Inst{19-16} = Rt2;
4564  let Inst{11-8}  = cop;
4565  let Inst{7-4}   = opc1;
4566  let Inst{3-0}   = CRm;
4567
4568  let DecoderNamespace = "Thumb2CoProc";
4569}
4570
4571/* from ARM core register to coprocessor */
4572def t2MCR : t2MovRCopro<0b1110, "mcr", 0,
4573           (outs),
4574           (ins p_imm:$cop, imm0_7:$opc1, GPR:$Rt, c_imm:$CRn,
4575                c_imm:$CRm, imm0_7:$opc2),
4576           [(int_arm_mcr timm:$cop, timm:$opc1, GPR:$Rt, timm:$CRn,
4577                         timm:$CRm, timm:$opc2)]>,
4578           ComplexDeprecationPredicate<"MCR">;
4579def : t2InstAlias<"mcr${p} $cop, $opc1, $Rt, $CRn, $CRm",
4580                  (t2MCR p_imm:$cop, imm0_7:$opc1, GPR:$Rt, c_imm:$CRn,
4581                         c_imm:$CRm, 0, pred:$p)>;
4582def t2MCR2 : t2MovRCopro<0b1111, "mcr2", 0,
4583             (outs), (ins p_imm:$cop, imm0_7:$opc1, GPR:$Rt, c_imm:$CRn,
4584                          c_imm:$CRm, imm0_7:$opc2),
4585             [(int_arm_mcr2 timm:$cop, timm:$opc1, GPR:$Rt, timm:$CRn,
4586                            timm:$CRm, timm:$opc2)]> {
4587  let Predicates = [IsThumb2, PreV8];
4588}
4589def : t2InstAlias<"mcr2${p} $cop, $opc1, $Rt, $CRn, $CRm",
4590                  (t2MCR2 p_imm:$cop, imm0_7:$opc1, GPR:$Rt, c_imm:$CRn,
4591                          c_imm:$CRm, 0, pred:$p)>;
4592
4593/* from coprocessor to ARM core register */
4594def t2MRC : t2MovRCopro<0b1110, "mrc", 1,
4595             (outs GPRwithAPSR:$Rt), (ins p_imm:$cop, imm0_7:$opc1, c_imm:$CRn,
4596                                  c_imm:$CRm, imm0_7:$opc2), []>;
4597def : t2InstAlias<"mrc${p} $cop, $opc1, $Rt, $CRn, $CRm",
4598                  (t2MRC GPRwithAPSR:$Rt, p_imm:$cop, imm0_7:$opc1, c_imm:$CRn,
4599                         c_imm:$CRm, 0, pred:$p)>;
4600
4601def t2MRC2 : t2MovRCopro<0b1111, "mrc2", 1,
4602             (outs GPRwithAPSR:$Rt), (ins p_imm:$cop, imm0_7:$opc1, c_imm:$CRn,
4603                                  c_imm:$CRm, imm0_7:$opc2), []> {
4604  let Predicates = [IsThumb2, PreV8];
4605}
4606def : t2InstAlias<"mrc2${p} $cop, $opc1, $Rt, $CRn, $CRm",
4607                  (t2MRC2 GPRwithAPSR:$Rt, p_imm:$cop, imm0_7:$opc1, c_imm:$CRn,
4608                          c_imm:$CRm, 0, pred:$p)>;
4609
4610def : T2v6Pat<(int_arm_mrc  timm:$cop, timm:$opc1, timm:$CRn, timm:$CRm, timm:$opc2),
4611              (t2MRC p_imm:$cop, imm0_7:$opc1, c_imm:$CRn, c_imm:$CRm, imm0_7:$opc2)>;
4612
4613def : T2v6Pat<(int_arm_mrc2 timm:$cop, timm:$opc1, timm:$CRn, timm:$CRm, timm:$opc2),
4614              (t2MRC2 p_imm:$cop, imm0_7:$opc1, c_imm:$CRn, c_imm:$CRm, imm0_7:$opc2)>;
4615
4616
4617/* from ARM core register to coprocessor */
4618def t2MCRR : t2MovRRCopro<0b1110, "mcrr", 0, (outs),
4619                         (ins p_imm:$cop, imm0_15:$opc1, GPR:$Rt, GPR:$Rt2,
4620                         c_imm:$CRm),
4621                        [(int_arm_mcrr timm:$cop, timm:$opc1, GPR:$Rt, GPR:$Rt2,
4622                                       timm:$CRm)]>;
4623def t2MCRR2 : t2MovRRCopro<0b1111, "mcrr2", 0, (outs),
4624                          (ins p_imm:$cop, imm0_15:$opc1, GPR:$Rt, GPR:$Rt2,
4625                           c_imm:$CRm),
4626                          [(int_arm_mcrr2 timm:$cop, timm:$opc1, GPR:$Rt,
4627                                          GPR:$Rt2, timm:$CRm)]> {
4628  let Predicates = [IsThumb2, PreV8];
4629}
4630
4631/* from coprocessor to ARM core register */
4632def t2MRRC : t2MovRRCopro<0b1110, "mrrc", 1, (outs GPR:$Rt, GPR:$Rt2),
4633                          (ins p_imm:$cop, imm0_15:$opc1, c_imm:$CRm)>;
4634
4635def t2MRRC2 : t2MovRRCopro<0b1111, "mrrc2", 1, (outs GPR:$Rt, GPR:$Rt2),
4636                           (ins p_imm:$cop, imm0_15:$opc1, c_imm:$CRm)> {
4637  let Predicates = [IsThumb2, PreV8];
4638}
4639
4640//===----------------------------------------------------------------------===//
4641// Other Coprocessor Instructions.
4642//
4643
4644def t2CDP : T2Cop<0b1110, (outs), (ins p_imm:$cop, imm0_15:$opc1,
4645                 c_imm:$CRd, c_imm:$CRn, c_imm:$CRm, imm0_7:$opc2),
4646                 "cdp", "\t$cop, $opc1, $CRd, $CRn, $CRm, $opc2",
4647                 [(int_arm_cdp timm:$cop, timm:$opc1, timm:$CRd, timm:$CRn,
4648                               timm:$CRm, timm:$opc2)]> {
4649  let Inst{27-24} = 0b1110;
4650
4651  bits<4> opc1;
4652  bits<4> CRn;
4653  bits<4> CRd;
4654  bits<4> cop;
4655  bits<3> opc2;
4656  bits<4> CRm;
4657
4658  let Inst{3-0}   = CRm;
4659  let Inst{4}     = 0;
4660  let Inst{7-5}   = opc2;
4661  let Inst{11-8}  = cop;
4662  let Inst{15-12} = CRd;
4663  let Inst{19-16} = CRn;
4664  let Inst{23-20} = opc1;
4665
4666  let Predicates = [IsThumb2, PreV8];
4667  let DecoderNamespace = "Thumb2CoProc";
4668}
4669
4670def t2CDP2 : T2Cop<0b1111, (outs), (ins p_imm:$cop, imm0_15:$opc1,
4671                   c_imm:$CRd, c_imm:$CRn, c_imm:$CRm, imm0_7:$opc2),
4672                   "cdp2", "\t$cop, $opc1, $CRd, $CRn, $CRm, $opc2",
4673                   [(int_arm_cdp2 timm:$cop, timm:$opc1, timm:$CRd, timm:$CRn,
4674                                  timm:$CRm, timm:$opc2)]> {
4675  let Inst{27-24} = 0b1110;
4676
4677  bits<4> opc1;
4678  bits<4> CRn;
4679  bits<4> CRd;
4680  bits<4> cop;
4681  bits<3> opc2;
4682  bits<4> CRm;
4683
4684  let Inst{3-0}   = CRm;
4685  let Inst{4}     = 0;
4686  let Inst{7-5}   = opc2;
4687  let Inst{11-8}  = cop;
4688  let Inst{15-12} = CRd;
4689  let Inst{19-16} = CRn;
4690  let Inst{23-20} = opc1;
4691
4692  let Predicates = [IsThumb2, PreV8];
4693  let DecoderNamespace = "Thumb2CoProc";
4694}
4695
4696
4697// Reading thread pointer from coprocessor register
4698def : T2Pat<(ARMthread_pointer), (t2MRC 15, 0, 13, 0, 3)>,
4699      Requires<[IsThumb2, IsReadTPHard]>;
4700
4701//===----------------------------------------------------------------------===//
4702// ARMv8.1 Privilege Access Never extension
4703//
4704// SETPAN #imm1
4705
4706def t2SETPAN : T1I<(outs), (ins imm0_1:$imm), NoItinerary, "setpan\t$imm", []>,
4707               T1Misc<0b0110000>, Requires<[IsThumb2, HasV8, HasV8_1a]> {
4708  bits<1> imm;
4709
4710  let Inst{4} = 0b1;
4711  let Inst{3} = imm;
4712  let Inst{2-0} = 0b000;
4713
4714  let Unpredictable{4} = 0b1;
4715  let Unpredictable{2-0} = 0b111;
4716}
4717
4718//===----------------------------------------------------------------------===//
4719// ARMv8-M Security Extensions instructions
4720//
4721
4722let hasSideEffects = 1 in
4723def t2SG : T2I<(outs), (ins), NoItinerary, "sg", "", []>,
4724           Requires<[Has8MSecExt]> {
4725  let Inst = 0xe97fe97f;
4726}
4727
4728class T2TT<bits<2> at, string asm, list<dag> pattern>
4729  : T2I<(outs rGPR:$Rt), (ins GPRnopc:$Rn), NoItinerary, asm, "\t$Rt, $Rn",
4730        pattern> {
4731  bits<4> Rn;
4732  bits<4> Rt;
4733
4734  let Inst{31-20} = 0b111010000100;
4735  let Inst{19-16} = Rn;
4736  let Inst{15-12} = 0b1111;
4737  let Inst{11-8} = Rt;
4738  let Inst{7-6} = at;
4739  let Inst{5-0} = 0b000000;
4740
4741  let Unpredictable{5-0} = 0b111111;
4742}
4743
4744def t2TT   : T2TT<0b00, "tt",
4745                 [(set rGPR:$Rt, (int_arm_cmse_tt   GPRnopc:$Rn))]>,
4746             Requires<[IsThumb, Has8MSecExt]>;
4747def t2TTT  : T2TT<0b01, "ttt",
4748                  [(set rGPR:$Rt, (int_arm_cmse_ttt  GPRnopc:$Rn))]>,
4749             Requires<[IsThumb, Has8MSecExt]>;
4750def t2TTA  : T2TT<0b10, "tta",
4751                  [(set rGPR:$Rt, (int_arm_cmse_tta  GPRnopc:$Rn))]>,
4752             Requires<[IsThumb, Has8MSecExt]>;
4753def t2TTAT : T2TT<0b11, "ttat",
4754                  [(set rGPR:$Rt, (int_arm_cmse_ttat GPRnopc:$Rn))]>,
4755             Requires<[IsThumb, Has8MSecExt]>;
4756
4757//===----------------------------------------------------------------------===//
4758// Non-Instruction Patterns
4759//
4760
4761// SXT/UXT with no rotate
4762let AddedComplexity = 16 in {
4763def : T2Pat<(and rGPR:$Rm, 0x000000FF), (t2UXTB rGPR:$Rm, 0)>,
4764           Requires<[IsThumb2]>;
4765def : T2Pat<(and rGPR:$Rm, 0x0000FFFF), (t2UXTH rGPR:$Rm, 0)>,
4766           Requires<[IsThumb2]>;
4767def : T2Pat<(and rGPR:$Rm, 0x00FF00FF), (t2UXTB16 rGPR:$Rm, 0)>,
4768           Requires<[HasDSP, IsThumb2]>;
4769def : T2Pat<(add rGPR:$Rn, (and rGPR:$Rm, 0x00FF)),
4770            (t2UXTAB rGPR:$Rn, rGPR:$Rm, 0)>,
4771           Requires<[HasDSP, IsThumb2]>;
4772def : T2Pat<(add rGPR:$Rn, (and rGPR:$Rm, 0xFFFF)),
4773            (t2UXTAH rGPR:$Rn, rGPR:$Rm, 0)>,
4774           Requires<[HasDSP, IsThumb2]>;
4775}
4776
4777def : T2Pat<(sext_inreg rGPR:$Src, i8),  (t2SXTB rGPR:$Src, 0)>,
4778           Requires<[IsThumb2]>;
4779def : T2Pat<(sext_inreg rGPR:$Src, i16), (t2SXTH rGPR:$Src, 0)>,
4780           Requires<[IsThumb2]>;
4781def : T2Pat<(add rGPR:$Rn, (sext_inreg rGPR:$Rm, i8)),
4782            (t2SXTAB rGPR:$Rn, rGPR:$Rm, 0)>,
4783           Requires<[HasDSP, IsThumb2]>;
4784def : T2Pat<(add rGPR:$Rn, (sext_inreg rGPR:$Rm, i16)),
4785            (t2SXTAH rGPR:$Rn, rGPR:$Rm, 0)>,
4786           Requires<[HasDSP, IsThumb2]>;
4787
4788// Atomic load/store patterns
4789def : T2Pat<(atomic_load_8   t2addrmode_imm12:$addr),
4790            (t2LDRBi12  t2addrmode_imm12:$addr)>;
4791def : T2Pat<(atomic_load_8   t2addrmode_negimm8:$addr),
4792            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
4793def : T2Pat<(atomic_load_8   t2addrmode_so_reg:$addr),
4794            (t2LDRBs    t2addrmode_so_reg:$addr)>;
4795def : T2Pat<(atomic_load_16  t2addrmode_imm12:$addr),
4796            (t2LDRHi12  t2addrmode_imm12:$addr)>;
4797def : T2Pat<(atomic_load_16  t2addrmode_negimm8:$addr),
4798            (t2LDRHi8   t2addrmode_negimm8:$addr)>;
4799def : T2Pat<(atomic_load_16  t2addrmode_so_reg:$addr),
4800            (t2LDRHs    t2addrmode_so_reg:$addr)>;
4801def : T2Pat<(atomic_load_32  t2addrmode_imm12:$addr),
4802            (t2LDRi12   t2addrmode_imm12:$addr)>;
4803def : T2Pat<(atomic_load_32  t2addrmode_negimm8:$addr),
4804            (t2LDRi8    t2addrmode_negimm8:$addr)>;
4805def : T2Pat<(atomic_load_32  t2addrmode_so_reg:$addr),
4806            (t2LDRs     t2addrmode_so_reg:$addr)>;
4807def : T2Pat<(atomic_store_8  t2addrmode_imm12:$addr, GPR:$val),
4808            (t2STRBi12  GPR:$val, t2addrmode_imm12:$addr)>;
4809def : T2Pat<(atomic_store_8  t2addrmode_negimm8:$addr, GPR:$val),
4810            (t2STRBi8   GPR:$val, t2addrmode_negimm8:$addr)>;
4811def : T2Pat<(atomic_store_8  t2addrmode_so_reg:$addr, GPR:$val),
4812            (t2STRBs    GPR:$val, t2addrmode_so_reg:$addr)>;
4813def : T2Pat<(atomic_store_16 t2addrmode_imm12:$addr, GPR:$val),
4814            (t2STRHi12  GPR:$val, t2addrmode_imm12:$addr)>;
4815def : T2Pat<(atomic_store_16 t2addrmode_negimm8:$addr, GPR:$val),
4816            (t2STRHi8   GPR:$val, t2addrmode_negimm8:$addr)>;
4817def : T2Pat<(atomic_store_16 t2addrmode_so_reg:$addr, GPR:$val),
4818            (t2STRHs    GPR:$val, t2addrmode_so_reg:$addr)>;
4819def : T2Pat<(atomic_store_32 t2addrmode_imm12:$addr, GPR:$val),
4820            (t2STRi12   GPR:$val, t2addrmode_imm12:$addr)>;
4821def : T2Pat<(atomic_store_32 t2addrmode_negimm8:$addr, GPR:$val),
4822            (t2STRi8    GPR:$val, t2addrmode_negimm8:$addr)>;
4823def : T2Pat<(atomic_store_32 t2addrmode_so_reg:$addr, GPR:$val),
4824            (t2STRs     GPR:$val, t2addrmode_so_reg:$addr)>;
4825
4826let AddedComplexity = 8, Predicates = [IsThumb, HasAcquireRelease, HasV7Clrex] in {
4827  def : Pat<(atomic_load_acquire_8 addr_offset_none:$addr),  (t2LDAB addr_offset_none:$addr)>;
4828  def : Pat<(atomic_load_acquire_16 addr_offset_none:$addr), (t2LDAH addr_offset_none:$addr)>;
4829  def : Pat<(atomic_load_acquire_32 addr_offset_none:$addr), (t2LDA  addr_offset_none:$addr)>;
4830  def : Pat<(atomic_store_release_8 addr_offset_none:$addr, GPR:$val),  (t2STLB GPR:$val, addr_offset_none:$addr)>;
4831  def : Pat<(atomic_store_release_16 addr_offset_none:$addr, GPR:$val), (t2STLH GPR:$val, addr_offset_none:$addr)>;
4832  def : Pat<(atomic_store_release_32 addr_offset_none:$addr, GPR:$val), (t2STL  GPR:$val, addr_offset_none:$addr)>;
4833}
4834
4835
4836//===----------------------------------------------------------------------===//
4837// Assembler aliases
4838//
4839
4840// Aliases for ADC without the ".w" optional width specifier.
4841def : t2InstAlias<"adc${s}${p} $Rd, $Rn, $Rm",
4842                  (t2ADCrr rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4843def : t2InstAlias<"adc${s}${p} $Rd, $Rn, $ShiftedRm",
4844                  (t2ADCrs rGPR:$Rd, rGPR:$Rn, t2_so_reg:$ShiftedRm,
4845                           pred:$p, cc_out:$s)>;
4846
4847// Aliases for SBC without the ".w" optional width specifier.
4848def : t2InstAlias<"sbc${s}${p} $Rd, $Rn, $Rm",
4849                  (t2SBCrr rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4850def : t2InstAlias<"sbc${s}${p} $Rd, $Rn, $ShiftedRm",
4851                  (t2SBCrs rGPR:$Rd, rGPR:$Rn, t2_so_reg:$ShiftedRm,
4852                           pred:$p, cc_out:$s)>;
4853
4854// Aliases for ADD without the ".w" optional width specifier.
4855def : t2InstAlias<"add${s}${p} $Rd, $Rn, $imm",
4856        (t2ADDri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm:$imm, pred:$p,
4857         cc_out:$s)>;
4858def : t2InstAlias<"add${p} $Rd, $Rn, $imm",
4859           (t2ADDri12 rGPR:$Rd, GPR:$Rn, imm0_4095:$imm, pred:$p)>;
4860def : t2InstAlias<"add${s}${p} $Rd, $Rn, $Rm",
4861              (t2ADDrr GPRnopc:$Rd, GPRnopc:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4862def : t2InstAlias<"add${s}${p} $Rd, $Rn, $ShiftedRm",
4863                  (t2ADDrs GPRnopc:$Rd, GPRnopc:$Rn, t2_so_reg:$ShiftedRm,
4864                           pred:$p, cc_out:$s)>;
4865// ... and with the destination and source register combined.
4866def : t2InstAlias<"add${s}${p} $Rdn, $imm",
4867      (t2ADDri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4868def : t2InstAlias<"add${p} $Rdn, $imm",
4869           (t2ADDri12 rGPR:$Rdn, rGPR:$Rdn, imm0_4095:$imm, pred:$p)>;
4870def : t2InstAlias<"addw${p} $Rdn, $imm",
4871           (t2ADDri12 rGPR:$Rdn, rGPR:$Rdn, imm0_4095:$imm, pred:$p)>;
4872def : t2InstAlias<"add${s}${p} $Rdn, $Rm",
4873            (t2ADDrr GPRnopc:$Rdn, GPRnopc:$Rdn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4874def : t2InstAlias<"add${s}${p} $Rdn, $ShiftedRm",
4875                  (t2ADDrs GPRnopc:$Rdn, GPRnopc:$Rdn, t2_so_reg:$ShiftedRm,
4876                           pred:$p, cc_out:$s)>;
4877
4878// add w/ negative immediates is just a sub.
4879def : t2InstSubst<"add${s}${p} $Rd, $Rn, $imm",
4880        (t2SUBri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm_neg:$imm, pred:$p,
4881                 cc_out:$s)>;
4882def : t2InstSubst<"add${p} $Rd, $Rn, $imm",
4883           (t2SUBri12 rGPR:$Rd, GPR:$Rn, imm0_4095_neg:$imm, pred:$p)>;
4884def : t2InstSubst<"add${s}${p} $Rdn, $imm",
4885      (t2SUBri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_neg:$imm, pred:$p,
4886               cc_out:$s)>;
4887def : t2InstSubst<"add${p} $Rdn, $imm",
4888           (t2SUBri12 rGPR:$Rdn, rGPR:$Rdn, imm0_4095_neg:$imm, pred:$p)>;
4889
4890def : t2InstSubst<"add${s}${p}.w $Rd, $Rn, $imm",
4891        (t2SUBri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm_neg:$imm, pred:$p,
4892                 cc_out:$s)>;
4893def : t2InstSubst<"addw${p} $Rd, $Rn, $imm",
4894           (t2SUBri12 rGPR:$Rd, rGPR:$Rn, imm0_4095_neg:$imm, pred:$p)>;
4895def : t2InstSubst<"add${s}${p}.w $Rdn, $imm",
4896      (t2SUBri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_neg:$imm, pred:$p,
4897               cc_out:$s)>;
4898def : t2InstSubst<"addw${p} $Rdn, $imm",
4899           (t2SUBri12 rGPR:$Rdn, rGPR:$Rdn, imm0_4095_neg:$imm, pred:$p)>;
4900
4901
4902// Aliases for SUB without the ".w" optional width specifier.
4903def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $imm",
4904        (t2SUBri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4905def : t2InstAlias<"sub${p} $Rd, $Rn, $imm",
4906           (t2SUBri12 rGPR:$Rd, GPR:$Rn, imm0_4095:$imm, pred:$p)>;
4907def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $Rm",
4908              (t2SUBrr GPRnopc:$Rd, GPRnopc:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4909def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $ShiftedRm",
4910                  (t2SUBrs GPRnopc:$Rd, GPRnopc:$Rn, t2_so_reg:$ShiftedRm,
4911                           pred:$p, cc_out:$s)>;
4912// ... and with the destination and source register combined.
4913def : t2InstAlias<"sub${s}${p} $Rdn, $imm",
4914      (t2SUBri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4915def : t2InstAlias<"sub${p} $Rdn, $imm",
4916           (t2SUBri12 rGPR:$Rdn, rGPR:$Rdn, imm0_4095:$imm, pred:$p)>;
4917def : t2InstAlias<"subw${p} $Rdn, $imm",
4918           (t2SUBri12 rGPR:$Rdn, rGPR:$Rdn, imm0_4095:$imm, pred:$p)>;
4919def : t2InstAlias<"sub${s}${p}.w $Rdn, $Rm",
4920            (t2SUBrr GPRnopc:$Rdn, GPRnopc:$Rdn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4921def : t2InstAlias<"sub${s}${p} $Rdn, $Rm",
4922            (t2SUBrr GPRnopc:$Rdn, GPRnopc:$Rdn, rGPR:$Rm, pred:$p, cc_out:$s)>;
4923def : t2InstAlias<"sub${s}${p} $Rdn, $ShiftedRm",
4924                  (t2SUBrs GPRnopc:$Rdn, GPRnopc:$Rdn, t2_so_reg:$ShiftedRm,
4925                           pred:$p, cc_out:$s)>;
4926
4927// SP to SP alike aliases
4928// Aliases for ADD without the ".w" optional width specifier.
4929def : t2InstAlias<"add${s}${p} $Rd, $Rn, $imm",
4930        (t2ADDspImm GPRsp:$Rd, GPRsp:$Rn, t2_so_imm:$imm, pred:$p,
4931         cc_out:$s)>;
4932def : t2InstAlias<"add${p} $Rd, $Rn, $imm",
4933           (t2ADDspImm12 GPRsp:$Rd, GPRsp:$Rn, imm0_4095:$imm, pred:$p)>;
4934// ... and with the destination and source register combined.
4935def : t2InstAlias<"add${s}${p} $Rdn, $imm",
4936      (t2ADDspImm GPRsp:$Rdn, GPRsp:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4937
4938def : t2InstAlias<"add${s}${p}.w $Rdn, $imm",
4939      (t2ADDspImm GPRsp:$Rdn, GPRsp:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4940
4941def : t2InstAlias<"add${p} $Rdn, $imm",
4942           (t2ADDspImm12 GPRsp:$Rdn, GPRsp:$Rdn, imm0_4095:$imm, pred:$p)>;
4943
4944def : t2InstAlias<"addw${p} $Rdn, $imm",
4945           (t2ADDspImm12 GPRsp:$Rdn, GPRsp:$Rdn, imm0_4095:$imm, pred:$p)>;
4946
4947// add w/ negative immediates is just a sub.
4948def : t2InstSubst<"add${s}${p} $Rd, $Rn, $imm",
4949        (t2SUBspImm GPRsp:$Rd, GPRsp:$Rn, t2_so_imm_neg:$imm, pred:$p,
4950                 cc_out:$s)>;
4951def : t2InstSubst<"add${p} $Rd, $Rn, $imm",
4952           (t2SUBspImm12 GPRsp:$Rd, GPRsp:$Rn, imm0_4095_neg:$imm, pred:$p)>;
4953def : t2InstSubst<"add${s}${p} $Rdn, $imm",
4954      (t2SUBspImm GPRsp:$Rdn, GPRsp:$Rdn, t2_so_imm_neg:$imm, pred:$p,
4955               cc_out:$s)>;
4956def : t2InstSubst<"add${p} $Rdn, $imm",
4957           (t2SUBspImm12 GPRsp:$Rdn, GPRsp:$Rdn, imm0_4095_neg:$imm, pred:$p)>;
4958
4959def : t2InstSubst<"add${s}${p}.w $Rd, $Rn, $imm",
4960        (t2SUBspImm GPRsp:$Rd, GPRsp:$Rn, t2_so_imm_neg:$imm, pred:$p,
4961                 cc_out:$s)>;
4962def : t2InstSubst<"addw${p} $Rd, $Rn, $imm",
4963           (t2SUBspImm12 GPRsp:$Rd, GPRsp:$Rn, imm0_4095_neg:$imm, pred:$p)>;
4964def : t2InstSubst<"add${s}${p}.w $Rdn, $imm",
4965      (t2SUBspImm GPRsp:$Rdn, GPRsp:$Rdn, t2_so_imm_neg:$imm, pred:$p,
4966               cc_out:$s)>;
4967def : t2InstSubst<"addw${p} $Rdn, $imm",
4968           (t2SUBspImm12 GPRsp:$Rdn, GPRsp:$Rdn, imm0_4095_neg:$imm, pred:$p)>;
4969
4970
4971// Aliases for SUB without the ".w" optional width specifier.
4972def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $imm",
4973        (t2SUBspImm GPRsp:$Rd, GPRsp:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4974def : t2InstAlias<"sub${p} $Rd, $Rn, $imm",
4975           (t2SUBspImm12 GPRsp:$Rd, GPRsp:$Rn, imm0_4095:$imm, pred:$p)>;
4976// ... and with the destination and source register combined.
4977def : t2InstAlias<"sub${s}${p} $Rdn, $imm",
4978      (t2SUBspImm GPRsp:$Rdn, GPRsp:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4979def : t2InstAlias<"sub${s}${p}.w $Rdn, $imm",
4980      (t2SUBspImm GPRsp:$Rdn, GPRsp:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
4981def : t2InstAlias<"sub${p} $Rdn, $imm",
4982           (t2SUBspImm12 GPRsp:$Rdn, GPRsp:$Rdn, imm0_4095:$imm, pred:$p)>;
4983def : t2InstAlias<"subw${p} $Rdn, $imm",
4984           (t2SUBspImm12 GPRsp:$Rdn, GPRsp:$Rdn, imm0_4095:$imm, pred:$p)>;
4985
4986// Alias for compares without the ".w" optional width specifier.
4987def : t2InstAlias<"cmn${p} $Rn, $Rm",
4988                  (t2CMNzrr GPRnopc:$Rn, rGPR:$Rm, pred:$p)>;
4989def : t2InstAlias<"teq${p} $Rn, $Rm",
4990                  (t2TEQrr rGPR:$Rn, rGPR:$Rm, pred:$p)>;
4991def : t2InstAlias<"tst${p} $Rn, $Rm",
4992                  (t2TSTrr rGPR:$Rn, rGPR:$Rm, pred:$p)>;
4993
4994// Memory barriers
4995def : InstAlias<"dmb${p}.w\t$opt", (t2DMB memb_opt:$opt, pred:$p), 0>, Requires<[HasDB]>;
4996def : InstAlias<"dmb${p}", (t2DMB 0xf, pred:$p), 0>, Requires<[HasDB]>;
4997def : InstAlias<"dmb${p}.w", (t2DMB 0xf, pred:$p), 0>, Requires<[HasDB]>;
4998def : InstAlias<"dsb${p}.w\t$opt", (t2DSB memb_opt:$opt, pred:$p), 0>, Requires<[HasDB]>;
4999def : InstAlias<"dsb${p}", (t2DSB 0xf, pred:$p), 0>, Requires<[HasDB]>;
5000def : InstAlias<"dsb${p}.w", (t2DSB 0xf, pred:$p), 0>, Requires<[HasDB]>;
5001def : InstAlias<"isb${p}.w\t$opt", (t2ISB memb_opt:$opt, pred:$p), 0>, Requires<[HasDB]>;
5002def : InstAlias<"isb${p}", (t2ISB 0xf, pred:$p), 0>, Requires<[HasDB]>;
5003def : InstAlias<"isb${p}.w", (t2ISB 0xf, pred:$p), 0>, Requires<[HasDB]>;
5004
5005// Non-predicable aliases of a predicable DSB: the predicate is (14, 0) where
5006// 14 = AL (always execute) and 0 = "instruction doesn't read the CPSR".
5007def : InstAlias<"ssbb", (t2DSB 0x0, 14, 0), 1>, Requires<[HasDB, IsThumb2]>;
5008def : InstAlias<"pssbb", (t2DSB 0x4, 14, 0), 1>, Requires<[HasDB, IsThumb2]>;
5009
5010// Armv8-R 'Data Full Barrier'
5011def : InstAlias<"dfb${p}", (t2DSB 0xc, pred:$p), 1>, Requires<[HasDFB]>;
5012
5013// SpeculationBarrierEndBB must only be used after an unconditional control
5014// flow, i.e. after a terminator for which isBarrier is True.
5015let hasSideEffects = 1, isCodeGenOnly = 1, isTerminator = 1, isBarrier = 1 in {
5016  // This gets lowered to a pair of 4-byte instructions
5017  let Size = 8 in
5018  def t2SpeculationBarrierISBDSBEndBB
5019      : PseudoInst<(outs), (ins), NoItinerary, []>, Sched<[]>;
5020  // This gets lowered to a single 4-byte instructions
5021  let Size = 4 in
5022  def t2SpeculationBarrierSBEndBB
5023      : PseudoInst<(outs), (ins), NoItinerary, []>, Sched<[]>;
5024}
5025
5026// Alias for LDR, LDRB, LDRH, LDRSB, and LDRSH without the ".w" optional
5027// width specifier.
5028def : t2InstAlias<"ldr${p} $Rt, $addr",
5029                  (t2LDRi12 GPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5030def : t2InstAlias<"ldrb${p} $Rt, $addr",
5031                  (t2LDRBi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5032def : t2InstAlias<"ldrh${p} $Rt, $addr",
5033                  (t2LDRHi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5034def : t2InstAlias<"ldrsb${p} $Rt, $addr",
5035                  (t2LDRSBi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5036def : t2InstAlias<"ldrsh${p} $Rt, $addr",
5037                  (t2LDRSHi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5038
5039def : t2InstAlias<"ldr${p} $Rt, $addr",
5040                  (t2LDRs GPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5041def : t2InstAlias<"ldrb${p} $Rt, $addr",
5042                  (t2LDRBs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5043def : t2InstAlias<"ldrh${p} $Rt, $addr",
5044                  (t2LDRHs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5045def : t2InstAlias<"ldrsb${p} $Rt, $addr",
5046                  (t2LDRSBs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5047def : t2InstAlias<"ldrsh${p} $Rt, $addr",
5048                  (t2LDRSHs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5049
5050def : t2InstAlias<"ldr${p} $Rt, $addr",
5051                  (t2LDRpci GPR:$Rt, t2ldrlabel:$addr, pred:$p)>;
5052def : t2InstAlias<"ldrb${p} $Rt, $addr",
5053                  (t2LDRBpci rGPR:$Rt, t2ldrlabel:$addr, pred:$p)>;
5054def : t2InstAlias<"ldrh${p} $Rt, $addr",
5055                  (t2LDRHpci rGPR:$Rt, t2ldrlabel:$addr, pred:$p)>;
5056def : t2InstAlias<"ldrsb${p} $Rt, $addr",
5057                  (t2LDRSBpci rGPR:$Rt, t2ldrlabel:$addr, pred:$p)>;
5058def : t2InstAlias<"ldrsh${p} $Rt, $addr",
5059                  (t2LDRSHpci rGPR:$Rt, t2ldrlabel:$addr, pred:$p)>;
5060
5061// Alias for MVN with(out) the ".w" optional width specifier.
5062def : t2InstAlias<"mvn${s}${p}.w $Rd, $imm",
5063           (t2MVNi rGPR:$Rd, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
5064def : t2InstAlias<"mvn${s}${p} $Rd, $Rm",
5065           (t2MVNr rGPR:$Rd, rGPR:$Rm, pred:$p, cc_out:$s)>;
5066def : t2InstAlias<"mvn${s}${p} $Rd, $ShiftedRm",
5067           (t2MVNs rGPR:$Rd, t2_so_reg:$ShiftedRm, pred:$p, cc_out:$s)>;
5068
5069// PKHBT/PKHTB with default shift amount. PKHTB is equivalent to PKHBT with the
5070// input operands swapped when the shift amount is zero (i.e., unspecified).
5071def : InstAlias<"pkhbt${p} $Rd, $Rn, $Rm",
5072                (t2PKHBT rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5073            Requires<[HasDSP, IsThumb2]>;
5074def : InstAlias<"pkhtb${p} $Rd, $Rn, $Rm",
5075                (t2PKHBT rGPR:$Rd, rGPR:$Rm, rGPR:$Rn, 0, pred:$p), 0>,
5076            Requires<[HasDSP, IsThumb2]>;
5077
5078// PUSH/POP aliases for STM/LDM
5079def : t2InstAlias<"push${p}.w $regs", (t2STMDB_UPD SP, pred:$p, reglist:$regs)>;
5080def : t2InstAlias<"push${p} $regs", (t2STMDB_UPD SP, pred:$p, reglist:$regs)>;
5081def : t2InstAlias<"pop${p}.w $regs", (t2LDMIA_UPD SP, pred:$p, reglist:$regs)>;
5082def : t2InstAlias<"pop${p} $regs", (t2LDMIA_UPD SP, pred:$p, reglist:$regs)>;
5083
5084// STMIA/STMIA_UPD aliases w/o the optional .w suffix
5085def : t2InstAlias<"stm${p} $Rn, $regs",
5086                  (t2STMIA GPR:$Rn, pred:$p, reglist:$regs)>;
5087def : t2InstAlias<"stm${p} $Rn!, $regs",
5088                  (t2STMIA_UPD GPR:$Rn, pred:$p, reglist:$regs)>;
5089
5090// LDMIA/LDMIA_UPD aliases w/o the optional .w suffix
5091def : t2InstAlias<"ldm${p} $Rn, $regs",
5092                  (t2LDMIA GPR:$Rn, pred:$p, reglist:$regs)>;
5093def : t2InstAlias<"ldm${p} $Rn!, $regs",
5094                  (t2LDMIA_UPD GPR:$Rn, pred:$p, reglist:$regs)>;
5095
5096// STMDB/STMDB_UPD aliases w/ the optional .w suffix
5097def : t2InstAlias<"stmdb${p}.w $Rn, $regs",
5098                  (t2STMDB GPR:$Rn, pred:$p, reglist:$regs)>;
5099def : t2InstAlias<"stmdb${p}.w $Rn!, $regs",
5100                  (t2STMDB_UPD GPR:$Rn, pred:$p, reglist:$regs)>;
5101
5102// LDMDB/LDMDB_UPD aliases w/ the optional .w suffix
5103def : t2InstAlias<"ldmdb${p}.w $Rn, $regs",
5104                  (t2LDMDB GPR:$Rn, pred:$p, reglist:$regs)>;
5105def : t2InstAlias<"ldmdb${p}.w $Rn!, $regs",
5106                  (t2LDMDB_UPD GPR:$Rn, pred:$p, reglist:$regs)>;
5107
5108// Alias for REV/REV16/REVSH without the ".w" optional width specifier.
5109def : t2InstAlias<"rev${p} $Rd, $Rm", (t2REV rGPR:$Rd, rGPR:$Rm, pred:$p)>;
5110def : t2InstAlias<"rev16${p} $Rd, $Rm", (t2REV16 rGPR:$Rd, rGPR:$Rm, pred:$p)>;
5111def : t2InstAlias<"revsh${p} $Rd, $Rm", (t2REVSH rGPR:$Rd, rGPR:$Rm, pred:$p)>;
5112
5113
5114// Alias for RSB with and without the ".w" optional width specifier, with and
5115// without explicit destination register.
5116def : t2InstAlias<"rsb${s}${p} $Rd, $Rn, $imm",
5117           (t2RSBri rGPR:$Rd, rGPR:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
5118def : t2InstAlias<"rsb${s}${p} $Rdn, $imm",
5119           (t2RSBri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
5120def : t2InstAlias<"rsb${s}${p} $Rdn, $Rm",
5121           (t2RSBrr rGPR:$Rdn, rGPR:$Rdn, rGPR:$Rm, pred:$p, cc_out:$s)>;
5122def : t2InstAlias<"rsb${s}${p} $Rdn, $ShiftedRm",
5123           (t2RSBrs rGPR:$Rdn, rGPR:$Rdn, t2_so_reg:$ShiftedRm, pred:$p,
5124                    cc_out:$s)>;
5125def : t2InstAlias<"rsb${s}${p}.w $Rdn, $Rm",
5126           (t2RSBrr rGPR:$Rdn, rGPR:$Rdn, rGPR:$Rm, pred:$p, cc_out:$s)>;
5127def : t2InstAlias<"rsb${s}${p}.w $Rd, $Rn, $Rm",
5128           (t2RSBrr rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
5129def : t2InstAlias<"rsb${s}${p}.w $Rd, $Rn, $ShiftedRm",
5130           (t2RSBrs rGPR:$Rd, rGPR:$Rn, t2_so_reg:$ShiftedRm, pred:$p,
5131                    cc_out:$s)>;
5132
5133// SSAT/USAT optional shift operand.
5134def : t2InstAlias<"ssat${p} $Rd, $sat_imm, $Rn",
5135                  (t2SSAT rGPR:$Rd, imm1_32:$sat_imm, rGPR:$Rn, 0, pred:$p)>;
5136def : t2InstAlias<"usat${p} $Rd, $sat_imm, $Rn",
5137                  (t2USAT rGPR:$Rd, imm0_31:$sat_imm, rGPR:$Rn, 0, pred:$p)>;
5138
5139// STM w/o the .w suffix.
5140def : t2InstAlias<"stm${p} $Rn, $regs",
5141                  (t2STMIA GPR:$Rn, pred:$p, reglist:$regs)>;
5142
5143// Alias for STR, STRB, and STRH without the ".w" optional
5144// width specifier.
5145def : t2InstAlias<"str${p} $Rt, $addr",
5146                  (t2STRi12 GPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5147def : t2InstAlias<"strb${p} $Rt, $addr",
5148                  (t2STRBi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5149def : t2InstAlias<"strh${p} $Rt, $addr",
5150                  (t2STRHi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
5151
5152def : t2InstAlias<"str${p} $Rt, $addr",
5153                  (t2STRs GPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5154def : t2InstAlias<"strb${p} $Rt, $addr",
5155                  (t2STRBs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5156def : t2InstAlias<"strh${p} $Rt, $addr",
5157                  (t2STRHs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
5158
5159// Extend instruction optional rotate operand.
5160def : InstAlias<"sxtab${p} $Rd, $Rn, $Rm",
5161              (t2SXTAB rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5162              Requires<[HasDSP, IsThumb2]>;
5163def : InstAlias<"sxtah${p} $Rd, $Rn, $Rm",
5164              (t2SXTAH rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5165              Requires<[HasDSP, IsThumb2]>;
5166def : InstAlias<"sxtab16${p} $Rd, $Rn, $Rm",
5167              (t2SXTAB16 rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5168              Requires<[HasDSP, IsThumb2]>;
5169def : InstAlias<"sxtb16${p} $Rd, $Rm",
5170              (t2SXTB16 rGPR:$Rd, rGPR:$Rm, 0, pred:$p), 0>,
5171              Requires<[HasDSP, IsThumb2]>;
5172
5173def : t2InstAlias<"sxtb${p} $Rd, $Rm",
5174                (t2SXTB rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5175def : t2InstAlias<"sxth${p} $Rd, $Rm",
5176                (t2SXTH rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5177def : t2InstAlias<"sxtb${p}.w $Rd, $Rm",
5178                (t2SXTB rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5179def : t2InstAlias<"sxth${p}.w $Rd, $Rm",
5180                (t2SXTH rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5181
5182def : InstAlias<"uxtab${p} $Rd, $Rn, $Rm",
5183              (t2UXTAB rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5184              Requires<[HasDSP, IsThumb2]>;
5185def : InstAlias<"uxtah${p} $Rd, $Rn, $Rm",
5186              (t2UXTAH rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5187              Requires<[HasDSP, IsThumb2]>;
5188def : InstAlias<"uxtab16${p} $Rd, $Rn, $Rm",
5189              (t2UXTAB16 rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p), 0>,
5190              Requires<[HasDSP, IsThumb2]>;
5191def : InstAlias<"uxtb16${p} $Rd, $Rm",
5192              (t2UXTB16 rGPR:$Rd, rGPR:$Rm, 0, pred:$p), 0>,
5193              Requires<[HasDSP, IsThumb2]>;
5194
5195def : t2InstAlias<"uxtb${p} $Rd, $Rm",
5196                (t2UXTB rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5197def : t2InstAlias<"uxth${p} $Rd, $Rm",
5198                (t2UXTH rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5199def : t2InstAlias<"uxtb${p}.w $Rd, $Rm",
5200                (t2UXTB rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5201def : t2InstAlias<"uxth${p}.w $Rd, $Rm",
5202                (t2UXTH rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
5203
5204// Extend instruction w/o the ".w" optional width specifier.
5205def : t2InstAlias<"uxtb${p} $Rd, $Rm$rot",
5206                  (t2UXTB rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
5207def : InstAlias<"uxtb16${p} $Rd, $Rm$rot",
5208                (t2UXTB16 rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p), 0>,
5209                Requires<[HasDSP, IsThumb2]>;
5210def : t2InstAlias<"uxth${p} $Rd, $Rm$rot",
5211                  (t2UXTH rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
5212
5213def : t2InstAlias<"sxtb${p} $Rd, $Rm$rot",
5214                  (t2SXTB rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
5215def : InstAlias<"sxtb16${p} $Rd, $Rm$rot",
5216                (t2SXTB16 rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p), 0>,
5217                Requires<[HasDSP, IsThumb2]>;
5218def : t2InstAlias<"sxth${p} $Rd, $Rm$rot",
5219                  (t2SXTH rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
5220
5221
5222// "mov Rd, t2_so_imm_not" can be handled via "mvn" in assembly, just like
5223// for isel.
5224def : t2InstSubst<"mov${p} $Rd, $imm",
5225                  (t2MVNi rGPR:$Rd, t2_so_imm_not:$imm, pred:$p, zero_reg)>;
5226def : t2InstSubst<"mvn${s}${p} $Rd, $imm",
5227                  (t2MOVi rGPR:$Rd, t2_so_imm_not:$imm, pred:$p, s_cc_out:$s)>;
5228// Same for AND <--> BIC
5229def : t2InstSubst<"bic${s}${p} $Rd, $Rn, $imm",
5230                  (t2ANDri rGPR:$Rd, rGPR:$Rn, t2_so_imm_not:$imm,
5231                           pred:$p, cc_out:$s)>;
5232def : t2InstSubst<"bic${s}${p} $Rdn, $imm",
5233                  (t2ANDri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_not:$imm,
5234                           pred:$p, cc_out:$s)>;
5235def : t2InstSubst<"bic${s}${p}.w $Rd, $Rn, $imm",
5236                  (t2ANDri rGPR:$Rd, rGPR:$Rn, t2_so_imm_not:$imm,
5237                           pred:$p, cc_out:$s)>;
5238def : t2InstSubst<"bic${s}${p}.w $Rdn, $imm",
5239                  (t2ANDri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_not:$imm,
5240                           pred:$p, cc_out:$s)>;
5241def : t2InstSubst<"and${s}${p} $Rd, $Rn, $imm",
5242                  (t2BICri rGPR:$Rd, rGPR:$Rn, t2_so_imm_not:$imm,
5243                           pred:$p, cc_out:$s)>;
5244def : t2InstSubst<"and${s}${p} $Rdn, $imm",
5245                  (t2BICri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_not:$imm,
5246                           pred:$p, cc_out:$s)>;
5247def : t2InstSubst<"and${s}${p}.w $Rd, $Rn, $imm",
5248                  (t2BICri rGPR:$Rd, rGPR:$Rn, t2_so_imm_not:$imm,
5249                           pred:$p, cc_out:$s)>;
5250def : t2InstSubst<"and${s}${p}.w $Rdn, $imm",
5251                  (t2BICri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_not:$imm,
5252                           pred:$p, cc_out:$s)>;
5253// And ORR <--> ORN
5254def : t2InstSubst<"orn${s}${p} $Rd, $Rn, $imm",
5255                  (t2ORRri rGPR:$Rd, rGPR:$Rn, t2_so_imm_not:$imm,
5256                           pred:$p, cc_out:$s)>;
5257def : t2InstSubst<"orn${s}${p} $Rdn, $imm",
5258                  (t2ORRri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_not:$imm,
5259                           pred:$p, cc_out:$s)>;
5260def : t2InstSubst<"orr${s}${p} $Rd, $Rn, $imm",
5261                  (t2ORNri rGPR:$Rd, rGPR:$Rn, t2_so_imm_not:$imm,
5262                           pred:$p, cc_out:$s)>;
5263def : t2InstSubst<"orr${s}${p} $Rdn, $imm",
5264                  (t2ORNri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm_not:$imm,
5265                           pred:$p, cc_out:$s)>;
5266// Likewise, "add Rd, t2_so_imm_neg" -> sub
5267def : t2InstSubst<"add${s}${p} $Rd, $Rn, $imm",
5268                  (t2SUBri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm_neg:$imm,
5269                           pred:$p, cc_out:$s)>;
5270def : t2InstSubst<"add${s}${p} $Rd, $Rn, $imm",
5271                  (t2SUBspImm GPRsp:$Rd, GPRsp:$Rn, t2_so_imm_neg:$imm,
5272                           pred:$p, cc_out:$s)>;
5273def : t2InstSubst<"add${s}${p} $Rd, $imm",
5274                  (t2SUBri rGPR:$Rd, rGPR:$Rd, t2_so_imm_neg:$imm,
5275                           pred:$p, cc_out:$s)>;
5276def : t2InstSubst<"add${s}${p} $Rd, $imm",
5277                  (t2SUBspImm GPRsp:$Rd, GPRsp:$Rd, t2_so_imm_neg:$imm,
5278                           pred:$p, cc_out:$s)>;
5279// Same for CMP <--> CMN via t2_so_imm_neg
5280def : t2InstSubst<"cmp${p} $Rd, $imm",
5281                  (t2CMNri rGPR:$Rd, t2_so_imm_neg:$imm, pred:$p)>;
5282def : t2InstSubst<"cmn${p} $Rd, $imm",
5283                  (t2CMPri rGPR:$Rd, t2_so_imm_neg:$imm, pred:$p)>;
5284
5285
5286// Wide 'mul' encoding can be specified with only two operands.
5287def : t2InstAlias<"mul${p} $Rn, $Rm",
5288                  (t2MUL rGPR:$Rn, rGPR:$Rm, rGPR:$Rn, pred:$p)>;
5289
5290// "neg" is and alias for "rsb rd, rn, #0"
5291def : t2InstAlias<"neg${s}${p} $Rd, $Rm",
5292                  (t2RSBri rGPR:$Rd, rGPR:$Rm, 0, pred:$p, cc_out:$s)>;
5293
5294// MOV so_reg assembler pseudos. InstAlias isn't expressive enough for
5295// these, unfortunately.
5296// FIXME: LSL #0 in the shift should allow SP to be used as either the
5297// source or destination (but not both).
5298def t2MOVsi: t2AsmPseudo<"mov${p} $Rd, $shift",
5299                         (ins rGPR:$Rd, t2_so_reg:$shift, pred:$p)>;
5300def t2MOVSsi: t2AsmPseudo<"movs${p} $Rd, $shift",
5301                          (ins rGPR:$Rd, t2_so_reg:$shift, pred:$p)>;
5302
5303def t2MOVsr: t2AsmPseudo<"mov${p} $Rd, $shift",
5304                         (ins rGPR:$Rd, so_reg_reg:$shift, pred:$p)>;
5305def t2MOVSsr: t2AsmPseudo<"movs${p} $Rd, $shift",
5306                          (ins rGPR:$Rd, so_reg_reg:$shift, pred:$p)>;
5307
5308// Aliases for the above with the .w qualifier
5309def : t2InstAlias<"mov${p}.w $Rd, $shift",
5310                  (t2MOVsi rGPR:$Rd, t2_so_reg:$shift, pred:$p)>;
5311def : t2InstAlias<"movs${p}.w $Rd, $shift",
5312                  (t2MOVSsi rGPR:$Rd, t2_so_reg:$shift, pred:$p)>;
5313def : t2InstAlias<"mov${p}.w $Rd, $shift",
5314                  (t2MOVsr rGPR:$Rd, so_reg_reg:$shift, pred:$p)>;
5315def : t2InstAlias<"movs${p}.w $Rd, $shift",
5316                  (t2MOVSsr rGPR:$Rd, so_reg_reg:$shift, pred:$p)>;
5317
5318// ADR w/o the .w suffix
5319def : t2InstAlias<"adr${p} $Rd, $addr",
5320                  (t2ADR rGPR:$Rd, t2adrlabel:$addr, pred:$p)>;
5321
5322// LDR(literal) w/ alternate [pc, #imm] syntax.
5323def t2LDRpcrel   : t2AsmPseudo<"ldr${p} $Rt, $addr",
5324                         (ins GPR:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5325def t2LDRBpcrel  : t2AsmPseudo<"ldrb${p} $Rt, $addr",
5326                         (ins GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5327def t2LDRHpcrel  : t2AsmPseudo<"ldrh${p} $Rt, $addr",
5328                         (ins GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5329def t2LDRSBpcrel  : t2AsmPseudo<"ldrsb${p} $Rt, $addr",
5330                         (ins GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5331def t2LDRSHpcrel  : t2AsmPseudo<"ldrsh${p} $Rt, $addr",
5332                         (ins GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5333    // Version w/ the .w suffix.
5334def : t2InstAlias<"ldr${p}.w $Rt, $addr",
5335                  (t2LDRpcrel GPR:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p), 0>;
5336def : t2InstAlias<"ldrb${p}.w $Rt, $addr",
5337                  (t2LDRBpcrel GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5338def : t2InstAlias<"ldrh${p}.w $Rt, $addr",
5339                  (t2LDRHpcrel GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5340def : t2InstAlias<"ldrsb${p}.w $Rt, $addr",
5341                  (t2LDRSBpcrel GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5342def : t2InstAlias<"ldrsh${p}.w $Rt, $addr",
5343                  (t2LDRSHpcrel GPRnopc:$Rt, t2ldr_pcrel_imm12:$addr, pred:$p)>;
5344
5345def : t2InstAlias<"add${p} $Rd, pc, $imm",
5346                  (t2ADR rGPR:$Rd, imm0_4095:$imm, pred:$p)>;
5347
5348// Pseudo instruction ldr Rt, =immediate
5349def t2LDRConstPool
5350  : t2AsmPseudo<"ldr${p} $Rt, $immediate",
5351                (ins GPR:$Rt, const_pool_asm_imm:$immediate, pred:$p)>;
5352// Version w/ the .w suffix.
5353def : t2InstAlias<"ldr${p}.w $Rt, $immediate",
5354                  (t2LDRConstPool GPRnopc:$Rt,
5355                  const_pool_asm_imm:$immediate, pred:$p)>;
5356
5357//===----------------------------------------------------------------------===//
5358// ARMv8.1m instructions
5359//
5360
5361class V8_1MI<dag oops, dag iops, AddrMode am, InstrItinClass itin, string asm,
5362             string ops, string cstr, list<dag> pattern>
5363  : Thumb2XI<oops, iops, am, 4, itin, !strconcat(asm, "\t", ops), cstr,
5364             pattern>,
5365    Requires<[HasV8_1MMainline]>;
5366
5367def t2CLRM : V8_1MI<(outs),
5368                    (ins pred:$p, reglist_with_apsr:$regs, variable_ops),
5369                    AddrModeNone, NoItinerary, "clrm${p}", "$regs", "", []> {
5370  bits<16> regs;
5371
5372  let Inst{31-16} = 0b1110100010011111;
5373  let Inst{15-14} = regs{15-14};
5374  let Inst{13} = 0b0;
5375  let Inst{12-0} = regs{12-0};
5376}
5377
5378class t2BF<dag iops, string asm, string ops>
5379  : V8_1MI<(outs ), iops, AddrModeNone, NoItinerary, asm, ops, "", []> {
5380
5381  let Inst{31-27} = 0b11110;
5382  let Inst{15-14} = 0b11;
5383  let Inst{12} = 0b0;
5384  let Inst{0} = 0b1;
5385
5386  let Predicates = [IsThumb2, HasV8_1MMainline, HasLOB];
5387}
5388
5389def t2BF_LabelPseudo
5390  : t2PseudoInst<(outs ), (ins pclabel:$cp), 0, NoItinerary, []> {
5391  let isTerminator = 1;
5392  let Predicates = [IsThumb2, HasV8_1MMainline, HasLOB];
5393  let hasNoSchedulingInfo = 1;
5394}
5395
5396def t2BFi : t2BF<(ins bflabel_u4:$b_label, bflabel_s16:$label, pred:$p),
5397                 !strconcat("bf", "${p}"), "$b_label, $label"> {
5398  bits<4> b_label;
5399  bits<16> label;
5400
5401  let Inst{26-23} = b_label{3-0};
5402  let Inst{22-21} = 0b10;
5403  let Inst{20-16} = label{15-11};
5404  let Inst{13} = 0b1;
5405  let Inst{11} = label{0};
5406  let Inst{10-1} = label{10-1};
5407}
5408
5409def t2BFic : t2BF<(ins bflabel_u4:$b_label, bflabel_s12:$label,
5410                   bfafter_target:$ba_label, pred_noal:$bcond), "bfcsel",
5411                  "$b_label, $label, $ba_label, $bcond"> {
5412  bits<4> bcond;
5413  bits<12> label;
5414  bits<1> ba_label;
5415  bits<4> b_label;
5416
5417  let Inst{26-23} = b_label{3-0};
5418  let Inst{22} = 0b0;
5419  let Inst{21-18} = bcond{3-0};
5420  let Inst{17} = ba_label{0};
5421  let Inst{16} = label{11};
5422  let Inst{13} = 0b1;
5423  let Inst{11} = label{0};
5424  let Inst{10-1} = label{10-1};
5425}
5426
5427def t2BFr : t2BF<(ins bflabel_u4:$b_label, rGPR:$Rn, pred:$p),
5428                 !strconcat("bfx", "${p}"), "$b_label, $Rn"> {
5429  bits<4> b_label;
5430  bits<4> Rn;
5431
5432  let Inst{26-23} = b_label{3-0};
5433  let Inst{22-20} = 0b110;
5434  let Inst{19-16} = Rn{3-0};
5435  let Inst{13-1} = 0b1000000000000;
5436}
5437
5438def t2BFLi : t2BF<(ins bflabel_u4:$b_label, bflabel_s18:$label, pred:$p),
5439                  !strconcat("bfl", "${p}"), "$b_label, $label"> {
5440  bits<4> b_label;
5441  bits<18> label;
5442
5443  let Inst{26-23} = b_label{3-0};
5444  let Inst{22-16} = label{17-11};
5445  let Inst{13} = 0b0;
5446  let Inst{11} = label{0};
5447  let Inst{10-1} = label{10-1};
5448}
5449
5450def t2BFLr : t2BF<(ins bflabel_u4:$b_label, rGPR:$Rn, pred:$p),
5451                  !strconcat("bflx", "${p}"), "$b_label, $Rn"> {
5452  bits<4> b_label;
5453  bits<4> Rn;
5454
5455  let Inst{26-23} = b_label{3-0};
5456  let Inst{22-20} = 0b111;
5457  let Inst{19-16} = Rn{3-0};
5458  let Inst{13-1} = 0b1000000000000;
5459}
5460
5461class t2LOL<dag oops, dag iops, string asm, string ops>
5462  : V8_1MI<oops, iops, AddrModeNone, NoItinerary, asm, ops, "", [] > {
5463  let Inst{31-23} = 0b111100000;
5464  let Inst{15-14} = 0b11;
5465  let Inst{0} = 0b1;
5466  let DecoderMethod = "DecodeLOLoop";
5467  let Predicates = [IsThumb2, HasV8_1MMainline, HasLOB];
5468}
5469
5470let isNotDuplicable = 1 in {
5471def t2WLS : t2LOL<(outs GPRlr:$LR),
5472                  (ins rGPR:$Rn, wlslabel_u11:$label),
5473                  "wls", "$LR, $Rn, $label"> {
5474  bits<4> Rn;
5475  bits<11> label;
5476  let Inst{22-20} = 0b100;
5477  let Inst{19-16} = Rn{3-0};
5478  let Inst{13-12} = 0b00;
5479  let Inst{11} = label{0};
5480  let Inst{10-1} = label{10-1};
5481  let usesCustomInserter = 1;
5482  let isBranch = 1;
5483  let isTerminator = 1;
5484}
5485
5486def t2DLS : t2LOL<(outs GPRlr:$LR), (ins rGPR:$Rn),
5487                  "dls", "$LR, $Rn"> {
5488  bits<4> Rn;
5489  let Inst{22-20} = 0b100;
5490  let Inst{19-16} = Rn{3-0};
5491  let Inst{13-1} = 0b1000000000000;
5492  let usesCustomInserter = 1;
5493}
5494
5495def t2LEUpdate : t2LOL<(outs GPRlr:$LRout),
5496                       (ins GPRlr:$LRin, lelabel_u11:$label),
5497                       "le", "$LRin, $label"> {
5498  bits<11> label;
5499  let Inst{22-16} = 0b0001111;
5500  let Inst{13-12} = 0b00;
5501  let Inst{11} = label{0};
5502  let Inst{10-1} = label{10-1};
5503  let usesCustomInserter = 1;
5504  let isBranch = 1;
5505  let isTerminator = 1;
5506}
5507
5508def t2LE : t2LOL<(outs ), (ins lelabel_u11:$label), "le", "$label"> {
5509  bits<11> label;
5510  let Inst{22-16} = 0b0101111;
5511  let Inst{13-12} = 0b00;
5512  let Inst{11} = label{0};
5513  let Inst{10-1} = label{10-1};
5514  let isBranch = 1;
5515  let isTerminator = 1;
5516}
5517
5518let Predicates = [IsThumb2, HasV8_1MMainline, HasLOB] in {
5519
5520// t2DoLoopStart a pseudo for DLS hardware loops. Lowered into a DLS in
5521// ARMLowOverheadLoops if possible, or reverted to a Mov if not.
5522def t2DoLoopStart :
5523  t2PseudoInst<(outs GPRlr:$X), (ins rGPR:$tc), 4, IIC_Br,
5524  [(set GPRlr:$X, (int_start_loop_iterations rGPR:$tc))]>;
5525
5526// A pseudo for a DLSTP, created in the MVETPAndVPTOptimizationPass from a
5527// t2DoLoopStart if the loops is tail predicated. Holds both the element
5528// count and trip count of the loop, picking the correct one during
5529// ARMLowOverheadLoops when it is converted to a DLSTP or DLS as required.
5530let isTerminator = 1, hasSideEffects = 1 in
5531def t2DoLoopStartTP :
5532  t2PseudoInst<(outs GPRlr:$X), (ins rGPR:$tc, rGPR:$elts), 4, IIC_Br, []>;
5533
5534// Setup for a t2WhileLoopStart. A pair of t2WhileLoopSetup and t2WhileLoopStart
5535// will be created post-ISel from a llvm.test.start.loop.iterations. This
5536// t2WhileLoopSetup to setup LR and t2WhileLoopStart to perform the branch. Not
5537// valid after reg alloc, as it should be lowered during MVETPAndVPTOptimisations
5538// into a t2WhileLoopStartLR (or expanded).
5539def t2WhileLoopSetup :
5540  t2PseudoInst<(outs GPRlr:$lr), (ins rGPR:$tc), 4, IIC_Br, []>;
5541
5542// A pseudo to represent the decrement in a low overhead loop. A t2LoopDec and
5543// t2LoopEnd together represent a LE instruction. Ideally these are converted
5544// to a t2LoopEndDec which is lowered as a single instruction.
5545let hasSideEffects = 0 in
5546def t2LoopDec :
5547  t2PseudoInst<(outs GPRlr:$Rm), (ins GPRlr:$Rn, imm0_7:$size),
5548               4, IIC_Br, []>, Sched<[WriteBr]>;
5549
5550let isBranch = 1, isTerminator = 1, hasSideEffects = 1, Defs = [CPSR] in {
5551// The branch in a t2WhileLoopSetup/t2WhileLoopStart pair, eventually turned
5552// into a t2WhileLoopStartLR that does both the LR setup and branch.
5553def t2WhileLoopStart :
5554    t2PseudoInst<(outs),
5555                 (ins GPRlr:$tc, brtarget:$target),
5556                 4, IIC_Br, []>,
5557                 Sched<[WriteBr]>;
5558
5559// WhileLoopStartLR that sets up LR and branches on zero, equivalent to WLS. It
5560// is lowered in the ARMLowOverheadLoops pass providing the branches are within
5561// range. WhileLoopStartLR and LoopEnd to occupy 8 bytes because they may get
5562// converted into t2CMP and t2Bcc.
5563def t2WhileLoopStartLR :
5564    t2PseudoInst<(outs GPRlr:$lr),
5565                 (ins rGPR:$tc, brtarget:$target),
5566                 8, IIC_Br, []>,
5567                 Sched<[WriteBr]>;
5568
5569// Similar to a t2DoLoopStartTP, a t2WhileLoopStartTP is a pseudo for a WLSTP
5570// holding both the element count and the tripcount of the loop.
5571def t2WhileLoopStartTP :
5572    t2PseudoInst<(outs GPRlr:$lr),
5573                 (ins rGPR:$tc, rGPR:$elts, brtarget:$target),
5574                 8, IIC_Br, []>,
5575                 Sched<[WriteBr]>;
5576
5577// t2LoopEnd - the branch half of a t2LoopDec/t2LoopEnd pair.
5578def t2LoopEnd :
5579  t2PseudoInst<(outs), (ins GPRlr:$tc, brtarget:$target),
5580  8, IIC_Br, []>, Sched<[WriteBr]>;
5581
5582// The combination of a t2LoopDec and t2LoopEnd, performing both the LR
5583// decrement and branch as a single instruction. Is lowered to a LE or
5584// LETP in ARMLowOverheadLoops as appropriate, or converted to t2CMP/t2Bcc
5585// if the branches are out of range.
5586def t2LoopEndDec :
5587  t2PseudoInst<(outs GPRlr:$Rm), (ins GPRlr:$tc, brtarget:$target),
5588  8, IIC_Br, []>, Sched<[WriteBr]>;
5589
5590} // end isBranch, isTerminator, hasSideEffects
5591
5592}
5593
5594} // end isNotDuplicable
5595
5596class CS<string iname, bits<4> opcode, list<dag> pattern=[]>
5597  : V8_1MI<(outs rGPR:$Rd), (ins GPRwithZRnosp:$Rn, GPRwithZRnosp:$Rm, pred_noal:$fcond),
5598           AddrModeNone, NoItinerary, iname, "$Rd, $Rn, $Rm, $fcond", "", pattern> {
5599  bits<4> Rd;
5600  bits<4> Rm;
5601  bits<4> Rn;
5602  bits<4> fcond;
5603
5604  let Inst{31-20} = 0b111010100101;
5605  let Inst{19-16} = Rn{3-0};
5606  let Inst{15-12} = opcode;
5607  let Inst{11-8} = Rd{3-0};
5608  let Inst{7-4} = fcond{3-0};
5609  let Inst{3-0} = Rm{3-0};
5610
5611  let Uses = [CPSR];
5612  let hasSideEffects = 0;
5613}
5614
5615def t2CSEL  : CS<"csel",  0b1000>;
5616def t2CSINC : CS<"csinc", 0b1001>;
5617def t2CSINV : CS<"csinv", 0b1010>;
5618def t2CSNEG : CS<"csneg", 0b1011>;
5619
5620
5621let Predicates = [HasV8_1MMainline] in {
5622  multiclass CSPats<SDNode Node, Instruction Insn> {
5623    def : T2Pat<(Node GPRwithZR:$tval, GPRwithZR:$fval, imm0_31:$imm),
5624                (Insn GPRwithZR:$tval, GPRwithZR:$fval, imm0_31:$imm)>;
5625    def : T2Pat<(Node (i32 0), GPRwithZR:$fval, imm0_31:$imm),
5626                (Insn ZR, GPRwithZR:$fval, imm0_31:$imm)>;
5627    def : T2Pat<(Node GPRwithZR:$tval, (i32 0), imm0_31:$imm),
5628                (Insn GPRwithZR:$tval, ZR, imm0_31:$imm)>;
5629    def : T2Pat<(Node (i32 0), (i32 0), imm0_31:$imm),
5630                (Insn ZR, ZR, imm0_31:$imm)>;
5631  }
5632
5633  defm : CSPats<ARMcsinc, t2CSINC>;
5634  defm : CSPats<ARMcsinv, t2CSINV>;
5635  defm : CSPats<ARMcsneg, t2CSNEG>;
5636
5637  def : T2Pat<(ARMcmov (i32 1), (i32 0), cmovpred:$imm),
5638              (t2CSINC ZR, ZR, imm0_31:$imm)>;
5639  def : T2Pat<(ARMcmov (i32 -1), (i32 0), cmovpred:$imm),
5640              (t2CSINV ZR, ZR, imm0_31:$imm)>;
5641  def : T2Pat<(ARMcmov (i32 0), (i32 1), cmovpred:$imm),
5642              (t2CSINC ZR, ZR, (inv_cond_XFORM imm:$imm))>;
5643  def : T2Pat<(ARMcmov (i32 0), (i32 -1), cmovpred:$imm),
5644              (t2CSINV ZR, ZR, (inv_cond_XFORM imm:$imm))>;
5645
5646  multiclass ModifiedV8_1CSEL<Instruction Insn, dag modvalue> {
5647    def : T2Pat<(ARMcmov modvalue, GPRwithZR:$tval, cmovpred:$imm),
5648                (Insn GPRwithZR:$tval, GPRwithZR:$fval, imm0_31:$imm)>;
5649    def : T2Pat<(ARMcmov GPRwithZR:$tval, modvalue, cmovpred:$imm),
5650                (Insn GPRwithZR:$tval, GPRwithZR:$fval,
5651                         (i32 (inv_cond_XFORM imm:$imm)))>;
5652  }
5653  defm : ModifiedV8_1CSEL<t2CSINC, (add rGPR:$fval, 1)>;
5654  defm : ModifiedV8_1CSEL<t2CSINV, (xor rGPR:$fval, -1)>;
5655  defm : ModifiedV8_1CSEL<t2CSNEG, (sub 0, rGPR:$fval)>;
5656}
5657
5658// CS aliases.
5659let Predicates = [HasV8_1MMainline] in {
5660  def : InstAlias<"csetm\t$Rd, $fcond",
5661                 (t2CSINV rGPR:$Rd, ZR, ZR, pred_noal_inv:$fcond)>;
5662
5663  def : InstAlias<"cset\t$Rd, $fcond",
5664                 (t2CSINC rGPR:$Rd, ZR, ZR, pred_noal_inv:$fcond)>;
5665
5666  def : InstAlias<"cinc\t$Rd, $Rn, $fcond",
5667                 (t2CSINC rGPR:$Rd, GPRwithZRnosp:$Rn, GPRwithZRnosp:$Rn, pred_noal_inv:$fcond)>;
5668
5669  def : InstAlias<"cinv\t$Rd, $Rn, $fcond",
5670                 (t2CSINV rGPR:$Rd, GPRwithZRnosp:$Rn, GPRwithZRnosp:$Rn, pred_noal_inv:$fcond)>;
5671
5672  def : InstAlias<"cneg\t$Rd, $Rn, $fcond",
5673                 (t2CSNEG rGPR:$Rd, GPRwithZRnosp:$Rn, GPRwithZRnosp:$Rn, pred_noal_inv:$fcond)>;
5674}
5675
5676
5677// PACBTI
5678let Predicates = [IsThumb2, HasV8_1MMainline, HasPACBTI] in {
5679def t2PACG : V8_1MI<(outs rGPR:$Rd),
5680                    (ins pred:$p, GPRnopc:$Rn, GPRnopc:$Rm),
5681                    AddrModeNone, NoItinerary, "pacg${p}", "$Rd, $Rn, $Rm", "", []> {
5682  bits<4> Rd;
5683  bits<4> Rn;
5684  bits<4> Rm;
5685  let Inst{31-20} = 0b111110110110;
5686  let Inst{19-16} = Rn;
5687  let Inst{15-12} = 0b1111;
5688  let Inst{11-8}  = Rd;
5689  let Inst{7-4}   = 0b0000;
5690  let Inst{3-0}   = Rm;
5691}
5692
5693let hasSideEffects = 1 in {
5694class PACBTIAut<dag iops, string asm, bit b>
5695  : V8_1MI<(outs), iops,
5696           AddrModeNone, NoItinerary, asm, "$Ra, $Rn, $Rm", "", []> {
5697  bits<4> Ra;
5698  bits<4> Rn;
5699  bits<4> Rm;
5700  let Inst{31-20} = 0b111110110101;
5701  let Inst{19-16} = Rn;
5702  let Inst{15-12} = Ra;
5703  let Inst{11-5}  = 0b1111000;
5704  let Inst{4}     = b;
5705  let Inst{3-0}   = Rm;
5706}
5707}
5708
5709def t2AUTG  : PACBTIAut<(ins pred:$p, GPRnosp:$Ra, GPRnopc:$Rn, GPRnopc:$Rm),
5710                        "autg${p}", 0>;
5711
5712let isBranch = 1, isTerminator = 1, isIndirectBranch = 1 in {
5713  def t2BXAUT : PACBTIAut<(ins pred:$p, GPRnosp:$Ra, rGPR:$Rn, GPRnopc:$Rm),
5714                          "bxaut${p}", 1>;
5715}
5716}
5717
5718
5719class PACBTIHintSpaceInst<string asm, string ops, bits<8> imm>
5720  : V8_1MI<(outs), (ins), AddrModeNone, NoItinerary, asm, ops, "", []> {
5721  let Inst{31-8} = 0b111100111010111110000000;
5722  let Inst{7-0}  = imm;
5723
5724  let Unpredictable{19-16} = 0b1111;
5725  let Unpredictable{13-11} = 0b101;
5726
5727  let DecoderMethod = "DecodeT2HintSpaceInstruction";
5728}
5729
5730class PACBTIHintSpaceNoOpsInst<string asm, bits<8> imm>
5731  : PACBTIHintSpaceInst<asm, "", imm>;
5732
5733class PACBTIHintSpaceDefInst<string asm, bits<8> imm>
5734  : PACBTIHintSpaceInst<asm, "r12, lr, sp", imm> {
5735  let Defs = [R12];
5736  let Uses = [LR, SP];
5737}
5738
5739class PACBTIHintSpaceUseInst<string asm, bits<8> imm>
5740  : PACBTIHintSpaceInst<asm, "r12, lr, sp", imm> {
5741  let Uses = [R12, LR, SP];
5742}
5743
5744def t2PAC    : PACBTIHintSpaceDefInst<"pac", 0b00011101>;
5745def t2PACBTI : PACBTIHintSpaceDefInst<"pacbti", 0b00001101>;
5746def t2BTI    : PACBTIHintSpaceNoOpsInst<"bti", 0b00001111>;
5747def t2AUT    : PACBTIHintSpaceUseInst<"aut", 0b00101101> {
5748  let hasSideEffects = 1;
5749}
5750
5751def ARMt2CallBTI : SDNode<"ARMISD::t2CALL_BTI", SDT_ARMcall,
5752                   [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue, SDNPVariadic]>;
5753
5754def t2CALL_BTI : PseudoInst<(outs), (ins pred:$p, thumb_bl_target:$func),
5755                 IIC_Br, [(ARMt2CallBTI tglobaladdr:$func)]>,
5756                 Requires<[IsThumb2]>, Sched<[WriteBrL]>;
5757