ARM: recommit r237590: allow jump tables to be placed as constant islands.
[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),
526                       0, IIC_Br,
527                       [(ARMbrjt tGPR:$target, tjumptable:$jt)]>,
528                       Sched<[WriteBrTbl]> {
529     let Size = 2;
530     list<Predicate> Predicates = [IsThumb, IsThumb1Only];
531   }
532 }
533
534 // FIXME: should be able to write a pattern for ARMBrcond, but can't use
535 // a two-value operand where a dag node expects two operands. :(
536 let isBranch = 1, isTerminator = 1 in
537   def tBcc : T1I<(outs), (ins t_bcctarget:$target, pred:$p), IIC_Br,
538                  "b${p}\t$target",
539                  [/*(ARMbrcond bb:$target, imm:$cc)*/]>,
540              T1BranchCond<{1,1,0,1}>, Sched<[WriteBr]> {
541   bits<4> p;
542   bits<8> target;
543   let Inst{11-8} = p;
544   let Inst{7-0} = target;
545   let AsmMatchConverter = "cvtThumbBranches";
546 }
547
548
549 // Tail calls
550 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1 in {
551   // IOS versions.
552   let Uses = [SP] in {
553     def tTAILJMPr : tPseudoExpand<(outs), (ins tcGPR:$dst),
554                      4, IIC_Br, [],
555                      (tBX GPR:$dst, (ops 14, zero_reg))>,
556                      Requires<[IsThumb]>, Sched<[WriteBr]>;
557   }
558   // tTAILJMPd: MachO version uses a Thumb2 branch (no Thumb1 tail calls
559   // on MachO), so it's in ARMInstrThumb2.td.
560   // Non-MachO version:
561   let Uses = [SP] in {
562     def tTAILJMPdND : tPseudoExpand<(outs),
563                    (ins t_brtarget:$dst, pred:$p),
564                    4, IIC_Br, [],
565                    (tB t_brtarget:$dst, pred:$p)>,
566                  Requires<[IsThumb, IsNotMachO]>, Sched<[WriteBr]>;
567   }
568 }
569
570
571 // A8.6.218 Supervisor Call (Software Interrupt)
572 // A8.6.16 B: Encoding T1
573 // If Inst{11-8} == 0b1111 then SEE SVC
574 let isCall = 1, Uses = [SP] in
575 def tSVC : T1pI<(outs), (ins imm0_255:$imm), IIC_Br,
576                 "svc", "\t$imm", []>, Encoding16, Sched<[WriteBr]> {
577   bits<8> imm;
578   let Inst{15-12} = 0b1101;
579   let Inst{11-8}  = 0b1111;
580   let Inst{7-0}   = imm;
581 }
582
583 // The assembler uses 0xDEFE for a trap instruction.
584 let isBarrier = 1, isTerminator = 1 in
585 def tTRAP : TI<(outs), (ins), IIC_Br,
586                "trap", [(trap)]>, Encoding16, Sched<[WriteBr]> {
587   let Inst = 0xdefe;
588 }
589
590 //===----------------------------------------------------------------------===//
591 //  Load Store Instructions.
592 //
593
594 // Loads: reg/reg and reg/imm5
595 let canFoldAsLoad = 1, isReMaterializable = 1 in
596 multiclass thumb_ld_rr_ri_enc<bits<3> reg_opc, bits<4> imm_opc,
597                               Operand AddrMode_r, Operand AddrMode_i,
598                               AddrMode am, InstrItinClass itin_r,
599                               InstrItinClass itin_i, string asm,
600                               PatFrag opnode> {
601   def r : // reg/reg
602     T1pILdStEncode<reg_opc,
603                    (outs tGPR:$Rt), (ins AddrMode_r:$addr),
604                    am, itin_r, asm, "\t$Rt, $addr",
605                    [(set tGPR:$Rt, (opnode AddrMode_r:$addr))]>;
606   def i : // reg/imm5
607     T1pILdStEncodeImm<imm_opc, 1 /* Load */,
608                       (outs tGPR:$Rt), (ins AddrMode_i:$addr),
609                       am, itin_i, asm, "\t$Rt, $addr",
610                       [(set tGPR:$Rt, (opnode AddrMode_i:$addr))]>;
611 }
612 // Stores: reg/reg and reg/imm5
613 multiclass thumb_st_rr_ri_enc<bits<3> reg_opc, bits<4> imm_opc,
614                               Operand AddrMode_r, Operand AddrMode_i,
615                               AddrMode am, InstrItinClass itin_r,
616                               InstrItinClass itin_i, string asm,
617                               PatFrag opnode> {
618   def r : // reg/reg
619     T1pILdStEncode<reg_opc,
620                    (outs), (ins tGPR:$Rt, AddrMode_r:$addr),
621                    am, itin_r, asm, "\t$Rt, $addr",
622                    [(opnode tGPR:$Rt, AddrMode_r:$addr)]>;
623   def i : // reg/imm5
624     T1pILdStEncodeImm<imm_opc, 0 /* Store */,
625                       (outs), (ins tGPR:$Rt, AddrMode_i:$addr),
626                       am, itin_i, asm, "\t$Rt, $addr",
627                       [(opnode tGPR:$Rt, AddrMode_i:$addr)]>;
628 }
629
630 // A8.6.57 & A8.6.60
631 defm tLDR  : thumb_ld_rr_ri_enc<0b100, 0b0110, t_addrmode_rrs4,
632                                 t_addrmode_is4, AddrModeT1_4,
633                                 IIC_iLoad_r, IIC_iLoad_i, "ldr",
634                                 UnOpFrag<(load node:$Src)>>;
635
636 // A8.6.64 & A8.6.61
637 defm tLDRB : thumb_ld_rr_ri_enc<0b110, 0b0111, t_addrmode_rrs1,
638                                 t_addrmode_is1, AddrModeT1_1,
639                                 IIC_iLoad_bh_r, IIC_iLoad_bh_i, "ldrb",
640                                 UnOpFrag<(zextloadi8 node:$Src)>>;
641
642 // A8.6.76 & A8.6.73
643 defm tLDRH : thumb_ld_rr_ri_enc<0b101, 0b1000, t_addrmode_rrs2,
644                                 t_addrmode_is2, AddrModeT1_2,
645                                 IIC_iLoad_bh_r, IIC_iLoad_bh_i, "ldrh",
646                                 UnOpFrag<(zextloadi16 node:$Src)>>;
647
648 let AddedComplexity = 10 in
649 def tLDRSB :                    // A8.6.80
650   T1pILdStEncode<0b011, (outs tGPR:$Rt), (ins t_addrmode_rr:$addr),
651                  AddrModeT1_1, IIC_iLoad_bh_r,
652                  "ldrsb", "\t$Rt, $addr",
653                  [(set tGPR:$Rt, (sextloadi8 t_addrmode_rr:$addr))]>;
654
655 let AddedComplexity = 10 in
656 def tLDRSH :                    // A8.6.84
657   T1pILdStEncode<0b111, (outs tGPR:$Rt), (ins t_addrmode_rr:$addr),
658                  AddrModeT1_2, IIC_iLoad_bh_r,
659                  "ldrsh", "\t$Rt, $addr",
660                  [(set tGPR:$Rt, (sextloadi16 t_addrmode_rr:$addr))]>;
661
662 let canFoldAsLoad = 1 in
663 def tLDRspi : T1pIs<(outs tGPR:$Rt), (ins t_addrmode_sp:$addr), IIC_iLoad_i,
664                     "ldr", "\t$Rt, $addr",
665                     [(set tGPR:$Rt, (load t_addrmode_sp:$addr))]>,
666               T1LdStSP<{1,?,?}> {
667   bits<3> Rt;
668   bits<8> addr;
669   let Inst{10-8} = Rt;
670   let Inst{7-0} = addr;
671 }
672
673 let canFoldAsLoad = 1, isReMaterializable = 1 in
674 def tLDRpci : T1pIs<(outs tGPR:$Rt), (ins t_addrmode_pc:$addr), IIC_iLoad_i,
675                   "ldr", "\t$Rt, $addr",
676                   [(set tGPR:$Rt, (load (ARMWrapper tconstpool:$addr)))]>,
677               T1Encoding<{0,1,0,0,1,?}> {
678   // A6.2 & A8.6.59
679   bits<3> Rt;
680   bits<8> addr;
681   let Inst{10-8} = Rt;
682   let Inst{7-0}  = addr;
683 }
684
685 // A8.6.194 & A8.6.192
686 defm tSTR  : thumb_st_rr_ri_enc<0b000, 0b0110, t_addrmode_rrs4,
687                                 t_addrmode_is4, AddrModeT1_4,
688                                 IIC_iStore_r, IIC_iStore_i, "str",
689                                 BinOpFrag<(store node:$LHS, node:$RHS)>>;
690
691 // A8.6.197 & A8.6.195
692 defm tSTRB : thumb_st_rr_ri_enc<0b010, 0b0111, t_addrmode_rrs1,
693                                 t_addrmode_is1, AddrModeT1_1,
694                                 IIC_iStore_bh_r, IIC_iStore_bh_i, "strb",
695                                 BinOpFrag<(truncstorei8 node:$LHS, node:$RHS)>>;
696
697 // A8.6.207 & A8.6.205
698 defm tSTRH : thumb_st_rr_ri_enc<0b001, 0b1000, t_addrmode_rrs2,
699                                t_addrmode_is2, AddrModeT1_2,
700                                IIC_iStore_bh_r, IIC_iStore_bh_i, "strh",
701                                BinOpFrag<(truncstorei16 node:$LHS, node:$RHS)>>;
702
703
704 def tSTRspi : T1pIs<(outs), (ins tGPR:$Rt, t_addrmode_sp:$addr), IIC_iStore_i,
705                     "str", "\t$Rt, $addr",
706                     [(store tGPR:$Rt, t_addrmode_sp:$addr)]>,
707               T1LdStSP<{0,?,?}> {
708   bits<3> Rt;
709   bits<8> addr;
710   let Inst{10-8} = Rt;
711   let Inst{7-0} = addr;
712 }
713
714 //===----------------------------------------------------------------------===//
715 //  Load / store multiple Instructions.
716 //
717
718 // These require base address to be written back or one of the loaded regs.
719 let hasSideEffects = 0 in {
720
721 let mayLoad = 1, hasExtraDefRegAllocReq = 1 in
722 def tLDMIA : T1I<(outs), (ins tGPR:$Rn, pred:$p, reglist:$regs, variable_ops),
723         IIC_iLoad_m, "ldm${p}\t$Rn, $regs", []>, T1Encoding<{1,1,0,0,1,?}> {
724   bits<3> Rn;
725   bits<8> regs;
726   let Inst{10-8} = Rn;
727   let Inst{7-0}  = regs;
728 }
729
730 // Writeback version is just a pseudo, as there's no encoding difference.
731 // Writeback happens iff the base register is not in the destination register
732 // list.
733 def tLDMIA_UPD :
734     InstTemplate<AddrModeNone, 0, IndexModeNone, Pseudo, GenericDomain,
735                  "$Rn = $wb", IIC_iLoad_mu>,
736     PseudoInstExpansion<(tLDMIA tGPR:$Rn, pred:$p, reglist:$regs)> {
737   let Size = 2;
738   let OutOperandList = (outs GPR:$wb);
739   let InOperandList = (ins GPR:$Rn, pred:$p, reglist:$regs, variable_ops);
740   let Pattern = [];
741   let isCodeGenOnly = 1;
742   let isPseudo = 1;
743   list<Predicate> Predicates = [IsThumb];
744 }
745
746 // There is no non-writeback version of STM for Thumb.
747 let mayStore = 1, hasExtraSrcRegAllocReq = 1 in
748 def tSTMIA_UPD : Thumb1I<(outs GPR:$wb),
749                          (ins tGPR:$Rn, pred:$p, reglist:$regs, variable_ops),
750                          AddrModeNone, 2, IIC_iStore_mu,
751                          "stm${p}\t$Rn!, $regs", "$Rn = $wb", []>,
752                      T1Encoding<{1,1,0,0,0,?}> {
753   bits<3> Rn;
754   bits<8> regs;
755   let Inst{10-8} = Rn;
756   let Inst{7-0}  = regs;
757 }
758
759 } // hasSideEffects
760
761 def : InstAlias<"ldm${p} $Rn!, $regs",
762                 (tLDMIA tGPR:$Rn, pred:$p, reglist:$regs)>,
763         Requires<[IsThumb, IsThumb1Only]>;
764
765 let mayLoad = 1, Uses = [SP], Defs = [SP], hasExtraDefRegAllocReq = 1 in
766 def tPOP : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
767                IIC_iPop,
768                "pop${p}\t$regs", []>,
769            T1Misc<{1,1,0,?,?,?,?}> {
770   bits<16> regs;
771   let Inst{8}   = regs{15};
772   let Inst{7-0} = regs{7-0};
773 }
774
775 let mayStore = 1, Uses = [SP], Defs = [SP], hasExtraSrcRegAllocReq = 1 in
776 def tPUSH : T1I<(outs), (ins pred:$p, reglist:$regs, variable_ops),
777                 IIC_iStore_m,
778                 "push${p}\t$regs", []>,
779             T1Misc<{0,1,0,?,?,?,?}> {
780   bits<16> regs;
781   let Inst{8}   = regs{14};
782   let Inst{7-0} = regs{7-0};
783 }
784
785 //===----------------------------------------------------------------------===//
786 //  Arithmetic Instructions.
787 //
788
789 // Helper classes for encoding T1pI patterns:
790 class T1pIDPEncode<bits<4> opA, dag oops, dag iops, InstrItinClass itin,
791                    string opc, string asm, list<dag> pattern>
792     : T1pI<oops, iops, itin, opc, asm, pattern>,
793       T1DataProcessing<opA> {
794   bits<3> Rm;
795   bits<3> Rn;
796   let Inst{5-3} = Rm;
797   let Inst{2-0} = Rn;
798 }
799 class T1pIMiscEncode<bits<7> opA, dag oops, dag iops, InstrItinClass itin,
800                      string opc, string asm, list<dag> pattern>
801     : T1pI<oops, iops, itin, opc, asm, pattern>,
802       T1Misc<opA> {
803   bits<3> Rm;
804   bits<3> Rd;
805   let Inst{5-3} = Rm;
806   let Inst{2-0} = Rd;
807 }
808
809 // Helper classes for encoding T1sI patterns:
810 class T1sIDPEncode<bits<4> opA, dag oops, dag iops, InstrItinClass itin,
811                    string opc, string asm, list<dag> pattern>
812     : T1sI<oops, iops, itin, opc, asm, pattern>,
813       T1DataProcessing<opA> {
814   bits<3> Rd;
815   bits<3> Rn;
816   let Inst{5-3} = Rn;
817   let Inst{2-0} = Rd;
818 }
819 class T1sIGenEncode<bits<5> opA, dag oops, dag iops, InstrItinClass itin,
820                     string opc, string asm, list<dag> pattern>
821     : T1sI<oops, iops, itin, opc, asm, pattern>,
822       T1General<opA> {
823   bits<3> Rm;
824   bits<3> Rn;
825   bits<3> Rd;
826   let Inst{8-6} = Rm;
827   let Inst{5-3} = Rn;
828   let Inst{2-0} = Rd;
829 }
830 class T1sIGenEncodeImm<bits<5> opA, dag oops, dag iops, InstrItinClass itin,
831                        string opc, string asm, list<dag> pattern>
832     : T1sI<oops, iops, itin, opc, asm, pattern>,
833       T1General<opA> {
834   bits<3> Rd;
835   bits<3> Rm;
836   let Inst{5-3} = Rm;
837   let Inst{2-0} = Rd;
838 }
839
840 // Helper classes for encoding T1sIt patterns:
841 class T1sItDPEncode<bits<4> opA, dag oops, dag iops, InstrItinClass itin,
842                     string opc, string asm, list<dag> pattern>
843     : T1sIt<oops, iops, itin, opc, asm, pattern>,
844       T1DataProcessing<opA> {
845   bits<3> Rdn;
846   bits<3> Rm;
847   let Inst{5-3} = Rm;
848   let Inst{2-0} = Rdn;
849 }
850 class T1sItGenEncodeImm<bits<5> opA, dag oops, dag iops, InstrItinClass itin,
851                         string opc, string asm, list<dag> pattern>
852     : T1sIt<oops, iops, itin, opc, asm, pattern>,
853       T1General<opA> {
854   bits<3> Rdn;
855   bits<8> imm8;
856   let Inst{10-8} = Rdn;
857   let Inst{7-0}  = imm8;
858 }
859
860 // Add with carry register
861 let isCommutable = 1, Uses = [CPSR] in
862 def tADC :                      // A8.6.2
863   T1sItDPEncode<0b0101, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm), IIC_iALUr,
864                 "adc", "\t$Rdn, $Rm",
865                 [(set tGPR:$Rdn, (adde tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
866
867 // Add immediate
868 def tADDi3 :                    // A8.6.4 T1
869   T1sIGenEncodeImm<0b01110, (outs tGPR:$Rd), (ins tGPR:$Rm, imm0_7:$imm3),
870                    IIC_iALUi,
871                    "add", "\t$Rd, $Rm, $imm3",
872                    [(set tGPR:$Rd, (add tGPR:$Rm, imm0_7:$imm3))]>,
873                    Sched<[WriteALU]> {
874   bits<3> imm3;
875   let Inst{8-6} = imm3;
876 }
877
878 def tADDi8 :                    // A8.6.4 T2
879   T1sItGenEncodeImm<{1,1,0,?,?}, (outs tGPR:$Rdn),
880                     (ins tGPR:$Rn, imm0_255:$imm8), IIC_iALUi,
881                     "add", "\t$Rdn, $imm8",
882                     [(set tGPR:$Rdn, (add tGPR:$Rn, imm8_255:$imm8))]>,
883                     Sched<[WriteALU]>;
884
885 // Add register
886 let isCommutable = 1 in
887 def tADDrr :                    // A8.6.6 T1
888   T1sIGenEncode<0b01100, (outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm),
889                 IIC_iALUr,
890                 "add", "\t$Rd, $Rn, $Rm",
891                 [(set tGPR:$Rd, (add tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
892
893 let hasSideEffects = 0 in
894 def tADDhirr : T1pIt<(outs GPR:$Rdn), (ins GPR:$Rn, GPR:$Rm), IIC_iALUr,
895                      "add", "\t$Rdn, $Rm", []>,
896                T1Special<{0,0,?,?}>, Sched<[WriteALU]> {
897   // A8.6.6 T2
898   bits<4> Rdn;
899   bits<4> Rm;
900   let Inst{7}   = Rdn{3};
901   let Inst{6-3} = Rm;
902   let Inst{2-0} = Rdn{2-0};
903 }
904
905 // AND register
906 let isCommutable = 1 in
907 def tAND :                      // A8.6.12
908   T1sItDPEncode<0b0000, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
909                 IIC_iBITr,
910                 "and", "\t$Rdn, $Rm",
911                 [(set tGPR:$Rdn, (and tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
912
913 // ASR immediate
914 def tASRri :                    // A8.6.14
915   T1sIGenEncodeImm<{0,1,0,?,?}, (outs tGPR:$Rd), (ins tGPR:$Rm, imm_sr:$imm5),
916                    IIC_iMOVsi,
917                    "asr", "\t$Rd, $Rm, $imm5",
918                    [(set tGPR:$Rd, (sra tGPR:$Rm, (i32 imm_sr:$imm5)))]>,
919                    Sched<[WriteALU]> {
920   bits<5> imm5;
921   let Inst{10-6} = imm5;
922 }
923
924 // ASR register
925 def tASRrr :                    // A8.6.15
926   T1sItDPEncode<0b0100, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
927                 IIC_iMOVsr,
928                 "asr", "\t$Rdn, $Rm",
929                 [(set tGPR:$Rdn, (sra tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
930
931 // BIC register
932 def tBIC :                      // A8.6.20
933   T1sItDPEncode<0b1110, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
934                 IIC_iBITr,
935                 "bic", "\t$Rdn, $Rm",
936                 [(set tGPR:$Rdn, (and tGPR:$Rn, (not tGPR:$Rm)))]>,
937                 Sched<[WriteALU]>;
938
939 // CMN register
940 let isCompare = 1, Defs = [CPSR] in {
941 //FIXME: Disable CMN, as CCodes are backwards from compare expectations
942 //       Compare-to-zero still works out, just not the relationals
943 //def tCMN :                     // A8.6.33
944 //  T1pIDPEncode<0b1011, (outs), (ins tGPR:$lhs, tGPR:$rhs),
945 //               IIC_iCMPr,
946 //               "cmn", "\t$lhs, $rhs",
947 //               [(ARMcmp tGPR:$lhs, (ineg tGPR:$rhs))]>;
948
949 def tCMNz :                     // A8.6.33
950   T1pIDPEncode<0b1011, (outs), (ins tGPR:$Rn, tGPR:$Rm),
951                IIC_iCMPr,
952                "cmn", "\t$Rn, $Rm",
953                [(ARMcmpZ tGPR:$Rn, (ineg tGPR:$Rm))]>, Sched<[WriteCMP]>;
954
955 } // isCompare = 1, Defs = [CPSR]
956
957 // CMP immediate
958 let isCompare = 1, Defs = [CPSR] in {
959 def tCMPi8 : T1pI<(outs), (ins tGPR:$Rn, imm0_255:$imm8), IIC_iCMPi,
960                   "cmp", "\t$Rn, $imm8",
961                   [(ARMcmp tGPR:$Rn, imm0_255:$imm8)]>,
962              T1General<{1,0,1,?,?}>, Sched<[WriteCMP]> {
963   // A8.6.35
964   bits<3> Rn;
965   bits<8> imm8;
966   let Inst{10-8} = Rn;
967   let Inst{7-0}  = imm8;
968 }
969
970 // CMP register
971 def tCMPr :                     // A8.6.36 T1
972   T1pIDPEncode<0b1010, (outs), (ins tGPR:$Rn, tGPR:$Rm),
973                IIC_iCMPr,
974                "cmp", "\t$Rn, $Rm",
975                [(ARMcmp tGPR:$Rn, tGPR:$Rm)]>, Sched<[WriteCMP]>;
976
977 def tCMPhir : T1pI<(outs), (ins GPR:$Rn, GPR:$Rm), IIC_iCMPr,
978                    "cmp", "\t$Rn, $Rm", []>,
979               T1Special<{0,1,?,?}>, Sched<[WriteCMP]> {
980   // A8.6.36 T2
981   bits<4> Rm;
982   bits<4> Rn;
983   let Inst{7}   = Rn{3};
984   let Inst{6-3} = Rm;
985   let Inst{2-0} = Rn{2-0};
986 }
987 } // isCompare = 1, Defs = [CPSR]
988
989
990 // XOR register
991 let isCommutable = 1 in
992 def tEOR :                      // A8.6.45
993   T1sItDPEncode<0b0001, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
994                 IIC_iBITr,
995                 "eor", "\t$Rdn, $Rm",
996                 [(set tGPR:$Rdn, (xor tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
997
998 // LSL immediate
999 def tLSLri :                    // A8.6.88
1000   T1sIGenEncodeImm<{0,0,0,?,?}, (outs tGPR:$Rd), (ins tGPR:$Rm, imm0_31:$imm5),
1001                    IIC_iMOVsi,
1002                    "lsl", "\t$Rd, $Rm, $imm5",
1003                    [(set tGPR:$Rd, (shl tGPR:$Rm, (i32 imm:$imm5)))]>,
1004                    Sched<[WriteALU]> {
1005   bits<5> imm5;
1006   let Inst{10-6} = imm5;
1007 }
1008
1009 // LSL register
1010 def tLSLrr :                    // A8.6.89
1011   T1sItDPEncode<0b0010, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1012                 IIC_iMOVsr,
1013                 "lsl", "\t$Rdn, $Rm",
1014                 [(set tGPR:$Rdn, (shl tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1015
1016 // LSR immediate
1017 def tLSRri :                    // A8.6.90
1018   T1sIGenEncodeImm<{0,0,1,?,?}, (outs tGPR:$Rd), (ins tGPR:$Rm, imm_sr:$imm5),
1019                    IIC_iMOVsi,
1020                    "lsr", "\t$Rd, $Rm, $imm5",
1021                    [(set tGPR:$Rd, (srl tGPR:$Rm, (i32 imm_sr:$imm5)))]>,
1022                    Sched<[WriteALU]> {
1023   bits<5> imm5;
1024   let Inst{10-6} = imm5;
1025 }
1026
1027 // LSR register
1028 def tLSRrr :                    // A8.6.91
1029   T1sItDPEncode<0b0011, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1030                 IIC_iMOVsr,
1031                 "lsr", "\t$Rdn, $Rm",
1032                 [(set tGPR:$Rdn, (srl tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1033
1034 // Move register
1035 let isMoveImm = 1 in
1036 def tMOVi8 : T1sI<(outs tGPR:$Rd), (ins imm0_255:$imm8), IIC_iMOVi,
1037                   "mov", "\t$Rd, $imm8",
1038                   [(set tGPR:$Rd, imm0_255:$imm8)]>,
1039              T1General<{1,0,0,?,?}>, Sched<[WriteALU]> {
1040   // A8.6.96
1041   bits<3> Rd;
1042   bits<8> imm8;
1043   let Inst{10-8} = Rd;
1044   let Inst{7-0}  = imm8;
1045 }
1046 // Because we have an explicit tMOVSr below, we need an alias to handle
1047 // the immediate "movs" form here. Blech.
1048 def : tInstAlias <"movs $Rdn, $imm",
1049                  (tMOVi8 tGPR:$Rdn, CPSR, imm0_255:$imm, 14, 0)>;
1050
1051 // A7-73: MOV(2) - mov setting flag.
1052
1053 let hasSideEffects = 0 in {
1054 def tMOVr : Thumb1pI<(outs GPR:$Rd), (ins GPR:$Rm), AddrModeNone,
1055                       2, IIC_iMOVr,
1056                       "mov", "\t$Rd, $Rm", "", []>,
1057                   T1Special<{1,0,?,?}>, Sched<[WriteALU]> {
1058   // A8.6.97
1059   bits<4> Rd;
1060   bits<4> Rm;
1061   let Inst{7}   = Rd{3};
1062   let Inst{6-3} = Rm;
1063   let Inst{2-0} = Rd{2-0};
1064 }
1065 let Defs = [CPSR] in
1066 def tMOVSr      : T1I<(outs tGPR:$Rd), (ins tGPR:$Rm), IIC_iMOVr,
1067                       "movs\t$Rd, $Rm", []>, Encoding16, Sched<[WriteALU]> {
1068   // A8.6.97
1069   bits<3> Rd;
1070   bits<3> Rm;
1071   let Inst{15-6} = 0b0000000000;
1072   let Inst{5-3}  = Rm;
1073   let Inst{2-0}  = Rd;
1074 }
1075 } // hasSideEffects
1076
1077 // Multiply register
1078 let isCommutable = 1 in
1079 def tMUL :                      // A8.6.105 T1
1080   Thumb1sI<(outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm), AddrModeNone, 2,
1081            IIC_iMUL32, "mul", "\t$Rd, $Rn, $Rm", "$Rm = $Rd",
1082            [(set tGPR:$Rd, (mul tGPR:$Rn, tGPR:$Rm))]>,
1083       T1DataProcessing<0b1101> {
1084   bits<3> Rd;
1085   bits<3> Rn;
1086   let Inst{5-3} = Rn;
1087   let Inst{2-0} = Rd;
1088   let AsmMatchConverter = "cvtThumbMultiply";
1089 }
1090
1091 def :tInstAlias<"mul${s}${p} $Rdm, $Rn", (tMUL tGPR:$Rdm, s_cc_out:$s, tGPR:$Rn,
1092                                                pred:$p)>;
1093
1094 // Move inverse register
1095 def tMVN :                      // A8.6.107
1096   T1sIDPEncode<0b1111, (outs tGPR:$Rd), (ins tGPR:$Rn), IIC_iMVNr,
1097                "mvn", "\t$Rd, $Rn",
1098                [(set tGPR:$Rd, (not tGPR:$Rn))]>, Sched<[WriteALU]>;
1099
1100 // Bitwise or register
1101 let isCommutable = 1 in
1102 def tORR :                      // A8.6.114
1103   T1sItDPEncode<0b1100, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1104                 IIC_iBITr,
1105                 "orr", "\t$Rdn, $Rm",
1106                 [(set tGPR:$Rdn, (or tGPR:$Rn, tGPR:$Rm))]>, Sched<[WriteALU]>;
1107
1108 // Swaps
1109 def tREV :                      // A8.6.134
1110   T1pIMiscEncode<{1,0,1,0,0,0,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1111                  IIC_iUNAr,
1112                  "rev", "\t$Rd, $Rm",
1113                  [(set tGPR:$Rd, (bswap tGPR:$Rm))]>,
1114                  Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1115
1116 def tREV16 :                    // A8.6.135
1117   T1pIMiscEncode<{1,0,1,0,0,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1118                  IIC_iUNAr,
1119                  "rev16", "\t$Rd, $Rm",
1120              [(set tGPR:$Rd, (rotr (bswap tGPR:$Rm), (i32 16)))]>,
1121                 Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1122
1123 def tREVSH :                    // A8.6.136
1124   T1pIMiscEncode<{1,0,1,0,1,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1125                  IIC_iUNAr,
1126                  "revsh", "\t$Rd, $Rm",
1127                  [(set tGPR:$Rd, (sra (bswap tGPR:$Rm), (i32 16)))]>,
1128                  Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1129
1130 // Rotate right register
1131 def tROR :                      // A8.6.139
1132   T1sItDPEncode<0b0111, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1133                 IIC_iMOVsr,
1134                 "ror", "\t$Rdn, $Rm",
1135                 [(set tGPR:$Rdn, (rotr tGPR:$Rn, tGPR:$Rm))]>,
1136                 Sched<[WriteALU]>;
1137
1138 // Negate register
1139 def tRSB :                      // A8.6.141
1140   T1sIDPEncode<0b1001, (outs tGPR:$Rd), (ins tGPR:$Rn),
1141                IIC_iALUi,
1142                "rsb", "\t$Rd, $Rn, #0",
1143                [(set tGPR:$Rd, (ineg tGPR:$Rn))]>, Sched<[WriteALU]>;
1144
1145 // Subtract with carry register
1146 let Uses = [CPSR] in
1147 def tSBC :                      // A8.6.151
1148   T1sItDPEncode<0b0110, (outs tGPR:$Rdn), (ins tGPR:$Rn, tGPR:$Rm),
1149                 IIC_iALUr,
1150                 "sbc", "\t$Rdn, $Rm",
1151                 [(set tGPR:$Rdn, (sube tGPR:$Rn, tGPR:$Rm))]>,
1152                 Sched<[WriteALU]>;
1153
1154 // Subtract immediate
1155 def tSUBi3 :                    // A8.6.210 T1
1156   T1sIGenEncodeImm<0b01111, (outs tGPR:$Rd), (ins tGPR:$Rm, imm0_7:$imm3),
1157                    IIC_iALUi,
1158                    "sub", "\t$Rd, $Rm, $imm3",
1159                    [(set tGPR:$Rd, (add tGPR:$Rm, imm0_7_neg:$imm3))]>,
1160                    Sched<[WriteALU]> {
1161   bits<3> imm3;
1162   let Inst{8-6} = imm3;
1163 }
1164
1165 def tSUBi8 :                    // A8.6.210 T2
1166   T1sItGenEncodeImm<{1,1,1,?,?}, (outs tGPR:$Rdn),
1167                     (ins tGPR:$Rn, imm0_255:$imm8), IIC_iALUi,
1168                     "sub", "\t$Rdn, $imm8",
1169                     [(set tGPR:$Rdn, (add tGPR:$Rn, imm8_255_neg:$imm8))]>,
1170                     Sched<[WriteALU]>;
1171
1172 // Subtract register
1173 def tSUBrr :                    // A8.6.212
1174   T1sIGenEncode<0b01101, (outs tGPR:$Rd), (ins tGPR:$Rn, tGPR:$Rm),
1175                 IIC_iALUr,
1176                 "sub", "\t$Rd, $Rn, $Rm",
1177                 [(set tGPR:$Rd, (sub tGPR:$Rn, tGPR:$Rm))]>,
1178                 Sched<[WriteALU]>;
1179
1180 // Sign-extend byte
1181 def tSXTB :                     // A8.6.222
1182   T1pIMiscEncode<{0,0,1,0,0,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1183                  IIC_iUNAr,
1184                  "sxtb", "\t$Rd, $Rm",
1185                  [(set tGPR:$Rd, (sext_inreg tGPR:$Rm, i8))]>,
1186                  Requires<[IsThumb, IsThumb1Only, HasV6]>,
1187                  Sched<[WriteALU]>;
1188
1189 // Sign-extend short
1190 def tSXTH :                     // A8.6.224
1191   T1pIMiscEncode<{0,0,1,0,0,0,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1192                  IIC_iUNAr,
1193                  "sxth", "\t$Rd, $Rm",
1194                  [(set tGPR:$Rd, (sext_inreg tGPR:$Rm, i16))]>,
1195                  Requires<[IsThumb, IsThumb1Only, HasV6]>,
1196                  Sched<[WriteALU]>;
1197
1198 // Test
1199 let isCompare = 1, isCommutable = 1, Defs = [CPSR] in
1200 def tTST :                      // A8.6.230
1201   T1pIDPEncode<0b1000, (outs), (ins tGPR:$Rn, tGPR:$Rm), IIC_iTSTr,
1202                "tst", "\t$Rn, $Rm",
1203                [(ARMcmpZ (and_su tGPR:$Rn, tGPR:$Rm), 0)]>,
1204                Sched<[WriteALU]>;
1205
1206 // A8.8.247  UDF - Undefined (Encoding T1)
1207 def tUDF : TI<(outs), (ins imm0_255:$imm8), IIC_Br, "udf\t$imm8",
1208               [(int_arm_undefined imm0_255:$imm8)]>, Encoding16 {
1209   bits<8> imm8;
1210   let Inst{15-12} = 0b1101;
1211   let Inst{11-8} = 0b1110;
1212   let Inst{7-0} = imm8;
1213 }
1214
1215 // Zero-extend byte
1216 def tUXTB :                     // A8.6.262
1217   T1pIMiscEncode<{0,0,1,0,1,1,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1218                  IIC_iUNAr,
1219                  "uxtb", "\t$Rd, $Rm",
1220                  [(set tGPR:$Rd, (and tGPR:$Rm, 0xFF))]>,
1221                  Requires<[IsThumb, IsThumb1Only, HasV6]>,
1222                  Sched<[WriteALU]>;
1223
1224 // Zero-extend short
1225 def tUXTH :                     // A8.6.264
1226   T1pIMiscEncode<{0,0,1,0,1,0,?}, (outs tGPR:$Rd), (ins tGPR:$Rm),
1227                  IIC_iUNAr,
1228                  "uxth", "\t$Rd, $Rm",
1229                  [(set tGPR:$Rd, (and tGPR:$Rm, 0xFFFF))]>,
1230                  Requires<[IsThumb, IsThumb1Only, HasV6]>, Sched<[WriteALU]>;
1231
1232 // Conditional move tMOVCCr - Used to implement the Thumb SELECT_CC operation.
1233 // Expanded after instruction selection into a branch sequence.
1234 let usesCustomInserter = 1 in  // Expanded after instruction selection.
1235   def tMOVCCr_pseudo :
1236   PseudoInst<(outs tGPR:$dst), (ins tGPR:$false, tGPR:$true, cmovpred:$p),
1237              NoItinerary,
1238              [(set tGPR:$dst, (ARMcmov tGPR:$false, tGPR:$true, cmovpred:$p))]>;
1239
1240 // tLEApcrel - Load a pc-relative address into a register without offending the
1241 // assembler.
1242
1243 def tADR : T1I<(outs tGPR:$Rd), (ins t_adrlabel:$addr, pred:$p),
1244                IIC_iALUi, "adr{$p}\t$Rd, $addr", []>,
1245                T1Encoding<{1,0,1,0,0,?}>, Sched<[WriteALU]> {
1246   bits<3> Rd;
1247   bits<8> addr;
1248   let Inst{10-8} = Rd;
1249   let Inst{7-0} = addr;
1250   let DecoderMethod = "DecodeThumbAddSpecialReg";
1251 }
1252
1253 let hasSideEffects = 0, isReMaterializable = 1 in
1254 def tLEApcrel   : tPseudoInst<(outs tGPR:$Rd), (ins i32imm:$label, pred:$p),
1255                               2, IIC_iALUi, []>, Sched<[WriteALU]>;
1256
1257 let hasSideEffects = 1 in
1258 def tLEApcrelJT : tPseudoInst<(outs tGPR:$Rd),
1259                               (ins i32imm:$label, pred:$p),
1260                               2, IIC_iALUi, []>, Sched<[WriteALU]>;
1261
1262 //===----------------------------------------------------------------------===//
1263 // TLS Instructions
1264 //
1265
1266 // __aeabi_read_tp preserves the registers r1-r3.
1267 // This is a pseudo inst so that we can get the encoding right,
1268 // complete with fixup for the aeabi_read_tp function.
1269 let isCall = 1, Defs = [R0, R12, LR, CPSR], Uses = [SP] in
1270 def tTPsoft : tPseudoInst<(outs), (ins), 4, IIC_Br,
1271                           [(set R0, ARMthread_pointer)]>,
1272                           Sched<[WriteBr]>;
1273
1274 //===----------------------------------------------------------------------===//
1275 // SJLJ Exception handling intrinsics
1276 //
1277
1278 // eh_sjlj_setjmp() is an instruction sequence to store the return address and
1279 // save #0 in R0 for the non-longjmp case.  Since by its nature we may be coming
1280 // from some other function to get here, and we're using the stack frame for the
1281 // containing function to save/restore registers, we can't keep anything live in
1282 // regs across the eh_sjlj_setjmp(), else it will almost certainly have been
1283 // tromped upon when we get here from a longjmp(). We force everything out of
1284 // registers except for our own input by listing the relevant registers in
1285 // Defs. By doing so, we also cause the prologue/epilogue code to actively
1286 // preserve all of the callee-saved resgisters, which is exactly what we want.
1287 // $val is a scratch register for our use.
1288 let Defs = [ R0,  R1,  R2,  R3,  R4,  R5,  R6,  R7, R12, CPSR ],
1289     hasSideEffects = 1, isBarrier = 1, isCodeGenOnly = 1,
1290     usesCustomInserter = 1 in
1291 def tInt_eh_sjlj_setjmp : ThumbXI<(outs),(ins tGPR:$src, tGPR:$val),
1292                                   AddrModeNone, 0, NoItinerary, "","",
1293                           [(set R0, (ARMeh_sjlj_setjmp tGPR:$src, tGPR:$val))]>;
1294
1295 // FIXME: Non-IOS version(s)
1296 let isBarrier = 1, hasSideEffects = 1, isTerminator = 1, isCodeGenOnly = 1,
1297     Defs = [ R7, LR, SP ] in
1298 def tInt_eh_sjlj_longjmp : XI<(outs), (ins GPR:$src, GPR:$scratch),
1299                               AddrModeNone, 0, IndexModeNone,
1300                               Pseudo, NoItinerary, "", "",
1301                               [(ARMeh_sjlj_longjmp GPR:$src, GPR:$scratch)]>,
1302                              Requires<[IsThumb]>;
1303
1304 //===----------------------------------------------------------------------===//
1305 // Non-Instruction Patterns
1306 //
1307
1308 // Comparisons
1309 def : T1Pat<(ARMcmpZ tGPR:$Rn, imm0_255:$imm8),
1310             (tCMPi8  tGPR:$Rn, imm0_255:$imm8)>;
1311 def : T1Pat<(ARMcmpZ tGPR:$Rn, tGPR:$Rm),
1312             (tCMPr   tGPR:$Rn, tGPR:$Rm)>;
1313
1314 // Add with carry
1315 def : T1Pat<(addc   tGPR:$lhs, imm0_7:$rhs),
1316             (tADDi3 tGPR:$lhs, imm0_7:$rhs)>;
1317 def : T1Pat<(addc   tGPR:$lhs, imm8_255:$rhs),
1318             (tADDi8 tGPR:$lhs, imm8_255:$rhs)>;
1319 def : T1Pat<(addc   tGPR:$lhs, tGPR:$rhs),
1320             (tADDrr tGPR:$lhs, tGPR:$rhs)>;
1321
1322 // Subtract with carry
1323 def : T1Pat<(addc   tGPR:$lhs, imm0_7_neg:$rhs),
1324             (tSUBi3 tGPR:$lhs, imm0_7_neg:$rhs)>;
1325 def : T1Pat<(addc   tGPR:$lhs, imm8_255_neg:$rhs),
1326             (tSUBi8 tGPR:$lhs, imm8_255_neg:$rhs)>;
1327 def : T1Pat<(subc   tGPR:$lhs, tGPR:$rhs),
1328             (tSUBrr tGPR:$lhs, tGPR:$rhs)>;
1329
1330 // Bswap 16 with load/store
1331 def : T1Pat<(srl (bswap (extloadi16 t_addrmode_rrs2:$addr)), (i32 16)),
1332             (tREV16 (tLDRHr t_addrmode_rrs2:$addr))>;
1333 def : T1Pat<(srl (bswap (extloadi16 t_addrmode_is2:$addr)), (i32 16)),
1334             (tREV16 (tLDRHi t_addrmode_is2:$addr))>;
1335 def : T1Pat<(truncstorei16 (srl (bswap tGPR:$Rn), (i32 16)),
1336                            t_addrmode_rrs2:$addr),
1337             (tSTRHr (tREV16 tGPR:$Rn), t_addrmode_rrs2:$addr)>;
1338 def : T1Pat<(truncstorei16 (srl (bswap tGPR:$Rn), (i32 16)),
1339                            t_addrmode_is2:$addr),
1340             (tSTRHi(tREV16 tGPR:$Rn), t_addrmode_is2:$addr)>;
1341
1342 // ConstantPool
1343 def : T1Pat<(ARMWrapper  tconstpool  :$dst), (tLEApcrel tconstpool  :$dst)>;
1344
1345 // GlobalAddress
1346 def tLDRLIT_ga_pcrel : PseudoInst<(outs tGPR:$dst), (ins i32imm:$addr),
1347                                   IIC_iLoadiALU,
1348                                   [(set tGPR:$dst,
1349                                         (ARMWrapperPIC tglobaladdr:$addr))]>,
1350                        Requires<[IsThumb, DontUseMovt]>;
1351
1352 def tLDRLIT_ga_abs : PseudoInst<(outs tGPR:$dst), (ins i32imm:$src),
1353                                 IIC_iLoad_i,
1354                                 [(set tGPR:$dst,
1355                                       (ARMWrapper tglobaladdr:$src))]>,
1356                      Requires<[IsThumb, DontUseMovt]>;
1357
1358
1359 // JumpTable
1360 def : T1Pat<(ARMWrapperJT tjumptable:$dst),
1361             (tLEApcrelJT tjumptable:$dst)>;
1362
1363 // Direct calls
1364 def : T1Pat<(ARMtcall texternalsym:$func), (tBL texternalsym:$func)>,
1365       Requires<[IsThumb]>;
1366
1367 def : Tv5Pat<(ARMcall texternalsym:$func), (tBLXi texternalsym:$func)>,
1368       Requires<[IsThumb, HasV5T, IsNotMClass]>;
1369
1370 // Indirect calls to ARM routines
1371 def : Tv5Pat<(ARMcall GPR:$dst), (tBLXr GPR:$dst)>,
1372       Requires<[IsThumb, HasV5T]>;
1373
1374 // zextload i1 -> zextload i8
1375 def : T1Pat<(zextloadi1 t_addrmode_rrs1:$addr),
1376             (tLDRBr t_addrmode_rrs1:$addr)>;
1377 def : T1Pat<(zextloadi1 t_addrmode_is1:$addr),
1378             (tLDRBi t_addrmode_is1:$addr)>;
1379
1380 // extload from the stack -> word load from the stack, as it avoids having to
1381 // materialize the base in a separate register. This only works when a word
1382 // load puts the byte/halfword value in the same place in the register that the
1383 // byte/halfword load would, i.e. when little-endian.
1384 def : T1Pat<(extloadi1  t_addrmode_sp:$addr), (tLDRspi t_addrmode_sp:$addr)>,
1385       Requires<[IsThumb, IsThumb1Only, IsLE]>;
1386 def : T1Pat<(extloadi8  t_addrmode_sp:$addr), (tLDRspi t_addrmode_sp:$addr)>,
1387       Requires<[IsThumb, IsThumb1Only, IsLE]>;
1388 def : T1Pat<(extloadi16 t_addrmode_sp:$addr), (tLDRspi t_addrmode_sp:$addr)>,
1389       Requires<[IsThumb, IsThumb1Only, IsLE]>;
1390
1391 // extload -> zextload
1392 def : T1Pat<(extloadi1  t_addrmode_rrs1:$addr), (tLDRBr t_addrmode_rrs1:$addr)>;
1393 def : T1Pat<(extloadi1  t_addrmode_is1:$addr),  (tLDRBi t_addrmode_is1:$addr)>;
1394 def : T1Pat<(extloadi8  t_addrmode_rrs1:$addr), (tLDRBr t_addrmode_rrs1:$addr)>;
1395 def : T1Pat<(extloadi8  t_addrmode_is1:$addr),  (tLDRBi t_addrmode_is1:$addr)>;
1396 def : T1Pat<(extloadi16 t_addrmode_rrs2:$addr), (tLDRHr t_addrmode_rrs2:$addr)>;
1397 def : T1Pat<(extloadi16 t_addrmode_is2:$addr),  (tLDRHi t_addrmode_is2:$addr)>;
1398
1399 // If it's impossible to use [r,r] address mode for sextload, select to
1400 // ldr{b|h} + sxt{b|h} instead.
1401 def : T1Pat<(sextloadi8 t_addrmode_is1:$addr),
1402             (tSXTB (tLDRBi t_addrmode_is1:$addr))>,
1403       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1404 def : T1Pat<(sextloadi8 t_addrmode_rrs1:$addr),
1405             (tSXTB (tLDRBr t_addrmode_rrs1:$addr))>,
1406       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1407 def : T1Pat<(sextloadi16 t_addrmode_is2:$addr),
1408             (tSXTH (tLDRHi t_addrmode_is2:$addr))>,
1409       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1410 def : T1Pat<(sextloadi16 t_addrmode_rrs2:$addr),
1411             (tSXTH (tLDRHr t_addrmode_rrs2:$addr))>,
1412       Requires<[IsThumb, IsThumb1Only, HasV6]>;
1413
1414 def : T1Pat<(sextloadi8 t_addrmode_rrs1:$addr),
1415             (tASRri (tLSLri (tLDRBr t_addrmode_rrs1:$addr), 24), 24)>;
1416 def : T1Pat<(sextloadi8 t_addrmode_is1:$addr),
1417             (tASRri (tLSLri (tLDRBi t_addrmode_is1:$addr), 24), 24)>;
1418 def : T1Pat<(sextloadi16 t_addrmode_rrs2:$addr),
1419             (tASRri (tLSLri (tLDRHr t_addrmode_rrs2:$addr), 16), 16)>;
1420 def : T1Pat<(sextloadi16 t_addrmode_is2:$addr),
1421             (tASRri (tLSLri (tLDRHi t_addrmode_is2:$addr), 16), 16)>;
1422
1423 def : T1Pat<(atomic_load_8 t_addrmode_is1:$src),
1424              (tLDRBi t_addrmode_is1:$src)>;
1425 def : T1Pat<(atomic_load_8 t_addrmode_rrs1:$src),
1426              (tLDRBr t_addrmode_rrs1:$src)>;
1427 def : T1Pat<(atomic_load_16 t_addrmode_is2:$src),
1428              (tLDRHi t_addrmode_is2:$src)>;
1429 def : T1Pat<(atomic_load_16 t_addrmode_rrs2:$src),
1430              (tLDRHr t_addrmode_rrs2:$src)>;
1431 def : T1Pat<(atomic_load_32 t_addrmode_is4:$src),
1432              (tLDRi t_addrmode_is4:$src)>;
1433 def : T1Pat<(atomic_load_32 t_addrmode_rrs4:$src),
1434              (tLDRr t_addrmode_rrs4:$src)>;
1435 def : T1Pat<(atomic_store_8 t_addrmode_is1:$ptr, tGPR:$val),
1436              (tSTRBi tGPR:$val, t_addrmode_is1:$ptr)>;
1437 def : T1Pat<(atomic_store_8 t_addrmode_rrs1:$ptr, tGPR:$val),
1438              (tSTRBr tGPR:$val, t_addrmode_rrs1:$ptr)>;
1439 def : T1Pat<(atomic_store_16 t_addrmode_is2:$ptr, tGPR:$val),
1440              (tSTRHi tGPR:$val, t_addrmode_is2:$ptr)>;
1441 def : T1Pat<(atomic_store_16 t_addrmode_rrs2:$ptr, tGPR:$val),
1442              (tSTRHr tGPR:$val, t_addrmode_rrs2:$ptr)>;
1443 def : T1Pat<(atomic_store_32 t_addrmode_is4:$ptr, tGPR:$val),
1444              (tSTRi tGPR:$val, t_addrmode_is4:$ptr)>;
1445 def : T1Pat<(atomic_store_32 t_addrmode_rrs4:$ptr, tGPR:$val),
1446              (tSTRr tGPR:$val, t_addrmode_rrs4:$ptr)>;
1447
1448 // Large immediate handling.
1449
1450 // Two piece imms.
1451 def : T1Pat<(i32 thumb_immshifted:$src),
1452             (tLSLri (tMOVi8 (thumb_immshifted_val imm:$src)),
1453                     (thumb_immshifted_shamt imm:$src))>;
1454
1455 def : T1Pat<(i32 imm0_255_comp:$src),
1456             (tMVN (tMOVi8 (imm_comp_XFORM imm:$src)))>;
1457
1458 // Pseudo instruction that combines ldr from constpool and add pc. This should
1459 // be expanded into two instructions late to allow if-conversion and
1460 // scheduling.
1461 let isReMaterializable = 1 in
1462 def tLDRpci_pic : PseudoInst<(outs GPR:$dst), (ins i32imm:$addr, pclabel:$cp),
1463                              NoItinerary,
1464                [(set GPR:$dst, (ARMpic_add (load (ARMWrapper tconstpool:$addr)),
1465                                            imm:$cp))]>,
1466                Requires<[IsThumb, IsThumb1Only]>;
1467
1468 // Pseudo-instruction for merged POP and return.
1469 // FIXME: remove when we have a way to marking a MI with these properties.
1470 let isReturn = 1, isTerminator = 1, isBarrier = 1, mayLoad = 1,
1471     hasExtraDefRegAllocReq = 1 in
1472 def tPOP_RET : tPseudoExpand<(outs), (ins pred:$p, reglist:$regs, variable_ops),
1473                            2, IIC_iPop_Br, [],
1474                            (tPOP pred:$p, reglist:$regs)>, Sched<[WriteBrL]>;
1475
1476 // Indirect branch using "mov pc, $Rm"
1477 let isBranch = 1, isTerminator = 1, isBarrier = 1, isIndirectBranch = 1 in {
1478   def tBRIND : tPseudoExpand<(outs), (ins GPR:$Rm, pred:$p),
1479                   2, IIC_Br, [(brind GPR:$Rm)],
1480                   (tMOVr PC, GPR:$Rm, pred:$p)>, Sched<[WriteBr]>;
1481 }
1482
1483
1484 // In Thumb1, "nop" is encoded as a "mov r8, r8". Technically, the bf00
1485 // encoding is available on ARMv6K, but we don't differentiate that finely.
1486 def : InstAlias<"nop", (tMOVr R8, R8, 14, 0)>,Requires<[IsThumb, IsThumb1Only]>;
1487
1488
1489 // For round-trip assembly/disassembly, we have to handle a CPS instruction
1490 // without any iflags. That's not, strictly speaking, valid syntax, but it's
1491 // a useful extension and assembles to defined behaviour (the insn does
1492 // nothing).
1493 def : tInstAlias<"cps$imod", (tCPS imod_op:$imod, 0)>;
1494 def : tInstAlias<"cps$imod", (tCPS imod_op:$imod, 0)>;
1495
1496 // "neg" is and alias for "rsb rd, rn, #0"
1497 def : tInstAlias<"neg${s}${p} $Rd, $Rm",
1498                  (tRSB tGPR:$Rd, s_cc_out:$s, tGPR:$Rm, pred:$p)>;
1499
1500
1501 // Implied destination operand forms for shifts.
1502 def : tInstAlias<"lsl${s}${p} $Rdm, $imm",
1503              (tLSLri tGPR:$Rdm, cc_out:$s, tGPR:$Rdm, imm0_31:$imm, pred:$p)>;
1504 def : tInstAlias<"lsr${s}${p} $Rdm, $imm",
1505              (tLSRri tGPR:$Rdm, cc_out:$s, tGPR:$Rdm, imm_sr:$imm, pred:$p)>;
1506 def : tInstAlias<"asr${s}${p} $Rdm, $imm",
1507              (tASRri tGPR:$Rdm, cc_out:$s, tGPR:$Rdm, imm_sr:$imm, pred:$p)>;