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