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