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