More Thumb encodings.
[oota-llvm.git] / lib / Target / ARM / ARMInstrThumb.td
1 //===- ARMInstrThumb.td - Thumb 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 Thumb instruction set.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // Thumb specific DAG Nodes.
16 //
17
18 def ARMtcall : SDNode<"ARMISD::tCALL", SDT_ARMcall,
19                       [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag,
20                        SDNPVariadic]>;
21
22 def imm_neg_XFORM : SDNodeXForm<imm, [{
23   return CurDAG->getTargetConstant(-(int)N->getZExtValue(), MVT::i32);
24 }]>;
25 def imm_comp_XFORM : SDNodeXForm<imm, [{
26   return CurDAG->getTargetConstant(~((uint32_t)N->getZExtValue()), MVT::i32);
27 }]>;
28
29
30 /// imm0_7 predicate - True if the 32-bit immediate is in the range [0,7].
31 def imm0_7 : PatLeaf<(i32 imm), [{
32   return (uint32_t)N->getZExtValue() < 8;
33 }]>;
34 def imm0_7_neg : PatLeaf<(i32 imm), [{
35   return (uint32_t)-N->getZExtValue() < 8;
36 }], imm_neg_XFORM>;
37
38 def imm0_255 : PatLeaf<(i32 imm), [{
39   return (uint32_t)N->getZExtValue() < 256;
40 }]>;
41 def imm0_255_comp : PatLeaf<(i32 imm), [{
42   return ~((uint32_t)N->getZExtValue()) < 256;
43 }]>;
44
45 def imm8_255 : PatLeaf<(i32 imm), [{
46   return (uint32_t)N->getZExtValue() >= 8 && (uint32_t)N->getZExtValue() < 256;
47 }]>;
48 def imm8_255_neg : PatLeaf<(i32 imm), [{
49   unsigned Val = -N->getZExtValue();
50   return Val >= 8 && Val < 256;
51 }], imm_neg_XFORM>;
52
53 // Break imm's up into two pieces: an immediate + a left shift.
54 // This uses thumb_immshifted to match and thumb_immshifted_val and
55 // thumb_immshifted_shamt to get the val/shift pieces.
56 def thumb_immshifted : PatLeaf<(imm), [{
57   return ARM_AM::isThumbImmShiftedVal((unsigned)N->getZExtValue());
58 }]>;
59
60 def thumb_immshifted_val : SDNodeXForm<imm, [{
61   unsigned V = ARM_AM::getThumbImmNonShiftedVal((unsigned)N->getZExtValue());
62   return CurDAG->getTargetConstant(V, MVT::i32);
63 }]>;
64
65 def thumb_immshifted_shamt : SDNodeXForm<imm, [{
66   unsigned V = ARM_AM::getThumbImmValShift((unsigned)N->getZExtValue());
67   return CurDAG->getTargetConstant(V, MVT::i32);
68 }]>;
69
70 // Scaled 4 immediate.
71 def t_imm_s4 : Operand<i32> {
72   let PrintMethod = "printThumbS4ImmOperand";
73 }
74
75 // Define Thumb specific addressing modes.
76
77 // t_addrmode_rr := reg + reg
78 //
79 def t_addrmode_rr : Operand<i32>,
80                     ComplexPattern<i32, 2, "SelectThumbAddrModeRR", []> {
81   let PrintMethod = "printThumbAddrModeRROperand";
82   let MIOperandInfo = (ops tGPR:$base, tGPR:$offsreg);
83 }
84
85 // t_addrmode_s4 := reg + reg
86 //                  reg + imm5 * 4
87 //
88 def t_addrmode_s4 : Operand<i32>,
89                     ComplexPattern<i32, 3, "SelectThumbAddrModeS4", []> {
90   let PrintMethod = "printThumbAddrModeS4Operand";
91   let MIOperandInfo = (ops tGPR:$base, i32imm:$offsimm, tGPR:$offsreg);
92 }
93
94 // t_addrmode_s2 := reg + reg
95 //                  reg + imm5 * 2
96 //
97 def t_addrmode_s2 : Operand<i32>,
98                     ComplexPattern<i32, 3, "SelectThumbAddrModeS2", []> {
99   let PrintMethod = "printThumbAddrModeS2Operand";
100   let MIOperandInfo = (ops tGPR:$base, i32imm:$offsimm, tGPR:$offsreg);
101 }
102
103 // t_addrmode_s1 := reg + reg
104 //                  reg + imm5
105 //
106 def t_addrmode_s1 : Operand<i32>,
107                     ComplexPattern<i32, 3, "SelectThumbAddrModeS1", []> {
108   let PrintMethod = "printThumbAddrModeS1Operand";
109   let MIOperandInfo = (ops tGPR:$base, i32imm:$offsimm, tGPR:$offsreg);
110 }
111
112 // t_addrmode_sp := sp + imm8 * 4
113 //
114 def t_addrmode_sp : Operand<i32>,
115                     ComplexPattern<i32, 2, "SelectThumbAddrModeSP", []> {
116   let PrintMethod = "printThumbAddrModeSPOperand";
117   let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
118 }
119
120 //===----------------------------------------------------------------------===//
121 //  Miscellaneous Instructions.
122 //
123
124 // FIXME: Marking these as hasSideEffects is necessary to prevent machine DCE
125 // from removing one half of the matched pairs. That breaks PEI, which assumes
126 // these will always be in pairs, and asserts if it finds otherwise. Better way?
127 let Defs = [SP], Uses = [SP], hasSideEffects = 1 in {
128 def tADJCALLSTACKUP :
129   PseudoInst<(outs), (ins i32imm:$amt1, i32imm:$amt2), NoItinerary,
130              [(ARMcallseq_end imm:$amt1, imm:$amt2)]>,
131             Requires<[IsThumb, IsThumb1Only]>;
132
133 def tADJCALLSTACKDOWN :
134   PseudoInst<(outs), (ins i32imm:$amt), NoItinerary,
135              [(ARMcallseq_start imm:$amt)]>,
136             Requires<[IsThumb, IsThumb1Only]>;
137 }
138
139 def tNOP : T1pI<(outs), (ins), NoItinerary, "nop", "",
140                 [/* For disassembly only; pattern left blank */]>,
141            T1Encoding<0b101111> {
142   // A8.6.110
143   let Inst{9-8} = 0b11;
144   let Inst{7-0} = 0x00;
145
146
147 def tYIELD : T1pI<(outs), (ins), NoItinerary, "yield", "",
148                   [/* For disassembly only; pattern left blank */]>,
149              T1Encoding<0b101111> {
150   // A8.6.410
151   let Inst{9-8} = 0b11;
152   let Inst{7-0} = 0x10;
153
154
155 def tWFE : T1pI<(outs), (ins), NoItinerary, "wfe", "",
156                 [/* For disassembly only; pattern left blank */]>,
157            T1Encoding<0b101111> {
158   // A8.6.408
159   let Inst{9-8} = 0b11;
160   let Inst{7-0} = 0x20;
161
162
163 def tWFI : T1pI<(outs), (ins), NoItinerary, "wfi", "",
164                 [/* For disassembly only; pattern left blank */]>,
165            T1Encoding<0b101111> {
166   // A8.6.409
167   let Inst{9-8} = 0b11;
168   let Inst{7-0} = 0x30;
169
170
171 def tSEV : T1pI<(outs), (ins), NoItinerary, "sev", "",
172                 [/* For disassembly only; pattern left blank */]>,
173            T1Encoding<0b101111> {
174   // A8.6.157
175   let Inst{9-8} = 0b11;
176   let Inst{7-0} = 0x40;
177
178
179 def tSETENDBE : T1I<(outs), (ins), NoItinerary, "setend\tbe",
180                     [/* For disassembly only; pattern left blank */]>,
181                 T1Encoding<0b101101> {
182   // A8.6.156
183   let Inst{9-5} = 0b10010;
184   let Inst{4}   = 1;
185   let Inst{3}   = 1;            // Big-Endian
186   let Inst{2-0} = 0b000;
187 }
188
189 def tSETENDLE : T1I<(outs), (ins), NoItinerary, "setend\tle",
190                     [/* For disassembly only; pattern left blank */]>,
191                 T1Encoding<0b101101> {
192   // A8.6.156
193   let Inst{9-5} = 0b10010;
194   let Inst{4}   = 1;
195   let Inst{3}   = 0;            // Little-Endian
196   let Inst{2-0} = 0b000;
197 }
198
199 // The i32imm operand $val can be used by a debugger to store more information
200 // about the breakpoint.
201 def tBKPT : T1I<(outs), (ins i32imm:$val), NoItinerary, "bkpt\t$val",
202                 [/* For disassembly only; pattern left blank */]>,
203             T1Encoding<0b101111> {
204   // A8.6.22
205   bits<8> val;
206   let Inst{9-8} = 0b10;
207   let Inst{7-0} = val;
208 }
209
210 // Change Processor State is a system instruction -- for disassembly only.
211 // The singleton $opt operand contains the following information:
212 // opt{4-0} = mode ==> don't care
213 // opt{5} = changemode ==> 0 (false for 16-bit Thumb instr)
214 // opt{8-6} = AIF from Inst{2-0}
215 // opt{10-9} = 1:imod from Inst{4} with 0b10 as enable and 0b11 as disable
216 //
217 // The opt{4-0} and opt{5} sub-fields are to accommodate 32-bit Thumb and ARM
218 // CPS which has more options.
219 def tCPS : T1I<(outs), (ins cps_opt:$opt), NoItinerary, "cps$opt",
220               [/* For disassembly only; pattern left blank */]>,
221            T1Misc<0b0110011>;   // A8.6.38
222
223 // For both thumb1 and thumb2.
224 let isNotDuplicable = 1, isCodeGenOnly = 1 in
225 def tPICADD : TIt<(outs GPR:$dst), (ins GPR:$lhs, pclabel:$cp), IIC_iALUr, "",
226                   [(set GPR:$dst, (ARMpic_add GPR:$lhs, imm:$cp))]>,
227               T1Special<{0,0,?,?}> {
228   // A8.6.6 Rm = pc
229   bits<3> dst;
230   let Inst{6-3} = 0b1111;
231   let Inst{2-0} = dst;
232 }
233
234 // PC relative add.
235 def tADDrPCi : T1I<(outs tGPR:$dst), (ins t_imm_s4:$rhs), IIC_iALUi,
236                    "add\t$dst, pc, $rhs", []>,
237                T1Encoding<{1,0,1,0,0,?}> {
238   // A6.2 & A8.6.10
239   bits<3> dst;
240   bits<8> rhs;
241   let Inst{10-8} = dst;
242   let Inst{7-0}  = rhs;
243 }
244
245 // ADD <Rd>, sp, #<imm8>
246 // This is rematerializable, which is particularly useful for taking the
247 // address of locals.
248 let isReMaterializable = 1 in
249 def tADDrSPi : T1I<(outs tGPR:$dst), (ins GPR:$sp, t_imm_s4:$rhs), IIC_iALUi,
250                    "add\t$dst, $sp, $rhs", []>,
251                T1Encoding<{1,0,1,0,1,?}> {
252   // A6.2 & A8.6.8
253   bits<3> dst;
254   bits<8> rhs;
255   let Inst{10-8} = dst;
256   let Inst{7-0}  = rhs;
257 }
258
259 // ADD sp, sp, #<imm7>
260 def tADDspi : TIt<(outs GPR:$dst), (ins GPR:$lhs, t_imm_s4:$rhs), IIC_iALUi,
261                   "add\t$dst, $rhs", []>,
262               T1Misc<{0,0,0,0,0,?,?}> {
263   // A6.2.5 & A8.6.8
264   bits<7> rhs;
265   let Inst{6-0} = rhs;
266 }
267
268 // SUB sp, sp, #<imm7>
269 // FIXME: The encoding and the ASM string don't match up.
270 def tSUBspi : TIt<(outs GPR:$dst), (ins GPR:$lhs, t_imm_s4:$rhs), IIC_iALUi,
271                   "sub\t$dst, $rhs", []>,
272               T1Misc<{0,0,0,0,1,?,?}> {
273   // A6.2.5 & A8.6.214
274   bits<7> rhs;
275   let Inst{6-0} = rhs;
276 }
277
278 // ADD <Rm>, sp
279 def tADDrSP : TIt<(outs GPR:$dst), (ins GPR:$lhs, GPR:$rhs), IIC_iALUr,
280                   "add\t$dst, $rhs", []>,
281               T1Special<{0,0,?,?}> {
282   // A8.6.9 Encoding T1
283   bits<4> dst;
284   let Inst{7}   = dst{3};
285   let Inst{6-3} = 0b1101;
286   let Inst{2-0} = dst{2-0};
287 }
288
289 // ADD sp, <Rm>
290 def tADDspr : TIt<(outs GPR:$dst), (ins GPR:$lhs, GPR:$rhs), IIC_iALUr,
291                   "add\t$dst, $rhs", []>,
292               T1Special<{0,0,?,?}> {
293   // A8.6.9 Encoding T2
294   bits<4> dst;
295   let Inst{7} = 1;
296   let Inst{6-3} = dst;
297   let Inst{2-0} = 0b101;
298 }
299
300 //===----------------------------------------------------------------------===//
301 //  Control Flow Instructions.
302 //
303
304 let isReturn = 1, isTerminator = 1, isBarrier = 1 in {
305   def tBX_RET : TI<(outs), (ins), IIC_Br, "bx\tlr",
306                    [(ARMretflag)]>,
307                 T1Special<{1,1,0,?}> { // A6.2.3 & A8.6.25
308     let Inst{6-3} = 0b1110; // Rm = lr
309     let Inst{2-0} = 0b000;
310   }
311
312   // Alternative return instruction used by vararg functions.
313   def tBX_RET_vararg : TI<(outs), (ins tGPR:$Rm),
314                           IIC_Br, "bx\t$Rm",
315                           []>,
316                        T1Special<{1,1,0,?}> { // A6.2.3 & A8.6.25
317     bits<4> Rm;
318     let Inst{6-3} = Rm;
319     let Inst{2-0} = 0b000;
320   }
321 }
322
323 // Indirect branches
324 let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
325   def tBRIND : TI<(outs), (ins GPR:$Rm), IIC_Br, "mov\tpc, $Rm",
326                   [(brind GPR:$Rm)]>,
327                T1Special<{1,0,?,?}> {
328     bits<4> Rm;
329     let Inst{6-3} = Rm;
330     let Inst{7}   = 0b1;        // <Rd> = Inst{7:2-0} = pc
331     let Inst{2-0} = 0b111;
332   }
333 }
334
335 // FIXME: remove when we have a way to marking a MI with these properties.
336 let isReturn = 1, isTerminator = 1, isBarrier = 1, mayLoad = 1,
337     hasExtraDefRegAllocReq = 1 in
338 def tPOP_RET : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
339                    IIC_iPop_Br,
340                    "pop${p}\t$regs", []>,
341                T1Misc<{1,1,0,?,?,?,?}> {
342   bits<16> regs;
343   let Inst{8}   = regs{15};
344   let Inst{7-0} = regs{7-0};
345 }
346
347 let isCall = 1,
348   Defs = [R0,  R1,  R2,  R3,  R12, LR,
349           D0,  D1,  D2,  D3,  D4,  D5,  D6,  D7,
350           D16, D17, D18, D19, D20, D21, D22, D23,
351           D24, D25, D26, D27, D28, D29, D30, D31, CPSR, FPSCR] in {
352   // Also used for Thumb2
353   def tBL  : TIx2<0b11110, 0b11, 1,
354                   (outs), (ins i32imm:$func, variable_ops), IIC_Br,
355                   "bl\t$func",
356                   [(ARMtcall tglobaladdr:$func)]>,
357              Requires<[IsThumb, IsNotDarwin]>;
358
359   // ARMv5T and above, also used for Thumb2
360   def tBLXi : TIx2<0b11110, 0b11, 0,
361                    (outs), (ins i32imm:$func, variable_ops), IIC_Br,
362                    "blx\t$func",
363                    [(ARMcall tglobaladdr:$func)]>,
364               Requires<[IsThumb, HasV5T, IsNotDarwin]>;
365
366   // Also used for Thumb2
367   def tBLXr : TI<(outs), (ins GPR:$func, variable_ops), IIC_Br,
368                   "blx\t$func",
369                   [(ARMtcall GPR:$func)]>,
370               Requires<[IsThumb, HasV5T, IsNotDarwin]>,
371               T1Special<{1,1,1,?}>; // A6.2.3 & A8.6.24;
372
373   // ARMv4T
374   let isCodeGenOnly = 1 in
375   def tBX : TIx2<{?,?,?,?,?}, {?,?}, ?,
376                   (outs), (ins tGPR:$func, variable_ops), IIC_Br,
377                   "mov\tlr, pc\n\tbx\t$func",
378                   [(ARMcall_nolink tGPR:$func)]>,
379             Requires<[IsThumb, IsThumb1Only, IsNotDarwin]>;
380 }
381
382 // On Darwin R9 is call-clobbered.
383 let isCall = 1,
384   Defs = [R0,  R1,  R2,  R3,  R9,  R12, LR,
385           D0,  D1,  D2,  D3,  D4,  D5,  D6,  D7,
386           D16, D17, D18, D19, D20, D21, D22, D23,
387           D24, D25, D26, D27, D28, D29, D30, D31, CPSR, FPSCR] in {
388   // Also used for Thumb2
389   def tBLr9 : TIx2<0b11110, 0b11, 1,
390                    (outs), (ins i32imm:$func, variable_ops), IIC_Br,
391                    "bl\t$func",
392                    [(ARMtcall tglobaladdr:$func)]>,
393               Requires<[IsThumb, IsDarwin]>;
394
395   // ARMv5T and above, also used for Thumb2
396   def tBLXi_r9 : TIx2<0b11110, 0b11, 0,
397                       (outs), (ins i32imm:$func, variable_ops), IIC_Br,
398                       "blx\t$func",
399                       [(ARMcall tglobaladdr:$func)]>,
400                  Requires<[IsThumb, HasV5T, IsDarwin]>;
401
402   // Also used for Thumb2
403   def tBLXr_r9 : TI<(outs), (ins GPR:$func, variable_ops), IIC_Br,
404                     "blx\t$func",
405                     [(ARMtcall GPR:$func)]>,
406                  Requires<[IsThumb, HasV5T, IsDarwin]>,
407                  T1Special<{1,1,1,?}>; // A6.2.3 & A8.6.24
408
409   // ARMv4T
410   let isCodeGenOnly = 1 in
411   def tBXr9 : TIx2<{?,?,?,?,?}, {?,?}, ?,
412                    (outs), (ins tGPR:$func, variable_ops), IIC_Br,
413                    "mov\tlr, pc\n\tbx\t$func",
414                    [(ARMcall_nolink tGPR:$func)]>,
415               Requires<[IsThumb, IsThumb1Only, IsDarwin]>;
416 }
417
418 let isBranch = 1, isTerminator = 1 in {
419   let isBarrier = 1 in {
420     let isPredicable = 1 in
421     def tB   : T1I<(outs), (ins brtarget:$target), IIC_Br,
422                    "b\t$target", [(br bb:$target)]>,
423                T1Encoding<{1,1,1,0,0,?}>;
424
425   // Far jump
426   let Defs = [LR] in
427   def tBfar : TIx2<0b11110, 0b11, 1, (outs), (ins brtarget:$target), IIC_Br,
428                     "bl\t$target",[]>;
429
430   let isCodeGenOnly = 1 in
431   def tBR_JTr : T1JTI<(outs),
432                       (ins tGPR:$target, jtblock_operand:$jt, i32imm:$id),
433                       IIC_Br, "mov\tpc, $target\n\t.align\t2$jt",
434                       [(ARMbrjt tGPR:$target, tjumptable:$jt, imm:$id)]>,
435                 Encoding16 {
436     let Inst{15-7} = 0b010001101;
437     let Inst{2-0} = 0b111;
438   }
439   }
440 }
441
442 // FIXME: should be able to write a pattern for ARMBrcond, but can't use
443 // a two-value operand where a dag node expects two operands. :(
444 let isBranch = 1, isTerminator = 1 in
445   def tBcc : T1I<(outs), (ins brtarget:$target, pred:$cc), IIC_Br,
446                  "b$cc\t$target",
447                  [/*(ARMbrcond bb:$target, imm:$cc)*/]>,
448              T1Encoding<{1,1,0,1,?,?}>;
449
450 // Compare and branch on zero / non-zero
451 let isBranch = 1, isTerminator = 1 in {
452   def tCBZ  : T1I<(outs), (ins tGPR:$Rn, brtarget:$target), IIC_Br,
453                   "cbz\t$Rn, $target", []>,
454               T1Misc<{0,0,?,1,?,?,?}> {
455     bits<6> target;
456     bits<3> Rn;
457     let Inst{9}   = target{5};
458     let Inst{7-3} = target{4-0};
459     let Inst{2-0} = Rn;
460   }
461
462   def tCBNZ : T1I<(outs), (ins tGPR:$cmp, brtarget:$target), IIC_Br,
463                   "cbnz\t$cmp, $target", []>,
464               T1Misc<{1,0,?,1,?,?,?}> {
465     bits<6> target;
466     bits<3> Rn;
467     let Inst{9}   = target{5};
468     let Inst{7-3} = target{4-0};
469     let Inst{2-0} = Rn;
470   }
471 }
472
473 // A8.6.218 Supervisor Call (Software Interrupt) -- for disassembly only
474 // A8.6.16 B: Encoding T1
475 // If Inst{11-8} == 0b1111 then SEE SVC
476 let isCall = 1 in
477 def tSVC : T1pI<(outs), (ins i32imm:$imm), IIC_Br,
478                 "svc", "\t$imm", []>, Encoding16 {
479   bits<8> imm;
480   let Inst{15-12} = 0b1101;
481   let Inst{11-8}  = 0b1111;
482   let Inst{7-0}   = imm;
483 }
484
485 // A8.6.16 B: Encoding T1
486 // If Inst{11-8} == 0b1110 then UNDEFINED
487 let isBarrier = 1, isTerminator = 1 in
488 def tTRAP : TI<(outs), (ins), IIC_Br, 
489                "trap", [(trap)]>, Encoding16 {
490   let Inst = 0xdefe;
491 }
492
493 //===----------------------------------------------------------------------===//
494 //  Load Store Instructions.
495 //
496
497 let canFoldAsLoad = 1, isReMaterializable = 1 in
498 def tLDR : T1pI4<(outs tGPR:$Rt), (ins t_addrmode_s4:$addr), IIC_iLoad_r,
499                "ldr", "\t$Rt, $addr",
500                [(set tGPR:$Rt, (load t_addrmode_s4:$addr))]>,
501            T1LdSt<0b100>;
502
503 def tLDRi: T1pI4<(outs tGPR:$dst), (ins t_addrmode_s4:$addr), IIC_iLoad_r,
504                "ldr", "\t$dst, $addr",
505                []>,
506            T1LdSt4Imm<{1,?,?}>;
507
508 def tLDRB : T1pI1<(outs tGPR:$dst), (ins t_addrmode_s1:$addr), IIC_iLoad_bh_r,
509                 "ldrb", "\t$dst, $addr",
510                 [(set tGPR:$dst, (zextloadi8 t_addrmode_s1:$addr))]>,
511             T1LdSt<0b110>;
512 def tLDRBi: T1pI1<(outs tGPR:$dst), (ins t_addrmode_s1:$addr), IIC_iLoad_bh_r,
513                 "ldrb", "\t$dst, $addr",
514                 []>,
515             T1LdSt1Imm<{1,?,?}>;
516
517 def tLDRH : T1pI2<(outs tGPR:$dst), (ins t_addrmode_s2:$addr), IIC_iLoad_bh_r,
518                 "ldrh", "\t$dst, $addr",
519                 [(set tGPR:$dst, (zextloadi16 t_addrmode_s2:$addr))]>,
520             T1LdSt<0b101>;
521 def tLDRHi: T1pI2<(outs tGPR:$dst), (ins t_addrmode_s2:$addr), IIC_iLoad_bh_r,
522                 "ldrh", "\t$dst, $addr",
523                 []>,
524             T1LdSt2Imm<{1,?,?}>;
525
526 let AddedComplexity = 10 in
527 def tLDRSB : T1pI1<(outs tGPR:$dst), (ins t_addrmode_rr:$addr), IIC_iLoad_bh_r,
528                  "ldrsb", "\t$dst, $addr",
529                  [(set tGPR:$dst, (sextloadi8 t_addrmode_rr:$addr))]>,
530              T1LdSt<0b011>;
531
532 let AddedComplexity = 10 in
533 def tLDRSH : T1pI2<(outs tGPR:$dst), (ins t_addrmode_rr:$addr), IIC_iLoad_bh_r,
534                  "ldrsh", "\t$dst, $addr",
535                  [(set tGPR:$dst, (sextloadi16 t_addrmode_rr:$addr))]>,
536              T1LdSt<0b111>;
537
538 let canFoldAsLoad = 1 in
539 def tLDRspi : T1pIs<(outs tGPR:$dst), (ins t_addrmode_sp:$addr), IIC_iLoad_i,
540                   "ldr", "\t$dst, $addr",
541                   [(set tGPR:$dst, (load t_addrmode_sp:$addr))]>,
542               T1LdStSP<{1,?,?}>;
543
544 // Special instruction for restore. It cannot clobber condition register
545 // when it's expanded by eliminateCallFramePseudoInstr().
546 let canFoldAsLoad = 1, mayLoad = 1, neverHasSideEffects = 1 in
547 def tRestore : T1pIs<(outs tGPR:$dst), (ins t_addrmode_sp:$addr), IIC_iLoad_i,
548                     "ldr", "\t$dst, $addr", []>,
549                T1LdStSP<{1,?,?}>;
550
551 // Load tconstpool
552 // FIXME: Use ldr.n to work around a Darwin assembler bug.
553 let canFoldAsLoad = 1, isReMaterializable = 1 in
554 def tLDRpci : T1pIs<(outs tGPR:$dst), (ins i32imm:$addr), IIC_iLoad_i,
555                   "ldr", ".n\t$dst, $addr",
556                   [(set tGPR:$dst, (load (ARMWrapper tconstpool:$addr)))]>,
557               T1Encoding<{0,1,0,0,1,?}>; // A6.2 & A8.6.59
558
559 // Special LDR for loads from non-pc-relative constpools.
560 let canFoldAsLoad = 1, mayLoad = 1, neverHasSideEffects = 1,
561     isReMaterializable = 1 in
562 def tLDRcp  : T1pIs<(outs tGPR:$dst), (ins i32imm:$addr), IIC_iLoad_i,
563                   "ldr", "\t$dst, $addr", []>,
564               T1LdStSP<{1,?,?}>;
565
566 def tSTR : T1pI4<(outs), (ins tGPR:$src, t_addrmode_s4:$addr), IIC_iStore_r,
567                "str", "\t$src, $addr",
568                [(store tGPR:$src, t_addrmode_s4:$addr)]>,
569            T1LdSt<0b000>;
570 def tSTRi: T1pI4<(outs), (ins tGPR:$src, t_addrmode_s4:$addr), IIC_iStore_r,
571                "str", "\t$src, $addr",
572                []>,
573            T1LdSt4Imm<{0,?,?}>;
574
575 def tSTRB : T1pI1<(outs), (ins tGPR:$src, t_addrmode_s1:$addr), IIC_iStore_bh_r,
576                  "strb", "\t$src, $addr",
577                  [(truncstorei8 tGPR:$src, t_addrmode_s1:$addr)]>,
578             T1LdSt<0b010>;
579 def tSTRBi: T1pI1<(outs), (ins tGPR:$src, t_addrmode_s1:$addr), IIC_iStore_bh_r,
580                  "strb", "\t$src, $addr",
581                  []>,
582             T1LdSt1Imm<{0,?,?}>;
583
584 def tSTRH : T1pI2<(outs), (ins tGPR:$src, t_addrmode_s2:$addr), IIC_iStore_bh_r,
585                  "strh", "\t$src, $addr",
586                  [(truncstorei16 tGPR:$src, t_addrmode_s2:$addr)]>,
587             T1LdSt<0b001>;
588 def tSTRHi: T1pI2<(outs), (ins tGPR:$src, t_addrmode_s2:$addr), IIC_iStore_bh_r,
589                  "strh", "\t$src, $addr",
590                  []>,
591             T1LdSt2Imm<{0,?,?}>;
592
593 def tSTRspi : T1pIs<(outs), (ins tGPR:$src, t_addrmode_sp:$addr), IIC_iStore_i,
594                    "str", "\t$src, $addr",
595                    [(store tGPR:$src, t_addrmode_sp:$addr)]>,
596               T1LdStSP<{0,?,?}>;
597
598 let mayStore = 1, neverHasSideEffects = 1 in {
599 // Special instruction for spill. It cannot clobber condition register
600 // when it's expanded by eliminateCallFramePseudoInstr().
601 def tSpill : T1pIs<(outs), (ins tGPR:$src, t_addrmode_sp:$addr), IIC_iStore_i,
602                   "str", "\t$src, $addr", []>,
603              T1LdStSP<{0,?,?}>;
604 }
605
606 //===----------------------------------------------------------------------===//
607 //  Load / store multiple Instructions.
608 //
609
610 multiclass thumb_ldst_mult<string asm, InstrItinClass itin,
611                            InstrItinClass itin_upd, bits<6> T1Enc,
612                            bit L_bit> {
613   def IA :
614     T1I<(outs), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
615         itin, !strconcat(asm, "ia${p}\t$Rn, $regs"), []>,
616        T1Encoding<T1Enc> {
617     bits<3> Rn;
618     bits<8> regs;
619     let Inst{10-8} = Rn;
620     let Inst{7-0}  = regs;
621   }
622   def IA_UPD :
623     T1It<(outs GPR:$wb), (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops),
624          itin_upd, !strconcat(asm, "ia${p}\t$Rn!, $regs"), "$Rn = $wb", []>,
625         T1Encoding<T1Enc> {
626     bits<3> Rn;
627     bits<8> regs;
628     let Inst{10-8} = Rn;
629     let Inst{7-0}  = regs;
630   }
631 }
632
633 // These require base address to be written back or one of the loaded regs.
634 let neverHasSideEffects = 1 in {
635
636 let mayLoad = 1, hasExtraDefRegAllocReq = 1 in
637 defm tLDM : thumb_ldst_mult<"ldm", IIC_iLoad_m, IIC_iLoad_mu,
638                             {1,1,0,0,1,?}, 1>;
639
640 let mayStore = 1, hasExtraSrcRegAllocReq = 1 in
641 defm tSTM : thumb_ldst_mult<"stm", IIC_iStore_m, IIC_iStore_mu,
642                             {1,1,0,0,0,?}, 0>;
643  
644 } // neverHasSideEffects
645
646 let mayLoad = 1, Uses = [SP], Defs = [SP], hasExtraDefRegAllocReq = 1 in
647 def tPOP : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
648                IIC_iPop,
649                "pop${p}\t$regs", []>,
650            T1Misc<{1,1,0,?,?,?,?}> {
651   bits<16> regs;
652   let Inst{8}   = regs{15};
653   let Inst{7-0} = regs{7-0};
654 }
655
656 let mayStore = 1, Uses = [SP], Defs = [SP], hasExtraSrcRegAllocReq = 1 in
657 def tPUSH : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
658                 IIC_iStore_m,
659                 "push${p}\t$regs", []>,
660             T1Misc<{0,1,0,?,?,?,?}> {
661   bits<16> regs;
662   let Inst{8}   = regs{14};
663   let Inst{7-0} = regs{7-0};
664 }
665
666 //===----------------------------------------------------------------------===//
667 //  Arithmetic Instructions.
668 //
669
670 // Add with carry register
671 let isCommutable = 1, Uses = [CPSR] in
672 def tADC : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iALUr,
673                  "adc", "\t$dst, $rhs",
674                  [(set tGPR:$dst, (adde tGPR:$lhs, tGPR:$rhs))]>,
675            T1DataProcessing<0b0101> {
676   // A8.6.2
677   bits<3> lhs;
678   bits<3> rhs;
679   let Inst{5-3} = lhs;
680   let Inst{2-0} = rhs;
681 }
682
683 // Add immediate
684 def tADDi3 : T1sI<(outs tGPR:$Rd), (ins tGPR:$Rn, i32imm:$imm3), IIC_iALUi,
685                    "add", "\t$Rd, $Rn, $imm3",
686                    [(set tGPR:$Rd, (add tGPR:$Rn, imm0_7:$imm3))]>,
687              T1General<0b01110> {
688   // A8.6.4 T1
689   bits<3> Rd;
690   bits<3> Rn;
691   bits<3> imm3;
692   let Inst{8-6} = imm3;
693   let Inst{5-3} = Rn;
694   let Inst{2-0} = Rd;
695 }
696
697 def tADDi8 : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, i32imm:$rhs), IIC_iALUi,
698                    "add", "\t$dst, $rhs",
699                    [(set tGPR:$dst, (add tGPR:$lhs, imm8_255:$rhs))]>,
700              T1General<{1,1,0,?,?}> {
701   // A8.6.4 T2
702   bits<3> lhs;
703   bits<8> rhs;
704   let Inst{10-8} = lhs;
705   let Inst{7-0}  = rhs;
706 }
707
708 // Add register
709 let isCommutable = 1 in
710 def tADDrr : T1sI<(outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm), IIC_iALUr,
711                    "add", "\t$Rd, $Rn, $Rm",
712                    [(set tGPR:$Rd, (add tGPR:$Rn, tGPR:$Rm))]>,
713              T1General<0b01100> {
714   // A8.6.6 T1
715   bits<3> Rm;
716   bits<3> Rn;
717   bits<3> Rd;
718   let Inst{8-6} = Rm;
719   let Inst{5-3} = Rn;
720   let Inst{2-0} = Rd;
721 }
722
723 let neverHasSideEffects = 1 in
724 def tADDhirr : T1pIt<(outs GPR:$dst), (ins GPR:$lhs, GPR:$rhs), IIC_iALUr,
725                      "add", "\t$dst, $rhs", []>,
726                T1Special<{0,0,?,?}> {
727   // A8.6.6 T2
728   bits<4> dst;
729   bits<4> rhs;
730   let Inst{6-3} = rhs;
731   let Inst{7}   = dst{3};
732   let Inst{2-0} = dst{2-0};
733 }
734
735 // AND register
736 let isCommutable = 1 in
737 def tAND : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iBITr,
738                  "and", "\t$dst, $rhs",
739                  [(set tGPR:$dst, (and tGPR:$lhs, tGPR:$rhs))]>,
740            T1DataProcessing<0b0000> {
741   // A8.6.12
742   bits<3> rhs;
743   bits<3> dst;
744   let Inst{5-3} = rhs;
745   let Inst{2-0} = dst;
746 }
747
748 // ASR immediate
749 def tASRri : T1sI<(outs tGPR:$Rd), (ins tGPR:$Rm, i32imm:$imm5), IIC_iMOVsi,
750                   "asr", "\t$Rd, $Rm, $imm5",
751                   [(set tGPR:$Rd, (sra tGPR:$Rm, (i32 imm:$imm5)))]>,
752              T1General<{0,1,0,?,?}> {
753   // A8.6.14
754   bits<3> Rd;
755   bits<3> Rm;
756   bits<5> imm5;
757   let Inst{10-6} = imm5;
758   let Inst{5-3}  = Rm;
759   let Inst{2-0}  = Rd;
760 }
761
762 // ASR register
763 def tASRrr : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iMOVsr,
764                    "asr", "\t$dst, $rhs",
765                    [(set tGPR:$dst, (sra tGPR:$lhs, tGPR:$rhs))]>,
766              T1DataProcessing<0b0100> {
767   // A8.6.15
768   bits<3> rhs;
769   bits<3> dst;
770   let Inst{5-3} = rhs;
771   let Inst{2-0} = dst;
772 }
773
774 // BIC register
775 def tBIC : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iBITr,
776                  "bic", "\t$dst, $rhs",
777                  [(set tGPR:$dst, (and tGPR:$lhs, (not tGPR:$rhs)))]>,
778            T1DataProcessing<0b1110> {
779   // A8.6.20
780   bits<3> dst;
781   bits<3> rhs;
782   let Inst{5-3} = rhs;
783   let Inst{2-0} = dst;
784 }
785
786 // CMN register
787 let isCompare = 1, Defs = [CPSR] in {
788 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
789 //       Compare-to-zero still works out, just not the relationals
790 //def tCMN : T1pI<(outs), (ins tGPR:$lhs, tGPR:$rhs), IIC_iCMPr,
791 //                "cmn", "\t$lhs, $rhs",
792 //                [(ARMcmp tGPR:$lhs, (ineg tGPR:$rhs))]>,
793 //           T1DataProcessing<0b1011>;
794 def tCMNz : T1pI<(outs), (ins tGPR:$Rn, tGPR:$Rm), IIC_iCMPr,
795                  "cmn", "\t$Rn, $Rm",
796                  [(ARMcmpZ tGPR:$Rn, (ineg tGPR:$Rm))]>,
797             T1DataProcessing<0b1011> {
798   // A8.6.33
799   bits<3> Rm;
800   bits<3> Rn;
801   let Inst{5-3} = Rm;
802   let Inst{2-0} = Rn;
803 }
804 }
805
806 // CMP immediate
807 let isCompare = 1, Defs = [CPSR] in {
808 def tCMPi8 : T1pI<(outs), (ins tGPR:$Rn, i32imm:$imm8), IIC_iCMPi,
809                   "cmp", "\t$Rn, $imm8",
810                   [(ARMcmp tGPR:$Rn, imm0_255:$imm8)]>,
811              T1General<{1,0,1,?,?}> {
812   // A8.6.35
813   bits<3> Rn;
814   bits<8> imm8;
815   let Inst{10-8} = Rn;
816   let Inst{7-0}  = imm8;
817 }
818
819 def tCMPzi8 : T1pI<(outs), (ins tGPR:$Rn, i32imm:$imm8), IIC_iCMPi,
820                   "cmp", "\t$Rn, $imm8",
821                   [(ARMcmpZ tGPR:$Rn, imm0_255:$imm8)]>,
822               T1General<{1,0,1,?,?}> {
823   // A8.6.35
824   bits<3> Rn;
825   let Inst{10-8} = Rn;
826   let Inst{7-0}  = 0x00;
827 }
828
829 // CMP register
830 def tCMPr : T1pI<(outs), (ins tGPR:$Rn, tGPR:$Rm), IIC_iCMPr,
831                  "cmp", "\t$Rn, $Rm",
832                  [(ARMcmp tGPR:$Rn, tGPR:$Rm)]>,
833             T1DataProcessing<0b1010> {
834   // A8.6.36
835   bits<3> Rm;
836   bits<3> Rn;
837   let Inst{5-3} = Rm;
838   let Inst{2-0} = Rn;
839 }
840
841 def tCMPzr : T1pI<(outs), (ins tGPR:$lhs, tGPR:$rhs), IIC_iCMPr,
842                   "cmp", "\t$lhs, $rhs",
843                   [(ARMcmpZ tGPR:$lhs, tGPR:$rhs)]>,
844              T1DataProcessing<0b1010>;
845
846 def tCMPhir : T1pI<(outs), (ins GPR:$lhs, GPR:$rhs), IIC_iCMPr,
847                    "cmp", "\t$lhs, $rhs", []>,
848               T1Special<{0,1,?,?}>;
849 def tCMPzhir : T1pI<(outs), (ins GPR:$lhs, GPR:$rhs), IIC_iCMPr,
850                     "cmp", "\t$lhs, $rhs", []>,
851                T1Special<{0,1,?,?}>;
852 } // isCompare = 1, Defs = [CPSR]
853
854
855 // XOR register
856 let isCommutable = 1 in
857 def tEOR : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iBITr,
858                  "eor", "\t$dst, $rhs",
859                  [(set tGPR:$dst, (xor tGPR:$lhs, tGPR:$rhs))]>,
860            T1DataProcessing<0b0001> {
861   // A8.6.45
862   bits<3> dst;
863   bits<3> rhs;
864   let Inst{5-3} = rhs;
865   let Inst{2-0} = dst;
866 }
867
868 // LSL immediate
869 def tLSLri : T1sI<(outs tGPR:$Rd), (ins tGPR:$Rm, i32imm:$imm5), IIC_iMOVsi,
870                   "lsl", "\t$Rd, $Rm, $imm5",
871                   [(set tGPR:$Rd, (shl tGPR:$Rm, (i32 imm:$imm5)))]>,
872              T1General<{0,0,0,?,?}> {
873   // A8.6.88
874   bits<3> Rd;
875   bits<3> Rm;
876   bits<5> imm5;
877   let Inst{10-6} = imm5;
878   let Inst{5-3}  = Rm;
879   let Inst{2-0}  = Rd;
880 }
881
882 // LSL register
883 def tLSLrr : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iMOVsr,
884                    "lsl", "\t$dst, $rhs",
885                    [(set tGPR:$dst, (shl tGPR:$lhs, tGPR:$rhs))]>,
886              T1DataProcessing<0b0010> {
887   // A8.6.89
888   bits<3> dst;
889   bits<3> rhs;
890   let Inst{5-3} = rhs;
891   let Inst{2-0} = dst;
892 }
893
894 // LSR immediate
895 def tLSRri : T1sI<(outs tGPR:$Rd), (ins tGPR:$Rm, i32imm:$imm5), IIC_iMOVsi,
896                   "lsr", "\t$Rd, $Rm, $imm5",
897                   [(set tGPR:$Rd, (srl tGPR:$Rm, (i32 imm:$imm5)))]>,
898              T1General<{0,0,1,?,?}> {
899   // A8.6.90
900   bits<3> Rd;
901   bits<3> Rm;
902   bits<5> imm5;
903   let Inst{10-6} = imm5;
904   let Inst{5-3}  = Rm;
905   let Inst{2-0}  = Rd;
906 }
907
908 // LSR register
909 def tLSRrr : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iMOVsr,
910                    "lsr", "\t$dst, $rhs",
911                    [(set tGPR:$dst, (srl tGPR:$lhs, tGPR:$rhs))]>,
912              T1DataProcessing<0b0011> {
913   // A8.6.91
914   bits<3> dst;
915   bits<3> rhs;
916   let Inst{5-3} = rhs;
917   let Inst{2-0} = dst;
918 }
919
920 // Move register
921 let isMoveImm = 1 in
922 def tMOVi8 : T1sI<(outs tGPR:$Rd), (ins i32imm:$imm8), IIC_iMOVi,
923                   "mov", "\t$Rd, $imm8",
924                   [(set tGPR:$Rd, imm0_255:$imm8)]>,
925              T1General<{1,0,0,?,?}> {
926   // A8.6.96
927   bits<3> Rd;
928   bits<8> imm8;
929   let Inst{10-8} = Rd;
930   let Inst{7-0}  = imm8;
931 }
932
933 // TODO: A7-73: MOV(2) - mov setting flag.
934
935 let neverHasSideEffects = 1 in {
936 // FIXME: Make this predicable.
937 def tMOVr       : T1I<(outs tGPR:$dst), (ins tGPR:$src), IIC_iMOVr,
938                       "mov\t$dst, $src", []>,
939                   T1Special<0b1000>;
940 let Defs = [CPSR] in
941 def tMOVSr      : T1I<(outs tGPR:$dst), (ins tGPR:$src), IIC_iMOVr,
942                        "movs\t$dst, $src", []>, Encoding16 {
943   let Inst{15-6} = 0b0000000000;
944 }
945
946 // FIXME: Make these predicable.
947 def tMOVgpr2tgpr : T1I<(outs tGPR:$dst), (ins GPR:$src), IIC_iMOVr,
948                        "mov\t$dst, $src", []>,
949                    T1Special<{1,0,0,?}>;
950 def tMOVtgpr2gpr : T1I<(outs GPR:$dst), (ins tGPR:$src), IIC_iMOVr,
951                        "mov\t$dst, $src", []>,
952                    T1Special<{1,0,?,0}>;
953 def tMOVgpr2gpr  : T1I<(outs GPR:$dst), (ins GPR:$src), IIC_iMOVr,
954                        "mov\t$dst, $src", []>,
955                    T1Special<{1,0,?,?}>;
956 } // neverHasSideEffects
957
958 // multiply register
959 let isCommutable = 1 in
960 def tMUL : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iMUL32,
961                  "mul", "\t$dst, $rhs, $dst", /* A8.6.105 MUL Encoding T1 */
962                  [(set tGPR:$dst, (mul tGPR:$lhs, tGPR:$rhs))]>,
963            T1DataProcessing<0b1101> {
964   // A8.6.105
965   bits<3> dst;
966   bits<3> rhs;
967   let Inst{5-3} = rhs;
968   let Inst{2-0} = dst;
969 }
970
971 // move inverse register
972 def tMVN : T1sI<(outs tGPR:$Rd), (ins tGPR:$Rm), IIC_iMVNr,
973                 "mvn", "\t$Rd, $Rm",
974                 [(set tGPR:$Rd, (not tGPR:$Rm))]>,
975            T1DataProcessing<0b1111> {
976   // A8.6.107
977   bits<3> Rd;
978   bits<3> Rm;
979   let Inst{5-3} = Rm;
980   let Inst{2-0} = Rd;
981 }
982
983 // Bitwise or register
984 let isCommutable = 1 in
985 def tORR : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs),  IIC_iBITr,
986                  "orr", "\t$dst, $rhs",
987                  [(set tGPR:$dst, (or tGPR:$lhs, tGPR:$rhs))]>,
988            T1DataProcessing<0b1100> {
989   // A8.6.114
990   bits<3> dst;
991   bits<3> rhs;
992   let Inst{5-3} = rhs;
993   let Inst{2-0} = dst;
994 }
995
996 // Swaps
997 def tREV : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
998                 "rev", "\t$dst, $src",
999                 [(set tGPR:$dst, (bswap tGPR:$src))]>,
1000                 Requires<[IsThumb, IsThumb1Only, HasV6]>,
1001            T1Misc<{1,0,1,0,0,0,?}>;
1002
1003 def tREV16 : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
1004                   "rev16", "\t$dst, $src",
1005              [(set tGPR:$dst,
1006                    (or (and (srl tGPR:$src, (i32 8)), 0xFF),
1007                        (or (and (shl tGPR:$src, (i32 8)), 0xFF00),
1008                            (or (and (srl tGPR:$src, (i32 8)), 0xFF0000),
1009                                (and (shl tGPR:$src, (i32 8)), 0xFF000000)))))]>,
1010                 Requires<[IsThumb, IsThumb1Only, HasV6]>,
1011              T1Misc<{1,0,1,0,0,1,?}>;
1012
1013 def tREVSH : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
1014                   "revsh", "\t$dst, $src",
1015                   [(set tGPR:$dst,
1016                         (sext_inreg
1017                           (or (srl (and tGPR:$src, 0xFF00), (i32 8)),
1018                               (shl tGPR:$src, (i32 8))), i16))]>,
1019                   Requires<[IsThumb, IsThumb1Only, HasV6]>,
1020              T1Misc<{1,0,1,0,1,1,?}>;
1021
1022 // rotate right register
1023 def tROR : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iMOVsr,
1024                  "ror", "\t$dst, $rhs",
1025                  [(set tGPR:$dst, (rotr tGPR:$lhs, tGPR:$rhs))]>,
1026            T1DataProcessing<0b0111>;
1027
1028 // negate register
1029 def tRSB : T1sI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iALUi,
1030                 "rsb", "\t$dst, $src, #0",
1031                 [(set tGPR:$dst, (ineg tGPR:$src))]>,
1032            T1DataProcessing<0b1001>;
1033
1034 // Subtract with carry register
1035 let Uses = [CPSR] in
1036 def tSBC : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iALUr,
1037                  "sbc", "\t$dst, $rhs",
1038                  [(set tGPR:$dst, (sube tGPR:$lhs, tGPR:$rhs))]>,
1039            T1DataProcessing<0b0110>;
1040
1041 // Subtract immediate
1042 def tSUBi3 : T1sI<(outs tGPR:$dst), (ins tGPR:$lhs, i32imm:$rhs), IIC_iALUi,
1043                   "sub", "\t$dst, $lhs, $rhs",
1044                   [(set tGPR:$dst, (add tGPR:$lhs, imm0_7_neg:$rhs))]>,
1045              T1General<0b01111>;
1046
1047 def tSUBi8 : T1sIt<(outs tGPR:$dst), (ins tGPR:$lhs, i32imm:$rhs), IIC_iALUi,
1048                    "sub", "\t$dst, $rhs",
1049                    [(set tGPR:$dst, (add tGPR:$lhs, imm8_255_neg:$rhs))]>,
1050              T1General<{1,1,1,?,?}>;
1051
1052 // subtract register
1053 def tSUBrr : T1sI<(outs tGPR:$dst), (ins tGPR:$lhs, tGPR:$rhs), IIC_iALUr,
1054                   "sub", "\t$dst, $lhs, $rhs",
1055                   [(set tGPR:$dst, (sub tGPR:$lhs, tGPR:$rhs))]>,
1056              T1General<0b01101>;
1057
1058 // TODO: A7-96: STMIA - store multiple.
1059
1060 // sign-extend byte
1061 def tSXTB  : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
1062                   "sxtb", "\t$dst, $src",
1063                   [(set tGPR:$dst, (sext_inreg tGPR:$src, i8))]>,
1064                   Requires<[IsThumb, IsThumb1Only, HasV6]>,
1065              T1Misc<{0,0,1,0,0,1,?}>;
1066
1067 // sign-extend short
1068 def tSXTH  : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
1069                   "sxth", "\t$dst, $src",
1070                   [(set tGPR:$dst, (sext_inreg tGPR:$src, i16))]>,
1071                   Requires<[IsThumb, IsThumb1Only, HasV6]>,
1072              T1Misc<{0,0,1,0,0,0,?}>;
1073
1074 // test
1075 let isCompare = 1, isCommutable = 1, Defs = [CPSR] in
1076 def tTST  : T1pI<(outs), (ins tGPR:$lhs, tGPR:$rhs), IIC_iTSTr,
1077                  "tst", "\t$lhs, $rhs",
1078                  [(ARMcmpZ (and_su tGPR:$lhs, tGPR:$rhs), 0)]>,
1079             T1DataProcessing<0b1000>;
1080
1081 // zero-extend byte
1082 def tUXTB  : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
1083                   "uxtb", "\t$dst, $src",
1084                   [(set tGPR:$dst, (and tGPR:$src, 0xFF))]>,
1085                   Requires<[IsThumb, IsThumb1Only, HasV6]>,
1086              T1Misc<{0,0,1,0,1,1,?}>;
1087
1088 // zero-extend short
1089 def tUXTH  : T1pI<(outs tGPR:$dst), (ins tGPR:$src), IIC_iUNAr,
1090                   "uxth", "\t$dst, $src",
1091                   [(set tGPR:$dst, (and tGPR:$src, 0xFFFF))]>,
1092                   Requires<[IsThumb, IsThumb1Only, HasV6]>,
1093              T1Misc<{0,0,1,0,1,0,?}>;
1094
1095
1096 // Conditional move tMOVCCr - Used to implement the Thumb SELECT_CC operation.
1097 // Expanded after instruction selection into a branch sequence.
1098 let usesCustomInserter = 1 in  // Expanded after instruction selection.
1099   def tMOVCCr_pseudo :
1100   PseudoInst<(outs tGPR:$dst), (ins tGPR:$false, tGPR:$true, pred:$cc),
1101               NoItinerary,
1102              [/*(set tGPR:$dst, (ARMcmov tGPR:$false, tGPR:$true, imm:$cc))*/]>;
1103
1104
1105 // 16-bit movcc in IT blocks for Thumb2.
1106 let neverHasSideEffects = 1 in {
1107 def tMOVCCr : T1pIt<(outs GPR:$dst), (ins GPR:$lhs, GPR:$rhs), IIC_iCMOVr,
1108                     "mov", "\t$dst, $rhs", []>,
1109               T1Special<{1,0,?,?}>;
1110
1111 let isMoveImm = 1 in
1112 def tMOVCCi : T1pIt<(outs tGPR:$dst), (ins tGPR:$lhs, i32imm:$rhs), IIC_iCMOVi,
1113                     "mov", "\t$dst, $rhs", []>,
1114               T1General<{1,0,0,?,?}>;
1115 } // neverHasSideEffects
1116
1117 // tLEApcrel - Load a pc-relative address into a register without offending the
1118 // assembler.
1119 let neverHasSideEffects = 1 in {
1120 let isReMaterializable = 1 in
1121 def tLEApcrel : T1I<(outs tGPR:$dst), (ins i32imm:$label, pred:$p), IIC_iALUi,
1122                     "adr$p\t$dst, #$label", []>,
1123                 T1Encoding<{1,0,1,0,0,?}>; // A6.2 & A8.6.10
1124
1125 } // neverHasSideEffects
1126 def tLEApcrelJT : T1I<(outs tGPR:$dst),
1127                       (ins i32imm:$label, nohash_imm:$id, pred:$p),
1128                       IIC_iALUi, "adr$p\t$dst, #${label}_${id}", []>,
1129                   T1Encoding<{1,0,1,0,0,?}>; // A6.2 & A8.6.10
1130
1131 //===----------------------------------------------------------------------===//
1132 // TLS Instructions
1133 //
1134
1135 // __aeabi_read_tp preserves the registers r1-r3.
1136 let isCall = 1,
1137   Defs = [R0, LR] in {
1138   def tTPsoft : TIx2<0b11110, 0b11, 1, (outs), (ins), IIC_Br,
1139                      "bl\t__aeabi_read_tp",
1140                      [(set R0, ARMthread_pointer)]>;
1141 }
1142
1143 // SJLJ Exception handling intrinsics
1144 //   eh_sjlj_setjmp() is an instruction sequence to store the return
1145 //   address and save #0 in R0 for the non-longjmp case.
1146 //   Since by its nature we may be coming from some other function to get
1147 //   here, and we're using the stack frame for the containing function to
1148 //   save/restore registers, we can't keep anything live in regs across
1149 //   the eh_sjlj_setjmp(), else it will almost certainly have been tromped upon
1150 //   when we get here from a longjmp(). We force everthing out of registers
1151 //   except for our own input by listing the relevant registers in Defs. By
1152 //   doing so, we also cause the prologue/epilogue code to actively preserve
1153 //   all of the callee-saved resgisters, which is exactly what we want.
1154 //   $val is a scratch register for our use.
1155 let Defs =
1156   [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7, R12 ], hasSideEffects = 1,
1157    isBarrier = 1, isCodeGenOnly = 1 in {
1158   def tInt_eh_sjlj_setjmp : ThumbXI<(outs),(ins tGPR:$src, tGPR:$val),
1159                               AddrModeNone, SizeSpecial, NoItinerary, "", "",
1160                    [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>;
1161 }
1162
1163 // FIXME: Non-Darwin version(s)
1164 let isBarrier = 1, hasSideEffects = 1, isTerminator = 1, isCodeGenOnly = 1,
1165     Defs = [ R7, LR, SP ] in {
1166 def tInt_eh_sjlj_longjmp : XI<(outs), (ins GPR:$src, GPR:$scratch),
1167                              AddrModeNone, SizeSpecial, IndexModeNone,
1168                              Pseudo, NoItinerary, "", "",
1169                          [(ARMeh_sjlj_longjmp GPR:$src, GPR:$scratch)]>,
1170                                 Requires<[IsThumb, IsDarwin]>;
1171 }
1172
1173 //===----------------------------------------------------------------------===//
1174 // Non-Instruction Patterns
1175 //
1176
1177 // Add with carry
1178 def : T1Pat<(addc   tGPR:$lhs, imm0_7:$rhs),
1179             (tADDi3 tGPR:$lhs, imm0_7:$rhs)>;
1180 def : T1Pat<(addc   tGPR:$lhs, imm8_255:$rhs),
1181             (tADDi8 tGPR:$lhs, imm8_255:$rhs)>;
1182 def : T1Pat<(addc   tGPR:$lhs, tGPR:$rhs),
1183             (tADDrr tGPR:$lhs, tGPR:$rhs)>;
1184
1185 // Subtract with carry
1186 def : T1Pat<(addc   tGPR:$lhs, imm0_7_neg:$rhs),
1187             (tSUBi3 tGPR:$lhs, imm0_7_neg:$rhs)>;
1188 def : T1Pat<(addc   tGPR:$lhs, imm8_255_neg:$rhs),
1189             (tSUBi8 tGPR:$lhs, imm8_255_neg:$rhs)>;
1190 def : T1Pat<(subc   tGPR:$lhs, tGPR:$rhs),
1191             (tSUBrr tGPR:$lhs, tGPR:$rhs)>;
1192
1193 // ConstantPool, GlobalAddress
1194 def : T1Pat<(ARMWrapper  tglobaladdr :$dst), (tLEApcrel tglobaladdr :$dst)>;
1195 def : T1Pat<(ARMWrapper  tconstpool  :$dst), (tLEApcrel tconstpool  :$dst)>;
1196
1197 // JumpTable
1198 def : T1Pat<(ARMWrapperJT tjumptable:$dst, imm:$id),
1199             (tLEApcrelJT tjumptable:$dst, imm:$id)>;
1200
1201 // Direct calls
1202 def : T1Pat<(ARMtcall texternalsym:$func), (tBL texternalsym:$func)>,
1203       Requires<[IsThumb, IsNotDarwin]>;
1204 def : T1Pat<(ARMtcall texternalsym:$func), (tBLr9 texternalsym:$func)>,
1205       Requires<[IsThumb, IsDarwin]>;
1206
1207 def : Tv5Pat<(ARMcall texternalsym:$func), (tBLXi texternalsym:$func)>,
1208       Requires<[IsThumb, HasV5T, IsNotDarwin]>;
1209 def : Tv5Pat<(ARMcall texternalsym:$func), (tBLXi_r9 texternalsym:$func)>,
1210       Requires<[IsThumb, HasV5T, IsDarwin]>;
1211
1212 // Indirect calls to ARM routines
1213 def : Tv5Pat<(ARMcall GPR:$dst), (tBLXr GPR:$dst)>,
1214       Requires<[IsThumb, HasV5T, IsNotDarwin]>;
1215 def : Tv5Pat<(ARMcall GPR:$dst), (tBLXr_r9 GPR:$dst)>,
1216       Requires<[IsThumb, HasV5T, IsDarwin]>;
1217
1218 // zextload i1 -> zextload i8
1219 def : T1Pat<(zextloadi1 t_addrmode_s1:$addr),
1220             (tLDRB t_addrmode_s1:$addr)>;
1221
1222 // extload -> zextload
1223 def : T1Pat<(extloadi1  t_addrmode_s1:$addr),  (tLDRB t_addrmode_s1:$addr)>;
1224 def : T1Pat<(extloadi8  t_addrmode_s1:$addr),  (tLDRB t_addrmode_s1:$addr)>;
1225 def : T1Pat<(extloadi16 t_addrmode_s2:$addr),  (tLDRH t_addrmode_s2:$addr)>;
1226
1227 // If it's impossible to use [r,r] address mode for sextload, select to
1228 // ldr{b|h} + sxt{b|h} instead.
1229 def : T1Pat<(sextloadi8 t_addrmode_s1:$addr),
1230             (tSXTB (tLDRB t_addrmode_s1:$addr))>,
1231       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1232 def : T1Pat<(sextloadi16 t_addrmode_s2:$addr),
1233             (tSXTH (tLDRH t_addrmode_s2:$addr))>,
1234       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1235
1236 def : T1Pat<(sextloadi8 t_addrmode_s1:$addr),
1237             (tASRri (tLSLri (tLDRB t_addrmode_s1:$addr), 24), 24)>;
1238 def : T1Pat<(sextloadi16 t_addrmode_s1:$addr),
1239             (tASRri (tLSLri (tLDRH t_addrmode_s1:$addr), 16), 16)>;
1240
1241 // Large immediate handling.
1242
1243 // Two piece imms.
1244 def : T1Pat<(i32 thumb_immshifted:$src),
1245             (tLSLri (tMOVi8 (thumb_immshifted_val imm:$src)),
1246                     (thumb_immshifted_shamt imm:$src))>;
1247
1248 def : T1Pat<(i32 imm0_255_comp:$src),
1249             (tMVN (tMOVi8 (imm_comp_XFORM imm:$src)))>;
1250
1251 // Pseudo instruction that combines ldr from constpool and add pc. This should
1252 // be expanded into two instructions late to allow if-conversion and
1253 // scheduling.
1254 let isReMaterializable = 1 in
1255 def tLDRpci_pic : PseudoInst<(outs GPR:$dst), (ins i32imm:$addr, pclabel:$cp),
1256                    NoItinerary,
1257                [(set GPR:$dst, (ARMpic_add (load (ARMWrapper tconstpool:$addr)),
1258                                            imm:$cp))]>,
1259                Requires<[IsThumb, IsThumb1Only]>;