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