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