1 //===- ARMInstrThumb.td - Thumb support for ARM ---------------------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by Chris Lattner and is distributed under the
6 // University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file describes the Thumb instruction set.
12 //===----------------------------------------------------------------------===//
14 //===----------------------------------------------------------------------===//
15 // Thumb specific DAG Nodes.
18 def ARMtcall : SDNode<"ARMISD::tCALL", SDT_ARMcall,
19 [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag]>;
21 // TI - Thumb instruction.
23 // ThumbPat - Same as Pat<>, but requires that the compiler be in Thumb mode.
24 class ThumbPat<dag pattern, dag result> : Pat<pattern, result> {
25 list<Predicate> Predicates = [IsThumb];
28 class ThumbV5Pat<dag pattern, dag result> : Pat<pattern, result> {
29 list<Predicate> Predicates = [IsThumb, HasV5T];
32 class ThumbI<dag ops, AddrMode am, SizeFlagVal sz,
33 string asm, string cstr, list<dag> pattern>
34 // FIXME: Set all opcodes to 0 for now.
35 : InstARM<0, am, sz, IndexModeNone, ops, asm, cstr> {
36 let Pattern = pattern;
37 list<Predicate> Predicates = [IsThumb];
40 class TI<dag ops, string asm, list<dag> pattern>
41 : ThumbI<ops, AddrModeNone, Size2Bytes, asm, "", pattern>;
42 class TI1<dag ops, string asm, list<dag> pattern>
43 : ThumbI<ops, AddrModeT1, Size2Bytes, asm, "", pattern>;
44 class TI2<dag ops, string asm, list<dag> pattern>
45 : ThumbI<ops, AddrModeT2, Size2Bytes, asm, "", pattern>;
46 class TI4<dag ops, string asm, list<dag> pattern>
47 : ThumbI<ops, AddrModeT4, Size2Bytes, asm, "", pattern>;
48 class TIs<dag ops, string asm, list<dag> pattern>
49 : ThumbI<ops, AddrModeTs, Size2Bytes, asm, "", pattern>;
51 // Two-address instructions
52 class TIt<dag ops, string asm, list<dag> pattern>
53 : ThumbI<ops, AddrModeNone, Size2Bytes, asm, "$lhs = $dst", pattern>;
55 // BL, BLX(1) are translated by assembler into two instructions
56 class TIx2<dag ops, string asm, list<dag> pattern>
57 : ThumbI<ops, AddrModeNone, Size4Bytes, asm, "", pattern>;
60 class TJTI<dag ops, string asm, list<dag> pattern>
61 : ThumbI<ops, AddrModeNone, SizeSpecial, asm, "", pattern>;
63 def imm_neg_XFORM : SDNodeXForm<imm, [{
64 return CurDAG->getTargetConstant(-(int)N->getValue(), MVT::i32);
66 def imm_comp_XFORM : SDNodeXForm<imm, [{
67 return CurDAG->getTargetConstant(~((uint32_t)N->getValue()), MVT::i32);
71 /// imm0_7 predicate - True if the 32-bit immediate is in the range [0,7].
72 def imm0_7 : PatLeaf<(i32 imm), [{
73 return (uint32_t)N->getValue() < 8;
75 def imm0_7_neg : PatLeaf<(i32 imm), [{
76 return (uint32_t)-N->getValue() < 8;
79 def imm0_255 : PatLeaf<(i32 imm), [{
80 return (uint32_t)N->getValue() < 256;
82 def imm0_255_comp : PatLeaf<(i32 imm), [{
83 return ~((uint32_t)N->getValue()) < 256;
86 def imm8_255 : PatLeaf<(i32 imm), [{
87 return (uint32_t)N->getValue() >= 8 && (uint32_t)N->getValue() < 256;
89 def imm8_255_neg : PatLeaf<(i32 imm), [{
90 unsigned Val = -N->getValue();
91 return Val >= 8 && Val < 256;
94 // Break imm's up into two pieces: an immediate + a left shift.
95 // This uses thumb_immshifted to match and thumb_immshifted_val and
96 // thumb_immshifted_shamt to get the val/shift pieces.
97 def thumb_immshifted : PatLeaf<(imm), [{
98 return ARM_AM::isThumbImmShiftedVal((unsigned)N->getValue());
101 def thumb_immshifted_val : SDNodeXForm<imm, [{
102 unsigned V = ARM_AM::getThumbImmNonShiftedVal((unsigned)N->getValue());
103 return CurDAG->getTargetConstant(V, MVT::i32);
106 def thumb_immshifted_shamt : SDNodeXForm<imm, [{
107 unsigned V = ARM_AM::getThumbImmValShift((unsigned)N->getValue());
108 return CurDAG->getTargetConstant(V, MVT::i32);
111 // Define Thumb specific addressing modes.
113 // t_addrmode_rr := reg + reg
115 def t_addrmode_rr : Operand<i32>,
116 ComplexPattern<i32, 2, "SelectThumbAddrModeRR", []> {
117 let PrintMethod = "printThumbAddrModeRROperand";
118 let MIOperandInfo = (ops GPR:$base, GPR:$offsreg);
121 // t_addrmode_s4 := reg + reg
124 def t_addrmode_s4 : Operand<i32>,
125 ComplexPattern<i32, 3, "SelectThumbAddrModeS4", []> {
126 let PrintMethod = "printThumbAddrModeS4Operand";
127 let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm, GPR:$offsreg);
130 // t_addrmode_s2 := reg + reg
133 def t_addrmode_s2 : Operand<i32>,
134 ComplexPattern<i32, 3, "SelectThumbAddrModeS2", []> {
135 let PrintMethod = "printThumbAddrModeS2Operand";
136 let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm, GPR:$offsreg);
139 // t_addrmode_s1 := reg + reg
142 def t_addrmode_s1 : Operand<i32>,
143 ComplexPattern<i32, 3, "SelectThumbAddrModeS1", []> {
144 let PrintMethod = "printThumbAddrModeS1Operand";
145 let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm, GPR:$offsreg);
148 // t_addrmode_sp := sp + imm8 * 4
150 def t_addrmode_sp : Operand<i32>,
151 ComplexPattern<i32, 2, "SelectThumbAddrModeSP", []> {
152 let PrintMethod = "printThumbAddrModeSPOperand";
153 let MIOperandInfo = (ops GPR:$base, i32imm:$offsimm);
156 //===----------------------------------------------------------------------===//
157 // Miscellaneous Instructions.
160 def tPICADD : TIt<(ops GPR:$dst, GPR:$lhs, pclabel:$cp),
161 "$cp:\n\tadd $dst, pc",
162 [(set GPR:$dst, (ARMpic_add GPR:$lhs, imm:$cp))]>;
164 //===----------------------------------------------------------------------===//
165 // Control Flow Instructions.
168 let isReturn = 1, isTerminator = 1 in {
169 def tBX_RET : TI<(ops), "bx lr", [(ARMretflag)]>;
170 // Alternative return instruction used by vararg functions.
171 def tBX_RET_vararg : TI<(ops GPR:$dst), "bx $dst", []>;
174 // FIXME: remove when we have a way to marking a MI with these properties.
175 let isLoad = 1, isReturn = 1, isTerminator = 1 in
176 def tPOP_RET : TI<(ops reglist:$dst1, variable_ops),
179 let isCall = 1, noResults = 1,
180 Defs = [R0, R1, R2, R3, LR,
181 D0, D1, D2, D3, D4, D5, D6, D7] in {
182 def tBL : TIx2<(ops i32imm:$func, variable_ops),
184 [(ARMtcall tglobaladdr:$func)]>;
186 def tBLXi : TIx2<(ops i32imm:$func, variable_ops),
188 [(ARMcall tglobaladdr:$func)]>, Requires<[HasV5T]>;
189 def tBLXr : TI<(ops GPR:$dst, variable_ops),
191 [(ARMtcall GPR:$dst)]>, Requires<[HasV5T]>;
193 def tBX : TIx2<(ops GPR:$dst, variable_ops),
194 "cpy lr, pc\n\tbx $dst",
195 [(ARMcall_nolink GPR:$dst)]>;
198 let isBranch = 1, isTerminator = 1, isBarrier = 1 in {
199 def tB : TI<(ops brtarget:$dst), "b $dst", [(br bb:$dst)]>;
202 def tBfar : TIx2<(ops brtarget:$dst), "bl $dst\t@ far jump", []>;
204 def tBR_JTr : TJTI<(ops GPR:$dst, jtblock_operand:$jt, i32imm:$id),
205 "cpy pc, $dst \n\t.align\t2\n$jt",
206 [(ARMbrjt GPR:$dst, tjumptable:$jt, imm:$id)]>;
209 let isBranch = 1, isTerminator = 1, noResults = 1, isBarrier = 1 in
210 def tBcc : TI<(ops brtarget:$dst, CCOp:$cc), "b$cc $dst",
211 [(ARMbrcond bb:$dst, imm:$cc)]>;
213 //===----------------------------------------------------------------------===//
214 // Load Store Instructions.
218 def tLDR : TI4<(ops GPR:$dst, t_addrmode_s4:$addr),
220 [(set GPR:$dst, (load t_addrmode_s4:$addr))]>;
222 def tLDRB : TI1<(ops GPR:$dst, t_addrmode_s1:$addr),
224 [(set GPR:$dst, (zextloadi8 t_addrmode_s1:$addr))]>;
226 def tLDRH : TI2<(ops GPR:$dst, t_addrmode_s2:$addr),
228 [(set GPR:$dst, (zextloadi16 t_addrmode_s2:$addr))]>;
230 def tLDRSB : TI1<(ops GPR:$dst, t_addrmode_rr:$addr),
232 [(set GPR:$dst, (sextloadi8 t_addrmode_rr:$addr))]>;
234 def tLDRSH : TI2<(ops GPR:$dst, t_addrmode_rr:$addr),
236 [(set GPR:$dst, (sextloadi16 t_addrmode_rr:$addr))]>;
238 def tLDRspi : TIs<(ops GPR:$dst, t_addrmode_sp:$addr),
240 [(set GPR:$dst, (load t_addrmode_sp:$addr))]>;
242 // Special instruction for restore. It cannot clobber condition register
243 // when it's expanded by eliminateCallFramePseudoInstr().
244 def tRestore : TIs<(ops GPR:$dst, t_addrmode_sp:$addr),
245 "ldr $dst, $addr", []>;
248 def tLDRpci : TIs<(ops GPR:$dst, i32imm:$addr),
250 [(set GPR:$dst, (load (ARMWrapper tconstpool:$addr)))]>;
254 def tSTR : TI4<(ops GPR:$src, t_addrmode_s4:$addr),
256 [(store GPR:$src, t_addrmode_s4:$addr)]>;
258 def tSTRB : TI1<(ops GPR:$src, t_addrmode_s1:$addr),
260 [(truncstorei8 GPR:$src, t_addrmode_s1:$addr)]>;
262 def tSTRH : TI2<(ops GPR:$src, t_addrmode_s2:$addr),
264 [(truncstorei16 GPR:$src, t_addrmode_s2:$addr)]>;
266 def tSTRspi : TIs<(ops GPR:$src, t_addrmode_sp:$addr),
268 [(store GPR:$src, t_addrmode_sp:$addr)]>;
270 // Special instruction for spill. It cannot clobber condition register
271 // when it's expanded by eliminateCallFramePseudoInstr().
272 def tSpill : TIs<(ops GPR:$src, t_addrmode_sp:$addr),
273 "str $src, $addr", []>;
276 //===----------------------------------------------------------------------===//
277 // Load / store multiple Instructions.
280 // TODO: A7-44: LDMIA - load multiple
283 def tPOP : TI<(ops reglist:$dst1, variable_ops),
287 def tPUSH : TI<(ops reglist:$src1, variable_ops),
290 //===----------------------------------------------------------------------===//
291 // Arithmetic Instructions.
295 def tADC : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
297 [(set GPR:$dst, (adde GPR:$lhs, GPR:$rhs))]>;
299 def tADDS : TI<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
300 "add $dst, $lhs, $rhs",
301 [(set GPR:$dst, (addc GPR:$lhs, GPR:$rhs))]>;
304 def tADDi3 : TI<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
305 "add $dst, $lhs, $rhs",
306 [(set GPR:$dst, (add GPR:$lhs, imm0_7:$rhs))]>;
308 def tADDi8 : TIt<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
310 [(set GPR:$dst, (add GPR:$lhs, imm8_255:$rhs))]>;
312 def tADDrr : TI<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
313 "add $dst, $lhs, $rhs",
314 [(set GPR:$dst, (add GPR:$lhs, GPR:$rhs))]>;
316 def tADDhirr : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
317 "add $dst, $rhs", []>;
319 def tADDrPCi : TI<(ops GPR:$dst, i32imm:$rhs),
320 "add $dst, pc, $rhs * 4", []>;
321 def tADDrSPi : TI<(ops GPR:$dst, GPR:$sp, i32imm:$rhs),
322 "add $dst, $sp, $rhs * 4", []>;
323 def tADDspi : TIt<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
324 "add $dst, $rhs * 4", []>;
326 def tAND : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
328 [(set GPR:$dst, (and GPR:$lhs, GPR:$rhs))]>;
330 def tASRri : TI<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
331 "asr $dst, $lhs, $rhs",
332 [(set GPR:$dst, (sra GPR:$lhs, imm:$rhs))]>;
334 def tASRrr : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
336 [(set GPR:$dst, (sra GPR:$lhs, GPR:$rhs))]>;
338 def tBIC : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
340 [(set GPR:$dst, (and GPR:$lhs, (not GPR:$rhs)))]>;
343 def tCMN : TI<(ops GPR:$lhs, GPR:$rhs),
345 [(ARMcmp GPR:$lhs, (ineg GPR:$rhs))]>;
347 def tCMPi8 : TI<(ops GPR:$lhs, i32imm:$rhs),
349 [(ARMcmp GPR:$lhs, imm0_255:$rhs)]>;
351 def tCMPr : TI<(ops GPR:$lhs, GPR:$rhs),
353 [(ARMcmp GPR:$lhs, GPR:$rhs)]>;
355 // TODO: A7-37: CMP(3) - cmp hi regs
357 def tEOR : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
359 [(set GPR:$dst, (xor GPR:$lhs, GPR:$rhs))]>;
361 def tLSLri : TI<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
362 "lsl $dst, $lhs, $rhs",
363 [(set GPR:$dst, (shl GPR:$lhs, imm:$rhs))]>;
365 def tLSLrr : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
367 [(set GPR:$dst, (shl GPR:$lhs, GPR:$rhs))]>;
369 def tLSRri : TI<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
370 "lsr $dst, $lhs, $rhs",
371 [(set GPR:$dst, (srl GPR:$lhs, imm:$rhs))]>;
373 def tLSRrr : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
375 [(set GPR:$dst, (srl GPR:$lhs, GPR:$rhs))]>;
377 def tMOVri8 : TI<(ops GPR:$dst, i32imm:$src),
379 [(set GPR:$dst, imm0_255:$src)]>;
381 // TODO: A7-73: MOV(2) - mov setting flag.
384 // Note: MOV(2) of two low regs updates the flags, so we emit this as 'cpy',
385 // which is MOV(3). This also supports high registers.
386 def tMOVrr : TI<(ops GPR:$dst, GPR:$src),
387 "cpy $dst, $src", []>;
389 def tMUL : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
391 [(set GPR:$dst, (mul GPR:$lhs, GPR:$rhs))]>;
393 def tMVN : TI<(ops GPR:$dst, GPR:$src),
395 [(set GPR:$dst, (not GPR:$src))]>;
397 def tNEG : TI<(ops GPR:$dst, GPR:$src),
399 [(set GPR:$dst, (ineg GPR:$src))]>;
401 def tORR : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
403 [(set GPR:$dst, (or GPR:$lhs, GPR:$rhs))]>;
406 def tREV : TI<(ops GPR:$dst, GPR:$src),
408 [(set GPR:$dst, (bswap GPR:$src))]>,
409 Requires<[IsThumb, HasV6]>;
411 def tREV16 : TI<(ops GPR:$dst, GPR:$src),
414 (or (and (srl GPR:$src, 8), 0xFF),
415 (or (and (shl GPR:$src, 8), 0xFF00),
416 (or (and (srl GPR:$src, 8), 0xFF0000),
417 (and (shl GPR:$src, 8), 0xFF000000)))))]>,
418 Requires<[IsThumb, HasV6]>;
420 def tREVSH : TI<(ops GPR:$dst, GPR:$src),
424 (or (srl (and GPR:$src, 0xFFFF), 8),
425 (shl GPR:$src, 8)), i16))]>,
426 Requires<[IsThumb, HasV6]>;
428 def tROR : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
430 [(set GPR:$dst, (rotr GPR:$lhs, GPR:$rhs))]>;
433 // Subtract with carry
434 def tSBC : TIt<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
436 [(set GPR:$dst, (sube GPR:$lhs, GPR:$rhs))]>;
438 def tSUBS : TI<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
439 "sub $dst, $lhs, $rhs",
440 [(set GPR:$dst, (subc GPR:$lhs, GPR:$rhs))]>;
443 // TODO: A7-96: STMIA - store multiple.
445 def tSUBi3 : TI<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
446 "sub $dst, $lhs, $rhs",
447 [(set GPR:$dst, (add GPR:$lhs, imm0_7_neg:$rhs))]>;
449 def tSUBi8 : TIt<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
451 [(set GPR:$dst, (add GPR:$lhs, imm8_255_neg:$rhs))]>;
453 def tSUBrr : TI<(ops GPR:$dst, GPR:$lhs, GPR:$rhs),
454 "sub $dst, $lhs, $rhs",
455 [(set GPR:$dst, (sub GPR:$lhs, GPR:$rhs))]>;
457 def tSUBspi : TIt<(ops GPR:$dst, GPR:$lhs, i32imm:$rhs),
458 "sub $dst, $rhs * 4", []>;
460 def tSXTB : TI<(ops GPR:$dst, GPR:$src),
462 [(set GPR:$dst, (sext_inreg GPR:$src, i8))]>,
463 Requires<[IsThumb, HasV6]>;
464 def tSXTH : TI<(ops GPR:$dst, GPR:$src),
466 [(set GPR:$dst, (sext_inreg GPR:$src, i16))]>,
467 Requires<[IsThumb, HasV6]>;
469 // TODO: A7-122: TST - test.
471 def tUXTB : TI<(ops GPR:$dst, GPR:$src),
473 [(set GPR:$dst, (and GPR:$src, 0xFF))]>,
474 Requires<[IsThumb, HasV6]>;
475 def tUXTH : TI<(ops GPR:$dst, GPR:$src),
477 [(set GPR:$dst, (and GPR:$src, 0xFFFF))]>,
478 Requires<[IsThumb, HasV6]>;
481 // Conditional move tMOVCCr - Used to implement the Thumb SELECT_CC DAG operation.
482 // Expanded by the scheduler into a branch sequence.
483 let usesCustomDAGSchedInserter = 1 in // Expanded by the scheduler.
485 PseudoInst<(ops GPR:$dst, GPR:$false, GPR:$true, CCOp:$cc),
487 [(set GPR:$dst, (ARMcmov GPR:$false, GPR:$true, imm:$cc))]>;
489 // tLEApcrel - Load a pc-relative address into a register without offending the
491 def tLEApcrel : TIx2<(ops GPR:$dst, i32imm:$label),
492 !strconcat(!strconcat(".set PCRELV${:uid}, ($label-(",
493 "${:private}PCRELL${:uid}+6))\n"),
494 !strconcat("\tmov $dst, #PCRELV${:uid}\n",
495 "${:private}PCRELL${:uid}:\n\tadd $dst, pc")),
498 def tLEApcrelJT : TIx2<(ops GPR:$dst, i32imm:$label, i32imm:$id),
499 !strconcat(!strconcat(".set PCRELV${:uid}, (${label}_${id:no_hash}-(",
500 "${:private}PCRELL${:uid}+4))\n"),
501 !strconcat("\tmov $dst, #PCRELV${:uid}\n",
502 "${:private}PCRELL${:uid}:\n\tadd $dst, pc")),
505 //===----------------------------------------------------------------------===//
506 // Non-Instruction Patterns
509 // ConstantPool, GlobalAddress
510 def : ThumbPat<(ARMWrapper tglobaladdr :$dst), (tLEApcrel tglobaladdr :$dst)>;
511 def : ThumbPat<(ARMWrapper tconstpool :$dst), (tLEApcrel tconstpool :$dst)>;
514 def : ThumbPat<(ARMWrapperJT tjumptable:$dst, imm:$id),
515 (tLEApcrelJT tjumptable:$dst, imm:$id)>;
518 def : ThumbPat<(ARMtcall texternalsym:$func), (tBL texternalsym:$func)>;
519 def : ThumbV5Pat<(ARMcall texternalsym:$func), (tBLXi texternalsym:$func)>;
521 // Indirect calls to ARM routines
522 def : ThumbV5Pat<(ARMcall GPR:$dst), (tBLXr GPR:$dst)>;
524 // zextload i1 -> zextload i8
525 def : ThumbPat<(zextloadi1 t_addrmode_s1:$addr),
526 (tLDRB t_addrmode_s1:$addr)>;
528 // extload -> zextload
529 def : ThumbPat<(extloadi1 t_addrmode_s1:$addr), (tLDRB t_addrmode_s1:$addr)>;
530 def : ThumbPat<(extloadi8 t_addrmode_s1:$addr), (tLDRB t_addrmode_s1:$addr)>;
531 def : ThumbPat<(extloadi16 t_addrmode_s2:$addr), (tLDRH t_addrmode_s2:$addr)>;
533 // truncstore i1 -> truncstore i8
534 def : ThumbPat<(truncstorei1 GPR:$src, t_addrmode_s1:$dst),
535 (tSTRB GPR:$src, t_addrmode_s1:$dst)>;
537 // Large immediate handling.
540 def : ThumbPat<(i32 thumb_immshifted:$src),
541 (tLSLri (tMOVri8 (thumb_immshifted_val imm:$src)),
542 (thumb_immshifted_shamt imm:$src))>;
544 def : ThumbPat<(i32 imm0_255_comp:$src),
545 (tMVN (tMOVri8 (imm_comp_XFORM imm:$src)))>;