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