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