]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - contrib/llvm/lib/Target/ARM/ARMInstrThumb.td
dts: Update our copy to Linux 4.17
[FreeBSD/FreeBSD.git] / contrib / llvm / 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 imm_sr_XFORM: SDNodeXForm<imm, [{
19   unsigned Imm = N->getZExtValue();
20   return CurDAG->getTargetConstant((Imm == 32 ? 0 : Imm), SDLoc(N), MVT::i32);
21 }]>;
22 def ThumbSRImmAsmOperand: ImmAsmOperand<1,32> { let Name = "ImmThumbSR"; }
23 def imm_sr : Operand<i32>, PatLeaf<(imm), [{
24   uint64_t Imm = N->getZExtValue();
25   return Imm > 0 && Imm <= 32;
26 }], imm_sr_XFORM> {
27   let PrintMethod = "printThumbSRImm";
28   let ParserMatchClass = ThumbSRImmAsmOperand;
29 }
30
31 def imm0_7_neg : PatLeaf<(i32 imm), [{
32   return (uint32_t)-N->getZExtValue() < 8;
33 }], imm_neg_XFORM>;
34
35 def ThumbModImmNeg1_7AsmOperand : AsmOperandClass { let Name = "ThumbModImmNeg1_7"; }
36 def mod_imm1_7_neg : Operand<i32>, PatLeaf<(imm), [{
37     unsigned Value = -(unsigned)N->getZExtValue();
38     return 0 < Value && Value < 8;
39   }], imm_neg_XFORM> {
40   let ParserMatchClass = ThumbModImmNeg1_7AsmOperand;
41 }
42
43 def ThumbModImmNeg8_255AsmOperand : AsmOperandClass { let Name = "ThumbModImmNeg8_255"; }
44 def mod_imm8_255_neg : Operand<i32>, PatLeaf<(imm), [{
45     unsigned Value = -(unsigned)N->getZExtValue();
46     return 7 < Value && Value < 256;
47   }], imm_neg_XFORM> {
48   let ParserMatchClass = ThumbModImmNeg8_255AsmOperand;
49 }
50
51
52 def imm0_255_comp : PatLeaf<(i32 imm), [{
53   return ~((uint32_t)N->getZExtValue()) < 256;
54 }]>;
55
56 def imm8_255_neg : PatLeaf<(i32 imm), [{
57   unsigned Val = -N->getZExtValue();
58   return Val >= 8 && Val < 256;
59 }], imm_neg_XFORM>;
60
61 // Break imm's up into two pieces: an immediate + a left shift. This uses
62 // thumb_immshifted to match and thumb_immshifted_val and thumb_immshifted_shamt
63 // to get the val/shift pieces.
64 def thumb_immshifted : PatLeaf<(imm), [{
65   return ARM_AM::isThumbImmShiftedVal((unsigned)N->getZExtValue());
66 }]>;
67
68 def thumb_immshifted_val : SDNodeXForm<imm, [{
69   unsigned V = ARM_AM::getThumbImmNonShiftedVal((unsigned)N->getZExtValue());
70   return CurDAG->getTargetConstant(V, SDLoc(N), MVT::i32);
71 }]>;
72
73 def thumb_immshifted_shamt : SDNodeXForm<imm, [{
74   unsigned V = ARM_AM::getThumbImmValShift((unsigned)N->getZExtValue());
75   return CurDAG->getTargetConstant(V, SDLoc(N), MVT::i32);
76 }]>;
77
78 def imm256_510 : ImmLeaf<i32, [{
79   return Imm >= 256 && Imm < 511;
80 }]>;
81
82 def thumb_imm256_510_addend : SDNodeXForm<imm, [{
83   return CurDAG->getTargetConstant(N->getZExtValue() - 255, SDLoc(N), MVT::i32);
84 }]>;
85
86 // Scaled 4 immediate.
87 def t_imm0_1020s4_asmoperand: AsmOperandClass { let Name = "Imm0_1020s4"; }
88 def t_imm0_1020s4 : Operand<i32> {
89   let PrintMethod = "printThumbS4ImmOperand";
90   let ParserMatchClass = t_imm0_1020s4_asmoperand;
91   let OperandType = "OPERAND_IMMEDIATE";
92 }
93
94 def t_imm0_508s4_asmoperand: AsmOperandClass { let Name = "Imm0_508s4"; }
95 def t_imm0_508s4 : Operand<i32> {
96   let PrintMethod = "printThumbS4ImmOperand";
97   let ParserMatchClass = t_imm0_508s4_asmoperand;
98   let OperandType = "OPERAND_IMMEDIATE";
99 }
100 // Alias use only, so no printer is necessary.
101 def t_imm0_508s4_neg_asmoperand: AsmOperandClass { let Name = "Imm0_508s4Neg"; }
102 def t_imm0_508s4_neg : Operand<i32> {
103   let ParserMatchClass = t_imm0_508s4_neg_asmoperand;
104   let OperandType = "OPERAND_IMMEDIATE";
105 }
106
107 // Define Thumb specific addressing modes.
108
109 // unsigned 8-bit, 2-scaled memory offset
110 class OperandUnsignedOffset_b8s2 : AsmOperandClass {
111   let Name = "UnsignedOffset_b8s2";
112   let PredicateMethod = "isUnsignedOffset<8, 2>";
113 }
114
115 def UnsignedOffset_b8s2 : OperandUnsignedOffset_b8s2;
116
117 // thumb style PC relative operand. signed, 8 bits magnitude,
118 // two bits shift. can be represented as either [pc, #imm], #imm,
119 // or relocatable expression...
120 def ThumbMemPC : AsmOperandClass {
121   let Name = "ThumbMemPC";
122 }
123
124 let OperandType = "OPERAND_PCREL" in {
125 def t_brtarget : Operand<OtherVT> {
126   let EncoderMethod = "getThumbBRTargetOpValue";
127   let DecoderMethod = "DecodeThumbBROperand";
128 }
129
130 // ADR instruction labels.
131 def t_adrlabel : Operand<i32> {
132   let EncoderMethod = "getThumbAdrLabelOpValue";
133   let PrintMethod = "printAdrLabelOperand<2>";
134   let ParserMatchClass = UnsignedOffset_b8s2;
135 }
136
137
138 def thumb_br_target : Operand<OtherVT> {
139   let ParserMatchClass = ThumbBranchTarget;
140   let EncoderMethod = "getThumbBranchTargetOpValue";
141   let OperandType = "OPERAND_PCREL";
142 }
143
144 def thumb_bl_target : Operand<i32> {
145   let ParserMatchClass = ThumbBranchTarget;
146   let EncoderMethod = "getThumbBLTargetOpValue";
147   let DecoderMethod = "DecodeThumbBLTargetOperand";
148 }
149
150 // Target for BLX *from* thumb mode.
151 def thumb_blx_target : Operand<i32> {
152   let ParserMatchClass = ARMBranchTarget;
153   let EncoderMethod = "getThumbBLXTargetOpValue";
154   let DecoderMethod = "DecodeThumbBLXOffset";
155 }
156
157 def thumb_bcc_target : Operand<OtherVT> {
158   let ParserMatchClass = ThumbBranchTarget;
159   let EncoderMethod = "getThumbBCCTargetOpValue";
160   let DecoderMethod = "DecodeThumbBCCTargetOperand";
161 }
162
163 def thumb_cb_target : Operand<OtherVT> {
164   let ParserMatchClass = ThumbBranchTarget;
165   let EncoderMethod = "getThumbCBTargetOpValue";
166   let DecoderMethod = "DecodeThumbCmpBROperand";
167 }
168
169 // t_addrmode_pc := <label> => pc + imm8 * 4
170 //
171 def t_addrmode_pc : MemOperand {
172   let EncoderMethod = "getAddrModePCOpValue";
173   let DecoderMethod = "DecodeThumbAddrModePC";
174   let PrintMethod = "printThumbLdrLabelOperand";
175   let ParserMatchClass = ThumbMemPC;
176 }
177 }
178
179 // t_addrmode_rr := reg + reg
180 //
181 def t_addrmode_rr_asm_operand : AsmOperandClass { let Name = "MemThumbRR"; }
182 def t_addrmode_rr : MemOperand,
183                     ComplexPattern<i32, 2, "SelectThumbAddrModeRR", []> {
184   let EncoderMethod = "getThumbAddrModeRegRegOpValue";
185   let PrintMethod = "printThumbAddrModeRROperand";
186   let DecoderMethod = "DecodeThumbAddrModeRR";
187   let ParserMatchClass = t_addrmode_rr_asm_operand;
188   let MIOperandInfo = (ops tGPR:$base, tGPR:$offsreg);
189 }
190
191 // t_addrmode_rrs := reg + reg
192 //
193 // We use separate scaled versions because the Select* functions need
194 // to explicitly check for a matching constant and return false here so that
195 // the reg+imm forms will match instead. This is a horrible way to do that,
196 // as it forces tight coupling between the methods, but it's how selectiondag
197 // currently works.
198 def t_addrmode_rrs1 : MemOperand,
199                       ComplexPattern<i32, 2, "SelectThumbAddrModeRI5S1", []> {
200   let EncoderMethod = "getThumbAddrModeRegRegOpValue";
201   let PrintMethod = "printThumbAddrModeRROperand";
202   let DecoderMethod = "DecodeThumbAddrModeRR";
203   let ParserMatchClass = t_addrmode_rr_asm_operand;
204   let MIOperandInfo = (ops tGPR:$base, tGPR:$offsreg);
205 }
206 def t_addrmode_rrs2 : MemOperand,
207                       ComplexPattern<i32, 2, "SelectThumbAddrModeRI5S2", []> {
208   let EncoderMethod = "getThumbAddrModeRegRegOpValue";
209   let DecoderMethod = "DecodeThumbAddrModeRR";
210   let PrintMethod = "printThumbAddrModeRROperand";
211   let ParserMatchClass = t_addrmode_rr_asm_operand;
212   let MIOperandInfo = (ops tGPR:$base, tGPR:$offsreg);
213 }
214 def t_addrmode_rrs4 : MemOperand,
215                       ComplexPattern<i32, 2, "SelectThumbAddrModeRI5S4", []> {
216   let EncoderMethod = "getThumbAddrModeRegRegOpValue";
217   let DecoderMethod = "DecodeThumbAddrModeRR";
218   let PrintMethod = "printThumbAddrModeRROperand";
219   let ParserMatchClass = t_addrmode_rr_asm_operand;
220   let MIOperandInfo = (ops tGPR:$base, tGPR:$offsreg);
221 }
222
223 // t_addrmode_is4 := reg + imm5 * 4
224 //
225 def t_addrmode_is4_asm_operand : AsmOperandClass { let Name = "MemThumbRIs4"; }
226 def t_addrmode_is4 : MemOperand,
227                      ComplexPattern<i32, 2, "SelectThumbAddrModeImm5S4", []> {
228   let EncoderMethod = "getAddrModeISOpValue";
229   let DecoderMethod = "DecodeThumbAddrModeIS";
230   let PrintMethod = "printThumbAddrModeImm5S4Operand";
231   let ParserMatchClass = t_addrmode_is4_asm_operand;
232   let MIOperandInfo = (ops tGPR:$base, i32imm:$offsimm);
233 }
234
235 // t_addrmode_is2 := reg + imm5 * 2
236 //
237 def t_addrmode_is2_asm_operand : AsmOperandClass { let Name = "MemThumbRIs2"; }
238 def t_addrmode_is2 : MemOperand,
239                      ComplexPattern<i32, 2, "SelectThumbAddrModeImm5S2", []> {
240   let EncoderMethod = "getAddrModeISOpValue";
241   let DecoderMethod = "DecodeThumbAddrModeIS";
242   let PrintMethod = "printThumbAddrModeImm5S2Operand";
243   let ParserMatchClass = t_addrmode_is2_asm_operand;
244   let MIOperandInfo = (ops tGPR:$base, i32imm:$offsimm);
245 }
246
247 // t_addrmode_is1 := reg + imm5
248 //
249 def t_addrmode_is1_asm_operand : AsmOperandClass { let Name = "MemThumbRIs1"; }
250 def t_addrmode_is1 : MemOperand,
251                      ComplexPattern<i32, 2, "SelectThumbAddrModeImm5S1", []> {
252   let EncoderMethod = "getAddrModeISOpValue";
253   let DecoderMethod = "DecodeThumbAddrModeIS";
254   let PrintMethod = "printThumbAddrModeImm5S1Operand";
255   let ParserMatchClass = t_addrmode_is1_asm_operand;
256   let MIOperandInfo = (ops tGPR:$base, i32imm:$offsimm);
257 }
258
259 // t_addrmode_sp := sp + imm8 * 4
260 //
261 // FIXME: This really shouldn't have an explicit SP operand at all. It should
262 // be implicit, just like in the instruction encoding itself.
263 def t_addrmode_sp_asm_operand : AsmOperandClass { let Name = "MemThumbSPI"; }
264 def t_addrmode_sp : MemOperand,
265                     ComplexPattern<i32, 2, "SelectThumbAddrModeSP", []> {
266   let EncoderMethod = "getAddrModeThumbSPOpValue";
267   let DecoderMethod = "DecodeThumbAddrModeSP";
268   let PrintMethod = "printThumbAddrModeSPOperand";
269   let ParserMatchClass = t_addrmode_sp_asm_operand;
270   let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
271 }
272
273 //===----------------------------------------------------------------------===//
274 //  Miscellaneous Instructions.
275 //
276
277 // FIXME: Marking these as hasSideEffects is necessary to prevent machine DCE
278 // from removing one half of the matched pairs. That breaks PEI, which assumes
279 // these will always be in pairs, and asserts if it finds otherwise. Better way?
280 let Defs = [SP], Uses = [SP], hasSideEffects = 1 in {
281 def tADJCALLSTACKUP :
282   PseudoInst<(outs), (ins i32imm:$amt1, i32imm:$amt2), NoItinerary,
283              [(ARMcallseq_end imm:$amt1, imm:$amt2)]>,
284             Requires<[IsThumb, IsThumb1Only]>;
285
286 def tADJCALLSTACKDOWN :
287   PseudoInst<(outs), (ins i32imm:$amt, i32imm:$amt2), NoItinerary,
288              [(ARMcallseq_start imm:$amt, imm:$amt2)]>,
289             Requires<[IsThumb, IsThumb1Only]>;
290 }
291
292 class T1SystemEncoding<bits<8> opc>
293   : T1Encoding<0b101111> {
294   let Inst{9-8} = 0b11;
295   let Inst{7-0} = opc;
296 }
297
298 def tHINT : T1pI<(outs), (ins imm0_15:$imm), NoItinerary, "hint", "\t$imm",
299                  [(int_arm_hint imm0_15:$imm)]>,
300             T1SystemEncoding<0x00>,
301             Requires<[IsThumb, HasV6M]> {
302   bits<4> imm;
303   let Inst{7-4} = imm;
304 }
305
306 // Note: When EmitPriority == 1, the alias will be used for printing
307 class tHintAlias<string Asm, dag Result, bit EmitPriority = 0> : tInstAlias<Asm, Result, EmitPriority> {
308   let Predicates = [IsThumb, HasV6M];
309 }
310
311 def : tHintAlias<"nop$p", (tHINT 0, pred:$p), 1>; // A8.6.110
312 def : tHintAlias<"yield$p", (tHINT 1, pred:$p), 1>; // A8.6.410
313 def : tHintAlias<"wfe$p", (tHINT 2, pred:$p), 1>; // A8.6.408
314 def : tHintAlias<"wfi$p", (tHINT 3, pred:$p), 1>; // A8.6.409
315 def : tHintAlias<"sev$p", (tHINT 4, pred:$p), 1>; // A8.6.157
316 def : tInstAlias<"sevl$p", (tHINT 5, pred:$p), 1> {
317   let Predicates = [IsThumb2, HasV8];
318 }
319
320 // The imm operand $val can be used by a debugger to store more information
321 // about the breakpoint.
322 def tBKPT : T1I<(outs), (ins imm0_255:$val), NoItinerary, "bkpt\t$val",
323                 []>,
324            T1Encoding<0b101111> {
325   let Inst{9-8} = 0b10;
326   // A8.6.22
327   bits<8> val;
328   let Inst{7-0} = val;
329 }
330 // default immediate for breakpoint mnemonic
331 def : InstAlias<"bkpt", (tBKPT 0), 0>, Requires<[IsThumb]>;
332
333 def tHLT : T1I<(outs), (ins imm0_63:$val), NoItinerary, "hlt\t$val",
334                 []>, T1Encoding<0b101110>, Requires<[IsThumb, HasV8]> {
335   let Inst{9-6} = 0b1010;
336   bits<6> val;
337   let Inst{5-0} = val;
338 }
339
340 def tSETEND : T1I<(outs), (ins setend_op:$end), NoItinerary, "setend\t$end",
341                   []>, T1Encoding<0b101101>, Requires<[IsThumb, IsNotMClass]>, Deprecated<HasV8Ops> {
342   bits<1> end;
343   // A8.6.156
344   let Inst{9-5} = 0b10010;
345   let Inst{4}   = 1;
346   let Inst{3}   = end;
347   let Inst{2-0} = 0b000;
348 }
349
350 // Change Processor State is a system instruction -- for disassembly only.
351 def tCPS : T1I<(outs), (ins imod_op:$imod, iflags_op:$iflags),
352                 NoItinerary, "cps$imod $iflags", []>,
353            T1Misc<0b0110011> {
354   // A8.6.38 & B6.1.1
355   bit imod;
356   bits<3> iflags;
357
358   let Inst{4}   = imod;
359   let Inst{3}   = 0;
360   let Inst{2-0} = iflags;
361   let DecoderMethod = "DecodeThumbCPS";
362 }
363
364 // For both thumb1 and thumb2.
365 let isNotDuplicable = 1, isCodeGenOnly = 1 in
366 def tPICADD : TIt<(outs GPR:$dst), (ins GPR:$lhs, pclabel:$cp), IIC_iALUr, "",
367                   [(set GPR:$dst, (ARMpic_add GPR:$lhs, imm:$cp))]>,
368               T1Special<{0,0,?,?}>, Sched<[WriteALU]> {
369   // A8.6.6
370   bits<3> dst;
371   let Inst{6-3} = 0b1111; // Rm = pc
372   let Inst{2-0} = dst;
373 }
374
375 // ADD <Rd>, sp, #<imm8>
376 // FIXME: This should not be marked as having side effects, and it should be
377 // rematerializable. Clearing the side effect bit causes miscompilations,
378 // probably because the instruction can be moved around.
379 def tADDrSPi : T1pI<(outs tGPR:$dst), (ins GPRsp:$sp, t_imm0_1020s4:$imm),
380                     IIC_iALUi, "add", "\t$dst, $sp, $imm", []>,
381                T1Encoding<{1,0,1,0,1,?}>, Sched<[WriteALU]> {
382   // A6.2 & A8.6.8
383   bits<3> dst;
384   bits<8> imm;
385   let Inst{10-8} = dst;
386   let Inst{7-0}  = imm;
387   let DecoderMethod = "DecodeThumbAddSpecialReg";
388 }
389
390 // Thumb1 frame lowering is rather fragile, we hope to be able to use
391 // tADDrSPi, but we may need to insert a sequence that clobbers CPSR.
392 def tADDframe : PseudoInst<(outs tGPR:$dst), (ins i32imm:$base, i32imm:$offset),
393                            NoItinerary, []>,
394                 Requires<[IsThumb, IsThumb1Only]> {
395   let Defs = [CPSR];
396 }
397
398 // ADD sp, sp, #<imm7>
399 def tADDspi : T1pIt<(outs GPRsp:$Rdn), (ins GPRsp:$Rn, t_imm0_508s4:$imm),
400                      IIC_iALUi, "add", "\t$Rdn, $imm", []>,
401               T1Misc<{0,0,0,0,0,?,?}>, Sched<[WriteALU]> {
402   // A6.2.5 & A8.6.8
403   bits<7> imm;
404   let Inst{6-0} = imm;
405   let DecoderMethod = "DecodeThumbAddSPImm";
406 }
407
408 // SUB sp, sp, #<imm7>
409 // FIXME: The encoding and the ASM string don't match up.
410 def tSUBspi : T1pIt<(outs GPRsp:$Rdn), (ins GPRsp:$Rn, t_imm0_508s4:$imm),
411                     IIC_iALUi, "sub", "\t$Rdn, $imm", []>,
412               T1Misc<{0,0,0,0,1,?,?}>, Sched<[WriteALU]> {
413   // A6.2.5 & A8.6.214
414   bits<7> imm;
415   let Inst{6-0} = imm;
416   let DecoderMethod = "DecodeThumbAddSPImm";
417 }
418
419 def : tInstSubst<"add${p} sp, $imm",
420                  (tSUBspi SP, t_imm0_508s4_neg:$imm, pred:$p)>;
421 def : tInstSubst<"add${p} sp, sp, $imm",
422                  (tSUBspi SP, t_imm0_508s4_neg:$imm, pred:$p)>;
423
424 // Can optionally specify SP as a three operand instruction.
425 def : tInstAlias<"add${p} sp, sp, $imm",
426                  (tADDspi SP, t_imm0_508s4:$imm, pred:$p)>;
427 def : tInstAlias<"sub${p} sp, sp, $imm",
428                  (tSUBspi SP, t_imm0_508s4:$imm, pred:$p)>;
429
430 // ADD <Rm>, sp
431 def tADDrSP : T1pI<(outs GPR:$Rdn), (ins GPRsp:$sp, GPR:$Rn), IIC_iALUr,
432                    "add", "\t$Rdn, $sp, $Rn", []>,
433               T1Special<{0,0,?,?}>, Sched<[WriteALU]> {
434   // A8.6.9 Encoding T1
435   bits<4> Rdn;
436   let Inst{7}   = Rdn{3};
437   let Inst{6-3} = 0b1101;
438   let Inst{2-0} = Rdn{2-0};
439   let DecoderMethod = "DecodeThumbAddSPReg";
440 }
441
442 // ADD sp, <Rm>
443 def tADDspr : T1pIt<(outs GPRsp:$Rdn), (ins GPRsp:$Rn, GPR:$Rm), IIC_iALUr,
444                   "add", "\t$Rdn, $Rm", []>,
445               T1Special<{0,0,?,?}>, Sched<[WriteALU]> {
446   // A8.6.9 Encoding T2
447   bits<4> Rm;
448   let Inst{7} = 1;
449   let Inst{6-3} = Rm;
450   let Inst{2-0} = 0b101;
451   let DecoderMethod = "DecodeThumbAddSPReg";
452 }
453
454 //===----------------------------------------------------------------------===//
455 //  Control Flow Instructions.
456 //
457
458 // Indirect branches
459 let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
460   def tBX : TI<(outs), (ins GPR:$Rm, pred:$p), IIC_Br, "bx${p}\t$Rm", []>,
461             T1Special<{1,1,0,?}>, Sched<[WriteBr]> {
462     // A6.2.3 & A8.6.25
463     bits<4> Rm;
464     let Inst{6-3} = Rm;
465     let Inst{2-0} = 0b000;
466     let Unpredictable{2-0} = 0b111;
467   }
468   def tBXNS : TI<(outs), (ins GPR:$Rm, pred:$p), IIC_Br, "bxns${p}\t$Rm", []>,
469               Requires<[IsThumb, Has8MSecExt]>,
470               T1Special<{1,1,0,?}>, Sched<[WriteBr]> {
471     bits<4> Rm;
472     let Inst{6-3} = Rm;
473     let Inst{2-0} = 0b100;
474     let Unpredictable{1-0} = 0b11;
475   }
476 }
477
478 let isReturn = 1, isTerminator = 1, isBarrier = 1 in {
479   def tBX_RET : tPseudoExpand<(outs), (ins pred:$p), 2, IIC_Br,
480                    [(ARMretflag)], (tBX LR, pred:$p)>, Sched<[WriteBr]>;
481
482   // Alternative return instruction used by vararg functions.
483   def tBX_RET_vararg : tPseudoExpand<(outs), (ins tGPR:$Rm, pred:$p),
484                    2, IIC_Br, [],
485                    (tBX GPR:$Rm, pred:$p)>, Sched<[WriteBr]>;
486 }
487
488 // All calls clobber the non-callee saved registers. SP is marked as a use to
489 // prevent stack-pointer assignments that appear immediately before calls from
490 // potentially appearing dead.
491 let isCall = 1,
492   Defs = [LR], Uses = [SP] in {
493   // Also used for Thumb2
494   def tBL  : TIx2<0b11110, 0b11, 1,
495                   (outs), (ins pred:$p, thumb_bl_target:$func), IIC_Br,
496                   "bl${p}\t$func",
497                   [(ARMcall tglobaladdr:$func)]>,
498              Requires<[IsThumb]>, Sched<[WriteBrL]> {
499     bits<24> func;
500     let Inst{26} = func{23};
501     let Inst{25-16} = func{20-11};
502     let Inst{13} = func{22};
503     let Inst{11} = func{21};
504     let Inst{10-0} = func{10-0};
505   }
506
507   // ARMv5T and above, also used for Thumb2
508   def tBLXi : TIx2<0b11110, 0b11, 0,
509                  (outs), (ins pred:$p, thumb_blx_target:$func), IIC_Br,
510                    "blx${p}\t$func", []>,
511               Requires<[IsThumb, HasV5T, IsNotMClass]>, Sched<[WriteBrL]> {
512     bits<24> func;
513     let Inst{26} = func{23};
514     let Inst{25-16} = func{20-11};
515     let Inst{13} = func{22};
516     let Inst{11} = func{21};
517     let Inst{10-1} = func{10-1};
518     let Inst{0} = 0; // func{0} is assumed zero
519   }
520
521   // Also used for Thumb2
522   def tBLXr : TI<(outs), (ins pred:$p, GPR:$func), IIC_Br,
523                   "blx${p}\t$func",
524                   [(ARMcall GPR:$func)]>,
525               Requires<[IsThumb, HasV5T]>,
526               T1Special<{1,1,1,?}>, Sched<[WriteBrL]> { // A6.2.3 & A8.6.24;
527     bits<4> func;
528     let Inst{6-3} = func;
529     let Inst{2-0} = 0b000;
530   }
531
532   // ARMv8-M Security Extensions
533   def tBLXNSr : TI<(outs), (ins pred:$p, GPRnopc:$func), IIC_Br,
534                    "blxns${p}\t$func", []>,
535                 Requires<[IsThumb, Has8MSecExt]>,
536                 T1Special<{1,1,1,?}>, Sched<[WriteBrL]> {
537     bits<4> func;
538     let Inst{6-3} = func;
539     let Inst{2-0} = 0b100;
540     let Unpredictable{1-0} = 0b11;
541   }
542
543   // ARMv4T
544   def tBX_CALL : tPseudoInst<(outs), (ins tGPR:$func),
545                   4, IIC_Br,
546                   [(ARMcall_nolink tGPR:$func)]>,
547             Requires<[IsThumb, IsThumb1Only]>, Sched<[WriteBr]>;
548 }
549
550 let isBranch = 1, isTerminator = 1, isBarrier = 1 in {
551   let isPredicable = 1 in
552   def tB   : T1pI<(outs), (ins t_brtarget:$target), IIC_Br,
553                  "b", "\t$target", [(br bb:$target)]>,
554              T1Encoding<{1,1,1,0,0,?}>, Sched<[WriteBr]> {
555     bits<11> target;
556     let Inst{10-0} = target;
557     let AsmMatchConverter = "cvtThumbBranches";
558  }
559
560   // Far jump
561   // Just a pseudo for a tBL instruction. Needed to let regalloc know about
562   // the clobber of LR.
563   let Defs = [LR] in
564   def tBfar : tPseudoExpand<(outs), (ins thumb_bl_target:$target, pred:$p),
565                           4, IIC_Br, [],
566                           (tBL pred:$p, thumb_bl_target:$target)>,
567                           Sched<[WriteBrTbl]>;
568
569   def tBR_JTr : tPseudoInst<(outs),
570                       (ins tGPR:$target, i32imm:$jt),
571                       0, IIC_Br,
572                       [(ARMbrjt tGPR:$target, tjumptable:$jt)]>,
573                       Sched<[WriteBrTbl]> {
574     let Size = 2;
575     list<Predicate> Predicates = [IsThumb, IsThumb1Only];
576   }
577 }
578
579 // FIXME: should be able to write a pattern for ARMBrcond, but can't use
580 // a two-value operand where a dag node expects two operands. :(
581 let isBranch = 1, isTerminator = 1 in
582   def tBcc : T1I<(outs), (ins thumb_bcc_target:$target, pred:$p), IIC_Br,
583                  "b${p}\t$target",
584                  [/*(ARMbrcond bb:$target, imm:$cc)*/]>,
585              T1BranchCond<{1,1,0,1}>, Sched<[WriteBr]> {
586   bits<4> p;
587   bits<8> target;
588   let Inst{11-8} = p;
589   let Inst{7-0} = target;
590   let AsmMatchConverter = "cvtThumbBranches";
591 }
592
593
594 // Tail calls
595 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in {
596   // IOS versions.
597   let Uses = [SP] in {
598     def tTAILJMPr : tPseudoExpand<(outs), (ins tcGPR:$dst),
599                      4, IIC_Br, [],
600                      (tBX GPR:$dst, (ops 14, zero_reg))>,
601                      Requires<[IsThumb]>, Sched<[WriteBr]>;
602   }
603   // tTAILJMPd: MachO version uses a Thumb2 branch (no Thumb1 tail calls
604   // on MachO), so it's in ARMInstrThumb2.td.
605   // Non-MachO version:
606   let Uses = [SP] in {
607     def tTAILJMPdND : tPseudoExpand<(outs),
608                    (ins t_brtarget:$dst, pred:$p),
609                    4, IIC_Br, [],
610                    (tB t_brtarget:$dst, pred:$p)>,
611                  Requires<[IsThumb, IsNotMachO]>, Sched<[WriteBr]>;
612   }
613 }
614
615
616 // A8.6.218 Supervisor Call (Software Interrupt)
617 // A8.6.16 B: Encoding T1
618 // If Inst{11-8} == 0b1111 then SEE SVC
619 let isCall = 1, Uses = [SP] in
620 def tSVC : T1pI<(outs), (ins imm0_255:$imm), IIC_Br,
621                 "svc", "\t$imm", []>, Encoding16, Sched<[WriteBr]> {
622   bits<8> imm;
623   let Inst{15-12} = 0b1101;
624   let Inst{11-8}  = 0b1111;
625   let Inst{7-0}   = imm;
626 }
627
628 // The assembler uses 0xDEFE for a trap instruction.
629 let isBarrier = 1, isTerminator = 1 in
630 def tTRAP : TI<(outs), (ins), IIC_Br,
631                "trap", [(trap)]>, Encoding16, Sched<[WriteBr]> {
632   let Inst = 0xdefe;
633 }
634
635 //===----------------------------------------------------------------------===//
636 //  Load Store Instructions.
637 //
638
639 // PC-relative loads need to be matched first as constant pool accesses need to
640 // always be PC-relative. We do this using AddedComplexity, as the pattern is
641 // simpler than the patterns of the other load instructions.
642 let canFoldAsLoad = 1, isReMaterializable = 1, AddedComplexity = 10 in
643 def tLDRpci : T1pIs<(outs tGPR:$Rt), (ins t_addrmode_pc:$addr), IIC_iLoad_i,
644                   "ldr", "\t$Rt, $addr",
645                   [(set tGPR:$Rt, (load (ARMWrapper tconstpool:$addr)))]>,
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 // SP-relative loads should be matched before standard immediate-offset loads as
655 // it means we avoid having to move SP to another register.
656 let canFoldAsLoad = 1 in
657 def tLDRspi : T1pIs<(outs tGPR:$Rt), (ins t_addrmode_sp:$addr), IIC_iLoad_i,
658                     "ldr", "\t$Rt, $addr",
659                     [(set tGPR:$Rt, (load t_addrmode_sp:$addr))]>,
660               T1LdStSP<{1,?,?}> {
661   bits<3> Rt;
662   bits<8> addr;
663   let Inst{10-8} = Rt;
664   let Inst{7-0} = addr;
665 }
666
667 // Loads: reg/reg and reg/imm5
668 let canFoldAsLoad = 1, isReMaterializable = 1 in
669 multiclass thumb_ld_rr_ri_enc<bits<3> reg_opc, bits<4> imm_opc,
670                               Operand AddrMode_r, Operand AddrMode_i,
671                               AddrMode am, InstrItinClass itin_r,
672                               InstrItinClass itin_i, string asm,
673                               PatFrag opnode> {
674   // Immediate-offset loads should be matched before register-offset loads as
675   // when the offset is a constant it's simpler to first check if it fits in the
676   // immediate offset field then fall back to register-offset if it doesn't.
677   def i : // reg/imm5
678     T1pILdStEncodeImm<imm_opc, 1 /* Load */,
679                       (outs tGPR:$Rt), (ins AddrMode_i:$addr),
680                       am, itin_i, asm, "\t$Rt, $addr",
681                       [(set tGPR:$Rt, (opnode AddrMode_i:$addr))]>;
682   // Register-offset loads are matched last.
683   def r : // reg/reg
684     T1pILdStEncode<reg_opc,
685                    (outs tGPR:$Rt), (ins AddrMode_r:$addr),
686                    am, itin_r, asm, "\t$Rt, $addr",
687                    [(set tGPR:$Rt, (opnode AddrMode_r:$addr))]>;
688 }
689 // Stores: reg/reg and reg/imm5
690 multiclass thumb_st_rr_ri_enc<bits<3> reg_opc, bits<4> imm_opc,
691                               Operand AddrMode_r, Operand AddrMode_i,
692                               AddrMode am, InstrItinClass itin_r,
693                               InstrItinClass itin_i, string asm,
694                               PatFrag opnode> {
695   def i : // reg/imm5
696     T1pILdStEncodeImm<imm_opc, 0 /* Store */,
697                       (outs), (ins tGPR:$Rt, AddrMode_i:$addr),
698                       am, itin_i, asm, "\t$Rt, $addr",
699                       [(opnode tGPR:$Rt, AddrMode_i:$addr)]>;
700   def r : // reg/reg
701     T1pILdStEncode<reg_opc,
702                    (outs), (ins tGPR:$Rt, AddrMode_r:$addr),
703                    am, itin_r, asm, "\t$Rt, $addr",
704                    [(opnode tGPR:$Rt, AddrMode_r:$addr)]>;
705 }
706
707 // A8.6.57 & A8.6.60
708 defm tLDR  : thumb_ld_rr_ri_enc<0b100, 0b0110, t_addrmode_rr,
709                                 t_addrmode_is4, AddrModeT1_4,
710                                 IIC_iLoad_r, IIC_iLoad_i, "ldr",
711                                 load>;
712
713 // A8.6.64 & A8.6.61
714 defm tLDRB : thumb_ld_rr_ri_enc<0b110, 0b0111, t_addrmode_rr,
715                                 t_addrmode_is1, AddrModeT1_1,
716                                 IIC_iLoad_bh_r, IIC_iLoad_bh_i, "ldrb",
717                                 zextloadi8>;
718
719 // A8.6.76 & A8.6.73
720 defm tLDRH : thumb_ld_rr_ri_enc<0b101, 0b1000, t_addrmode_rr,
721                                 t_addrmode_is2, AddrModeT1_2,
722                                 IIC_iLoad_bh_r, IIC_iLoad_bh_i, "ldrh",
723                                 zextloadi16>;
724
725 let AddedComplexity = 10 in
726 def tLDRSB :                    // A8.6.80
727   T1pILdStEncode<0b011, (outs tGPR:$Rt), (ins t_addrmode_rr:$addr),
728                  AddrModeT1_1, IIC_iLoad_bh_r,
729                  "ldrsb", "\t$Rt, $addr",
730                  [(set tGPR:$Rt, (sextloadi8 t_addrmode_rr:$addr))]>;
731
732 let AddedComplexity = 10 in
733 def tLDRSH :                    // A8.6.84
734   T1pILdStEncode<0b111, (outs tGPR:$Rt), (ins t_addrmode_rr:$addr),
735                  AddrModeT1_2, IIC_iLoad_bh_r,
736                  "ldrsh", "\t$Rt, $addr",
737                  [(set tGPR:$Rt, (sextloadi16 t_addrmode_rr:$addr))]>;
738
739
740 def tSTRspi : T1pIs<(outs), (ins tGPR:$Rt, t_addrmode_sp:$addr), IIC_iStore_i,
741                     "str", "\t$Rt, $addr",
742                     [(store tGPR:$Rt, t_addrmode_sp:$addr)]>,
743               T1LdStSP<{0,?,?}> {
744   bits<3> Rt;
745   bits<8> addr;
746   let Inst{10-8} = Rt;
747   let Inst{7-0} = addr;
748 }
749
750 // A8.6.194 & A8.6.192
751 defm tSTR  : thumb_st_rr_ri_enc<0b000, 0b0110, t_addrmode_rr,
752                                 t_addrmode_is4, AddrModeT1_4,
753                                 IIC_iStore_r, IIC_iStore_i, "str",
754                                 store>;
755
756 // A8.6.197 & A8.6.195
757 defm tSTRB : thumb_st_rr_ri_enc<0b010, 0b0111, t_addrmode_rr,
758                                 t_addrmode_is1, AddrModeT1_1,
759                                 IIC_iStore_bh_r, IIC_iStore_bh_i, "strb",
760                                 truncstorei8>;
761
762 // A8.6.207 & A8.6.205
763 defm tSTRH : thumb_st_rr_ri_enc<0b001, 0b1000, t_addrmode_rr,
764                                t_addrmode_is2, AddrModeT1_2,
765                                IIC_iStore_bh_r, IIC_iStore_bh_i, "strh",
766                                truncstorei16>;
767
768
769 //===----------------------------------------------------------------------===//
770 //  Load / store multiple Instructions.
771 //
772
773 // These require base address to be written back or one of the loaded regs.
774 let hasSideEffects = 0 in {
775
776 let mayLoad = 1, hasExtraDefRegAllocReq = 1 in
777 def tLDMIA : T1I<(outs), (ins tGPR:$Rn, pred:$p, reglist:$regs, variable_ops),
778         IIC_iLoad_m, "ldm${p}\t$Rn, $regs", []>, T1Encoding<{1,1,0,0,1,?}> {
779   bits<3> Rn;
780   bits<8> regs;
781   let Inst{10-8} = Rn;
782   let Inst{7-0}  = regs;
783 }
784
785 // Writeback version is just a pseudo, as there's no encoding difference.
786 // Writeback happens iff the base register is not in the destination register
787 // list.
788 let mayLoad = 1, hasExtraDefRegAllocReq = 1 in
789 def tLDMIA_UPD :
790     InstTemplate<AddrModeNone, 0, IndexModeNone, Pseudo, GenericDomain,
791                  "$Rn = $wb", IIC_iLoad_mu>,
792     PseudoInstExpansion<(tLDMIA tGPR:$Rn, pred:$p, reglist:$regs)> {
793   let Size = 2;
794   let OutOperandList = (outs GPR:$wb);
795   let InOperandList = (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops);
796   let Pattern = [];
797   let isCodeGenOnly = 1;
798   let isPseudo = 1;
799   list<Predicate> Predicates = [IsThumb];
800 }
801
802 // There is no non-writeback version of STM for Thumb.
803 let mayStore = 1, hasExtraSrcRegAllocReq = 1 in
804 def tSTMIA_UPD : Thumb1I<(outs GPR:$wb),
805                          (ins tGPR:$Rn, pred:$p, reglist:$regs, variable_ops),
806                          AddrModeNone, 2, IIC_iStore_mu,
807                          "stm${p}\t$Rn!, $regs", "$Rn = $wb", []>,
808                      T1Encoding<{1,1,0,0,0,?}> {
809   bits<3> Rn;
810   bits<8> regs;
811   let Inst{10-8} = Rn;
812   let Inst{7-0}  = regs;
813 }
814
815 } // hasSideEffects
816
817 def : InstAlias<"ldm${p} $Rn!, $regs",
818                 (tLDMIA tGPR:$Rn, pred:$p, reglist:$regs), 0>,
819         Requires<[IsThumb, IsThumb1Only]>;
820
821 let mayLoad = 1, Uses = [SP], Defs = [SP], hasExtraDefRegAllocReq = 1 in
822 def tPOP : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
823                IIC_iPop,
824                "pop${p}\t$regs", []>,
825            T1Misc<{1,1,0,?,?,?,?}> {
826   bits<16> regs;
827   let Inst{8}   = regs{15};
828   let Inst{7-0} = regs{7-0};
829 }
830
831 let mayStore = 1, Uses = [SP], Defs = [SP], hasExtraSrcRegAllocReq = 1 in
832 def tPUSH : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
833                 IIC_iStore_m,
834                 "push${p}\t$regs", []>,
835             T1Misc<{0,1,0,?,?,?,?}> {
836   bits<16> regs;
837   let Inst{8}   = regs{14};
838   let Inst{7-0} = regs{7-0};
839 }
840
841 //===----------------------------------------------------------------------===//
842 //  Arithmetic Instructions.
843 //
844
845 // Helper classes for encoding T1pI patterns:
846 class T1pIDPEncode<bits<4> opA, dag oops, dag iops, InstrItinClass itin,
847                    string opc, string asm, list<dag> pattern>
848     : T1pI<oops, iops, itin, opc, asm, pattern>,
849       T1DataProcessing<opA> {
850   bits<3> Rm;
851   bits<3> Rn;
852   let Inst{5-3} = Rm;
853   let Inst{2-0} = Rn;
854 }
855 class T1pIMiscEncode<bits<7> opA, dag oops, dag iops, InstrItinClass itin,
856                      string opc, string asm, list<dag> pattern>
857     : T1pI<oops, iops, itin, opc, asm, pattern>,
858       T1Misc<opA> {
859   bits<3> Rm;
860   bits<3> Rd;
861   let Inst{5-3} = Rm;
862   let Inst{2-0} = Rd;
863 }
864
865 // Helper classes for encoding T1sI patterns:
866 class T1sIDPEncode<bits<4> opA, dag oops, dag iops, InstrItinClass itin,
867                    string opc, string asm, list<dag> pattern>
868     : T1sI<oops, iops, itin, opc, asm, pattern>,
869       T1DataProcessing<opA> {
870   bits<3> Rd;
871   bits<3> Rn;
872   let Inst{5-3} = Rn;
873   let Inst{2-0} = Rd;
874 }
875 class T1sIGenEncode<bits<5> opA, dag oops, dag iops, InstrItinClass itin,
876                     string opc, string asm, list<dag> pattern>
877     : T1sI<oops, iops, itin, opc, asm, pattern>,
878       T1General<opA> {
879   bits<3> Rm;
880   bits<3> Rn;
881   bits<3> Rd;
882   let Inst{8-6} = Rm;
883   let Inst{5-3} = Rn;
884   let Inst{2-0} = Rd;
885 }
886 class T1sIGenEncodeImm<bits<5> opA, dag oops, dag iops, InstrItinClass itin,
887                        string opc, string asm, list<dag> pattern>
888     : T1sI<oops, iops, itin, opc, asm, pattern>,
889       T1General<opA> {
890   bits<3> Rd;
891   bits<3> Rm;
892   let Inst{5-3} = Rm;
893   let Inst{2-0} = Rd;
894 }
895
896 // Helper classes for encoding T1sIt patterns:
897 class T1sItDPEncode<bits<4> opA, dag oops, dag iops, InstrItinClass itin,
898                     string opc, string asm, list<dag> pattern>
899     : T1sIt<oops, iops, itin, opc, asm, pattern>,
900       T1DataProcessing<opA> {
901   bits<3> Rdn;
902   bits<3> Rm;
903   let Inst{5-3} = Rm;
904   let Inst{2-0} = Rdn;
905 }
906 class T1sItGenEncodeImm<bits<5> opA, dag oops, dag iops, InstrItinClass itin,
907                         string opc, string asm, list<dag> pattern>
908     : T1sIt<oops, iops, itin, opc, asm, pattern>,
909       T1General<opA> {
910   bits<3> Rdn;
911   bits<8> imm8;
912   let Inst{10-8} = Rdn;
913   let Inst{7-0}  = imm8;
914 }
915
916 let isAdd = 1 in {
917   // Add with carry register
918   let isCommutable = 1, Uses = [CPSR] in
919   def tADC :                      // A8.6.2
920     T1sItDPEncode<0b0101, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm), IIC_iALUr,
921                   "adc", "\t$Rdn, $Rm",
922                   []>, Sched<[WriteALU]>;
923
924   // Add immediate
925   def tADDi3 :                    // A8.6.4 T1
926     T1sIGenEncodeImm<0b01110, (outs tGPR:$Rd), (ins tGPR:$Rm, imm0_7:$imm3),
927                      IIC_iALUi,
928                      "add", "\t$Rd, $Rm, $imm3",
929                      [(set tGPR:$Rd, (add tGPR:$Rm, imm0_7:$imm3))]>,
930                      Sched<[WriteALU]> {
931     bits<3> imm3;
932     let Inst{8-6} = imm3;
933   }
934
935   def tADDi8 :                    // A8.6.4 T2
936     T1sItGenEncodeImm<{1,1,0,?,?}, (outs tGPR:$Rdn),
937                       (ins tGPR:$Rn, imm0_255:$imm8), IIC_iALUi,
938                       "add", "\t$Rdn, $imm8",
939                       [(set tGPR:$Rdn, (add tGPR:$Rn, imm8_255:$imm8))]>,
940                       Sched<[WriteALU]>;
941
942   // Add register
943   let isCommutable = 1 in
944   def tADDrr :                    // A8.6.6 T1
945     T1sIGenEncode<0b01100, (outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm),
946                   IIC_iALUr,
947                   "add", "\t$Rd, $Rn, $Rm",
948                   [(set tGPR:$Rd, (add tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
949
950   /// Similar to the above except these set the 's' bit so the
951   /// instruction modifies the CPSR register.
952   ///
953   /// These opcodes will be converted to the real non-S opcodes by
954   /// AdjustInstrPostInstrSelection after giving then an optional CPSR operand.
955   let hasPostISelHook = 1, Defs = [CPSR] in {
956     let isCommutable = 1, Uses = [CPSR] in
957     def tADCS : tPseudoInst<(outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
958                             2, IIC_iALUr,
959                             [(set tGPR:$Rdn, CPSR, (ARMadde tGPR:$Rn, tGPR:$Rm,
960                                                             CPSR))]>,
961                 Requires<[IsThumb1Only]>,
962                 Sched<[WriteALU]>;
963
964     def tADDSi3 : tPseudoInst<(outs tGPR:$Rd), (ins tGPR:$Rm, imm0_7:$imm3),
965                               2, IIC_iALUi,
966                               [(set tGPR:$Rd, CPSR, (ARMaddc tGPR:$Rm,
967                                                              imm0_7:$imm3))]>,
968                   Requires<[IsThumb1Only]>,
969                   Sched<[WriteALU]>;
970
971     def tADDSi8 : tPseudoInst<(outs tGPR:$Rdn), (ins tGPR:$Rn, imm0_255:$imm8),
972                               2, IIC_iALUi,
973                               [(set tGPR:$Rdn, CPSR, (ARMaddc tGPR:$Rn,
974                                                       imm8_255:$imm8))]>,
975                   Requires<[IsThumb1Only]>,
976                   Sched<[WriteALU]>;
977
978     let isCommutable = 1 in
979     def tADDSrr : tPseudoInst<(outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm),
980                               2, IIC_iALUr,
981                               [(set tGPR:$Rd, CPSR, (ARMaddc tGPR:$Rn,
982                                                              tGPR:$Rm))]>,
983                   Requires<[IsThumb1Only]>,
984                   Sched<[WriteALU]>;
985   }
986
987   let hasSideEffects = 0 in
988   def tADDhirr : T1pIt<(outs GPR:$Rdn), (ins GPR:$Rn, GPR:$Rm), IIC_iALUr,
989                        "add", "\t$Rdn, $Rm", []>,
990                  T1Special<{0,0,?,?}>, Sched<[WriteALU]> {
991     // A8.6.6 T2
992     bits<4> Rdn;
993     bits<4> Rm;
994     let Inst{7}   = Rdn{3};
995     let Inst{6-3} = Rm;
996     let Inst{2-0} = Rdn{2-0};
997   }
998 }
999
1000 def : tInstAlias <"add${s}${p} $Rdn, $Rm",
1001                  (tADDrr tGPR:$Rdn,s_cc_out:$s, tGPR:$Rdn, tGPR:$Rm, pred:$p)>;
1002
1003 def : tInstSubst<"sub${s}${p} $rd, $rn, $imm",
1004                  (tADDi3 tGPR:$rd, s_cc_out:$s, tGPR:$rn, mod_imm1_7_neg:$imm, pred:$p)>;
1005 def : tInstSubst<"sub${s}${p} $rdn, $imm",
1006                  (tADDi8 tGPR:$rdn, s_cc_out:$s, mod_imm8_255_neg:$imm, pred:$p)>;
1007
1008
1009 // AND register
1010 let isCommutable = 1 in
1011 def tAND :                      // A8.6.12
1012   T1sItDPEncode<0b0000, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1013                 IIC_iBITr,
1014                 "and", "\t$Rdn, $Rm",
1015                 [(set tGPR:$Rdn, (and tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1016
1017 // ASR immediate
1018 def tASRri :                    // A8.6.14
1019   T1sIGenEncodeImm<{0,1,0,?,?}, (outs tGPR:$Rd), (ins tGPR:$Rm, imm_sr:$imm5),
1020                    IIC_iMOVsi,
1021                    "asr", "\t$Rd, $Rm, $imm5",
1022                    [(set tGPR:$Rd, (sra tGPR:$Rm, (i32 imm_sr:$imm5)))]>,
1023                    Sched<[WriteALU]> {
1024   bits<5> imm5;
1025   let Inst{10-6} = imm5;
1026 }
1027
1028 // ASR register
1029 def tASRrr :                    // A8.6.15
1030   T1sItDPEncode<0b0100, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1031                 IIC_iMOVsr,
1032                 "asr", "\t$Rdn, $Rm",
1033                 [(set tGPR:$Rdn, (sra tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1034
1035 // BIC register
1036 def tBIC :                      // A8.6.20
1037   T1sItDPEncode<0b1110, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1038                 IIC_iBITr,
1039                 "bic", "\t$Rdn, $Rm",
1040                 [(set tGPR:$Rdn, (and tGPR:$Rn, (not tGPR:$Rm)))]>,
1041                 Sched<[WriteALU]>;
1042
1043 // CMN register
1044 let isCompare = 1, Defs = [CPSR] in {
1045 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
1046 //       Compare-to-zero still works out, just not the relationals
1047 //def tCMN :                     // A8.6.33
1048 //  T1pIDPEncode<0b1011, (outs), (ins tGPR:$lhs, tGPR:$rhs),
1049 //               IIC_iCMPr,
1050 //               "cmn", "\t$lhs, $rhs",
1051 //               [(ARMcmp tGPR:$lhs, (ineg tGPR:$rhs))]>;
1052
1053 def tCMNz :                     // A8.6.33
1054   T1pIDPEncode<0b1011, (outs), (ins tGPR:$Rn, tGPR:$Rm),
1055                IIC_iCMPr,
1056                "cmn", "\t$Rn, $Rm",
1057                [(ARMcmpZ tGPR:$Rn, (ineg tGPR:$Rm))]>, Sched<[WriteCMP]>;
1058
1059 } // isCompare = 1, Defs = [CPSR]
1060
1061 // CMP immediate
1062 let isCompare = 1, Defs = [CPSR] in {
1063 def tCMPi8 : T1pI<(outs), (ins tGPR:$Rn, imm0_255:$imm8), IIC_iCMPi,
1064                   "cmp", "\t$Rn, $imm8",
1065                   [(ARMcmp tGPR:$Rn, imm0_255:$imm8)]>,
1066              T1General<{1,0,1,?,?}>, Sched<[WriteCMP]> {
1067   // A8.6.35
1068   bits<3> Rn;
1069   bits<8> imm8;
1070   let Inst{10-8} = Rn;
1071   let Inst{7-0}  = imm8;
1072 }
1073
1074 // CMP register
1075 def tCMPr :                     // A8.6.36 T1
1076   T1pIDPEncode<0b1010, (outs), (ins tGPR:$Rn, tGPR:$Rm),
1077                IIC_iCMPr,
1078                "cmp", "\t$Rn, $Rm",
1079                [(ARMcmp tGPR:$Rn, tGPR:$Rm)]>, Sched<[WriteCMP]>;
1080
1081 def tCMPhir : T1pI<(outs), (ins GPR:$Rn, GPR:$Rm), IIC_iCMPr,
1082                    "cmp", "\t$Rn, $Rm", []>,
1083               T1Special<{0,1,?,?}>, Sched<[WriteCMP]> {
1084   // A8.6.36 T2
1085   bits<4> Rm;
1086   bits<4> Rn;
1087   let Inst{7}   = Rn{3};
1088   let Inst{6-3} = Rm;
1089   let Inst{2-0} = Rn{2-0};
1090 }
1091 } // isCompare = 1, Defs = [CPSR]
1092
1093
1094 // XOR register
1095 let isCommutable = 1 in
1096 def tEOR :                      // A8.6.45
1097   T1sItDPEncode<0b0001, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1098                 IIC_iBITr,
1099                 "eor", "\t$Rdn, $Rm",
1100                 [(set tGPR:$Rdn, (xor tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1101
1102 // LSL immediate
1103 def tLSLri :                    // A8.6.88
1104   T1sIGenEncodeImm<{0,0,0,?,?}, (outs tGPR:$Rd), (ins tGPR:$Rm, imm0_31:$imm5),
1105                    IIC_iMOVsi,
1106                    "lsl", "\t$Rd, $Rm, $imm5",
1107                    [(set tGPR:$Rd, (shl tGPR:$Rm, (i32 imm:$imm5)))]>,
1108                    Sched<[WriteALU]> {
1109   bits<5> imm5;
1110   let Inst{10-6} = imm5;
1111 }
1112
1113 // LSL register
1114 def tLSLrr :                    // A8.6.89
1115   T1sItDPEncode<0b0010, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1116                 IIC_iMOVsr,
1117                 "lsl", "\t$Rdn, $Rm",
1118                 [(set tGPR:$Rdn, (shl tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1119
1120 // LSR immediate
1121 def tLSRri :                    // A8.6.90
1122   T1sIGenEncodeImm<{0,0,1,?,?}, (outs tGPR:$Rd), (ins tGPR:$Rm, imm_sr:$imm5),
1123                    IIC_iMOVsi,
1124                    "lsr", "\t$Rd, $Rm, $imm5",
1125                    [(set tGPR:$Rd, (srl tGPR:$Rm, (i32 imm_sr:$imm5)))]>,
1126                    Sched<[WriteALU]> {
1127   bits<5> imm5;
1128   let Inst{10-6} = imm5;
1129 }
1130
1131 // LSR register
1132 def tLSRrr :                    // A8.6.91
1133   T1sItDPEncode<0b0011, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1134                 IIC_iMOVsr,
1135                 "lsr", "\t$Rdn, $Rm",
1136                 [(set tGPR:$Rdn, (srl tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1137
1138 // Move register
1139 let isMoveImm = 1 in
1140 def tMOVi8 : T1sI<(outs tGPR:$Rd), (ins imm0_255:$imm8), IIC_iMOVi,
1141                   "mov", "\t$Rd, $imm8",
1142                   [(set tGPR:$Rd, imm0_255:$imm8)]>,
1143              T1General<{1,0,0,?,?}>, Sched<[WriteALU]> {
1144   // A8.6.96
1145   bits<3> Rd;
1146   bits<8> imm8;
1147   let Inst{10-8} = Rd;
1148   let Inst{7-0}  = imm8;
1149 }
1150 // Because we have an explicit tMOVSr below, we need an alias to handle
1151 // the immediate "movs" form here. Blech.
1152 def : tInstAlias <"movs $Rdn, $imm",
1153                  (tMOVi8 tGPR:$Rdn, CPSR, imm0_255:$imm, 14, 0)>;
1154
1155 // A7-73: MOV(2) - mov setting flag.
1156
1157 let hasSideEffects = 0 in {
1158 def tMOVr : Thumb1pI<(outs GPR:$Rd), (ins GPR:$Rm), AddrModeNone,
1159                       2, IIC_iMOVr,
1160                       "mov", "\t$Rd, $Rm", "", []>,
1161                   T1Special<{1,0,?,?}>, Sched<[WriteALU]> {
1162   // A8.6.97
1163   bits<4> Rd;
1164   bits<4> Rm;
1165   let Inst{7}   = Rd{3};
1166   let Inst{6-3} = Rm;
1167   let Inst{2-0} = Rd{2-0};
1168 }
1169 let Defs = [CPSR] in
1170 def tMOVSr      : T1I<(outs tGPR:$Rd), (ins tGPR:$Rm), IIC_iMOVr,
1171                       "movs\t$Rd, $Rm", []>, Encoding16, Sched<[WriteALU]> {
1172   // A8.6.97
1173   bits<3> Rd;
1174   bits<3> Rm;
1175   let Inst{15-6} = 0b0000000000;
1176   let Inst{5-3}  = Rm;
1177   let Inst{2-0}  = Rd;
1178 }
1179 } // hasSideEffects
1180
1181 // Multiply register
1182 let isCommutable = 1 in
1183 def tMUL :                      // A8.6.105 T1
1184   Thumb1sI<(outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm), AddrModeNone, 2,
1185            IIC_iMUL32, "mul", "\t$Rd, $Rn, $Rm", "$Rm = $Rd",
1186            [(set tGPR:$Rd, (mul tGPR:$Rn, tGPR:$Rm))]>,
1187       T1DataProcessing<0b1101> {
1188   bits<3> Rd;
1189   bits<3> Rn;
1190   let Inst{5-3} = Rn;
1191   let Inst{2-0} = Rd;
1192   let AsmMatchConverter = "cvtThumbMultiply";
1193 }
1194
1195 def :tInstAlias<"mul${s}${p} $Rdm, $Rn", (tMUL tGPR:$Rdm, s_cc_out:$s, tGPR:$Rn,
1196                                                pred:$p)>;
1197
1198 // Move inverse register
1199 def tMVN :                      // A8.6.107
1200   T1sIDPEncode<0b1111, (outs tGPR:$Rd), (ins tGPR:$Rn), IIC_iMVNr,
1201                "mvn", "\t$Rd, $Rn",
1202                [(set tGPR:$Rd, (not tGPR:$Rn))]>, Sched<[WriteALU]>;
1203
1204 // Bitwise or register
1205 let isCommutable = 1 in
1206 def tORR :                      // A8.6.114
1207   T1sItDPEncode<0b1100, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1208                 IIC_iBITr,
1209                 "orr", "\t$Rdn, $Rm",
1210                 [(set tGPR:$Rdn, (or tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1211
1212 // Swaps
1213 def tREV :                      // A8.6.134
1214   T1pIMiscEncode<{1,0,1,0,0,0,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1215                  IIC_iUNAr,
1216                  "rev", "\t$Rd, $Rm",
1217                  [(set tGPR:$Rd, (bswap tGPR:$Rm))]>,
1218                  Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1219
1220 def tREV16 :                    // A8.6.135
1221   T1pIMiscEncode<{1,0,1,0,0,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1222                  IIC_iUNAr,
1223                  "rev16", "\t$Rd, $Rm",
1224              [(set tGPR:$Rd, (rotr (bswap tGPR:$Rm), (i32 16)))]>,
1225                 Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1226
1227 def tREVSH :                    // A8.6.136
1228   T1pIMiscEncode<{1,0,1,0,1,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1229                  IIC_iUNAr,
1230                  "revsh", "\t$Rd, $Rm",
1231                  [(set tGPR:$Rd, (sra (bswap tGPR:$Rm), (i32 16)))]>,
1232                  Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1233
1234 // Rotate right register
1235 def tROR :                      // A8.6.139
1236   T1sItDPEncode<0b0111, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1237                 IIC_iMOVsr,
1238                 "ror", "\t$Rdn, $Rm",
1239                 [(set tGPR:$Rdn, (rotr tGPR:$Rn, tGPR:$Rm))]>,
1240                 Sched<[WriteALU]>;
1241
1242 // Negate register
1243 def tRSB :                      // A8.6.141
1244   T1sIDPEncode<0b1001, (outs tGPR:$Rd), (ins tGPR:$Rn),
1245                IIC_iALUi,
1246                "rsb", "\t$Rd, $Rn, #0",
1247                [(set tGPR:$Rd, (ineg tGPR:$Rn))]>, Sched<[WriteALU]>;
1248
1249 // Subtract with carry register
1250 let Uses = [CPSR] in
1251 def tSBC :                      // A8.6.151
1252   T1sItDPEncode<0b0110, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1253                 IIC_iALUr,
1254                 "sbc", "\t$Rdn, $Rm",
1255                 []>,
1256                 Sched<[WriteALU]>;
1257
1258 // Subtract immediate
1259 def tSUBi3 :                    // A8.6.210 T1
1260   T1sIGenEncodeImm<0b01111, (outs tGPR:$Rd), (ins tGPR:$Rm, imm0_7:$imm3),
1261                    IIC_iALUi,
1262                    "sub", "\t$Rd, $Rm, $imm3",
1263                    [(set tGPR:$Rd, (add tGPR:$Rm, imm0_7_neg:$imm3))]>,
1264                    Sched<[WriteALU]> {
1265   bits<3> imm3;
1266   let Inst{8-6} = imm3;
1267 }
1268
1269 def tSUBi8 :                    // A8.6.210 T2
1270   T1sItGenEncodeImm<{1,1,1,?,?}, (outs tGPR:$Rdn),
1271                     (ins tGPR:$Rn, imm0_255:$imm8), IIC_iALUi,
1272                     "sub", "\t$Rdn, $imm8",
1273                     [(set tGPR:$Rdn, (add tGPR:$Rn, imm8_255_neg:$imm8))]>,
1274                     Sched<[WriteALU]>;
1275
1276 def : tInstSubst<"add${s}${p} $rd, $rn, $imm",
1277                  (tSUBi3 tGPR:$rd, s_cc_out:$s, tGPR:$rn, mod_imm1_7_neg:$imm, pred:$p)>;
1278
1279
1280 def : tInstSubst<"add${s}${p} $rdn, $imm",
1281                  (tSUBi8 tGPR:$rdn, s_cc_out:$s, mod_imm8_255_neg:$imm, pred:$p)>;
1282
1283
1284 // Subtract register
1285 def tSUBrr :                    // A8.6.212
1286   T1sIGenEncode<0b01101, (outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm),
1287                 IIC_iALUr,
1288                 "sub", "\t$Rd, $Rn, $Rm",
1289                 [(set tGPR:$Rd, (sub tGPR:$Rn, tGPR:$Rm))]>,
1290                 Sched<[WriteALU]>;
1291
1292 def : tInstAlias <"sub${s}${p} $Rdn, $Rm",
1293                  (tSUBrr tGPR:$Rdn,s_cc_out:$s, tGPR:$Rdn, tGPR:$Rm, pred:$p)>;
1294
1295 /// Similar to the above except these set the 's' bit so the
1296 /// instruction modifies the CPSR register.
1297 ///
1298 /// These opcodes will be converted to the real non-S opcodes by
1299 /// AdjustInstrPostInstrSelection after giving then an optional CPSR operand.
1300 let hasPostISelHook = 1, Defs = [CPSR] in {
1301   let Uses = [CPSR] in
1302   def tSBCS : tPseudoInst<(outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1303                           2, IIC_iALUr,
1304                           [(set tGPR:$Rdn, CPSR, (ARMsube tGPR:$Rn, tGPR:$Rm,
1305                                                           CPSR))]>,
1306               Requires<[IsThumb1Only]>,
1307               Sched<[WriteALU]>;
1308
1309   def tSUBSi3 : tPseudoInst<(outs tGPR:$Rd), (ins tGPR:$Rm, imm0_7:$imm3),
1310                             2, IIC_iALUi,
1311                             [(set tGPR:$Rd, CPSR, (ARMsubc tGPR:$Rm,
1312                                                            imm0_7:$imm3))]>,
1313                 Requires<[IsThumb1Only]>,
1314                 Sched<[WriteALU]>;
1315
1316   def tSUBSi8 : tPseudoInst<(outs tGPR:$Rdn), (ins tGPR:$Rn, imm0_255:$imm8),
1317                             2, IIC_iALUi,
1318                             [(set tGPR:$Rdn, CPSR, (ARMsubc tGPR:$Rn,
1319                                                             imm8_255:$imm8))]>,
1320                 Requires<[IsThumb1Only]>,
1321                 Sched<[WriteALU]>;
1322
1323   def tSUBSrr : tPseudoInst<(outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm),
1324                             2, IIC_iALUr,
1325                             [(set tGPR:$Rd, CPSR, (ARMsubc tGPR:$Rn,
1326                                                            tGPR:$Rm))]>,
1327                 Requires<[IsThumb1Only]>,
1328                 Sched<[WriteALU]>;
1329 }
1330
1331 // Sign-extend byte
1332 def tSXTB :                     // A8.6.222
1333   T1pIMiscEncode<{0,0,1,0,0,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1334                  IIC_iUNAr,
1335                  "sxtb", "\t$Rd, $Rm",
1336                  [(set tGPR:$Rd, (sext_inreg tGPR:$Rm, i8))]>,
1337                  Requires<[IsThumb, IsThumb1Only, HasV6]>,
1338                  Sched<[WriteALU]>;
1339
1340 // Sign-extend short
1341 def tSXTH :                     // A8.6.224
1342   T1pIMiscEncode<{0,0,1,0,0,0,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1343                  IIC_iUNAr,
1344                  "sxth", "\t$Rd, $Rm",
1345                  [(set tGPR:$Rd, (sext_inreg tGPR:$Rm, i16))]>,
1346                  Requires<[IsThumb, IsThumb1Only, HasV6]>,
1347                  Sched<[WriteALU]>;
1348
1349 // Test
1350 let isCompare = 1, isCommutable = 1, Defs = [CPSR] in
1351 def tTST :                      // A8.6.230
1352   T1pIDPEncode<0b1000, (outs), (ins tGPR:$Rn, tGPR:$Rm), IIC_iTSTr,
1353                "tst", "\t$Rn, $Rm",
1354                [(ARMcmpZ (and_su tGPR:$Rn, tGPR:$Rm), 0)]>,
1355                Sched<[WriteALU]>;
1356
1357 // A8.8.247  UDF - Undefined (Encoding T1)
1358 def tUDF : TI<(outs), (ins imm0_255:$imm8), IIC_Br, "udf\t$imm8",
1359               [(int_arm_undefined imm0_255:$imm8)]>, Encoding16 {
1360   bits<8> imm8;
1361   let Inst{15-12} = 0b1101;
1362   let Inst{11-8} = 0b1110;
1363   let Inst{7-0} = imm8;
1364 }
1365
1366 def t__brkdiv0 : TI<(outs), (ins), IIC_Br, "__brkdiv0",
1367                     [(int_arm_undefined 249)]>, Encoding16,
1368     Requires<[IsThumb, IsWindows]> {
1369   let Inst = 0xdef9;
1370   let isTerminator = 1;
1371 }
1372
1373 // Zero-extend byte
1374 def tUXTB :                     // A8.6.262
1375   T1pIMiscEncode<{0,0,1,0,1,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1376                  IIC_iUNAr,
1377                  "uxtb", "\t$Rd, $Rm",
1378                  [(set tGPR:$Rd, (and tGPR:$Rm, 0xFF))]>,
1379                  Requires<[IsThumb, IsThumb1Only, HasV6]>,
1380                  Sched<[WriteALU]>;
1381
1382 // Zero-extend short
1383 def tUXTH :                     // A8.6.264
1384   T1pIMiscEncode<{0,0,1,0,1,0,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1385                  IIC_iUNAr,
1386                  "uxth", "\t$Rd, $Rm",
1387                  [(set tGPR:$Rd, (and tGPR:$Rm, 0xFFFF))]>,
1388                  Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1389
1390 // Conditional move tMOVCCr - Used to implement the Thumb SELECT_CC operation.
1391 // Expanded after instruction selection into a branch sequence.
1392 let usesCustomInserter = 1 in  // Expanded after instruction selection.
1393   def tMOVCCr_pseudo :
1394   PseudoInst<(outs tGPR:$dst), (ins tGPR:$false, tGPR:$true, cmovpred:$p),
1395              NoItinerary,
1396              [(set tGPR:$dst, (ARMcmov tGPR:$false, tGPR:$true, cmovpred:$p))]>;
1397
1398 // tLEApcrel - Load a pc-relative address into a register without offending the
1399 // assembler.
1400
1401 def tADR : T1I<(outs tGPR:$Rd), (ins t_adrlabel:$addr, pred:$p),
1402                IIC_iALUi, "adr{$p}\t$Rd, $addr", []>,
1403                T1Encoding<{1,0,1,0,0,?}>, Sched<[WriteALU]> {
1404   bits<3> Rd;
1405   bits<8> addr;
1406   let Inst{10-8} = Rd;
1407   let Inst{7-0} = addr;
1408   let DecoderMethod = "DecodeThumbAddSpecialReg";
1409 }
1410
1411 let hasSideEffects = 0, isReMaterializable = 1 in
1412 def tLEApcrel   : tPseudoInst<(outs tGPR:$Rd), (ins i32imm:$label, pred:$p),
1413                               2, IIC_iALUi, []>, Sched<[WriteALU]>;
1414
1415 let hasSideEffects = 1 in
1416 def tLEApcrelJT : tPseudoInst<(outs tGPR:$Rd),
1417                               (ins i32imm:$label, pred:$p),
1418                               2, IIC_iALUi, []>, Sched<[WriteALU]>;
1419
1420 // Thumb-1 doesn't have the TBB or TBH instructions, but we can synthesize them
1421 // and make use of the same compressed jump table format as Thumb-2.
1422 let Size = 2, isBranch = 1, isTerminator = 1, isBarrier = 1,
1423     isIndirectBranch = 1 in {
1424 def tTBB_JT : tPseudoInst<(outs),
1425         (ins tGPRwithpc:$base, tGPR:$index, i32imm:$jt, i32imm:$pclbl), 0,
1426          IIC_Br, []>, Sched<[WriteBr]>;
1427
1428 def tTBH_JT : tPseudoInst<(outs),
1429         (ins tGPRwithpc:$base, tGPR:$index, i32imm:$jt, i32imm:$pclbl), 0,
1430          IIC_Br, []>,  Sched<[WriteBr]>;
1431 }
1432
1433 //===----------------------------------------------------------------------===//
1434 // TLS Instructions
1435 //
1436
1437 // __aeabi_read_tp preserves the registers r1-r3.
1438 // This is a pseudo inst so that we can get the encoding right,
1439 // complete with fixup for the aeabi_read_tp function.
1440 let isCall = 1, Defs = [R0, R12, LR, CPSR], Uses = [SP] in
1441 def tTPsoft : tPseudoInst<(outs), (ins), 4, IIC_Br,
1442                           [(set R0, ARMthread_pointer)]>,
1443                           Sched<[WriteBr]>;
1444
1445 //===----------------------------------------------------------------------===//
1446 // SJLJ Exception handling intrinsics
1447 //
1448
1449 // eh_sjlj_setjmp() is an instruction sequence to store the return address and
1450 // save #0 in R0 for the non-longjmp case.  Since by its nature we may be coming
1451 // from some other function to get here, and we're using the stack frame for the
1452 // containing function to save/restore registers, we can't keep anything live in
1453 // regs across the eh_sjlj_setjmp(), else it will almost certainly have been
1454 // tromped upon when we get here from a longjmp(). We force everything out of
1455 // registers except for our own input by listing the relevant registers in
1456 // Defs. By doing so, we also cause the prologue/epilogue code to actively
1457 // preserve all of the callee-saved resgisters, which is exactly what we want.
1458 // $val is a scratch register for our use.
1459 let Defs = [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7, R12, CPSR ],
1460     hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1,
1461     usesCustomInserter = 1 in
1462 def tInt_eh_sjlj_setjmp : ThumbXI<(outs),(ins tGPR:$src, tGPR:$val),
1463                                   AddrModeNone, 0, NoItinerary, "","",
1464                           [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>;
1465
1466 // FIXME: Non-IOS version(s)
1467 let isBarrier = 1, hasSideEffects = 1, isTerminator = 1, isCodeGenOnly = 1,
1468     Defs = [ R7, LR, SP ] in
1469 def tInt_eh_sjlj_longjmp : XI<(outs), (ins GPR:$src, GPR:$scratch),
1470                               AddrModeNone, 0, IndexModeNone,
1471                               Pseudo, NoItinerary, "", "",
1472                               [(ARMeh_sjlj_longjmp GPR:$src, GPR:$scratch)]>,
1473                              Requires<[IsThumb,IsNotWindows]>;
1474
1475 let isBarrier = 1, hasSideEffects = 1, isTerminator = 1, isCodeGenOnly = 1,
1476     Defs = [ R11, LR, SP ] in
1477 def tInt_WIN_eh_sjlj_longjmp
1478   : XI<(outs), (ins GPR:$src, GPR:$scratch), AddrModeNone, 0, IndexModeNone,
1479        Pseudo, NoItinerary, "", "", [(ARMeh_sjlj_longjmp GPR:$src, GPR:$scratch)]>,
1480     Requires<[IsThumb,IsWindows]>;
1481
1482 //===----------------------------------------------------------------------===//
1483 // Non-Instruction Patterns
1484 //
1485
1486 // Comparisons
1487 def : T1Pat<(ARMcmpZ tGPR:$Rn, imm0_255:$imm8),
1488             (tCMPi8  tGPR:$Rn, imm0_255:$imm8)>;
1489 def : T1Pat<(ARMcmpZ tGPR:$Rn, tGPR:$Rm),
1490             (tCMPr   tGPR:$Rn, tGPR:$Rm)>;
1491
1492 // Bswap 16 with load/store
1493 def : T1Pat<(srl (bswap (extloadi16 t_addrmode_is2:$addr)), (i32 16)),
1494             (tREV16 (tLDRHi t_addrmode_is2:$addr))>;
1495 def : T1Pat<(srl (bswap (extloadi16 t_addrmode_rr:$addr)), (i32 16)),
1496             (tREV16 (tLDRHr t_addrmode_rr:$addr))>;
1497 def : T1Pat<(truncstorei16 (srl (bswap tGPR:$Rn), (i32 16)),
1498                            t_addrmode_is2:$addr),
1499             (tSTRHi(tREV16 tGPR:$Rn), t_addrmode_is2:$addr)>;
1500 def : T1Pat<(truncstorei16 (srl (bswap tGPR:$Rn), (i32 16)),
1501                            t_addrmode_rr:$addr),
1502             (tSTRHr (tREV16 tGPR:$Rn), t_addrmode_rr:$addr)>;
1503
1504 // ConstantPool
1505 def : T1Pat<(ARMWrapper  tconstpool  :$dst), (tLEApcrel tconstpool  :$dst)>;
1506
1507 // GlobalAddress
1508 def tLDRLIT_ga_pcrel : PseudoInst<(outs tGPR:$dst), (ins i32imm:$addr),
1509                                   IIC_iLoadiALU,
1510                                   [(set tGPR:$dst,
1511                                         (ARMWrapperPIC tglobaladdr:$addr))]>,
1512                        Requires<[IsThumb, DontUseMovtInPic]>;
1513
1514 def tLDRLIT_ga_abs : PseudoInst<(outs tGPR:$dst), (ins i32imm:$src),
1515                                 IIC_iLoad_i,
1516                                 [(set tGPR:$dst,
1517                                       (ARMWrapper tglobaladdr:$src))]>,
1518                      Requires<[IsThumb, DontUseMovt]>;
1519
1520 // TLS globals
1521 def : Pat<(ARMWrapperPIC tglobaltlsaddr:$addr),
1522           (tLDRLIT_ga_pcrel tglobaltlsaddr:$addr)>,
1523       Requires<[IsThumb, DontUseMovtInPic]>;
1524 def : Pat<(ARMWrapper tglobaltlsaddr:$addr),
1525           (tLDRLIT_ga_abs tglobaltlsaddr:$addr)>,
1526       Requires<[IsThumb, DontUseMovt]>;
1527
1528
1529 // JumpTable
1530 def : T1Pat<(ARMWrapperJT tjumptable:$dst),
1531             (tLEApcrelJT tjumptable:$dst)>;
1532
1533 // Direct calls
1534 def : T1Pat<(ARMcall texternalsym:$func), (tBL texternalsym:$func)>,
1535       Requires<[IsThumb]>;
1536
1537 // zextload i1 -> zextload i8
1538 def : T1Pat<(zextloadi1 t_addrmode_is1:$addr),
1539             (tLDRBi t_addrmode_is1:$addr)>;
1540 def : T1Pat<(zextloadi1 t_addrmode_rr:$addr),
1541             (tLDRBr t_addrmode_rr:$addr)>;
1542
1543 // extload from the stack -> word load from the stack, as it avoids having to
1544 // materialize the base in a separate register. This only works when a word
1545 // load puts the byte/halfword value in the same place in the register that the
1546 // byte/halfword load would, i.e. when little-endian.
1547 def : T1Pat<(extloadi1  t_addrmode_sp:$addr), (tLDRspi t_addrmode_sp:$addr)>,
1548       Requires<[IsThumb, IsThumb1Only, IsLE]>;
1549 def : T1Pat<(extloadi8  t_addrmode_sp:$addr), (tLDRspi t_addrmode_sp:$addr)>,
1550       Requires<[IsThumb, IsThumb1Only, IsLE]>;
1551 def : T1Pat<(extloadi16 t_addrmode_sp:$addr), (tLDRspi t_addrmode_sp:$addr)>,
1552       Requires<[IsThumb, IsThumb1Only, IsLE]>;
1553
1554 // extload -> zextload
1555 def : T1Pat<(extloadi1  t_addrmode_is1:$addr), (tLDRBi t_addrmode_is1:$addr)>;
1556 def : T1Pat<(extloadi1  t_addrmode_rr:$addr),  (tLDRBr t_addrmode_rr:$addr)>;
1557 def : T1Pat<(extloadi8  t_addrmode_is1:$addr), (tLDRBi t_addrmode_is1:$addr)>;
1558 def : T1Pat<(extloadi8  t_addrmode_rr:$addr),  (tLDRBr t_addrmode_rr:$addr)>;
1559 def : T1Pat<(extloadi16 t_addrmode_is2:$addr), (tLDRHi t_addrmode_is2:$addr)>;
1560 def : T1Pat<(extloadi16 t_addrmode_rr:$addr),  (tLDRHr t_addrmode_rr:$addr)>;
1561
1562 // post-inc loads and stores
1563
1564 // post-inc LDR -> LDM r0!, {r1}. The way operands are layed out in LDMs is
1565 // different to how ISel expects them for a post-inc load, so use a pseudo
1566 // and expand it just after ISel.
1567 let usesCustomInserter = 1, mayLoad =1,
1568     Constraints = "$Rn = $Rn_wb,@earlyclobber $Rn_wb" in
1569  def tLDR_postidx: tPseudoInst<(outs rGPR:$Rt, rGPR:$Rn_wb),
1570                                (ins rGPR:$Rn, pred:$p),
1571                                4, IIC_iStore_ru,
1572                                []>;
1573
1574 // post-inc STR -> STM r0!, {r1}. The layout of this (because it doesn't def
1575 // multiple registers) is the same in ISel as MachineInstr, so there's no need
1576 // for a pseudo.
1577 def : T1Pat<(post_store rGPR:$Rt, rGPR:$Rn, 4),
1578             (tSTMIA_UPD rGPR:$Rn, rGPR:$Rt)>;
1579
1580 // If it's impossible to use [r,r] address mode for sextload, select to
1581 // ldr{b|h} + sxt{b|h} instead.
1582 def : T1Pat<(sextloadi8 t_addrmode_is1:$addr),
1583             (tSXTB (tLDRBi t_addrmode_is1:$addr))>,
1584       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1585 def : T1Pat<(sextloadi8 t_addrmode_rr:$addr),
1586             (tSXTB (tLDRBr t_addrmode_rr:$addr))>,
1587       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1588 def : T1Pat<(sextloadi16 t_addrmode_is2:$addr),
1589             (tSXTH (tLDRHi t_addrmode_is2:$addr))>,
1590       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1591 def : T1Pat<(sextloadi16 t_addrmode_rr:$addr),
1592             (tSXTH (tLDRHr t_addrmode_rr:$addr))>,
1593       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1594
1595 def : T1Pat<(sextloadi8 t_addrmode_is1:$addr),
1596             (tASRri (tLSLri (tLDRBi t_addrmode_is1:$addr), 24), 24)>;
1597 def : T1Pat<(sextloadi8 t_addrmode_rr:$addr),
1598             (tASRri (tLSLri (tLDRBr t_addrmode_rr:$addr), 24), 24)>;
1599 def : T1Pat<(sextloadi16 t_addrmode_is2:$addr),
1600             (tASRri (tLSLri (tLDRHi t_addrmode_is2:$addr), 16), 16)>;
1601 def : T1Pat<(sextloadi16 t_addrmode_rr:$addr),
1602             (tASRri (tLSLri (tLDRHr t_addrmode_rr:$addr), 16), 16)>;
1603
1604 def : T1Pat<(atomic_load_8 t_addrmode_is1:$src),
1605              (tLDRBi t_addrmode_is1:$src)>;
1606 def : T1Pat<(atomic_load_8 t_addrmode_rr:$src),
1607              (tLDRBr t_addrmode_rr:$src)>;
1608 def : T1Pat<(atomic_load_16 t_addrmode_is2:$src),
1609              (tLDRHi t_addrmode_is2:$src)>;
1610 def : T1Pat<(atomic_load_16 t_addrmode_rr:$src),
1611              (tLDRHr t_addrmode_rr:$src)>;
1612 def : T1Pat<(atomic_load_32 t_addrmode_is4:$src),
1613              (tLDRi t_addrmode_is4:$src)>;
1614 def : T1Pat<(atomic_load_32 t_addrmode_rr:$src),
1615              (tLDRr t_addrmode_rr:$src)>;
1616 def : T1Pat<(atomic_store_8 t_addrmode_is1:$ptr, tGPR:$val),
1617              (tSTRBi tGPR:$val, t_addrmode_is1:$ptr)>;
1618 def : T1Pat<(atomic_store_8 t_addrmode_rr:$ptr, tGPR:$val),
1619              (tSTRBr tGPR:$val, t_addrmode_rr:$ptr)>;
1620 def : T1Pat<(atomic_store_16 t_addrmode_is2:$ptr, tGPR:$val),
1621              (tSTRHi tGPR:$val, t_addrmode_is2:$ptr)>;
1622 def : T1Pat<(atomic_store_16 t_addrmode_rr:$ptr, tGPR:$val),
1623              (tSTRHr tGPR:$val, t_addrmode_rr:$ptr)>;
1624 def : T1Pat<(atomic_store_32 t_addrmode_is4:$ptr, tGPR:$val),
1625              (tSTRi tGPR:$val, t_addrmode_is4:$ptr)>;
1626 def : T1Pat<(atomic_store_32 t_addrmode_rr:$ptr, tGPR:$val),
1627              (tSTRr tGPR:$val, t_addrmode_rr:$ptr)>;
1628
1629 // Large immediate handling.
1630
1631 // Two piece imms.
1632 def : T1Pat<(i32 thumb_immshifted:$src),
1633             (tLSLri (tMOVi8 (thumb_immshifted_val imm:$src)),
1634                     (thumb_immshifted_shamt imm:$src))>;
1635
1636 def : T1Pat<(i32 imm0_255_comp:$src),
1637             (tMVN (tMOVi8 (imm_not_XFORM imm:$src)))>;
1638
1639 def : T1Pat<(i32 imm256_510:$src),
1640             (tADDi8 (tMOVi8 255),
1641                     (thumb_imm256_510_addend imm:$src))>;
1642
1643 // Pseudo instruction that combines ldr from constpool and add pc. This should
1644 // be expanded into two instructions late to allow if-conversion and
1645 // scheduling.
1646 let isReMaterializable = 1 in
1647 def tLDRpci_pic : PseudoInst<(outs GPR:$dst), (ins i32imm:$addr, pclabel:$cp),
1648                              NoItinerary,
1649                [(set GPR:$dst, (ARMpic_add (load (ARMWrapper tconstpool:$addr)),
1650                                            imm:$cp))]>,
1651                Requires<[IsThumb, IsThumb1Only]>;
1652
1653 // Pseudo-instruction for merged POP and return.
1654 // FIXME: remove when we have a way to marking a MI with these properties.
1655 let isReturn = 1, isTerminator = 1, isBarrier = 1, mayLoad = 1,
1656     hasExtraDefRegAllocReq = 1 in
1657 def tPOP_RET : tPseudoExpand<(outs), (ins pred:$p, reglist:$regs, variable_ops),
1658                            2, IIC_iPop_Br, [],
1659                            (tPOP pred:$p, reglist:$regs)>, Sched<[WriteBrL]>;
1660
1661 // Indirect branch using "mov pc, $Rm"
1662 let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
1663   def tBRIND : tPseudoExpand<(outs), (ins GPR:$Rm, pred:$p),
1664                   2, IIC_Br, [(brind GPR:$Rm)],
1665                   (tMOVr PC, GPR:$Rm, pred:$p)>, Sched<[WriteBr]>;
1666 }
1667
1668
1669 // In Thumb1, "nop" is encoded as a "mov r8, r8". Technically, the bf00
1670 // encoding is available on ARMv6K, but we don't differentiate that finely.
1671 def : InstAlias<"nop", (tMOVr R8, R8, 14, 0), 0>, Requires<[IsThumb, IsThumb1Only]>;
1672
1673
1674 // "neg" is and alias for "rsb rd, rn, #0"
1675 def : tInstAlias<"neg${s}${p} $Rd, $Rm",
1676                  (tRSB tGPR:$Rd, s_cc_out:$s, tGPR:$Rm, pred:$p)>;
1677
1678
1679 // Implied destination operand forms for shifts.
1680 def : tInstAlias<"lsl${s}${p} $Rdm, $imm",
1681              (tLSLri tGPR:$Rdm, cc_out:$s, tGPR:$Rdm, imm0_31:$imm, pred:$p)>;
1682 def : tInstAlias<"lsr${s}${p} $Rdm, $imm",
1683              (tLSRri tGPR:$Rdm, cc_out:$s, tGPR:$Rdm, imm_sr:$imm, pred:$p)>;
1684 def : tInstAlias<"asr${s}${p} $Rdm, $imm",
1685              (tASRri tGPR:$Rdm, cc_out:$s, tGPR:$Rdm, imm_sr:$imm, pred:$p)>;
1686
1687 // Pseudo instruction ldr Rt, =immediate
1688 def tLDRConstPool
1689   : tAsmPseudo<"ldr${p} $Rt, $immediate",
1690                (ins tGPR:$Rt, const_pool_asm_imm:$immediate, pred:$p)>;