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