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