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