7305e4950db19ad7f04c82ef1759da10856a0b39
[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 : Operand<i32> {
16   let PrintMethod = "printMandatoryPredicateOperand";
17 }
18
19 // IT block condition mask
20 def it_mask : Operand<i32> {
21   let PrintMethod = "printThumbITMask";
22 }
23
24 // Table branch address
25 def tb_addrmode : Operand<i32> {
26   let PrintMethod = "printTBAddrMode";
27 }
28
29 // Shifted operands. No register controlled shifts for Thumb2.
30 // Note: We do not support rrx shifted operands yet.
31 def t2_so_reg : Operand<i32>,    // reg imm
32                 ComplexPattern<i32, 2, "SelectT2ShifterOperandReg",
33                                [shl,srl,sra,rotr]> {
34   string EncoderMethod = "getT2SORegOpValue";
35   let PrintMethod = "printT2SOOperand";
36   let MIOperandInfo = (ops rGPR, i32imm);
37 }
38
39 // t2_so_imm_not_XFORM - Return the complement of a t2_so_imm value
40 def t2_so_imm_not_XFORM : SDNodeXForm<imm, [{
41   return CurDAG->getTargetConstant(~((uint32_t)N->getZExtValue()), MVT::i32);
42 }]>;
43
44 // t2_so_imm_neg_XFORM - Return the negation of a t2_so_imm value
45 def t2_so_imm_neg_XFORM : SDNodeXForm<imm, [{
46   return CurDAG->getTargetConstant(-((int)N->getZExtValue()), MVT::i32);
47 }]>;
48
49 // t2_so_imm - Match a 32-bit immediate operand, which is an
50 // 8-bit immediate rotated by an arbitrary number of bits, or an 8-bit
51 // immediate splatted into multiple bytes of the word. t2_so_imm values are
52 // represented in the imm field in the same 12-bit form that they are encoded
53 // into t2_so_imm instructions: the 8-bit immediate is the least significant
54 // bits [bits 0-7], the 4-bit shift/splat amount is the next 4 bits [bits 8-11].
55 def t2_so_imm : Operand<i32>, PatLeaf<(imm), [{ return Pred_t2_so_imm(N); }]> {
56   string EncoderMethod = "getT2SOImmOpValue";
57 }
58
59 // t2_so_imm_not - Match an immediate that is a complement
60 // of a t2_so_imm.
61 def t2_so_imm_not : Operand<i32>,
62                     PatLeaf<(imm), [{
63   return ARM_AM::getT2SOImmVal(~((uint32_t)N->getZExtValue())) != -1;
64 }], t2_so_imm_not_XFORM>;
65
66 // t2_so_imm_neg - Match an immediate that is a negation of a t2_so_imm.
67 def t2_so_imm_neg : Operand<i32>,
68                     PatLeaf<(imm), [{
69   return ARM_AM::getT2SOImmVal(-((uint32_t)N->getZExtValue())) != -1;
70 }], t2_so_imm_neg_XFORM>;
71
72 // Break t2_so_imm's up into two pieces.  This handles immediates with up to 16
73 // bits set in them.  This uses t2_so_imm2part to match and t2_so_imm2part_[12]
74 // to get the first/second pieces.
75 def t2_so_imm2part : Operand<i32>,
76                   PatLeaf<(imm), [{
77       return ARM_AM::isT2SOImmTwoPartVal((unsigned)N->getZExtValue());
78     }]> {
79 }
80
81 def t2_so_imm2part_1 : SDNodeXForm<imm, [{
82   unsigned V = ARM_AM::getT2SOImmTwoPartFirst((unsigned)N->getZExtValue());
83   return CurDAG->getTargetConstant(V, MVT::i32);
84 }]>;
85
86 def t2_so_imm2part_2 : SDNodeXForm<imm, [{
87   unsigned V = ARM_AM::getT2SOImmTwoPartSecond((unsigned)N->getZExtValue());
88   return CurDAG->getTargetConstant(V, MVT::i32);
89 }]>;
90
91 def t2_so_neg_imm2part : Operand<i32>, PatLeaf<(imm), [{
92       return ARM_AM::isT2SOImmTwoPartVal(-(int)N->getZExtValue());
93     }]> {
94 }
95
96 def t2_so_neg_imm2part_1 : SDNodeXForm<imm, [{
97   unsigned V = ARM_AM::getT2SOImmTwoPartFirst(-(int)N->getZExtValue());
98   return CurDAG->getTargetConstant(V, MVT::i32);
99 }]>;
100
101 def t2_so_neg_imm2part_2 : SDNodeXForm<imm, [{
102   unsigned V = ARM_AM::getT2SOImmTwoPartSecond(-(int)N->getZExtValue());
103   return CurDAG->getTargetConstant(V, MVT::i32);
104 }]>;
105
106 /// imm1_31 predicate - True if the 32-bit immediate is in the range [1,31].
107 def imm1_31 : PatLeaf<(i32 imm), [{
108   return (int32_t)N->getZExtValue() >= 1 && (int32_t)N->getZExtValue() < 32;
109 }]>;
110
111 /// imm0_4095 predicate - True if the 32-bit immediate is in the range [0.4095].
112 def imm0_4095 : Operand<i32>,
113                 PatLeaf<(i32 imm), [{
114   return (uint32_t)N->getZExtValue() < 4096;
115 }]>;
116
117 def imm0_4095_neg : PatLeaf<(i32 imm), [{
118  return (uint32_t)(-N->getZExtValue()) < 4096;
119 }], imm_neg_XFORM>;
120
121 def imm0_255_neg : PatLeaf<(i32 imm), [{
122   return (uint32_t)(-N->getZExtValue()) < 255;
123 }], imm_neg_XFORM>;
124
125 def imm0_255_not : PatLeaf<(i32 imm), [{
126   return (uint32_t)(~N->getZExtValue()) < 255;
127 }], imm_comp_XFORM>;
128
129 // Define Thumb2 specific addressing modes.
130
131 // t2addrmode_imm12  := reg + imm12
132 def t2addrmode_imm12 : Operand<i32>,
133                        ComplexPattern<i32, 2, "SelectT2AddrModeImm12", []> {
134   let PrintMethod = "printAddrModeImm12Operand";
135   let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
136 }
137
138 // t2addrmode_imm8  := reg +/- imm8
139 def t2addrmode_imm8 : Operand<i32>,
140                       ComplexPattern<i32, 2, "SelectT2AddrModeImm8", []> {
141   let PrintMethod = "printT2AddrModeImm8Operand";
142   let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
143 }
144
145 def t2am_imm8_offset : Operand<i32>,
146                        ComplexPattern<i32, 1, "SelectT2AddrModeImm8Offset",
147                                       [], [SDNPWantRoot]> {
148   let PrintMethod = "printT2AddrModeImm8OffsetOperand";
149 }
150
151 // t2addrmode_imm8s4  := reg +/- (imm8 << 2)
152 def t2addrmode_imm8s4 : Operand<i32> {
153   let PrintMethod = "printT2AddrModeImm8s4Operand";
154   let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
155 }
156
157 def t2am_imm8s4_offset : Operand<i32> {
158   let PrintMethod = "printT2AddrModeImm8s4OffsetOperand";
159 }
160
161 // t2addrmode_so_reg  := reg + (reg << imm2)
162 def t2addrmode_so_reg : Operand<i32>,
163                         ComplexPattern<i32, 3, "SelectT2AddrModeSoReg", []> {
164   let PrintMethod = "printT2AddrModeSoRegOperand";
165   let MIOperandInfo = (ops GPR:$base, rGPR:$offsreg, i32imm:$offsimm);
166 }
167
168
169 //===----------------------------------------------------------------------===//
170 // Multiclass helpers...
171 //
172
173 class T2TwoRegShiftedImm<dag oops, dag iops, InstrItinClass itin,
174            string opc, string asm, list<dag> pattern>
175   : T2sI<oops, iops, itin, opc, asm, pattern> {
176   bits<4> Rd;
177   bits<4> Rn;
178   bits<12> imm;
179   
180   let Inst{11-8}  = Rd{3-0};
181   let Inst{19-16} = Rn{3-0};
182   let Inst{26}    = imm{11};
183   let Inst{14-12} = imm{10-8};
184   let Inst{7-0}   = imm{7-0};
185 }
186
187 class T2ThreeReg<dag oops, dag iops, InstrItinClass itin,
188            string opc, string asm, list<dag> pattern>
189   : T2sI<oops, iops, itin, opc, asm, pattern> { 
190   bits<4> Rd;
191   bits<4> Rn;
192   bits<4> Rm;
193      
194   let Inst{11-8}  = Rd{3-0};
195   let Inst{19-16} = Rn{3-0};
196   let Inst{3-0}   = Rm{3-0};
197 }
198
199 class T2TwoRegShiftedReg<dag oops, dag iops, InstrItinClass itin,
200            string opc, string asm, list<dag> pattern>
201   : T2sI<oops, iops, itin, opc, asm, pattern> {
202   bits<4> Rd;
203   bits<4> Rn;
204   bits<12> ShiftedRm;
205   
206   let Inst{11-8}  = Rd{3-0};
207   let Inst{19-16} = Rn{3-0};
208   let Inst{3-0}   = ShiftedRm{3-0};
209   let Inst{5-4}   = ShiftedRm{6-5};
210   let Inst{14-12} = ShiftedRm{11-9};
211   let Inst{7-6}   = ShiftedRm{8-7};
212 }
213
214 /// T2I_un_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns for a
215 /// unary operation that produces a value. These are predicable and can be
216 /// changed to modify CPSR.
217 multiclass T2I_un_irs<bits<4> opcod, string opc,
218                      InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
219                       PatFrag opnode, bit Cheap = 0, bit ReMat = 0> {
220    // shifted imm
221    def i : T2sI<(outs rGPR:$dst), (ins t2_so_imm:$src), iii,
222                 opc, "\t$dst, $src",
223                 [(set rGPR:$dst, (opnode t2_so_imm:$src))]> {
224      let isAsCheapAsAMove = Cheap;
225      let isReMaterializable = ReMat;
226      let Inst{31-27} = 0b11110;
227      let Inst{25} = 0;
228      let Inst{24-21} = opcod;
229      let Inst{20} = ?; // The S bit.
230      let Inst{19-16} = 0b1111; // Rn
231      let Inst{15} = 0;
232    }
233    // register
234    def r : T2sI<(outs rGPR:$dst), (ins rGPR:$src), iir,
235                 opc, ".w\t$dst, $src",
236                 [(set rGPR:$dst, (opnode rGPR:$src))]> {
237      let Inst{31-27} = 0b11101;
238      let Inst{26-25} = 0b01;
239      let Inst{24-21} = opcod;
240      let Inst{20} = ?; // The S bit.
241      let Inst{19-16} = 0b1111; // Rn
242      let Inst{14-12} = 0b000; // imm3
243      let Inst{7-6} = 0b00; // imm2
244      let Inst{5-4} = 0b00; // type
245    }
246    // shifted register
247    def s : T2sI<(outs rGPR:$dst), (ins t2_so_reg:$src), iis,
248                 opc, ".w\t$dst, $src",
249                 [(set rGPR:$dst, (opnode t2_so_reg:$src))]> {
250      let Inst{31-27} = 0b11101;
251      let Inst{26-25} = 0b01;
252      let Inst{24-21} = opcod;
253      let Inst{20} = ?; // The S bit.
254      let Inst{19-16} = 0b1111; // Rn
255    }
256 }
257
258 /// T2I_bin_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns for a
259 /// binary operation that produces a value. These are predicable and can be
260 /// changed to modify CPSR.
261 multiclass T2I_bin_irs<bits<4> opcod, string opc,
262                      InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
263                        PatFrag opnode, bit Commutable = 0, string wide = ""> {
264    // shifted imm
265    def ri : T2sI<(outs rGPR:$dst), (ins rGPR:$lhs, t2_so_imm:$rhs), iii,
266                  opc, "\t$dst, $lhs, $rhs",
267                  [(set rGPR:$dst, (opnode rGPR:$lhs, t2_so_imm:$rhs))]> {
268      let Inst{31-27} = 0b11110;
269      let Inst{25} = 0;
270      let Inst{24-21} = opcod;
271      let Inst{20} = ?; // The S bit.
272      let Inst{15} = 0;
273    }
274    // register
275    def rr : T2sI<(outs rGPR:$dst), (ins rGPR:$lhs, rGPR:$rhs), iir,
276                  opc, !strconcat(wide, "\t$dst, $lhs, $rhs"),
277                  [(set rGPR:$dst, (opnode rGPR:$lhs, rGPR:$rhs))]> {
278      let isCommutable = Commutable;
279      let Inst{31-27} = 0b11101;
280      let Inst{26-25} = 0b01;
281      let Inst{24-21} = opcod;
282      let Inst{20} = ?; // The S bit.
283      let Inst{14-12} = 0b000; // imm3
284      let Inst{7-6} = 0b00; // imm2
285      let Inst{5-4} = 0b00; // type
286    }
287    // shifted register
288    def rs : T2sI<(outs rGPR:$dst), (ins rGPR:$lhs, t2_so_reg:$rhs), iis,
289                  opc, !strconcat(wide, "\t$dst, $lhs, $rhs"),
290                  [(set rGPR:$dst, (opnode rGPR:$lhs, t2_so_reg:$rhs))]> {
291      let Inst{31-27} = 0b11101;
292      let Inst{26-25} = 0b01;
293      let Inst{24-21} = opcod;
294      let Inst{20} = ?; // The S bit.
295    }
296 }
297
298 /// T2I_bin_w_irs - Same as T2I_bin_irs except these operations need
299 //  the ".w" prefix to indicate that they are wide.
300 multiclass T2I_bin_w_irs<bits<4> opcod, string opc,
301                      InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
302                          PatFrag opnode, bit Commutable = 0> :
303     T2I_bin_irs<opcod, opc, iii, iir, iis, opnode, Commutable, ".w">;
304
305 /// T2I_rbin_is - Same as T2I_bin_irs except the order of operands are
306 /// reversed.  The 'rr' form is only defined for the disassembler; for codegen
307 /// it is equivalent to the T2I_bin_irs counterpart.
308 multiclass T2I_rbin_irs<bits<4> opcod, string opc, PatFrag opnode> {
309    // shifted imm
310    def ri : T2sI<(outs rGPR:$dst), (ins rGPR:$rhs, t2_so_imm:$lhs), IIC_iALUi,
311                  opc, ".w\t$dst, $rhs, $lhs",
312                  [(set rGPR:$dst, (opnode t2_so_imm:$lhs, rGPR:$rhs))]> {
313      let Inst{31-27} = 0b11110;
314      let Inst{25} = 0;
315      let Inst{24-21} = opcod;
316      let Inst{20} = ?; // The S bit.
317      let Inst{15} = 0;
318    }
319    // register
320    def rr : T2sI<(outs rGPR:$dst), (ins rGPR:$rhs, rGPR:$lhs), IIC_iALUr,
321                  opc, "\t$dst, $rhs, $lhs",
322                  [/* For disassembly only; pattern left blank */]> {
323      let Inst{31-27} = 0b11101;
324      let Inst{26-25} = 0b01;
325      let Inst{24-21} = opcod;
326      let Inst{20} = ?; // The S bit.
327      let Inst{14-12} = 0b000; // imm3
328      let Inst{7-6} = 0b00; // imm2
329      let Inst{5-4} = 0b00; // type
330    }
331    // shifted register
332    def rs : T2sI<(outs rGPR:$dst), (ins rGPR:$rhs, t2_so_reg:$lhs), IIC_iALUsir,
333                  opc, "\t$dst, $rhs, $lhs",
334                  [(set rGPR:$dst, (opnode t2_so_reg:$lhs, rGPR:$rhs))]> {
335      let Inst{31-27} = 0b11101;
336      let Inst{26-25} = 0b01;
337      let Inst{24-21} = opcod;
338      let Inst{20} = ?; // The S bit.
339    }
340 }
341
342 /// T2I_bin_s_irs - Similar to T2I_bin_irs except it sets the 's' bit so the
343 /// instruction modifies the CPSR register.
344 let Defs = [CPSR] in {
345 multiclass T2I_bin_s_irs<bits<4> opcod, string opc,
346                      InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
347                          PatFrag opnode, bit Commutable = 0> {
348    // shifted imm
349    def ri : T2I<(outs rGPR:$dst), (ins GPR:$lhs, t2_so_imm:$rhs), iii,
350                 !strconcat(opc, "s"), ".w\t$dst, $lhs, $rhs",
351                 [(set rGPR:$dst, (opnode GPR:$lhs, t2_so_imm:$rhs))]> {
352      let Inst{31-27} = 0b11110;
353      let Inst{25} = 0;
354      let Inst{24-21} = opcod;
355      let Inst{20} = 1; // The S bit.
356      let Inst{15} = 0;
357    }
358    // register
359    def rr : T2I<(outs rGPR:$dst), (ins GPR:$lhs, rGPR:$rhs), iir,
360                 !strconcat(opc, "s"), ".w\t$dst, $lhs, $rhs",
361                 [(set rGPR:$dst, (opnode GPR:$lhs, rGPR:$rhs))]> {
362      let isCommutable = Commutable;
363      let Inst{31-27} = 0b11101;
364      let Inst{26-25} = 0b01;
365      let Inst{24-21} = opcod;
366      let Inst{20} = 1; // The S bit.
367      let Inst{14-12} = 0b000; // imm3
368      let Inst{7-6} = 0b00; // imm2
369      let Inst{5-4} = 0b00; // type
370    }
371    // shifted register
372    def rs : T2I<(outs rGPR:$dst), (ins GPR:$lhs, t2_so_reg:$rhs), iis,
373                 !strconcat(opc, "s"), ".w\t$dst, $lhs, $rhs",
374                 [(set rGPR:$dst, (opnode GPR:$lhs, t2_so_reg:$rhs))]> {
375      let Inst{31-27} = 0b11101;
376      let Inst{26-25} = 0b01;
377      let Inst{24-21} = opcod;
378      let Inst{20} = 1; // The S bit.
379    }
380 }
381 }
382
383 /// T2I_bin_ii12rs - Defines a set of (op reg, {so_imm|imm0_4095|r|so_reg})
384 /// patterns for a binary operation that produces a value.
385 multiclass T2I_bin_ii12rs<bits<3> op23_21, string opc, PatFrag opnode,
386                           bit Commutable = 0> {
387    // shifted imm
388    // The register-immediate version is re-materializable. This is useful
389    // in particular for taking the address of a local.
390    let isReMaterializable = 1 in {
391    def ri : T2sI<(outs rGPR:$dst), (ins GPR:$lhs, t2_so_imm:$rhs), IIC_iALUi,
392                  opc, ".w\t$dst, $lhs, $rhs",
393                  [(set rGPR:$dst, (opnode GPR:$lhs, t2_so_imm:$rhs))]> {
394      let Inst{31-27} = 0b11110;
395      let Inst{25} = 0;
396      let Inst{24} = 1;
397      let Inst{23-21} = op23_21;
398      let Inst{20} = 0; // The S bit.
399      let Inst{15} = 0;
400    }
401    }
402    // 12-bit imm
403    def ri12 : T2I<(outs rGPR:$dst), (ins GPR:$lhs, imm0_4095:$rhs), IIC_iALUi,
404                   !strconcat(opc, "w"), "\t$dst, $lhs, $rhs",
405                   [(set rGPR:$dst, (opnode GPR:$lhs, imm0_4095:$rhs))]> {
406      let Inst{31-27} = 0b11110;
407      let Inst{25} = 1;
408      let Inst{24} = 0;
409      let Inst{23-21} = op23_21;
410      let Inst{20} = 0; // The S bit.
411      let Inst{15} = 0;
412    }
413    // register
414    def rr : T2sI<(outs rGPR:$dst), (ins GPR:$lhs, rGPR:$rhs), IIC_iALUr,
415                  opc, ".w\t$dst, $lhs, $rhs",
416                  [(set rGPR:$dst, (opnode GPR:$lhs, rGPR:$rhs))]> {
417      let isCommutable = Commutable;
418      let Inst{31-27} = 0b11101;
419      let Inst{26-25} = 0b01;
420      let Inst{24} = 1;
421      let Inst{23-21} = op23_21;
422      let Inst{20} = 0; // The S bit.
423      let Inst{14-12} = 0b000; // imm3
424      let Inst{7-6} = 0b00; // imm2
425      let Inst{5-4} = 0b00; // type
426    }
427    // shifted register
428    def rs : T2sI<(outs rGPR:$dst), (ins GPR:$lhs, t2_so_reg:$rhs), IIC_iALUsi,
429                  opc, ".w\t$dst, $lhs, $rhs",
430                  [(set rGPR:$dst, (opnode GPR:$lhs, t2_so_reg:$rhs))]> {
431      let Inst{31-27} = 0b11101;
432      let Inst{26-25} = 0b01;
433      let Inst{24} = 1;
434      let Inst{23-21} = op23_21;
435      let Inst{20} = 0; // The S bit.
436    }
437 }
438
439 /// T2I_adde_sube_irs - Defines a set of (op reg, {so_imm|r|so_reg}) patterns
440 /// for a binary operation that produces a value and use the carry
441 /// bit. It's not predicable.
442 let Uses = [CPSR] in {
443 multiclass T2I_adde_sube_irs<bits<4> opcod, string opc, PatFrag opnode,
444                              bit Commutable = 0> {
445    // shifted imm
446    def ri : T2TwoRegShiftedImm<(outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm),
447                  IIC_iALUi, opc, "\t$Rd, $Rn, $imm",
448                  [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_imm:$imm))]>,
449                  Requires<[IsThumb2]> {
450      let Inst{31-27} = 0b11110;
451      let Inst{25} = 0;
452      let Inst{24-21} = opcod;
453      let Inst{20} = 0; // The S bit.
454      let Inst{15} = 0;
455    }
456    // register
457    def rr : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUr,
458                  opc, ".w\t$Rd, $Rn, $Rm",
459                  [(set rGPR:$Rd, (opnode rGPR:$Rn, rGPR:$Rm))]>,
460                  Requires<[IsThumb2]> {
461      let isCommutable = Commutable;
462      let Inst{31-27} = 0b11101;
463      let Inst{26-25} = 0b01;
464      let Inst{24-21} = opcod;
465      let Inst{20} = 0; // The S bit.
466      let Inst{14-12} = 0b000; // imm3
467      let Inst{7-6} = 0b00; // imm2
468      let Inst{5-4} = 0b00; // type
469    }
470    // shifted register
471    def rs : T2TwoRegShiftedReg<
472                  (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm), 
473                  IIC_iALUsi, opc, ".w\t$Rd, $Rn, $ShiftedRm",
474                  [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_reg:$ShiftedRm))]>,
475                  Requires<[IsThumb2]> {
476      let Inst{31-27} = 0b11101;
477      let Inst{26-25} = 0b01;
478      let Inst{24-21} = opcod;
479      let Inst{20} = 0; // The S bit.
480    }
481 }
482
483 // Carry setting variants
484 let Defs = [CPSR] in {
485 multiclass T2I_adde_sube_s_irs<bits<4> opcod, string opc, PatFrag opnode,
486                                bit Commutable = 0> {
487    // shifted imm
488    def ri : T2TwoRegShiftedImm<
489                  (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_imm:$imm), IIC_iALUi,
490                  opc, "\t$Rd, $Rn, $imm",
491                  [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_imm:$imm))]>,
492                  Requires<[IsThumb2]> {
493      let Inst{31-27} = 0b11110;
494      let Inst{25} = 0;
495      let Inst{24-21} = opcod;
496      let Inst{20} = 1; // The S bit.
497      let Inst{15} = 0;
498    }
499    // register
500    def rr : T2ThreeReg<(outs rGPR:$Rd), (ins rGPR:$Rn, rGPR:$Rm), IIC_iALUr,
501                  opc, ".w\t$Rd, $Rn, $Rm",
502                  [(set rGPR:$Rd, (opnode rGPR:$Rn, rGPR:$Rm))]>,
503                  Requires<[IsThumb2]> {
504      let isCommutable = Commutable;
505      let Inst{31-27} = 0b11101;
506      let Inst{26-25} = 0b01;
507      let Inst{24-21} = opcod;
508      let Inst{20} = 1; // The S bit.
509      let Inst{14-12} = 0b000; // imm3
510      let Inst{7-6} = 0b00; // imm2
511      let Inst{5-4} = 0b00; // type
512    }
513    // shifted register
514    def rs : T2TwoRegShiftedReg<
515                  (outs rGPR:$Rd), (ins rGPR:$Rn, t2_so_reg:$ShiftedRm),
516                  IIC_iALUsi, opc, ".w\t$Rd, $Rn, $ShiftedRm",
517                  [(set rGPR:$Rd, (opnode rGPR:$Rn, t2_so_reg:$ShiftedRm))]>,
518                  Requires<[IsThumb2]> {
519      let Inst{31-27} = 0b11101;
520      let Inst{26-25} = 0b01;
521      let Inst{24-21} = opcod;
522      let Inst{20} = 1; // The S bit.
523    }
524 }
525 }
526 }
527
528 /// T2I_rbin_s_is - Same as T2I_rbin_irs except sets 's' bit and the register
529 /// version is not needed since this is only for codegen.
530 let Defs = [CPSR] in {
531 multiclass T2I_rbin_s_is<bits<4> opcod, string opc, PatFrag opnode> {
532    // shifted imm
533    def ri : T2I<(outs rGPR:$dst), (ins rGPR:$rhs, t2_so_imm:$lhs), IIC_iALUi,
534                 !strconcat(opc, "s"), ".w\t$dst, $rhs, $lhs",
535                 [(set rGPR:$dst, (opnode t2_so_imm:$lhs, rGPR:$rhs))]> {
536      let Inst{31-27} = 0b11110;
537      let Inst{25} = 0;
538      let Inst{24-21} = opcod;
539      let Inst{20} = 1; // The S bit.
540      let Inst{15} = 0;
541    }
542    // shifted register
543    def rs : T2I<(outs rGPR:$dst), (ins rGPR:$rhs, t2_so_reg:$lhs), IIC_iALUsi,
544                 !strconcat(opc, "s"), "\t$dst, $rhs, $lhs",
545                 [(set rGPR:$dst, (opnode t2_so_reg:$lhs, rGPR:$rhs))]> {
546      let Inst{31-27} = 0b11101;
547      let Inst{26-25} = 0b01;
548      let Inst{24-21} = opcod;
549      let Inst{20} = 1; // The S bit.
550    }
551 }
552 }
553
554 /// T2I_sh_ir - Defines a set of (op reg, {so_imm|r}) patterns for a shift /
555 //  rotate operation that produces a value.
556 multiclass T2I_sh_ir<bits<2> opcod, string opc, PatFrag opnode> {
557    // 5-bit imm
558    def ri : T2sI<(outs rGPR:$dst), (ins rGPR:$lhs, i32imm:$rhs), IIC_iMOVsi,
559                  opc, ".w\t$dst, $lhs, $rhs",
560                  [(set rGPR:$dst, (opnode rGPR:$lhs, imm1_31:$rhs))]> {
561      let Inst{31-27} = 0b11101;
562      let Inst{26-21} = 0b010010;
563      let Inst{19-16} = 0b1111; // Rn
564      let Inst{5-4} = opcod;
565    }
566    // register
567    def rr : T2sI<(outs rGPR:$dst), (ins rGPR:$lhs, rGPR:$rhs), IIC_iMOVsr,
568                  opc, ".w\t$dst, $lhs, $rhs",
569                  [(set rGPR:$dst, (opnode rGPR:$lhs, rGPR:$rhs))]> {
570      let Inst{31-27} = 0b11111;
571      let Inst{26-23} = 0b0100;
572      let Inst{22-21} = opcod;
573      let Inst{15-12} = 0b1111;
574      let Inst{7-4} = 0b0000;
575    }
576 }
577
578 /// T2I_cmp_irs - Defines a set of (op r, {so_imm|r|so_reg}) cmp / test
579 /// patterns. Similar to T2I_bin_irs except the instruction does not produce
580 /// a explicit result, only implicitly set CPSR.
581 let isCompare = 1, Defs = [CPSR] in {
582 multiclass T2I_cmp_irs<bits<4> opcod, string opc,
583                      InstrItinClass iii, InstrItinClass iir, InstrItinClass iis,
584                        PatFrag opnode> {
585    // shifted imm
586    def ri : T2I<(outs), (ins GPR:$lhs, t2_so_imm:$rhs), iii,
587                 opc, ".w\t$lhs, $rhs",
588                 [(opnode GPR:$lhs, t2_so_imm:$rhs)]> {
589      let Inst{31-27} = 0b11110;
590      let Inst{25} = 0;
591      let Inst{24-21} = opcod;
592      let Inst{20} = 1; // The S bit.
593      let Inst{15} = 0;
594      let Inst{11-8} = 0b1111; // Rd
595    }
596    // register
597    def rr : T2I<(outs), (ins GPR:$lhs, rGPR:$rhs), iir,
598                 opc, ".w\t$lhs, $rhs",
599                 [(opnode GPR:$lhs, rGPR:$rhs)]> {
600      let Inst{31-27} = 0b11101;
601      let Inst{26-25} = 0b01;
602      let Inst{24-21} = opcod;
603      let Inst{20} = 1; // The S bit.
604      let Inst{14-12} = 0b000; // imm3
605      let Inst{11-8} = 0b1111; // Rd
606      let Inst{7-6} = 0b00; // imm2
607      let Inst{5-4} = 0b00; // type
608    }
609    // shifted register
610    def rs : T2I<(outs), (ins GPR:$lhs, t2_so_reg:$rhs), iis,
611                 opc, ".w\t$lhs, $rhs",
612                 [(opnode GPR:$lhs, t2_so_reg:$rhs)]> {
613      let Inst{31-27} = 0b11101;
614      let Inst{26-25} = 0b01;
615      let Inst{24-21} = opcod;
616      let Inst{20} = 1; // The S bit.
617      let Inst{11-8} = 0b1111; // Rd
618    }
619 }
620 }
621
622 /// T2I_ld - Defines a set of (op r, {imm12|imm8|so_reg}) load patterns.
623 multiclass T2I_ld<bit signed, bits<2> opcod, string opc,
624                   InstrItinClass iii, InstrItinClass iis, PatFrag opnode> {
625   def i12 : T2Ii12<(outs GPR:$dst), (ins t2addrmode_imm12:$addr), iii,
626                    opc, ".w\t$dst, $addr",
627                    [(set GPR:$dst, (opnode t2addrmode_imm12:$addr))]> {
628     let Inst{31-27} = 0b11111;
629     let Inst{26-25} = 0b00;
630     let Inst{24} = signed;
631     let Inst{23} = 1;
632     let Inst{22-21} = opcod;
633     let Inst{20} = 1; // load
634   }
635   def i8  : T2Ii8 <(outs GPR:$dst), (ins t2addrmode_imm8:$addr), iii,
636                    opc, "\t$dst, $addr",
637                    [(set GPR:$dst, (opnode t2addrmode_imm8:$addr))]> {
638     let Inst{31-27} = 0b11111;
639     let Inst{26-25} = 0b00;
640     let Inst{24} = signed;
641     let Inst{23} = 0;
642     let Inst{22-21} = opcod;
643     let Inst{20} = 1; // load
644     let Inst{11} = 1;
645     // Offset: index==TRUE, wback==FALSE
646     let Inst{10} = 1; // The P bit.
647     let Inst{8} = 0; // The W bit.
648   }
649   def s   : T2Iso <(outs GPR:$dst), (ins t2addrmode_so_reg:$addr), iis,
650                    opc, ".w\t$dst, $addr",
651                    [(set GPR:$dst, (opnode t2addrmode_so_reg:$addr))]> {
652     let Inst{31-27} = 0b11111;
653     let Inst{26-25} = 0b00;
654     let Inst{24} = signed;
655     let Inst{23} = 0;
656     let Inst{22-21} = opcod;
657     let Inst{20} = 1; // load
658     let Inst{11-6} = 0b000000;
659   }
660
661   // FIXME: Is the pci variant actually needed?
662   def pci : T2Ipc <(outs GPR:$dst), (ins i32imm:$addr), iii,
663                    opc, ".w\t$dst, $addr",
664                    [(set GPR:$dst, (opnode (ARMWrapper tconstpool:$addr)))]> {
665     let isReMaterializable = 1;
666     let Inst{31-27} = 0b11111;
667     let Inst{26-25} = 0b00;
668     let Inst{24} = signed;
669     let Inst{23} = ?; // add = (U == '1')
670     let Inst{22-21} = opcod;
671     let Inst{20} = 1; // load
672     let Inst{19-16} = 0b1111; // Rn
673   }
674 }
675
676 /// T2I_st - Defines a set of (op r, {imm12|imm8|so_reg}) store patterns.
677 multiclass T2I_st<bits<2> opcod, string opc,
678                   InstrItinClass iii, InstrItinClass iis, PatFrag opnode> {
679   def i12 : T2Ii12<(outs), (ins GPR:$src, t2addrmode_imm12:$addr), iii,
680                    opc, ".w\t$src, $addr",
681                    [(opnode GPR:$src, t2addrmode_imm12:$addr)]> {
682     let Inst{31-27} = 0b11111;
683     let Inst{26-23} = 0b0001;
684     let Inst{22-21} = opcod;
685     let Inst{20} = 0; // !load
686   }
687   def i8  : T2Ii8 <(outs), (ins GPR:$src, t2addrmode_imm8:$addr), iii,
688                    opc, "\t$src, $addr",
689                    [(opnode GPR:$src, t2addrmode_imm8:$addr)]> {
690     let Inst{31-27} = 0b11111;
691     let Inst{26-23} = 0b0000;
692     let Inst{22-21} = opcod;
693     let Inst{20} = 0; // !load
694     let Inst{11} = 1;
695     // Offset: index==TRUE, wback==FALSE
696     let Inst{10} = 1; // The P bit.
697     let Inst{8} = 0; // The W bit.
698   }
699   def s   : T2Iso <(outs), (ins GPR:$src, t2addrmode_so_reg:$addr), iis,
700                    opc, ".w\t$src, $addr",
701                    [(opnode GPR:$src, t2addrmode_so_reg:$addr)]> {
702     let Inst{31-27} = 0b11111;
703     let Inst{26-23} = 0b0000;
704     let Inst{22-21} = opcod;
705     let Inst{20} = 0; // !load
706     let Inst{11-6} = 0b000000;
707   }
708 }
709
710 /// T2I_ext_rrot - A unary operation with two forms: one whose operand is a
711 /// register and one whose operand is a register rotated by 8/16/24.
712 multiclass T2I_ext_rrot<bits<3> opcod, string opc, PatFrag opnode> {
713   def r     : T2I<(outs rGPR:$dst), (ins rGPR:$src), IIC_iEXTr,
714                   opc, ".w\t$dst, $src",
715                  [(set rGPR:$dst, (opnode rGPR:$src))]> {
716      let Inst{31-27} = 0b11111;
717      let Inst{26-23} = 0b0100;
718      let Inst{22-20} = opcod;
719      let Inst{19-16} = 0b1111; // Rn
720      let Inst{15-12} = 0b1111;
721      let Inst{7} = 1;
722      let Inst{5-4} = 0b00; // rotate
723    }
724   def r_rot : T2I<(outs rGPR:$dst), (ins rGPR:$src, i32imm:$rot), IIC_iEXTr,
725                   opc, ".w\t$dst, $src, ror $rot",
726                  [(set rGPR:$dst, (opnode (rotr rGPR:$src, rot_imm:$rot)))]> {
727      let Inst{31-27} = 0b11111;
728      let Inst{26-23} = 0b0100;
729      let Inst{22-20} = opcod;
730      let Inst{19-16} = 0b1111; // Rn
731      let Inst{15-12} = 0b1111;
732      let Inst{7} = 1;
733      let Inst{5-4} = {?,?}; // rotate
734    }
735 }
736
737 // UXTB16 - Requres T2ExtractPack, does not need the .w qualifier.
738 multiclass T2I_ext_rrot_uxtb16<bits<3> opcod, string opc, PatFrag opnode> {
739   def r     : T2I<(outs rGPR:$dst), (ins rGPR:$src), IIC_iEXTr,
740                   opc, "\t$dst, $src",
741                  [(set rGPR:$dst, (opnode rGPR:$src))]>,
742                  Requires<[HasT2ExtractPack, IsThumb2]> {
743      let Inst{31-27} = 0b11111;
744      let Inst{26-23} = 0b0100;
745      let Inst{22-20} = opcod;
746      let Inst{19-16} = 0b1111; // Rn
747      let Inst{15-12} = 0b1111;
748      let Inst{7} = 1;
749      let Inst{5-4} = 0b00; // rotate
750    }
751   def r_rot : T2I<(outs rGPR:$dst), (ins rGPR:$src, i32imm:$rot), IIC_iEXTr,
752                   opc, "\t$dst, $src, ror $rot",
753                  [(set rGPR:$dst, (opnode (rotr rGPR:$src, rot_imm:$rot)))]>,
754                  Requires<[HasT2ExtractPack, IsThumb2]> {
755      let Inst{31-27} = 0b11111;
756      let Inst{26-23} = 0b0100;
757      let Inst{22-20} = opcod;
758      let Inst{19-16} = 0b1111; // Rn
759      let Inst{15-12} = 0b1111;
760      let Inst{7} = 1;
761      let Inst{5-4} = {?,?}; // rotate
762    }
763 }
764
765 // SXTB16 - Requres T2ExtractPack, does not need the .w qualifier, no pattern
766 // supported yet.
767 multiclass T2I_ext_rrot_sxtb16<bits<3> opcod, string opc> {
768   def r     : T2I<(outs rGPR:$dst), (ins rGPR:$src), IIC_iEXTr,
769                   opc, "\t$dst, $src", []> {
770      let Inst{31-27} = 0b11111;
771      let Inst{26-23} = 0b0100;
772      let Inst{22-20} = opcod;
773      let Inst{19-16} = 0b1111; // Rn
774      let Inst{15-12} = 0b1111;
775      let Inst{7} = 1;
776      let Inst{5-4} = 0b00; // rotate
777    }
778   def r_rot : T2I<(outs rGPR:$dst), (ins rGPR:$src, i32imm:$rot), IIC_iEXTr,
779                   opc, "\t$dst, $src, ror $rot", []> {
780      let Inst{31-27} = 0b11111;
781      let Inst{26-23} = 0b0100;
782      let Inst{22-20} = opcod;
783      let Inst{19-16} = 0b1111; // Rn
784      let Inst{15-12} = 0b1111;
785      let Inst{7} = 1;
786      let Inst{5-4} = {?,?}; // rotate
787    }
788 }
789
790 /// T2I_exta_rrot - A binary operation with two forms: one whose operand is a
791 /// register and one whose operand is a register rotated by 8/16/24.
792 multiclass T2I_exta_rrot<bits<3> opcod, string opc, PatFrag opnode> {
793   def rr     : T2I<(outs rGPR:$dst), (ins rGPR:$LHS, rGPR:$RHS), IIC_iEXTAr,
794                   opc, "\t$dst, $LHS, $RHS",
795                   [(set rGPR:$dst, (opnode rGPR:$LHS, rGPR:$RHS))]>,
796                   Requires<[HasT2ExtractPack, IsThumb2]> {
797      let Inst{31-27} = 0b11111;
798      let Inst{26-23} = 0b0100;
799      let Inst{22-20} = opcod;
800      let Inst{15-12} = 0b1111;
801      let Inst{7} = 1;
802      let Inst{5-4} = 0b00; // rotate
803    }
804   def rr_rot : T2I<(outs rGPR:$dst), (ins rGPR:$LHS, rGPR:$RHS, i32imm:$rot),
805                   IIC_iEXTAsr, opc, "\t$dst, $LHS, $RHS, ror $rot",
806                   [(set rGPR:$dst, (opnode rGPR:$LHS,
807                                           (rotr rGPR:$RHS, rot_imm:$rot)))]>,
808                   Requires<[HasT2ExtractPack, IsThumb2]> {
809      let Inst{31-27} = 0b11111;
810      let Inst{26-23} = 0b0100;
811      let Inst{22-20} = opcod;
812      let Inst{15-12} = 0b1111;
813      let Inst{7} = 1;
814      let Inst{5-4} = {?,?}; // rotate
815    }
816 }
817
818 // DO variant - disassembly only, no pattern
819
820 multiclass T2I_exta_rrot_DO<bits<3> opcod, string opc> {
821   def rr     : T2I<(outs rGPR:$dst), (ins rGPR:$LHS, rGPR:$RHS), IIC_iEXTAr,
822                   opc, "\t$dst, $LHS, $RHS", []> {
823      let Inst{31-27} = 0b11111;
824      let Inst{26-23} = 0b0100;
825      let Inst{22-20} = opcod;
826      let Inst{15-12} = 0b1111;
827      let Inst{7} = 1;
828      let Inst{5-4} = 0b00; // rotate
829    }
830   def rr_rot : T2I<(outs rGPR:$dst), (ins rGPR:$LHS, rGPR:$RHS, i32imm:$rot),
831                   IIC_iEXTAsr, opc, "\t$dst, $LHS, $RHS, ror $rot", []> {
832      let Inst{31-27} = 0b11111;
833      let Inst{26-23} = 0b0100;
834      let Inst{22-20} = opcod;
835      let Inst{15-12} = 0b1111;
836      let Inst{7} = 1;
837      let Inst{5-4} = {?,?}; // rotate
838    }
839 }
840
841 //===----------------------------------------------------------------------===//
842 // Instructions
843 //===----------------------------------------------------------------------===//
844
845 //===----------------------------------------------------------------------===//
846 //  Miscellaneous Instructions.
847 //
848
849 // LEApcrel - Load a pc-relative address into a register without offending the
850 // assembler.
851 let neverHasSideEffects = 1 in {
852 let isReMaterializable = 1 in
853 def t2LEApcrel : T2XI<(outs rGPR:$dst), (ins i32imm:$label, pred:$p), IIC_iALUi,
854                       "adr${p}.w\t$dst, #$label", []> {
855   let Inst{31-27} = 0b11110;
856   let Inst{25-24} = 0b10;
857   // Inst{23:21} = '11' (add = FALSE) or '00' (add = TRUE)
858   let Inst{22} = 0;
859   let Inst{20} = 0;
860   let Inst{19-16} = 0b1111; // Rn
861   let Inst{15} = 0;
862 }
863 } // neverHasSideEffects
864 def t2LEApcrelJT : T2XI<(outs rGPR:$dst),
865                         (ins i32imm:$label, nohash_imm:$id, pred:$p), IIC_iALUi,
866                         "adr${p}.w\t$dst, #${label}_${id}", []> {
867   let Inst{31-27} = 0b11110;
868   let Inst{25-24} = 0b10;
869   // Inst{23:21} = '11' (add = FALSE) or '00' (add = TRUE)
870   let Inst{22} = 0;
871   let Inst{20} = 0;
872   let Inst{19-16} = 0b1111; // Rn
873   let Inst{15} = 0;
874 }
875
876 // ADD r, sp, {so_imm|i12}
877 def t2ADDrSPi   : T2sI<(outs GPR:$dst), (ins GPR:$sp, t2_so_imm:$imm),
878                         IIC_iALUi, "add", ".w\t$dst, $sp, $imm", []> {
879   let Inst{31-27} = 0b11110;
880   let Inst{25} = 0;
881   let Inst{24-21} = 0b1000;
882   let Inst{20} = ?; // The S bit.
883   let Inst{19-16} = 0b1101; // Rn = sp
884   let Inst{15} = 0;
885 }
886 def t2ADDrSPi12 : T2I<(outs GPR:$dst), (ins GPR:$sp, imm0_4095:$imm),
887                        IIC_iALUi, "addw", "\t$dst, $sp, $imm", []> {
888   let Inst{31-27} = 0b11110;
889   let Inst{25} = 1;
890   let Inst{24-21} = 0b0000;
891   let Inst{20} = 0; // The S bit.
892   let Inst{19-16} = 0b1101; // Rn = sp
893   let Inst{15} = 0;
894 }
895
896 // ADD r, sp, so_reg
897 def t2ADDrSPs   : T2sI<(outs GPR:$dst), (ins GPR:$sp, t2_so_reg:$rhs),
898                         IIC_iALUsi, "add", ".w\t$dst, $sp, $rhs", []> {
899   let Inst{31-27} = 0b11101;
900   let Inst{26-25} = 0b01;
901   let Inst{24-21} = 0b1000;
902   let Inst{20} = ?; // The S bit.
903   let Inst{19-16} = 0b1101; // Rn = sp
904   let Inst{15} = 0;
905 }
906
907 // SUB r, sp, {so_imm|i12}
908 def t2SUBrSPi   : T2sI<(outs GPR:$dst), (ins GPR:$sp, t2_so_imm:$imm),
909                         IIC_iALUi, "sub", ".w\t$dst, $sp, $imm", []> {
910   let Inst{31-27} = 0b11110;
911   let Inst{25} = 0;
912   let Inst{24-21} = 0b1101;
913   let Inst{20} = ?; // The S bit.
914   let Inst{19-16} = 0b1101; // Rn = sp
915   let Inst{15} = 0;
916 }
917 def t2SUBrSPi12 : T2I<(outs GPR:$dst), (ins GPR:$sp, imm0_4095:$imm),
918                        IIC_iALUi, "subw", "\t$dst, $sp, $imm", []> {
919   let Inst{31-27} = 0b11110;
920   let Inst{25} = 1;
921   let Inst{24-21} = 0b0101;
922   let Inst{20} = 0; // The S bit.
923   let Inst{19-16} = 0b1101; // Rn = sp
924   let Inst{15} = 0;
925 }
926
927 // SUB r, sp, so_reg
928 def t2SUBrSPs   : T2sI<(outs GPR:$dst), (ins GPR:$sp, t2_so_reg:$rhs),
929                        IIC_iALUsi,
930                        "sub", "\t$dst, $sp, $rhs", []> {
931   let Inst{31-27} = 0b11101;
932   let Inst{26-25} = 0b01;
933   let Inst{24-21} = 0b1101;
934   let Inst{20} = ?; // The S bit.
935   let Inst{19-16} = 0b1101; // Rn = sp
936   let Inst{15} = 0;
937 }
938
939 // Signed and unsigned division on v7-M
940 def t2SDIV : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iALUi, 
941                  "sdiv", "\t$dst, $a, $b",
942                  [(set rGPR:$dst, (sdiv rGPR:$a, rGPR:$b))]>,
943                  Requires<[HasDivide]> {
944   let Inst{31-27} = 0b11111;
945   let Inst{26-21} = 0b011100;
946   let Inst{20} = 0b1;
947   let Inst{15-12} = 0b1111;
948   let Inst{7-4} = 0b1111;
949 }
950
951 def t2UDIV : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iALUi, 
952                  "udiv", "\t$dst, $a, $b",
953                  [(set rGPR:$dst, (udiv rGPR:$a, rGPR:$b))]>,
954                  Requires<[HasDivide]> {
955   let Inst{31-27} = 0b11111;
956   let Inst{26-21} = 0b011101;
957   let Inst{20} = 0b1;
958   let Inst{15-12} = 0b1111;
959   let Inst{7-4} = 0b1111;
960 }
961
962 //===----------------------------------------------------------------------===//
963 //  Load / store Instructions.
964 //
965
966 // Load
967 let canFoldAsLoad = 1, isReMaterializable = 1  in
968 defm t2LDR   : T2I_ld<0, 0b10, "ldr", IIC_iLoad_i, IIC_iLoad_si,
969                       UnOpFrag<(load node:$Src)>>;
970
971 // Loads with zero extension
972 defm t2LDRH  : T2I_ld<0, 0b01, "ldrh", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
973                       UnOpFrag<(zextloadi16 node:$Src)>>;
974 defm t2LDRB  : T2I_ld<0, 0b00, "ldrb", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
975                       UnOpFrag<(zextloadi8  node:$Src)>>;
976
977 // Loads with sign extension
978 defm t2LDRSH : T2I_ld<1, 0b01, "ldrsh", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
979                       UnOpFrag<(sextloadi16 node:$Src)>>;
980 defm t2LDRSB : T2I_ld<1, 0b00, "ldrsb", IIC_iLoad_bh_i, IIC_iLoad_bh_si,
981                       UnOpFrag<(sextloadi8  node:$Src)>>;
982
983 let mayLoad = 1, neverHasSideEffects = 1, hasExtraDefRegAllocReq = 1,
984     isCodeGenOnly = 1 in { // $dst doesn't exist in asmstring?
985 // Load doubleword
986 def t2LDRDi8  : T2Ii8s4<1, 0, 1, (outs rGPR:$dst1, rGPR:$dst2),
987                         (ins t2addrmode_imm8s4:$addr),
988                         IIC_iLoad_d_i, "ldrd", "\t$dst1, $addr", []>;
989 def t2LDRDpci : T2Ii8s4<1, 0, 1, (outs rGPR:$dst1, rGPR:$dst2),
990                         (ins i32imm:$addr), IIC_iLoad_d_i,
991                        "ldrd", "\t$dst1, $addr", []> {
992   let Inst{19-16} = 0b1111; // Rn
993 }
994 } // mayLoad = 1, neverHasSideEffects = 1, hasExtraDefRegAllocReq = 1
995
996 // zextload i1 -> zextload i8
997 def : T2Pat<(zextloadi1 t2addrmode_imm12:$addr),
998             (t2LDRBi12  t2addrmode_imm12:$addr)>;
999 def : T2Pat<(zextloadi1 t2addrmode_imm8:$addr),
1000             (t2LDRBi8   t2addrmode_imm8:$addr)>;
1001 def : T2Pat<(zextloadi1 t2addrmode_so_reg:$addr),
1002             (t2LDRBs    t2addrmode_so_reg:$addr)>;
1003 def : T2Pat<(zextloadi1 (ARMWrapper tconstpool:$addr)),
1004             (t2LDRBpci  tconstpool:$addr)>;
1005
1006 // extload -> zextload
1007 // FIXME: Reduce the number of patterns by legalizing extload to zextload
1008 // earlier?
1009 def : T2Pat<(extloadi1  t2addrmode_imm12:$addr),
1010             (t2LDRBi12  t2addrmode_imm12:$addr)>;
1011 def : T2Pat<(extloadi1  t2addrmode_imm8:$addr),
1012             (t2LDRBi8   t2addrmode_imm8:$addr)>;
1013 def : T2Pat<(extloadi1  t2addrmode_so_reg:$addr),
1014             (t2LDRBs    t2addrmode_so_reg:$addr)>;
1015 def : T2Pat<(extloadi1  (ARMWrapper tconstpool:$addr)),
1016             (t2LDRBpci  tconstpool:$addr)>;
1017
1018 def : T2Pat<(extloadi8  t2addrmode_imm12:$addr),
1019             (t2LDRBi12  t2addrmode_imm12:$addr)>;
1020 def : T2Pat<(extloadi8  t2addrmode_imm8:$addr),
1021             (t2LDRBi8   t2addrmode_imm8:$addr)>;
1022 def : T2Pat<(extloadi8  t2addrmode_so_reg:$addr),
1023             (t2LDRBs    t2addrmode_so_reg:$addr)>;
1024 def : T2Pat<(extloadi8  (ARMWrapper tconstpool:$addr)),
1025             (t2LDRBpci  tconstpool:$addr)>;
1026
1027 def : T2Pat<(extloadi16 t2addrmode_imm12:$addr),
1028             (t2LDRHi12  t2addrmode_imm12:$addr)>;
1029 def : T2Pat<(extloadi16 t2addrmode_imm8:$addr),
1030             (t2LDRHi8   t2addrmode_imm8:$addr)>;
1031 def : T2Pat<(extloadi16 t2addrmode_so_reg:$addr),
1032             (t2LDRHs    t2addrmode_so_reg:$addr)>;
1033 def : T2Pat<(extloadi16 (ARMWrapper tconstpool:$addr)),
1034             (t2LDRHpci  tconstpool:$addr)>;
1035
1036 // FIXME: The destination register of the loads and stores can't be PC, but
1037 //        can be SP. We need another regclass (similar to rGPR) to represent
1038 //        that. Not a pressing issue since these are selected manually,
1039 //        not via pattern.
1040
1041 // Indexed loads
1042 let mayLoad = 1, neverHasSideEffects = 1 in {
1043 def t2LDR_PRE  : T2Iidxldst<0, 0b10, 1, 1, (outs GPR:$dst, GPR:$base_wb),
1044                             (ins t2addrmode_imm8:$addr),
1045                             AddrModeT2_i8, IndexModePre, IIC_iLoad_iu,
1046                             "ldr", "\t$dst, $addr!", "$addr.base = $base_wb",
1047                             []>;
1048
1049 def t2LDR_POST : T2Iidxldst<0, 0b10, 1, 0, (outs GPR:$dst, GPR:$base_wb),
1050                             (ins GPR:$base, t2am_imm8_offset:$offset),
1051                             AddrModeT2_i8, IndexModePost, IIC_iLoad_iu,
1052                           "ldr", "\t$dst, [$base], $offset", "$base = $base_wb",
1053                             []>;
1054
1055 def t2LDRB_PRE : T2Iidxldst<0, 0b00, 1, 1, (outs GPR:$dst, GPR:$base_wb),
1056                             (ins t2addrmode_imm8:$addr),
1057                             AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1058                             "ldrb", "\t$dst, $addr!", "$addr.base = $base_wb",
1059                             []>;
1060 def t2LDRB_POST : T2Iidxldst<0, 0b00, 1, 0, (outs GPR:$dst, GPR:$base_wb),
1061                             (ins GPR:$base, t2am_imm8_offset:$offset),
1062                             AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1063                          "ldrb", "\t$dst, [$base], $offset", "$base = $base_wb",
1064                             []>;
1065
1066 def t2LDRH_PRE : T2Iidxldst<0, 0b01, 1, 1, (outs GPR:$dst, GPR:$base_wb),
1067                             (ins t2addrmode_imm8:$addr),
1068                             AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1069                             "ldrh", "\t$dst, $addr!", "$addr.base = $base_wb",
1070                             []>;
1071 def t2LDRH_POST : T2Iidxldst<0, 0b01, 1, 0, (outs GPR:$dst, GPR:$base_wb),
1072                             (ins GPR:$base, t2am_imm8_offset:$offset),
1073                             AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1074                          "ldrh", "\t$dst, [$base], $offset", "$base = $base_wb",
1075                             []>;
1076
1077 def t2LDRSB_PRE : T2Iidxldst<1, 0b00, 1, 1, (outs GPR:$dst, GPR:$base_wb),
1078                             (ins t2addrmode_imm8:$addr),
1079                             AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1080                             "ldrsb", "\t$dst, $addr!", "$addr.base = $base_wb",
1081                             []>;
1082 def t2LDRSB_POST : T2Iidxldst<1, 0b00, 1, 0, (outs GPR:$dst, GPR:$base_wb),
1083                             (ins GPR:$base, t2am_imm8_offset:$offset),
1084                             AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1085                         "ldrsb", "\t$dst, [$base], $offset", "$base = $base_wb",
1086                             []>;
1087
1088 def t2LDRSH_PRE : T2Iidxldst<1, 0b01, 1, 1, (outs GPR:$dst, GPR:$base_wb),
1089                             (ins t2addrmode_imm8:$addr),
1090                             AddrModeT2_i8, IndexModePre, IIC_iLoad_bh_iu,
1091                             "ldrsh", "\t$dst, $addr!", "$addr.base = $base_wb",
1092                             []>;
1093 def t2LDRSH_POST : T2Iidxldst<1, 0b01, 1, 0, (outs GPR:$dst, GPR:$base_wb),
1094                             (ins GPR:$base, t2am_imm8_offset:$offset),
1095                             AddrModeT2_i8, IndexModePost, IIC_iLoad_bh_iu,
1096                         "ldrsh", "\t$dst, [$base], $offset", "$base = $base_wb",
1097                             []>;
1098 } // mayLoad = 1, neverHasSideEffects = 1 
1099
1100 // LDRT, LDRBT, LDRHT, LDRSBT, LDRSHT all have offset mode (PUW=0b110) and are
1101 // for disassembly only.
1102 // Ref: A8.6.57 LDR (immediate, Thumb) Encoding T4
1103 class T2IldT<bit signed, bits<2> type, string opc, InstrItinClass ii>
1104   : T2Ii8<(outs GPR:$dst), (ins t2addrmode_imm8:$addr), ii, opc,
1105           "\t$dst, $addr", []> {
1106   let Inst{31-27} = 0b11111;
1107   let Inst{26-25} = 0b00;
1108   let Inst{24} = signed;
1109   let Inst{23} = 0;
1110   let Inst{22-21} = type;
1111   let Inst{20} = 1; // load
1112   let Inst{11} = 1;
1113   let Inst{10-8} = 0b110; // PUW.
1114 }
1115
1116 def t2LDRT   : T2IldT<0, 0b10, "ldrt", IIC_iLoad_i>;
1117 def t2LDRBT  : T2IldT<0, 0b00, "ldrbt", IIC_iLoad_bh_i>;
1118 def t2LDRHT  : T2IldT<0, 0b01, "ldrht", IIC_iLoad_bh_i>;
1119 def t2LDRSBT : T2IldT<1, 0b00, "ldrsbt", IIC_iLoad_bh_i>;
1120 def t2LDRSHT : T2IldT<1, 0b01, "ldrsht", IIC_iLoad_bh_i>;
1121
1122 // Store
1123 defm t2STR :T2I_st<0b10,"str", IIC_iStore_i, IIC_iStore_si,
1124                    BinOpFrag<(store node:$LHS, node:$RHS)>>;
1125 defm t2STRB:T2I_st<0b00,"strb", IIC_iStore_bh_i, IIC_iStore_bh_si,
1126                    BinOpFrag<(truncstorei8 node:$LHS, node:$RHS)>>;
1127 defm t2STRH:T2I_st<0b01,"strh", IIC_iStore_bh_i, IIC_iStore_bh_si,
1128                    BinOpFrag<(truncstorei16 node:$LHS, node:$RHS)>>;
1129
1130 // Store doubleword
1131 let mayLoad = 1, neverHasSideEffects = 1, hasExtraSrcRegAllocReq = 1,
1132     isCodeGenOnly = 1 in  // $src2 doesn't exist in asm string
1133 def t2STRDi8 : T2Ii8s4<1, 0, 0, (outs),
1134                        (ins GPR:$src1, GPR:$src2, t2addrmode_imm8s4:$addr),
1135                IIC_iStore_d_r, "strd", "\t$src1, $addr", []>;
1136
1137 // Indexed stores
1138 def t2STR_PRE  : T2Iidxldst<0, 0b10, 0, 1, (outs GPR:$base_wb),
1139                             (ins GPR:$src, GPR:$base, t2am_imm8_offset:$offset),
1140                             AddrModeT2_i8, IndexModePre, IIC_iStore_iu,
1141                          "str", "\t$src, [$base, $offset]!", "$base = $base_wb",
1142              [(set GPR:$base_wb,
1143                    (pre_store GPR:$src, GPR:$base, t2am_imm8_offset:$offset))]>;
1144
1145 def t2STR_POST : T2Iidxldst<0, 0b10, 0, 0, (outs GPR:$base_wb),
1146                             (ins GPR:$src, GPR:$base, t2am_imm8_offset:$offset),
1147                             AddrModeT2_i8, IndexModePost, IIC_iStore_iu,
1148                           "str", "\t$src, [$base], $offset", "$base = $base_wb",
1149              [(set GPR:$base_wb,
1150                   (post_store GPR:$src, GPR:$base, t2am_imm8_offset:$offset))]>;
1151
1152 def t2STRH_PRE  : T2Iidxldst<0, 0b01, 0, 1, (outs GPR:$base_wb),
1153                             (ins GPR:$src, GPR:$base, t2am_imm8_offset:$offset),
1154                             AddrModeT2_i8, IndexModePre, IIC_iStore_iu,
1155                         "strh", "\t$src, [$base, $offset]!", "$base = $base_wb",
1156         [(set GPR:$base_wb,
1157               (pre_truncsti16 GPR:$src, GPR:$base, t2am_imm8_offset:$offset))]>;
1158
1159 def t2STRH_POST : T2Iidxldst<0, 0b01, 0, 0, (outs GPR:$base_wb),
1160                             (ins GPR:$src, GPR:$base, t2am_imm8_offset:$offset),
1161                             AddrModeT2_i8, IndexModePost, IIC_iStore_bh_iu,
1162                          "strh", "\t$src, [$base], $offset", "$base = $base_wb",
1163        [(set GPR:$base_wb,
1164              (post_truncsti16 GPR:$src, GPR:$base, t2am_imm8_offset:$offset))]>;
1165
1166 def t2STRB_PRE  : T2Iidxldst<0, 0b00, 0, 1, (outs GPR:$base_wb),
1167                             (ins GPR:$src, GPR:$base, t2am_imm8_offset:$offset),
1168                             AddrModeT2_i8, IndexModePre, IIC_iStore_bh_iu,
1169                         "strb", "\t$src, [$base, $offset]!", "$base = $base_wb",
1170          [(set GPR:$base_wb,
1171                (pre_truncsti8 GPR:$src, GPR:$base, t2am_imm8_offset:$offset))]>;
1172
1173 def t2STRB_POST : T2Iidxldst<0, 0b00, 0, 0, (outs GPR:$base_wb),
1174                             (ins GPR:$src, GPR:$base, t2am_imm8_offset:$offset),
1175                             AddrModeT2_i8, IndexModePost, IIC_iStore_bh_iu,
1176                          "strb", "\t$src, [$base], $offset", "$base = $base_wb",
1177         [(set GPR:$base_wb,
1178               (post_truncsti8 GPR:$src, GPR:$base, t2am_imm8_offset:$offset))]>;
1179
1180 // STRT, STRBT, STRHT all have offset mode (PUW=0b110) and are for disassembly
1181 // only.
1182 // Ref: A8.6.193 STR (immediate, Thumb) Encoding T4
1183 class T2IstT<bits<2> type, string opc, InstrItinClass ii>
1184   : T2Ii8<(outs GPR:$src), (ins t2addrmode_imm8:$addr), ii, opc,
1185           "\t$src, $addr", []> {
1186   let Inst{31-27} = 0b11111;
1187   let Inst{26-25} = 0b00;
1188   let Inst{24} = 0; // not signed
1189   let Inst{23} = 0;
1190   let Inst{22-21} = type;
1191   let Inst{20} = 0; // store
1192   let Inst{11} = 1;
1193   let Inst{10-8} = 0b110; // PUW
1194 }
1195
1196 def t2STRT   : T2IstT<0b10, "strt", IIC_iStore_i>;
1197 def t2STRBT  : T2IstT<0b00, "strbt", IIC_iStore_bh_i>;
1198 def t2STRHT  : T2IstT<0b01, "strht", IIC_iStore_bh_i>;
1199
1200 // ldrd / strd pre / post variants
1201 // For disassembly only.
1202
1203 def t2LDRD_PRE  : T2Ii8s4<1, 1, 1, (outs GPR:$dst1, GPR:$dst2),
1204                  (ins GPR:$base, t2am_imm8s4_offset:$imm), IIC_iLoad_d_ru,
1205                  "ldrd", "\t$dst1, $dst2, [$base, $imm]!", []>;
1206
1207 def t2LDRD_POST : T2Ii8s4<0, 1, 1, (outs GPR:$dst1, GPR:$dst2),
1208                  (ins GPR:$base, t2am_imm8s4_offset:$imm), IIC_iLoad_d_ru,
1209                  "ldrd", "\t$dst1, $dst2, [$base], $imm", []>;
1210
1211 def t2STRD_PRE  : T2Ii8s4<1, 1, 0, (outs),
1212                  (ins GPR:$src1, GPR:$src2, GPR:$base, t2am_imm8s4_offset:$imm),
1213                  IIC_iStore_d_ru, "strd", "\t$src1, $src2, [$base, $imm]!", []>;
1214
1215 def t2STRD_POST : T2Ii8s4<0, 1, 0, (outs),
1216                  (ins GPR:$src1, GPR:$src2, GPR:$base, t2am_imm8s4_offset:$imm),
1217                  IIC_iStore_d_ru, "strd", "\t$src1, $src2, [$base], $imm", []>;
1218
1219 // T2Ipl (Preload Data/Instruction) signals the memory system of possible future
1220 // data/instruction access.  These are for disassembly only.
1221 // instr_write is inverted for Thumb mode: (prefetch 3) -> (preload 0),
1222 // (prefetch 1) -> (preload 2),  (prefetch 2) -> (preload 1).
1223 multiclass T2Ipl<bits<1> write, bits<1> instr, string opc> {
1224
1225   def i12 : T2Ii12<(outs), (ins t2addrmode_imm12:$addr), IIC_Preload, opc,
1226                 "\t$addr",
1227               [(ARMPreload t2addrmode_imm12:$addr, (i32 write), (i32 instr))]> {
1228     let Inst{31-25} = 0b1111100;
1229     let Inst{24} = instr;
1230     let Inst{23} = 1; // U = 1
1231     let Inst{22} = 0;
1232     let Inst{21} = write;
1233     let Inst{20} = 1;
1234     let Inst{15-12} = 0b1111;
1235   }
1236
1237   def i8 : T2Ii8<(outs), (ins t2addrmode_imm8:$addr), IIC_Preload, opc,
1238                 "\t$addr",
1239                [(ARMPreload t2addrmode_imm8:$addr, (i32 write), (i32 instr))]> {
1240     let Inst{31-25} = 0b1111100;
1241     let Inst{24} = instr;
1242     let Inst{23} = 0; // U = 0
1243     let Inst{22} = 0;
1244     let Inst{21} = write;
1245     let Inst{20} = 1;
1246     let Inst{15-12} = 0b1111;
1247     let Inst{11-8} = 0b1100;
1248   }
1249
1250   def s : T2Iso<(outs), (ins t2addrmode_so_reg:$addr), IIC_Preload, opc,
1251                "\t$addr",
1252              [(ARMPreload t2addrmode_so_reg:$addr, (i32 write), (i32 instr))]> {
1253     let Inst{31-25} = 0b1111100;
1254     let Inst{24} = instr;
1255     let Inst{23} = 0; // add = TRUE for T1
1256     let Inst{22} = 0;
1257     let Inst{21} = write;
1258     let Inst{20} = 1;
1259     let Inst{15-12} = 0b1111;
1260     let Inst{11-6} = 0000000;
1261   }
1262
1263   let isCodeGenOnly = 1 in
1264   def pci : T2Ipc<(outs), (ins i32imm:$addr), IIC_Preload, opc,
1265                 "\t$addr",
1266                []> {
1267     let Inst{31-25} = 0b1111100;
1268     let Inst{24} = write;
1269     let Inst{23} = ?; // add = (U == 1)
1270     let Inst{22} = 0;
1271     let Inst{21} = instr;
1272     let Inst{20} = 1;
1273     let Inst{19-16} = 0b1111; // Rn = 0b1111
1274     let Inst{15-12} = 0b1111;
1275   }
1276 }
1277
1278 defm t2PLD  : T2Ipl<0, 0, "pld">,  Requires<[IsThumb2]>;
1279 defm t2PLDW : T2Ipl<1, 0, "pldw">, Requires<[IsThumb2,HasV7,HasMP]>;
1280 defm t2PLI  : T2Ipl<0, 1, "pli">,  Requires<[IsThumb2,HasV7]>;
1281
1282 //===----------------------------------------------------------------------===//
1283 //  Load / store multiple Instructions.
1284 //
1285
1286 multiclass thumb2_ldst_mult<string asm, InstrItinClass itin,
1287                             InstrItinClass itin_upd, bit L_bit> {
1288   def IA :
1289     T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1290          itin, !strconcat(asm, "${p}.w\t$Rn, $regs"), []> {
1291     bits<4>  Rn;
1292     bits<16> regs;
1293  
1294     let Inst{31-27} = 0b11101;
1295     let Inst{26-25} = 0b00;
1296     let Inst{24-23} = 0b01;     // Increment After
1297     let Inst{22}    = 0;
1298     let Inst{21}    = 0;        // No writeback
1299     let Inst{20}    = L_bit;
1300     let Inst{19-16} = Rn;
1301     let Inst{15-0}  = regs;
1302   }
1303   def IA_UPD :
1304     T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1305           itin_upd, !strconcat(asm, "${p}.w\t$Rn!, $regs"), "$Rn = $wb", []> {
1306     bits<4>  Rn;
1307     bits<16> regs;
1308  
1309     let Inst{31-27} = 0b11101;
1310     let Inst{26-25} = 0b00;
1311     let Inst{24-23} = 0b01;     // Increment After
1312     let Inst{22}    = 0;
1313     let Inst{21}    = 1;        // Writeback
1314     let Inst{20}    = L_bit;
1315     let Inst{19-16} = Rn;
1316     let Inst{15-0}  = regs;
1317   }
1318   def DB :
1319     T2XI<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1320          itin, !strconcat(asm, "db${p}.w\t$Rn, $regs"), []> {
1321     bits<4>  Rn;
1322     bits<16> regs;
1323
1324     let Inst{31-27} = 0b11101;
1325     let Inst{26-25} = 0b00;
1326     let Inst{24-23} = 0b10;     // Decrement Before
1327     let Inst{22}    = 0;
1328     let Inst{21}    = 0;        // No writeback
1329     let Inst{20}    = L_bit;
1330     let Inst{19-16} = Rn;
1331     let Inst{15-0}  = regs;
1332   }
1333   def DB_UPD :
1334     T2XIt<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
1335           itin_upd, !strconcat(asm, "db${p}.w\t$Rn, $regs"), "$Rn = $wb", []> {
1336     bits<4>  Rn;
1337     bits<16> regs;
1338
1339     let Inst{31-27} = 0b11101;
1340     let Inst{26-25} = 0b00;
1341     let Inst{24-23} = 0b10;     // Decrement Before
1342     let Inst{22}    = 0;
1343     let Inst{21}    = 1;        // Writeback
1344     let Inst{20}    = L_bit;
1345     let Inst{19-16} = Rn;
1346     let Inst{15-0}  = regs;
1347   }
1348 }
1349
1350 let mayLoad = 1, neverHasSideEffects = 1, hasExtraDefRegAllocReq = 1,
1351     isCodeGenOnly = 1 in {
1352 def t2LDM : T2XI<(outs), (ins GPR:$Rn, ldstm_mode:$amode, pred:$p,
1353                           reglist:$dsts, variable_ops), IIC_iLoad_m,
1354                  "ldm${amode}${p}.w\t$Rn, $dsts", []> {
1355   let Inst{31-27} = 0b11101;
1356   let Inst{26-25} = 0b00;
1357   let Inst{24-23} = {?, ?}; // IA: '01', DB: '10'
1358   let Inst{22} = 0;
1359   let Inst{21} = 0; // The W bit.
1360   let Inst{20} = 1; // Load
1361 }
1362
1363 def t2LDM_UPD : T2XIt<(outs GPR:$wb), (ins GPR:$Rn, ldstm_mode:$amode, pred:$p,
1364                                        reglist:$dsts, variable_ops),
1365                       IIC_iLoad_mu,
1366                       "ldm${amode}${p}.w\t$Rn!, $dsts",
1367                       "$Rn = $wb", []> {
1368   let Inst{31-27} = 0b11101;
1369   let Inst{26-25} = 0b00;
1370   let Inst{24-23} = {?, ?}; // IA: '01', DB: '10'
1371   let Inst{22} = 0;
1372   let Inst{21} = 1; // The W bit.
1373   let Inst{20} = 1; // Load
1374 }
1375 } // mayLoad, neverHasSideEffects, hasExtraDefRegAllocReq
1376
1377 let mayStore = 1, neverHasSideEffects = 1, hasExtraSrcRegAllocReq = 1,
1378     isCodeGenOnly = 1 in {
1379 def t2STM : T2XI<(outs), (ins GPR:$Rn, ldstm_mode:$amode, pred:$p,
1380                           reglist:$srcs, variable_ops), IIC_iStore_m,
1381                  "stm${amode}${p}.w\t$Rn, $srcs", []> {
1382   let Inst{31-27} = 0b11101;
1383   let Inst{26-25} = 0b00;
1384   let Inst{24-23} = {?, ?}; // IA: '01', DB: '10'
1385   let Inst{22} = 0;
1386   let Inst{21} = 0; // The W bit.
1387   let Inst{20} = 0; // Store
1388 }
1389
1390 def t2STM_UPD : T2XIt<(outs GPR:$wb), (ins GPR:$Rn, ldstm_mode:$amode, pred:$p,
1391                                        reglist:$srcs, variable_ops),
1392                       IIC_iStore_m,
1393                       "stm${amode}${p}.w\t$Rn!, $srcs",
1394                       "$Rn = $wb", []> {
1395   let Inst{31-27} = 0b11101;
1396   let Inst{26-25} = 0b00;
1397   let Inst{24-23} = {?, ?}; // IA: '01', DB: '10'
1398   let Inst{22} = 0;
1399   let Inst{21} = 1; // The W bit.
1400   let Inst{20} = 0; // Store
1401 }
1402 } // mayStore, neverHasSideEffects, hasExtraSrcRegAllocReq
1403
1404 //===----------------------------------------------------------------------===//
1405 //  Move Instructions.
1406 //
1407
1408 let neverHasSideEffects = 1 in
1409 def t2MOVr : T2sI<(outs GPR:$dst), (ins GPR:$src), IIC_iMOVr,
1410                    "mov", ".w\t$dst, $src", []> {
1411   let Inst{31-27} = 0b11101;
1412   let Inst{26-25} = 0b01;
1413   let Inst{24-21} = 0b0010;
1414   let Inst{20} = ?; // The S bit.
1415   let Inst{19-16} = 0b1111; // Rn
1416   let Inst{14-12} = 0b000;
1417   let Inst{7-4} = 0b0000;
1418 }
1419
1420 // AddedComplexity to ensure isel tries t2MOVi before t2MOVi16.
1421 let isReMaterializable = 1, isAsCheapAsAMove = 1, AddedComplexity = 1 in
1422 def t2MOVi : T2sI<(outs rGPR:$dst), (ins t2_so_imm:$src), IIC_iMOVi,
1423                    "mov", ".w\t$dst, $src",
1424                    [(set rGPR:$dst, t2_so_imm:$src)]> {
1425   let Inst{31-27} = 0b11110;
1426   let Inst{25} = 0;
1427   let Inst{24-21} = 0b0010;
1428   let Inst{20} = ?; // The S bit.
1429   let Inst{19-16} = 0b1111; // Rn
1430   let Inst{15} = 0;
1431 }
1432
1433 let isReMaterializable = 1, isAsCheapAsAMove = 1 in
1434 def t2MOVi16 : T2I<(outs rGPR:$dst), (ins i32imm:$src), IIC_iMOVi,
1435                    "movw", "\t$dst, $src",
1436                    [(set rGPR:$dst, imm0_65535:$src)]> {
1437   let Inst{31-27} = 0b11110;
1438   let Inst{25} = 1;
1439   let Inst{24-21} = 0b0010;
1440   let Inst{20} = 0; // The S bit.
1441   let Inst{15} = 0;
1442 }
1443
1444 let Constraints = "$src = $dst" in
1445 def t2MOVTi16 : T2I<(outs rGPR:$dst), (ins rGPR:$src, i32imm:$imm), IIC_iMOVi,
1446                     "movt", "\t$dst, $imm",
1447                     [(set rGPR:$dst,
1448                           (or (and rGPR:$src, 0xffff), lo16AllZero:$imm))]> {
1449   let Inst{31-27} = 0b11110;
1450   let Inst{25} = 1;
1451   let Inst{24-21} = 0b0110;
1452   let Inst{20} = 0; // The S bit.
1453   let Inst{15} = 0;
1454 }
1455
1456 def : T2Pat<(or rGPR:$src, 0xffff0000), (t2MOVTi16 rGPR:$src, 0xffff)>;
1457
1458 //===----------------------------------------------------------------------===//
1459 //  Extend Instructions.
1460 //
1461
1462 // Sign extenders
1463
1464 defm t2SXTB  : T2I_ext_rrot<0b100, "sxtb",
1465                               UnOpFrag<(sext_inreg node:$Src, i8)>>;
1466 defm t2SXTH  : T2I_ext_rrot<0b000, "sxth",
1467                               UnOpFrag<(sext_inreg node:$Src, i16)>>;
1468 defm t2SXTB16 : T2I_ext_rrot_sxtb16<0b010, "sxtb16">;
1469
1470 defm t2SXTAB : T2I_exta_rrot<0b100, "sxtab",
1471                         BinOpFrag<(add node:$LHS, (sext_inreg node:$RHS, i8))>>;
1472 defm t2SXTAH : T2I_exta_rrot<0b000, "sxtah",
1473                         BinOpFrag<(add node:$LHS, (sext_inreg node:$RHS,i16))>>;
1474 defm t2SXTAB16 : T2I_exta_rrot_DO<0b010, "sxtab16">;
1475
1476 // TODO: SXT(A){B|H}16 - done for disassembly only
1477
1478 // Zero extenders
1479
1480 let AddedComplexity = 16 in {
1481 defm t2UXTB   : T2I_ext_rrot<0b101, "uxtb",
1482                                UnOpFrag<(and node:$Src, 0x000000FF)>>;
1483 defm t2UXTH   : T2I_ext_rrot<0b001, "uxth",
1484                                UnOpFrag<(and node:$Src, 0x0000FFFF)>>;
1485 defm t2UXTB16 : T2I_ext_rrot_uxtb16<0b011, "uxtb16",
1486                                UnOpFrag<(and node:$Src, 0x00FF00FF)>>;
1487
1488 // FIXME: This pattern incorrectly assumes the shl operator is a rotate.
1489 //        The transformation should probably be done as a combiner action
1490 //        instead so we can include a check for masking back in the upper
1491 //        eight bits of the source into the lower eight bits of the result.
1492 //def : T2Pat<(and (shl rGPR:$Src, (i32 8)), 0xFF00FF),
1493 //            (t2UXTB16r_rot rGPR:$Src, 24)>,
1494 //          Requires<[HasT2ExtractPack, IsThumb2]>;
1495 def : T2Pat<(and (srl rGPR:$Src, (i32 8)), 0xFF00FF),
1496             (t2UXTB16r_rot rGPR:$Src, 8)>,
1497         Requires<[HasT2ExtractPack, IsThumb2]>;
1498
1499 defm t2UXTAB : T2I_exta_rrot<0b101, "uxtab",
1500                            BinOpFrag<(add node:$LHS, (and node:$RHS, 0x00FF))>>;
1501 defm t2UXTAH : T2I_exta_rrot<0b001, "uxtah",
1502                            BinOpFrag<(add node:$LHS, (and node:$RHS, 0xFFFF))>>;
1503 defm t2UXTAB16 : T2I_exta_rrot_DO<0b011, "uxtab16">;
1504 }
1505
1506 //===----------------------------------------------------------------------===//
1507 //  Arithmetic Instructions.
1508 //
1509
1510 defm t2ADD  : T2I_bin_ii12rs<0b000, "add",
1511                              BinOpFrag<(add  node:$LHS, node:$RHS)>, 1>;
1512 defm t2SUB  : T2I_bin_ii12rs<0b101, "sub",
1513                              BinOpFrag<(sub  node:$LHS, node:$RHS)>>;
1514
1515 // ADD and SUB with 's' bit set. No 12-bit immediate (T4) variants.
1516 defm t2ADDS : T2I_bin_s_irs <0b1000, "add",
1517                              IIC_iALUi, IIC_iALUr, IIC_iALUsi,
1518                              BinOpFrag<(addc node:$LHS, node:$RHS)>, 1>;
1519 defm t2SUBS : T2I_bin_s_irs <0b1101, "sub",
1520                              IIC_iALUi, IIC_iALUr, IIC_iALUsi,
1521                              BinOpFrag<(subc node:$LHS, node:$RHS)>>;
1522
1523 defm t2ADC  : T2I_adde_sube_irs<0b1010, "adc",
1524                           BinOpFrag<(adde_dead_carry node:$LHS, node:$RHS)>, 1>;
1525 defm t2SBC  : T2I_adde_sube_irs<0b1011, "sbc",
1526                           BinOpFrag<(sube_dead_carry node:$LHS, node:$RHS)>>;
1527 defm t2ADCS : T2I_adde_sube_s_irs<0b1010, "adc",
1528                           BinOpFrag<(adde_live_carry node:$LHS, node:$RHS)>, 1>;
1529 defm t2SBCS : T2I_adde_sube_s_irs<0b1011, "sbc",
1530                           BinOpFrag<(sube_live_carry node:$LHS, node:$RHS)>>;
1531
1532 // RSB
1533 defm t2RSB  : T2I_rbin_irs  <0b1110, "rsb",
1534                              BinOpFrag<(sub  node:$LHS, node:$RHS)>>;
1535 defm t2RSBS : T2I_rbin_s_is <0b1110, "rsb",
1536                              BinOpFrag<(subc node:$LHS, node:$RHS)>>;
1537
1538 // (sub X, imm) gets canonicalized to (add X, -imm).  Match this form.
1539 // The assume-no-carry-in form uses the negation of the input since add/sub
1540 // assume opposite meanings of the carry flag (i.e., carry == !borrow).
1541 // See the definition of AddWithCarry() in the ARM ARM A2.2.1 for the gory
1542 // details.
1543 // The AddedComplexity preferences the first variant over the others since
1544 // it can be shrunk to a 16-bit wide encoding, while the others cannot.
1545 let AddedComplexity = 1 in
1546 def : T2Pat<(add        GPR:$src, imm0_255_neg:$imm),
1547             (t2SUBri    GPR:$src, imm0_255_neg:$imm)>;
1548 def : T2Pat<(add        GPR:$src, t2_so_imm_neg:$imm),
1549             (t2SUBri    GPR:$src, t2_so_imm_neg:$imm)>;
1550 def : T2Pat<(add        GPR:$src, imm0_4095_neg:$imm),
1551             (t2SUBri12  GPR:$src, imm0_4095_neg:$imm)>;
1552 let AddedComplexity = 1 in
1553 def : T2Pat<(addc       rGPR:$src, imm0_255_neg:$imm),
1554             (t2SUBSri   rGPR:$src, imm0_255_neg:$imm)>;
1555 def : T2Pat<(addc       rGPR:$src, t2_so_imm_neg:$imm),
1556             (t2SUBSri   rGPR:$src, t2_so_imm_neg:$imm)>;
1557 // The with-carry-in form matches bitwise not instead of the negation.
1558 // Effectively, the inverse interpretation of the carry flag already accounts
1559 // for part of the negation.
1560 let AddedComplexity = 1 in
1561 def : T2Pat<(adde       rGPR:$src, imm0_255_not:$imm),
1562             (t2SBCSri   rGPR:$src, imm0_255_not:$imm)>;
1563 def : T2Pat<(adde       rGPR:$src, t2_so_imm_not:$imm),
1564             (t2SBCSri   rGPR:$src, t2_so_imm_not:$imm)>;
1565
1566 // Select Bytes -- for disassembly only
1567
1568 def t2SEL : T2I<(outs GPR:$dst), (ins GPR:$a, GPR:$b), NoItinerary, "sel",
1569                 "\t$dst, $a, $b", []> {
1570   let Inst{31-27} = 0b11111;
1571   let Inst{26-24} = 0b010;
1572   let Inst{23} = 0b1;
1573   let Inst{22-20} = 0b010;
1574   let Inst{15-12} = 0b1111;
1575   let Inst{7} = 0b1;
1576   let Inst{6-4} = 0b000;
1577 }
1578
1579 // A6.3.13, A6.3.14, A6.3.15 Parallel addition and subtraction (signed/unsigned)
1580 // And Miscellaneous operations -- for disassembly only
1581 class T2I_pam<bits<3> op22_20, bits<4> op7_4, string opc,
1582               list<dag> pat = [/* For disassembly only; pattern left blank */]>
1583   : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), NoItinerary, opc,
1584         "\t$dst, $a, $b", pat> {
1585   let Inst{31-27} = 0b11111;
1586   let Inst{26-23} = 0b0101;
1587   let Inst{22-20} = op22_20;
1588   let Inst{15-12} = 0b1111;
1589   let Inst{7-4} = op7_4;
1590 }
1591
1592 // Saturating add/subtract -- for disassembly only
1593
1594 def t2QADD    : T2I_pam<0b000, 0b1000, "qadd",
1595                         [(set rGPR:$dst, (int_arm_qadd rGPR:$a, rGPR:$b))]>;
1596 def t2QADD16  : T2I_pam<0b001, 0b0001, "qadd16">;
1597 def t2QADD8   : T2I_pam<0b000, 0b0001, "qadd8">;
1598 def t2QASX    : T2I_pam<0b010, 0b0001, "qasx">;
1599 def t2QDADD   : T2I_pam<0b000, 0b1001, "qdadd">;
1600 def t2QDSUB   : T2I_pam<0b000, 0b1011, "qdsub">;
1601 def t2QSAX    : T2I_pam<0b110, 0b0001, "qsax">;
1602 def t2QSUB    : T2I_pam<0b000, 0b1010, "qsub",
1603                         [(set rGPR:$dst, (int_arm_qsub rGPR:$a, rGPR:$b))]>;
1604 def t2QSUB16  : T2I_pam<0b101, 0b0001, "qsub16">;
1605 def t2QSUB8   : T2I_pam<0b100, 0b0001, "qsub8">;
1606 def t2UQADD16 : T2I_pam<0b001, 0b0101, "uqadd16">;
1607 def t2UQADD8  : T2I_pam<0b000, 0b0101, "uqadd8">;
1608 def t2UQASX   : T2I_pam<0b010, 0b0101, "uqasx">;
1609 def t2UQSAX   : T2I_pam<0b110, 0b0101, "uqsax">;
1610 def t2UQSUB16 : T2I_pam<0b101, 0b0101, "uqsub16">;
1611 def t2UQSUB8  : T2I_pam<0b100, 0b0101, "uqsub8">;
1612
1613 // Signed/Unsigned add/subtract -- for disassembly only
1614
1615 def t2SASX    : T2I_pam<0b010, 0b0000, "sasx">;
1616 def t2SADD16  : T2I_pam<0b001, 0b0000, "sadd16">;
1617 def t2SADD8   : T2I_pam<0b000, 0b0000, "sadd8">;
1618 def t2SSAX    : T2I_pam<0b110, 0b0000, "ssax">;
1619 def t2SSUB16  : T2I_pam<0b101, 0b0000, "ssub16">;
1620 def t2SSUB8   : T2I_pam<0b100, 0b0000, "ssub8">;
1621 def t2UASX    : T2I_pam<0b010, 0b0100, "uasx">;
1622 def t2UADD16  : T2I_pam<0b001, 0b0100, "uadd16">;
1623 def t2UADD8   : T2I_pam<0b000, 0b0100, "uadd8">;
1624 def t2USAX    : T2I_pam<0b110, 0b0100, "usax">;
1625 def t2USUB16  : T2I_pam<0b101, 0b0100, "usub16">;
1626 def t2USUB8   : T2I_pam<0b100, 0b0100, "usub8">;
1627
1628 // Signed/Unsigned halving add/subtract -- for disassembly only
1629
1630 def t2SHASX   : T2I_pam<0b010, 0b0010, "shasx">;
1631 def t2SHADD16 : T2I_pam<0b001, 0b0010, "shadd16">;
1632 def t2SHADD8  : T2I_pam<0b000, 0b0010, "shadd8">;
1633 def t2SHSAX   : T2I_pam<0b110, 0b0010, "shsax">;
1634 def t2SHSUB16 : T2I_pam<0b101, 0b0010, "shsub16">;
1635 def t2SHSUB8  : T2I_pam<0b100, 0b0010, "shsub8">;
1636 def t2UHASX   : T2I_pam<0b010, 0b0110, "uhasx">;
1637 def t2UHADD16 : T2I_pam<0b001, 0b0110, "uhadd16">;
1638 def t2UHADD8  : T2I_pam<0b000, 0b0110, "uhadd8">;
1639 def t2UHSAX   : T2I_pam<0b110, 0b0110, "uhsax">;
1640 def t2UHSUB16 : T2I_pam<0b101, 0b0110, "uhsub16">;
1641 def t2UHSUB8  : T2I_pam<0b100, 0b0110, "uhsub8">;
1642
1643 // Unsigned Sum of Absolute Differences [and Accumulate] -- for disassembly only
1644
1645 def t2USAD8   : T2I_mac<0, 0b111, 0b0000, (outs rGPR:$dst),
1646                                            (ins rGPR:$a, rGPR:$b),
1647                         NoItinerary, "usad8", "\t$dst, $a, $b", []> {
1648   let Inst{15-12} = 0b1111;
1649 }
1650 def t2USADA8  : T2I_mac<0, 0b111, 0b0000, (outs rGPR:$dst),
1651                        (ins rGPR:$a, rGPR:$b, rGPR:$acc), NoItinerary, "usada8",
1652                         "\t$dst, $a, $b, $acc", []>;
1653
1654 // Signed/Unsigned saturate -- for disassembly only
1655
1656 def t2SSAT: T2I<(outs rGPR:$dst), (ins i32imm:$bit_pos, rGPR:$a, shift_imm:$sh),
1657                 NoItinerary, "ssat", "\t$dst, $bit_pos, $a$sh",
1658                 [/* For disassembly only; pattern left blank */]> {
1659   let Inst{31-27} = 0b11110;
1660   let Inst{25-22} = 0b1100;
1661   let Inst{20} = 0;
1662   let Inst{15} = 0;
1663 }
1664
1665 def t2SSAT16: T2I<(outs rGPR:$dst), (ins i32imm:$bit_pos, rGPR:$a), NoItinerary,
1666                    "ssat16", "\t$dst, $bit_pos, $a",
1667                    [/* For disassembly only; pattern left blank */]> {
1668   let Inst{31-27} = 0b11110;
1669   let Inst{25-22} = 0b1100;
1670   let Inst{20} = 0;
1671   let Inst{15} = 0;
1672   let Inst{21} = 1;        // sh = '1'
1673   let Inst{14-12} = 0b000; // imm3 = '000'
1674   let Inst{7-6} = 0b00;    // imm2 = '00'
1675 }
1676
1677 def t2USAT: T2I<(outs rGPR:$dst), (ins i32imm:$bit_pos, rGPR:$a, shift_imm:$sh),
1678                 NoItinerary, "usat", "\t$dst, $bit_pos, $a$sh",
1679                 [/* For disassembly only; pattern left blank */]> {
1680   let Inst{31-27} = 0b11110;
1681   let Inst{25-22} = 0b1110;
1682   let Inst{20} = 0;
1683   let Inst{15} = 0;
1684 }
1685
1686 def t2USAT16: T2I<(outs rGPR:$dst), (ins i32imm:$bit_pos, rGPR:$a), NoItinerary,
1687                    "usat16", "\t$dst, $bit_pos, $a",
1688                    [/* For disassembly only; pattern left blank */]> {
1689   let Inst{31-27} = 0b11110;
1690   let Inst{25-22} = 0b1110;
1691   let Inst{20} = 0;
1692   let Inst{15} = 0;
1693   let Inst{21} = 1;        // sh = '1'
1694   let Inst{14-12} = 0b000; // imm3 = '000'
1695   let Inst{7-6} = 0b00;    // imm2 = '00'
1696 }
1697
1698 def : T2Pat<(int_arm_ssat GPR:$a, imm:$pos), (t2SSAT imm:$pos, GPR:$a, 0)>;
1699 def : T2Pat<(int_arm_usat GPR:$a, imm:$pos), (t2USAT imm:$pos, GPR:$a, 0)>;
1700
1701 //===----------------------------------------------------------------------===//
1702 //  Shift and rotate Instructions.
1703 //
1704
1705 defm t2LSL  : T2I_sh_ir<0b00, "lsl", BinOpFrag<(shl  node:$LHS, node:$RHS)>>;
1706 defm t2LSR  : T2I_sh_ir<0b01, "lsr", BinOpFrag<(srl  node:$LHS, node:$RHS)>>;
1707 defm t2ASR  : T2I_sh_ir<0b10, "asr", BinOpFrag<(sra  node:$LHS, node:$RHS)>>;
1708 defm t2ROR  : T2I_sh_ir<0b11, "ror", BinOpFrag<(rotr node:$LHS, node:$RHS)>>;
1709
1710 let Uses = [CPSR] in {
1711 def t2RRX : T2sI<(outs rGPR:$dst), (ins rGPR:$src), IIC_iMOVsi,
1712                    "rrx", "\t$dst, $src",
1713                    [(set rGPR:$dst, (ARMrrx rGPR:$src))]> {
1714   let Inst{31-27} = 0b11101;
1715   let Inst{26-25} = 0b01;
1716   let Inst{24-21} = 0b0010;
1717   let Inst{20} = ?; // The S bit.
1718   let Inst{19-16} = 0b1111; // Rn
1719   let Inst{14-12} = 0b000;
1720   let Inst{7-4} = 0b0011;
1721 }
1722 }
1723
1724 let Defs = [CPSR] in {
1725 def t2MOVsrl_flag : T2I<(outs rGPR:$dst), (ins rGPR:$src), IIC_iMOVsi,
1726                         "lsrs", ".w\t$dst, $src, #1",
1727                         [(set rGPR:$dst, (ARMsrl_flag rGPR:$src))]> {
1728   let Inst{31-27} = 0b11101;
1729   let Inst{26-25} = 0b01;
1730   let Inst{24-21} = 0b0010;
1731   let Inst{20} = 1; // The S bit.
1732   let Inst{19-16} = 0b1111; // Rn
1733   let Inst{5-4} = 0b01; // Shift type.
1734   // Shift amount = Inst{14-12:7-6} = 1.
1735   let Inst{14-12} = 0b000;
1736   let Inst{7-6} = 0b01;
1737 }
1738 def t2MOVsra_flag : T2I<(outs rGPR:$dst), (ins rGPR:$src), IIC_iMOVsi,
1739                         "asrs", ".w\t$dst, $src, #1",
1740                         [(set rGPR:$dst, (ARMsra_flag rGPR:$src))]> {
1741   let Inst{31-27} = 0b11101;
1742   let Inst{26-25} = 0b01;
1743   let Inst{24-21} = 0b0010;
1744   let Inst{20} = 1; // The S bit.
1745   let Inst{19-16} = 0b1111; // Rn
1746   let Inst{5-4} = 0b10; // Shift type.
1747   // Shift amount = Inst{14-12:7-6} = 1.
1748   let Inst{14-12} = 0b000;
1749   let Inst{7-6} = 0b01;
1750 }
1751 }
1752
1753 //===----------------------------------------------------------------------===//
1754 //  Bitwise Instructions.
1755 //
1756
1757 defm t2AND  : T2I_bin_w_irs<0b0000, "and",
1758                             IIC_iBITi, IIC_iBITr, IIC_iBITsi,
1759                             BinOpFrag<(and node:$LHS, node:$RHS)>, 1>;
1760 defm t2ORR  : T2I_bin_w_irs<0b0010, "orr",
1761                             IIC_iBITi, IIC_iBITr, IIC_iBITsi,
1762                             BinOpFrag<(or  node:$LHS, node:$RHS)>, 1>;
1763 defm t2EOR  : T2I_bin_w_irs<0b0100, "eor",
1764                             IIC_iBITi, IIC_iBITr, IIC_iBITsi,
1765                             BinOpFrag<(xor node:$LHS, node:$RHS)>, 1>;
1766
1767 defm t2BIC  : T2I_bin_w_irs<0b0001, "bic",
1768                             IIC_iBITi, IIC_iBITr, IIC_iBITsi,
1769                             BinOpFrag<(and node:$LHS, (not node:$RHS))>>;
1770
1771 let Constraints = "$src = $dst" in
1772 def t2BFC : T2I<(outs rGPR:$dst), (ins rGPR:$src, bf_inv_mask_imm:$imm),
1773                 IIC_iUNAsi, "bfc", "\t$dst, $imm",
1774                 [(set rGPR:$dst, (and rGPR:$src, bf_inv_mask_imm:$imm))]> {
1775   let Inst{31-27} = 0b11110;
1776   let Inst{25} = 1;
1777   let Inst{24-20} = 0b10110;
1778   let Inst{19-16} = 0b1111; // Rn
1779   let Inst{15} = 0;
1780 }
1781
1782 def t2SBFX: T2I<(outs rGPR:$dst), (ins rGPR:$src, imm0_31:$lsb, imm0_31:$width),
1783                  IIC_iUNAsi, "sbfx", "\t$dst, $src, $lsb, $width", []> {
1784   let Inst{31-27} = 0b11110;
1785   let Inst{25} = 1;
1786   let Inst{24-20} = 0b10100;
1787   let Inst{15} = 0;
1788 }
1789
1790 def t2UBFX: T2I<(outs rGPR:$dst), (ins rGPR:$src, imm0_31:$lsb, imm0_31:$width),
1791                  IIC_iUNAsi, "ubfx", "\t$dst, $src, $lsb, $width", []> {
1792   let Inst{31-27} = 0b11110;
1793   let Inst{25} = 1;
1794   let Inst{24-20} = 0b11100;
1795   let Inst{15} = 0;
1796 }
1797
1798 // A8.6.18  BFI - Bitfield insert (Encoding T1)
1799 let Constraints = "$src = $dst" in
1800 def t2BFI : T2I<(outs rGPR:$dst),
1801                 (ins rGPR:$src, rGPR:$val, bf_inv_mask_imm:$imm),
1802                 IIC_iBITi, "bfi", "\t$dst, $val, $imm",
1803                 [(set rGPR:$dst, (ARMbfi rGPR:$src, rGPR:$val,
1804                                  bf_inv_mask_imm:$imm))]> {
1805   let Inst{31-27} = 0b11110;
1806   let Inst{25} = 1;
1807   let Inst{24-20} = 0b10110;
1808   let Inst{15} = 0;
1809 }
1810
1811 defm t2ORN  : T2I_bin_irs<0b0011, "orn",
1812                           IIC_iBITi, IIC_iBITr, IIC_iBITsi,
1813                           BinOpFrag<(or  node:$LHS, (not node:$RHS))>, 0, "">;
1814
1815 // Prefer over of t2EORri ra, rb, -1 because mvn has 16-bit version
1816 let AddedComplexity = 1 in
1817 defm t2MVN  : T2I_un_irs <0b0011, "mvn",
1818                           IIC_iMVNi, IIC_iMVNr, IIC_iMVNsi,
1819                           UnOpFrag<(not node:$Src)>, 1, 1>;
1820
1821
1822 let AddedComplexity = 1 in
1823 def : T2Pat<(and     rGPR:$src, t2_so_imm_not:$imm),
1824             (t2BICri rGPR:$src, t2_so_imm_not:$imm)>;
1825
1826 // FIXME: Disable this pattern on Darwin to workaround an assembler bug.
1827 def : T2Pat<(or      rGPR:$src, t2_so_imm_not:$imm),
1828             (t2ORNri rGPR:$src, t2_so_imm_not:$imm)>,
1829             Requires<[IsThumb2]>;
1830
1831 def : T2Pat<(t2_so_imm_not:$src),
1832             (t2MVNi t2_so_imm_not:$src)>;
1833
1834 //===----------------------------------------------------------------------===//
1835 //  Multiply Instructions.
1836 //
1837 let isCommutable = 1 in
1838 def t2MUL: T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL32,
1839                 "mul", "\t$dst, $a, $b",
1840                 [(set rGPR:$dst, (mul rGPR:$a, rGPR:$b))]> {
1841   let Inst{31-27} = 0b11111;
1842   let Inst{26-23} = 0b0110;
1843   let Inst{22-20} = 0b000;
1844   let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
1845   let Inst{7-4} = 0b0000; // Multiply
1846 }
1847
1848 def t2MLA: T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$c), IIC_iMAC32,
1849                 "mla", "\t$dst, $a, $b, $c",
1850                 [(set rGPR:$dst, (add (mul rGPR:$a, rGPR:$b), rGPR:$c))]> {
1851   let Inst{31-27} = 0b11111;
1852   let Inst{26-23} = 0b0110;
1853   let Inst{22-20} = 0b000;
1854   let Inst{15-12} = {?, ?, ?, ?}; // Ra
1855   let Inst{7-4} = 0b0000; // Multiply
1856 }
1857
1858 def t2MLS: T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$c), IIC_iMAC32,
1859                 "mls", "\t$dst, $a, $b, $c",
1860                 [(set rGPR:$dst, (sub rGPR:$c, (mul rGPR:$a, rGPR:$b)))]> {
1861   let Inst{31-27} = 0b11111;
1862   let Inst{26-23} = 0b0110;
1863   let Inst{22-20} = 0b000;
1864   let Inst{15-12} = {?, ?, ?, ?}; // Ra
1865   let Inst{7-4} = 0b0001; // Multiply and Subtract
1866 }
1867
1868 // Extra precision multiplies with low / high results
1869 let neverHasSideEffects = 1 in {
1870 let isCommutable = 1 in {
1871 def t2SMULL : T2I<(outs rGPR:$ldst, rGPR:$hdst),
1872                   (ins rGPR:$a, rGPR:$b), IIC_iMUL64,
1873                    "smull", "\t$ldst, $hdst, $a, $b", []> {
1874   let Inst{31-27} = 0b11111;
1875   let Inst{26-23} = 0b0111;
1876   let Inst{22-20} = 0b000;
1877   let Inst{7-4} = 0b0000;
1878 }
1879
1880 def t2UMULL : T2I<(outs rGPR:$ldst, rGPR:$hdst),
1881                   (ins rGPR:$a, rGPR:$b), IIC_iMUL64,
1882                    "umull", "\t$ldst, $hdst, $a, $b", []> {
1883   let Inst{31-27} = 0b11111;
1884   let Inst{26-23} = 0b0111;
1885   let Inst{22-20} = 0b010;
1886   let Inst{7-4} = 0b0000;
1887 }
1888 } // isCommutable
1889
1890 // Multiply + accumulate
1891 def t2SMLAL : T2I<(outs rGPR:$ldst, rGPR:$hdst),
1892                   (ins rGPR:$a, rGPR:$b), IIC_iMAC64,
1893                   "smlal", "\t$ldst, $hdst, $a, $b", []>{
1894   let Inst{31-27} = 0b11111;
1895   let Inst{26-23} = 0b0111;
1896   let Inst{22-20} = 0b100;
1897   let Inst{7-4} = 0b0000;
1898 }
1899
1900 def t2UMLAL : T2I<(outs rGPR:$ldst, rGPR:$hdst),
1901                   (ins rGPR:$a, rGPR:$b), IIC_iMAC64,
1902                   "umlal", "\t$ldst, $hdst, $a, $b", []>{
1903   let Inst{31-27} = 0b11111;
1904   let Inst{26-23} = 0b0111;
1905   let Inst{22-20} = 0b110;
1906   let Inst{7-4} = 0b0000;
1907 }
1908
1909 def t2UMAAL : T2I<(outs rGPR:$ldst, rGPR:$hdst),
1910                   (ins rGPR:$a, rGPR:$b), IIC_iMAC64,
1911                   "umaal", "\t$ldst, $hdst, $a, $b", []>{
1912   let Inst{31-27} = 0b11111;
1913   let Inst{26-23} = 0b0111;
1914   let Inst{22-20} = 0b110;
1915   let Inst{7-4} = 0b0110;
1916 }
1917 } // neverHasSideEffects
1918
1919 // Rounding variants of the below included for disassembly only
1920
1921 // Most significant word multiply
1922 def t2SMMUL : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL32,
1923                   "smmul", "\t$dst, $a, $b",
1924                   [(set rGPR:$dst, (mulhs rGPR:$a, rGPR:$b))]> {
1925   let Inst{31-27} = 0b11111;
1926   let Inst{26-23} = 0b0110;
1927   let Inst{22-20} = 0b101;
1928   let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
1929   let Inst{7-4} = 0b0000; // No Rounding (Inst{4} = 0)
1930 }
1931
1932 def t2SMMULR : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL32,
1933                   "smmulr", "\t$dst, $a, $b", []> {
1934   let Inst{31-27} = 0b11111;
1935   let Inst{26-23} = 0b0110;
1936   let Inst{22-20} = 0b101;
1937   let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
1938   let Inst{7-4} = 0b0001; // Rounding (Inst{4} = 1)
1939 }
1940
1941 def t2SMMLA : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$c), IIC_iMAC32,
1942                   "smmla", "\t$dst, $a, $b, $c",
1943                   [(set rGPR:$dst, (add (mulhs rGPR:$a, rGPR:$b), rGPR:$c))]> {
1944   let Inst{31-27} = 0b11111;
1945   let Inst{26-23} = 0b0110;
1946   let Inst{22-20} = 0b101;
1947   let Inst{15-12} = {?, ?, ?, ?}; // Ra
1948   let Inst{7-4} = 0b0000; // No Rounding (Inst{4} = 0)
1949 }
1950
1951 def t2SMMLAR: T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$c), IIC_iMAC32,
1952                   "smmlar", "\t$dst, $a, $b, $c", []> {
1953   let Inst{31-27} = 0b11111;
1954   let Inst{26-23} = 0b0110;
1955   let Inst{22-20} = 0b101;
1956   let Inst{15-12} = {?, ?, ?, ?}; // Ra
1957   let Inst{7-4} = 0b0001; // Rounding (Inst{4} = 1)
1958 }
1959
1960 def t2SMMLS: T2I <(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$c), IIC_iMAC32,
1961                    "smmls", "\t$dst, $a, $b, $c",
1962                    [(set rGPR:$dst, (sub rGPR:$c, (mulhs rGPR:$a, rGPR:$b)))]> {
1963   let Inst{31-27} = 0b11111;
1964   let Inst{26-23} = 0b0110;
1965   let Inst{22-20} = 0b110;
1966   let Inst{15-12} = {?, ?, ?, ?}; // Ra
1967   let Inst{7-4} = 0b0000; // No Rounding (Inst{4} = 0)
1968 }
1969
1970 def t2SMMLSR:T2I <(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$c), IIC_iMAC32,
1971                    "smmlsr", "\t$dst, $a, $b, $c", []> {
1972   let Inst{31-27} = 0b11111;
1973   let Inst{26-23} = 0b0110;
1974   let Inst{22-20} = 0b110;
1975   let Inst{15-12} = {?, ?, ?, ?}; // Ra
1976   let Inst{7-4} = 0b0001; // Rounding (Inst{4} = 1)
1977 }
1978
1979 multiclass T2I_smul<string opc, PatFrag opnode> {
1980   def BB : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL16,
1981               !strconcat(opc, "bb"), "\t$dst, $a, $b",
1982               [(set rGPR:$dst, (opnode (sext_inreg rGPR:$a, i16),
1983                                       (sext_inreg rGPR:$b, i16)))]> {
1984     let Inst{31-27} = 0b11111;
1985     let Inst{26-23} = 0b0110;
1986     let Inst{22-20} = 0b001;
1987     let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
1988     let Inst{7-6} = 0b00;
1989     let Inst{5-4} = 0b00;
1990   }
1991
1992   def BT : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL16,
1993               !strconcat(opc, "bt"), "\t$dst, $a, $b",
1994               [(set rGPR:$dst, (opnode (sext_inreg rGPR:$a, i16),
1995                                       (sra rGPR:$b, (i32 16))))]> {
1996     let Inst{31-27} = 0b11111;
1997     let Inst{26-23} = 0b0110;
1998     let Inst{22-20} = 0b001;
1999     let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2000     let Inst{7-6} = 0b00;
2001     let Inst{5-4} = 0b01;
2002   }
2003
2004   def TB : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL16,
2005               !strconcat(opc, "tb"), "\t$dst, $a, $b",
2006               [(set rGPR:$dst, (opnode (sra rGPR:$a, (i32 16)),
2007                                       (sext_inreg rGPR:$b, i16)))]> {
2008     let Inst{31-27} = 0b11111;
2009     let Inst{26-23} = 0b0110;
2010     let Inst{22-20} = 0b001;
2011     let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2012     let Inst{7-6} = 0b00;
2013     let Inst{5-4} = 0b10;
2014   }
2015
2016   def TT : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL16,
2017               !strconcat(opc, "tt"), "\t$dst, $a, $b",
2018               [(set rGPR:$dst, (opnode (sra rGPR:$a, (i32 16)),
2019                                       (sra rGPR:$b, (i32 16))))]> {
2020     let Inst{31-27} = 0b11111;
2021     let Inst{26-23} = 0b0110;
2022     let Inst{22-20} = 0b001;
2023     let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2024     let Inst{7-6} = 0b00;
2025     let Inst{5-4} = 0b11;
2026   }
2027
2028   def WB : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL16,
2029               !strconcat(opc, "wb"), "\t$dst, $a, $b",
2030               [(set rGPR:$dst, (sra (opnode rGPR:$a,
2031                                     (sext_inreg rGPR:$b, i16)), (i32 16)))]> {
2032     let Inst{31-27} = 0b11111;
2033     let Inst{26-23} = 0b0110;
2034     let Inst{22-20} = 0b011;
2035     let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2036     let Inst{7-6} = 0b00;
2037     let Inst{5-4} = 0b00;
2038   }
2039
2040   def WT : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b), IIC_iMUL16,
2041               !strconcat(opc, "wt"), "\t$dst, $a, $b",
2042               [(set rGPR:$dst, (sra (opnode rGPR:$a,
2043                                     (sra rGPR:$b, (i32 16))), (i32 16)))]> {
2044     let Inst{31-27} = 0b11111;
2045     let Inst{26-23} = 0b0110;
2046     let Inst{22-20} = 0b011;
2047     let Inst{15-12} = 0b1111; // Ra = 0b1111 (no accumulate)
2048     let Inst{7-6} = 0b00;
2049     let Inst{5-4} = 0b01;
2050   }
2051 }
2052
2053
2054 multiclass T2I_smla<string opc, PatFrag opnode> {
2055   def BB : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC16,
2056               !strconcat(opc, "bb"), "\t$dst, $a, $b, $acc",
2057               [(set rGPR:$dst, (add rGPR:$acc,
2058                                (opnode (sext_inreg rGPR:$a, i16),
2059                                        (sext_inreg rGPR:$b, i16))))]> {
2060     let Inst{31-27} = 0b11111;
2061     let Inst{26-23} = 0b0110;
2062     let Inst{22-20} = 0b001;
2063     let Inst{15-12} = {?, ?, ?, ?}; // Ra
2064     let Inst{7-6} = 0b00;
2065     let Inst{5-4} = 0b00;
2066   }
2067
2068   def BT : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC16,
2069              !strconcat(opc, "bt"), "\t$dst, $a, $b, $acc",
2070              [(set rGPR:$dst, (add rGPR:$acc, (opnode (sext_inreg rGPR:$a, i16),
2071                                                   (sra rGPR:$b, (i32 16)))))]> {
2072     let Inst{31-27} = 0b11111;
2073     let Inst{26-23} = 0b0110;
2074     let Inst{22-20} = 0b001;
2075     let Inst{15-12} = {?, ?, ?, ?}; // Ra
2076     let Inst{7-6} = 0b00;
2077     let Inst{5-4} = 0b01;
2078   }
2079
2080   def TB : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC16,
2081               !strconcat(opc, "tb"), "\t$dst, $a, $b, $acc",
2082               [(set rGPR:$dst, (add rGPR:$acc, (opnode (sra rGPR:$a, (i32 16)),
2083                                                 (sext_inreg rGPR:$b, i16))))]> {
2084     let Inst{31-27} = 0b11111;
2085     let Inst{26-23} = 0b0110;
2086     let Inst{22-20} = 0b001;
2087     let Inst{15-12} = {?, ?, ?, ?}; // Ra
2088     let Inst{7-6} = 0b00;
2089     let Inst{5-4} = 0b10;
2090   }
2091
2092   def TT : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC16,
2093               !strconcat(opc, "tt"), "\t$dst, $a, $b, $acc",
2094              [(set rGPR:$dst, (add rGPR:$acc, (opnode (sra rGPR:$a, (i32 16)),
2095                                                   (sra rGPR:$b, (i32 16)))))]> {
2096     let Inst{31-27} = 0b11111;
2097     let Inst{26-23} = 0b0110;
2098     let Inst{22-20} = 0b001;
2099     let Inst{15-12} = {?, ?, ?, ?}; // Ra
2100     let Inst{7-6} = 0b00;
2101     let Inst{5-4} = 0b11;
2102   }
2103
2104   def WB : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC16,
2105               !strconcat(opc, "wb"), "\t$dst, $a, $b, $acc",
2106               [(set rGPR:$dst, (add rGPR:$acc, (sra (opnode rGPR:$a,
2107                                      (sext_inreg rGPR:$b, i16)), (i32 16))))]> {
2108     let Inst{31-27} = 0b11111;
2109     let Inst{26-23} = 0b0110;
2110     let Inst{22-20} = 0b011;
2111     let Inst{15-12} = {?, ?, ?, ?}; // Ra
2112     let Inst{7-6} = 0b00;
2113     let Inst{5-4} = 0b00;
2114   }
2115
2116   def WT : T2I<(outs rGPR:$dst), (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC16,
2117               !strconcat(opc, "wt"), "\t$dst, $a, $b, $acc",
2118               [(set rGPR:$dst, (add rGPR:$acc, (sra (opnode rGPR:$a,
2119                                        (sra rGPR:$b, (i32 16))), (i32 16))))]> {
2120     let Inst{31-27} = 0b11111;
2121     let Inst{26-23} = 0b0110;
2122     let Inst{22-20} = 0b011;
2123     let Inst{15-12} = {?, ?, ?, ?}; // Ra
2124     let Inst{7-6} = 0b00;
2125     let Inst{5-4} = 0b01;
2126   }
2127 }
2128
2129 defm t2SMUL : T2I_smul<"smul", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
2130 defm t2SMLA : T2I_smla<"smla", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
2131
2132 // Halfword multiple accumulate long: SMLAL<x><y> -- for disassembly only
2133 def t2SMLALBB : T2I_mac<1, 0b100, 0b1000, (outs rGPR:$ldst,rGPR:$hdst),
2134          (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlalbb", "\t$ldst, $hdst, $a, $b",
2135            [/* For disassembly only; pattern left blank */]>;
2136 def t2SMLALBT : T2I_mac<1, 0b100, 0b1001, (outs rGPR:$ldst,rGPR:$hdst),
2137          (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlalbt", "\t$ldst, $hdst, $a, $b",
2138            [/* For disassembly only; pattern left blank */]>;
2139 def t2SMLALTB : T2I_mac<1, 0b100, 0b1010, (outs rGPR:$ldst,rGPR:$hdst),
2140          (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlaltb", "\t$ldst, $hdst, $a, $b",
2141            [/* For disassembly only; pattern left blank */]>;
2142 def t2SMLALTT : T2I_mac<1, 0b100, 0b1011, (outs rGPR:$ldst,rGPR:$hdst),
2143          (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlaltt", "\t$ldst, $hdst, $a, $b",
2144            [/* For disassembly only; pattern left blank */]>;
2145
2146 // Dual halfword multiple: SMUAD, SMUSD, SMLAD, SMLSD, SMLALD, SMLSLD
2147 // These are for disassembly only.
2148
2149 def t2SMUAD: T2I_mac<0, 0b010, 0b0000, (outs rGPR:$dst), (ins rGPR:$a, rGPR:$b),
2150                      IIC_iMAC32, "smuad", "\t$dst, $a, $b", []> {
2151   let Inst{15-12} = 0b1111;
2152 }
2153 def t2SMUADX:T2I_mac<0, 0b010, 0b0001, (outs rGPR:$dst), (ins rGPR:$a, rGPR:$b),
2154                      IIC_iMAC32, "smuadx", "\t$dst, $a, $b", []> {
2155   let Inst{15-12} = 0b1111;
2156 }
2157 def t2SMUSD: T2I_mac<0, 0b100, 0b0000, (outs rGPR:$dst), (ins rGPR:$a, rGPR:$b),
2158                      IIC_iMAC32, "smusd", "\t$dst, $a, $b", []> {
2159   let Inst{15-12} = 0b1111;
2160 }
2161 def t2SMUSDX:T2I_mac<0, 0b100, 0b0001, (outs rGPR:$dst), (ins rGPR:$a, rGPR:$b),
2162                      IIC_iMAC32, "smusdx", "\t$dst, $a, $b", []> {
2163   let Inst{15-12} = 0b1111;
2164 }
2165 def t2SMLAD   : T2I_mac<0, 0b010, 0b0000, (outs rGPR:$dst),
2166                         (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC32, "smlad",
2167                         "\t$dst, $a, $b, $acc", []>;
2168 def t2SMLADX  : T2I_mac<0, 0b010, 0b0001, (outs rGPR:$dst),
2169                         (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC32, "smladx",
2170                         "\t$dst, $a, $b, $acc", []>;
2171 def t2SMLSD   : T2I_mac<0, 0b100, 0b0000, (outs rGPR:$dst),
2172                         (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC32, "smlsd",
2173                         "\t$dst, $a, $b, $acc", []>;
2174 def t2SMLSDX  : T2I_mac<0, 0b100, 0b0001, (outs rGPR:$dst),
2175                         (ins rGPR:$a, rGPR:$b, rGPR:$acc), IIC_iMAC32, "smlsdx",
2176                         "\t$dst, $a, $b, $acc", []>;
2177 def t2SMLALD  : T2I_mac<1, 0b100, 0b1100, (outs rGPR:$ldst,rGPR:$hdst),
2178                         (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlald",
2179                         "\t$ldst, $hdst, $a, $b", []>;
2180 def t2SMLALDX : T2I_mac<1, 0b100, 0b1101, (outs rGPR:$ldst,rGPR:$hdst),
2181                         (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlaldx",
2182                         "\t$ldst, $hdst, $a, $b", []>;
2183 def t2SMLSLD  : T2I_mac<1, 0b101, 0b1100, (outs rGPR:$ldst,rGPR:$hdst),
2184                         (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlsld",
2185                         "\t$ldst, $hdst, $a, $b", []>;
2186 def t2SMLSLDX : T2I_mac<1, 0b101, 0b1101, (outs rGPR:$ldst,rGPR:$hdst),
2187                         (ins rGPR:$a,rGPR:$b), IIC_iMAC64, "smlsldx",
2188                         "\t$ldst, $hdst, $a, $b", []>;
2189
2190 //===----------------------------------------------------------------------===//
2191 //  Misc. Arithmetic Instructions.
2192 //
2193
2194 class T2I_misc<bits<2> op1, bits<2> op2, dag oops, dag iops,
2195       InstrItinClass itin, string opc, string asm, list<dag> pattern>
2196   : T2I<oops, iops, itin, opc, asm, pattern> {
2197   let Inst{31-27} = 0b11111;
2198   let Inst{26-22} = 0b01010;
2199   let Inst{21-20} = op1;
2200   let Inst{15-12} = 0b1111;
2201   let Inst{7-6} = 0b10;
2202   let Inst{5-4} = op2;
2203 }
2204
2205 def t2CLZ : T2I_misc<0b11, 0b00, (outs rGPR:$dst), (ins rGPR:$src), IIC_iUNAr,
2206                     "clz", "\t$dst, $src", [(set rGPR:$dst, (ctlz rGPR:$src))]>;
2207
2208 def t2RBIT : T2I_misc<0b01, 0b10, (outs rGPR:$dst), (ins rGPR:$src), IIC_iUNAr,
2209                       "rbit", "\t$dst, $src",
2210                       [(set rGPR:$dst, (ARMrbit rGPR:$src))]>;
2211
2212 def t2REV : T2I_misc<0b01, 0b00, (outs rGPR:$dst), (ins rGPR:$src), IIC_iUNAr,
2213                  "rev", ".w\t$dst, $src", [(set rGPR:$dst, (bswap rGPR:$src))]>;
2214
2215 def t2REV16 : T2I_misc<0b01, 0b01, (outs rGPR:$dst), (ins rGPR:$src), IIC_iUNAr,
2216                        "rev16", ".w\t$dst, $src",
2217                 [(set rGPR:$dst,
2218                     (or (and (srl rGPR:$src, (i32 8)), 0xFF),
2219                         (or (and (shl rGPR:$src, (i32 8)), 0xFF00),
2220                             (or (and (srl rGPR:$src, (i32 8)), 0xFF0000),
2221                                (and (shl rGPR:$src, (i32 8)), 0xFF000000)))))]>;
2222
2223 def t2REVSH : T2I_misc<0b01, 0b11, (outs rGPR:$dst), (ins rGPR:$src), IIC_iUNAr,
2224                        "revsh", ".w\t$dst, $src",
2225                  [(set rGPR:$dst,
2226                     (sext_inreg
2227                       (or (srl (and rGPR:$src, 0xFF00), (i32 8)),
2228                           (shl rGPR:$src, (i32 8))), i16))]>;
2229
2230 def t2PKHBT : T2I<(outs rGPR:$dst), (ins rGPR:$src1, rGPR:$src2, shift_imm:$sh),
2231                   IIC_iBITsi, "pkhbt", "\t$dst, $src1, $src2$sh",
2232                   [(set rGPR:$dst, (or (and rGPR:$src1, 0xFFFF),
2233                                       (and (shl rGPR:$src2, lsl_amt:$sh),
2234                                            0xFFFF0000)))]>,
2235                   Requires<[HasT2ExtractPack, IsThumb2]> {
2236   let Inst{31-27} = 0b11101;
2237   let Inst{26-25} = 0b01;
2238   let Inst{24-20} = 0b01100;
2239   let Inst{5} = 0; // BT form
2240   let Inst{4} = 0;
2241 }
2242
2243 // Alternate cases for PKHBT where identities eliminate some nodes.
2244 def : T2Pat<(or (and rGPR:$src1, 0xFFFF), (and rGPR:$src2, 0xFFFF0000)),
2245             (t2PKHBT rGPR:$src1, rGPR:$src2, 0)>,
2246             Requires<[HasT2ExtractPack, IsThumb2]>;
2247 def : T2Pat<(or (and rGPR:$src1, 0xFFFF), (shl rGPR:$src2, imm16_31:$sh)),
2248             (t2PKHBT rGPR:$src1, rGPR:$src2, (lsl_shift_imm imm16_31:$sh))>,
2249             Requires<[HasT2ExtractPack, IsThumb2]>;
2250
2251 // Note: Shifts of 1-15 bits will be transformed to srl instead of sra and
2252 // will match the pattern below.
2253 def t2PKHTB : T2I<(outs rGPR:$dst), (ins rGPR:$src1, rGPR:$src2, shift_imm:$sh),
2254                   IIC_iBITsi, "pkhtb", "\t$dst, $src1, $src2$sh",
2255                   [(set rGPR:$dst, (or (and rGPR:$src1, 0xFFFF0000),
2256                                        (and (sra rGPR:$src2, asr_amt:$sh),
2257                                             0xFFFF)))]>,
2258                   Requires<[HasT2ExtractPack, IsThumb2]> {
2259   let Inst{31-27} = 0b11101;
2260   let Inst{26-25} = 0b01;
2261   let Inst{24-20} = 0b01100;
2262   let Inst{5} = 1; // TB form
2263   let Inst{4} = 0;
2264 }
2265
2266 // Alternate cases for PKHTB where identities eliminate some nodes.  Note that
2267 // a shift amount of 0 is *not legal* here, it is PKHBT instead.
2268 def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000), (srl rGPR:$src2, imm16_31:$sh)),
2269             (t2PKHTB rGPR:$src1, rGPR:$src2, (asr_shift_imm imm16_31:$sh))>,
2270             Requires<[HasT2ExtractPack, IsThumb2]>;
2271 def : T2Pat<(or (and rGPR:$src1, 0xFFFF0000),
2272                 (and (srl rGPR:$src2, imm1_15:$sh), 0xFFFF)),
2273             (t2PKHTB rGPR:$src1, rGPR:$src2, (asr_shift_imm imm1_15:$sh))>,
2274             Requires<[HasT2ExtractPack, IsThumb2]>;
2275
2276 //===----------------------------------------------------------------------===//
2277 //  Comparison Instructions...
2278 //
2279 defm t2CMP  : T2I_cmp_irs<0b1101, "cmp",
2280                           IIC_iCMPi, IIC_iCMPr, IIC_iCMPsi,
2281                           BinOpFrag<(ARMcmp node:$LHS, node:$RHS)>>;
2282 defm t2CMPz : T2I_cmp_irs<0b1101, "cmp",
2283                           IIC_iCMPi, IIC_iCMPr, IIC_iCMPsi,
2284                           BinOpFrag<(ARMcmpZ node:$LHS, node:$RHS)>>;
2285
2286 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
2287 //       Compare-to-zero still works out, just not the relationals
2288 //defm t2CMN  : T2I_cmp_irs<0b1000, "cmn",
2289 //                          BinOpFrag<(ARMcmp node:$LHS,(ineg node:$RHS))>>;
2290 defm t2CMNz : T2I_cmp_irs<0b1000, "cmn",
2291                           IIC_iCMPi, IIC_iCMPr, IIC_iCMPsi,
2292                           BinOpFrag<(ARMcmpZ node:$LHS,(ineg node:$RHS))>>;
2293
2294 //def : T2Pat<(ARMcmp  GPR:$src, t2_so_imm_neg:$imm),
2295 //            (t2CMNri GPR:$src, t2_so_imm_neg:$imm)>;
2296
2297 def : T2Pat<(ARMcmpZ  GPR:$src, t2_so_imm_neg:$imm),
2298             (t2CMNzri GPR:$src, t2_so_imm_neg:$imm)>;
2299
2300 defm t2TST  : T2I_cmp_irs<0b0000, "tst",
2301                           IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
2302                           BinOpFrag<(ARMcmpZ (and node:$LHS, node:$RHS), 0)>>;
2303 defm t2TEQ  : T2I_cmp_irs<0b0100, "teq",
2304                           IIC_iTSTi, IIC_iTSTr, IIC_iTSTsi,
2305                           BinOpFrag<(ARMcmpZ (xor node:$LHS, node:$RHS), 0)>>;
2306
2307 // Conditional moves
2308 // FIXME: should be able to write a pattern for ARMcmov, but can't use
2309 // a two-value operand where a dag node expects two operands. :(
2310 let neverHasSideEffects = 1 in {
2311 def t2MOVCCr : T2I<(outs rGPR:$dst), (ins rGPR:$false, rGPR:$true), IIC_iCMOVr,
2312                    "mov", ".w\t$dst, $true",
2313    [/*(set rGPR:$dst, (ARMcmov rGPR:$false, rGPR:$true, imm:$cc, CCR:$ccr))*/]>,
2314                 RegConstraint<"$false = $dst"> {
2315   let Inst{31-27} = 0b11101;
2316   let Inst{26-25} = 0b01;
2317   let Inst{24-21} = 0b0010;
2318   let Inst{20} = 0; // The S bit.
2319   let Inst{19-16} = 0b1111; // Rn
2320   let Inst{14-12} = 0b000;
2321   let Inst{7-4} = 0b0000;
2322 }
2323
2324 def t2MOVCCi : T2I<(outs rGPR:$dst), (ins rGPR:$false, t2_so_imm:$true),
2325                    IIC_iCMOVi, "mov", ".w\t$dst, $true",
2326 [/*(set rGPR:$dst,(ARMcmov rGPR:$false,t2_so_imm:$true, imm:$cc, CCR:$ccr))*/]>,
2327                    RegConstraint<"$false = $dst"> {
2328   let Inst{31-27} = 0b11110;
2329   let Inst{25} = 0;
2330   let Inst{24-21} = 0b0010;
2331   let Inst{20} = 0; // The S bit.
2332   let Inst{19-16} = 0b1111; // Rn
2333   let Inst{15} = 0;
2334 }
2335
2336 def t2MOVCCi16 : T2I<(outs rGPR:$dst), (ins rGPR:$false, i32imm:$src),
2337                       IIC_iCMOVi,
2338                       "movw", "\t$dst, $src", []>,
2339                       RegConstraint<"$false = $dst"> {
2340   let Inst{31-27} = 0b11110;
2341   let Inst{25} = 1;
2342   let Inst{24-21} = 0b0010;
2343   let Inst{20} = 0; // The S bit.
2344   let Inst{15} = 0;
2345 }
2346
2347 def t2MOVCCi32imm : PseudoInst<(outs rGPR:$dst),
2348                                (ins rGPR:$false, i32imm:$src, pred:$p),
2349                     IIC_iCMOVix2, "", []>, RegConstraint<"$false = $dst">;
2350
2351 def t2MVNCCi : T2I<(outs rGPR:$dst), (ins rGPR:$false, t2_so_imm:$true),
2352                    IIC_iCMOVi, "mvn", ".w\t$dst, $true",
2353 [/*(set rGPR:$dst,(ARMcmov rGPR:$false,t2_so_imm_not:$true,
2354                    imm:$cc, CCR:$ccr))*/]>,
2355                    RegConstraint<"$false = $dst"> {
2356   let Inst{31-27} = 0b11110;
2357   let Inst{25} = 0;
2358   let Inst{24-21} = 0b0011;
2359   let Inst{20} = 0; // The S bit.
2360   let Inst{19-16} = 0b1111; // Rn
2361   let Inst{15} = 0;
2362 }
2363
2364 class T2I_movcc_sh<bits<2> opcod, dag oops, dag iops, InstrItinClass itin,
2365                    string opc, string asm, list<dag> pattern>
2366   : T2I<oops, iops, itin, opc, asm, pattern> {
2367   let Inst{31-27} = 0b11101;
2368   let Inst{26-25} = 0b01;
2369   let Inst{24-21} = 0b0010;
2370   let Inst{20} = 0; // The S bit.
2371   let Inst{19-16} = 0b1111; // Rn
2372   let Inst{5-4} = opcod; // Shift type.
2373 }
2374 def t2MOVCClsl : T2I_movcc_sh<0b00, (outs rGPR:$dst),
2375                              (ins rGPR:$false, rGPR:$true, i32imm:$rhs),
2376                              IIC_iCMOVsi, "lsl", ".w\t$dst, $true, $rhs", []>,
2377                  RegConstraint<"$false = $dst">;
2378 def t2MOVCClsr : T2I_movcc_sh<0b01, (outs rGPR:$dst),
2379                              (ins rGPR:$false, rGPR:$true, i32imm:$rhs),
2380                              IIC_iCMOVsi, "lsr", ".w\t$dst, $true, $rhs", []>,
2381                  RegConstraint<"$false = $dst">;
2382 def t2MOVCCasr : T2I_movcc_sh<0b10, (outs rGPR:$dst),
2383                              (ins rGPR:$false, rGPR:$true, i32imm:$rhs),
2384                              IIC_iCMOVsi, "asr", ".w\t$dst, $true, $rhs", []>,
2385                  RegConstraint<"$false = $dst">;
2386 def t2MOVCCror : T2I_movcc_sh<0b11, (outs rGPR:$dst),
2387                              (ins rGPR:$false, rGPR:$true, i32imm:$rhs),
2388                              IIC_iCMOVsi, "ror", ".w\t$dst, $true, $rhs", []>,
2389                  RegConstraint<"$false = $dst">;
2390 } // neverHasSideEffects
2391
2392 //===----------------------------------------------------------------------===//
2393 // Atomic operations intrinsics
2394 //
2395
2396 // memory barriers protect the atomic sequences
2397 let hasSideEffects = 1 in {
2398 def t2DMB : AInoP<(outs), (ins memb_opt:$opt), ThumbFrm, NoItinerary,
2399                   "dmb", "\t$opt", [(ARMMemBarrier (i32 imm:$opt))]>,
2400                   Requires<[IsThumb, HasDB]> {
2401   bits<4> opt;
2402   let Inst{31-4} = 0xf3bf8f5;
2403   let Inst{3-0} = opt;
2404 }
2405 }
2406
2407 def t2DSB : AInoP<(outs), (ins memb_opt:$opt), ThumbFrm, NoItinerary,
2408                   "dsb", "\t$opt",
2409                   [/* For disassembly only; pattern left blank */]>,
2410                   Requires<[IsThumb, HasDB]> {
2411   bits<4> opt;
2412   let Inst{31-4} = 0xf3bf8f4;
2413   let Inst{3-0} = opt;
2414 }
2415
2416 // ISB has only full system option -- for disassembly only
2417 def t2ISB : T2I<(outs), (ins), NoItinerary, "isb", "",
2418                   [/* For disassembly only; pattern left blank */]>,
2419                   Requires<[IsThumb2, HasV7]> {
2420   let Inst{31-4} = 0xf3bf8f6;
2421   let Inst{3-0} = 0b1111;
2422 }
2423
2424 class T2I_ldrex<bits<2> opcod, dag oops, dag iops, AddrMode am, SizeFlagVal sz,
2425                 InstrItinClass itin, string opc, string asm, string cstr,
2426                 list<dag> pattern, bits<4> rt2 = 0b1111>
2427   : Thumb2I<oops, iops, am, sz, itin, opc, asm, cstr, pattern> {
2428   let Inst{31-27} = 0b11101;
2429   let Inst{26-20} = 0b0001101;
2430   let Inst{11-8} = rt2;
2431   let Inst{7-6} = 0b01;
2432   let Inst{5-4} = opcod;
2433   let Inst{3-0} = 0b1111;
2434 }
2435 class T2I_strex<bits<2> opcod, dag oops, dag iops, AddrMode am, SizeFlagVal sz,
2436                 InstrItinClass itin, string opc, string asm, string cstr,
2437                 list<dag> pattern, bits<4> rt2 = 0b1111>
2438   : Thumb2I<oops, iops, am, sz, itin, opc, asm, cstr, pattern> {
2439   let Inst{31-27} = 0b11101;
2440   let Inst{26-20} = 0b0001100;
2441   let Inst{11-8} = rt2;
2442   let Inst{7-6} = 0b01;
2443   let Inst{5-4} = opcod;
2444 }
2445
2446 let mayLoad = 1 in {
2447 def t2LDREXB : T2I_ldrex<0b00, (outs rGPR:$dest), (ins rGPR:$ptr), AddrModeNone,
2448                          Size4Bytes, NoItinerary, "ldrexb", "\t$dest, [$ptr]",
2449                          "", []>;
2450 def t2LDREXH : T2I_ldrex<0b01, (outs rGPR:$dest), (ins rGPR:$ptr), AddrModeNone,
2451                          Size4Bytes, NoItinerary, "ldrexh", "\t$dest, [$ptr]",
2452                          "", []>;
2453 def t2LDREX  : Thumb2I<(outs rGPR:$dest), (ins rGPR:$ptr), AddrModeNone,
2454                        Size4Bytes, NoItinerary,
2455                        "ldrex", "\t$dest, [$ptr]", "",
2456                       []> {
2457   let Inst{31-27} = 0b11101;
2458   let Inst{26-20} = 0b0000101;
2459   let Inst{11-8} = 0b1111;
2460   let Inst{7-0} = 0b00000000; // imm8 = 0
2461 }
2462 def t2LDREXD : T2I_ldrex<0b11, (outs rGPR:$dest, rGPR:$dest2), (ins rGPR:$ptr),
2463                          AddrModeNone, Size4Bytes, NoItinerary,
2464                          "ldrexd", "\t$dest, $dest2, [$ptr]", "",
2465                          [], {?, ?, ?, ?}>;
2466 }
2467
2468 let mayStore = 1, Constraints = "@earlyclobber $success" in {
2469 def t2STREXB : T2I_strex<0b00, (outs rGPR:$success), (ins rGPR:$src, rGPR:$ptr),
2470                          AddrModeNone, Size4Bytes, NoItinerary,
2471                          "strexb", "\t$success, $src, [$ptr]", "", []>;
2472 def t2STREXH : T2I_strex<0b01, (outs rGPR:$success), (ins rGPR:$src, rGPR:$ptr),
2473                          AddrModeNone, Size4Bytes, NoItinerary,
2474                          "strexh", "\t$success, $src, [$ptr]", "", []>;
2475 def t2STREX  : Thumb2I<(outs rGPR:$success), (ins rGPR:$src, rGPR:$ptr),
2476                        AddrModeNone, Size4Bytes, NoItinerary,
2477                        "strex", "\t$success, $src, [$ptr]", "",
2478                       []> {
2479   let Inst{31-27} = 0b11101;
2480   let Inst{26-20} = 0b0000100;
2481   let Inst{7-0} = 0b00000000; // imm8 = 0
2482 }
2483 def t2STREXD : T2I_strex<0b11, (outs rGPR:$success),
2484                          (ins rGPR:$src, rGPR:$src2, rGPR:$ptr),
2485                          AddrModeNone, Size4Bytes, NoItinerary,
2486                          "strexd", "\t$success, $src, $src2, [$ptr]", "", [],
2487                          {?, ?, ?, ?}>;
2488 }
2489
2490 // Clear-Exclusive is for disassembly only.
2491 def t2CLREX : T2I<(outs), (ins), NoItinerary, "clrex", "",
2492                   [/* For disassembly only; pattern left blank */]>,
2493             Requires<[IsARM, HasV7]>  {
2494   let Inst{31-20} = 0xf3b;
2495   let Inst{15-14} = 0b10;
2496   let Inst{12} = 0;
2497   let Inst{7-4} = 0b0010;
2498 }
2499
2500 //===----------------------------------------------------------------------===//
2501 // TLS Instructions
2502 //
2503
2504 // __aeabi_read_tp preserves the registers r1-r3.
2505 let isCall = 1,
2506   Defs = [R0, R12, LR, CPSR] in {
2507   def t2TPsoft : T2XI<(outs), (ins), IIC_Br,
2508                      "bl\t__aeabi_read_tp",
2509                      [(set R0, ARMthread_pointer)]> {
2510     let Inst{31-27} = 0b11110;
2511     let Inst{15-14} = 0b11;
2512     let Inst{12} = 1;
2513   }
2514 }
2515
2516 //===----------------------------------------------------------------------===//
2517 // SJLJ Exception handling intrinsics
2518 //   eh_sjlj_setjmp() is an instruction sequence to store the return
2519 //   address and save #0 in R0 for the non-longjmp case.
2520 //   Since by its nature we may be coming from some other function to get
2521 //   here, and we're using the stack frame for the containing function to
2522 //   save/restore registers, we can't keep anything live in regs across
2523 //   the eh_sjlj_setjmp(), else it will almost certainly have been tromped upon
2524 //   when we get here from a longjmp(). We force everthing out of registers
2525 //   except for our own input by listing the relevant registers in Defs. By
2526 //   doing so, we also cause the prologue/epilogue code to actively preserve
2527 //   all of the callee-saved resgisters, which is exactly what we want.
2528 //   $val is a scratch register for our use.
2529 let Defs =
2530   [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7,  R8,  R9,  R10, R11, R12, LR,  D0,
2531     D1,  D2,  D3,  D4,  D5,  D6,  D7,  D8,  D9,  D10, D11, D12, D13, D14, D15,
2532     D16, D17, D18, D19, D20, D21, D22, D23, D24, D25, D26, D27, D28, D29, D30,
2533     D31 ], hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1 in {
2534   def t2Int_eh_sjlj_setjmp : Thumb2XI<(outs), (ins tGPR:$src, tGPR:$val),
2535                                AddrModeNone, SizeSpecial, NoItinerary, "", "",
2536                           [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>,
2537                              Requires<[IsThumb2, HasVFP2]>;
2538 }
2539
2540 let Defs =
2541   [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7,  R8,  R9,  R10, R11, R12, LR ],
2542   hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1 in {
2543   def t2Int_eh_sjlj_setjmp_nofp : Thumb2XI<(outs), (ins tGPR:$src, tGPR:$val),
2544                                AddrModeNone, SizeSpecial, NoItinerary, "", "",
2545                           [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>,
2546                                   Requires<[IsThumb2, NoVFP]>;
2547 }
2548
2549
2550 //===----------------------------------------------------------------------===//
2551 // Control-Flow Instructions
2552 //
2553
2554 // FIXME: remove when we have a way to marking a MI with these properties.
2555 // FIXME: $dst1 should be a def. But the extra ops must be in the end of the
2556 // operand list.
2557 // FIXME: Should pc be an implicit operand like PICADD, etc?
2558 let isReturn = 1, isTerminator = 1, isBarrier = 1, mayLoad = 1,
2559     hasExtraDefRegAllocReq = 1, isCodeGenOnly = 1 in
2560   def t2LDM_RET: T2XIt<(outs GPR:$wb), (ins GPR:$Rn, ldstm_mode:$amode, pred:$p,
2561                                         reglist:$dsts, variable_ops),
2562                         IIC_iLoad_mBr,
2563                         "ldm${amode}${p}.w\t$Rn!, $dsts",
2564                         "$Rn = $wb", []> {
2565   let Inst{31-27} = 0b11101;
2566   let Inst{26-25} = 0b00;
2567   let Inst{24-23} = {?, ?}; // IA: '01', DB: '10'
2568   let Inst{22} = 0;
2569   let Inst{21} = 1; // The W bit.
2570   let Inst{20} = 1; // Load
2571 }
2572
2573 let isBranch = 1, isTerminator = 1, isBarrier = 1 in {
2574 let isPredicable = 1 in
2575 def t2B   : T2XI<(outs), (ins brtarget:$target), IIC_Br,
2576                  "b.w\t$target",
2577                  [(br bb:$target)]> {
2578   let Inst{31-27} = 0b11110;
2579   let Inst{15-14} = 0b10;
2580   let Inst{12} = 1;
2581 }
2582
2583 let isNotDuplicable = 1, isIndirectBranch = 1,
2584     isCodeGenOnly = 1 in { // $id doesn't exist in asmstring, should be lowered.
2585 def t2BR_JT :
2586     T2JTI<(outs),
2587           (ins GPR:$target, GPR:$index, jt2block_operand:$jt, i32imm:$id),
2588            IIC_Br, "mov\tpc, $target$jt",
2589           [(ARMbr2jt GPR:$target, GPR:$index, tjumptable:$jt, imm:$id)]> {
2590   let Inst{31-27} = 0b11101;
2591   let Inst{26-20} = 0b0100100;
2592   let Inst{19-16} = 0b1111;
2593   let Inst{14-12} = 0b000;
2594   let Inst{11-8} = 0b1111; // Rd = pc
2595   let Inst{7-4} = 0b0000;
2596 }
2597
2598 // FIXME: Add a non-pc based case that can be predicated.
2599 let isCodeGenOnly = 1 in  // $id doesn't exist in asm string, should be lowered.
2600 def t2TBB :
2601     T2JTI<(outs),
2602         (ins tb_addrmode:$index, jt2block_operand:$jt, i32imm:$id),
2603          IIC_Br, "tbb\t$index$jt", []> {
2604   let Inst{31-27} = 0b11101;
2605   let Inst{26-20} = 0b0001101;
2606   let Inst{19-16} = 0b1111; // Rn = pc (table follows this instruction)
2607   let Inst{15-8} = 0b11110000;
2608   let Inst{7-4} = 0b0000; // B form
2609 }
2610
2611 let isCodeGenOnly = 1 in  // $id doesn't exist in asm string, should be lowered.
2612 def t2TBH :
2613     T2JTI<(outs),
2614         (ins tb_addrmode:$index, jt2block_operand:$jt, i32imm:$id),
2615          IIC_Br, "tbh\t$index$jt", []> {
2616   let Inst{31-27} = 0b11101;
2617   let Inst{26-20} = 0b0001101;
2618   let Inst{19-16} = 0b1111; // Rn = pc (table follows this instruction)
2619   let Inst{15-8} = 0b11110000;
2620   let Inst{7-4} = 0b0001; // H form
2621 }
2622
2623 // Generic versions of the above two instructions, for disassembly only
2624
2625 def t2TBBgen : T2I<(outs), (ins GPR:$a, GPR:$b), IIC_Br,
2626                     "tbb", "\t[$a, $b]", []>{
2627   let Inst{31-27} = 0b11101;
2628   let Inst{26-20} = 0b0001101;
2629   let Inst{15-8} = 0b11110000;
2630   let Inst{7-4} = 0b0000; // B form
2631 }
2632
2633 def t2TBHgen : T2I<(outs), (ins GPR:$a, GPR:$b), IIC_Br,
2634                    "tbh", "\t[$a, $b, lsl #1]", []> {
2635   let Inst{31-27} = 0b11101;
2636   let Inst{26-20} = 0b0001101;
2637   let Inst{15-8} = 0b11110000;
2638   let Inst{7-4} = 0b0001; // H form
2639 }
2640 } // isNotDuplicable, isIndirectBranch
2641
2642 } // isBranch, isTerminator, isBarrier
2643
2644 // FIXME: should be able to write a pattern for ARMBrcond, but can't use
2645 // a two-value operand where a dag node expects two operands. :(
2646 let isBranch = 1, isTerminator = 1 in
2647 def t2Bcc : T2I<(outs), (ins brtarget:$target), IIC_Br,
2648                 "b", ".w\t$target",
2649                 [/*(ARMbrcond bb:$target, imm:$cc)*/]> {
2650   let Inst{31-27} = 0b11110;
2651   let Inst{15-14} = 0b10;
2652   let Inst{12} = 0;
2653 }
2654
2655
2656 // IT block
2657 let Defs = [ITSTATE] in
2658 def t2IT : Thumb2XI<(outs), (ins it_pred:$cc, it_mask:$mask),
2659                     AddrModeNone, Size2Bytes,  IIC_iALUx,
2660                     "it$mask\t$cc", "", []> {
2661   // 16-bit instruction.
2662   let Inst{31-16} = 0x0000;
2663   let Inst{15-8} = 0b10111111;
2664 }
2665
2666 // Branch and Exchange Jazelle -- for disassembly only
2667 // Rm = Inst{19-16}
2668 def t2BXJ : T2I<(outs), (ins rGPR:$func), NoItinerary, "bxj", "\t$func",
2669               [/* For disassembly only; pattern left blank */]> {
2670   let Inst{31-27} = 0b11110;
2671   let Inst{26} = 0;
2672   let Inst{25-20} = 0b111100;
2673   let Inst{15-14} = 0b10;
2674   let Inst{12} = 0;
2675 }
2676
2677 // Change Processor State is a system instruction -- for disassembly only.
2678 // The singleton $opt operand contains the following information:
2679 // opt{4-0} = mode from Inst{4-0}
2680 // opt{5} = changemode from Inst{17}
2681 // opt{8-6} = AIF from Inst{8-6}
2682 // opt{10-9} = imod from Inst{19-18} with 0b10 as enable and 0b11 as disable
2683 def t2CPS : T2XI<(outs),(ins cps_opt:$opt), NoItinerary, "cps$opt",
2684                  [/* For disassembly only; pattern left blank */]> {
2685   let Inst{31-27} = 0b11110;
2686   let Inst{26} = 0;
2687   let Inst{25-20} = 0b111010;
2688   let Inst{15-14} = 0b10;
2689   let Inst{12} = 0;
2690 }
2691
2692 // A6.3.4 Branches and miscellaneous control
2693 // Table A6-14 Change Processor State, and hint instructions
2694 // Helper class for disassembly only.
2695 class T2I_hint<bits<8> op7_0, string opc, string asm>
2696   : T2I<(outs), (ins), NoItinerary, opc, asm,
2697         [/* For disassembly only; pattern left blank */]> {
2698   let Inst{31-20} = 0xf3a;
2699   let Inst{15-14} = 0b10;
2700   let Inst{12} = 0;
2701   let Inst{10-8} = 0b000;
2702   let Inst{7-0} = op7_0;
2703 }
2704
2705 def t2NOP   : T2I_hint<0b00000000, "nop",   ".w">;
2706 def t2YIELD : T2I_hint<0b00000001, "yield", ".w">;
2707 def t2WFE   : T2I_hint<0b00000010, "wfe",   ".w">;
2708 def t2WFI   : T2I_hint<0b00000011, "wfi",   ".w">;
2709 def t2SEV   : T2I_hint<0b00000100, "sev",   ".w">;
2710
2711 def t2DBG : T2I<(outs),(ins i32imm:$opt), NoItinerary, "dbg", "\t$opt",
2712                 [/* For disassembly only; pattern left blank */]> {
2713   let Inst{31-20} = 0xf3a;
2714   let Inst{15-14} = 0b10;
2715   let Inst{12} = 0;
2716   let Inst{10-8} = 0b000;
2717   let Inst{7-4} = 0b1111;
2718 }
2719
2720 // Secure Monitor Call is a system instruction -- for disassembly only
2721 // Option = Inst{19-16}
2722 def t2SMC : T2I<(outs), (ins i32imm:$opt), NoItinerary, "smc", "\t$opt",
2723                 [/* For disassembly only; pattern left blank */]> {
2724   let Inst{31-27} = 0b11110;
2725   let Inst{26-20} = 0b1111111;
2726   let Inst{15-12} = 0b1000;
2727 }
2728
2729 // Store Return State is a system instruction -- for disassembly only
2730 def t2SRSDBW : T2I<(outs),(ins i32imm:$mode),NoItinerary,"srsdb","\tsp!, $mode",
2731                    [/* For disassembly only; pattern left blank */]> {
2732   let Inst{31-27} = 0b11101;
2733   let Inst{26-20} = 0b0000010; // W = 1
2734 }
2735
2736 def t2SRSDB  : T2I<(outs),(ins i32imm:$mode),NoItinerary,"srsdb","\tsp, $mode",
2737                    [/* For disassembly only; pattern left blank */]> {
2738   let Inst{31-27} = 0b11101;
2739   let Inst{26-20} = 0b0000000; // W = 0
2740 }
2741
2742 def t2SRSIAW : T2I<(outs),(ins i32imm:$mode),NoItinerary,"srsia","\tsp!, $mode",
2743                    [/* For disassembly only; pattern left blank */]> {
2744   let Inst{31-27} = 0b11101;
2745   let Inst{26-20} = 0b0011010; // W = 1
2746 }
2747
2748 def t2SRSIA  : T2I<(outs), (ins i32imm:$mode),NoItinerary,"srsia","\tsp, $mode",
2749                    [/* For disassembly only; pattern left blank */]> {
2750   let Inst{31-27} = 0b11101;
2751   let Inst{26-20} = 0b0011000; // W = 0
2752 }
2753
2754 // Return From Exception is a system instruction -- for disassembly only
2755 def t2RFEDBW : T2I<(outs), (ins rGPR:$base), NoItinerary, "rfedb", "\t$base!",
2756                    [/* For disassembly only; pattern left blank */]> {
2757   let Inst{31-27} = 0b11101;
2758   let Inst{26-20} = 0b0000011; // W = 1
2759 }
2760
2761 def t2RFEDB  : T2I<(outs), (ins rGPR:$base), NoItinerary, "rfeab", "\t$base",
2762                    [/* For disassembly only; pattern left blank */]> {
2763   let Inst{31-27} = 0b11101;
2764   let Inst{26-20} = 0b0000001; // W = 0
2765 }
2766
2767 def t2RFEIAW : T2I<(outs), (ins rGPR:$base), NoItinerary, "rfeia", "\t$base!",
2768                    [/* For disassembly only; pattern left blank */]> {
2769   let Inst{31-27} = 0b11101;
2770   let Inst{26-20} = 0b0011011; // W = 1
2771 }
2772
2773 def t2RFEIA  : T2I<(outs), (ins rGPR:$base), NoItinerary, "rfeia", "\t$base",
2774                    [/* For disassembly only; pattern left blank */]> {
2775   let Inst{31-27} = 0b11101;
2776   let Inst{26-20} = 0b0011001; // W = 0
2777 }
2778
2779 //===----------------------------------------------------------------------===//
2780 // Non-Instruction Patterns
2781 //
2782
2783 // Two piece so_imms.
2784 def : T2Pat<(or rGPR:$LHS, t2_so_imm2part:$RHS),
2785              (t2ORRri (t2ORRri rGPR:$LHS, (t2_so_imm2part_1 imm:$RHS)),
2786                     (t2_so_imm2part_2 imm:$RHS))>;
2787 def : T2Pat<(xor rGPR:$LHS, t2_so_imm2part:$RHS),
2788              (t2EORri (t2EORri rGPR:$LHS, (t2_so_imm2part_1 imm:$RHS)),
2789                     (t2_so_imm2part_2 imm:$RHS))>;
2790 def : T2Pat<(add rGPR:$LHS, t2_so_imm2part:$RHS),
2791              (t2ADDri (t2ADDri rGPR:$LHS, (t2_so_imm2part_1 imm:$RHS)),
2792                     (t2_so_imm2part_2 imm:$RHS))>;
2793 def : T2Pat<(add rGPR:$LHS, t2_so_neg_imm2part:$RHS),
2794              (t2SUBri (t2SUBri rGPR:$LHS, (t2_so_neg_imm2part_1 imm:$RHS)),
2795                     (t2_so_neg_imm2part_2 imm:$RHS))>;
2796
2797 // 32-bit immediate using movw + movt.
2798 // This is a single pseudo instruction to make it re-materializable.
2799 // FIXME: Remove this when we can do generalized remat.
2800 let isReMaterializable = 1 in
2801 def t2MOVi32imm : PseudoInst<(outs rGPR:$dst), (ins i32imm:$src), IIC_iMOVix2,
2802                             "", [(set rGPR:$dst, (i32 imm:$src))]>,
2803                             Requires<[IsThumb, HasV6T2]>;
2804
2805 // ConstantPool, GlobalAddress, and JumpTable
2806 def : T2Pat<(ARMWrapper  tglobaladdr :$dst), (t2LEApcrel tglobaladdr :$dst)>,
2807            Requires<[IsThumb2, DontUseMovt]>;
2808 def : T2Pat<(ARMWrapper  tconstpool  :$dst), (t2LEApcrel tconstpool  :$dst)>;
2809 def : T2Pat<(ARMWrapper  tglobaladdr :$dst), (t2MOVi32imm tglobaladdr :$dst)>,
2810            Requires<[IsThumb2, UseMovt]>;
2811
2812 def : T2Pat<(ARMWrapperJT tjumptable:$dst, imm:$id),
2813             (t2LEApcrelJT tjumptable:$dst, imm:$id)>;
2814
2815 // Pseudo instruction that combines ldr from constpool and add pc. This should
2816 // be expanded into two instructions late to allow if-conversion and
2817 // scheduling.
2818 let canFoldAsLoad = 1, isReMaterializable = 1 in
2819 def t2LDRpci_pic : PseudoInst<(outs GPR:$dst), (ins i32imm:$addr, pclabel:$cp),
2820                    IIC_iLoadiALU, "",
2821                [(set GPR:$dst, (ARMpic_add (load (ARMWrapper tconstpool:$addr)),
2822                                            imm:$cp))]>,
2823                Requires<[IsThumb2]>;
2824
2825 //===----------------------------------------------------------------------===//
2826 // Move between special register and ARM core register -- for disassembly only
2827 //
2828
2829 // Rd = Instr{11-8}
2830 def t2MRS : T2I<(outs rGPR:$dst), (ins), NoItinerary, "mrs", "\t$dst, cpsr",
2831                 [/* For disassembly only; pattern left blank */]> {
2832   let Inst{31-27} = 0b11110;
2833   let Inst{26} = 0;
2834   let Inst{25-21} = 0b11111;
2835   let Inst{20} = 0; // The R bit.
2836   let Inst{15-14} = 0b10;
2837   let Inst{12} = 0;
2838 }
2839
2840 // Rd = Instr{11-8}
2841 def t2MRSsys : T2I<(outs rGPR:$dst), (ins), NoItinerary, "mrs", "\t$dst, spsr",
2842                    [/* For disassembly only; pattern left blank */]> {
2843   let Inst{31-27} = 0b11110;
2844   let Inst{26} = 0;
2845   let Inst{25-21} = 0b11111;
2846   let Inst{20} = 1; // The R bit.
2847   let Inst{15-14} = 0b10;
2848   let Inst{12} = 0;
2849 }
2850
2851 // Rn = Inst{19-16}
2852 def t2MSR : T2I<(outs), (ins rGPR:$src, msr_mask:$mask), NoItinerary, "msr",
2853                 "\tcpsr$mask, $src",
2854                 [/* For disassembly only; pattern left blank */]> {
2855   let Inst{31-27} = 0b11110;
2856   let Inst{26} = 0;
2857   let Inst{25-21} = 0b11100;
2858   let Inst{20} = 0; // The R bit.
2859   let Inst{15-14} = 0b10;
2860   let Inst{12} = 0;
2861 }
2862
2863 // Rn = Inst{19-16}
2864 def t2MSRsys : T2I<(outs), (ins rGPR:$src, msr_mask:$mask), NoItinerary, "msr",
2865                    "\tspsr$mask, $src",
2866                    [/* For disassembly only; pattern left blank */]> {
2867   let Inst{31-27} = 0b11110;
2868   let Inst{26} = 0;
2869   let Inst{25-21} = 0b11100;
2870   let Inst{20} = 1; // The R bit.
2871   let Inst{15-14} = 0b10;
2872   let Inst{12} = 0;
2873 }