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