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