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