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