Fix encoding of multiple instructions with 3 src operands; also handle smmul, smmla...
[oota-llvm.git] / lib / Target / ARM / ARMInstrInfo.td
1 //===- ARMInstrInfo.td - Target Description for ARM Target -*- tablegen -*-===//
2 //
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
7 //
8 //===----------------------------------------------------------------------===//
9 //
10 // This file describes the ARM instructions in TableGen format.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // ARM specific DAG Nodes.
16 //
17
18 // Type profiles.
19 def SDT_ARMCallSeqStart : SDCallSeqStart<[ SDTCisVT<0, i32> ]>;
20 def SDT_ARMCallSeqEnd   : SDCallSeqEnd<[ SDTCisVT<0, i32>, SDTCisVT<1, i32> ]>;
21
22 def SDT_ARMSaveCallPC : SDTypeProfile<0, 1, []>;
23
24 def SDT_ARMcall    : SDTypeProfile<0, -1, [SDTCisInt<0>]>;
25
26 def SDT_ARMCMov    : SDTypeProfile<1, 3,
27                                    [SDTCisSameAs<0, 1>, SDTCisSameAs<0, 2>,
28                                     SDTCisVT<3, i32>]>;
29
30 def SDT_ARMBrcond  : SDTypeProfile<0, 2,
31                                    [SDTCisVT<0, OtherVT>, SDTCisVT<1, i32>]>;
32
33 def SDT_ARMBrJT    : SDTypeProfile<0, 3,
34                                   [SDTCisPtrTy<0>, SDTCisVT<1, i32>,
35                                    SDTCisVT<2, i32>]>;
36
37 def SDT_ARMCmp     : SDTypeProfile<0, 2, [SDTCisSameAs<0, 1>]>;
38
39 def SDT_ARMPICAdd  : SDTypeProfile<1, 2, [SDTCisSameAs<0, 1>,
40                                           SDTCisPtrTy<1>, SDTCisVT<2, i32>]>;
41
42 def SDT_ARMThreadPointer : SDTypeProfile<1, 0, [SDTCisPtrTy<0>]>;
43
44 // Node definitions.
45 def ARMWrapper       : SDNode<"ARMISD::Wrapper",     SDTIntUnaryOp>;
46 def ARMWrapperJT     : SDNode<"ARMISD::WrapperJT",   SDTIntBinOp>;
47
48 def ARMcallseq_start : SDNode<"ISD::CALLSEQ_START", SDT_ARMCallSeqStart,
49                               [SDNPHasChain, SDNPOutFlag]>;
50 def ARMcallseq_end   : SDNode<"ISD::CALLSEQ_END",   SDT_ARMCallSeqEnd,
51                               [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag]>;
52
53 def ARMcall          : SDNode<"ARMISD::CALL", SDT_ARMcall,
54                               [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag]>;
55 def ARMcall_pred    : SDNode<"ARMISD::CALL_PRED", SDT_ARMcall,
56                               [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag]>;
57 def ARMcall_nolink   : SDNode<"ARMISD::CALL_NOLINK", SDT_ARMcall,
58                               [SDNPHasChain, SDNPOptInFlag, SDNPOutFlag]>;
59
60 def ARMretflag       : SDNode<"ARMISD::RET_FLAG", SDTNone,
61                               [SDNPHasChain, SDNPOptInFlag]>;
62
63 def ARMcmov          : SDNode<"ARMISD::CMOV", SDT_ARMCMov,
64                               [SDNPInFlag]>;
65 def ARMcneg          : SDNode<"ARMISD::CNEG", SDT_ARMCMov,
66                               [SDNPInFlag]>;
67
68 def ARMbrcond        : SDNode<"ARMISD::BRCOND", SDT_ARMBrcond,
69                               [SDNPHasChain, SDNPInFlag, SDNPOutFlag]>;
70
71 def ARMbrjt          : SDNode<"ARMISD::BR_JT", SDT_ARMBrJT,
72                               [SDNPHasChain]>;
73
74 def ARMcmp           : SDNode<"ARMISD::CMP", SDT_ARMCmp,
75                               [SDNPOutFlag]>;
76
77 def ARMcmpNZ         : SDNode<"ARMISD::CMPNZ", SDT_ARMCmp,
78                               [SDNPOutFlag]>;
79
80 def ARMpic_add       : SDNode<"ARMISD::PIC_ADD", SDT_ARMPICAdd>;
81
82 def ARMsrl_flag      : SDNode<"ARMISD::SRL_FLAG", SDTIntUnaryOp, [SDNPOutFlag]>;
83 def ARMsra_flag      : SDNode<"ARMISD::SRA_FLAG", SDTIntUnaryOp, [SDNPOutFlag]>;
84 def ARMrrx           : SDNode<"ARMISD::RRX"     , SDTIntUnaryOp, [SDNPInFlag ]>;
85
86 def ARMthread_pointer: SDNode<"ARMISD::THREAD_POINTER", SDT_ARMThreadPointer>;
87
88 //===----------------------------------------------------------------------===//
89 // ARM Instruction Predicate Definitions.
90 //
91 def HasV5T  : Predicate<"Subtarget->hasV5TOps()">;
92 def HasV5TE : Predicate<"Subtarget->hasV5TEOps()">;
93 def HasV6   : Predicate<"Subtarget->hasV6Ops()">;
94 def IsThumb : Predicate<"Subtarget->isThumb()">;
95 def IsARM   : Predicate<"!Subtarget->isThumb()">;
96
97 //===----------------------------------------------------------------------===//
98 // ARM Flag Definitions.
99
100 class RegConstraint<string C> {
101   string Constraints = C;
102 }
103
104 //===----------------------------------------------------------------------===//
105 //  ARM specific transformation functions and pattern fragments.
106 //
107
108 // so_imm_XFORM - Return a so_imm value packed into the format described for
109 // so_imm def below.
110 def so_imm_XFORM : SDNodeXForm<imm, [{
111   return CurDAG->getTargetConstant(ARM_AM::getSOImmVal(N->getZExtValue()),
112                                    MVT::i32);
113 }]>;
114
115 // so_imm_neg_XFORM - Return a so_imm value packed into the format described for
116 // so_imm_neg def below.
117 def so_imm_neg_XFORM : SDNodeXForm<imm, [{
118   return CurDAG->getTargetConstant(ARM_AM::getSOImmVal(-(int)N->getZExtValue()),
119                                    MVT::i32);
120 }]>;
121
122 // so_imm_not_XFORM - Return a so_imm value packed into the format described for
123 // so_imm_not def below.
124 def so_imm_not_XFORM : SDNodeXForm<imm, [{
125   return CurDAG->getTargetConstant(ARM_AM::getSOImmVal(~(int)N->getZExtValue()),
126                                    MVT::i32);
127 }]>;
128
129 // rot_imm predicate - True if the 32-bit immediate is equal to 8, 16, or 24.
130 def rot_imm : PatLeaf<(i32 imm), [{
131   int32_t v = (int32_t)N->getZExtValue();
132   return v == 8 || v == 16 || v == 24;
133 }]>;
134
135 /// imm1_15 predicate - True if the 32-bit immediate is in the range [1,15].
136 def imm1_15 : PatLeaf<(i32 imm), [{
137   return (int32_t)N->getZExtValue() >= 1 && (int32_t)N->getZExtValue() < 16;
138 }]>;
139
140 /// imm16_31 predicate - True if the 32-bit immediate is in the range [16,31].
141 def imm16_31 : PatLeaf<(i32 imm), [{
142   return (int32_t)N->getZExtValue() >= 16 && (int32_t)N->getZExtValue() < 32;
143 }]>;
144
145 def so_imm_neg : 
146   PatLeaf<(imm), [{
147     return ARM_AM::getSOImmVal(-(int)N->getZExtValue()) != -1;
148   }], so_imm_neg_XFORM>;
149
150 def so_imm_not :
151   PatLeaf<(imm), [{
152     return ARM_AM::getSOImmVal(~(int)N->getZExtValue()) != -1;
153   }], so_imm_not_XFORM>;
154
155 // sext_16_node predicate - True if the SDNode is sign-extended 16 or more bits.
156 def sext_16_node : PatLeaf<(i32 GPR:$a), [{
157   return CurDAG->ComputeNumSignBits(SDValue(N,0)) >= 17;
158 }]>;
159
160 class BinOpFrag<dag res> : PatFrag<(ops node:$LHS, node:$RHS), res>;
161 class UnOpFrag <dag res> : PatFrag<(ops node:$Src), res>;
162
163 //===----------------------------------------------------------------------===//
164 // Operand Definitions.
165 //
166
167 // Branch target.
168 def brtarget : Operand<OtherVT>;
169
170 // A list of registers separated by comma. Used by load/store multiple.
171 def reglist : Operand<i32> {
172   let PrintMethod = "printRegisterList";
173 }
174
175 // An operand for the CONSTPOOL_ENTRY pseudo-instruction.
176 def cpinst_operand : Operand<i32> {
177   let PrintMethod = "printCPInstOperand";
178 }
179
180 def jtblock_operand : Operand<i32> {
181   let PrintMethod = "printJTBlockOperand";
182 }
183
184 // Local PC labels.
185 def pclabel : Operand<i32> {
186   let PrintMethod = "printPCLabel";
187 }
188
189 // shifter_operand operands: so_reg and so_imm.
190 def so_reg : Operand<i32>,    // reg reg imm
191             ComplexPattern<i32, 3, "SelectShifterOperandReg",
192                             [shl,srl,sra,rotr]> {
193   let PrintMethod = "printSORegOperand";
194   let MIOperandInfo = (ops GPR, GPR, i32imm);
195 }
196
197 // so_imm - Match a 32-bit shifter_operand immediate operand, which is an
198 // 8-bit immediate rotated by an arbitrary number of bits.  so_imm values are
199 // represented in the imm field in the same 12-bit form that they are encoded
200 // into so_imm instructions: the 8-bit immediate is the least significant bits
201 // [bits 0-7], the 4-bit shift amount is the next 4 bits [bits 8-11].
202 def so_imm : Operand<i32>,
203              PatLeaf<(imm),
204                      [{ return ARM_AM::getSOImmVal(N->getZExtValue()) != -1; }],
205                      so_imm_XFORM> {
206   let PrintMethod = "printSOImmOperand";
207 }
208
209 // Break so_imm's up into two pieces.  This handles immediates with up to 16
210 // bits set in them.  This uses so_imm2part to match and so_imm2part_[12] to
211 // get the first/second pieces.
212 def so_imm2part : Operand<i32>,
213                   PatLeaf<(imm), [{
214       return ARM_AM::isSOImmTwoPartVal((unsigned)N->getZExtValue());
215     }]> {
216   let PrintMethod = "printSOImm2PartOperand";
217 }
218
219 def so_imm2part_1 : SDNodeXForm<imm, [{
220   unsigned V = ARM_AM::getSOImmTwoPartFirst((unsigned)N->getZExtValue());
221   return CurDAG->getTargetConstant(ARM_AM::getSOImmVal(V), MVT::i32);
222 }]>;
223
224 def so_imm2part_2 : SDNodeXForm<imm, [{
225   unsigned V = ARM_AM::getSOImmTwoPartSecond((unsigned)N->getZExtValue());
226   return CurDAG->getTargetConstant(ARM_AM::getSOImmVal(V), MVT::i32);
227 }]>;
228
229
230 // Define ARM specific addressing modes.
231
232 // addrmode2 := reg +/- reg shop imm
233 // addrmode2 := reg +/- imm12
234 //
235 def addrmode2 : Operand<i32>,
236                 ComplexPattern<i32, 3, "SelectAddrMode2", []> {
237   let PrintMethod = "printAddrMode2Operand";
238   let MIOperandInfo = (ops GPR:$base, GPR:$offsreg, i32imm:$offsimm);
239 }
240
241 def am2offset : Operand<i32>,
242                 ComplexPattern<i32, 2, "SelectAddrMode2Offset", []> {
243   let PrintMethod = "printAddrMode2OffsetOperand";
244   let MIOperandInfo = (ops GPR, i32imm);
245 }
246
247 // addrmode3 := reg +/- reg
248 // addrmode3 := reg +/- imm8
249 //
250 def addrmode3 : Operand<i32>,
251                 ComplexPattern<i32, 3, "SelectAddrMode3", []> {
252   let PrintMethod = "printAddrMode3Operand";
253   let MIOperandInfo = (ops GPR:$base, GPR:$offsreg, i32imm:$offsimm);
254 }
255
256 def am3offset : Operand<i32>,
257                 ComplexPattern<i32, 2, "SelectAddrMode3Offset", []> {
258   let PrintMethod = "printAddrMode3OffsetOperand";
259   let MIOperandInfo = (ops GPR, i32imm);
260 }
261
262 // addrmode4 := reg, <mode|W>
263 //
264 def addrmode4 : Operand<i32>,
265                 ComplexPattern<i32, 2, "", []> {
266   let PrintMethod = "printAddrMode4Operand";
267   let MIOperandInfo = (ops GPR, i32imm);
268 }
269
270 // addrmode5 := reg +/- imm8*4
271 //
272 def addrmode5 : Operand<i32>,
273                 ComplexPattern<i32, 2, "SelectAddrMode5", []> {
274   let PrintMethod = "printAddrMode5Operand";
275   let MIOperandInfo = (ops GPR, i32imm);
276 }
277
278 // addrmodepc := pc + reg
279 //
280 def addrmodepc : Operand<i32>,
281                  ComplexPattern<i32, 2, "SelectAddrModePC", []> {
282   let PrintMethod = "printAddrModePCOperand";
283   let MIOperandInfo = (ops GPR, i32imm);
284 }
285
286 // ARM Predicate operand. Default to 14 = always (AL). Second part is CC
287 // register whose default is 0 (no register).
288 def pred : PredicateOperand<OtherVT, (ops i32imm, CCR),
289                                      (ops (i32 14), (i32 zero_reg))> {
290   let PrintMethod = "printPredicateOperand";
291 }
292
293 // Conditional code result for instructions whose 's' bit is set, e.g. subs.
294 //
295 def cc_out : OptionalDefOperand<OtherVT, (ops CCR), (ops (i32 zero_reg))> {
296   let PrintMethod = "printSBitModifierOperand";
297 }
298
299 //===----------------------------------------------------------------------===//
300 // ARM Instruction flags.  These need to match ARMInstrInfo.h.
301 //
302
303 // Addressing mode.
304 class AddrMode<bits<4> val> {
305   bits<4> Value = val;
306 }
307 def AddrModeNone : AddrMode<0>;
308 def AddrMode1    : AddrMode<1>;
309 def AddrMode2    : AddrMode<2>;
310 def AddrMode3    : AddrMode<3>;
311 def AddrMode4    : AddrMode<4>;
312 def AddrMode5    : AddrMode<5>;
313 def AddrModeT1   : AddrMode<6>;
314 def AddrModeT2   : AddrMode<7>;
315 def AddrModeT4   : AddrMode<8>;
316 def AddrModeTs   : AddrMode<9>;
317
318 // Instruction size.
319 class SizeFlagVal<bits<3> val> {
320   bits<3> Value = val;
321 }
322 def SizeInvalid  : SizeFlagVal<0>;  // Unset.
323 def SizeSpecial  : SizeFlagVal<1>;  // Pseudo or special.
324 def Size8Bytes   : SizeFlagVal<2>;
325 def Size4Bytes   : SizeFlagVal<3>;
326 def Size2Bytes   : SizeFlagVal<4>;
327
328 // Load / store index mode.
329 class IndexMode<bits<2> val> {
330   bits<2> Value = val;
331 }
332 def IndexModeNone : IndexMode<0>;
333 def IndexModePre  : IndexMode<1>;
334 def IndexModePost : IndexMode<2>;
335
336 //===----------------------------------------------------------------------===//
337
338 include "ARMInstrFormats.td"
339
340 //===----------------------------------------------------------------------===//
341 // Multiclass helpers...
342 //
343
344 /// AsI1_bin_irs - Defines a set of (op r, {so_imm|r|so_reg}) patterns for a
345 /// binop that produces a value.
346 multiclass AsI1_bin_irs<bits<4> opcod, string opc, PatFrag opnode> {
347   def ri : AsI1<opcod, (outs GPR:$dst), (ins GPR:$a, so_imm:$b), DPFrm,
348                opc, " $dst, $a, $b",
349                [(set GPR:$dst, (opnode GPR:$a, so_imm:$b))]>;
350   def rr : AsI1<opcod, (outs GPR:$dst), (ins GPR:$a, GPR:$b), DPFrm,
351                opc, " $dst, $a, $b",
352                [(set GPR:$dst, (opnode GPR:$a, GPR:$b))]>;
353   def rs : AsI1<opcod, (outs GPR:$dst), (ins GPR:$a, so_reg:$b), DPSoRegFrm,
354                opc, " $dst, $a, $b",
355                [(set GPR:$dst, (opnode GPR:$a, so_reg:$b))]>;
356 }
357
358 /// ASI1_bin_s_irs - Similar to AsI1_bin_irs except it sets the 's' bit so the
359 /// instruction modifies the CSPR register.
360 let Defs = [CPSR] in {
361 multiclass ASI1_bin_s_irs<bits<4> opcod, string opc, PatFrag opnode> {
362   def ri : AI1<opcod, (outs GPR:$dst), (ins GPR:$a, so_imm:$b), DPFrm,
363                opc, "s $dst, $a, $b",
364                [(set GPR:$dst, (opnode GPR:$a, so_imm:$b))]>;
365   def rr : AI1<opcod, (outs GPR:$dst), (ins GPR:$a, GPR:$b), DPFrm,
366                opc, "s $dst, $a, $b",
367                [(set GPR:$dst, (opnode GPR:$a, GPR:$b))]>;
368   def rs : AI1<opcod, (outs GPR:$dst), (ins GPR:$a, so_reg:$b), DPSoRegFrm,
369                opc, "s $dst, $a, $b",
370                [(set GPR:$dst, (opnode GPR:$a, so_reg:$b))]>;
371 }
372 }
373
374 /// AI1_cmp_irs - Defines a set of (op r, {so_imm|r|so_reg}) cmp / test
375 /// patterns. Similar to AsI1_bin_irs except the instruction does not produce
376 /// a explicit result, only implicitly set CPSR.
377 let Defs = [CPSR] in {
378 multiclass AI1_cmp_irs<bits<4> opcod, string opc, PatFrag opnode> {
379   def ri : AI1<opcod, (outs), (ins GPR:$a, so_imm:$b), DPFrm,
380                opc, " $a, $b",
381                [(opnode GPR:$a, so_imm:$b)]>;
382   def rr : AI1<opcod, (outs), (ins GPR:$a, GPR:$b), DPFrm,
383                opc, " $a, $b",
384                [(opnode GPR:$a, GPR:$b)]>;
385   def rs : AI1<opcod, (outs), (ins GPR:$a, so_reg:$b), DPSoRegFrm,
386                opc, " $a, $b",
387                [(opnode GPR:$a, so_reg:$b)]>;
388 }
389 }
390
391 /// AI_unary_rrot - A unary operation with two forms: one whose operand is a
392 /// register and one whose operand is a register rotated by 8/16/24.
393 multiclass AI_unary_rrot<bits<4> opcod, string opc, PatFrag opnode> {
394   def r     : AI<opcod, (outs GPR:$dst), (ins GPR:$Src), Pseudo,
395                  opc, " $dst, $Src",
396                  [(set GPR:$dst, (opnode GPR:$Src))]>, Requires<[IsARM, HasV6]>;
397   def r_rot : AI<opcod, (outs GPR:$dst), (ins GPR:$Src, i32imm:$rot), Pseudo,
398                  opc, " $dst, $Src, ror $rot",
399                  [(set GPR:$dst, (opnode (rotr GPR:$Src, rot_imm:$rot)))]>,
400               Requires<[IsARM, HasV6]>;
401 }
402
403 /// AI_bin_rrot - A binary operation with two forms: one whose operand is a
404 /// register and one whose operand is a register rotated by 8/16/24.
405 multiclass AI_bin_rrot<bits<4> opcod, string opc, PatFrag opnode> {
406   def rr     : AI<opcod, (outs GPR:$dst), (ins GPR:$LHS, GPR:$RHS),
407                   Pseudo, opc, " $dst, $LHS, $RHS",
408                   [(set GPR:$dst, (opnode GPR:$LHS, GPR:$RHS))]>,
409                   Requires<[IsARM, HasV6]>;
410   def rr_rot : AI<opcod, (outs GPR:$dst), (ins GPR:$LHS, GPR:$RHS, i32imm:$rot),
411                   Pseudo, opc, " $dst, $LHS, $RHS, ror $rot",
412                   [(set GPR:$dst, (opnode GPR:$LHS,
413                                           (rotr GPR:$RHS, rot_imm:$rot)))]>,
414                   Requires<[IsARM, HasV6]>;
415 }
416
417 /// AsXI1_bin_c_irs - Same as AsI1_bin_irs but without the predicate operand and
418 /// setting carry bit. But it can optionally set CPSR.
419 let Uses = [CPSR] in {
420 multiclass AsXI1_bin_c_irs<bits<4> opcod, string opc, PatFrag opnode> {
421   def ri : AXI1<opcod, (outs GPR:$dst), (ins GPR:$a, so_imm:$b, cc_out:$s),
422                 DPFrm, !strconcat(opc, "${s} $dst, $a, $b"),
423                [(set GPR:$dst, (opnode GPR:$a, so_imm:$b))]>;
424   def rr : AXI1<opcod, (outs GPR:$dst), (ins GPR:$a, GPR:$b, cc_out:$s),
425                 DPFrm, !strconcat(opc, "${s} $dst, $a, $b"),
426                [(set GPR:$dst, (opnode GPR:$a, GPR:$b))]>;
427   def rs : AXI1<opcod, (outs GPR:$dst), (ins GPR:$a, so_reg:$b, cc_out:$s),
428                 DPSoRegFrm, !strconcat(opc, "${s} $dst, $a, $b"),
429                [(set GPR:$dst, (opnode GPR:$a, so_reg:$b))]>;
430 }
431 }
432
433 //===----------------------------------------------------------------------===//
434 // Instructions
435 //===----------------------------------------------------------------------===//
436
437 //===----------------------------------------------------------------------===//
438 //  Miscellaneous Instructions.
439 //
440
441 /// CONSTPOOL_ENTRY - This instruction represents a floating constant pool in
442 /// the function.  The first operand is the ID# for this instruction, the second
443 /// is the index into the MachineConstantPool that this is, the third is the
444 /// size in bytes of this constant pool entry.
445 let isNotDuplicable = 1 in
446 def CONSTPOOL_ENTRY :
447 PseudoInst<(outs), (ins cpinst_operand:$instid, cpinst_operand:$cpidx,
448                         i32imm:$size),
449            "${instid:label} ${cpidx:cpentry}", []>;
450
451 let Defs = [SP], Uses = [SP] in {
452 def ADJCALLSTACKUP :
453 PseudoInst<(outs), (ins i32imm:$amt1, i32imm:$amt2, pred:$p),
454            "@ ADJCALLSTACKUP $amt1",
455            [(ARMcallseq_end timm:$amt1, timm:$amt2)]>;
456
457 def ADJCALLSTACKDOWN : 
458 PseudoInst<(outs), (ins i32imm:$amt, pred:$p),
459            "@ ADJCALLSTACKDOWN $amt",
460            [(ARMcallseq_start timm:$amt)]>;
461 }
462
463 def DWARF_LOC :
464 PseudoInst<(outs), (ins i32imm:$line, i32imm:$col, i32imm:$file),
465            ".loc $file, $line, $col",
466            [(dwarf_loc (i32 imm:$line), (i32 imm:$col), (i32 imm:$file))]>;
467
468 let isNotDuplicable = 1 in {
469 def PICADD : AXI1<0b0100, (outs GPR:$dst), (ins GPR:$a, pclabel:$cp, pred:$p),
470                   Pseudo, "$cp:\n\tadd$p $dst, pc, $a",
471                    [(set GPR:$dst, (ARMpic_add GPR:$a, imm:$cp))]>;
472
473 let AddedComplexity = 10 in {
474 let isSimpleLoad = 1 in
475 def PICLDR  : AXI2ldw<0x0, (outs GPR:$dst), (ins addrmodepc:$addr, pred:$p),
476                   Pseudo, "${addr:label}:\n\tldr$p $dst, $addr",
477                   [(set GPR:$dst, (load addrmodepc:$addr))]>;
478
479 def PICLDRH : AXI3ldh<0xB, (outs GPR:$dst), (ins addrmodepc:$addr, pred:$p),
480                   Pseudo, "${addr:label}:\n\tldr${p}h $dst, $addr",
481                   [(set GPR:$dst, (zextloadi16 addrmodepc:$addr))]>;
482
483 def PICLDRB : AXI2ldb<0x1, (outs GPR:$dst), (ins addrmodepc:$addr, pred:$p),
484                   Pseudo, "${addr:label}:\n\tldr${p}b $dst, $addr",
485                   [(set GPR:$dst, (zextloadi8 addrmodepc:$addr))]>;
486
487 def PICLDRSH : AXI3ldsh<0xE, (outs GPR:$dst), (ins addrmodepc:$addr, pred:$p),
488                   Pseudo, "${addr:label}:\n\tldr${p}sh $dst, $addr",
489                   [(set GPR:$dst, (sextloadi16 addrmodepc:$addr))]>;
490
491 def PICLDRSB : AXI3ldsb<0xD, (outs GPR:$dst), (ins addrmodepc:$addr, pred:$p),
492                   Pseudo, "${addr:label}:\n\tldr${p}sb $dst, $addr",
493                   [(set GPR:$dst, (sextloadi8 addrmodepc:$addr))]>;
494 }
495 let AddedComplexity = 10 in {
496 def PICSTR  : AXI2stw<0x0, (outs), (ins GPR:$src, addrmodepc:$addr, pred:$p),
497                Pseudo, "${addr:label}:\n\tstr$p $src, $addr",
498                [(store GPR:$src, addrmodepc:$addr)]>;
499
500 def PICSTRH : AXI3sth<0xB, (outs), (ins GPR:$src, addrmodepc:$addr, pred:$p),
501                Pseudo, "${addr:label}:\n\tstr${p}h $src, $addr",
502                [(truncstorei16 GPR:$src, addrmodepc:$addr)]>;
503
504 def PICSTRB : AXI2stb<0x1, (outs), (ins GPR:$src, addrmodepc:$addr, pred:$p),
505                Pseudo, "${addr:label}:\n\tstr${p}b $src, $addr",
506                [(truncstorei8 GPR:$src, addrmodepc:$addr)]>;
507 }
508 }
509
510 //===----------------------------------------------------------------------===//
511 //  Control Flow Instructions.
512 //
513
514 let isReturn = 1, isTerminator = 1 in
515   def BX_RET : AI<0x0, (outs), (ins), BranchMisc, "bx", " lr", [(ARMretflag)]> {
516   let Inst{7-4}   = 0b0001;
517   let Inst{19-8}  = 0b111111111111;
518   let Inst{27-20} = 0b00010010;
519 }
520
521 // FIXME: remove when we have a way to marking a MI with these properties.
522 // FIXME: $dst1 should be a def. But the extra ops must be in the end of the
523 // operand list.
524 let isReturn = 1, isTerminator = 1 in
525   def LDM_RET : AXI4ldpc<0x0, (outs),
526                     (ins addrmode4:$addr, pred:$p, reglist:$dst1, variable_ops),
527                     LdMulFrm, "ldm${p}${addr:submode} $addr, $dst1",
528                     []>;
529
530 let isCall = 1,
531   Defs = [R0, R1, R2, R3, R12, LR,
532           D0, D1, D2, D3, D4, D5, D6, D7, CPSR] in {
533   def BL  : ABLI<0xB, (outs), (ins i32imm:$func, variable_ops), Branch,
534                 "bl ${func:call}",
535                 [(ARMcall tglobaladdr:$func)]>;
536
537   def BL_pred : ABLpredI<0xB, (outs), (ins i32imm:$func, variable_ops), Branch,
538                    "bl", " ${func:call}",
539                    [(ARMcall_pred tglobaladdr:$func)]>;
540
541   // ARMv5T and above
542   def BLX : AXI<0x0, (outs), (ins GPR:$func, variable_ops), BranchMisc,
543                 "blx $func",
544                 [(ARMcall GPR:$func)]>, Requires<[IsARM, HasV5T]> {
545     let Inst{7-4}   = 0b0011;
546     let Inst{19-8}  = 0b111111111111;
547     let Inst{27-20} = 0b00010010;
548   }
549
550   let Uses = [LR] in {
551     // ARMv4T
552     def BX : AXIx2<0x0, (outs), (ins GPR:$func, variable_ops),
553                    BranchMisc, "mov lr, pc\n\tbx $func",
554                    [(ARMcall_nolink GPR:$func)]>;
555   }
556 }
557
558 let isBranch = 1, isTerminator = 1 in {
559   // B is "predicable" since it can be xformed into a Bcc.
560   let isBarrier = 1 in {
561     let isPredicable = 1 in
562     def B : ABI<0xA, (outs), (ins brtarget:$target), Branch, "b $target",
563                 [(br bb:$target)]>;
564
565   let isNotDuplicable = 1, isIndirectBranch = 1 in {
566   def BR_JTr : JTI<0b1101, (outs), (ins GPR:$target, jtblock_operand:$jt, i32imm:$id),
567                     "mov pc, $target \n$jt",
568                     [(ARMbrjt GPR:$target, tjumptable:$jt, imm:$id)]>;
569   def BR_JTm : JTI2<0x0, (outs), (ins addrmode2:$target, jtblock_operand:$jt, i32imm:$id),
570                      "ldr pc, $target \n$jt",
571                      [(ARMbrjt (i32 (load addrmode2:$target)), tjumptable:$jt,
572                        imm:$id)]>;
573   def BR_JTadd : JTI1<0b0100, (outs), (ins GPR:$target, GPR:$idx, jtblock_operand:$jt,
574                        i32imm:$id),
575                        "add pc, $target, $idx \n$jt",
576                        [(ARMbrjt (add GPR:$target, GPR:$idx), tjumptable:$jt,
577                          imm:$id)]>;
578   }
579   }
580
581   // FIXME: should be able to write a pattern for ARMBrcond, but can't use
582   // a two-value operand where a dag node expects two operands. :( 
583   def Bcc : ABccI<0xA, (outs), (ins brtarget:$target), Branch,
584                "b", " $target",
585                [/*(ARMbrcond bb:$target, imm:$cc, CCR:$ccr)*/]>;
586 }
587
588 //===----------------------------------------------------------------------===//
589 //  Load / store Instructions.
590 //
591
592 // Load
593 let isSimpleLoad = 1 in 
594 def LDR  : AI2ldw<0x0, (outs GPR:$dst), (ins addrmode2:$addr), LdFrm,
595                "ldr", " $dst, $addr",
596                [(set GPR:$dst, (load addrmode2:$addr))]>;
597
598 // Special LDR for loads from non-pc-relative constpools.
599 let isSimpleLoad = 1, mayLoad = 1, isReMaterializable = 1 in
600 def LDRcp : AI2ldw<0x0, (outs GPR:$dst), (ins addrmode2:$addr), LdFrm,
601                  "ldr", " $dst, $addr", []>;
602
603 // Loads with zero extension
604 def LDRH  : AI3ldh<0xB, (outs GPR:$dst), (ins addrmode3:$addr), LdMiscFrm,
605                  "ldr", "h $dst, $addr",
606                 [(set GPR:$dst, (zextloadi16 addrmode3:$addr))]>;
607
608 def LDRB  : AI2ldb<0x1, (outs GPR:$dst), (ins addrmode2:$addr), LdFrm,
609                  "ldr", "b $dst, $addr",
610                 [(set GPR:$dst, (zextloadi8 addrmode2:$addr))]>;
611
612 // Loads with sign extension
613 def LDRSH : AI3ldsh<0xE, (outs GPR:$dst), (ins addrmode3:$addr), LdMiscFrm,
614                  "ldr", "sh $dst, $addr",
615                 [(set GPR:$dst, (sextloadi16 addrmode3:$addr))]>;
616
617 def LDRSB : AI3ldsb<0xD, (outs GPR:$dst), (ins addrmode3:$addr), LdMiscFrm,
618                  "ldr", "sb $dst, $addr",
619                 [(set GPR:$dst, (sextloadi8 addrmode3:$addr))]>;
620
621 let mayLoad = 1 in {
622 // Load doubleword
623 def LDRD  : AI3ldd<0xD, (outs GPR:$dst), (ins addrmode3:$addr), LdMiscFrm,
624                  "ldr", "d $dst, $addr",
625                 []>, Requires<[IsARM, HasV5T]>;
626
627 // Indexed loads
628 def LDR_PRE  : AI2ldwpr<0x0, (outs GPR:$dst, GPR:$base_wb),
629                      (ins addrmode2:$addr), LdFrm,
630                      "ldr", " $dst, $addr!", "$addr.base = $base_wb", []>;
631
632 def LDR_POST : AI2ldwpo<0x0, (outs GPR:$dst, GPR:$base_wb),
633                      (ins GPR:$base, am2offset:$offset), LdFrm,
634                      "ldr", " $dst, [$base], $offset", "$base = $base_wb", []>;
635
636 def LDRH_PRE  : AI3ldhpr<0xB, (outs GPR:$dst, GPR:$base_wb),
637                      (ins addrmode3:$addr), LdMiscFrm,
638                      "ldr", "h $dst, $addr!", "$addr.base = $base_wb", []>;
639
640 def LDRH_POST : AI3ldhpo<0xB, (outs GPR:$dst, GPR:$base_wb),
641                      (ins GPR:$base,am3offset:$offset), LdMiscFrm,
642                      "ldr", "h $dst, [$base], $offset", "$base = $base_wb", []>;
643
644 def LDRB_PRE  : AI2ldbpr<0x1, (outs GPR:$dst, GPR:$base_wb),
645                      (ins addrmode2:$addr), LdFrm,
646                      "ldr", "b $dst, $addr!", "$addr.base = $base_wb", []>;
647
648 def LDRB_POST : AI2ldbpo<0x1, (outs GPR:$dst, GPR:$base_wb),
649                      (ins GPR:$base,am2offset:$offset), LdFrm,
650                      "ldr", "b $dst, [$base], $offset", "$base = $base_wb", []>;
651
652 def LDRSH_PRE : AI3ldshpr<0xE, (outs GPR:$dst, GPR:$base_wb),
653                       (ins addrmode3:$addr), LdMiscFrm,
654                       "ldr", "sh $dst, $addr!", "$addr.base = $base_wb", []>;
655
656 def LDRSH_POST: AI3ldshpo<0xE, (outs GPR:$dst, GPR:$base_wb),
657                       (ins GPR:$base,am3offset:$offset), LdMiscFrm,
658                       "ldr", "sh $dst, [$base], $offset", "$base = $base_wb", []>;
659
660 def LDRSB_PRE : AI3ldsbpr<0xD, (outs GPR:$dst, GPR:$base_wb),
661                       (ins addrmode3:$addr), LdMiscFrm,
662                       "ldr", "sb $dst, $addr!", "$addr.base = $base_wb", []>;
663
664 def LDRSB_POST: AI3ldsbpo<0xD, (outs GPR:$dst, GPR:$base_wb),
665                       (ins GPR:$base,am3offset:$offset), LdMiscFrm,
666                       "ldr", "sb $dst, [$base], $offset", "$base = $base_wb", []>;
667 }
668
669 // Store
670 def STR  : AI2stw<0x0, (outs), (ins GPR:$src, addrmode2:$addr), StFrm,
671                "str", " $src, $addr",
672                [(store GPR:$src, addrmode2:$addr)]>;
673
674 // Stores with truncate
675 def STRH : AI3sth<0xB, (outs), (ins GPR:$src, addrmode3:$addr), StMiscFrm,
676                "str", "h $src, $addr",
677                [(truncstorei16 GPR:$src, addrmode3:$addr)]>;
678
679 def STRB : AI2stb<0x1, (outs), (ins GPR:$src, addrmode2:$addr), StFrm,
680                "str", "b $src, $addr",
681                [(truncstorei8 GPR:$src, addrmode2:$addr)]>;
682
683 // Store doubleword
684 let mayStore = 1 in
685 def STRD : AI3std<0xF, (outs), (ins GPR:$src, addrmode3:$addr), StMiscFrm,
686                "str", "d $src, $addr",
687                []>, Requires<[IsARM, HasV5T]>;
688
689 // Indexed stores
690 def STR_PRE  : AI2stwpr<0x0, (outs GPR:$base_wb),
691                      (ins GPR:$src, GPR:$base, am2offset:$offset), StFrm,
692                     "str", " $src, [$base, $offset]!", "$base = $base_wb",
693                     [(set GPR:$base_wb,
694                       (pre_store GPR:$src, GPR:$base, am2offset:$offset))]>;
695
696 def STR_POST : AI2stwpo<0x0, (outs GPR:$base_wb),
697                      (ins GPR:$src, GPR:$base,am2offset:$offset), StFrm,
698                     "str", " $src, [$base], $offset", "$base = $base_wb",
699                     [(set GPR:$base_wb,
700                       (post_store GPR:$src, GPR:$base, am2offset:$offset))]>;
701
702 def STRH_PRE : AI3sthpr<0xB, (outs GPR:$base_wb),
703                      (ins GPR:$src, GPR:$base,am3offset:$offset), StMiscFrm,
704                      "str", "h $src, [$base, $offset]!", "$base = $base_wb",
705                     [(set GPR:$base_wb,
706                       (pre_truncsti16 GPR:$src, GPR:$base,am3offset:$offset))]>;
707
708 def STRH_POST: AI3sthpo<0xB, (outs GPR:$base_wb),
709                      (ins GPR:$src, GPR:$base,am3offset:$offset), StMiscFrm,
710                      "str", "h $src, [$base], $offset", "$base = $base_wb",
711                     [(set GPR:$base_wb, (post_truncsti16 GPR:$src,
712                                          GPR:$base, am3offset:$offset))]>;
713
714 def STRB_PRE : AI2stbpr<0x1, (outs GPR:$base_wb),
715                      (ins GPR:$src, GPR:$base,am2offset:$offset), StFrm,
716                      "str", "b $src, [$base, $offset]!", "$base = $base_wb",
717                     [(set GPR:$base_wb, (pre_truncsti8 GPR:$src,
718                                          GPR:$base, am2offset:$offset))]>;
719
720 def STRB_POST: AI2stbpo<0x1, (outs GPR:$base_wb),
721                      (ins GPR:$src, GPR:$base,am2offset:$offset), StFrm,
722                      "str", "b $src, [$base], $offset", "$base = $base_wb",
723                     [(set GPR:$base_wb, (post_truncsti8 GPR:$src,
724                                          GPR:$base, am2offset:$offset))]>;
725
726 //===----------------------------------------------------------------------===//
727 //  Load / store multiple Instructions.
728 //
729
730 // FIXME: $dst1 should be a def.
731 let mayLoad = 1 in
732 def LDM : AXI4ld<0x0, (outs),
733                (ins addrmode4:$addr, pred:$p, reglist:$dst1, variable_ops),
734                LdMulFrm, "ldm${p}${addr:submode} $addr, $dst1",
735                []>;
736
737 let mayStore = 1 in
738 def STM : AXI4st<0x0, (outs),
739                (ins addrmode4:$addr, pred:$p, reglist:$src1, variable_ops),
740                StMulFrm, "stm${p}${addr:submode} $addr, $src1",
741                []>;
742
743 //===----------------------------------------------------------------------===//
744 //  Move Instructions.
745 //
746
747 def MOVr : AsI1<0b1101, (outs GPR:$dst), (ins GPR:$src), DPFrm,
748                  "mov", " $dst, $src", []>, UnaryDP;
749 def MOVs : AsI1<0b1101, (outs GPR:$dst), (ins so_reg:$src), DPSoRegFrm,
750                  "mov", " $dst, $src", [(set GPR:$dst, so_reg:$src)]>, UnaryDP;
751
752 let isReMaterializable = 1 in
753 def MOVi : AsI1<0b1101, (outs GPR:$dst), (ins so_imm:$src), DPFrm,
754                  "mov", " $dst, $src", [(set GPR:$dst, so_imm:$src)]>, UnaryDP;
755
756 def MOVrx : AsI1<0b1101, (outs GPR:$dst), (ins GPR:$src), DPFrm,
757                  "mov", " $dst, $src, rrx",
758                  [(set GPR:$dst, (ARMrrx GPR:$src))]>, UnaryDP;
759
760 // These aren't really mov instructions, but we have to define them this way
761 // due to flag operands.
762
763 let Defs = [CPSR] in {
764 def MOVsrl_flag : AI1<0b1101, (outs GPR:$dst), (ins GPR:$src), DPFrm,
765                       "mov", "s $dst, $src, lsr #1",
766                       [(set GPR:$dst, (ARMsrl_flag GPR:$src))]>, UnaryDP;
767 def MOVsra_flag : AI1<0b1101, (outs GPR:$dst), (ins GPR:$src), DPFrm,
768                       "mov", "s $dst, $src, asr #1",
769                       [(set GPR:$dst, (ARMsra_flag GPR:$src))]>, UnaryDP;
770 }
771
772 //===----------------------------------------------------------------------===//
773 //  Extend Instructions.
774 //
775
776 // Sign extenders
777
778 defm SXTB  : AI_unary_rrot<0x0, "sxtb", UnOpFrag<(sext_inreg node:$Src, i8)>>;
779 defm SXTH  : AI_unary_rrot<0x0, "sxth", UnOpFrag<(sext_inreg node:$Src, i16)>>;
780
781 defm SXTAB : AI_bin_rrot<0x0, "sxtab",
782                         BinOpFrag<(add node:$LHS, (sext_inreg node:$RHS, i8))>>;
783 defm SXTAH : AI_bin_rrot<0x0, "sxtah",
784                         BinOpFrag<(add node:$LHS, (sext_inreg node:$RHS,i16))>>;
785
786 // TODO: SXT(A){B|H}16
787
788 // Zero extenders
789
790 let AddedComplexity = 16 in {
791 defm UXTB   : AI_unary_rrot<0x0, "uxtb"  , UnOpFrag<(and node:$Src, 0x000000FF)>>;
792 defm UXTH   : AI_unary_rrot<0x0, "uxth"  , UnOpFrag<(and node:$Src, 0x0000FFFF)>>;
793 defm UXTB16 : AI_unary_rrot<0x0, "uxtb16", UnOpFrag<(and node:$Src, 0x00FF00FF)>>;
794
795 def : ARMV6Pat<(and (shl GPR:$Src, 8), 0xFF00FF),
796                (UXTB16r_rot GPR:$Src, 24)>;
797 def : ARMV6Pat<(and (srl GPR:$Src, 8), 0xFF00FF),
798                (UXTB16r_rot GPR:$Src, 8)>;
799
800 defm UXTAB : AI_bin_rrot<0x0, "uxtab",
801                         BinOpFrag<(add node:$LHS, (and node:$RHS, 0x00FF))>>;
802 defm UXTAH : AI_bin_rrot<0x0, "uxtah",
803                         BinOpFrag<(add node:$LHS, (and node:$RHS, 0xFFFF))>>;
804 }
805
806 // This isn't safe in general, the add is two 16-bit units, not a 32-bit add.
807 //defm UXTAB16 : xxx<"uxtab16", 0xff00ff>;
808
809 // TODO: UXT(A){B|H}16
810
811 //===----------------------------------------------------------------------===//
812 //  Arithmetic Instructions.
813 //
814
815 defm ADD  : AsI1_bin_irs<0b0100, "add",
816                          BinOpFrag<(add  node:$LHS, node:$RHS)>>;
817 defm SUB  : AsI1_bin_irs<0b0010, "sub",
818                          BinOpFrag<(sub  node:$LHS, node:$RHS)>>;
819
820 // ADD and SUB with 's' bit set.
821 defm ADDS : ASI1_bin_s_irs<0b0100, "add",
822                            BinOpFrag<(addc node:$LHS, node:$RHS)>>;
823 defm SUBS : ASI1_bin_s_irs<0b0010, "sub",
824                            BinOpFrag<(subc node:$LHS, node:$RHS)>>;
825
826 // FIXME: Do not allow ADC / SBC to be predicated for now.
827 defm ADC  : AsXI1_bin_c_irs<0b0101, "adc",
828                             BinOpFrag<(adde node:$LHS, node:$RHS)>>;
829 defm SBC  : AsXI1_bin_c_irs<0b0110, "sbc",
830                             BinOpFrag<(sube node:$LHS, node:$RHS)>>;
831
832 // These don't define reg/reg forms, because they are handled above.
833 def RSBri : AsI1<0b0011, (outs GPR:$dst), (ins GPR:$a, so_imm:$b), DPFrm,
834                   "rsb", " $dst, $a, $b",
835                   [(set GPR:$dst, (sub so_imm:$b, GPR:$a))]>;
836
837 def RSBrs : AsI1<0b0011, (outs GPR:$dst), (ins GPR:$a, so_reg:$b), DPSoRegFrm,
838                   "rsb", " $dst, $a, $b",
839                   [(set GPR:$dst, (sub so_reg:$b, GPR:$a))]>;
840
841 // RSB with 's' bit set.
842 let Defs = [CPSR] in {
843 def RSBSri : AI1<0b0011, (outs GPR:$dst), (ins GPR:$a, so_imm:$b), DPFrm,
844                  "rsb", "s $dst, $a, $b",
845                  [(set GPR:$dst, (subc so_imm:$b, GPR:$a))]>;
846 def RSBSrs : AI1<0b0011, (outs GPR:$dst), (ins GPR:$a, so_reg:$b), DPSoRegFrm,
847                  "rsb", "s $dst, $a, $b",
848                  [(set GPR:$dst, (subc so_reg:$b, GPR:$a))]>;
849 }
850
851 // FIXME: Do not allow RSC to be predicated for now. But they can set CPSR.
852 let Uses = [CPSR] in {
853 def RSCri : AXI1<0b0111, (outs GPR:$dst), (ins GPR:$a, so_imm:$b, cc_out:$s),
854                  DPFrm, "rsc${s} $dst, $a, $b",
855                  [(set GPR:$dst, (sube so_imm:$b, GPR:$a))]>;
856 def RSCrs : AXI1<0b0111, (outs GPR:$dst), (ins GPR:$a, so_reg:$b, cc_out:$s),
857                  DPSoRegFrm, "rsc${s} $dst, $a, $b",
858                  [(set GPR:$dst, (sube so_reg:$b, GPR:$a))]>;
859 }
860
861 // (sub X, imm) gets canonicalized to (add X, -imm).  Match this form.
862 def : ARMPat<(add    GPR:$src, so_imm_neg:$imm),
863              (SUBri  GPR:$src, so_imm_neg:$imm)>;
864
865 //def : ARMPat<(addc   GPR:$src, so_imm_neg:$imm),
866 //             (SUBSri GPR:$src, so_imm_neg:$imm)>;
867 //def : ARMPat<(adde   GPR:$src, so_imm_neg:$imm),
868 //             (SBCri  GPR:$src, so_imm_neg:$imm)>;
869
870 // Note: These are implemented in C++ code, because they have to generate
871 // ADD/SUBrs instructions, which use a complex pattern that a xform function
872 // cannot produce.
873 // (mul X, 2^n+1) -> (add (X << n), X)
874 // (mul X, 2^n-1) -> (rsb X, (X << n))
875
876
877 //===----------------------------------------------------------------------===//
878 //  Bitwise Instructions.
879 //
880
881 defm AND   : AsI1_bin_irs<0b0000, "and",
882                           BinOpFrag<(and node:$LHS, node:$RHS)>>;
883 defm ORR   : AsI1_bin_irs<0b1100, "orr",
884                           BinOpFrag<(or  node:$LHS, node:$RHS)>>;
885 defm EOR   : AsI1_bin_irs<0b0001, "eor",
886                           BinOpFrag<(xor node:$LHS, node:$RHS)>>;
887 defm BIC   : AsI1_bin_irs<0b1110, "bic",
888                           BinOpFrag<(and node:$LHS, (not node:$RHS))>>;
889
890 def  MVNr  : AsI1<0b1111, (outs GPR:$dst), (ins GPR:$src), DPFrm,
891                   "mvn", " $dst, $src",
892                   [(set GPR:$dst, (not GPR:$src))]>, UnaryDP;
893 def  MVNs  : AsI1<0b1111, (outs GPR:$dst), (ins so_reg:$src), DPSoRegFrm,
894                   "mvn", " $dst, $src",
895                   [(set GPR:$dst, (not so_reg:$src))]>, UnaryDP;
896 let isReMaterializable = 1 in
897 def  MVNi  : AsI1<0b1111, (outs GPR:$dst), (ins so_imm:$imm), DPFrm,
898                   "mvn", " $dst, $imm",
899                   [(set GPR:$dst, so_imm_not:$imm)]>,UnaryDP;
900
901 def : ARMPat<(and   GPR:$src, so_imm_not:$imm),
902              (BICri GPR:$src, so_imm_not:$imm)>;
903
904 //===----------------------------------------------------------------------===//
905 //  Multiply Instructions.
906 //
907
908 def MUL   : AsMul1I<0b0000000, (outs GPR:$dst), (ins GPR:$a, GPR:$b),
909                  "mul", " $dst, $a, $b",
910                  [(set GPR:$dst, (mul GPR:$a, GPR:$b))]>;
911
912 def MLA   : AsMul1I<0b0000001, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$c),
913                  "mla", " $dst, $a, $b, $c",
914                  [(set GPR:$dst, (add (mul GPR:$a, GPR:$b), GPR:$c))]>;
915
916 // Extra precision multiplies with low / high results
917 def SMULL : AsMul1I<0b0000110, (outs GPR:$ldst, GPR:$hdst),
918                                (ins GPR:$a, GPR:$b),
919                     "smull", " $ldst, $hdst, $a, $b", []>;
920
921 def UMULL : AsMul1I<0b0000100, (outs GPR:$ldst, GPR:$hdst),
922                                (ins GPR:$a, GPR:$b),
923                     "umull", " $ldst, $hdst, $a, $b", []>;
924
925 // Multiply + accumulate
926 def SMLAL : AsMul1I<0b0000111, (outs GPR:$ldst, GPR:$hdst),
927                                (ins GPR:$a, GPR:$b),
928                     "smlal", " $ldst, $hdst, $a, $b", []>;
929
930 def UMLAL : AsMul1I<0b0000101, (outs GPR:$ldst, GPR:$hdst),
931                                (ins GPR:$a, GPR:$b),
932                     "umlal", " $ldst, $hdst, $a, $b", []>;
933
934 def UMAAL : AMul1I <0b0000010, (outs GPR:$ldst, GPR:$hdst),
935                                (ins GPR:$a, GPR:$b),
936                     "umaal", " $ldst, $hdst, $a, $b", []>,
937                     Requires<[IsARM, HasV6]>;
938
939 // Most significant word multiply
940 def SMMUL : AMul2I <0b0111010, (outs GPR:$dst), (ins GPR:$a, GPR:$b),
941                "smmul", " $dst, $a, $b",
942                [(set GPR:$dst, (mulhs GPR:$a, GPR:$b))]>,
943             Requires<[IsARM, HasV6]> {
944   let Inst{7-4}   = 0b0001;
945   let Inst{15-12} = 0b1111;
946 }
947
948 def SMMLA : AMul2I <0b0111010, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$c),
949                "smmla", " $dst, $a, $b, $c",
950                [(set GPR:$dst, (add (mulhs GPR:$a, GPR:$b), GPR:$c))]>,
951             Requires<[IsARM, HasV6]> {
952   let Inst{7-4}   = 0b0001;
953 }
954
955
956 def SMMLS : AMul2I <0b0111010, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$c),
957                "smmls", " $dst, $a, $b, $c",
958                [(set GPR:$dst, (sub GPR:$c, (mulhs GPR:$a, GPR:$b)))]>,
959             Requires<[IsARM, HasV6]> {
960   let Inst{7-4}   = 0b1101;
961 }
962
963 // FIXME: encoding
964 multiclass AI_smul<string opc, PatFrag opnode> {
965   def BB : AI<0x8, (outs GPR:$dst), (ins GPR:$a, GPR:$b), MulSMUL,
966               !strconcat(opc, "bb"), " $dst, $a, $b",
967               [(set GPR:$dst, (opnode (sext_inreg GPR:$a, i16),
968                                       (sext_inreg GPR:$b, i16)))]>,
969            Requires<[IsARM, HasV5TE]>;
970
971   def BT : AI<0xC, (outs GPR:$dst), (ins GPR:$a, GPR:$b), MulSMUL,
972               !strconcat(opc, "bt"), " $dst, $a, $b",
973               [(set GPR:$dst, (opnode (sext_inreg GPR:$a, i16),
974                                       (sra GPR:$b, 16)))]>,
975            Requires<[IsARM, HasV5TE]>;
976
977   def TB : AI<0xA, (outs GPR:$dst), (ins GPR:$a, GPR:$b), MulSMUL,
978               !strconcat(opc, "tb"), " $dst, $a, $b",
979               [(set GPR:$dst, (opnode (sra GPR:$a, 16),
980                                       (sext_inreg GPR:$b, i16)))]>,
981            Requires<[IsARM, HasV5TE]>;
982
983   def TT : AI<0xE, (outs GPR:$dst), (ins GPR:$a, GPR:$b), MulSMUL,
984               !strconcat(opc, "tt"), " $dst, $a, $b",
985               [(set GPR:$dst, (opnode (sra GPR:$a, 16),
986                                       (sra GPR:$b, 16)))]>,
987             Requires<[IsARM, HasV5TE]>;
988
989   def WB : AI<0xA, (outs GPR:$dst), (ins GPR:$a, GPR:$b), MulSMULW,
990               !strconcat(opc, "wb"), " $dst, $a, $b",
991               [(set GPR:$dst, (sra (opnode GPR:$a,
992                                     (sext_inreg GPR:$b, i16)), 16))]>,
993            Requires<[IsARM, HasV5TE]>;
994
995   def WT : AI<0xE, (outs GPR:$dst), (ins GPR:$a, GPR:$b), MulSMULW,
996               !strconcat(opc, "wt"), " $dst, $a, $b",
997               [(set GPR:$dst, (sra (opnode GPR:$a,
998                                     (sra GPR:$b, 16)), 16))]>,
999             Requires<[IsARM, HasV5TE]>;
1000 }
1001
1002
1003 // FIXME: encoding
1004 multiclass AI_smla<string opc, PatFrag opnode> {
1005   def BB : AI<0x8, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$acc), MulSMLA,
1006               !strconcat(opc, "bb"), " $dst, $a, $b, $acc",
1007               [(set GPR:$dst, (add GPR:$acc,
1008                                (opnode (sext_inreg GPR:$a, i16),
1009                                        (sext_inreg GPR:$b, i16))))]>,
1010            Requires<[IsARM, HasV5TE]>;
1011
1012   def BT : AI<0xC, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$acc), MulSMLA,
1013               !strconcat(opc, "bt"), " $dst, $a, $b, $acc",
1014               [(set GPR:$dst, (add GPR:$acc, (opnode (sext_inreg GPR:$a, i16),
1015                                                      (sra GPR:$b, 16))))]>,
1016            Requires<[IsARM, HasV5TE]>;
1017
1018   def TB : AI<0xA, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$acc), MulSMLA,
1019               !strconcat(opc, "tb"), " $dst, $a, $b, $acc",
1020               [(set GPR:$dst, (add GPR:$acc, (opnode (sra GPR:$a, 16),
1021                                                  (sext_inreg GPR:$b, i16))))]>,
1022            Requires<[IsARM, HasV5TE]>;
1023
1024   def TT : AI<0xE, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$acc), MulSMLA,
1025               !strconcat(opc, "tt"), " $dst, $a, $b, $acc",
1026               [(set GPR:$dst, (add GPR:$acc, (opnode (sra GPR:$a, 16),
1027                                                      (sra GPR:$b, 16))))]>,
1028             Requires<[IsARM, HasV5TE]>;
1029
1030   def WB : AI<0xA, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$acc), MulSMLAW,
1031               !strconcat(opc, "wb"), " $dst, $a, $b, $acc",
1032               [(set GPR:$dst, (add GPR:$acc, (sra (opnode GPR:$a,
1033                                             (sext_inreg GPR:$b, i16)), 16)))]>,
1034            Requires<[IsARM, HasV5TE]>;
1035
1036   def WT : AI<0xE, (outs GPR:$dst), (ins GPR:$a, GPR:$b, GPR:$acc), MulSMLAW,
1037               !strconcat(opc, "wt"), " $dst, $a, $b, $acc",
1038               [(set GPR:$dst, (add GPR:$acc, (sra (opnode GPR:$a,
1039                                                    (sra GPR:$b, 16)), 16)))]>,
1040             Requires<[IsARM, HasV5TE]>;
1041 }
1042
1043 // FIXME: encoding
1044 defm SMUL : AI_smul<"smul", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
1045 // FIXME: encoding
1046 defm SMLA : AI_smla<"smla", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
1047
1048 // TODO: Halfword multiple accumulate long: SMLAL<x><y>
1049 // TODO: Dual halfword multiple: SMUAD, SMUSD, SMLAD, SMLSD, SMLALD, SMLSLD
1050
1051 //===----------------------------------------------------------------------===//
1052 //  Misc. Arithmetic Instructions.
1053 //
1054
1055 def CLZ  : AI<0x0, (outs GPR:$dst), (ins GPR:$src), ArithMisc,
1056               "clz", " $dst, $src",
1057               [(set GPR:$dst, (ctlz GPR:$src))]>, Requires<[IsARM, HasV5T]>;
1058
1059 def REV  : AI<0x0, (outs GPR:$dst), (ins GPR:$src), ArithMisc,
1060               "rev", " $dst, $src",
1061               [(set GPR:$dst, (bswap GPR:$src))]>, Requires<[IsARM, HasV6]>;
1062
1063 def REV16 : AI<0x0, (outs GPR:$dst), (ins GPR:$src), ArithMisc,
1064                "rev16", " $dst, $src",
1065                [(set GPR:$dst,
1066                    (or (and (srl GPR:$src, 8), 0xFF),
1067                        (or (and (shl GPR:$src, 8), 0xFF00),
1068                            (or (and (srl GPR:$src, 8), 0xFF0000),
1069                                (and (shl GPR:$src, 8), 0xFF000000)))))]>,
1070                Requires<[IsARM, HasV6]>;
1071
1072 def REVSH : AI<0x0, (outs GPR:$dst), (ins GPR:$src), ArithMisc,
1073                "revsh", " $dst, $src",
1074                [(set GPR:$dst,
1075                   (sext_inreg
1076                     (or (srl (and GPR:$src, 0xFF00), 8),
1077                         (shl GPR:$src, 8)), i16))]>,
1078                Requires<[IsARM, HasV6]>;
1079
1080 def PKHBT : AI<0x0, (outs GPR:$dst), (ins GPR:$src1, GPR:$src2, i32imm:$shamt),
1081                Pseudo, "pkhbt", " $dst, $src1, $src2, LSL $shamt",
1082                [(set GPR:$dst, (or (and GPR:$src1, 0xFFFF),
1083                                    (and (shl GPR:$src2, (i32 imm:$shamt)),
1084                                         0xFFFF0000)))]>,
1085                Requires<[IsARM, HasV6]>;
1086
1087 // Alternate cases for PKHBT where identities eliminate some nodes.
1088 def : ARMV6Pat<(or (and GPR:$src1, 0xFFFF), (and GPR:$src2, 0xFFFF0000)),
1089                (PKHBT GPR:$src1, GPR:$src2, 0)>;
1090 def : ARMV6Pat<(or (and GPR:$src1, 0xFFFF), (shl GPR:$src2, imm16_31:$shamt)),
1091                (PKHBT GPR:$src1, GPR:$src2, imm16_31:$shamt)>;
1092
1093
1094 def PKHTB : AI<0x0, (outs GPR:$dst), (ins GPR:$src1, GPR:$src2, i32imm:$shamt),
1095                Pseudo, "pkhtb", " $dst, $src1, $src2, ASR $shamt",
1096                [(set GPR:$dst, (or (and GPR:$src1, 0xFFFF0000),
1097                                    (and (sra GPR:$src2, imm16_31:$shamt),
1098                                         0xFFFF)))]>, Requires<[IsARM, HasV6]>;
1099
1100 // Alternate cases for PKHTB where identities eliminate some nodes.  Note that
1101 // a shift amount of 0 is *not legal* here, it is PKHBT instead.
1102 def : ARMV6Pat<(or (and GPR:$src1, 0xFFFF0000), (srl GPR:$src2, 16)),
1103                (PKHTB GPR:$src1, GPR:$src2, 16)>;
1104 def : ARMV6Pat<(or (and GPR:$src1, 0xFFFF0000),
1105                    (and (srl GPR:$src2, imm1_15:$shamt), 0xFFFF)),
1106                (PKHTB GPR:$src1, GPR:$src2, imm1_15:$shamt)>;
1107
1108
1109 //===----------------------------------------------------------------------===//
1110 //  Comparison Instructions...
1111 //
1112
1113 defm CMP  : AI1_cmp_irs<0b1010, "cmp",
1114                         BinOpFrag<(ARMcmp node:$LHS, node:$RHS)>>;
1115 defm CMN  : AI1_cmp_irs<0b1011, "cmn",
1116                         BinOpFrag<(ARMcmp node:$LHS,(ineg node:$RHS))>>;
1117
1118 // Note that TST/TEQ don't set all the same flags that CMP does!
1119 defm TST  : AI1_cmp_irs<0x8, "tst",
1120                         BinOpFrag<(ARMcmpNZ (and node:$LHS, node:$RHS), 0)>>;
1121 defm TEQ  : AI1_cmp_irs<0x9, "teq",
1122                         BinOpFrag<(ARMcmpNZ (xor node:$LHS, node:$RHS), 0)>>;
1123
1124 defm CMPnz : AI1_cmp_irs<0b1010, "cmp",
1125                          BinOpFrag<(ARMcmpNZ node:$LHS, node:$RHS)>>;
1126 defm CMNnz : AI1_cmp_irs<0b1011, "cmn",
1127                          BinOpFrag<(ARMcmpNZ node:$LHS,(ineg node:$RHS))>>;
1128
1129 def : ARMPat<(ARMcmp GPR:$src, so_imm_neg:$imm),
1130              (CMNri  GPR:$src, so_imm_neg:$imm)>;
1131
1132 def : ARMPat<(ARMcmpNZ GPR:$src, so_imm_neg:$imm),
1133              (CMNri  GPR:$src, so_imm_neg:$imm)>;
1134
1135
1136 // Conditional moves
1137 // FIXME: should be able to write a pattern for ARMcmov, but can't use
1138 // a two-value operand where a dag node expects two operands. :( 
1139 def MOVCCr : AI<0xD, (outs GPR:$dst), (ins GPR:$false, GPR:$true), DPFrm,
1140                 "mov", " $dst, $true",
1141       [/*(set GPR:$dst, (ARMcmov GPR:$false, GPR:$true, imm:$cc, CCR:$ccr))*/]>,
1142                 RegConstraint<"$false = $dst">;
1143
1144 def MOVCCs : AI<0xD, (outs GPR:$dst), (ins GPR:$false, so_reg:$true), DPSoRegFrm,
1145                 "mov", " $dst, $true",
1146    [/*(set GPR:$dst, (ARMcmov GPR:$false, so_reg:$true, imm:$cc, CCR:$ccr))*/]>,
1147                 RegConstraint<"$false = $dst">, UnaryDP;
1148
1149 def MOVCCi : AI<0xD, (outs GPR:$dst), (ins GPR:$false, so_imm:$true), DPFrm,
1150                 "mov", " $dst, $true",
1151    [/*(set GPR:$dst, (ARMcmov GPR:$false, so_imm:$true, imm:$cc, CCR:$ccr))*/]>,
1152                 RegConstraint<"$false = $dst">, UnaryDP;
1153
1154
1155 // LEApcrel - Load a pc-relative address into a register without offending the
1156 // assembler.
1157 def LEApcrel : AXI1<0x0, (outs GPR:$dst), (ins i32imm:$label, pred:$p), Pseudo,
1158                    !strconcat(!strconcat(".set PCRELV${:uid}, ($label-(",
1159                                          "${:private}PCRELL${:uid}+8))\n"),
1160                               !strconcat("${:private}PCRELL${:uid}:\n\t",
1161                                          "add$p $dst, pc, #PCRELV${:uid}")),
1162                    []>;
1163
1164 def LEApcrelJT : AXI1<0x0, (outs GPR:$dst), (ins i32imm:$label, i32imm:$id, pred:$p),
1165           Pseudo,
1166           !strconcat(!strconcat(".set PCRELV${:uid}, (${label}_${id:no_hash}-(",
1167                                          "${:private}PCRELL${:uid}+8))\n"),
1168                               !strconcat("${:private}PCRELL${:uid}:\n\t",
1169                                          "add$p $dst, pc, #PCRELV${:uid}")),
1170                    []>;
1171
1172 //===----------------------------------------------------------------------===//
1173 // TLS Instructions
1174 //
1175
1176 // __aeabi_read_tp preserves the registers r1-r3.
1177 let isCall = 1,
1178   Defs = [R0, R12, LR, CPSR] in {
1179   def TPsoft : AXI<0x0, (outs), (ins), BranchMisc,
1180                "bl __aeabi_read_tp",
1181                [(set R0, ARMthread_pointer)]>;
1182 }
1183
1184 //===----------------------------------------------------------------------===//
1185 // Non-Instruction Patterns
1186 //
1187
1188 // ConstantPool, GlobalAddress, and JumpTable
1189 def : ARMPat<(ARMWrapper  tglobaladdr :$dst), (LEApcrel tglobaladdr :$dst)>;
1190 def : ARMPat<(ARMWrapper  tconstpool  :$dst), (LEApcrel tconstpool  :$dst)>;
1191 def : ARMPat<(ARMWrapperJT tjumptable:$dst, imm:$id),
1192              (LEApcrelJT tjumptable:$dst, imm:$id)>;
1193
1194 // Large immediate handling.
1195
1196 // Two piece so_imms.
1197 let isReMaterializable = 1 in
1198 def MOVi2pieces : AI1x2<0x0, (outs GPR:$dst), (ins so_imm2part:$src), DPFrm,
1199                          "mov", " $dst, $src",
1200                          [(set GPR:$dst, so_imm2part:$src)]>, UnaryDP;
1201
1202 def : ARMPat<(or GPR:$LHS, so_imm2part:$RHS),
1203               (ORRri (ORRri GPR:$LHS, (so_imm2part_1 imm:$RHS)),
1204                      (so_imm2part_2 imm:$RHS))>;
1205 def : ARMPat<(xor GPR:$LHS, so_imm2part:$RHS),
1206               (EORri (EORri GPR:$LHS, (so_imm2part_1 imm:$RHS)),
1207                      (so_imm2part_2 imm:$RHS))>;
1208
1209 // TODO: add,sub,and, 3-instr forms?
1210
1211
1212 // Direct calls
1213 def : ARMPat<(ARMcall texternalsym:$func), (BL texternalsym:$func)>;
1214
1215 // zextload i1 -> zextload i8
1216 def : ARMPat<(zextloadi1 addrmode2:$addr),  (LDRB addrmode2:$addr)>;
1217
1218 // extload -> zextload
1219 def : ARMPat<(extloadi1  addrmode2:$addr),  (LDRB addrmode2:$addr)>;
1220 def : ARMPat<(extloadi8  addrmode2:$addr),  (LDRB addrmode2:$addr)>;
1221 def : ARMPat<(extloadi16 addrmode3:$addr),  (LDRH addrmode3:$addr)>;
1222
1223 def : ARMPat<(extloadi8  addrmodepc:$addr), (PICLDRB addrmodepc:$addr)>;
1224 def : ARMPat<(extloadi16 addrmodepc:$addr), (PICLDRH addrmodepc:$addr)>;
1225
1226 // smul* and smla*
1227 def : ARMV5TEPat<(mul (sra (shl GPR:$a, 16), 16), (sra (shl GPR:$b, 16), 16)),
1228                  (SMULBB GPR:$a, GPR:$b)>;
1229 def : ARMV5TEPat<(mul sext_16_node:$a, sext_16_node:$b),
1230                  (SMULBB GPR:$a, GPR:$b)>;
1231 def : ARMV5TEPat<(mul (sra (shl GPR:$a, 16), 16), (sra GPR:$b, 16)),
1232                  (SMULBT GPR:$a, GPR:$b)>;
1233 def : ARMV5TEPat<(mul sext_16_node:$a, (sra GPR:$b, 16)),
1234                  (SMULBT GPR:$a, GPR:$b)>;
1235 def : ARMV5TEPat<(mul (sra GPR:$a, 16), (sra (shl GPR:$b, 16), 16)),
1236                  (SMULTB GPR:$a, GPR:$b)>;
1237 def : ARMV5TEPat<(mul (sra GPR:$a, 16), sext_16_node:$b),
1238                 (SMULTB GPR:$a, GPR:$b)>;
1239 def : ARMV5TEPat<(sra (mul GPR:$a, (sra (shl GPR:$b, 16), 16)), 16),
1240                  (SMULWB GPR:$a, GPR:$b)>;
1241 def : ARMV5TEPat<(sra (mul GPR:$a, sext_16_node:$b), 16),
1242                  (SMULWB GPR:$a, GPR:$b)>;
1243
1244 def : ARMV5TEPat<(add GPR:$acc,
1245                       (mul (sra (shl GPR:$a, 16), 16),
1246                            (sra (shl GPR:$b, 16), 16))),
1247                  (SMLABB GPR:$a, GPR:$b, GPR:$acc)>;
1248 def : ARMV5TEPat<(add GPR:$acc,
1249                       (mul sext_16_node:$a, sext_16_node:$b)),
1250                  (SMLABB GPR:$a, GPR:$b, GPR:$acc)>;
1251 def : ARMV5TEPat<(add GPR:$acc,
1252                       (mul (sra (shl GPR:$a, 16), 16), (sra GPR:$b, 16))),
1253                  (SMLABT GPR:$a, GPR:$b, GPR:$acc)>;
1254 def : ARMV5TEPat<(add GPR:$acc,
1255                       (mul sext_16_node:$a, (sra GPR:$b, 16))),
1256                  (SMLABT GPR:$a, GPR:$b, GPR:$acc)>;
1257 def : ARMV5TEPat<(add GPR:$acc,
1258                       (mul (sra GPR:$a, 16), (sra (shl GPR:$b, 16), 16))),
1259                  (SMLATB GPR:$a, GPR:$b, GPR:$acc)>;
1260 def : ARMV5TEPat<(add GPR:$acc,
1261                       (mul (sra GPR:$a, 16), sext_16_node:$b)),
1262                  (SMLATB GPR:$a, GPR:$b, GPR:$acc)>;
1263 def : ARMV5TEPat<(add GPR:$acc,
1264                       (sra (mul GPR:$a, (sra (shl GPR:$b, 16), 16)), 16)),
1265                  (SMLAWB GPR:$a, GPR:$b, GPR:$acc)>;
1266 def : ARMV5TEPat<(add GPR:$acc,
1267                       (sra (mul GPR:$a, sext_16_node:$b), 16)),
1268                  (SMLAWB GPR:$a, GPR:$b, GPR:$acc)>;
1269
1270 //===----------------------------------------------------------------------===//
1271 // Thumb Support
1272 //
1273
1274 include "ARMInstrThumb.td"
1275
1276 //===----------------------------------------------------------------------===//
1277 // Floating Point Support
1278 //
1279
1280 include "ARMInstrVFP.td"