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