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