ARMInstrThumb2.td revision bc80e94865d139a60534ac40cbf12f2d214dad56
1//===- ARMInstrThumb2.td - Thumb2 support for ARM -------------------------===//
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// Shifted operands. No register controlled shifts for Thumb2.
32// Note: We do not support rrx shifted operands yet.
33def t2_so_reg : Operand<i32>,    // reg imm
34                ComplexPattern<i32, 2, "SelectT2ShifterOperandReg",
35                               [shl,srl,sra,rotr]> {
36  let EncoderMethod = "getT2SORegOpValue";
37  let PrintMethod = "printT2SOOperand";
38  let DecoderMethod = "DecodeSORegImmOperand";
39  let ParserMatchClass = ShiftedImmAsmOperand;
40  let MIOperandInfo = (ops rGPR, i32imm);
41}
42
43// t2_so_imm_not_XFORM - Return the complement of a t2_so_imm value
44def t2_so_imm_not_XFORM : SDNodeXForm<imm, [{
45  return CurDAG->getTargetConstant(~((uint32_t)N->getZExtValue()), MVT::i32);
46}]>;
47
48// t2_so_imm_neg_XFORM - Return the negation of a t2_so_imm value
49def t2_so_imm_neg_XFORM : SDNodeXForm<imm, [{
50  return CurDAG->getTargetConstant(-((int)N->getZExtValue()), MVT::i32);
51}]>;
52
53// t2_so_imm - Match a 32-bit immediate operand, which is an
54// 8-bit immediate rotated by an arbitrary number of bits, or an 8-bit
55// immediate splatted into multiple bytes of the word.
56def t2_so_imm_asmoperand : AsmOperandClass { let Name = "T2SOImm"; }
57def t2_so_imm : Operand<i32>, ImmLeaf<i32, [{
58    return ARM_AM::getT2SOImmVal(Imm) != -1;
59  }]> {
60  let ParserMatchClass = t2_so_imm_asmoperand;
61  let EncoderMethod = "getT2SOImmOpValue";
62  let DecoderMethod = "DecodeT2SOImm";
63}
64
65// t2_so_imm_not - Match an immediate that is a complement
66// of a t2_so_imm.
67def t2_so_imm_not : Operand<i32>,
68                    PatLeaf<(imm), [{
69  return ARM_AM::getT2SOImmVal(~((uint32_t)N->getZExtValue())) != -1;
70}], t2_so_imm_not_XFORM>;
71
72// t2_so_imm_neg - Match an immediate that is a negation of a t2_so_imm.
73def t2_so_imm_neg : Operand<i32>,
74                    PatLeaf<(imm), [{
75  return ARM_AM::getT2SOImmVal(-((uint32_t)N->getZExtValue())) != -1;
76}], t2_so_imm_neg_XFORM>;
77
78/// imm0_4095 predicate - True if the 32-bit immediate is in the range [0.4095].
79def imm0_4095 : Operand<i32>,
80                ImmLeaf<i32, [{
81  return Imm >= 0 && Imm < 4096;
82}]>;
83
84def imm0_4095_neg : PatLeaf<(i32 imm), [{
85 return (uint32_t)(-N->getZExtValue()) < 4096;
86}], imm_neg_XFORM>;
87
88def imm0_255_neg : PatLeaf<(i32 imm), [{
89  return (uint32_t)(-N->getZExtValue()) < 255;
90}], imm_neg_XFORM>;
91
92def imm0_255_not : PatLeaf<(i32 imm), [{
93  return (uint32_t)(~N->getZExtValue()) < 255;
94}], imm_comp_XFORM>;
95
96def lo5AllOne : PatLeaf<(i32 imm), [{
97  // Returns true if all low 5-bits are 1.
98  return (((uint32_t)N->getZExtValue()) & 0x1FUL) == 0x1FUL;
99}]>;
100
101// Define Thumb2 specific addressing modes.
102
103// t2addrmode_imm12  := reg + imm12
104def t2addrmode_imm12_asmoperand : AsmOperandClass {let Name="MemUImm12Offset";}
105def t2addrmode_imm12 : Operand<i32>,
106                       ComplexPattern<i32, 2, "SelectT2AddrModeImm12", []> {
107  let PrintMethod = "printAddrModeImm12Operand";
108  let EncoderMethod = "getAddrModeImm12OpValue";
109  let DecoderMethod = "DecodeT2AddrModeImm12";
110  let ParserMatchClass = t2addrmode_imm12_asmoperand;
111  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
112}
113
114// t2ldrlabel  := imm12
115def t2ldrlabel : Operand<i32> {
116  let EncoderMethod = "getAddrModeImm12OpValue";
117}
118
119
120// ADR instruction labels.
121def t2adrlabel : Operand<i32> {
122  let EncoderMethod = "getT2AdrLabelOpValue";
123}
124
125
126// t2addrmode_posimm8  := reg + imm8
127def MemPosImm8OffsetAsmOperand : AsmOperandClass {let Name="MemPosImm8Offset";}
128def t2addrmode_posimm8 : Operand<i32> {
129  let PrintMethod = "printT2AddrModeImm8Operand";
130  let EncoderMethod = "getT2AddrModeImm8OpValue";
131  let DecoderMethod = "DecodeT2AddrModeImm8";
132  let ParserMatchClass = MemPosImm8OffsetAsmOperand;
133  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
134}
135
136// t2addrmode_negimm8  := reg - imm8
137def MemNegImm8OffsetAsmOperand : AsmOperandClass {let Name="MemNegImm8Offset";}
138def t2addrmode_negimm8 : Operand<i32>,
139                      ComplexPattern<i32, 2, "SelectT2AddrModeImm8", []> {
140  let PrintMethod = "printT2AddrModeImm8Operand";
141  let EncoderMethod = "getT2AddrModeImm8OpValue";
142  let DecoderMethod = "DecodeT2AddrModeImm8";
143  let ParserMatchClass = MemNegImm8OffsetAsmOperand;
144  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
145}
146
147// t2addrmode_imm8  := reg +/- imm8
148def MemImm8OffsetAsmOperand : AsmOperandClass { let Name = "MemImm8Offset"; }
149def t2addrmode_imm8 : Operand<i32>,
150                      ComplexPattern<i32, 2, "SelectT2AddrModeImm8", []> {
151  let PrintMethod = "printT2AddrModeImm8Operand";
152  let EncoderMethod = "getT2AddrModeImm8OpValue";
153  let DecoderMethod = "DecodeT2AddrModeImm8";
154  let ParserMatchClass = MemImm8OffsetAsmOperand;
155  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
156}
157
158def t2am_imm8_offset : Operand<i32>,
159                       ComplexPattern<i32, 1, "SelectT2AddrModeImm8Offset",
160                                      [], [SDNPWantRoot]> {
161  let PrintMethod = "printT2AddrModeImm8OffsetOperand";
162  let EncoderMethod = "getT2AddrModeImm8OffsetOpValue";
163  let DecoderMethod = "DecodeT2Imm8";
164}
165
166// t2addrmode_imm8s4  := reg +/- (imm8 << 2)
167def MemImm8s4OffsetAsmOperand : AsmOperandClass {let Name = "MemImm8s4Offset";}
168def t2addrmode_imm8s4 : Operand<i32> {
169  let PrintMethod = "printT2AddrModeImm8s4Operand";
170  let EncoderMethod = "getT2AddrModeImm8s4OpValue";
171  let DecoderMethod = "DecodeT2AddrModeImm8s4";
172  let ParserMatchClass = MemImm8s4OffsetAsmOperand;
173  let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
174}
175
176def t2am_imm8s4_offset_asmoperand : AsmOperandClass { let Name = "Imm8s4"; }
177def t2am_imm8s4_offset : Operand<i32> {
178  let PrintMethod = "printT2AddrModeImm8s4OffsetOperand";
179  let EncoderMethod = "getT2Imm8s4OpValue";
180  let DecoderMethod = "DecodeT2Imm8S4";
181}
182
183// t2addrmode_imm0_1020s4  := reg + (imm8 << 2)
184def MemImm0_1020s4OffsetAsmOperand : AsmOperandClass {
185  let Name = "MemImm0_1020s4Offset";
186}
187def t2addrmode_imm0_1020s4 : Operand<i32> {
188  let PrintMethod = "printT2AddrModeImm0_1020s4Operand";
189  let EncoderMethod = "getT2AddrModeImm0_1020s4OpValue";
190  let DecoderMethod = "DecodeT2AddrModeImm0_1020s4";
191  let ParserMatchClass = MemImm0_1020s4OffsetAsmOperand;
192  let MIOperandInfo = (ops GPRnopc:$base, i32imm:$offsimm);
193}
194
195// t2addrmode_so_reg  := reg + (reg << imm2)
196def t2addrmode_so_reg_asmoperand : AsmOperandClass {let Name="T2MemRegOffset";}
197def t2addrmode_so_reg : Operand<i32>,
198                        ComplexPattern<i32, 3, "SelectT2AddrModeSoReg", []> {
199  let PrintMethod = "printT2AddrModeSoRegOperand";
200  let EncoderMethod = "getT2AddrModeSORegOpValue";
201  let DecoderMethod = "DecodeT2AddrModeSOReg";
202  let ParserMatchClass = t2addrmode_so_reg_asmoperand;
203  let MIOperandInfo = (ops GPR:$base, rGPR:$offsreg, i32imm:$offsimm);
204}
205
206//===----------------------------------------------------------------------===//
207// Multiclass helpers...
208//
209
210
211class T2OneRegImm<dag oops, dag iops, InstrItinClass itin,
212           string opc, string asm, list<dag> pattern>
213  : T2I<oops, iops, itin, opc, asm, pattern> {
214  bits<4> Rd;
215  bits<12> imm;
216
217  let Inst{11-8}  = Rd;
218  let Inst{26}    = imm{11};
219  let Inst{14-12} = imm{10-8};
220  let Inst{7-0}   = imm{7-0};
221}
222
223
224class T2sOneRegImm<dag oops, dag iops, InstrItinClass itin,
225           string opc, string asm, list<dag> pattern>
226  : T2sI<oops, iops, itin, opc, asm, pattern> {
227  bits<4> Rd;
228  bits<4> Rn;
229  bits<12> imm;
230
231  let Inst{11-8}  = Rd;
232  let Inst{26}    = imm{11};
233  let Inst{14-12} = imm{10-8};
234  let Inst{7-0}   = imm{7-0};
235}
236
237class T2OneRegCmpImm<dag oops, dag iops, InstrItinClass itin,
238           string opc, string asm, list<dag> pattern>
239  : T2I<oops, iops, itin, opc, asm, pattern> {
240  bits<4> Rn;
241  bits<12> imm;
242
243  let Inst{19-16}  = Rn;
244  let Inst{26}    = imm{11};
245  let Inst{14-12} = imm{10-8};
246  let Inst{7-0}   = imm{7-0};
247}
248
249
250class T2OneRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
251           string opc, string asm, list<dag> pattern>
252  : T2I<oops, iops, itin, opc, asm, pattern> {
253  bits<4> Rd;
254  bits<12> ShiftedRm;
255
256  let Inst{11-8}  = Rd;
257  let Inst{3-0}   = ShiftedRm{3-0};
258  let Inst{5-4}   = ShiftedRm{6-5};
259  let Inst{14-12} = ShiftedRm{11-9};
260  let Inst{7-6}   = ShiftedRm{8-7};
261}
262
263class T2sOneRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
264           string opc, string asm, list<dag> pattern>
265  : T2sI<oops, iops, itin, opc, asm, pattern> {
266  bits<4> Rd;
267  bits<12> ShiftedRm;
268
269  let Inst{11-8}  = Rd;
270  let Inst{3-0}   = ShiftedRm{3-0};
271  let Inst{5-4}   = ShiftedRm{6-5};
272  let Inst{14-12} = ShiftedRm{11-9};
273  let Inst{7-6}   = ShiftedRm{8-7};
274}
275
276class T2OneRegCmpShiftedReg<dag oops, dag iops, InstrItinClass itin,
277           string opc, string asm, list<dag> pattern>
278  : T2I<oops, iops, itin, opc, asm, pattern> {
279  bits<4> Rn;
280  bits<12> ShiftedRm;
281
282  let Inst{19-16} = Rn;
283  let Inst{3-0}   = ShiftedRm{3-0};
284  let Inst{5-4}   = ShiftedRm{6-5};
285  let Inst{14-12} = ShiftedRm{11-9};
286  let Inst{7-6}   = ShiftedRm{8-7};
287}
288
289class T2TwoReg<dag oops, dag iops, InstrItinClass itin,
290           string opc, string asm, list<dag> pattern>
291  : T2I<oops, iops, itin, opc, asm, pattern> {
292  bits<4> Rd;
293  bits<4> Rm;
294
295  let Inst{11-8}  = Rd;
296  let Inst{3-0}   = Rm;
297}
298
299class T2sTwoReg<dag oops, dag iops, InstrItinClass itin,
300           string opc, string asm, list<dag> pattern>
301  : T2sI<oops, iops, itin, opc, asm, pattern> {
302  bits<4> Rd;
303  bits<4> Rm;
304
305  let Inst{11-8}  = Rd;
306  let Inst{3-0}   = Rm;
307}
308
309class T2TwoRegCmp<dag oops, dag iops, InstrItinClass itin,
310           string opc, string asm, list<dag> pattern>
311  : T2I<oops, iops, itin, opc, asm, pattern> {
312  bits<4> Rn;
313  bits<4> Rm;
314
315  let Inst{19-16} = Rn;
316  let Inst{3-0}   = Rm;
317}
318
319
320class T2TwoRegImm<dag oops, dag iops, InstrItinClass itin,
321           string opc, string asm, list<dag> pattern>
322  : T2I<oops, iops, itin, opc, asm, pattern> {
323  bits<4> Rd;
324  bits<4> Rn;
325  bits<12> imm;
326
327  let Inst{11-8}  = Rd;
328  let Inst{19-16} = Rn;
329  let Inst{26}    = imm{11};
330  let Inst{14-12} = imm{10-8};
331  let Inst{7-0}   = imm{7-0};
332}
333
334class T2sTwoRegImm<dag oops, dag iops, InstrItinClass itin,
335           string opc, string asm, list<dag> pattern>
336  : T2sI<oops, iops, itin, opc, asm, pattern> {
337  bits<4> Rd;
338  bits<4> Rn;
339  bits<12> imm;
340
341  let Inst{11-8}  = Rd;
342  let Inst{19-16} = Rn;
343  let Inst{26}    = imm{11};
344  let Inst{14-12} = imm{10-8};
345  let Inst{7-0}   = imm{7-0};
346}
347
348class T2TwoRegShiftImm<dag oops, dag iops, InstrItinClass itin,
349           string opc, string asm, list<dag> pattern>
350  : T2I<oops, iops, itin, opc, asm, pattern> {
351  bits<4> Rd;
352  bits<4> Rm;
353  bits<5> imm;
354
355  let Inst{11-8}  = Rd;
356  let Inst{3-0}   = Rm;
357  let Inst{14-12} = imm{4-2};
358  let Inst{7-6}   = imm{1-0};
359}
360
361class T2sTwoRegShiftImm<dag oops, dag iops, InstrItinClass itin,
362           string opc, string asm, list<dag> pattern>
363  : T2sI<oops, iops, itin, opc, asm, pattern> {
364  bits<4> Rd;
365  bits<4> Rm;
366  bits<5> imm;
367
368  let Inst{11-8}  = Rd;
369  let Inst{3-0}   = Rm;
370  let Inst{14-12} = imm{4-2};
371  let Inst{7-6}   = imm{1-0};
372}
373
374class T2ThreeReg<dag oops, dag iops, InstrItinClass itin,
375           string opc, string asm, list<dag> pattern>
376  : T2I<oops, iops, itin, opc, asm, pattern> {
377  bits<4> Rd;
378  bits<4> Rn;
379  bits<4> Rm;
380
381  let Inst{11-8}  = Rd;
382  let Inst{19-16} = Rn;
383  let Inst{3-0}   = Rm;
384}
385
386class T2sThreeReg<dag oops, dag iops, InstrItinClass itin,
387           string opc, string asm, list<dag> pattern>
388  : T2sI<oops, iops, itin, opc, asm, pattern> {
389  bits<4> Rd;
390  bits<4> Rn;
391  bits<4> Rm;
392
393  let Inst{11-8}  = Rd;
394  let Inst{19-16} = Rn;
395  let Inst{3-0}   = Rm;
396}
397
398class T2TwoRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
399           string opc, string asm, list<dag> pattern>
400  : T2I<oops, iops, itin, opc, asm, pattern> {
401  bits<4> Rd;
402  bits<4> Rn;
403  bits<12> ShiftedRm;
404
405  let Inst{11-8}  = Rd;
406  let Inst{19-16} = Rn;
407  let Inst{3-0}   = ShiftedRm{3-0};
408  let Inst{5-4}   = ShiftedRm{6-5};
409  let Inst{14-12} = ShiftedRm{11-9};
410  let Inst{7-6}   = ShiftedRm{8-7};
411}
412
413class T2sTwoRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
414           string opc, string asm, list<dag> pattern>
415  : T2sI<oops, iops, itin, opc, asm, pattern> {
416  bits<4> Rd;
417  bits<4> Rn;
418  bits<12> ShiftedRm;
419
420  let Inst{11-8}  = Rd;
421  let Inst{19-16} = Rn;
422  let Inst{3-0}   = ShiftedRm{3-0};
423  let Inst{5-4}   = ShiftedRm{6-5};
424  let Inst{14-12} = ShiftedRm{11-9};
425  let Inst{7-6}   = ShiftedRm{8-7};
426}
427
428class T2FourReg<dag oops, dag iops, InstrItinClass itin,
429           string opc, string asm, list<dag> pattern>
430  : T2I<oops, iops, itin, opc, asm, pattern> {
431  bits<4> Rd;
432  bits<4> Rn;
433  bits<4> Rm;
434  bits<4> Ra;
435
436  let Inst{19-16} = Rn;
437  let Inst{15-12} = Ra;
438  let Inst{11-8}  = Rd;
439  let Inst{3-0}   = Rm;
440}
441
442class T2MulLong<bits<3> opc22_20, bits<4> opc7_4,
443                dag oops, dag iops, InstrItinClass itin,
444                string opc, string asm, list<dag> pattern>
445  : T2I<oops, iops, itin, opc, asm, pattern> {
446  bits<4> RdLo;
447  bits<4> RdHi;
448  bits<4> Rn;
449  bits<4> Rm;
450
451  let Inst{31-23} = 0b111110111;
452  let Inst{22-20} = opc22_20;
453  let Inst{19-16} = Rn;
454  let Inst{15-12} = RdLo;
455  let Inst{11-8}  = RdHi;
456  let Inst{7-4}   = opc7_4;
457  let Inst{3-0}   = Rm;
458}
459
460
461/// T2I_bin_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns for a
462/// binary operation that produces a value. These are predicable and can be
463/// changed to modify CPSR.
464multiclass T2I_bin_irs<bits<4> opcod, string opc,
465                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
466                       PatFrag opnode, string baseOpc, bit Commutable = 0,
467                       string wide = ""> {
468   // shifted imm
469   def ri : T2sTwoRegImm<
470                (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm), iii,
471                 opc, "\t$Rd, $Rn, $imm",
472                 [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_imm:$imm))]> {
473     let Inst{31-27} = 0b11110;
474     let Inst{25} = 0;
475     let Inst{24-21} = opcod;
476     let Inst{15} = 0;
477   }
478   // register
479   def rr : T2sThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), iir,
480                 opc, !strconcat(wide, "\t$Rd, $Rn, $Rm"),
481                 [(set rGPR:$Rd, (opnode rGPR:$Rn, rGPR:$Rm))]> {
482     let isCommutable = Commutable;
483     let Inst{31-27} = 0b11101;
484     let Inst{26-25} = 0b01;
485     let Inst{24-21} = opcod;
486     let Inst{14-12} = 0b000; // imm3
487     let Inst{7-6} = 0b00; // imm2
488     let Inst{5-4} = 0b00; // type
489   }
490   // shifted register
491   def rs : T2sTwoRegShiftedReg<
492                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm), iis,
493                 opc, !strconcat(wide, "\t$Rd, $Rn, $ShiftedRm"),
494                 [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_reg:$ShiftedRm))]> {
495     let Inst{31-27} = 0b11101;
496     let Inst{26-25} = 0b01;
497     let Inst{24-21} = opcod;
498   }
499  // Assembly aliases for optional destination operand when it's the same
500  // as the source operand.
501  def : t2InstAlias<!strconcat(opc, "${s}${p} $Rdn, $imm"),
502     (!cast<Instruction>(!strconcat(baseOpc, "ri")) rGPR:$Rdn, rGPR:$Rdn,
503                                                    t2_so_imm:$imm, pred:$p,
504                                                    cc_out:$s)>;
505  def : t2InstAlias<!strconcat(opc, "${s}${p}", wide, " $Rdn, $Rm"),
506     (!cast<Instruction>(!strconcat(baseOpc, "rr")) rGPR:$Rdn, rGPR:$Rdn,
507                                                    rGPR:$Rm, pred:$p,
508                                                    cc_out:$s)>;
509  def : t2InstAlias<!strconcat(opc, "${s}${p}", wide, " $Rdn, $shift"),
510     (!cast<Instruction>(!strconcat(baseOpc, "rs")) rGPR:$Rdn, rGPR:$Rdn,
511                                                    t2_so_reg:$shift, pred:$p,
512                                                    cc_out:$s)>;
513}
514
515/// T2I_bin_w_irs - Same as T2I_bin_irs except these operations need
516//  the ".w" suffix to indicate that they are wide.
517multiclass T2I_bin_w_irs<bits<4> opcod, string opc,
518                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
519                         PatFrag opnode, string baseOpc, bit Commutable = 0> :
520    T2I_bin_irs<opcod, opc, iii, iir, iis, opnode, baseOpc, Commutable, ".w"> {
521  // Assembler aliases w/o the ".w" suffix.
522  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $Rm"),
523     (!cast<Instruction>(!strconcat(baseOpc, "rr")) rGPR:$Rd, rGPR:$Rn,
524                                                    rGPR:$Rm, pred:$p,
525                                                    cc_out:$s)>;
526  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $shift"),
527     (!cast<Instruction>(!strconcat(baseOpc, "rs")) rGPR:$Rd, rGPR:$Rn,
528                                                    t2_so_reg:$shift, pred:$p,
529                                                    cc_out:$s)>;
530
531  // and with the optional destination operand, too.
532  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $Rm"),
533     (!cast<Instruction>(!strconcat(baseOpc, "rr")) rGPR:$Rdn, rGPR:$Rdn,
534                                                    rGPR:$Rm, pred:$p,
535                                                    cc_out:$s)>;
536  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $shift"),
537     (!cast<Instruction>(!strconcat(baseOpc, "rs")) rGPR:$Rdn, rGPR:$Rdn,
538                                                    t2_so_reg:$shift, pred:$p,
539                                                    cc_out:$s)>;
540}
541
542/// T2I_rbin_is - Same as T2I_bin_irs except the order of operands are
543/// reversed.  The 'rr' form is only defined for the disassembler; for codegen
544/// it is equivalent to the T2I_bin_irs counterpart.
545multiclass T2I_rbin_irs<bits<4> opcod, string opc, PatFrag opnode> {
546   // shifted imm
547   def ri : T2sTwoRegImm<
548                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm), IIC_iALUi,
549                 opc, ".w\t$Rd, $Rn, $imm",
550                 [(set rGPR:$Rd, (opnode t2_so_imm:$imm, rGPR:$Rn))]> {
551     let Inst{31-27} = 0b11110;
552     let Inst{25} = 0;
553     let Inst{24-21} = opcod;
554     let Inst{15} = 0;
555   }
556   // register
557   def rr : T2sThreeReg<
558                 (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUr,
559                 opc, "\t$Rd, $Rn, $Rm",
560                 [/* For disassembly only; pattern left blank */]> {
561     let Inst{31-27} = 0b11101;
562     let Inst{26-25} = 0b01;
563     let Inst{24-21} = opcod;
564     let Inst{14-12} = 0b000; // imm3
565     let Inst{7-6} = 0b00; // imm2
566     let Inst{5-4} = 0b00; // type
567   }
568   // shifted register
569   def rs : T2sTwoRegShiftedReg<
570                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm),
571                 IIC_iALUsir, opc, "\t$Rd, $Rn, $ShiftedRm",
572                 [(set rGPR:$Rd, (opnode t2_so_reg:$ShiftedRm, rGPR:$Rn))]> {
573     let Inst{31-27} = 0b11101;
574     let Inst{26-25} = 0b01;
575     let Inst{24-21} = opcod;
576   }
577}
578
579/// T2I_bin_s_irs - Similar to T2I_bin_irs except it sets the 's' bit so the
580/// instruction modifies the CPSR register.
581let hasPostISelHook = 1, isCodeGenOnly = 1, Defs = [CPSR] in {
582multiclass T2I_bin_s_irs<bits<4> opcod, string opc,
583                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
584                         PatFrag opnode, bit Commutable = 0> {
585   // shifted imm
586   def ri : T2sTwoRegImm<
587                (outs rGPR:$Rd), (ins GPR:$Rn, t2_so_imm:$imm), iii,
588                opc, ".w\t$Rd, $Rn, $imm",
589                [(set rGPR:$Rd, CPSR, (opnode GPR:$Rn, t2_so_imm:$imm))]> {
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<
597                (outs rGPR:$Rd), (ins GPR:$Rn, rGPR:$Rm), iir,
598                opc, ".w\t$Rd, $Rn, $Rm",
599                [(set rGPR:$Rd, CPSR, (opnode GPR:$Rn, rGPR:$Rm))]> {
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 GPR:$Rn, t2_so_reg:$ShiftedRm), iis,
611                opc, ".w\t$Rd, $Rn, $ShiftedRm",
612               [(set rGPR:$Rd, CPSR, (opnode GPR:$Rn, t2_so_reg:$ShiftedRm))]> {
613     let Inst{31-27} = 0b11101;
614     let Inst{26-25} = 0b01;
615     let Inst{24-21} = opcod;
616   }
617}
618}
619
620/// T2I_bin_ii12rs - Defines a set of (op reg, {so_imm|imm0_4095|r|so_reg})
621/// patterns for a binary operation that produces a value.
622multiclass T2I_bin_ii12rs<bits<3> op23_21, string opc, PatFrag opnode,
623                          bit Commutable = 0> {
624   // shifted imm
625   // The register-immediate version is re-materializable. This is useful
626   // in particular for taking the address of a local.
627   let isReMaterializable = 1 in {
628   def ri : T2sTwoRegImm<
629                 (outs rGPR:$Rd), (ins GPRnopc:$Rn, t2_so_imm:$imm), IIC_iALUi,
630                 opc, ".w\t$Rd, $Rn, $imm",
631                 [(set rGPR:$Rd, (opnode GPRnopc:$Rn, t2_so_imm:$imm))]> {
632     let Inst{31-27} = 0b11110;
633     let Inst{25} = 0;
634     let Inst{24} = 1;
635     let Inst{23-21} = op23_21;
636     let Inst{15} = 0;
637   }
638   }
639   // 12-bit imm
640   def ri12 : T2I<
641                  (outs rGPR:$Rd), (ins GPR:$Rn, imm0_4095:$imm), IIC_iALUi,
642                  !strconcat(opc, "w"), "\t$Rd, $Rn, $imm",
643                  [(set rGPR:$Rd, (opnode GPR:$Rn, imm0_4095:$imm))]> {
644     bits<4> Rd;
645     bits<4> Rn;
646     bits<12> imm;
647     let Inst{31-27} = 0b11110;
648     let Inst{26} = imm{11};
649     let Inst{25-24} = 0b10;
650     let Inst{23-21} = op23_21;
651     let Inst{20} = 0; // The S bit.
652     let Inst{19-16} = Rn;
653     let Inst{15} = 0;
654     let Inst{14-12} = imm{10-8};
655     let Inst{11-8} = Rd;
656     let Inst{7-0} = imm{7-0};
657   }
658   // register
659   def rr : T2sThreeReg<(outs rGPR:$Rd), (ins GPRnopc:$Rn, rGPR:$Rm), IIC_iALUr,
660                 opc, ".w\t$Rd, $Rn, $Rm",
661                 [(set rGPR:$Rd, (opnode GPRnopc:$Rn, rGPR:$Rm))]> {
662     let isCommutable = Commutable;
663     let Inst{31-27} = 0b11101;
664     let Inst{26-25} = 0b01;
665     let Inst{24} = 1;
666     let Inst{23-21} = op23_21;
667     let Inst{14-12} = 0b000; // imm3
668     let Inst{7-6} = 0b00; // imm2
669     let Inst{5-4} = 0b00; // type
670   }
671   // shifted register
672   def rs : T2sTwoRegShiftedReg<
673                 (outs rGPR:$Rd), (ins GPRnopc:$Rn, t2_so_reg:$ShiftedRm),
674                 IIC_iALUsi, opc, ".w\t$Rd, $Rn, $ShiftedRm",
675                 [(set rGPR:$Rd, (opnode GPRnopc:$Rn, t2_so_reg:$ShiftedRm))]> {
676     let Inst{31-27} = 0b11101;
677     let Inst{26-25} = 0b01;
678     let Inst{24} = 1;
679     let Inst{23-21} = op23_21;
680   }
681}
682
683/// T2I_adde_sube_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns
684/// for a binary operation that produces a value and use the carry
685/// bit. It's not predicable.
686let Defs = [CPSR], Uses = [CPSR] in {
687multiclass T2I_adde_sube_irs<bits<4> opcod, string opc, PatFrag opnode,
688                             bit Commutable = 0> {
689   // shifted imm
690   def ri : T2sTwoRegImm<(outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm),
691                 IIC_iALUi, opc, "\t$Rd, $Rn, $imm",
692               [(set rGPR:$Rd, CPSR, (opnode rGPR:$Rn, t2_so_imm:$imm, CPSR))]>,
693                 Requires<[IsThumb2]> {
694     let Inst{31-27} = 0b11110;
695     let Inst{25} = 0;
696     let Inst{24-21} = opcod;
697     let Inst{15} = 0;
698   }
699   // register
700   def rr : T2sThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUr,
701                 opc, ".w\t$Rd, $Rn, $Rm",
702                 [(set rGPR:$Rd, CPSR, (opnode rGPR:$Rn, rGPR:$Rm, CPSR))]>,
703                 Requires<[IsThumb2]> {
704     let isCommutable = Commutable;
705     let Inst{31-27} = 0b11101;
706     let Inst{26-25} = 0b01;
707     let Inst{24-21} = opcod;
708     let Inst{14-12} = 0b000; // imm3
709     let Inst{7-6} = 0b00; // imm2
710     let Inst{5-4} = 0b00; // type
711   }
712   // shifted register
713   def rs : T2sTwoRegShiftedReg<
714                 (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm),
715                 IIC_iALUsi, opc, ".w\t$Rd, $Rn, $ShiftedRm",
716         [(set rGPR:$Rd, CPSR, (opnode rGPR:$Rn, t2_so_reg:$ShiftedRm, CPSR))]>,
717                 Requires<[IsThumb2]> {
718     let Inst{31-27} = 0b11101;
719     let Inst{26-25} = 0b01;
720     let Inst{24-21} = opcod;
721   }
722}
723}
724
725/// T2I_rbin_s_is - Same as T2I_rbin_irs except sets 's' bit and the register
726/// version is not needed since this is only for codegen.
727let hasPostISelHook = 1, isCodeGenOnly = 1, Defs = [CPSR] in {
728multiclass T2I_rbin_s_is<bits<4> opcod, string opc, PatFrag opnode> {
729   // shifted imm
730   def ri : T2sTwoRegImm<
731                (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm), IIC_iALUi,
732                opc, ".w\t$Rd, $Rn, $imm",
733                [(set rGPR:$Rd, CPSR, (opnode t2_so_imm:$imm, rGPR:$Rn))]> {
734     let Inst{31-27} = 0b11110;
735     let Inst{25} = 0;
736     let Inst{24-21} = opcod;
737     let Inst{15} = 0;
738   }
739   // shifted register
740   def rs : T2sTwoRegShiftedReg<
741                (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm),
742                IIC_iALUsi, opc, "\t$Rd, $Rn, $ShiftedRm",
743              [(set rGPR:$Rd, CPSR, (opnode t2_so_reg:$ShiftedRm, rGPR:$Rn))]> {
744     let Inst{31-27} = 0b11101;
745     let Inst{26-25} = 0b01;
746     let Inst{24-21} = opcod;
747   }
748}
749}
750
751/// T2I_sh_ir - Defines a set of (op reg, {so_imm|r}) patterns for a shift /
752//  rotate operation that produces a value.
753multiclass T2I_sh_ir<bits<2> opcod, string opc, Operand ty, PatFrag opnode,
754                     string baseOpc> {
755   // 5-bit imm
756   def ri : T2sTwoRegShiftImm<
757                 (outs rGPR:$Rd), (ins rGPR:$Rm, ty:$imm), IIC_iMOVsi,
758                 opc, ".w\t$Rd, $Rm, $imm",
759                 [(set rGPR:$Rd, (opnode rGPR:$Rm, (i32 ty:$imm)))]> {
760     let Inst{31-27} = 0b11101;
761     let Inst{26-21} = 0b010010;
762     let Inst{19-16} = 0b1111; // Rn
763     let Inst{5-4} = opcod;
764   }
765   // register
766   def rr : T2sThreeReg<
767                 (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMOVsr,
768                 opc, ".w\t$Rd, $Rn, $Rm",
769                 [(set rGPR:$Rd, (opnode rGPR:$Rn, rGPR:$Rm))]> {
770     let Inst{31-27} = 0b11111;
771     let Inst{26-23} = 0b0100;
772     let Inst{22-21} = opcod;
773     let Inst{15-12} = 0b1111;
774     let Inst{7-4} = 0b0000;
775   }
776
777  // Optional destination register
778  def : t2InstAlias<!strconcat(opc, "${s}${p}", ".w $Rdn, $imm"),
779     (!cast<Instruction>(!strconcat(baseOpc, "ri")) rGPR:$Rdn, rGPR:$Rdn,
780                                                    ty:$imm, pred:$p,
781                                                    cc_out:$s)>;
782  def : t2InstAlias<!strconcat(opc, "${s}${p}", ".w $Rdn, $Rm"),
783     (!cast<Instruction>(!strconcat(baseOpc, "rr")) rGPR:$Rdn, rGPR:$Rdn,
784                                                    rGPR:$Rm, pred:$p,
785                                                    cc_out:$s)>;
786
787  // Assembler aliases w/o the ".w" suffix.
788  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $imm"),
789     (!cast<Instruction>(!strconcat(baseOpc, "ri")) rGPR:$Rd, rGPR:$Rn,
790                                                    ty:$imm, pred:$p,
791                                                   cc_out:$s)>;
792  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rd, $Rn, $Rm"),
793     (!cast<Instruction>(!strconcat(baseOpc, "rr")) rGPR:$Rd, rGPR:$Rn,
794                                                    rGPR:$Rm, pred:$p,
795                                                    cc_out:$s)>;
796
797  // and with the optional destination operand, too.
798  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $imm"),
799     (!cast<Instruction>(!strconcat(baseOpc, "ri")) rGPR:$Rdn, rGPR:$Rdn,
800                                                    ty:$imm, pred:$p,
801                                                    cc_out:$s)>;
802  def : t2InstAlias<!strconcat(opc, "${s}${p}", " $Rdn, $Rm"),
803     (!cast<Instruction>(!strconcat(baseOpc, "rr")) rGPR:$Rdn, rGPR:$Rdn,
804                                                    rGPR:$Rm, pred:$p,
805                                                    cc_out:$s)>;
806}
807
808/// T2I_cmp_irs - Defines a set of (op r, {so_imm|r|so_reg}) cmp / test
809/// patterns. Similar to T2I_bin_irs except the instruction does not produce
810/// a explicit result, only implicitly set CPSR.
811multiclass T2I_cmp_irs<bits<4> opcod, string opc,
812                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
813                       PatFrag opnode, string baseOpc> {
814let isCompare = 1, Defs = [CPSR] in {
815   // shifted imm
816   def ri : T2OneRegCmpImm<
817                (outs), (ins GPRnopc:$Rn, t2_so_imm:$imm), iii,
818                opc, ".w\t$Rn, $imm",
819                [(opnode GPRnopc:$Rn, t2_so_imm:$imm)]> {
820     let Inst{31-27} = 0b11110;
821     let Inst{25} = 0;
822     let Inst{24-21} = opcod;
823     let Inst{20} = 1; // The S bit.
824     let Inst{15} = 0;
825     let Inst{11-8} = 0b1111; // Rd
826   }
827   // register
828   def rr : T2TwoRegCmp<
829                (outs), (ins GPRnopc:$Rn, rGPR:$Rm), iir,
830                opc, ".w\t$Rn, $Rm",
831                [(opnode GPRnopc:$Rn, rGPR:$Rm)]> {
832     let Inst{31-27} = 0b11101;
833     let Inst{26-25} = 0b01;
834     let Inst{24-21} = opcod;
835     let Inst{20} = 1; // The S bit.
836     let Inst{14-12} = 0b000; // imm3
837     let Inst{11-8} = 0b1111; // Rd
838     let Inst{7-6} = 0b00; // imm2
839     let Inst{5-4} = 0b00; // type
840   }
841   // shifted register
842   def rs : T2OneRegCmpShiftedReg<
843                (outs), (ins GPRnopc:$Rn, t2_so_reg:$ShiftedRm), iis,
844                opc, ".w\t$Rn, $ShiftedRm",
845                [(opnode GPRnopc:$Rn, t2_so_reg:$ShiftedRm)]> {
846     let Inst{31-27} = 0b11101;
847     let Inst{26-25} = 0b01;
848     let Inst{24-21} = opcod;
849     let Inst{20} = 1; // The S bit.
850     let Inst{11-8} = 0b1111; // Rd
851   }
852}
853
854  // Assembler aliases w/o the ".w" suffix.
855  // No alias here for 'rr' version as not all instantiations of this
856  // multiclass want one (CMP in particular, does not).
857  def : t2InstAlias<!strconcat(opc, "${p}", " $Rn, $imm"),
858     (!cast<Instruction>(!strconcat(baseOpc, "ri")) GPRnopc:$Rn,
859                                                    t2_so_imm:$imm, pred:$p)>;
860  def : t2InstAlias<!strconcat(opc, "${p}", " $Rn, $shift"),
861     (!cast<Instruction>(!strconcat(baseOpc, "rs")) GPRnopc:$Rn,
862                                                    t2_so_reg:$shift,
863                                                    pred:$p)>;
864}
865
866/// T2I_ld - Defines a set of (op r, {imm12|imm8|so_reg}) load patterns.
867multiclass T2I_ld<bit signed, bits<2> opcod, string opc,
868                  InstrItinClass iii, InstrItinClass iis, RegisterClass target,
869                  PatFrag opnode> {
870  def i12 : T2Ii12<(outs target:$Rt), (ins t2addrmode_imm12:$addr), iii,
871                   opc, ".w\t$Rt, $addr",
872                   [(set target:$Rt, (opnode t2addrmode_imm12:$addr))]> {
873    bits<4> Rt;
874    bits<17> addr;
875    let Inst{31-25} = 0b1111100;
876    let Inst{24} = signed;
877    let Inst{23} = 1;
878    let Inst{22-21} = opcod;
879    let Inst{20} = 1; // load
880    let Inst{19-16} = addr{16-13}; // Rn
881    let Inst{15-12} = Rt;
882    let Inst{11-0}  = addr{11-0};  // imm
883  }
884  def i8  : T2Ii8 <(outs target:$Rt), (ins t2addrmode_negimm8:$addr), iii,
885                   opc, "\t$Rt, $addr",
886                   [(set target:$Rt, (opnode t2addrmode_negimm8:$addr))]> {
887    bits<4> Rt;
888    bits<13> addr;
889    let Inst{31-27} = 0b11111;
890    let Inst{26-25} = 0b00;
891    let Inst{24} = signed;
892    let Inst{23} = 0;
893    let Inst{22-21} = opcod;
894    let Inst{20} = 1; // load
895    let Inst{19-16} = addr{12-9}; // Rn
896    let Inst{15-12} = Rt;
897    let Inst{11} = 1;
898    // Offset: index==TRUE, wback==FALSE
899    let Inst{10} = 1; // The P bit.
900    let Inst{9}     = addr{8};    // U
901    let Inst{8} = 0; // The W bit.
902    let Inst{7-0}   = addr{7-0};  // imm
903  }
904  def s   : T2Iso <(outs target:$Rt), (ins t2addrmode_so_reg:$addr), iis,
905                   opc, ".w\t$Rt, $addr",
906                   [(set target:$Rt, (opnode t2addrmode_so_reg:$addr))]> {
907    let Inst{31-27} = 0b11111;
908    let Inst{26-25} = 0b00;
909    let Inst{24} = signed;
910    let Inst{23} = 0;
911    let Inst{22-21} = opcod;
912    let Inst{20} = 1; // load
913    let Inst{11-6} = 0b000000;
914
915    bits<4> Rt;
916    let Inst{15-12} = Rt;
917
918    bits<10> addr;
919    let Inst{19-16} = addr{9-6}; // Rn
920    let Inst{3-0}   = addr{5-2}; // Rm
921    let Inst{5-4}   = addr{1-0}; // imm
922
923    let DecoderMethod = "DecodeT2LoadShift";
924  }
925
926  // FIXME: Is the pci variant actually needed?
927  def pci : T2Ipc <(outs target:$Rt), (ins t2ldrlabel:$addr), iii,
928                   opc, ".w\t$Rt, $addr",
929                   [(set target:$Rt, (opnode (ARMWrapper tconstpool:$addr)))]> {
930    let isReMaterializable = 1;
931    let Inst{31-27} = 0b11111;
932    let Inst{26-25} = 0b00;
933    let Inst{24} = signed;
934    let Inst{23} = ?; // add = (U == '1')
935    let Inst{22-21} = opcod;
936    let Inst{20} = 1; // load
937    let Inst{19-16} = 0b1111; // Rn
938    bits<4> Rt;
939    bits<12> addr;
940    let Inst{15-12} = Rt{3-0};
941    let Inst{11-0}  = addr{11-0};
942  }
943}
944
945/// T2I_st - Defines a set of (op r, {imm12|imm8|so_reg}) store patterns.
946multiclass T2I_st<bits<2> opcod, string opc,
947                  InstrItinClass iii, InstrItinClass iis, RegisterClass target,
948                  PatFrag opnode> {
949  def i12 : T2Ii12<(outs), (ins target:$Rt, t2addrmode_imm12:$addr), iii,
950                   opc, ".w\t$Rt, $addr",
951                   [(opnode target:$Rt, t2addrmode_imm12:$addr)]> {
952    let Inst{31-27} = 0b11111;
953    let Inst{26-23} = 0b0001;
954    let Inst{22-21} = opcod;
955    let Inst{20} = 0; // !load
956
957    bits<4> Rt;
958    let Inst{15-12} = Rt;
959
960    bits<17> addr;
961    let addr{12}    = 1;           // add = TRUE
962    let Inst{19-16} = addr{16-13}; // Rn
963    let Inst{23}    = addr{12};    // U
964    let Inst{11-0}  = addr{11-0};  // imm
965  }
966  def i8  : T2Ii8 <(outs), (ins target:$Rt, t2addrmode_negimm8:$addr), iii,
967                   opc, "\t$Rt, $addr",
968                   [(opnode target:$Rt, t2addrmode_negimm8:$addr)]> {
969    let Inst{31-27} = 0b11111;
970    let Inst{26-23} = 0b0000;
971    let Inst{22-21} = opcod;
972    let Inst{20} = 0; // !load
973    let Inst{11} = 1;
974    // Offset: index==TRUE, wback==FALSE
975    let Inst{10} = 1; // The P bit.
976    let Inst{8} = 0; // The W bit.
977
978    bits<4> Rt;
979    let Inst{15-12} = Rt;
980
981    bits<13> addr;
982    let Inst{19-16} = addr{12-9}; // Rn
983    let Inst{9}     = addr{8};    // U
984    let Inst{7-0}   = addr{7-0};  // imm
985  }
986  def s   : T2Iso <(outs), (ins target:$Rt, t2addrmode_so_reg:$addr), iis,
987                   opc, ".w\t$Rt, $addr",
988                   [(opnode target:$Rt, t2addrmode_so_reg:$addr)]> {
989    let Inst{31-27} = 0b11111;
990    let Inst{26-23} = 0b0000;
991    let Inst{22-21} = opcod;
992    let Inst{20} = 0; // !load
993    let Inst{11-6} = 0b000000;
994
995    bits<4> Rt;
996    let Inst{15-12} = Rt;
997
998    bits<10> addr;
999    let Inst{19-16}   = addr{9-6}; // Rn
1000    let Inst{3-0} = addr{5-2}; // Rm
1001    let Inst{5-4}   = addr{1-0}; // imm
1002  }
1003}
1004
1005/// T2I_ext_rrot - A unary operation with two forms: one whose operand is a
1006/// register and one whose operand is a register rotated by 8/16/24.
1007class T2I_ext_rrot<bits<3> opcod, string opc, PatFrag opnode>
1008  : T2TwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm, rot_imm:$rot), IIC_iEXTr,
1009             opc, ".w\t$Rd, $Rm$rot",
1010             [(set rGPR:$Rd, (opnode (rotr rGPR:$Rm, rot_imm:$rot)))]>,
1011             Requires<[IsThumb2]> {
1012   let Inst{31-27} = 0b11111;
1013   let Inst{26-23} = 0b0100;
1014   let Inst{22-20} = opcod;
1015   let Inst{19-16} = 0b1111; // Rn
1016   let Inst{15-12} = 0b1111;
1017   let Inst{7} = 1;
1018
1019   bits<2> rot;
1020   let Inst{5-4} = rot{1-0}; // rotate
1021}
1022
1023// UXTB16 - Requres T2ExtractPack, does not need the .w qualifier.
1024class T2I_ext_rrot_uxtb16<bits<3> opcod, string opc, PatFrag opnode>
1025  : T2TwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm, rot_imm:$rot),
1026             IIC_iEXTr, opc, "\t$Rd, $Rm$rot",
1027            [(set rGPR:$Rd, (opnode (rotr rGPR:$Rm, rot_imm:$rot)))]>,
1028          Requires<[HasT2ExtractPack, IsThumb2]> {
1029  bits<2> rot;
1030  let Inst{31-27} = 0b11111;
1031  let Inst{26-23} = 0b0100;
1032  let Inst{22-20} = opcod;
1033  let Inst{19-16} = 0b1111; // Rn
1034  let Inst{15-12} = 0b1111;
1035  let Inst{7} = 1;
1036  let Inst{5-4} = rot;
1037}
1038
1039// SXTB16 - Requres T2ExtractPack, does not need the .w qualifier, no pattern
1040// supported yet.
1041class T2I_ext_rrot_sxtb16<bits<3> opcod, string opc>
1042  : T2TwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm, rot_imm:$rot), IIC_iEXTr,
1043             opc, "\t$Rd, $Rm$rot", []>,
1044          Requires<[IsThumb2, HasT2ExtractPack]> {
1045  bits<2> rot;
1046  let Inst{31-27} = 0b11111;
1047  let Inst{26-23} = 0b0100;
1048  let Inst{22-20} = opcod;
1049  let Inst{19-16} = 0b1111; // Rn
1050  let Inst{15-12} = 0b1111;
1051  let Inst{7} = 1;
1052  let Inst{5-4} = rot;
1053}
1054
1055/// T2I_exta_rrot - A binary operation with two forms: one whose operand is a
1056/// register and one whose operand is a register rotated by 8/16/24.
1057class T2I_exta_rrot<bits<3> opcod, string opc, PatFrag opnode>
1058  : T2ThreeReg<(outs rGPR:$Rd),
1059               (ins rGPR:$Rn, rGPR:$Rm, rot_imm:$rot),
1060               IIC_iEXTAsr, opc, "\t$Rd, $Rn, $Rm$rot",
1061             [(set rGPR:$Rd, (opnode rGPR:$Rn, (rotr rGPR:$Rm,rot_imm:$rot)))]>,
1062           Requires<[HasT2ExtractPack, IsThumb2]> {
1063  bits<2> rot;
1064  let Inst{31-27} = 0b11111;
1065  let Inst{26-23} = 0b0100;
1066  let Inst{22-20} = opcod;
1067  let Inst{15-12} = 0b1111;
1068  let Inst{7} = 1;
1069  let Inst{5-4} = rot;
1070}
1071
1072class T2I_exta_rrot_np<bits<3> opcod, string opc>
1073  : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm,rot_imm:$rot),
1074               IIC_iEXTAsr, opc, "\t$Rd, $Rn, $Rm$rot", []> {
1075  bits<2> rot;
1076  let Inst{31-27} = 0b11111;
1077  let Inst{26-23} = 0b0100;
1078  let Inst{22-20} = opcod;
1079  let Inst{15-12} = 0b1111;
1080  let Inst{7} = 1;
1081  let Inst{5-4} = rot;
1082}
1083
1084//===----------------------------------------------------------------------===//
1085// Instructions
1086//===----------------------------------------------------------------------===//
1087
1088//===----------------------------------------------------------------------===//
1089//  Miscellaneous Instructions.
1090//
1091
1092class T2PCOneRegImm<dag oops, dag iops, InstrItinClass itin,
1093           string asm, list<dag> pattern>
1094  : T2XI<oops, iops, itin, asm, pattern> {
1095  bits<4> Rd;
1096  bits<12> label;
1097
1098  let Inst{11-8}  = Rd;
1099  let Inst{26}    = label{11};
1100  let Inst{14-12} = label{10-8};
1101  let Inst{7-0}   = label{7-0};
1102}
1103
1104// LEApcrel - Load a pc-relative address into a register without offending the
1105// assembler.
1106def t2ADR : T2PCOneRegImm<(outs rGPR:$Rd),
1107              (ins t2adrlabel:$addr, pred:$p),
1108              IIC_iALUi, "adr{$p}.w\t$Rd, $addr", []> {
1109  let Inst{31-27} = 0b11110;
1110  let Inst{25-24} = 0b10;
1111  // Inst{23:21} = '11' (add = FALSE) or '00' (add = TRUE)
1112  let Inst{22} = 0;
1113  let Inst{20} = 0;
1114  let Inst{19-16} = 0b1111; // Rn
1115  let Inst{15} = 0;
1116
1117  bits<4> Rd;
1118  bits<13> addr;
1119  let Inst{11-8} = Rd;
1120  let Inst{23}    = addr{12};
1121  let Inst{21}    = addr{12};
1122  let Inst{26}    = addr{11};
1123  let Inst{14-12} = addr{10-8};
1124  let Inst{7-0}   = addr{7-0};
1125
1126  let DecoderMethod = "DecodeT2Adr";
1127}
1128
1129let neverHasSideEffects = 1, isReMaterializable = 1 in
1130def t2LEApcrel   : t2PseudoInst<(outs rGPR:$Rd), (ins i32imm:$label, pred:$p),
1131                                4, IIC_iALUi, []>;
1132def t2LEApcrelJT : t2PseudoInst<(outs rGPR:$Rd),
1133                                (ins i32imm:$label, nohash_imm:$id, pred:$p),
1134                                4, IIC_iALUi,
1135                                []>;
1136
1137
1138//===----------------------------------------------------------------------===//
1139//  Load / store Instructions.
1140//
1141
1142// Load
1143let canFoldAsLoad = 1, isReMaterializable = 1  in
1144defm t2LDR   : T2I_ld<0, 0b10, "ldr", IIC_iLoad_i, IIC_iLoad_si, GPR,
1145                      UnOpFrag<(load node:$Src)>>;
1146
1147// Loads with zero extension
1148defm t2LDRH  : T2I_ld<0, 0b01, "ldrh", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1149                      rGPR, UnOpFrag<(zextloadi16 node:$Src)>>;
1150defm t2LDRB  : T2I_ld<0, 0b00, "ldrb", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1151                      rGPR, UnOpFrag<(zextloadi8  node:$Src)>>;
1152
1153// Loads with sign extension
1154defm t2LDRSH : T2I_ld<1, 0b01, "ldrsh", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1155                      rGPR, UnOpFrag<(sextloadi16 node:$Src)>>;
1156defm t2LDRSB : T2I_ld<1, 0b00, "ldrsb", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
1157                      rGPR, UnOpFrag<(sextloadi8  node:$Src)>>;
1158
1159let mayLoad = 1, neverHasSideEffects = 1, hasExtraDefRegAllocReq = 1 in {
1160// Load doubleword
1161def t2LDRDi8  : T2Ii8s4<1, 0, 1, (outs rGPR:$Rt, rGPR:$Rt2),
1162                        (ins t2addrmode_imm8s4:$addr),
1163                        IIC_iLoad_d_i, "ldrd", "\t$Rt, $Rt2, $addr", "", []>;
1164} // mayLoad = 1, neverHasSideEffects = 1, hasExtraDefRegAllocReq = 1
1165
1166// zextload i1 -> zextload i8
1167def : T2Pat<(zextloadi1 t2addrmode_imm12:$addr),
1168            (t2LDRBi12  t2addrmode_imm12:$addr)>;
1169def : T2Pat<(zextloadi1 t2addrmode_negimm8:$addr),
1170            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
1171def : T2Pat<(zextloadi1 t2addrmode_so_reg:$addr),
1172            (t2LDRBs    t2addrmode_so_reg:$addr)>;
1173def : T2Pat<(zextloadi1 (ARMWrapper tconstpool:$addr)),
1174            (t2LDRBpci  tconstpool:$addr)>;
1175
1176// extload -> zextload
1177// FIXME: Reduce the number of patterns by legalizing extload to zextload
1178// earlier?
1179def : T2Pat<(extloadi1  t2addrmode_imm12:$addr),
1180            (t2LDRBi12  t2addrmode_imm12:$addr)>;
1181def : T2Pat<(extloadi1  t2addrmode_negimm8:$addr),
1182            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
1183def : T2Pat<(extloadi1  t2addrmode_so_reg:$addr),
1184            (t2LDRBs    t2addrmode_so_reg:$addr)>;
1185def : T2Pat<(extloadi1  (ARMWrapper tconstpool:$addr)),
1186            (t2LDRBpci  tconstpool:$addr)>;
1187
1188def : T2Pat<(extloadi8  t2addrmode_imm12:$addr),
1189            (t2LDRBi12  t2addrmode_imm12:$addr)>;
1190def : T2Pat<(extloadi8  t2addrmode_negimm8:$addr),
1191            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
1192def : T2Pat<(extloadi8  t2addrmode_so_reg:$addr),
1193            (t2LDRBs    t2addrmode_so_reg:$addr)>;
1194def : T2Pat<(extloadi8  (ARMWrapper tconstpool:$addr)),
1195            (t2LDRBpci  tconstpool:$addr)>;
1196
1197def : T2Pat<(extloadi16 t2addrmode_imm12:$addr),
1198            (t2LDRHi12  t2addrmode_imm12:$addr)>;
1199def : T2Pat<(extloadi16 t2addrmode_negimm8:$addr),
1200            (t2LDRHi8   t2addrmode_negimm8:$addr)>;
1201def : T2Pat<(extloadi16 t2addrmode_so_reg:$addr),
1202            (t2LDRHs    t2addrmode_so_reg:$addr)>;
1203def : T2Pat<(extloadi16 (ARMWrapper tconstpool:$addr)),
1204            (t2LDRHpci  tconstpool:$addr)>;
1205
1206// FIXME: The destination register of the loads and stores can't be PC, but
1207//        can be SP. We need another regclass (similar to rGPR) to represent
1208//        that. Not a pressing issue since these are selected manually,
1209//        not via pattern.
1210
1211// Indexed loads
1212
1213let mayLoad = 1, neverHasSideEffects = 1 in {
1214def t2LDR_PRE  : T2Ipreldst<0, 0b10, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1215                            (ins t2addrmode_imm8:$addr),
1216                            AddrModeT2_i8, IndexModePre, IIC_iLoad_iu,
1217                            "ldr", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1218                            []> {
1219  let AsmMatchConverter = "cvtLdWriteBackRegT2AddrModeImm8";
1220}
1221
1222def t2LDR_POST : T2Ipostldst<0, 0b10, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1223                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1224                          AddrModeT2_i8, IndexModePost, IIC_iLoad_iu,
1225                          "ldr", "\t$Rt, $Rn, $offset", "$Rn = $Rn_wb", []>;
1226
1227def t2LDRB_PRE : T2Ipreldst<0, 0b00, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1228                            (ins t2addrmode_imm8:$addr),
1229                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1230                            "ldrb", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1231                            []> {
1232  let AsmMatchConverter = "cvtLdWriteBackRegT2AddrModeImm8";
1233}
1234def t2LDRB_POST : T2Ipostldst<0, 0b00, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1235                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1236                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1237                          "ldrb", "\t$Rt, $Rn, $offset", "$Rn = $Rn_wb", []>;
1238
1239def t2LDRH_PRE : T2Ipreldst<0, 0b01, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1240                            (ins t2addrmode_imm8:$addr),
1241                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1242                            "ldrh", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1243                            []> {
1244  let AsmMatchConverter = "cvtLdWriteBackRegT2AddrModeImm8";
1245}
1246def t2LDRH_POST : T2Ipostldst<0, 0b01, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1247                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1248                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1249                          "ldrh", "\t$Rt, $Rn, $offset", "$Rn = $Rn_wb", []>;
1250
1251def t2LDRSB_PRE : T2Ipreldst<1, 0b00, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1252                            (ins t2addrmode_imm8:$addr),
1253                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1254                            "ldrsb", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1255                            []> {
1256  let AsmMatchConverter = "cvtLdWriteBackRegT2AddrModeImm8";
1257}
1258def t2LDRSB_POST : T2Ipostldst<1, 0b00, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1259                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1260                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1261                          "ldrsb", "\t$Rt, $Rn, $offset", "$Rn = $Rn_wb", []>;
1262
1263def t2LDRSH_PRE : T2Ipreldst<1, 0b01, 1, 1, (outs GPR:$Rt, GPR:$Rn_wb),
1264                            (ins t2addrmode_imm8:$addr),
1265                            AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1266                            "ldrsh", "\t$Rt, $addr!", "$addr.base = $Rn_wb",
1267                            []> {
1268  let AsmMatchConverter = "cvtLdWriteBackRegT2AddrModeImm8";
1269}
1270def t2LDRSH_POST : T2Ipostldst<1, 0b01, 1, 0, (outs GPR:$Rt, GPR:$Rn_wb),
1271                          (ins addr_offset_none:$Rn, t2am_imm8_offset:$offset),
1272                          AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1273                          "ldrsh", "\t$Rt, $Rn, $offset", "$Rn = $Rn_wb", []>;
1274} // mayLoad = 1, neverHasSideEffects = 1
1275
1276// LDRT, LDRBT, LDRHT, LDRSBT, LDRSHT all have offset mode (PUW=0b110).
1277// Ref: A8.6.57 LDR (immediate, Thumb) Encoding T4
1278class T2IldT<bit signed, bits<2> type, string opc, InstrItinClass ii>
1279  : T2Ii8<(outs rGPR:$Rt), (ins t2addrmode_posimm8:$addr), ii, opc,
1280          "\t$Rt, $addr", []> {
1281  bits<4> Rt;
1282  bits<13> addr;
1283  let Inst{31-27} = 0b11111;
1284  let Inst{26-25} = 0b00;
1285  let Inst{24} = signed;
1286  let Inst{23} = 0;
1287  let Inst{22-21} = type;
1288  let Inst{20} = 1; // load
1289  let Inst{19-16} = addr{12-9};
1290  let Inst{15-12} = Rt;
1291  let Inst{11} = 1;
1292  let Inst{10-8} = 0b110; // PUW.
1293  let Inst{7-0} = addr{7-0};
1294}
1295
1296def t2LDRT   : T2IldT<0, 0b10, "ldrt", IIC_iLoad_i>;
1297def t2LDRBT  : T2IldT<0, 0b00, "ldrbt", IIC_iLoad_bh_i>;
1298def t2LDRHT  : T2IldT<0, 0b01, "ldrht", IIC_iLoad_bh_i>;
1299def t2LDRSBT : T2IldT<1, 0b00, "ldrsbt", IIC_iLoad_bh_i>;
1300def t2LDRSHT : T2IldT<1, 0b01, "ldrsht", IIC_iLoad_bh_i>;
1301
1302// Store
1303defm t2STR :T2I_st<0b10,"str", IIC_iStore_i, IIC_iStore_si, GPR,
1304                   BinOpFrag<(store node:$LHS, node:$RHS)>>;
1305defm t2STRB:T2I_st<0b00,"strb", IIC_iStore_bh_i, IIC_iStore_bh_si,
1306                   rGPR, BinOpFrag<(truncstorei8 node:$LHS, node:$RHS)>>;
1307defm t2STRH:T2I_st<0b01,"strh", IIC_iStore_bh_i, IIC_iStore_bh_si,
1308                   rGPR, BinOpFrag<(truncstorei16 node:$LHS, node:$RHS)>>;
1309
1310// Store doubleword
1311let mayLoad = 1, neverHasSideEffects = 1, hasExtraSrcRegAllocReq = 1 in
1312def t2STRDi8 : T2Ii8s4<1, 0, 0, (outs),
1313                       (ins GPR:$Rt, GPR:$Rt2, t2addrmode_imm8s4:$addr),
1314               IIC_iStore_d_r, "strd", "\t$Rt, $Rt2, $addr", "", []>;
1315
1316// Indexed stores
1317def t2STR_PRE  : T2Ipreldst<0, 0b10, 0, 1, (outs GPRnopc:$Rn_wb),
1318                            (ins rGPR:$Rt, t2addrmode_imm8:$addr),
1319                            AddrModeT2_i8, IndexModePre, IIC_iStore_iu,
1320                            "str", "\t$Rt, $addr!",
1321                            "$addr.base = $Rn_wb,@earlyclobber $Rn_wb", []> {
1322  let AsmMatchConverter = "cvtStWriteBackRegT2AddrModeImm8";
1323}
1324def t2STRH_PRE  : T2Ipreldst<0, 0b01, 0, 1, (outs GPRnopc:$Rn_wb),
1325                            (ins rGPR:$Rt, t2addrmode_imm8:$addr),
1326                            AddrModeT2_i8, IndexModePre, IIC_iStore_iu,
1327                        "strh", "\t$Rt, $addr!",
1328                        "$addr.base = $Rn_wb,@earlyclobber $Rn_wb", []> {
1329  let AsmMatchConverter = "cvtStWriteBackRegT2AddrModeImm8";
1330}
1331
1332def t2STRB_PRE  : T2Ipreldst<0, 0b00, 0, 1, (outs GPRnopc:$Rn_wb),
1333                            (ins rGPR:$Rt, t2addrmode_imm8:$addr),
1334                            AddrModeT2_i8, IndexModePre, IIC_iStore_bh_iu,
1335                        "strb", "\t$Rt, $addr!",
1336                        "$addr.base = $Rn_wb,@earlyclobber $Rn_wb", []> {
1337  let AsmMatchConverter = "cvtStWriteBackRegT2AddrModeImm8";
1338}
1339
1340def t2STR_POST : T2Ipostldst<0, 0b10, 0, 0, (outs GPRnopc:$Rn_wb),
1341                            (ins rGPR:$Rt, addr_offset_none:$Rn,
1342                                 t2am_imm8_offset:$offset),
1343                            AddrModeT2_i8, IndexModePost, IIC_iStore_iu,
1344                          "str", "\t$Rt, $Rn, $offset",
1345                          "$Rn = $Rn_wb,@earlyclobber $Rn_wb",
1346             [(set GPRnopc:$Rn_wb,
1347                  (post_store rGPR:$Rt, addr_offset_none:$Rn,
1348                              t2am_imm8_offset:$offset))]>;
1349
1350def t2STRH_POST : T2Ipostldst<0, 0b01, 0, 0, (outs GPRnopc:$Rn_wb),
1351                            (ins rGPR:$Rt, addr_offset_none:$Rn,
1352                                 t2am_imm8_offset:$offset),
1353                            AddrModeT2_i8, IndexModePost, IIC_iStore_bh_iu,
1354                         "strh", "\t$Rt, $Rn, $offset",
1355                         "$Rn = $Rn_wb,@earlyclobber $Rn_wb",
1356       [(set GPRnopc:$Rn_wb,
1357             (post_truncsti16 rGPR:$Rt, addr_offset_none:$Rn,
1358                              t2am_imm8_offset:$offset))]>;
1359
1360def t2STRB_POST : T2Ipostldst<0, 0b00, 0, 0, (outs GPRnopc:$Rn_wb),
1361                            (ins rGPR:$Rt, addr_offset_none:$Rn,
1362                                 t2am_imm8_offset:$offset),
1363                            AddrModeT2_i8, IndexModePost, IIC_iStore_bh_iu,
1364                         "strb", "\t$Rt, $Rn, $offset",
1365                         "$Rn = $Rn_wb,@earlyclobber $Rn_wb",
1366        [(set GPRnopc:$Rn_wb,
1367              (post_truncsti8 rGPR:$Rt, addr_offset_none:$Rn,
1368                              t2am_imm8_offset:$offset))]>;
1369
1370// Pseudo-instructions for pattern matching the pre-indexed stores. We can't
1371// put the patterns on the instruction definitions directly as ISel wants
1372// the address base and offset to be separate operands, not a single
1373// complex operand like we represent the instructions themselves. The
1374// pseudos map between the two.
1375let usesCustomInserter = 1,
1376    Constraints = "$Rn = $Rn_wb,@earlyclobber $Rn_wb" in {
1377def t2STR_preidx: t2PseudoInst<(outs GPRnopc:$Rn_wb),
1378               (ins rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset, pred:$p),
1379               4, IIC_iStore_ru,
1380      [(set GPRnopc:$Rn_wb,
1381            (pre_store rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset))]>;
1382def t2STRB_preidx: t2PseudoInst<(outs GPRnopc:$Rn_wb),
1383               (ins rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset, pred:$p),
1384               4, IIC_iStore_ru,
1385      [(set GPRnopc:$Rn_wb,
1386            (pre_truncsti8 rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset))]>;
1387def t2STRH_preidx: t2PseudoInst<(outs GPRnopc:$Rn_wb),
1388               (ins rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset, pred:$p),
1389               4, IIC_iStore_ru,
1390      [(set GPRnopc:$Rn_wb,
1391            (pre_truncsti16 rGPR:$Rt, GPRnopc:$Rn, t2am_imm8_offset:$offset))]>;
1392}
1393
1394
1395// STRT, STRBT, STRHT all have offset mode (PUW=0b110) and are for disassembly
1396// only.
1397// Ref: A8.6.193 STR (immediate, Thumb) Encoding T4
1398class T2IstT<bits<2> type, string opc, InstrItinClass ii>
1399  : T2Ii8<(outs rGPR:$Rt), (ins t2addrmode_imm8:$addr), ii, opc,
1400          "\t$Rt, $addr", []> {
1401  let Inst{31-27} = 0b11111;
1402  let Inst{26-25} = 0b00;
1403  let Inst{24} = 0; // not signed
1404  let Inst{23} = 0;
1405  let Inst{22-21} = type;
1406  let Inst{20} = 0; // store
1407  let Inst{11} = 1;
1408  let Inst{10-8} = 0b110; // PUW
1409
1410  bits<4> Rt;
1411  bits<13> addr;
1412  let Inst{15-12} = Rt;
1413  let Inst{19-16} = addr{12-9};
1414  let Inst{7-0}   = addr{7-0};
1415}
1416
1417def t2STRT   : T2IstT<0b10, "strt", IIC_iStore_i>;
1418def t2STRBT  : T2IstT<0b00, "strbt", IIC_iStore_bh_i>;
1419def t2STRHT  : T2IstT<0b01, "strht", IIC_iStore_bh_i>;
1420
1421// ldrd / strd pre / post variants
1422// For disassembly only.
1423
1424def t2LDRD_PRE  : T2Ii8s4<1, 1, 1, (outs rGPR:$Rt, rGPR:$Rt2, GPR:$wb),
1425                 (ins t2addrmode_imm8s4:$addr), IIC_iLoad_d_ru,
1426                 "ldrd", "\t$Rt, $Rt2, $addr!", "$addr.base = $wb", []> {
1427  let AsmMatchConverter = "cvtT2LdrdPre";
1428  let DecoderMethod = "DecodeT2LDRDPreInstruction";
1429}
1430
1431def t2LDRD_POST : T2Ii8s4post<0, 1, 1, (outs rGPR:$Rt, rGPR:$Rt2, GPR:$wb),
1432                 (ins addr_offset_none:$addr, t2am_imm8s4_offset:$imm),
1433                 IIC_iLoad_d_ru, "ldrd", "\t$Rt, $Rt2, $addr$imm",
1434                 "$addr.base = $wb", []>;
1435
1436def t2STRD_PRE  : T2Ii8s4<1, 1, 0, (outs GPR:$wb),
1437                 (ins rGPR:$Rt, rGPR:$Rt2, t2addrmode_imm8s4:$addr),
1438                 IIC_iStore_d_ru, "strd", "\t$Rt, $Rt2, $addr!",
1439                 "$addr.base = $wb", []> {
1440  let AsmMatchConverter = "cvtT2StrdPre";
1441  let DecoderMethod = "DecodeT2STRDPreInstruction";
1442}
1443
1444def t2STRD_POST : T2Ii8s4post<0, 1, 0, (outs GPR:$wb),
1445                 (ins rGPR:$Rt, rGPR:$Rt2, addr_offset_none:$addr,
1446                      t2am_imm8s4_offset:$imm),
1447                 IIC_iStore_d_ru, "strd", "\t$Rt, $Rt2, $addr$imm",
1448                 "$addr.base = $wb", []>;
1449
1450// T2Ipl (Preload Data/Instruction) signals the memory system of possible future
1451// data/instruction access.  These are for disassembly only.
1452// instr_write is inverted for Thumb mode: (prefetch 3) -> (preload 0),
1453// (prefetch 1) -> (preload 2),  (prefetch 2) -> (preload 1).
1454multiclass T2Ipl<bits<1> write, bits<1> instr, string opc> {
1455
1456  def i12 : T2Ii12<(outs), (ins t2addrmode_imm12:$addr), IIC_Preload, opc,
1457                "\t$addr",
1458              [(ARMPreload t2addrmode_imm12:$addr, (i32 write), (i32 instr))]> {
1459    let Inst{31-25} = 0b1111100;
1460    let Inst{24} = instr;
1461    let Inst{22} = 0;
1462    let Inst{21} = write;
1463    let Inst{20} = 1;
1464    let Inst{15-12} = 0b1111;
1465
1466    bits<17> addr;
1467    let addr{12}    = 1;           // add = TRUE
1468    let Inst{19-16} = addr{16-13}; // Rn
1469    let Inst{23}    = addr{12};    // U
1470    let Inst{11-0}  = addr{11-0};  // imm12
1471  }
1472
1473  def i8 : T2Ii8<(outs), (ins t2addrmode_negimm8:$addr), IIC_Preload, opc,
1474                "\t$addr",
1475            [(ARMPreload t2addrmode_negimm8:$addr, (i32 write), (i32 instr))]> {
1476    let Inst{31-25} = 0b1111100;
1477    let Inst{24} = instr;
1478    let Inst{23} = 0; // U = 0
1479    let Inst{22} = 0;
1480    let Inst{21} = write;
1481    let Inst{20} = 1;
1482    let Inst{15-12} = 0b1111;
1483    let Inst{11-8} = 0b1100;
1484
1485    bits<13> addr;
1486    let Inst{19-16} = addr{12-9}; // Rn
1487    let Inst{7-0}   = addr{7-0};  // imm8
1488  }
1489
1490  def s : T2Iso<(outs), (ins t2addrmode_so_reg:$addr), IIC_Preload, opc,
1491               "\t$addr",
1492             [(ARMPreload t2addrmode_so_reg:$addr, (i32 write), (i32 instr))]> {
1493    let Inst{31-25} = 0b1111100;
1494    let Inst{24} = instr;
1495    let Inst{23} = 0; // add = TRUE for T1
1496    let Inst{22} = 0;
1497    let Inst{21} = write;
1498    let Inst{20} = 1;
1499    let Inst{15-12} = 0b1111;
1500    let Inst{11-6} = 0000000;
1501
1502    bits<10> addr;
1503    let Inst{19-16} = addr{9-6}; // Rn
1504    let Inst{3-0}   = addr{5-2}; // Rm
1505    let Inst{5-4}   = addr{1-0}; // imm2
1506
1507    let DecoderMethod = "DecodeT2LoadShift";
1508  }
1509}
1510
1511defm t2PLD  : T2Ipl<0, 0, "pld">,  Requires<[IsThumb2]>;
1512defm t2PLDW : T2Ipl<1, 0, "pldw">, Requires<[IsThumb2,HasV7,HasMP]>;
1513defm t2PLI  : T2Ipl<0, 1, "pli">,  Requires<[IsThumb2,HasV7]>;
1514
1515//===----------------------------------------------------------------------===//
1516//  Load / store multiple Instructions.
1517//
1518
1519multiclass thumb2_ld_mult<string asm, InstrItinClass itin,
1520                            InstrItinClass itin_upd, bit L_bit> {
1521  def IA :
1522    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1523         itin, !strconcat(asm, "${p}.w\t$Rn, $regs"), []> {
1524    bits<4>  Rn;
1525    bits<16> regs;
1526
1527    let Inst{31-27} = 0b11101;
1528    let Inst{26-25} = 0b00;
1529    let Inst{24-23} = 0b01;     // Increment After
1530    let Inst{22}    = 0;
1531    let Inst{21}    = 0;        // No writeback
1532    let Inst{20}    = L_bit;
1533    let Inst{19-16} = Rn;
1534    let Inst{15}    = 0;
1535    let Inst{14-0}  = regs{14-0};
1536  }
1537  def IA_UPD :
1538    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1539          itin_upd, !strconcat(asm, "${p}.w\t$Rn!, $regs"), "$Rn = $wb", []> {
1540    bits<4>  Rn;
1541    bits<16> regs;
1542
1543    let Inst{31-27} = 0b11101;
1544    let Inst{26-25} = 0b00;
1545    let Inst{24-23} = 0b01;     // Increment After
1546    let Inst{22}    = 0;
1547    let Inst{21}    = 1;        // Writeback
1548    let Inst{20}    = L_bit;
1549    let Inst{19-16} = Rn;
1550    let Inst{15}    = 0;
1551    let Inst{14-0}  = regs{14-0};
1552  }
1553  def DB :
1554    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1555         itin, !strconcat(asm, "db${p}\t$Rn, $regs"), []> {
1556    bits<4>  Rn;
1557    bits<16> regs;
1558
1559    let Inst{31-27} = 0b11101;
1560    let Inst{26-25} = 0b00;
1561    let Inst{24-23} = 0b10;     // Decrement Before
1562    let Inst{22}    = 0;
1563    let Inst{21}    = 0;        // No writeback
1564    let Inst{20}    = L_bit;
1565    let Inst{19-16} = Rn;
1566    let Inst{15}    = 0;
1567    let Inst{14-0}  = regs{14-0};
1568  }
1569  def DB_UPD :
1570    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1571          itin_upd, !strconcat(asm, "db${p}\t$Rn!, $regs"), "$Rn = $wb", []> {
1572    bits<4>  Rn;
1573    bits<16> regs;
1574
1575    let Inst{31-27} = 0b11101;
1576    let Inst{26-25} = 0b00;
1577    let Inst{24-23} = 0b10;     // Decrement Before
1578    let Inst{22}    = 0;
1579    let Inst{21}    = 1;        // Writeback
1580    let Inst{20}    = L_bit;
1581    let Inst{19-16} = Rn;
1582    let Inst{15}    = 0;
1583    let Inst{14-0}  = regs{14-0};
1584  }
1585}
1586
1587let neverHasSideEffects = 1 in {
1588
1589let mayLoad = 1, hasExtraDefRegAllocReq = 1 in
1590defm t2LDM : thumb2_ld_mult<"ldm", IIC_iLoad_m, IIC_iLoad_mu, 1>;
1591
1592multiclass thumb2_st_mult<string asm, InstrItinClass itin,
1593                            InstrItinClass itin_upd, bit L_bit> {
1594  def IA :
1595    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1596         itin, !strconcat(asm, "${p}.w\t$Rn, $regs"), []> {
1597    bits<4>  Rn;
1598    bits<16> regs;
1599
1600    let Inst{31-27} = 0b11101;
1601    let Inst{26-25} = 0b00;
1602    let Inst{24-23} = 0b01;     // Increment After
1603    let Inst{22}    = 0;
1604    let Inst{21}    = 0;        // No writeback
1605    let Inst{20}    = L_bit;
1606    let Inst{19-16} = Rn;
1607    let Inst{15}    = 0;
1608    let Inst{14}    = regs{14};
1609    let Inst{13}    = 0;
1610    let Inst{12-0}  = regs{12-0};
1611  }
1612  def IA_UPD :
1613    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1614          itin_upd, !strconcat(asm, "${p}.w\t$Rn!, $regs"), "$Rn = $wb", []> {
1615    bits<4>  Rn;
1616    bits<16> regs;
1617
1618    let Inst{31-27} = 0b11101;
1619    let Inst{26-25} = 0b00;
1620    let Inst{24-23} = 0b01;     // Increment After
1621    let Inst{22}    = 0;
1622    let Inst{21}    = 1;        // Writeback
1623    let Inst{20}    = L_bit;
1624    let Inst{19-16} = Rn;
1625    let Inst{15}    = 0;
1626    let Inst{14}    = regs{14};
1627    let Inst{13}    = 0;
1628    let Inst{12-0}  = regs{12-0};
1629  }
1630  def DB :
1631    T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1632         itin, !strconcat(asm, "db${p}\t$Rn, $regs"), []> {
1633    bits<4>  Rn;
1634    bits<16> regs;
1635
1636    let Inst{31-27} = 0b11101;
1637    let Inst{26-25} = 0b00;
1638    let Inst{24-23} = 0b10;     // Decrement Before
1639    let Inst{22}    = 0;
1640    let Inst{21}    = 0;        // No writeback
1641    let Inst{20}    = L_bit;
1642    let Inst{19-16} = Rn;
1643    let Inst{15}    = 0;
1644    let Inst{14}    = regs{14};
1645    let Inst{13}    = 0;
1646    let Inst{12-0}  = regs{12-0};
1647  }
1648  def DB_UPD :
1649    T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1650          itin_upd, !strconcat(asm, "db${p}\t$Rn!, $regs"), "$Rn = $wb", []> {
1651    bits<4>  Rn;
1652    bits<16> regs;
1653
1654    let Inst{31-27} = 0b11101;
1655    let Inst{26-25} = 0b00;
1656    let Inst{24-23} = 0b10;     // Decrement Before
1657    let Inst{22}    = 0;
1658    let Inst{21}    = 1;        // Writeback
1659    let Inst{20}    = L_bit;
1660    let Inst{19-16} = Rn;
1661    let Inst{15}    = 0;
1662    let Inst{14}    = regs{14};
1663    let Inst{13}    = 0;
1664    let Inst{12-0}  = regs{12-0};
1665  }
1666}
1667
1668
1669let mayStore = 1, hasExtraSrcRegAllocReq = 1 in
1670defm t2STM : thumb2_st_mult<"stm", IIC_iStore_m, IIC_iStore_mu, 0>;
1671
1672} // neverHasSideEffects
1673
1674
1675//===----------------------------------------------------------------------===//
1676//  Move Instructions.
1677//
1678
1679let neverHasSideEffects = 1 in
1680def t2MOVr : T2sTwoReg<(outs GPRnopc:$Rd), (ins GPR:$Rm), IIC_iMOVr,
1681                   "mov", ".w\t$Rd, $Rm", []> {
1682  let Inst{31-27} = 0b11101;
1683  let Inst{26-25} = 0b01;
1684  let Inst{24-21} = 0b0010;
1685  let Inst{19-16} = 0b1111; // Rn
1686  let Inst{14-12} = 0b000;
1687  let Inst{7-4} = 0b0000;
1688}
1689def : t2InstAlias<"movs${p}.w $Rd, $Rm", (t2MOVr GPRnopc:$Rd, GPR:$Rm,
1690                                                 pred:$p, CPSR)>;
1691def : t2InstAlias<"movs${p} $Rd, $Rm", (t2MOVr GPRnopc:$Rd, GPR:$Rm,
1692                                               pred:$p, CPSR)>;
1693
1694// AddedComplexity to ensure isel tries t2MOVi before t2MOVi16.
1695let isReMaterializable = 1, isAsCheapAsAMove = 1, isMoveImm = 1,
1696    AddedComplexity = 1 in
1697def t2MOVi : T2sOneRegImm<(outs rGPR:$Rd), (ins t2_so_imm:$imm), IIC_iMOVi,
1698                   "mov", ".w\t$Rd, $imm",
1699                   [(set rGPR:$Rd, t2_so_imm:$imm)]> {
1700  let Inst{31-27} = 0b11110;
1701  let Inst{25} = 0;
1702  let Inst{24-21} = 0b0010;
1703  let Inst{19-16} = 0b1111; // Rn
1704  let Inst{15} = 0;
1705}
1706
1707// cc_out is handled as part of the explicit mnemonic in the parser for 'mov'.
1708// Use aliases to get that to play nice here.
1709def : t2InstAlias<"movs${p}.w $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
1710                                                pred:$p, CPSR)>;
1711def : t2InstAlias<"movs${p} $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
1712                                                pred:$p, CPSR)>;
1713
1714def : t2InstAlias<"mov${p}.w $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
1715                                                 pred:$p, zero_reg)>;
1716def : t2InstAlias<"mov${p} $Rd, $imm", (t2MOVi rGPR:$Rd, t2_so_imm:$imm,
1717                                               pred:$p, zero_reg)>;
1718
1719let isReMaterializable = 1, isAsCheapAsAMove = 1, isMoveImm = 1 in
1720def t2MOVi16 : T2I<(outs rGPR:$Rd), (ins imm0_65535_expr:$imm), IIC_iMOVi,
1721                   "movw", "\t$Rd, $imm",
1722                   [(set rGPR:$Rd, imm0_65535:$imm)]> {
1723  let Inst{31-27} = 0b11110;
1724  let Inst{25} = 1;
1725  let Inst{24-21} = 0b0010;
1726  let Inst{20} = 0; // The S bit.
1727  let Inst{15} = 0;
1728
1729  bits<4> Rd;
1730  bits<16> imm;
1731
1732  let Inst{11-8}  = Rd;
1733  let Inst{19-16} = imm{15-12};
1734  let Inst{26}    = imm{11};
1735  let Inst{14-12} = imm{10-8};
1736  let Inst{7-0}   = imm{7-0};
1737}
1738
1739def t2MOVi16_ga_pcrel : PseudoInst<(outs rGPR:$Rd),
1740                                (ins i32imm:$addr, pclabel:$id), IIC_iMOVi, []>;
1741
1742let Constraints = "$src = $Rd" in {
1743def t2MOVTi16 : T2I<(outs rGPR:$Rd),
1744                    (ins rGPR:$src, imm0_65535_expr:$imm), IIC_iMOVi,
1745                    "movt", "\t$Rd, $imm",
1746                    [(set rGPR:$Rd,
1747                          (or (and rGPR:$src, 0xffff), lo16AllZero:$imm))]> {
1748  let Inst{31-27} = 0b11110;
1749  let Inst{25} = 1;
1750  let Inst{24-21} = 0b0110;
1751  let Inst{20} = 0; // The S bit.
1752  let Inst{15} = 0;
1753
1754  bits<4> Rd;
1755  bits<16> imm;
1756
1757  let Inst{11-8}  = Rd;
1758  let Inst{19-16} = imm{15-12};
1759  let Inst{26}    = imm{11};
1760  let Inst{14-12} = imm{10-8};
1761  let Inst{7-0}   = imm{7-0};
1762}
1763
1764def t2MOVTi16_ga_pcrel : PseudoInst<(outs rGPR:$Rd),
1765                     (ins rGPR:$src, i32imm:$addr, pclabel:$id), IIC_iMOVi, []>;
1766} // Constraints
1767
1768def : T2Pat<(or rGPR:$src, 0xffff0000), (t2MOVTi16 rGPR:$src, 0xffff)>;
1769
1770//===----------------------------------------------------------------------===//
1771//  Extend Instructions.
1772//
1773
1774// Sign extenders
1775
1776def t2SXTB  : T2I_ext_rrot<0b100, "sxtb",
1777                              UnOpFrag<(sext_inreg node:$Src, i8)>>;
1778def t2SXTH  : T2I_ext_rrot<0b000, "sxth",
1779                              UnOpFrag<(sext_inreg node:$Src, i16)>>;
1780def t2SXTB16 : T2I_ext_rrot_sxtb16<0b010, "sxtb16">;
1781
1782def t2SXTAB : T2I_exta_rrot<0b100, "sxtab",
1783                        BinOpFrag<(add node:$LHS, (sext_inreg node:$RHS, i8))>>;
1784def t2SXTAH : T2I_exta_rrot<0b000, "sxtah",
1785                        BinOpFrag<(add node:$LHS, (sext_inreg node:$RHS,i16))>>;
1786def t2SXTAB16 : T2I_exta_rrot_np<0b010, "sxtab16">;
1787
1788// Zero extenders
1789
1790let AddedComplexity = 16 in {
1791def t2UXTB   : T2I_ext_rrot<0b101, "uxtb",
1792                               UnOpFrag<(and node:$Src, 0x000000FF)>>;
1793def t2UXTH   : T2I_ext_rrot<0b001, "uxth",
1794                               UnOpFrag<(and node:$Src, 0x0000FFFF)>>;
1795def t2UXTB16 : T2I_ext_rrot_uxtb16<0b011, "uxtb16",
1796                               UnOpFrag<(and node:$Src, 0x00FF00FF)>>;
1797
1798// FIXME: This pattern incorrectly assumes the shl operator is a rotate.
1799//        The transformation should probably be done as a combiner action
1800//        instead so we can include a check for masking back in the upper
1801//        eight bits of the source into the lower eight bits of the result.
1802//def : T2Pat<(and (shl rGPR:$Src, (i32 8)), 0xFF00FF),
1803//            (t2UXTB16 rGPR:$Src, 3)>,
1804//          Requires<[HasT2ExtractPack, IsThumb2]>;
1805def : T2Pat<(and (srl rGPR:$Src, (i32 8)), 0xFF00FF),
1806            (t2UXTB16 rGPR:$Src, 1)>,
1807        Requires<[HasT2ExtractPack, IsThumb2]>;
1808
1809def t2UXTAB : T2I_exta_rrot<0b101, "uxtab",
1810                           BinOpFrag<(add node:$LHS, (and node:$RHS, 0x00FF))>>;
1811def t2UXTAH : T2I_exta_rrot<0b001, "uxtah",
1812                           BinOpFrag<(add node:$LHS, (and node:$RHS, 0xFFFF))>>;
1813def t2UXTAB16 : T2I_exta_rrot_np<0b011, "uxtab16">;
1814}
1815
1816//===----------------------------------------------------------------------===//
1817//  Arithmetic Instructions.
1818//
1819
1820defm t2ADD  : T2I_bin_ii12rs<0b000, "add",
1821                             BinOpFrag<(add  node:$LHS, node:$RHS)>, 1>;
1822defm t2SUB  : T2I_bin_ii12rs<0b101, "sub",
1823                             BinOpFrag<(sub  node:$LHS, node:$RHS)>>;
1824
1825// ADD and SUB with 's' bit set. No 12-bit immediate (T4) variants.
1826// FIXME: Eliminate them if we can write def : Pat patterns which defines
1827// CPSR and the implicit def of CPSR is not needed.
1828defm t2ADDS : T2I_bin_s_irs <0b1000, "add",
1829                             IIC_iALUi, IIC_iALUr, IIC_iALUsi,
1830                             BinOpFrag<(ARMaddc node:$LHS, node:$RHS)>, 1>;
1831defm t2SUBS : T2I_bin_s_irs <0b1101, "sub",
1832                             IIC_iALUi, IIC_iALUr, IIC_iALUsi,
1833                             BinOpFrag<(ARMsubc node:$LHS, node:$RHS)>>;
1834
1835let hasPostISelHook = 1 in {
1836defm t2ADC  : T2I_adde_sube_irs<0b1010, "adc",
1837              BinOpWithFlagFrag<(ARMadde node:$LHS, node:$RHS, node:$FLAG)>, 1>;
1838defm t2SBC  : T2I_adde_sube_irs<0b1011, "sbc",
1839              BinOpWithFlagFrag<(ARMsube node:$LHS, node:$RHS, node:$FLAG)>>;
1840}
1841
1842// RSB
1843defm t2RSB  : T2I_rbin_irs  <0b1110, "rsb",
1844                             BinOpFrag<(sub  node:$LHS, node:$RHS)>>;
1845
1846// FIXME: Eliminate them if we can write def : Pat patterns which defines
1847// CPSR and the implicit def of CPSR is not needed.
1848defm t2RSBS : T2I_rbin_s_is <0b1110, "rsb",
1849                             BinOpFrag<(ARMsubc node:$LHS, node:$RHS)>>;
1850
1851// (sub X, imm) gets canonicalized to (add X, -imm).  Match this form.
1852// The assume-no-carry-in form uses the negation of the input since add/sub
1853// assume opposite meanings of the carry flag (i.e., carry == !borrow).
1854// See the definition of AddWithCarry() in the ARM ARM A2.2.1 for the gory
1855// details.
1856// The AddedComplexity preferences the first variant over the others since
1857// it can be shrunk to a 16-bit wide encoding, while the others cannot.
1858let AddedComplexity = 1 in
1859def : T2Pat<(add        GPR:$src, imm0_255_neg:$imm),
1860            (t2SUBri    GPR:$src, imm0_255_neg:$imm)>;
1861def : T2Pat<(add        GPR:$src, t2_so_imm_neg:$imm),
1862            (t2SUBri    GPR:$src, t2_so_imm_neg:$imm)>;
1863def : T2Pat<(add        GPR:$src, imm0_4095_neg:$imm),
1864            (t2SUBri12  GPR:$src, imm0_4095_neg:$imm)>;
1865let AddedComplexity = 1 in
1866def : T2Pat<(ARMaddc    rGPR:$src, imm0_255_neg:$imm),
1867            (t2SUBSri   rGPR:$src, imm0_255_neg:$imm)>;
1868def : T2Pat<(ARMaddc    rGPR:$src, t2_so_imm_neg:$imm),
1869            (t2SUBSri   rGPR:$src, t2_so_imm_neg:$imm)>;
1870// The with-carry-in form matches bitwise not instead of the negation.
1871// Effectively, the inverse interpretation of the carry flag already accounts
1872// for part of the negation.
1873let AddedComplexity = 1 in
1874def : T2Pat<(ARMadde    rGPR:$src, imm0_255_not:$imm, CPSR),
1875            (t2SBCri    rGPR:$src, imm0_255_not:$imm)>;
1876def : T2Pat<(ARMadde    rGPR:$src, t2_so_imm_not:$imm, CPSR),
1877            (t2SBCri    rGPR:$src, t2_so_imm_not:$imm)>;
1878
1879// Select Bytes -- for disassembly only
1880
1881def t2SEL : T2ThreeReg<(outs GPR:$Rd), (ins GPR:$Rn, GPR:$Rm),
1882                NoItinerary, "sel", "\t$Rd, $Rn, $Rm", []>,
1883          Requires<[IsThumb2, HasThumb2DSP]> {
1884  let Inst{31-27} = 0b11111;
1885  let Inst{26-24} = 0b010;
1886  let Inst{23} = 0b1;
1887  let Inst{22-20} = 0b010;
1888  let Inst{15-12} = 0b1111;
1889  let Inst{7} = 0b1;
1890  let Inst{6-4} = 0b000;
1891}
1892
1893// A6.3.13, A6.3.14, A6.3.15 Parallel addition and subtraction (signed/unsigned)
1894// And Miscellaneous operations -- for disassembly only
1895class T2I_pam<bits<3> op22_20, bits<4> op7_4, string opc,
1896              list<dag> pat = [/* For disassembly only; pattern left blank */],
1897              dag iops = (ins rGPR:$Rn, rGPR:$Rm),
1898              string asm = "\t$Rd, $Rn, $Rm">
1899  : T2I<(outs rGPR:$Rd), iops, NoItinerary, opc, asm, pat>,
1900    Requires<[IsThumb2, HasThumb2DSP]> {
1901  let Inst{31-27} = 0b11111;
1902  let Inst{26-23} = 0b0101;
1903  let Inst{22-20} = op22_20;
1904  let Inst{15-12} = 0b1111;
1905  let Inst{7-4} = op7_4;
1906
1907  bits<4> Rd;
1908  bits<4> Rn;
1909  bits<4> Rm;
1910
1911  let Inst{11-8}  = Rd;
1912  let Inst{19-16} = Rn;
1913  let Inst{3-0}   = Rm;
1914}
1915
1916// Saturating add/subtract -- for disassembly only
1917
1918def t2QADD    : T2I_pam<0b000, 0b1000, "qadd",
1919                        [(set rGPR:$Rd, (int_arm_qadd rGPR:$Rn, rGPR:$Rm))],
1920                        (ins rGPR:$Rm, rGPR:$Rn), "\t$Rd, $Rm, $Rn">;
1921def t2QADD16  : T2I_pam<0b001, 0b0001, "qadd16">;
1922def t2QADD8   : T2I_pam<0b000, 0b0001, "qadd8">;
1923def t2QASX    : T2I_pam<0b010, 0b0001, "qasx">;
1924def t2QDADD   : T2I_pam<0b000, 0b1001, "qdadd", [],
1925                        (ins rGPR:$Rm, rGPR:$Rn), "\t$Rd, $Rm, $Rn">;
1926def t2QDSUB   : T2I_pam<0b000, 0b1011, "qdsub", [],
1927                        (ins rGPR:$Rm, rGPR:$Rn), "\t$Rd, $Rm, $Rn">;
1928def t2QSAX    : T2I_pam<0b110, 0b0001, "qsax">;
1929def t2QSUB    : T2I_pam<0b000, 0b1010, "qsub",
1930                        [(set rGPR:$Rd, (int_arm_qsub rGPR:$Rn, rGPR:$Rm))],
1931                        (ins rGPR:$Rm, rGPR:$Rn), "\t$Rd, $Rm, $Rn">;
1932def t2QSUB16  : T2I_pam<0b101, 0b0001, "qsub16">;
1933def t2QSUB8   : T2I_pam<0b100, 0b0001, "qsub8">;
1934def t2UQADD16 : T2I_pam<0b001, 0b0101, "uqadd16">;
1935def t2UQADD8  : T2I_pam<0b000, 0b0101, "uqadd8">;
1936def t2UQASX   : T2I_pam<0b010, 0b0101, "uqasx">;
1937def t2UQSAX   : T2I_pam<0b110, 0b0101, "uqsax">;
1938def t2UQSUB16 : T2I_pam<0b101, 0b0101, "uqsub16">;
1939def t2UQSUB8  : T2I_pam<0b100, 0b0101, "uqsub8">;
1940
1941// Signed/Unsigned add/subtract -- for disassembly only
1942
1943def t2SASX    : T2I_pam<0b010, 0b0000, "sasx">;
1944def t2SADD16  : T2I_pam<0b001, 0b0000, "sadd16">;
1945def t2SADD8   : T2I_pam<0b000, 0b0000, "sadd8">;
1946def t2SSAX    : T2I_pam<0b110, 0b0000, "ssax">;
1947def t2SSUB16  : T2I_pam<0b101, 0b0000, "ssub16">;
1948def t2SSUB8   : T2I_pam<0b100, 0b0000, "ssub8">;
1949def t2UASX    : T2I_pam<0b010, 0b0100, "uasx">;
1950def t2UADD16  : T2I_pam<0b001, 0b0100, "uadd16">;
1951def t2UADD8   : T2I_pam<0b000, 0b0100, "uadd8">;
1952def t2USAX    : T2I_pam<0b110, 0b0100, "usax">;
1953def t2USUB16  : T2I_pam<0b101, 0b0100, "usub16">;
1954def t2USUB8   : T2I_pam<0b100, 0b0100, "usub8">;
1955
1956// Signed/Unsigned halving add/subtract -- for disassembly only
1957
1958def t2SHASX   : T2I_pam<0b010, 0b0010, "shasx">;
1959def t2SHADD16 : T2I_pam<0b001, 0b0010, "shadd16">;
1960def t2SHADD8  : T2I_pam<0b000, 0b0010, "shadd8">;
1961def t2SHSAX   : T2I_pam<0b110, 0b0010, "shsax">;
1962def t2SHSUB16 : T2I_pam<0b101, 0b0010, "shsub16">;
1963def t2SHSUB8  : T2I_pam<0b100, 0b0010, "shsub8">;
1964def t2UHASX   : T2I_pam<0b010, 0b0110, "uhasx">;
1965def t2UHADD16 : T2I_pam<0b001, 0b0110, "uhadd16">;
1966def t2UHADD8  : T2I_pam<0b000, 0b0110, "uhadd8">;
1967def t2UHSAX   : T2I_pam<0b110, 0b0110, "uhsax">;
1968def t2UHSUB16 : T2I_pam<0b101, 0b0110, "uhsub16">;
1969def t2UHSUB8  : T2I_pam<0b100, 0b0110, "uhsub8">;
1970
1971// Helper class for disassembly only
1972// A6.3.16 & A6.3.17
1973// T2Imac - Thumb2 multiply [accumulate, and absolute difference] instructions.
1974class T2ThreeReg_mac<bit long, bits<3> op22_20, bits<4> op7_4, dag oops,
1975  dag iops, InstrItinClass itin, string opc, string asm, list<dag> pattern>
1976  : T2ThreeReg<oops, iops, itin, opc, asm, pattern> {
1977  let Inst{31-27} = 0b11111;
1978  let Inst{26-24} = 0b011;
1979  let Inst{23}    = long;
1980  let Inst{22-20} = op22_20;
1981  let Inst{7-4}   = op7_4;
1982}
1983
1984class T2FourReg_mac<bit long, bits<3> op22_20, bits<4> op7_4, dag oops,
1985  dag iops, InstrItinClass itin, string opc, string asm, list<dag> pattern>
1986  : T2FourReg<oops, iops, itin, opc, asm, pattern> {
1987  let Inst{31-27} = 0b11111;
1988  let Inst{26-24} = 0b011;
1989  let Inst{23}    = long;
1990  let Inst{22-20} = op22_20;
1991  let Inst{7-4}   = op7_4;
1992}
1993
1994// Unsigned Sum of Absolute Differences [and Accumulate] -- for disassembly only
1995
1996def t2USAD8   : T2ThreeReg_mac<0, 0b111, 0b0000, (outs rGPR:$Rd),
1997                                           (ins rGPR:$Rn, rGPR:$Rm),
1998                        NoItinerary, "usad8", "\t$Rd, $Rn, $Rm", []>,
1999          Requires<[IsThumb2, HasThumb2DSP]> {
2000  let Inst{15-12} = 0b1111;
2001}
2002def t2USADA8  : T2FourReg_mac<0, 0b111, 0b0000, (outs rGPR:$Rd),
2003                       (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), NoItinerary,
2004                        "usada8", "\t$Rd, $Rn, $Rm, $Ra", []>,
2005          Requires<[IsThumb2, HasThumb2DSP]>;
2006
2007// Signed/Unsigned saturate -- for disassembly only
2008
2009class T2SatI<dag oops, dag iops, InstrItinClass itin,
2010           string opc, string asm, list<dag> pattern>
2011  : T2I<oops, iops, itin, opc, asm, pattern> {
2012  bits<4> Rd;
2013  bits<4> Rn;
2014  bits<5> sat_imm;
2015  bits<7> sh;
2016
2017  let Inst{11-8}  = Rd;
2018  let Inst{19-16} = Rn;
2019  let Inst{4-0}   = sat_imm;
2020  let Inst{21}    = sh{5};
2021  let Inst{14-12} = sh{4-2};
2022  let Inst{7-6}   = sh{1-0};
2023}
2024
2025def t2SSAT: T2SatI<
2026              (outs rGPR:$Rd), (ins imm1_32:$sat_imm, rGPR:$Rn, shift_imm:$sh),
2027              NoItinerary, "ssat", "\t$Rd, $sat_imm, $Rn$sh",
2028              [/* For disassembly only; pattern left blank */]> {
2029  let Inst{31-27} = 0b11110;
2030  let Inst{25-22} = 0b1100;
2031  let Inst{20} = 0;
2032  let Inst{15} = 0;
2033  let Inst{5}  = 0;
2034}
2035
2036def t2SSAT16: T2SatI<
2037                (outs rGPR:$Rd), (ins imm1_16:$sat_imm, rGPR:$Rn), NoItinerary,
2038                "ssat16", "\t$Rd, $sat_imm, $Rn",
2039                [/* For disassembly only; pattern left blank */]>,
2040          Requires<[IsThumb2, HasThumb2DSP]> {
2041  let Inst{31-27} = 0b11110;
2042  let Inst{25-22} = 0b1100;
2043  let Inst{20} = 0;
2044  let Inst{15} = 0;
2045  let Inst{21} = 1;        // sh = '1'
2046  let Inst{14-12} = 0b000; // imm3 = '000'
2047  let Inst{7-6} = 0b00;    // imm2 = '00'
2048  let Inst{5-4} = 0b00;
2049}
2050
2051def t2USAT: T2SatI<
2052               (outs rGPR:$Rd), (ins imm0_31:$sat_imm, rGPR:$Rn, shift_imm:$sh),
2053                NoItinerary, "usat", "\t$Rd, $sat_imm, $Rn$sh",
2054                [/* For disassembly only; pattern left blank */]> {
2055  let Inst{31-27} = 0b11110;
2056  let Inst{25-22} = 0b1110;
2057  let Inst{20} = 0;
2058  let Inst{15} = 0;
2059}
2060
2061def t2USAT16: T2SatI<(outs rGPR:$Rd), (ins imm0_15:$sat_imm, rGPR:$Rn),
2062                     NoItinerary,
2063                     "usat16", "\t$Rd, $sat_imm, $Rn",
2064                     [/* For disassembly only; pattern left blank */]>,
2065          Requires<[IsThumb2, HasThumb2DSP]> {
2066  let Inst{31-27} = 0b11110;
2067  let Inst{25-22} = 0b1110;
2068  let Inst{20} = 0;
2069  let Inst{15} = 0;
2070  let Inst{21} = 1;        // sh = '1'
2071  let Inst{14-12} = 0b000; // imm3 = '000'
2072  let Inst{7-6} = 0b00;    // imm2 = '00'
2073}
2074
2075def : T2Pat<(int_arm_ssat GPR:$a, imm:$pos), (t2SSAT imm:$pos, GPR:$a, 0)>;
2076def : T2Pat<(int_arm_usat GPR:$a, imm:$pos), (t2USAT imm:$pos, GPR:$a, 0)>;
2077
2078//===----------------------------------------------------------------------===//
2079//  Shift and rotate Instructions.
2080//
2081
2082defm t2LSL  : T2I_sh_ir<0b00, "lsl", imm0_31,
2083                        BinOpFrag<(shl  node:$LHS, node:$RHS)>, "t2LSL">;
2084defm t2LSR  : T2I_sh_ir<0b01, "lsr", imm_sr,
2085                        BinOpFrag<(srl  node:$LHS, node:$RHS)>, "t2LSR">;
2086defm t2ASR  : T2I_sh_ir<0b10, "asr", imm_sr,
2087                        BinOpFrag<(sra  node:$LHS, node:$RHS)>, "t2ASR">;
2088defm t2ROR  : T2I_sh_ir<0b11, "ror", imm0_31,
2089                        BinOpFrag<(rotr node:$LHS, node:$RHS)>, "t2ROR">;
2090
2091// (rotr x, (and y, 0x...1f)) ==> (ROR x, y)
2092def : Pat<(rotr rGPR:$lhs, (and rGPR:$rhs, lo5AllOne)),
2093          (t2RORrr rGPR:$lhs, rGPR:$rhs)>;
2094
2095let Uses = [CPSR] in {
2096def t2RRX : T2sTwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iMOVsi,
2097                   "rrx", "\t$Rd, $Rm",
2098                   [(set rGPR:$Rd, (ARMrrx rGPR:$Rm))]> {
2099  let Inst{31-27} = 0b11101;
2100  let Inst{26-25} = 0b01;
2101  let Inst{24-21} = 0b0010;
2102  let Inst{19-16} = 0b1111; // Rn
2103  let Inst{14-12} = 0b000;
2104  let Inst{7-4} = 0b0011;
2105}
2106}
2107
2108let isCodeGenOnly = 1, Defs = [CPSR] in {
2109def t2MOVsrl_flag : T2TwoRegShiftImm<
2110                        (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iMOVsi,
2111                        "lsrs", ".w\t$Rd, $Rm, #1",
2112                        [(set rGPR:$Rd, (ARMsrl_flag rGPR:$Rm))]> {
2113  let Inst{31-27} = 0b11101;
2114  let Inst{26-25} = 0b01;
2115  let Inst{24-21} = 0b0010;
2116  let Inst{20} = 1; // The S bit.
2117  let Inst{19-16} = 0b1111; // Rn
2118  let Inst{5-4} = 0b01; // Shift type.
2119  // Shift amount = Inst{14-12:7-6} = 1.
2120  let Inst{14-12} = 0b000;
2121  let Inst{7-6} = 0b01;
2122}
2123def t2MOVsra_flag : T2TwoRegShiftImm<
2124                        (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iMOVsi,
2125                        "asrs", ".w\t$Rd, $Rm, #1",
2126                        [(set rGPR:$Rd, (ARMsra_flag rGPR:$Rm))]> {
2127  let Inst{31-27} = 0b11101;
2128  let Inst{26-25} = 0b01;
2129  let Inst{24-21} = 0b0010;
2130  let Inst{20} = 1; // The S bit.
2131  let Inst{19-16} = 0b1111; // Rn
2132  let Inst{5-4} = 0b10; // Shift type.
2133  // Shift amount = Inst{14-12:7-6} = 1.
2134  let Inst{14-12} = 0b000;
2135  let Inst{7-6} = 0b01;
2136}
2137}
2138
2139//===----------------------------------------------------------------------===//
2140//  Bitwise Instructions.
2141//
2142
2143defm t2AND  : T2I_bin_w_irs<0b0000, "and",
2144                            IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2145                            BinOpFrag<(and node:$LHS, node:$RHS)>, "t2AND", 1>;
2146defm t2ORR  : T2I_bin_w_irs<0b0010, "orr",
2147                            IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2148                            BinOpFrag<(or  node:$LHS, node:$RHS)>, "t2ORR", 1>;
2149defm t2EOR  : T2I_bin_w_irs<0b0100, "eor",
2150                            IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2151                            BinOpFrag<(xor node:$LHS, node:$RHS)>, "t2EOR", 1>;
2152
2153defm t2BIC  : T2I_bin_w_irs<0b0001, "bic",
2154                            IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2155                            BinOpFrag<(and node:$LHS, (not node:$RHS))>,
2156                            "t2BIC">;
2157
2158class T2BitFI<dag oops, dag iops, InstrItinClass itin,
2159              string opc, string asm, list<dag> pattern>
2160    : T2I<oops, iops, itin, opc, asm, pattern> {
2161  bits<4> Rd;
2162  bits<5> msb;
2163  bits<5> lsb;
2164
2165  let Inst{11-8}  = Rd;
2166  let Inst{4-0}   = msb{4-0};
2167  let Inst{14-12} = lsb{4-2};
2168  let Inst{7-6}   = lsb{1-0};
2169}
2170
2171class T2TwoRegBitFI<dag oops, dag iops, InstrItinClass itin,
2172              string opc, string asm, list<dag> pattern>
2173    : T2BitFI<oops, iops, itin, opc, asm, pattern> {
2174  bits<4> Rn;
2175
2176  let Inst{19-16} = Rn;
2177}
2178
2179let Constraints = "$src = $Rd" in
2180def t2BFC : T2BitFI<(outs rGPR:$Rd), (ins rGPR:$src, bf_inv_mask_imm:$imm),
2181                IIC_iUNAsi, "bfc", "\t$Rd, $imm",
2182                [(set rGPR:$Rd, (and rGPR:$src, bf_inv_mask_imm:$imm))]> {
2183  let Inst{31-27} = 0b11110;
2184  let Inst{26} = 0; // should be 0.
2185  let Inst{25} = 1;
2186  let Inst{24-20} = 0b10110;
2187  let Inst{19-16} = 0b1111; // Rn
2188  let Inst{15} = 0;
2189  let Inst{5} = 0; // should be 0.
2190
2191  bits<10> imm;
2192  let msb{4-0} = imm{9-5};
2193  let lsb{4-0} = imm{4-0};
2194}
2195
2196def t2SBFX: T2TwoRegBitFI<
2197                (outs rGPR:$Rd), (ins rGPR:$Rn, imm0_31:$lsb, imm1_32:$msb),
2198                 IIC_iUNAsi, "sbfx", "\t$Rd, $Rn, $lsb, $msb", []> {
2199  let Inst{31-27} = 0b11110;
2200  let Inst{25} = 1;
2201  let Inst{24-20} = 0b10100;
2202  let Inst{15} = 0;
2203}
2204
2205def t2UBFX: T2TwoRegBitFI<
2206                (outs rGPR:$Rd), (ins rGPR:$Rn, imm0_31:$lsb, imm1_32:$msb),
2207                 IIC_iUNAsi, "ubfx", "\t$Rd, $Rn, $lsb, $msb", []> {
2208  let Inst{31-27} = 0b11110;
2209  let Inst{25} = 1;
2210  let Inst{24-20} = 0b11100;
2211  let Inst{15} = 0;
2212}
2213
2214// A8.6.18  BFI - Bitfield insert (Encoding T1)
2215let Constraints = "$src = $Rd" in {
2216  def t2BFI : T2TwoRegBitFI<(outs rGPR:$Rd),
2217                  (ins rGPR:$src, rGPR:$Rn, bf_inv_mask_imm:$imm),
2218                  IIC_iBITi, "bfi", "\t$Rd, $Rn, $imm",
2219                  [(set rGPR:$Rd, (ARMbfi rGPR:$src, rGPR:$Rn,
2220                                   bf_inv_mask_imm:$imm))]> {
2221    let Inst{31-27} = 0b11110;
2222    let Inst{26} = 0; // should be 0.
2223    let Inst{25} = 1;
2224    let Inst{24-20} = 0b10110;
2225    let Inst{15} = 0;
2226    let Inst{5} = 0; // should be 0.
2227
2228    bits<10> imm;
2229    let msb{4-0} = imm{9-5};
2230    let lsb{4-0} = imm{4-0};
2231  }
2232}
2233
2234defm t2ORN  : T2I_bin_irs<0b0011, "orn",
2235                          IIC_iBITi, IIC_iBITr, IIC_iBITsi,
2236                          BinOpFrag<(or  node:$LHS, (not node:$RHS))>,
2237                          "t2ORN", 0, "">;
2238
2239/// T2I_un_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns for a
2240/// unary operation that produces a value. These are predicable and can be
2241/// changed to modify CPSR.
2242multiclass T2I_un_irs<bits<4> opcod, string opc,
2243                     InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
2244                      PatFrag opnode, bit Cheap = 0, bit ReMat = 0> {
2245   // shifted imm
2246   def i : T2sOneRegImm<(outs rGPR:$Rd), (ins t2_so_imm:$imm), iii,
2247                opc, "\t$Rd, $imm",
2248                [(set rGPR:$Rd, (opnode t2_so_imm:$imm))]> {
2249     let isAsCheapAsAMove = Cheap;
2250     let isReMaterializable = ReMat;
2251     let Inst{31-27} = 0b11110;
2252     let Inst{25} = 0;
2253     let Inst{24-21} = opcod;
2254     let Inst{19-16} = 0b1111; // Rn
2255     let Inst{15} = 0;
2256   }
2257   // register
2258   def r : T2sTwoReg<(outs rGPR:$Rd), (ins rGPR:$Rm), iir,
2259                opc, ".w\t$Rd, $Rm",
2260                [(set rGPR:$Rd, (opnode rGPR:$Rm))]> {
2261     let Inst{31-27} = 0b11101;
2262     let Inst{26-25} = 0b01;
2263     let Inst{24-21} = opcod;
2264     let Inst{19-16} = 0b1111; // Rn
2265     let Inst{14-12} = 0b000; // imm3
2266     let Inst{7-6} = 0b00; // imm2
2267     let Inst{5-4} = 0b00; // type
2268   }
2269   // shifted register
2270   def s : T2sOneRegShiftedReg<(outs rGPR:$Rd), (ins t2_so_reg:$ShiftedRm), iis,
2271                opc, ".w\t$Rd, $ShiftedRm",
2272                [(set rGPR:$Rd, (opnode t2_so_reg:$ShiftedRm))]> {
2273     let Inst{31-27} = 0b11101;
2274     let Inst{26-25} = 0b01;
2275     let Inst{24-21} = opcod;
2276     let Inst{19-16} = 0b1111; // Rn
2277   }
2278}
2279
2280// Prefer over of t2EORri ra, rb, -1 because mvn has 16-bit version
2281let AddedComplexity = 1 in
2282defm t2MVN  : T2I_un_irs <0b0011, "mvn",
2283                          IIC_iMVNi, IIC_iMVNr, IIC_iMVNsi,
2284                          UnOpFrag<(not node:$Src)>, 1, 1>;
2285
2286let AddedComplexity = 1 in
2287def : T2Pat<(and     rGPR:$src, t2_so_imm_not:$imm),
2288            (t2BICri rGPR:$src, t2_so_imm_not:$imm)>;
2289
2290// FIXME: Disable this pattern on Darwin to workaround an assembler bug.
2291def : T2Pat<(or      rGPR:$src, t2_so_imm_not:$imm),
2292            (t2ORNri rGPR:$src, t2_so_imm_not:$imm)>,
2293            Requires<[IsThumb2]>;
2294
2295def : T2Pat<(t2_so_imm_not:$src),
2296            (t2MVNi t2_so_imm_not:$src)>;
2297
2298//===----------------------------------------------------------------------===//
2299//  Multiply Instructions.
2300//
2301let isCommutable = 1 in
2302def t2MUL: T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL32,
2303                "mul", "\t$Rd, $Rn, $Rm",
2304                [(set rGPR:$Rd, (mul rGPR:$Rn, rGPR:$Rm))]> {
2305  let Inst{31-27} = 0b11111;
2306  let Inst{26-23} = 0b0110;
2307  let Inst{22-20} = 0b000;
2308  let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2309  let Inst{7-4} = 0b0000; // Multiply
2310}
2311
2312def t2MLA: T2FourReg<
2313                (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2314                "mla", "\t$Rd, $Rn, $Rm, $Ra",
2315                [(set rGPR:$Rd, (add (mul rGPR:$Rn, rGPR:$Rm), rGPR:$Ra))]> {
2316  let Inst{31-27} = 0b11111;
2317  let Inst{26-23} = 0b0110;
2318  let Inst{22-20} = 0b000;
2319  let Inst{7-4} = 0b0000; // Multiply
2320}
2321
2322def t2MLS: T2FourReg<
2323                (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2324                "mls", "\t$Rd, $Rn, $Rm, $Ra",
2325                [(set rGPR:$Rd, (sub rGPR:$Ra, (mul rGPR:$Rn, rGPR:$Rm)))]> {
2326  let Inst{31-27} = 0b11111;
2327  let Inst{26-23} = 0b0110;
2328  let Inst{22-20} = 0b000;
2329  let Inst{7-4} = 0b0001; // Multiply and Subtract
2330}
2331
2332// Extra precision multiplies with low / high results
2333let neverHasSideEffects = 1 in {
2334let isCommutable = 1 in {
2335def t2SMULL : T2MulLong<0b000, 0b0000,
2336                  (outs rGPR:$RdLo, rGPR:$RdHi),
2337                  (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL64,
2338                   "smull", "\t$RdLo, $RdHi, $Rn, $Rm", []>;
2339
2340def t2UMULL : T2MulLong<0b010, 0b0000,
2341                  (outs rGPR:$RdLo, rGPR:$RdHi),
2342                  (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL64,
2343                   "umull", "\t$RdLo, $RdHi, $Rn, $Rm", []>;
2344} // isCommutable
2345
2346// Multiply + accumulate
2347def t2SMLAL : T2MulLong<0b100, 0b0000,
2348                  (outs rGPR:$RdLo, rGPR:$RdHi),
2349                  (ins rGPR:$Rn, rGPR:$Rm), IIC_iMAC64,
2350                  "smlal", "\t$RdLo, $RdHi, $Rn, $Rm", []>;
2351
2352def t2UMLAL : T2MulLong<0b110, 0b0000,
2353                  (outs rGPR:$RdLo, rGPR:$RdHi),
2354                  (ins rGPR:$Rn, rGPR:$Rm), IIC_iMAC64,
2355                  "umlal", "\t$RdLo, $RdHi, $Rn, $Rm", []>;
2356
2357def t2UMAAL : T2MulLong<0b110, 0b0110,
2358                  (outs rGPR:$RdLo, rGPR:$RdHi),
2359                  (ins rGPR:$Rn, rGPR:$Rm), IIC_iMAC64,
2360                  "umaal", "\t$RdLo, $RdHi, $Rn, $Rm", []>,
2361          Requires<[IsThumb2, HasThumb2DSP]>;
2362} // neverHasSideEffects
2363
2364// Rounding variants of the below included for disassembly only
2365
2366// Most significant word multiply
2367def t2SMMUL : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL32,
2368                  "smmul", "\t$Rd, $Rn, $Rm",
2369                  [(set rGPR:$Rd, (mulhs rGPR:$Rn, rGPR:$Rm))]>,
2370          Requires<[IsThumb2, HasThumb2DSP]> {
2371  let Inst{31-27} = 0b11111;
2372  let Inst{26-23} = 0b0110;
2373  let Inst{22-20} = 0b101;
2374  let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2375  let Inst{7-4} = 0b0000; // No Rounding (Inst{4} = 0)
2376}
2377
2378def t2SMMULR : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL32,
2379                  "smmulr", "\t$Rd, $Rn, $Rm", []>,
2380          Requires<[IsThumb2, HasThumb2DSP]> {
2381  let Inst{31-27} = 0b11111;
2382  let Inst{26-23} = 0b0110;
2383  let Inst{22-20} = 0b101;
2384  let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2385  let Inst{7-4} = 0b0001; // Rounding (Inst{4} = 1)
2386}
2387
2388def t2SMMLA : T2FourReg<
2389        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2390                "smmla", "\t$Rd, $Rn, $Rm, $Ra",
2391                [(set rGPR:$Rd, (add (mulhs rGPR:$Rm, rGPR:$Rn), rGPR:$Ra))]>,
2392          Requires<[IsThumb2, HasThumb2DSP]> {
2393  let Inst{31-27} = 0b11111;
2394  let Inst{26-23} = 0b0110;
2395  let Inst{22-20} = 0b101;
2396  let Inst{7-4} = 0b0000; // No Rounding (Inst{4} = 0)
2397}
2398
2399def t2SMMLAR: T2FourReg<
2400        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2401                  "smmlar", "\t$Rd, $Rn, $Rm, $Ra", []>,
2402          Requires<[IsThumb2, HasThumb2DSP]> {
2403  let Inst{31-27} = 0b11111;
2404  let Inst{26-23} = 0b0110;
2405  let Inst{22-20} = 0b101;
2406  let Inst{7-4} = 0b0001; // Rounding (Inst{4} = 1)
2407}
2408
2409def t2SMMLS: T2FourReg<
2410        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2411                "smmls", "\t$Rd, $Rn, $Rm, $Ra",
2412                [(set rGPR:$Rd, (sub rGPR:$Ra, (mulhs rGPR:$Rn, rGPR:$Rm)))]>,
2413          Requires<[IsThumb2, HasThumb2DSP]> {
2414  let Inst{31-27} = 0b11111;
2415  let Inst{26-23} = 0b0110;
2416  let Inst{22-20} = 0b110;
2417  let Inst{7-4} = 0b0000; // No Rounding (Inst{4} = 0)
2418}
2419
2420def t2SMMLSR:T2FourReg<
2421        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32,
2422                "smmlsr", "\t$Rd, $Rn, $Rm, $Ra", []>,
2423          Requires<[IsThumb2, HasThumb2DSP]> {
2424  let Inst{31-27} = 0b11111;
2425  let Inst{26-23} = 0b0110;
2426  let Inst{22-20} = 0b110;
2427  let Inst{7-4} = 0b0001; // Rounding (Inst{4} = 1)
2428}
2429
2430multiclass T2I_smul<string opc, PatFrag opnode> {
2431  def BB : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16,
2432              !strconcat(opc, "bb"), "\t$Rd, $Rn, $Rm",
2433              [(set rGPR:$Rd, (opnode (sext_inreg rGPR:$Rn, i16),
2434                                      (sext_inreg rGPR:$Rm, i16)))]>,
2435          Requires<[IsThumb2, HasThumb2DSP]> {
2436    let Inst{31-27} = 0b11111;
2437    let Inst{26-23} = 0b0110;
2438    let Inst{22-20} = 0b001;
2439    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2440    let Inst{7-6} = 0b00;
2441    let Inst{5-4} = 0b00;
2442  }
2443
2444  def BT : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16,
2445              !strconcat(opc, "bt"), "\t$Rd, $Rn, $Rm",
2446              [(set rGPR:$Rd, (opnode (sext_inreg rGPR:$Rn, i16),
2447                                      (sra rGPR:$Rm, (i32 16))))]>,
2448          Requires<[IsThumb2, HasThumb2DSP]> {
2449    let Inst{31-27} = 0b11111;
2450    let Inst{26-23} = 0b0110;
2451    let Inst{22-20} = 0b001;
2452    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2453    let Inst{7-6} = 0b00;
2454    let Inst{5-4} = 0b01;
2455  }
2456
2457  def TB : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16,
2458              !strconcat(opc, "tb"), "\t$Rd, $Rn, $Rm",
2459              [(set rGPR:$Rd, (opnode (sra rGPR:$Rn, (i32 16)),
2460                                      (sext_inreg rGPR:$Rm, i16)))]>,
2461          Requires<[IsThumb2, HasThumb2DSP]> {
2462    let Inst{31-27} = 0b11111;
2463    let Inst{26-23} = 0b0110;
2464    let Inst{22-20} = 0b001;
2465    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2466    let Inst{7-6} = 0b00;
2467    let Inst{5-4} = 0b10;
2468  }
2469
2470  def TT : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16,
2471              !strconcat(opc, "tt"), "\t$Rd, $Rn, $Rm",
2472              [(set rGPR:$Rd, (opnode (sra rGPR:$Rn, (i32 16)),
2473                                      (sra rGPR:$Rm, (i32 16))))]>,
2474          Requires<[IsThumb2, HasThumb2DSP]> {
2475    let Inst{31-27} = 0b11111;
2476    let Inst{26-23} = 0b0110;
2477    let Inst{22-20} = 0b001;
2478    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2479    let Inst{7-6} = 0b00;
2480    let Inst{5-4} = 0b11;
2481  }
2482
2483  def WB : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16,
2484              !strconcat(opc, "wb"), "\t$Rd, $Rn, $Rm",
2485              [(set rGPR:$Rd, (sra (opnode rGPR:$Rn,
2486                                    (sext_inreg rGPR:$Rm, i16)), (i32 16)))]>,
2487          Requires<[IsThumb2, HasThumb2DSP]> {
2488    let Inst{31-27} = 0b11111;
2489    let Inst{26-23} = 0b0110;
2490    let Inst{22-20} = 0b011;
2491    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2492    let Inst{7-6} = 0b00;
2493    let Inst{5-4} = 0b00;
2494  }
2495
2496  def WT : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iMUL16,
2497              !strconcat(opc, "wt"), "\t$Rd, $Rn, $Rm",
2498              [(set rGPR:$Rd, (sra (opnode rGPR:$Rn,
2499                                    (sra rGPR:$Rm, (i32 16))), (i32 16)))]>,
2500          Requires<[IsThumb2, HasThumb2DSP]> {
2501    let Inst{31-27} = 0b11111;
2502    let Inst{26-23} = 0b0110;
2503    let Inst{22-20} = 0b011;
2504    let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2505    let Inst{7-6} = 0b00;
2506    let Inst{5-4} = 0b01;
2507  }
2508}
2509
2510
2511multiclass T2I_smla<string opc, PatFrag opnode> {
2512  def BB : T2FourReg<
2513        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC16,
2514              !strconcat(opc, "bb"), "\t$Rd, $Rn, $Rm, $Ra",
2515              [(set rGPR:$Rd, (add rGPR:$Ra,
2516                               (opnode (sext_inreg rGPR:$Rn, i16),
2517                                       (sext_inreg rGPR:$Rm, i16))))]>,
2518          Requires<[IsThumb2, HasThumb2DSP]> {
2519    let Inst{31-27} = 0b11111;
2520    let Inst{26-23} = 0b0110;
2521    let Inst{22-20} = 0b001;
2522    let Inst{7-6} = 0b00;
2523    let Inst{5-4} = 0b00;
2524  }
2525
2526  def BT : T2FourReg<
2527       (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC16,
2528             !strconcat(opc, "bt"), "\t$Rd, $Rn, $Rm, $Ra",
2529             [(set rGPR:$Rd, (add rGPR:$Ra, (opnode (sext_inreg rGPR:$Rn, i16),
2530                                                 (sra rGPR:$Rm, (i32 16)))))]>,
2531          Requires<[IsThumb2, HasThumb2DSP]> {
2532    let Inst{31-27} = 0b11111;
2533    let Inst{26-23} = 0b0110;
2534    let Inst{22-20} = 0b001;
2535    let Inst{7-6} = 0b00;
2536    let Inst{5-4} = 0b01;
2537  }
2538
2539  def TB : T2FourReg<
2540        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC16,
2541              !strconcat(opc, "tb"), "\t$Rd, $Rn, $Rm, $Ra",
2542              [(set rGPR:$Rd, (add rGPR:$Ra, (opnode (sra rGPR:$Rn, (i32 16)),
2543                                               (sext_inreg rGPR:$Rm, i16))))]>,
2544          Requires<[IsThumb2, HasThumb2DSP]> {
2545    let Inst{31-27} = 0b11111;
2546    let Inst{26-23} = 0b0110;
2547    let Inst{22-20} = 0b001;
2548    let Inst{7-6} = 0b00;
2549    let Inst{5-4} = 0b10;
2550  }
2551
2552  def TT : T2FourReg<
2553        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC16,
2554              !strconcat(opc, "tt"), "\t$Rd, $Rn, $Rm, $Ra",
2555             [(set rGPR:$Rd, (add rGPR:$Ra, (opnode (sra rGPR:$Rn, (i32 16)),
2556                                                 (sra rGPR:$Rm, (i32 16)))))]>,
2557          Requires<[IsThumb2, HasThumb2DSP]> {
2558    let Inst{31-27} = 0b11111;
2559    let Inst{26-23} = 0b0110;
2560    let Inst{22-20} = 0b001;
2561    let Inst{7-6} = 0b00;
2562    let Inst{5-4} = 0b11;
2563  }
2564
2565  def WB : T2FourReg<
2566        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC16,
2567              !strconcat(opc, "wb"), "\t$Rd, $Rn, $Rm, $Ra",
2568              [(set rGPR:$Rd, (add rGPR:$Ra, (sra (opnode rGPR:$Rn,
2569                                    (sext_inreg rGPR:$Rm, i16)), (i32 16))))]>,
2570          Requires<[IsThumb2, HasThumb2DSP]> {
2571    let Inst{31-27} = 0b11111;
2572    let Inst{26-23} = 0b0110;
2573    let Inst{22-20} = 0b011;
2574    let Inst{7-6} = 0b00;
2575    let Inst{5-4} = 0b00;
2576  }
2577
2578  def WT : T2FourReg<
2579        (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC16,
2580              !strconcat(opc, "wt"), "\t$Rd, $Rn, $Rm, $Ra",
2581              [(set rGPR:$Rd, (add rGPR:$Ra, (sra (opnode rGPR:$Rn,
2582                                      (sra rGPR:$Rm, (i32 16))), (i32 16))))]>,
2583          Requires<[IsThumb2, HasThumb2DSP]> {
2584    let Inst{31-27} = 0b11111;
2585    let Inst{26-23} = 0b0110;
2586    let Inst{22-20} = 0b011;
2587    let Inst{7-6} = 0b00;
2588    let Inst{5-4} = 0b01;
2589  }
2590}
2591
2592defm t2SMUL : T2I_smul<"smul", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
2593defm t2SMLA : T2I_smla<"smla", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
2594
2595// Halfword multiple accumulate long: SMLAL<x><y>
2596def t2SMLALBB : T2FourReg_mac<1, 0b100, 0b1000, (outs rGPR:$Ra,rGPR:$Rd),
2597         (ins rGPR:$Rn,rGPR:$Rm), IIC_iMAC64, "smlalbb", "\t$Ra, $Rd, $Rn, $Rm",
2598           [/* For disassembly only; pattern left blank */]>,
2599          Requires<[IsThumb2, HasThumb2DSP]>;
2600def t2SMLALBT : T2FourReg_mac<1, 0b100, 0b1001, (outs rGPR:$Ra,rGPR:$Rd),
2601         (ins rGPR:$Rn,rGPR:$Rm), IIC_iMAC64, "smlalbt", "\t$Ra, $Rd, $Rn, $Rm",
2602           [/* For disassembly only; pattern left blank */]>,
2603          Requires<[IsThumb2, HasThumb2DSP]>;
2604def t2SMLALTB : T2FourReg_mac<1, 0b100, 0b1010, (outs rGPR:$Ra,rGPR:$Rd),
2605         (ins rGPR:$Rn,rGPR:$Rm), IIC_iMAC64, "smlaltb", "\t$Ra, $Rd, $Rn, $Rm",
2606           [/* For disassembly only; pattern left blank */]>,
2607          Requires<[IsThumb2, HasThumb2DSP]>;
2608def t2SMLALTT : T2FourReg_mac<1, 0b100, 0b1011, (outs rGPR:$Ra,rGPR:$Rd),
2609         (ins rGPR:$Rn,rGPR:$Rm), IIC_iMAC64, "smlaltt", "\t$Ra, $Rd, $Rn, $Rm",
2610           [/* For disassembly only; pattern left blank */]>,
2611          Requires<[IsThumb2, HasThumb2DSP]>;
2612
2613// Dual halfword multiple: SMUAD, SMUSD, SMLAD, SMLSD, SMLALD, SMLSLD
2614def t2SMUAD: T2ThreeReg_mac<
2615            0, 0b010, 0b0000, (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm),
2616            IIC_iMAC32, "smuad", "\t$Rd, $Rn, $Rm", []>,
2617          Requires<[IsThumb2, HasThumb2DSP]> {
2618  let Inst{15-12} = 0b1111;
2619}
2620def t2SMUADX:T2ThreeReg_mac<
2621            0, 0b010, 0b0001, (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm),
2622            IIC_iMAC32, "smuadx", "\t$Rd, $Rn, $Rm", []>,
2623          Requires<[IsThumb2, HasThumb2DSP]> {
2624  let Inst{15-12} = 0b1111;
2625}
2626def t2SMUSD: T2ThreeReg_mac<
2627            0, 0b100, 0b0000, (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm),
2628            IIC_iMAC32, "smusd", "\t$Rd, $Rn, $Rm", []>,
2629          Requires<[IsThumb2, HasThumb2DSP]> {
2630  let Inst{15-12} = 0b1111;
2631}
2632def t2SMUSDX:T2ThreeReg_mac<
2633            0, 0b100, 0b0001, (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm),
2634            IIC_iMAC32, "smusdx", "\t$Rd, $Rn, $Rm", []>,
2635          Requires<[IsThumb2, HasThumb2DSP]> {
2636  let Inst{15-12} = 0b1111;
2637}
2638def t2SMLAD   : T2FourReg_mac<
2639            0, 0b010, 0b0000, (outs rGPR:$Rd),
2640            (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32, "smlad",
2641            "\t$Rd, $Rn, $Rm, $Ra", []>,
2642          Requires<[IsThumb2, HasThumb2DSP]>;
2643def t2SMLADX  : T2FourReg_mac<
2644            0, 0b010, 0b0001, (outs rGPR:$Rd),
2645            (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32, "smladx",
2646            "\t$Rd, $Rn, $Rm, $Ra", []>,
2647          Requires<[IsThumb2, HasThumb2DSP]>;
2648def t2SMLSD   : T2FourReg_mac<0, 0b100, 0b0000, (outs rGPR:$Rd),
2649            (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32, "smlsd",
2650            "\t$Rd, $Rn, $Rm, $Ra", []>,
2651          Requires<[IsThumb2, HasThumb2DSP]>;
2652def t2SMLSDX  : T2FourReg_mac<0, 0b100, 0b0001, (outs rGPR:$Rd),
2653            (ins rGPR:$Rn, rGPR:$Rm, rGPR:$Ra), IIC_iMAC32, "smlsdx",
2654            "\t$Rd, $Rn, $Rm, $Ra", []>,
2655          Requires<[IsThumb2, HasThumb2DSP]>;
2656def t2SMLALD  : T2FourReg_mac<1, 0b100, 0b1100, (outs rGPR:$Ra,rGPR:$Rd),
2657                        (ins rGPR:$Rn, rGPR:$Rm), IIC_iMAC64, "smlald",
2658                        "\t$Ra, $Rd, $Rn, $Rm", []>,
2659          Requires<[IsThumb2, HasThumb2DSP]>;
2660def t2SMLALDX : T2FourReg_mac<1, 0b100, 0b1101, (outs rGPR:$Ra,rGPR:$Rd),
2661                        (ins rGPR:$Rn,rGPR:$Rm), IIC_iMAC64, "smlaldx",
2662                        "\t$Ra, $Rd, $Rn, $Rm", []>,
2663          Requires<[IsThumb2, HasThumb2DSP]>;
2664def t2SMLSLD  : T2FourReg_mac<1, 0b101, 0b1100, (outs rGPR:$Ra,rGPR:$Rd),
2665                        (ins rGPR:$Rn,rGPR:$Rm), IIC_iMAC64, "smlsld",
2666                        "\t$Ra, $Rd, $Rn, $Rm", []>,
2667          Requires<[IsThumb2, HasThumb2DSP]>;
2668def t2SMLSLDX : T2FourReg_mac<1, 0b101, 0b1101, (outs rGPR:$Ra,rGPR:$Rd),
2669                        (ins rGPR:$Rm,rGPR:$Rn), IIC_iMAC64, "smlsldx",
2670                        "\t$Ra, $Rd, $Rn, $Rm", []>,
2671          Requires<[IsThumb2, HasThumb2DSP]>;
2672
2673//===----------------------------------------------------------------------===//
2674//  Division Instructions.
2675//  Signed and unsigned division on v7-M
2676//
2677def t2SDIV : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUi,
2678                 "sdiv", "\t$Rd, $Rn, $Rm",
2679                 [(set rGPR:$Rd, (sdiv rGPR:$Rn, rGPR:$Rm))]>,
2680                 Requires<[HasDivide, IsThumb2]> {
2681  let Inst{31-27} = 0b11111;
2682  let Inst{26-21} = 0b011100;
2683  let Inst{20} = 0b1;
2684  let Inst{15-12} = 0b1111;
2685  let Inst{7-4} = 0b1111;
2686}
2687
2688def t2UDIV : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUi,
2689                 "udiv", "\t$Rd, $Rn, $Rm",
2690                 [(set rGPR:$Rd, (udiv rGPR:$Rn, rGPR:$Rm))]>,
2691                 Requires<[HasDivide, IsThumb2]> {
2692  let Inst{31-27} = 0b11111;
2693  let Inst{26-21} = 0b011101;
2694  let Inst{20} = 0b1;
2695  let Inst{15-12} = 0b1111;
2696  let Inst{7-4} = 0b1111;
2697}
2698
2699//===----------------------------------------------------------------------===//
2700//  Misc. Arithmetic Instructions.
2701//
2702
2703class T2I_misc<bits<2> op1, bits<2> op2, dag oops, dag iops,
2704      InstrItinClass itin, string opc, string asm, list<dag> pattern>
2705  : T2ThreeReg<oops, iops, itin, opc, asm, pattern> {
2706  let Inst{31-27} = 0b11111;
2707  let Inst{26-22} = 0b01010;
2708  let Inst{21-20} = op1;
2709  let Inst{15-12} = 0b1111;
2710  let Inst{7-6} = 0b10;
2711  let Inst{5-4} = op2;
2712  let Rn{3-0} = Rm;
2713}
2714
2715def t2CLZ : T2I_misc<0b11, 0b00, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
2716                    "clz", "\t$Rd, $Rm", [(set rGPR:$Rd, (ctlz rGPR:$Rm))]>;
2717
2718def t2RBIT : T2I_misc<0b01, 0b10, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
2719                      "rbit", "\t$Rd, $Rm",
2720                      [(set rGPR:$Rd, (ARMrbit rGPR:$Rm))]>;
2721
2722def t2REV : T2I_misc<0b01, 0b00, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
2723                 "rev", ".w\t$Rd, $Rm", [(set rGPR:$Rd, (bswap rGPR:$Rm))]>;
2724
2725def t2REV16 : T2I_misc<0b01, 0b01, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
2726                       "rev16", ".w\t$Rd, $Rm",
2727                [(set rGPR:$Rd, (rotr (bswap rGPR:$Rm), (i32 16)))]>;
2728
2729def t2REVSH : T2I_misc<0b01, 0b11, (outs rGPR:$Rd), (ins rGPR:$Rm), IIC_iUNAr,
2730                       "revsh", ".w\t$Rd, $Rm",
2731                 [(set rGPR:$Rd, (sra (bswap rGPR:$Rm), (i32 16)))]>;
2732
2733def : T2Pat<(or (sra (shl rGPR:$Rm, (i32 24)), (i32 16)),
2734                (and (srl rGPR:$Rm, (i32 8)), 0xFF)),
2735            (t2REVSH rGPR:$Rm)>;
2736
2737def t2PKHBT : T2ThreeReg<
2738            (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, pkh_lsl_amt:$sh),
2739                  IIC_iBITsi, "pkhbt", "\t$Rd, $Rn, $Rm$sh",
2740                  [(set rGPR:$Rd, (or (and rGPR:$Rn, 0xFFFF),
2741                                      (and (shl rGPR:$Rm, pkh_lsl_amt:$sh),
2742                                           0xFFFF0000)))]>,
2743                  Requires<[HasT2ExtractPack, IsThumb2]> {
2744  let Inst{31-27} = 0b11101;
2745  let Inst{26-25} = 0b01;
2746  let Inst{24-20} = 0b01100;
2747  let Inst{5} = 0; // BT form
2748  let Inst{4} = 0;
2749
2750  bits<5> sh;
2751  let Inst{14-12} = sh{4-2};
2752  let Inst{7-6}   = sh{1-0};
2753}
2754
2755// Alternate cases for PKHBT where identities eliminate some nodes.
2756def : T2Pat<(or (and rGPR:$src1, 0xFFFF), (and rGPR:$src2, 0xFFFF0000)),
2757            (t2PKHBT rGPR:$src1, rGPR:$src2, 0)>,
2758            Requires<[HasT2ExtractPack, IsThumb2]>;
2759def : T2Pat<(or (and rGPR:$src1, 0xFFFF), (shl rGPR:$src2, imm16_31:$sh)),
2760            (t2PKHBT rGPR:$src1, rGPR:$src2, imm16_31:$sh)>,
2761            Requires<[HasT2ExtractPack, IsThumb2]>;
2762
2763// Note: Shifts of 1-15 bits will be transformed to srl instead of sra and
2764// will match the pattern below.
2765def t2PKHTB : T2ThreeReg<
2766                  (outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm, pkh_asr_amt:$sh),
2767                  IIC_iBITsi, "pkhtb", "\t$Rd, $Rn, $Rm$sh",
2768                  [(set rGPR:$Rd, (or (and rGPR:$Rn, 0xFFFF0000),
2769                                       (and (sra rGPR:$Rm, pkh_asr_amt:$sh),
2770                                            0xFFFF)))]>,
2771                  Requires<[HasT2ExtractPack, IsThumb2]> {
2772  let Inst{31-27} = 0b11101;
2773  let Inst{26-25} = 0b01;
2774  let Inst{24-20} = 0b01100;
2775  let Inst{5} = 1; // TB form
2776  let Inst{4} = 0;
2777
2778  bits<5> sh;
2779  let Inst{14-12} = sh{4-2};
2780  let Inst{7-6}   = sh{1-0};
2781}
2782
2783// Alternate cases for PKHTB where identities eliminate some nodes.  Note that
2784// a shift amount of 0 is *not legal* here, it is PKHBT instead.
2785def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000), (srl rGPR:$src2, imm16_31:$sh)),
2786            (t2PKHTB rGPR:$src1, rGPR:$src2, imm16_31:$sh)>,
2787            Requires<[HasT2ExtractPack, IsThumb2]>;
2788def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000),
2789                (and (srl rGPR:$src2, imm1_15:$sh), 0xFFFF)),
2790            (t2PKHTB rGPR:$src1, rGPR:$src2, imm1_15:$sh)>,
2791            Requires<[HasT2ExtractPack, IsThumb2]>;
2792
2793//===----------------------------------------------------------------------===//
2794//  Comparison Instructions...
2795//
2796defm t2CMP  : T2I_cmp_irs<0b1101, "cmp",
2797                          IIC_iCMPi, IIC_iCMPr, IIC_iCMPsi,
2798                          BinOpFrag<(ARMcmp node:$LHS, node:$RHS)>, "t2CMP">;
2799
2800def : T2Pat<(ARMcmpZ  GPRnopc:$lhs, t2_so_imm:$imm),
2801            (t2CMPri  GPRnopc:$lhs, t2_so_imm:$imm)>;
2802def : T2Pat<(ARMcmpZ  GPRnopc:$lhs, rGPR:$rhs),
2803            (t2CMPrr  GPRnopc:$lhs, rGPR:$rhs)>;
2804def : T2Pat<(ARMcmpZ  GPRnopc:$lhs, t2_so_reg:$rhs),
2805            (t2CMPrs  GPRnopc:$lhs, t2_so_reg:$rhs)>;
2806
2807//FIXME: Disable CMN, as CCodes are backwards from compare expectations
2808//       Compare-to-zero still works out, just not the relationals
2809//defm t2CMN  : T2I_cmp_irs<0b1000, "cmn",
2810//                          BinOpFrag<(ARMcmp node:$LHS,(ineg node:$RHS))>>;
2811defm t2CMNz : T2I_cmp_irs<0b1000, "cmn",
2812                          IIC_iCMPi, IIC_iCMPr, IIC_iCMPsi,
2813                          BinOpFrag<(ARMcmpZ node:$LHS,(ineg node:$RHS))>,
2814                          "t2CMNz">;
2815
2816//def : T2Pat<(ARMcmp  GPR:$src, t2_so_imm_neg:$imm),
2817//            (t2CMNri GPR:$src, t2_so_imm_neg:$imm)>;
2818
2819def : T2Pat<(ARMcmpZ  GPRnopc:$src, t2_so_imm_neg:$imm),
2820            (t2CMNzri GPRnopc:$src, t2_so_imm_neg:$imm)>;
2821
2822defm t2TST  : T2I_cmp_irs<0b0000, "tst",
2823                          IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
2824                         BinOpFrag<(ARMcmpZ (and_su node:$LHS, node:$RHS), 0)>,
2825                          "t2TST">;
2826defm t2TEQ  : T2I_cmp_irs<0b0100, "teq",
2827                          IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
2828                         BinOpFrag<(ARMcmpZ (xor_su node:$LHS, node:$RHS), 0)>,
2829                          "t2TEQ">;
2830
2831// Conditional moves
2832// FIXME: should be able to write a pattern for ARMcmov, but can't use
2833// a two-value operand where a dag node expects two operands. :(
2834let neverHasSideEffects = 1 in {
2835def t2MOVCCr : t2PseudoInst<(outs rGPR:$Rd),
2836                            (ins rGPR:$false, rGPR:$Rm, pred:$p),
2837                            4, IIC_iCMOVr,
2838   [/*(set rGPR:$Rd, (ARMcmov rGPR:$false, rGPR:$Rm, imm:$cc, CCR:$ccr))*/]>,
2839                RegConstraint<"$false = $Rd">;
2840
2841let isMoveImm = 1 in
2842def t2MOVCCi : t2PseudoInst<(outs rGPR:$Rd),
2843                            (ins rGPR:$false, t2_so_imm:$imm, pred:$p),
2844                   4, IIC_iCMOVi,
2845[/*(set rGPR:$Rd,(ARMcmov rGPR:$false,t2_so_imm:$imm, imm:$cc, CCR:$ccr))*/]>,
2846                   RegConstraint<"$false = $Rd">;
2847
2848// FIXME: Pseudo-ize these. For now, just mark codegen only.
2849let isCodeGenOnly = 1 in {
2850let isMoveImm = 1 in
2851def t2MOVCCi16 : T2I<(outs rGPR:$Rd), (ins rGPR:$false, imm0_65535_expr:$imm),
2852                      IIC_iCMOVi,
2853                      "movw", "\t$Rd, $imm", []>,
2854                      RegConstraint<"$false = $Rd"> {
2855  let Inst{31-27} = 0b11110;
2856  let Inst{25} = 1;
2857  let Inst{24-21} = 0b0010;
2858  let Inst{20} = 0; // The S bit.
2859  let Inst{15} = 0;
2860
2861  bits<4> Rd;
2862  bits<16> imm;
2863
2864  let Inst{11-8}  = Rd;
2865  let Inst{19-16} = imm{15-12};
2866  let Inst{26}    = imm{11};
2867  let Inst{14-12} = imm{10-8};
2868  let Inst{7-0}   = imm{7-0};
2869}
2870
2871let isMoveImm = 1 in
2872def t2MOVCCi32imm : PseudoInst<(outs rGPR:$dst),
2873                               (ins rGPR:$false, i32imm:$src, pred:$p),
2874                    IIC_iCMOVix2, []>, RegConstraint<"$false = $dst">;
2875
2876let isMoveImm = 1 in
2877def t2MVNCCi : T2OneRegImm<(outs rGPR:$Rd), (ins rGPR:$false, t2_so_imm:$imm),
2878                   IIC_iCMOVi, "mvn", ".w\t$Rd, $imm",
2879[/*(set rGPR:$Rd,(ARMcmov rGPR:$false,t2_so_imm_not:$imm,
2880                   imm:$cc, CCR:$ccr))*/]>,
2881                   RegConstraint<"$false = $Rd"> {
2882  let Inst{31-27} = 0b11110;
2883  let Inst{25} = 0;
2884  let Inst{24-21} = 0b0011;
2885  let Inst{20} = 0; // The S bit.
2886  let Inst{19-16} = 0b1111; // Rn
2887  let Inst{15} = 0;
2888}
2889
2890class T2I_movcc_sh<bits<2> opcod, dag oops, dag iops, InstrItinClass itin,
2891                   string opc, string asm, list<dag> pattern>
2892  : T2TwoRegShiftImm<oops, iops, itin, opc, asm, pattern> {
2893  let Inst{31-27} = 0b11101;
2894  let Inst{26-25} = 0b01;
2895  let Inst{24-21} = 0b0010;
2896  let Inst{20} = 0; // The S bit.
2897  let Inst{19-16} = 0b1111; // Rn
2898  let Inst{5-4} = opcod; // Shift type.
2899}
2900def t2MOVCClsl : T2I_movcc_sh<0b00, (outs rGPR:$Rd),
2901                             (ins rGPR:$false, rGPR:$Rm, i32imm:$imm),
2902                             IIC_iCMOVsi, "lsl", ".w\t$Rd, $Rm, $imm", []>,
2903                 RegConstraint<"$false = $Rd">;
2904def t2MOVCClsr : T2I_movcc_sh<0b01, (outs rGPR:$Rd),
2905                             (ins rGPR:$false, rGPR:$Rm, i32imm:$imm),
2906                             IIC_iCMOVsi, "lsr", ".w\t$Rd, $Rm, $imm", []>,
2907                 RegConstraint<"$false = $Rd">;
2908def t2MOVCCasr : T2I_movcc_sh<0b10, (outs rGPR:$Rd),
2909                             (ins rGPR:$false, rGPR:$Rm, i32imm:$imm),
2910                             IIC_iCMOVsi, "asr", ".w\t$Rd, $Rm, $imm", []>,
2911                 RegConstraint<"$false = $Rd">;
2912def t2MOVCCror : T2I_movcc_sh<0b11, (outs rGPR:$Rd),
2913                             (ins rGPR:$false, rGPR:$Rm, i32imm:$imm),
2914                             IIC_iCMOVsi, "ror", ".w\t$Rd, $Rm, $imm", []>,
2915                 RegConstraint<"$false = $Rd">;
2916} // isCodeGenOnly = 1
2917} // neverHasSideEffects
2918
2919//===----------------------------------------------------------------------===//
2920// Atomic operations intrinsics
2921//
2922
2923// memory barriers protect the atomic sequences
2924let hasSideEffects = 1 in {
2925def t2DMB : AInoP<(outs), (ins memb_opt:$opt), ThumbFrm, NoItinerary,
2926                  "dmb", "\t$opt", [(ARMMemBarrier (i32 imm:$opt))]>,
2927                  Requires<[IsThumb, HasDB]> {
2928  bits<4> opt;
2929  let Inst{31-4} = 0xf3bf8f5;
2930  let Inst{3-0} = opt;
2931}
2932}
2933
2934def t2DSB : AInoP<(outs), (ins memb_opt:$opt), ThumbFrm, NoItinerary,
2935                  "dsb", "\t$opt", []>,
2936                  Requires<[IsThumb, HasDB]> {
2937  bits<4> opt;
2938  let Inst{31-4} = 0xf3bf8f4;
2939  let Inst{3-0} = opt;
2940}
2941
2942def t2ISB : AInoP<(outs), (ins memb_opt:$opt), ThumbFrm, NoItinerary,
2943                  "isb", "\t$opt",
2944                  []>, Requires<[IsThumb2, HasDB]> {
2945  bits<4> opt;
2946  let Inst{31-4} = 0xf3bf8f6;
2947  let Inst{3-0} = opt;
2948}
2949
2950class T2I_ldrex<bits<2> opcod, dag oops, dag iops, AddrMode am, int sz,
2951                InstrItinClass itin, string opc, string asm, string cstr,
2952                list<dag> pattern, bits<4> rt2 = 0b1111>
2953  : Thumb2I<oops, iops, am, sz, itin, opc, asm, cstr, pattern> {
2954  let Inst{31-27} = 0b11101;
2955  let Inst{26-20} = 0b0001101;
2956  let Inst{11-8} = rt2;
2957  let Inst{7-6} = 0b01;
2958  let Inst{5-4} = opcod;
2959  let Inst{3-0} = 0b1111;
2960
2961  bits<4> addr;
2962  bits<4> Rt;
2963  let Inst{19-16} = addr;
2964  let Inst{15-12} = Rt;
2965}
2966class T2I_strex<bits<2> opcod, dag oops, dag iops, AddrMode am, int sz,
2967                InstrItinClass itin, string opc, string asm, string cstr,
2968                list<dag> pattern, bits<4> rt2 = 0b1111>
2969  : Thumb2I<oops, iops, am, sz, itin, opc, asm, cstr, pattern> {
2970  let Inst{31-27} = 0b11101;
2971  let Inst{26-20} = 0b0001100;
2972  let Inst{11-8} = rt2;
2973  let Inst{7-6} = 0b01;
2974  let Inst{5-4} = opcod;
2975
2976  bits<4> Rd;
2977  bits<4> addr;
2978  bits<4> Rt;
2979  let Inst{3-0}  = Rd;
2980  let Inst{19-16} = addr;
2981  let Inst{15-12} = Rt;
2982}
2983
2984let mayLoad = 1 in {
2985def t2LDREXB : T2I_ldrex<0b00, (outs rGPR:$Rt), (ins addr_offset_none:$addr),
2986                         AddrModeNone, 4, NoItinerary,
2987                         "ldrexb", "\t$Rt, $addr", "", []>;
2988def t2LDREXH : T2I_ldrex<0b01, (outs rGPR:$Rt), (ins addr_offset_none:$addr),
2989                         AddrModeNone, 4, NoItinerary,
2990                         "ldrexh", "\t$Rt, $addr", "", []>;
2991def t2LDREX  : Thumb2I<(outs rGPR:$Rt), (ins t2addrmode_imm0_1020s4:$addr),
2992                       AddrModeNone, 4, NoItinerary,
2993                       "ldrex", "\t$Rt, $addr", "", []> {
2994  bits<4> Rt;
2995  bits<12> addr;
2996  let Inst{31-27} = 0b11101;
2997  let Inst{26-20} = 0b0000101;
2998  let Inst{19-16} = addr{11-8};
2999  let Inst{15-12} = Rt;
3000  let Inst{11-8} = 0b1111;
3001  let Inst{7-0} = addr{7-0};
3002}
3003let hasExtraDefRegAllocReq = 1 in
3004def t2LDREXD : T2I_ldrex<0b11, (outs rGPR:$Rt, rGPR:$Rt2),
3005                         (ins addr_offset_none:$addr),
3006                         AddrModeNone, 4, NoItinerary,
3007                         "ldrexd", "\t$Rt, $Rt2, $addr", "",
3008                         [], {?, ?, ?, ?}> {
3009  bits<4> Rt2;
3010  let Inst{11-8} = Rt2;
3011}
3012}
3013
3014let mayStore = 1, Constraints = "@earlyclobber $Rd" in {
3015def t2STREXB : T2I_strex<0b00, (outs rGPR:$Rd),
3016                         (ins rGPR:$Rt, addr_offset_none:$addr),
3017                         AddrModeNone, 4, NoItinerary,
3018                         "strexb", "\t$Rd, $Rt, $addr", "", []>;
3019def t2STREXH : T2I_strex<0b01, (outs rGPR:$Rd),
3020                         (ins rGPR:$Rt, addr_offset_none:$addr),
3021                         AddrModeNone, 4, NoItinerary,
3022                         "strexh", "\t$Rd, $Rt, $addr", "", []>;
3023def t2STREX  : Thumb2I<(outs rGPR:$Rd), (ins rGPR:$Rt,
3024                             t2addrmode_imm0_1020s4:$addr),
3025                  AddrModeNone, 4, NoItinerary,
3026                  "strex", "\t$Rd, $Rt, $addr", "",
3027                  []> {
3028  bits<4> Rd;
3029  bits<4> Rt;
3030  bits<12> addr;
3031  let Inst{31-27} = 0b11101;
3032  let Inst{26-20} = 0b0000100;
3033  let Inst{19-16} = addr{11-8};
3034  let Inst{15-12} = Rt;
3035  let Inst{11-8}  = Rd;
3036  let Inst{7-0} = addr{7-0};
3037}
3038}
3039
3040let hasExtraSrcRegAllocReq = 1, Constraints = "@earlyclobber $Rd" in
3041def t2STREXD : T2I_strex<0b11, (outs rGPR:$Rd),
3042                         (ins rGPR:$Rt, rGPR:$Rt2, addr_offset_none:$addr),
3043                         AddrModeNone, 4, NoItinerary,
3044                         "strexd", "\t$Rd, $Rt, $Rt2, $addr", "", [],
3045                         {?, ?, ?, ?}> {
3046  bits<4> Rt2;
3047  let Inst{11-8} = Rt2;
3048}
3049
3050def t2CLREX : T2I<(outs), (ins), NoItinerary, "clrex", "", []>,
3051            Requires<[IsThumb2, HasV7]>  {
3052  let Inst{31-16} = 0xf3bf;
3053  let Inst{15-14} = 0b10;
3054  let Inst{13} = 0;
3055  let Inst{12} = 0;
3056  let Inst{11-8} = 0b1111;
3057  let Inst{7-4} = 0b0010;
3058  let Inst{3-0} = 0b1111;
3059}
3060
3061//===----------------------------------------------------------------------===//
3062// SJLJ Exception handling intrinsics
3063//   eh_sjlj_setjmp() is an instruction sequence to store the return
3064//   address and save #0 in R0 for the non-longjmp case.
3065//   Since by its nature we may be coming from some other function to get
3066//   here, and we're using the stack frame for the containing function to
3067//   save/restore registers, we can't keep anything live in regs across
3068//   the eh_sjlj_setjmp(), else it will almost certainly have been tromped upon
3069//   when we get here from a longjmp(). We force everything out of registers
3070//   except for our own input by listing the relevant registers in Defs. By
3071//   doing so, we also cause the prologue/epilogue code to actively preserve
3072//   all of the callee-saved resgisters, which is exactly what we want.
3073//   $val is a scratch register for our use.
3074let Defs =
3075  [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7,  R8,  R9,  R10, R11, R12, LR, CPSR,
3076    QQQQ0, QQQQ1, QQQQ2, QQQQ3 ],
3077  hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1 in {
3078  def t2Int_eh_sjlj_setjmp : Thumb2XI<(outs), (ins tGPR:$src, tGPR:$val),
3079                               AddrModeNone, 0, NoItinerary, "", "",
3080                          [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>,
3081                             Requires<[IsThumb2, HasVFP2]>;
3082}
3083
3084let Defs =
3085  [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7,  R8,  R9,  R10, R11, R12, LR, CPSR ],
3086  hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1 in {
3087  def t2Int_eh_sjlj_setjmp_nofp : Thumb2XI<(outs), (ins tGPR:$src, tGPR:$val),
3088                               AddrModeNone, 0, NoItinerary, "", "",
3089                          [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>,
3090                                  Requires<[IsThumb2, NoVFP]>;
3091}
3092
3093
3094//===----------------------------------------------------------------------===//
3095// Control-Flow Instructions
3096//
3097
3098// FIXME: remove when we have a way to marking a MI with these properties.
3099// FIXME: Should pc be an implicit operand like PICADD, etc?
3100let isReturn = 1, isTerminator = 1, isBarrier = 1, mayLoad = 1,
3101    hasExtraDefRegAllocReq = 1, isCodeGenOnly = 1 in
3102def t2LDMIA_RET: t2PseudoExpand<(outs GPR:$wb), (ins GPR:$Rn, pred:$p,
3103                                                   reglist:$regs, variable_ops),
3104                              4, IIC_iLoad_mBr, [],
3105            (t2LDMIA_UPD GPR:$wb, GPR:$Rn, pred:$p, reglist:$regs)>,
3106                         RegConstraint<"$Rn = $wb">;
3107
3108let isBranch = 1, isTerminator = 1, isBarrier = 1 in {
3109let isPredicable = 1 in
3110def t2B   : T2I<(outs), (ins uncondbrtarget:$target), IIC_Br,
3111                 "b", ".w\t$target",
3112                 [(br bb:$target)]> {
3113  let Inst{31-27} = 0b11110;
3114  let Inst{15-14} = 0b10;
3115  let Inst{12} = 1;
3116
3117  bits<20> target;
3118  let Inst{26} = target{19};
3119  let Inst{11} = target{18};
3120  let Inst{13} = target{17};
3121  let Inst{21-16} = target{16-11};
3122  let Inst{10-0} = target{10-0};
3123}
3124
3125let isNotDuplicable = 1, isIndirectBranch = 1 in {
3126def t2BR_JT : t2PseudoInst<(outs),
3127          (ins GPR:$target, GPR:$index, i32imm:$jt, i32imm:$id),
3128           0, IIC_Br,
3129          [(ARMbr2jt GPR:$target, GPR:$index, tjumptable:$jt, imm:$id)]>;
3130
3131// FIXME: Add a non-pc based case that can be predicated.
3132def t2TBB_JT : t2PseudoInst<(outs),
3133        (ins GPR:$index, i32imm:$jt, i32imm:$id), 0, IIC_Br, []>;
3134
3135def t2TBH_JT : t2PseudoInst<(outs),
3136        (ins GPR:$index, i32imm:$jt, i32imm:$id), 0, IIC_Br, []>;
3137
3138def t2TBB : T2I<(outs), (ins GPR:$Rn, GPR:$Rm), IIC_Br,
3139                    "tbb", "\t[$Rn, $Rm]", []> {
3140  bits<4> Rn;
3141  bits<4> Rm;
3142  let Inst{31-20} = 0b111010001101;
3143  let Inst{19-16} = Rn;
3144  let Inst{15-5} = 0b11110000000;
3145  let Inst{4} = 0; // B form
3146  let Inst{3-0} = Rm;
3147}
3148
3149def t2TBH : T2I<(outs), (ins GPR:$Rn, GPR:$Rm), IIC_Br,
3150                   "tbh", "\t[$Rn, $Rm, lsl #1]", []> {
3151  bits<4> Rn;
3152  bits<4> Rm;
3153  let Inst{31-20} = 0b111010001101;
3154  let Inst{19-16} = Rn;
3155  let Inst{15-5} = 0b11110000000;
3156  let Inst{4} = 1; // H form
3157  let Inst{3-0} = Rm;
3158}
3159} // isNotDuplicable, isIndirectBranch
3160
3161} // isBranch, isTerminator, isBarrier
3162
3163// FIXME: should be able to write a pattern for ARMBrcond, but can't use
3164// a two-value operand where a dag node expects ", "two operands. :(
3165let isBranch = 1, isTerminator = 1 in
3166def t2Bcc : T2I<(outs), (ins brtarget:$target), IIC_Br,
3167                "b", ".w\t$target",
3168                [/*(ARMbrcond bb:$target, imm:$cc)*/]> {
3169  let Inst{31-27} = 0b11110;
3170  let Inst{15-14} = 0b10;
3171  let Inst{12} = 0;
3172
3173  bits<4> p;
3174  let Inst{25-22} = p;
3175
3176  bits<21> target;
3177  let Inst{26} = target{20};
3178  let Inst{11} = target{19};
3179  let Inst{13} = target{18};
3180  let Inst{21-16} = target{17-12};
3181  let Inst{10-0} = target{11-1};
3182
3183  let DecoderMethod = "DecodeThumb2BCCInstruction";
3184}
3185
3186// Tail calls. The Darwin version of thumb tail calls uses a t2 branch, so
3187// it goes here.
3188let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in {
3189  // Darwin version.
3190  let Defs = [R0, R1, R2, R3, R9, R12, QQQQ0, QQQQ2, QQQQ3, PC],
3191      Uses = [SP] in
3192  def tTAILJMPd: tPseudoExpand<(outs),
3193                   (ins uncondbrtarget:$dst, pred:$p, variable_ops),
3194                   4, IIC_Br, [],
3195                   (t2B uncondbrtarget:$dst, pred:$p)>,
3196                 Requires<[IsThumb2, IsDarwin]>;
3197}
3198
3199// IT block
3200let Defs = [ITSTATE] in
3201def t2IT : Thumb2XI<(outs), (ins it_pred:$cc, it_mask:$mask),
3202                    AddrModeNone, 2,  IIC_iALUx,
3203                    "it$mask\t$cc", "", []> {
3204  // 16-bit instruction.
3205  let Inst{31-16} = 0x0000;
3206  let Inst{15-8} = 0b10111111;
3207
3208  bits<4> cc;
3209  bits<4> mask;
3210  let Inst{7-4} = cc;
3211  let Inst{3-0} = mask;
3212
3213  let DecoderMethod = "DecodeIT";
3214}
3215
3216// Branch and Exchange Jazelle -- for disassembly only
3217// Rm = Inst{19-16}
3218def t2BXJ : T2I<(outs), (ins rGPR:$func), NoItinerary, "bxj", "\t$func", []> {
3219  bits<4> func;
3220  let Inst{31-27} = 0b11110;
3221  let Inst{26} = 0;
3222  let Inst{25-20} = 0b111100;
3223  let Inst{19-16} = func;
3224  let Inst{15-0} = 0b1000111100000000;
3225}
3226
3227// Compare and branch on zero / non-zero
3228let isBranch = 1, isTerminator = 1 in {
3229  def tCBZ  : T1I<(outs), (ins tGPR:$Rn, t_cbtarget:$target), IIC_Br,
3230                  "cbz\t$Rn, $target", []>,
3231              T1Misc<{0,0,?,1,?,?,?}>,
3232              Requires<[IsThumb2]> {
3233    // A8.6.27
3234    bits<6> target;
3235    bits<3> Rn;
3236    let Inst{9}   = target{5};
3237    let Inst{7-3} = target{4-0};
3238    let Inst{2-0} = Rn;
3239  }
3240
3241  def tCBNZ : T1I<(outs), (ins tGPR:$Rn, t_cbtarget:$target), IIC_Br,
3242                  "cbnz\t$Rn, $target", []>,
3243              T1Misc<{1,0,?,1,?,?,?}>,
3244              Requires<[IsThumb2]> {
3245    // A8.6.27
3246    bits<6> target;
3247    bits<3> Rn;
3248    let Inst{9}   = target{5};
3249    let Inst{7-3} = target{4-0};
3250    let Inst{2-0} = Rn;
3251  }
3252}
3253
3254
3255// Change Processor State is a system instruction -- for disassembly and
3256// parsing only.
3257// FIXME: Since the asm parser has currently no clean way to handle optional
3258// operands, create 3 versions of the same instruction. Once there's a clean
3259// framework to represent optional operands, change this behavior.
3260class t2CPS<dag iops, string asm_op> : T2XI<(outs), iops, NoItinerary,
3261            !strconcat("cps", asm_op),
3262            [/* For disassembly only; pattern left blank */]> {
3263  bits<2> imod;
3264  bits<3> iflags;
3265  bits<5> mode;
3266  bit M;
3267
3268  let Inst{31-27} = 0b11110;
3269  let Inst{26}    = 0;
3270  let Inst{25-20} = 0b111010;
3271  let Inst{19-16} = 0b1111;
3272  let Inst{15-14} = 0b10;
3273  let Inst{12}    = 0;
3274  let Inst{10-9}  = imod;
3275  let Inst{8}     = M;
3276  let Inst{7-5}   = iflags;
3277  let Inst{4-0}   = mode;
3278  let DecoderMethod = "DecodeT2CPSInstruction";
3279}
3280
3281let M = 1 in
3282  def t2CPS3p : t2CPS<(ins imod_op:$imod, iflags_op:$iflags, i32imm:$mode),
3283                      "$imod.w\t$iflags, $mode">;
3284let mode = 0, M = 0 in
3285  def t2CPS2p : t2CPS<(ins imod_op:$imod, iflags_op:$iflags),
3286                      "$imod.w\t$iflags">;
3287let imod = 0, iflags = 0, M = 1 in
3288  def t2CPS1p : t2CPS<(ins i32imm:$mode), "\t$mode">;
3289
3290// A6.3.4 Branches and miscellaneous control
3291// Table A6-14 Change Processor State, and hint instructions
3292// Helper class for disassembly only.
3293class T2I_hint<bits<8> op7_0, string opc, string asm>
3294  : T2I<(outs), (ins), NoItinerary, opc, asm,
3295        [/* For disassembly only; pattern left blank */]> {
3296  let Inst{31-20} = 0xf3a;
3297  let Inst{19-16} = 0b1111;
3298  let Inst{15-14} = 0b10;
3299  let Inst{12} = 0;
3300  let Inst{10-8} = 0b000;
3301  let Inst{7-0} = op7_0;
3302}
3303
3304def t2NOP   : T2I_hint<0b00000000, "nop",   ".w">;
3305def t2YIELD : T2I_hint<0b00000001, "yield", ".w">;
3306def t2WFE   : T2I_hint<0b00000010, "wfe",   ".w">;
3307def t2WFI   : T2I_hint<0b00000011, "wfi",   ".w">;
3308def t2SEV   : T2I_hint<0b00000100, "sev",   ".w">;
3309
3310def t2DBG : T2I<(outs), (ins imm0_15:$opt), NoItinerary, "dbg", "\t$opt", []> {
3311  bits<4> opt;
3312  let Inst{31-20} = 0b111100111010;
3313  let Inst{19-16} = 0b1111;
3314  let Inst{15-8} = 0b10000000;
3315  let Inst{7-4} = 0b1111;
3316  let Inst{3-0} = opt;
3317}
3318
3319// Secure Monitor Call is a system instruction -- for disassembly only
3320// Option = Inst{19-16}
3321def t2SMC : T2I<(outs), (ins imm0_15:$opt), NoItinerary, "smc", "\t$opt",
3322                [/* For disassembly only; pattern left blank */]> {
3323  let Inst{31-27} = 0b11110;
3324  let Inst{26-20} = 0b1111111;
3325  let Inst{15-12} = 0b1000;
3326
3327  bits<4> opt;
3328  let Inst{19-16} = opt;
3329}
3330
3331class T2SRS<bits<2> Op, bit W, dag oops, dag iops, InstrItinClass itin,
3332            string opc, string asm, list<dag> pattern>
3333  : T2I<oops, iops, itin, opc, asm, pattern> {
3334  bits<5> mode;
3335  let Inst{31-25} = 0b1110100;
3336  let Inst{24-23} = Op;
3337  let Inst{22} = 0;
3338  let Inst{21} = W;
3339  let Inst{20-16} = 0b01101;
3340  let Inst{15-5} = 0b11000000000;
3341  let Inst{4-0} = mode{4-0};
3342}
3343
3344// Store Return State is a system instruction.
3345def t2SRSDB_UPD : T2SRS<0b00, 1, (outs), (ins imm0_31:$mode), NoItinerary,
3346                        "srsdb", "\tsp!, $mode", []>;
3347def t2SRSDB  : T2SRS<0b00, 0, (outs), (ins imm0_31:$mode), NoItinerary,
3348                     "srsdb","\tsp, $mode", []>;
3349def t2SRSIA_UPD : T2SRS<0b11, 1, (outs), (ins imm0_31:$mode), NoItinerary,
3350                        "srsia","\tsp!, $mode", []>;
3351def t2SRSIA  : T2SRS<0b11, 0, (outs), (ins imm0_31:$mode), NoItinerary,
3352                     "srsia","\tsp, $mode", []>;
3353
3354// Return From Exception is a system instruction.
3355class T2RFE<bits<12> op31_20, dag oops, dag iops, InstrItinClass itin,
3356          string opc, string asm, list<dag> pattern>
3357  : T2I<oops, iops, itin, opc, asm, pattern> {
3358  let Inst{31-20} = op31_20{11-0};
3359
3360  bits<4> Rn;
3361  let Inst{19-16} = Rn;
3362  let Inst{15-0} = 0xc000;
3363}
3364
3365def t2RFEDBW : T2RFE<0b111010000011,
3366                   (outs), (ins GPR:$Rn), NoItinerary, "rfedb", "\t$Rn!",
3367                   [/* For disassembly only; pattern left blank */]>;
3368def t2RFEDB  : T2RFE<0b111010000001,
3369                   (outs), (ins GPR:$Rn), NoItinerary, "rfedb", "\t$Rn",
3370                   [/* For disassembly only; pattern left blank */]>;
3371def t2RFEIAW : T2RFE<0b111010011011,
3372                   (outs), (ins GPR:$Rn), NoItinerary, "rfeia", "\t$Rn!",
3373                   [/* For disassembly only; pattern left blank */]>;
3374def t2RFEIA  : T2RFE<0b111010011001,
3375                   (outs), (ins GPR:$Rn), NoItinerary, "rfeia", "\t$Rn",
3376                   [/* For disassembly only; pattern left blank */]>;
3377
3378//===----------------------------------------------------------------------===//
3379// Non-Instruction Patterns
3380//
3381
3382// 32-bit immediate using movw + movt.
3383// This is a single pseudo instruction to make it re-materializable.
3384// FIXME: Remove this when we can do generalized remat.
3385let isReMaterializable = 1, isMoveImm = 1 in
3386def t2MOVi32imm : PseudoInst<(outs rGPR:$dst), (ins i32imm:$src), IIC_iMOVix2,
3387                            [(set rGPR:$dst, (i32 imm:$src))]>,
3388                            Requires<[IsThumb, HasV6T2]>;
3389
3390// Pseudo instruction that combines movw + movt + add pc (if pic).
3391// It also makes it possible to rematerialize the instructions.
3392// FIXME: Remove this when we can do generalized remat and when machine licm
3393// can properly the instructions.
3394let isReMaterializable = 1 in {
3395def t2MOV_ga_pcrel : PseudoInst<(outs rGPR:$dst), (ins i32imm:$addr),
3396                                IIC_iMOVix2addpc,
3397                          [(set rGPR:$dst, (ARMWrapperPIC tglobaladdr:$addr))]>,
3398                          Requires<[IsThumb2, UseMovt]>;
3399
3400def t2MOV_ga_dyn : PseudoInst<(outs rGPR:$dst), (ins i32imm:$addr),
3401                              IIC_iMOVix2,
3402                          [(set rGPR:$dst, (ARMWrapperDYN tglobaladdr:$addr))]>,
3403                          Requires<[IsThumb2, UseMovt]>;
3404}
3405
3406// ConstantPool, GlobalAddress, and JumpTable
3407def : T2Pat<(ARMWrapper  tglobaladdr :$dst), (t2LEApcrel tglobaladdr :$dst)>,
3408           Requires<[IsThumb2, DontUseMovt]>;
3409def : T2Pat<(ARMWrapper  tconstpool  :$dst), (t2LEApcrel tconstpool  :$dst)>;
3410def : T2Pat<(ARMWrapper  tglobaladdr :$dst), (t2MOVi32imm tglobaladdr :$dst)>,
3411           Requires<[IsThumb2, UseMovt]>;
3412
3413def : T2Pat<(ARMWrapperJT tjumptable:$dst, imm:$id),
3414            (t2LEApcrelJT tjumptable:$dst, imm:$id)>;
3415
3416// Pseudo instruction that combines ldr from constpool and add pc. This should
3417// be expanded into two instructions late to allow if-conversion and
3418// scheduling.
3419let canFoldAsLoad = 1, isReMaterializable = 1 in
3420def t2LDRpci_pic : PseudoInst<(outs rGPR:$dst), (ins i32imm:$addr, pclabel:$cp),
3421                   IIC_iLoadiALU,
3422              [(set rGPR:$dst, (ARMpic_add (load (ARMWrapper tconstpool:$addr)),
3423                                           imm:$cp))]>,
3424               Requires<[IsThumb2]>;
3425//===----------------------------------------------------------------------===//
3426// Coprocessor load/store -- for disassembly only
3427//
3428class T2CI<dag oops, dag iops, string opc, string asm>
3429  : T2I<oops, iops, NoItinerary, opc, asm, []> {
3430  let Inst{27-25} = 0b110;
3431}
3432
3433multiclass T2LdStCop<bits<4> op31_28, bit load, string opc> {
3434  def _OFFSET : T2CI<(outs),
3435      (ins nohash_imm:$cop, nohash_imm:$CRd, addrmode2:$addr),
3436      opc, "\tp$cop, cr$CRd, $addr"> {
3437    let Inst{31-28} = op31_28;
3438    let Inst{24} = 1; // P = 1
3439    let Inst{21} = 0; // W = 0
3440    let Inst{22} = 0; // D = 0
3441    let Inst{20} = load;
3442    let DecoderMethod = "DecodeCopMemInstruction";
3443  }
3444
3445  def _PRE : T2CI<(outs),
3446      (ins nohash_imm:$cop, nohash_imm:$CRd, addrmode2:$addr),
3447      opc, "\tp$cop, cr$CRd, $addr!"> {
3448    let Inst{31-28} = op31_28;
3449    let Inst{24} = 1; // P = 1
3450    let Inst{21} = 1; // W = 1
3451    let Inst{22} = 0; // D = 0
3452    let Inst{20} = load;
3453    let DecoderMethod = "DecodeCopMemInstruction";
3454  }
3455
3456  def _POST : T2CI<(outs),
3457      (ins nohash_imm:$cop, nohash_imm:$CRd, addrmode2:$addr),
3458      opc, "\tp$cop, cr$CRd, $addr"> {
3459    let Inst{31-28} = op31_28;
3460    let Inst{24} = 0; // P = 0
3461    let Inst{21} = 1; // W = 1
3462    let Inst{22} = 0; // D = 0
3463    let Inst{20} = load;
3464    let DecoderMethod = "DecodeCopMemInstruction";
3465  }
3466
3467  def _OPTION : T2CI<(outs),
3468      (ins nohash_imm:$cop,nohash_imm:$CRd,GPR:$base, nohash_imm:$option),
3469      opc, "\tp$cop, cr$CRd, [$base], \\{$option\\}"> {
3470    let Inst{31-28} = op31_28;
3471    let Inst{24} = 0; // P = 0
3472    let Inst{23} = 1; // U = 1
3473    let Inst{21} = 0; // W = 0
3474    let Inst{22} = 0; // D = 0
3475    let Inst{20} = load;
3476    let DecoderMethod = "DecodeCopMemInstruction";
3477  }
3478
3479  def L_OFFSET : T2CI<(outs),
3480      (ins nohash_imm:$cop, nohash_imm:$CRd, addrmode2:$addr),
3481      !strconcat(opc, "l"), "\tp$cop, cr$CRd, $addr"> {
3482    let Inst{31-28} = op31_28;
3483    let Inst{24} = 1; // P = 1
3484    let Inst{21} = 0; // W = 0
3485    let Inst{22} = 1; // D = 1
3486    let Inst{20} = load;
3487    let DecoderMethod = "DecodeCopMemInstruction";
3488  }
3489
3490  def L_PRE : T2CI<(outs),
3491      (ins nohash_imm:$cop, nohash_imm:$CRd, addrmode2:$addr),
3492      !strconcat(opc, "l"), "\tp$cop, cr$CRd, $addr!"> {
3493    let Inst{31-28} = op31_28;
3494    let Inst{24} = 1; // P = 1
3495    let Inst{21} = 1; // W = 1
3496    let Inst{22} = 1; // D = 1
3497    let Inst{20} = load;
3498    let DecoderMethod = "DecodeCopMemInstruction";
3499  }
3500
3501  def L_POST : T2CI<(outs),
3502      (ins nohash_imm:$cop, nohash_imm:$CRd, addr_offset_none:$addr,
3503            postidx_imm8s4:$offset),
3504      !strconcat(opc, "l"), "\tp$cop, cr$CRd, $addr, $offset"> {
3505    let Inst{31-28} = op31_28;
3506    let Inst{24} = 0; // P = 0
3507    let Inst{21} = 1; // W = 1
3508    let Inst{22} = 1; // D = 1
3509    let Inst{20} = load;
3510    let DecoderMethod = "DecodeCopMemInstruction";
3511  }
3512
3513  def L_OPTION : T2CI<(outs),
3514      (ins nohash_imm:$cop, nohash_imm:$CRd,GPR:$base,nohash_imm:$option),
3515      !strconcat(opc, "l"), "\tp$cop, cr$CRd, [$base], \\{$option\\}"> {
3516    let Inst{31-28} = op31_28;
3517    let Inst{24} = 0; // P = 0
3518    let Inst{23} = 1; // U = 1
3519    let Inst{21} = 0; // W = 0
3520    let Inst{22} = 1; // D = 1
3521    let Inst{20} = load;
3522    let DecoderMethod = "DecodeCopMemInstruction";
3523  }
3524}
3525
3526defm t2LDC  : T2LdStCop<0b1111, 1, "ldc">;
3527defm t2STC  : T2LdStCop<0b1111, 0, "stc">;
3528
3529
3530//===----------------------------------------------------------------------===//
3531// Move between special register and ARM core register -- for disassembly only
3532//
3533// Move to ARM core register from Special Register
3534def t2MRS : T2I<(outs GPR:$Rd), (ins), NoItinerary, "mrs", "\t$Rd, apsr", []> {
3535  bits<4> Rd;
3536  let Inst{31-12} = 0b11110011111011111000;
3537  let Inst{11-8} = Rd;
3538  let Inst{7-0} = 0b0000;
3539}
3540
3541def : t2InstAlias<"mrs${p} $Rd, cpsr", (t2MRS GPR:$Rd, pred:$p)>;
3542
3543def t2MRSsys:T2I<(outs GPR:$Rd), (ins), NoItinerary, "mrs", "\t$Rd, spsr", []> {
3544  bits<4> Rd;
3545  let Inst{31-12} = 0b11110011111111111000;
3546  let Inst{11-8} = Rd;
3547  let Inst{7-0} = 0b0000;
3548}
3549
3550// Move from ARM core register to Special Register
3551//
3552// No need to have both system and application versions, the encodings are the
3553// same and the assembly parser has no way to distinguish between them. The mask
3554// operand contains the special register (R Bit) in bit 4 and bits 3-0 contains
3555// the mask with the fields to be accessed in the special register.
3556def t2MSR : T2I<(outs), (ins msr_mask:$mask, rGPR:$Rn),
3557                NoItinerary, "msr", "\t$mask, $Rn", []> {
3558  bits<5> mask;
3559  bits<4> Rn;
3560  let Inst{31-21} = 0b11110011100;
3561  let Inst{20}    = mask{4}; // R Bit
3562  let Inst{19-16} = Rn;
3563  let Inst{15-12} = 0b1000;
3564  let Inst{11-8}  = mask{3-0};
3565  let Inst{7-0}   = 0;
3566}
3567
3568//===----------------------------------------------------------------------===//
3569// Move between coprocessor and ARM core register
3570//
3571
3572class t2MovRCopro<bits<4> Op, string opc, bit direction, dag oops, dag iops,
3573                  list<dag> pattern>
3574  : T2Cop<Op, oops, iops,
3575          !strconcat(opc, "\t$cop, $opc1, $Rt, $CRn, $CRm, $opc2"),
3576          pattern> {
3577  let Inst{27-24} = 0b1110;
3578  let Inst{20} = direction;
3579  let Inst{4} = 1;
3580
3581  bits<4> Rt;
3582  bits<4> cop;
3583  bits<3> opc1;
3584  bits<3> opc2;
3585  bits<4> CRm;
3586  bits<4> CRn;
3587
3588  let Inst{15-12} = Rt;
3589  let Inst{11-8}  = cop;
3590  let Inst{23-21} = opc1;
3591  let Inst{7-5}   = opc2;
3592  let Inst{3-0}   = CRm;
3593  let Inst{19-16} = CRn;
3594}
3595
3596class t2MovRRCopro<bits<4> Op, string opc, bit direction,
3597                   list<dag> pattern = []>
3598  : T2Cop<Op, (outs),
3599          (ins p_imm:$cop, imm0_15:$opc1, GPR:$Rt, GPR:$Rt2, c_imm:$CRm),
3600          !strconcat(opc, "\t$cop, $opc1, $Rt, $Rt2, $CRm"), pattern> {
3601  let Inst{27-24} = 0b1100;
3602  let Inst{23-21} = 0b010;
3603  let Inst{20} = direction;
3604
3605  bits<4> Rt;
3606  bits<4> Rt2;
3607  bits<4> cop;
3608  bits<4> opc1;
3609  bits<4> CRm;
3610
3611  let Inst{15-12} = Rt;
3612  let Inst{19-16} = Rt2;
3613  let Inst{11-8}  = cop;
3614  let Inst{7-4}   = opc1;
3615  let Inst{3-0}   = CRm;
3616}
3617
3618/* from ARM core register to coprocessor */
3619def t2MCR : t2MovRCopro<0b1110, "mcr", 0,
3620           (outs),
3621           (ins p_imm:$cop, imm0_7:$opc1, GPR:$Rt, c_imm:$CRn,
3622                c_imm:$CRm, imm0_7:$opc2),
3623           [(int_arm_mcr imm:$cop, imm:$opc1, GPR:$Rt, imm:$CRn,
3624                         imm:$CRm, imm:$opc2)]>;
3625def t2MCR2 : t2MovRCopro<0b1111, "mcr2", 0,
3626             (outs), (ins p_imm:$cop, imm0_7:$opc1, GPR:$Rt, c_imm:$CRn,
3627                          c_imm:$CRm, imm0_7:$opc2),
3628             [(int_arm_mcr2 imm:$cop, imm:$opc1, GPR:$Rt, imm:$CRn,
3629                            imm:$CRm, imm:$opc2)]>;
3630
3631/* from coprocessor to ARM core register */
3632def t2MRC : t2MovRCopro<0b1110, "mrc", 1,
3633             (outs GPR:$Rt), (ins p_imm:$cop, imm0_7:$opc1, c_imm:$CRn,
3634                                  c_imm:$CRm, imm0_7:$opc2), []>;
3635
3636def t2MRC2 : t2MovRCopro<0b1111, "mrc2", 1,
3637             (outs GPR:$Rt), (ins p_imm:$cop, imm0_7:$opc1, c_imm:$CRn,
3638                                  c_imm:$CRm, imm0_7:$opc2), []>;
3639
3640def : T2v6Pat<(int_arm_mrc  imm:$cop, imm:$opc1, imm:$CRn, imm:$CRm, imm:$opc2),
3641              (t2MRC imm:$cop, imm:$opc1, imm:$CRn, imm:$CRm, imm:$opc2)>;
3642
3643def : T2v6Pat<(int_arm_mrc2 imm:$cop, imm:$opc1, imm:$CRn, imm:$CRm, imm:$opc2),
3644              (t2MRC2 imm:$cop, imm:$opc1, imm:$CRn, imm:$CRm, imm:$opc2)>;
3645
3646
3647/* from ARM core register to coprocessor */
3648def t2MCRR : t2MovRRCopro<0b1110, "mcrr", 0,
3649                        [(int_arm_mcrr imm:$cop, imm:$opc1, GPR:$Rt, GPR:$Rt2,
3650                                       imm:$CRm)]>;
3651def t2MCRR2 : t2MovRRCopro<0b1111, "mcrr2", 0,
3652                           [(int_arm_mcrr2 imm:$cop, imm:$opc1, GPR:$Rt,
3653                                           GPR:$Rt2, imm:$CRm)]>;
3654/* from coprocessor to ARM core register */
3655def t2MRRC : t2MovRRCopro<0b1110, "mrrc", 1>;
3656
3657def t2MRRC2 : t2MovRRCopro<0b1111, "mrrc2", 1>;
3658
3659//===----------------------------------------------------------------------===//
3660// Other Coprocessor Instructions.
3661//
3662
3663def tCDP : T2Cop<0b1110, (outs), (ins p_imm:$cop, imm0_15:$opc1,
3664                 c_imm:$CRd, c_imm:$CRn, c_imm:$CRm, imm0_7:$opc2),
3665                 "cdp\t$cop, $opc1, $CRd, $CRn, $CRm, $opc2",
3666                 [(int_arm_cdp imm:$cop, imm:$opc1, imm:$CRd, imm:$CRn,
3667                               imm:$CRm, imm:$opc2)]> {
3668  let Inst{27-24} = 0b1110;
3669
3670  bits<4> opc1;
3671  bits<4> CRn;
3672  bits<4> CRd;
3673  bits<4> cop;
3674  bits<3> opc2;
3675  bits<4> CRm;
3676
3677  let Inst{3-0}   = CRm;
3678  let Inst{4}     = 0;
3679  let Inst{7-5}   = opc2;
3680  let Inst{11-8}  = cop;
3681  let Inst{15-12} = CRd;
3682  let Inst{19-16} = CRn;
3683  let Inst{23-20} = opc1;
3684}
3685
3686def t2CDP2 : T2Cop<0b1111, (outs), (ins p_imm:$cop, imm0_15:$opc1,
3687                   c_imm:$CRd, c_imm:$CRn, c_imm:$CRm, imm0_7:$opc2),
3688                   "cdp2\t$cop, $opc1, $CRd, $CRn, $CRm, $opc2",
3689                   [(int_arm_cdp2 imm:$cop, imm:$opc1, imm:$CRd, imm:$CRn,
3690                                  imm:$CRm, imm:$opc2)]> {
3691  let Inst{27-24} = 0b1110;
3692
3693  bits<4> opc1;
3694  bits<4> CRn;
3695  bits<4> CRd;
3696  bits<4> cop;
3697  bits<3> opc2;
3698  bits<4> CRm;
3699
3700  let Inst{3-0}   = CRm;
3701  let Inst{4}     = 0;
3702  let Inst{7-5}   = opc2;
3703  let Inst{11-8}  = cop;
3704  let Inst{15-12} = CRd;
3705  let Inst{19-16} = CRn;
3706  let Inst{23-20} = opc1;
3707}
3708
3709
3710
3711//===----------------------------------------------------------------------===//
3712// Non-Instruction Patterns
3713//
3714
3715// SXT/UXT with no rotate
3716let AddedComplexity = 16 in {
3717def : T2Pat<(and rGPR:$Rm, 0x000000FF), (t2UXTB rGPR:$Rm, 0)>,
3718           Requires<[IsThumb2]>;
3719def : T2Pat<(and rGPR:$Rm, 0x0000FFFF), (t2UXTH rGPR:$Rm, 0)>,
3720           Requires<[IsThumb2]>;
3721def : T2Pat<(and rGPR:$Rm, 0x00FF00FF), (t2UXTB16 rGPR:$Rm, 0)>,
3722           Requires<[HasT2ExtractPack, IsThumb2]>;
3723def : T2Pat<(add rGPR:$Rn, (and rGPR:$Rm, 0x00FF)),
3724            (t2UXTAB rGPR:$Rn, rGPR:$Rm, 0)>,
3725           Requires<[HasT2ExtractPack, IsThumb2]>;
3726def : T2Pat<(add rGPR:$Rn, (and rGPR:$Rm, 0xFFFF)),
3727            (t2UXTAH rGPR:$Rn, rGPR:$Rm, 0)>,
3728           Requires<[HasT2ExtractPack, IsThumb2]>;
3729}
3730
3731def : T2Pat<(sext_inreg rGPR:$Src, i8),  (t2SXTB rGPR:$Src, 0)>,
3732           Requires<[IsThumb2]>;
3733def : T2Pat<(sext_inreg rGPR:$Src, i16), (t2SXTH rGPR:$Src, 0)>,
3734           Requires<[IsThumb2]>;
3735def : T2Pat<(add rGPR:$Rn, (sext_inreg rGPR:$Rm, i8)),
3736            (t2SXTAB rGPR:$Rn, rGPR:$Rm, 0)>,
3737           Requires<[HasT2ExtractPack, IsThumb2]>;
3738def : T2Pat<(add rGPR:$Rn, (sext_inreg rGPR:$Rm, i16)),
3739            (t2SXTAH rGPR:$Rn, rGPR:$Rm, 0)>,
3740           Requires<[HasT2ExtractPack, IsThumb2]>;
3741
3742// Atomic load/store patterns
3743def : T2Pat<(atomic_load_8   t2addrmode_imm12:$addr),
3744            (t2LDRBi12  t2addrmode_imm12:$addr)>;
3745def : T2Pat<(atomic_load_8   t2addrmode_negimm8:$addr),
3746            (t2LDRBi8   t2addrmode_negimm8:$addr)>;
3747def : T2Pat<(atomic_load_8   t2addrmode_so_reg:$addr),
3748            (t2LDRBs    t2addrmode_so_reg:$addr)>;
3749def : T2Pat<(atomic_load_16  t2addrmode_imm12:$addr),
3750            (t2LDRHi12  t2addrmode_imm12:$addr)>;
3751def : T2Pat<(atomic_load_16  t2addrmode_negimm8:$addr),
3752            (t2LDRHi8   t2addrmode_negimm8:$addr)>;
3753def : T2Pat<(atomic_load_16  t2addrmode_so_reg:$addr),
3754            (t2LDRHs    t2addrmode_so_reg:$addr)>;
3755def : T2Pat<(atomic_load_32  t2addrmode_imm12:$addr),
3756            (t2LDRi12   t2addrmode_imm12:$addr)>;
3757def : T2Pat<(atomic_load_32  t2addrmode_negimm8:$addr),
3758            (t2LDRi8    t2addrmode_negimm8:$addr)>;
3759def : T2Pat<(atomic_load_32  t2addrmode_so_reg:$addr),
3760            (t2LDRs     t2addrmode_so_reg:$addr)>;
3761def : T2Pat<(atomic_store_8  t2addrmode_imm12:$addr, GPR:$val),
3762            (t2STRBi12  GPR:$val, t2addrmode_imm12:$addr)>;
3763def : T2Pat<(atomic_store_8  t2addrmode_negimm8:$addr, GPR:$val),
3764            (t2STRBi8   GPR:$val, t2addrmode_negimm8:$addr)>;
3765def : T2Pat<(atomic_store_8  t2addrmode_so_reg:$addr, GPR:$val),
3766            (t2STRBs    GPR:$val, t2addrmode_so_reg:$addr)>;
3767def : T2Pat<(atomic_store_16 t2addrmode_imm12:$addr, GPR:$val),
3768            (t2STRHi12  GPR:$val, t2addrmode_imm12:$addr)>;
3769def : T2Pat<(atomic_store_16 t2addrmode_negimm8:$addr, GPR:$val),
3770            (t2STRHi8   GPR:$val, t2addrmode_negimm8:$addr)>;
3771def : T2Pat<(atomic_store_16 t2addrmode_so_reg:$addr, GPR:$val),
3772            (t2STRHs    GPR:$val, t2addrmode_so_reg:$addr)>;
3773def : T2Pat<(atomic_store_32 t2addrmode_imm12:$addr, GPR:$val),
3774            (t2STRi12   GPR:$val, t2addrmode_imm12:$addr)>;
3775def : T2Pat<(atomic_store_32 t2addrmode_negimm8:$addr, GPR:$val),
3776            (t2STRi8    GPR:$val, t2addrmode_negimm8:$addr)>;
3777def : T2Pat<(atomic_store_32 t2addrmode_so_reg:$addr, GPR:$val),
3778            (t2STRs     GPR:$val, t2addrmode_so_reg:$addr)>;
3779
3780
3781//===----------------------------------------------------------------------===//
3782// Assembler aliases
3783//
3784
3785// Aliases for ADC without the ".w" optional width specifier.
3786def : t2InstAlias<"adc${s}${p} $Rd, $Rn, $Rm",
3787                  (t2ADCrr rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
3788def : t2InstAlias<"adc${s}${p} $Rd, $Rn, $ShiftedRm",
3789                  (t2ADCrs rGPR:$Rd, rGPR:$Rn, t2_so_reg:$ShiftedRm,
3790                           pred:$p, cc_out:$s)>;
3791
3792// Aliases for SBC without the ".w" optional width specifier.
3793def : t2InstAlias<"sbc${s}${p} $Rd, $Rn, $Rm",
3794                  (t2SBCrr rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
3795def : t2InstAlias<"sbc${s}${p} $Rd, $Rn, $ShiftedRm",
3796                  (t2SBCrs rGPR:$Rd, rGPR:$Rn, t2_so_reg:$ShiftedRm,
3797                           pred:$p, cc_out:$s)>;
3798
3799// Aliases for ADD without the ".w" optional width specifier.
3800def : t2InstAlias<"add${s}${p} $Rd, $Rn, $imm",
3801           (t2ADDri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
3802def : t2InstAlias<"add${p} $Rd, $Rn, $imm",
3803           (t2ADDri12 rGPR:$Rd, GPR:$Rn, imm0_4095:$imm, pred:$p)>;
3804def : t2InstAlias<"add${s}${p} $Rd, $Rn, $Rm",
3805                 (t2ADDrr rGPR:$Rd, GPRnopc:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
3806def : t2InstAlias<"add${s}${p} $Rd, $Rn, $ShiftedRm",
3807                  (t2ADDrs rGPR:$Rd, GPRnopc:$Rn, t2_so_reg:$ShiftedRm,
3808                           pred:$p, cc_out:$s)>;
3809
3810// Aliases for SUB without the ".w" optional width specifier.
3811def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $imm",
3812           (t2SUBri rGPR:$Rd, GPRnopc:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
3813def : t2InstAlias<"sub${p} $Rd, $Rn, $imm",
3814           (t2SUBri12 rGPR:$Rd, GPR:$Rn, imm0_4095:$imm, pred:$p)>;
3815def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $Rm",
3816                 (t2SUBrr rGPR:$Rd, GPRnopc:$Rn, rGPR:$Rm, pred:$p, cc_out:$s)>;
3817def : t2InstAlias<"sub${s}${p} $Rd, $Rn, $ShiftedRm",
3818                  (t2SUBrs rGPR:$Rd, GPRnopc:$Rn, t2_so_reg:$ShiftedRm,
3819                           pred:$p, cc_out:$s)>;
3820
3821// Alias for compares without the ".w" optional width specifier.
3822def : t2InstAlias<"cmn${p} $Rn, $Rm",
3823                  (t2CMNzrr GPRnopc:$Rn, rGPR:$Rm, pred:$p)>;
3824def : t2InstAlias<"teq${p} $Rn, $Rm",
3825                  (t2TEQrr GPRnopc:$Rn, rGPR:$Rm, pred:$p)>;
3826def : t2InstAlias<"tst${p} $Rn, $Rm",
3827                  (t2TSTrr GPRnopc:$Rn, rGPR:$Rm, pred:$p)>;
3828
3829// Memory barriers
3830def : InstAlias<"dmb", (t2DMB 0xf)>, Requires<[IsThumb2, HasDB]>;
3831def : InstAlias<"dsb", (t2DSB 0xf)>, Requires<[IsThumb2, HasDB]>;
3832def : InstAlias<"isb", (t2ISB 0xf)>, Requires<[IsThumb2, HasDB]>;
3833
3834// Alias for LDR, LDRB, LDRH, LDRSB, and LDRSH without the ".w" optional
3835// width specifier.
3836def : t2InstAlias<"ldr${p} $Rt, $addr",
3837                  (t2LDRi12 GPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3838def : t2InstAlias<"ldrb${p} $Rt, $addr",
3839                  (t2LDRBi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3840def : t2InstAlias<"ldrh${p} $Rt, $addr",
3841                  (t2LDRHi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3842def : t2InstAlias<"ldrsb${p} $Rt, $addr",
3843                  (t2LDRSBi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3844def : t2InstAlias<"ldrsh${p} $Rt, $addr",
3845                  (t2LDRSHi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3846
3847def : t2InstAlias<"ldr${p} $Rt, $addr",
3848                  (t2LDRs GPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3849def : t2InstAlias<"ldrb${p} $Rt, $addr",
3850                  (t2LDRBs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3851def : t2InstAlias<"ldrh${p} $Rt, $addr",
3852                  (t2LDRHs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3853def : t2InstAlias<"ldrsb${p} $Rt, $addr",
3854                  (t2LDRSBs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3855def : t2InstAlias<"ldrsh${p} $Rt, $addr",
3856                  (t2LDRSHs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3857
3858// Alias for MVN without the ".w" optional width specifier.
3859def : t2InstAlias<"mvn${s}${p} $Rd, $Rm",
3860           (t2MVNr rGPR:$Rd, rGPR:$Rm, pred:$p, cc_out:$s)>;
3861def : t2InstAlias<"mvn${s}${p} $Rd, $ShiftedRm",
3862           (t2MVNs rGPR:$Rd, t2_so_reg:$ShiftedRm, pred:$p, cc_out:$s)>;
3863
3864// PKHBT/PKHTB with default shift amount. PKHTB is equivalent to PKHBT when the
3865// shift amount is zero (i.e., unspecified).
3866def : InstAlias<"pkhbt${p} $Rd, $Rn, $Rm",
3867                (t2PKHBT rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p)>,
3868            Requires<[HasT2ExtractPack, IsThumb2]>;
3869def : InstAlias<"pkhtb${p} $Rd, $Rn, $Rm",
3870                (t2PKHBT rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p)>,
3871            Requires<[HasT2ExtractPack, IsThumb2]>;
3872
3873// PUSH/POP aliases for STM/LDM
3874def : t2InstAlias<"push${p}.w $regs", (t2STMDB_UPD SP, pred:$p, reglist:$regs)>;
3875def : t2InstAlias<"push${p} $regs", (t2STMDB_UPD SP, pred:$p, reglist:$regs)>;
3876def : t2InstAlias<"pop${p}.w $regs", (t2LDMIA_UPD SP, pred:$p, reglist:$regs)>;
3877def : t2InstAlias<"pop${p} $regs", (t2LDMIA_UPD SP, pred:$p, reglist:$regs)>;
3878
3879// Alias for REV/REV16/REVSH without the ".w" optional width specifier.
3880def : t2InstAlias<"rev${p} $Rd, $Rm", (t2REV rGPR:$Rd, rGPR:$Rm, pred:$p)>;
3881def : t2InstAlias<"rev16${p} $Rd, $Rm", (t2REV16 rGPR:$Rd, rGPR:$Rm, pred:$p)>;
3882def : t2InstAlias<"revsh${p} $Rd, $Rm", (t2REVSH rGPR:$Rd, rGPR:$Rm, pred:$p)>;
3883
3884
3885// Alias for RSB without the ".w" optional width specifier, and with optional
3886// implied destination register.
3887def : t2InstAlias<"rsb${s}${p} $Rd, $Rn, $imm",
3888           (t2RSBri rGPR:$Rd, rGPR:$Rn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
3889def : t2InstAlias<"rsb${s}${p} $Rdn, $imm",
3890           (t2RSBri rGPR:$Rdn, rGPR:$Rdn, t2_so_imm:$imm, pred:$p, cc_out:$s)>;
3891def : t2InstAlias<"rsb${s}${p} $Rdn, $Rm",
3892           (t2RSBrr rGPR:$Rdn, rGPR:$Rdn, rGPR:$Rm, pred:$p, cc_out:$s)>;
3893def : t2InstAlias<"rsb${s}${p} $Rdn, $ShiftedRm",
3894           (t2RSBrs rGPR:$Rdn, rGPR:$Rdn, t2_so_reg:$ShiftedRm, pred:$p,
3895                    cc_out:$s)>;
3896
3897// SSAT/USAT optional shift operand.
3898def : t2InstAlias<"ssat${p} $Rd, $sat_imm, $Rn",
3899                  (t2SSAT rGPR:$Rd, imm1_32:$sat_imm, rGPR:$Rn, 0, pred:$p)>;
3900def : t2InstAlias<"usat${p} $Rd, $sat_imm, $Rn",
3901                  (t2USAT rGPR:$Rd, imm0_31:$sat_imm, rGPR:$Rn, 0, pred:$p)>;
3902
3903// STM w/o the .w suffix.
3904def : t2InstAlias<"stm${p} $Rn, $regs",
3905                  (t2STMIA GPR:$Rn, pred:$p, reglist:$regs)>;
3906
3907// Alias for STR, STRB, and STRH without the ".w" optional
3908// width specifier.
3909def : t2InstAlias<"str${p} $Rt, $addr",
3910                  (t2STRi12 GPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3911def : t2InstAlias<"strb${p} $Rt, $addr",
3912                  (t2STRBi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3913def : t2InstAlias<"strh${p} $Rt, $addr",
3914                  (t2STRHi12 rGPR:$Rt, t2addrmode_imm12:$addr, pred:$p)>;
3915
3916def : t2InstAlias<"str${p} $Rt, $addr",
3917                  (t2STRs GPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3918def : t2InstAlias<"strb${p} $Rt, $addr",
3919                  (t2STRBs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3920def : t2InstAlias<"strh${p} $Rt, $addr",
3921                  (t2STRHs rGPR:$Rt, t2addrmode_so_reg:$addr, pred:$p)>;
3922
3923// Extend instruction optional rotate operand.
3924def : t2InstAlias<"sxtab${p} $Rd, $Rn, $Rm",
3925                (t2SXTAB rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p)>;
3926def : t2InstAlias<"sxtah${p} $Rd, $Rn, $Rm",
3927                (t2SXTAH rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p)>;
3928def : t2InstAlias<"sxtab16${p} $Rd, $Rn, $Rm",
3929                (t2SXTAB16 rGPR:$Rd, rGPR:$Rn, rGPR:$Rm, 0, pred:$p)>;
3930def : t2InstAlias<"sxtb${p} $Rd, $Rm",
3931                (t2SXTB rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
3932def : t2InstAlias<"sxtb16${p} $Rd, $Rm",
3933                (t2SXTB16 rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
3934def : t2InstAlias<"sxth${p} $Rd, $Rm",
3935                (t2SXTH rGPR:$Rd, rGPR:$Rm, 0, pred:$p)>;
3936
3937// Extend instruction w/o the ".w" optional width specifier.
3938def : t2InstAlias<"sxtb${p} $Rd, $Rm$rot",
3939                  (t2SXTB rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
3940def : t2InstAlias<"sxtb16${p} $Rd, $Rm$rot",
3941                  (t2SXTB16 rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
3942def : t2InstAlias<"sxth${p} $Rd, $Rm$rot",
3943                  (t2SXTH rGPR:$Rd, rGPR:$Rm, rot_imm:$rot, pred:$p)>;
3944