Define unaligned load and store.
[oota-llvm.git] / lib / Target / Mips / MipsInstrInfo.td
1 //===- MipsInstrInfo.td - Target Description for Mips 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 contains the Mips implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // Instruction format superclass
16 //===----------------------------------------------------------------------===//
17
18 include "MipsInstrFormats.td"
19
20 //===----------------------------------------------------------------------===//
21 // Mips profiles and nodes
22 //===----------------------------------------------------------------------===//
23
24 def SDT_MipsRet          : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
25 def SDT_MipsJmpLink      : SDTypeProfile<0, 1, [SDTCisVT<0, iPTR>]>;
26 def SDT_MipsCMov         : SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
27                                                 SDTCisSameAs<1, 2>,
28                                                 SDTCisSameAs<3, 4>,
29                                                 SDTCisInt<4>]>;
30 def SDT_MipsCallSeqStart : SDCallSeqStart<[SDTCisVT<0, i32>]>;
31 def SDT_MipsCallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
32 def SDT_MipsMAddMSub     : SDTypeProfile<0, 4,
33                                          [SDTCisVT<0, i32>, SDTCisSameAs<0, 1>,
34                                           SDTCisSameAs<1, 2>,
35                                           SDTCisSameAs<2, 3>]>;
36 def SDT_MipsDivRem       : SDTypeProfile<0, 2,
37                                          [SDTCisVT<0, i32>,
38                                           SDTCisSameAs<0, 1>]>;
39
40 def SDT_MipsThreadPointer : SDTypeProfile<1, 0, [SDTCisPtrTy<0>]>;
41
42 def SDT_MipsDynAlloc    : SDTypeProfile<1, 1, [SDTCisVT<0, i32>,
43                                                SDTCisVT<1, iPTR>]>;
44 def SDT_Sync             : SDTypeProfile<0, 1, [SDTCisVT<0, i32>]>;
45
46 // Call
47 def MipsJmpLink : SDNode<"MipsISD::JmpLink",SDT_MipsJmpLink,
48                          [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,
49                           SDNPVariadic]>;
50
51 // Hi and Lo nodes are used to handle global addresses. Used on
52 // MipsISelLowering to lower stuff like GlobalAddress, ExternalSymbol
53 // static model. (nothing to do with Mips Registers Hi and Lo)
54 def MipsHi    : SDNode<"MipsISD::Hi", SDTIntUnaryOp>;
55 def MipsLo    : SDNode<"MipsISD::Lo", SDTIntUnaryOp>;
56 def MipsGPRel : SDNode<"MipsISD::GPRel", SDTIntUnaryOp>;
57
58 // TlsGd node is used to handle General Dynamic TLS
59 def MipsTlsGd : SDNode<"MipsISD::TlsGd", SDTIntUnaryOp>;
60
61 // TprelHi and TprelLo nodes are used to handle Local Exec TLS
62 def MipsTprelHi    : SDNode<"MipsISD::TprelHi", SDTIntUnaryOp>;
63 def MipsTprelLo    : SDNode<"MipsISD::TprelLo", SDTIntUnaryOp>;
64
65 // Thread pointer
66 def MipsThreadPointer: SDNode<"MipsISD::ThreadPointer", SDT_MipsThreadPointer>;
67
68 // Return
69 def MipsRet : SDNode<"MipsISD::Ret", SDT_MipsRet, [SDNPHasChain,
70                      SDNPOptInGlue]>;
71
72 // These are target-independent nodes, but have target-specific formats.
73 def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_MipsCallSeqStart,
74                            [SDNPHasChain, SDNPOutGlue]>;
75 def callseq_end   : SDNode<"ISD::CALLSEQ_END", SDT_MipsCallSeqEnd,
76                            [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
77
78 // MAdd*/MSub* nodes
79 def MipsMAdd      : SDNode<"MipsISD::MAdd", SDT_MipsMAddMSub,
80                            [SDNPOptInGlue, SDNPOutGlue]>;
81 def MipsMAddu     : SDNode<"MipsISD::MAddu", SDT_MipsMAddMSub,
82                            [SDNPOptInGlue, SDNPOutGlue]>;
83 def MipsMSub      : SDNode<"MipsISD::MSub", SDT_MipsMAddMSub,
84                            [SDNPOptInGlue, SDNPOutGlue]>;
85 def MipsMSubu     : SDNode<"MipsISD::MSubu", SDT_MipsMAddMSub,
86                            [SDNPOptInGlue, SDNPOutGlue]>;
87
88 // DivRem(u) nodes
89 def MipsDivRem    : SDNode<"MipsISD::DivRem", SDT_MipsDivRem,
90                            [SDNPOutGlue]>;
91 def MipsDivRemU   : SDNode<"MipsISD::DivRemU", SDT_MipsDivRem,
92                            [SDNPOutGlue]>;
93
94 // Target constant nodes that are not part of any isel patterns and remain
95 // unchanged can cause instructions with illegal operands to be emitted.
96 // Wrapper node patterns give the instruction selector a chance to replace
97 // target constant nodes that would otherwise remain unchanged with ADDiu
98 // nodes. Without these wrapper node patterns, the following conditional move
99 // instrucion is emitted when function cmov2 in test/CodeGen/Mips/cmov.ll is
100 // compiled: 
101 //  movn  %got(d)($gp), %got(c)($gp), $4
102 // This instruction is illegal since movn can take only register operands.
103
104 def MipsWrapperPIC    : SDNode<"MipsISD::WrapperPIC",  SDTIntUnaryOp>;
105
106 // Pointer to dynamically allocated stack area.
107 def MipsDynAlloc  : SDNode<"MipsISD::DynAlloc", SDT_MipsDynAlloc,
108                            [SDNPHasChain, SDNPInGlue]>;
109
110 def MipsSync : SDNode<"MipsISD::Sync", SDT_Sync, [SDNPHasChain]>;
111
112 //===----------------------------------------------------------------------===//
113 // Mips Instruction Predicate Definitions.
114 //===----------------------------------------------------------------------===//
115 def HasSEInReg  : Predicate<"Subtarget.hasSEInReg()">;
116 def HasBitCount : Predicate<"Subtarget.hasBitCount()">;
117 def HasSwap     : Predicate<"Subtarget.hasSwap()">;
118 def HasCondMov  : Predicate<"Subtarget.hasCondMov()">;
119 def IsMips32    : Predicate<"Subtarget.isMips32()">;
120 def IsMips32r2  : Predicate<"Subtarget.isMips32r2()">;
121
122 //===----------------------------------------------------------------------===//
123 // Mips Operand, Complex Patterns and Transformations Definitions.
124 //===----------------------------------------------------------------------===//
125
126 // Instruction operand types
127 def brtarget    : Operand<OtherVT>;
128 def calltarget  : Operand<i32>;
129 def simm16      : Operand<i32>;
130 def shamt       : Operand<i32>;
131
132 // Unsigned Operand
133 def uimm16      : Operand<i32> {
134   let PrintMethod = "printUnsignedImm";
135 }
136
137 // Address operand
138 def mem : Operand<i32> {
139   let PrintMethod = "printMemOperand";
140   let MIOperandInfo = (ops CPURegs, simm16);
141 }
142
143 def mem_ea : Operand<i32> {
144   let PrintMethod = "printMemOperandEA";
145   let MIOperandInfo = (ops CPURegs, simm16);
146 }
147
148 // Transformation Function - get the lower 16 bits.
149 def LO16 : SDNodeXForm<imm, [{
150   return getI32Imm((unsigned)N->getZExtValue() & 0xFFFF);
151 }]>;
152
153 // Transformation Function - get the higher 16 bits.
154 def HI16 : SDNodeXForm<imm, [{
155   return getI32Imm((unsigned)N->getZExtValue() >> 16);
156 }]>;
157
158 // Node immediate fits as 16-bit sign extended on target immediate.
159 // e.g. addi, andi
160 def immSExt16  : PatLeaf<(imm), [{ return isInt<16>(N->getSExtValue()); }]>;
161
162 // Node immediate fits as 16-bit zero extended on target immediate.
163 // The LO16 param means that only the lower 16 bits of the node
164 // immediate are caught.
165 // e.g. addiu, sltiu
166 def immZExt16  : PatLeaf<(imm), [{
167   if (N->getValueType(0) == MVT::i32)
168     return (uint32_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
169   else
170     return (uint64_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
171 }], LO16>;
172
173 // shamt field must fit in 5 bits.
174 def immZExt5 : PatLeaf<(imm), [{
175   return N->getZExtValue() == ((N->getZExtValue()) & 0x1f) ;
176 }]>;
177
178 // Mips Address Mode! SDNode frameindex could possibily be a match
179 // since load and store instructions from stack used it.
180 def addr : ComplexPattern<iPTR, 2, "SelectAddr", [frameindex], []>;
181
182 //===----------------------------------------------------------------------===//
183 // Instructions specific format
184 //===----------------------------------------------------------------------===//
185
186 // Arithmetic 3 register operands
187 class ArithR<bits<6> op, bits<6> func, string instr_asm, SDNode OpNode,
188              InstrItinClass itin, bit isComm = 0>:
189   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
190      !strconcat(instr_asm, "\t$dst, $b, $c"),
191      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], itin> {
192   let isCommutable = isComm;
193 }
194
195 class ArithOverflowR<bits<6> op, bits<6> func, string instr_asm,
196                      bit isComm = 0>:
197   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
198      !strconcat(instr_asm, "\t$dst, $b, $c"), [], IIAlu> {
199   let isCommutable = isComm;
200 }
201
202 // Arithmetic 2 register operands
203 class ArithI<bits<6> op, string instr_asm, SDNode OpNode,
204              Operand Od, PatLeaf imm_type> :
205   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
206      !strconcat(instr_asm, "\t$dst, $b, $c"),
207      [(set CPURegs:$dst, (OpNode CPURegs:$b, imm_type:$c))], IIAlu>;
208
209 class ArithOverflowI<bits<6> op, string instr_asm, SDNode OpNode,
210              Operand Od, PatLeaf imm_type> :
211   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
212      !strconcat(instr_asm, "\t$dst, $b, $c"), [], IIAlu>;
213
214 // Arithmetic Multiply ADD/SUB
215 let rd = 0, shamt = 0, Defs = [HI, LO], Uses = [HI, LO] in
216 class MArithR<bits<6> func, string instr_asm, SDNode op, bit isComm = 0> :
217   FR<0x1c, func, (outs), (ins CPURegs:$rs, CPURegs:$rt),
218      !strconcat(instr_asm, "\t$rs, $rt"),
219      [(op CPURegs:$rs, CPURegs:$rt, LO, HI)], IIImul> {
220   let isCommutable = isComm;
221 }
222
223 //  Logical
224 let isCommutable = 1 in
225 class LogicR<bits<6> func, string instr_asm, SDNode OpNode>:
226   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
227      !strconcat(instr_asm, "\t$dst, $b, $c"),
228      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], IIAlu>;
229
230 class LogicI<bits<6> op, string instr_asm, SDNode OpNode>:
231   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, uimm16:$c),
232      !strconcat(instr_asm, "\t$dst, $b, $c"),
233      [(set CPURegs:$dst, (OpNode CPURegs:$b, immZExt16:$c))], IIAlu>;
234
235 let isCommutable = 1 in
236 class LogicNOR<bits<6> op, bits<6> func, string instr_asm>:
237   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
238      !strconcat(instr_asm, "\t$dst, $b, $c"),
239      [(set CPURegs:$dst, (not (or CPURegs:$b, CPURegs:$c)))], IIAlu>;
240
241 // Shifts
242 class LogicR_shift_rotate_imm<bits<6> func, bits<5> _rs, string instr_asm,
243                               SDNode OpNode>:
244   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$b, shamt:$c),
245      !strconcat(instr_asm, "\t$dst, $b, $c"),
246      [(set CPURegs:$dst, (OpNode CPURegs:$b, immZExt5:$c))], IIAlu> {
247   let rs = _rs;
248 }
249
250 class LogicR_shift_rotate_reg<bits<6> func, bits<5> _shamt, string instr_asm,
251                               SDNode OpNode>:
252   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$c, CPURegs:$b),
253      !strconcat(instr_asm, "\t$dst, $b, $c"),
254      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], IIAlu> {
255   let shamt = _shamt;
256 }
257
258 // Load Upper Imediate
259 class LoadUpper<bits<6> op, string instr_asm>:
260   FI< op,
261       (outs CPURegs:$dst),
262       (ins uimm16:$imm),
263       !strconcat(instr_asm, "\t$dst, $imm"),
264       [], IIAlu>;
265
266 // Memory Load/Store
267 let canFoldAsLoad = 1, hasDelaySlot = 1 in
268 class LoadM<bits<6> op, string instr_asm, PatFrag OpNode>:
269   FI<op, (outs CPURegs:$dst), (ins mem:$addr),
270      !strconcat(instr_asm, "\t$dst, $addr"),
271      [(set CPURegs:$dst, (OpNode addr:$addr))], IILoad>;
272
273 class StoreM<bits<6> op, string instr_asm, PatFrag OpNode>:
274   FI<op, (outs), (ins CPURegs:$dst, mem:$addr),
275      !strconcat(instr_asm, "\t$dst, $addr"),
276      [(OpNode CPURegs:$dst, addr:$addr)], IIStore>;
277
278 // Conditional Branch
279 let isBranch = 1, isTerminator=1, hasDelaySlot = 1 in {
280 class CBranch<bits<6> op, string instr_asm, PatFrag cond_op>:
281   FI<op, (outs), (ins CPURegs:$a, CPURegs:$b, brtarget:$offset),
282      !strconcat(instr_asm, "\t$a, $b, $offset"),
283      [(brcond (cond_op CPURegs:$a, CPURegs:$b), bb:$offset)],
284      IIBranch>;
285
286 class CBranchZero<bits<6> op, string instr_asm, PatFrag cond_op>:
287   FI<op, (outs), (ins CPURegs:$src, brtarget:$offset),
288      !strconcat(instr_asm, "\t$src, $offset"),
289      [(brcond (cond_op CPURegs:$src, 0), bb:$offset)],
290      IIBranch>;
291 }
292
293 // SetCC
294 class SetCC_R<bits<6> op, bits<6> func, string instr_asm,
295       PatFrag cond_op>:
296   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
297      !strconcat(instr_asm, "\t$dst, $b, $c"),
298      [(set CPURegs:$dst, (cond_op CPURegs:$b, CPURegs:$c))],
299      IIAlu>;
300
301 class SetCC_I<bits<6> op, string instr_asm, PatFrag cond_op,
302       Operand Od, PatLeaf imm_type>:
303   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
304      !strconcat(instr_asm, "\t$dst, $b, $c"),
305      [(set CPURegs:$dst, (cond_op CPURegs:$b, imm_type:$c))],
306      IIAlu>;
307
308 // Unconditional branch
309 let isBranch=1, isTerminator=1, isBarrier=1, hasDelaySlot = 1 in
310 class JumpFJ<bits<6> op, string instr_asm>:
311   FJ<op, (outs), (ins brtarget:$target),
312      !strconcat(instr_asm, "\t$target"), [(br bb:$target)], IIBranch>;
313
314 let isBranch=1, isTerminator=1, isBarrier=1, rd=0, hasDelaySlot = 1 in
315 class JumpFR<bits<6> op, bits<6> func, string instr_asm>:
316   FR<op, func, (outs), (ins CPURegs:$target),
317      !strconcat(instr_asm, "\t$target"), [(brind CPURegs:$target)], IIBranch>;
318
319 // Jump and Link (Call)
320 let isCall=1, hasDelaySlot=1,
321   // All calls clobber the non-callee saved registers...
322   Defs = [AT, V0, V1, A0, A1, A2, A3, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9,
323           K0, K1, D0, D1, D2, D3, D4, D5, D6, D7, D8, D9], Uses = [GP] in {
324   class JumpLink<bits<6> op, string instr_asm>:
325     FJ<op, (outs), (ins calltarget:$target, variable_ops),
326        !strconcat(instr_asm, "\t$target"), [(MipsJmpLink imm:$target)],
327        IIBranch>;
328
329   let rd=31 in
330   class JumpLinkReg<bits<6> op, bits<6> func, string instr_asm>:
331     FR<op, func, (outs), (ins CPURegs:$rs, variable_ops),
332        !strconcat(instr_asm, "\t$rs"), [(MipsJmpLink CPURegs:$rs)], IIBranch>;
333
334   class BranchLink<string instr_asm>:
335     FI<0x1, (outs), (ins CPURegs:$rs, brtarget:$target, variable_ops),
336        !strconcat(instr_asm, "\t$rs, $target"), [], IIBranch>;
337 }
338
339 // Mul, Div
340 let Defs = [HI, LO] in {
341   let isCommutable = 1 in
342   class Mul<bits<6> func, string instr_asm, InstrItinClass itin>:
343     FR<0x00, func, (outs), (ins CPURegs:$a, CPURegs:$b),
344        !strconcat(instr_asm, "\t$a, $b"), [], itin>;
345
346   class Div<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin>:
347             FR<0x00, func, (outs), (ins CPURegs:$a, CPURegs:$b),
348             !strconcat(instr_asm, "\t$$zero, $a, $b"),
349             [(op CPURegs:$a, CPURegs:$b)], itin>;
350 }
351
352 // Move from Hi/Lo
353 class MoveFromLOHI<bits<6> func, string instr_asm>:
354   FR<0x00, func, (outs CPURegs:$dst), (ins),
355      !strconcat(instr_asm, "\t$dst"), [], IIHiLo>;
356
357 class MoveToLOHI<bits<6> func, string instr_asm>:
358   FR<0x00, func, (outs), (ins CPURegs:$src),
359      !strconcat(instr_asm, "\t$src"), [], IIHiLo>;
360
361 class EffectiveAddress<string instr_asm> :
362   FI<0x09, (outs CPURegs:$dst), (ins mem_ea:$addr),
363      instr_asm, [(set CPURegs:$dst, addr:$addr)], IIAlu>;
364
365 // Count Leading Ones/Zeros in Word
366 class CountLeading<bits<6> func, string instr_asm, list<dag> pattern>:
367   FR<0x1c, func, (outs CPURegs:$dst), (ins CPURegs:$src),
368      !strconcat(instr_asm, "\t$dst, $src"), pattern, IIAlu>,
369      Requires<[HasBitCount]> {
370   let shamt = 0;
371   let rt = rd;
372 }
373
374 // Sign Extend in Register.
375 class SignExtInReg<bits<6> func, string instr_asm, ValueType vt>:
376   FR<0x3f, func, (outs CPURegs:$dst), (ins CPURegs:$src),
377      !strconcat(instr_asm, "\t$dst, $src"),
378      [(set CPURegs:$dst, (sext_inreg CPURegs:$src, vt))], NoItinerary>;
379
380 // Byte Swap
381 class ByteSwap<bits<6> func, string instr_asm>:
382   FR<0x1f, func, (outs CPURegs:$dst), (ins CPURegs:$src),
383      !strconcat(instr_asm, "\t$dst, $src"),
384      [(set CPURegs:$dst, (bswap CPURegs:$src))], NoItinerary>;
385
386 // Conditional Move
387 class CondMov<bits<6> func, string instr_asm, PatLeaf MovCode>:
388   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$F, CPURegs:$T,
389      CPURegs:$cond), !strconcat(instr_asm, "\t$dst, $T, $cond"),
390      [], NoItinerary>;
391
392 // Read Hardware
393 class ReadHardware: FR<0x1f, 0x3b, (outs CPURegs:$dst), (ins HWRegs:$src),
394     "rdhwr\t$dst, $src", [], IIAlu> {
395   let rs = 0;
396   let shamt = 0;
397 }
398
399 // Atomic instructions with 2 source operands (ATOMIC_SWAP & ATOMIC_LOAD_*).
400 class Atomic2Ops<PatFrag Op, string Opstr> :
401   MipsPseudo<(outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
402              !strconcat("atomic_", Opstr, "\t$dst, $ptr, $incr"),
403              [(set CPURegs:$dst,
404               (Op CPURegs:$ptr, CPURegs:$incr))]>;
405
406 // Atomic Compare & Swap.
407 class AtomicCmpSwap<PatFrag Op, string Width> :
408   MipsPseudo<(outs CPURegs:$dst), 
409              (ins CPURegs:$ptr, CPURegs:$cmp, CPURegs:$swap),
410              !strconcat("atomic_cmp_swap_", Width, 
411                         "\t$dst, $ptr, $cmp, $swap"),
412              [(set CPURegs:$dst,
413               (Op CPURegs:$ptr, CPURegs:$cmp, CPURegs:$swap))]>;
414
415 //===----------------------------------------------------------------------===//
416 // Pseudo instructions
417 //===----------------------------------------------------------------------===//
418
419 // As stack alignment is always done with addiu, we need a 16-bit immediate
420 let Defs = [SP], Uses = [SP] in {
421 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins uimm16:$amt),
422                                   "!ADJCALLSTACKDOWN $amt",
423                                   [(callseq_start timm:$amt)]>;
424 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins uimm16:$amt1, uimm16:$amt2),
425                                   "!ADJCALLSTACKUP $amt1",
426                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
427 }
428
429 // Some assembly macros need to avoid pseudoinstructions and assembler
430 // automatic reodering, we should reorder ourselves.
431 def MACRO     : MipsPseudo<(outs), (ins), ".set\tmacro",     []>;
432 def REORDER   : MipsPseudo<(outs), (ins), ".set\treorder",   []>;
433 def NOMACRO   : MipsPseudo<(outs), (ins), ".set\tnomacro",   []>;
434 def NOREORDER : MipsPseudo<(outs), (ins), ".set\tnoreorder", []>;
435
436 // These macros are inserted to prevent GAS from complaining
437 // when using the AT register.
438 def NOAT      : MipsPseudo<(outs), (ins), ".set\tnoat", []>;
439 def ATMACRO   : MipsPseudo<(outs), (ins), ".set\tat", []>;
440
441 // When handling PIC code the assembler needs .cpload and .cprestore
442 // directives. If the real instructions corresponding these directives
443 // are used, we have the same behavior, but get also a bunch of warnings
444 // from the assembler.
445 def CPLOAD : MipsPseudo<(outs), (ins CPURegs:$picreg), ".cpload\t$picreg", []>;
446 def CPRESTORE : MipsPseudo<(outs), (ins i32imm:$loc), ".cprestore\t$loc", []>;
447
448 let usesCustomInserter = 1 in {
449   def ATOMIC_LOAD_ADD_I8   : Atomic2Ops<atomic_load_add_8, "load_add_8">;
450   def ATOMIC_LOAD_ADD_I16  : Atomic2Ops<atomic_load_add_16, "load_add_16">;
451   def ATOMIC_LOAD_ADD_I32  : Atomic2Ops<atomic_load_add_32, "load_add_32">;
452   def ATOMIC_LOAD_SUB_I8   : Atomic2Ops<atomic_load_sub_8, "load_sub_8">;
453   def ATOMIC_LOAD_SUB_I16  : Atomic2Ops<atomic_load_sub_16, "load_sub_16">;
454   def ATOMIC_LOAD_SUB_I32  : Atomic2Ops<atomic_load_sub_32, "load_sub_32">;
455   def ATOMIC_LOAD_AND_I8   : Atomic2Ops<atomic_load_and_8, "load_and_8">;
456   def ATOMIC_LOAD_AND_I16  : Atomic2Ops<atomic_load_and_16, "load_and_16">;
457   def ATOMIC_LOAD_AND_I32  : Atomic2Ops<atomic_load_and_32, "load_and_32">;
458   def ATOMIC_LOAD_OR_I8    : Atomic2Ops<atomic_load_or_8, "load_or_8">;
459   def ATOMIC_LOAD_OR_I16   : Atomic2Ops<atomic_load_or_16, "load_or_16">;
460   def ATOMIC_LOAD_OR_I32   : Atomic2Ops<atomic_load_or_32, "load_or_32">;
461   def ATOMIC_LOAD_XOR_I8   : Atomic2Ops<atomic_load_xor_8, "load_xor_8">;
462   def ATOMIC_LOAD_XOR_I16  : Atomic2Ops<atomic_load_xor_16, "load_xor_16">;
463   def ATOMIC_LOAD_XOR_I32  : Atomic2Ops<atomic_load_xor_32, "load_xor_32">;
464   def ATOMIC_LOAD_NAND_I8  : Atomic2Ops<atomic_load_nand_8, "load_nand_8">;
465   def ATOMIC_LOAD_NAND_I16 : Atomic2Ops<atomic_load_nand_16, "load_nand_16">;
466   def ATOMIC_LOAD_NAND_I32 : Atomic2Ops<atomic_load_nand_32, "load_nand_32">;
467
468   def ATOMIC_SWAP_I8       : Atomic2Ops<atomic_swap_8, "swap_8">;
469   def ATOMIC_SWAP_I16      : Atomic2Ops<atomic_swap_16, "swap_16">;
470   def ATOMIC_SWAP_I32      : Atomic2Ops<atomic_swap_32, "swap_32">;
471
472   def ATOMIC_CMP_SWAP_I8   : AtomicCmpSwap<atomic_cmp_swap_8, "8">;
473   def ATOMIC_CMP_SWAP_I16  : AtomicCmpSwap<atomic_cmp_swap_16, "16">;
474   def ATOMIC_CMP_SWAP_I32  : AtomicCmpSwap<atomic_cmp_swap_32, "32">;
475 }
476
477 // Unaligned memory load and store.
478 // Replaces LW or SW during MCInstLowering if memory access is unaligned.
479 def ULW :
480   MipsPseudo<(outs CPURegs:$dst), (ins mem:$addr), "ulw\t$dst, $addr", []>;
481 def USW : 
482   MipsPseudo<(outs), (ins CPURegs:$dst, mem:$addr), "usw\t$dst, $addr", []>;
483
484 //===----------------------------------------------------------------------===//
485 // Instruction definition
486 //===----------------------------------------------------------------------===//
487
488 //===----------------------------------------------------------------------===//
489 // MipsI Instructions
490 //===----------------------------------------------------------------------===//
491
492 /// Arithmetic Instructions (ALU Immediate)
493 def ADDiu   : ArithI<0x09, "addiu", add, simm16, immSExt16>;
494 def ADDi    : ArithOverflowI<0x08, "addi",  add, simm16, immSExt16>;
495 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
496 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16>;
497 def ANDi    : LogicI<0x0c, "andi", and>;
498 def ORi     : LogicI<0x0d, "ori",  or>;
499 def XORi    : LogicI<0x0e, "xori",  xor>;
500 def LUi     : LoadUpper<0x0f, "lui">;
501
502 /// Arithmetic Instructions (3-Operand, R-Type)
503 def ADDu    : ArithR<0x00, 0x21, "addu", add, IIAlu, 1>;
504 def SUBu    : ArithR<0x00, 0x23, "subu", sub, IIAlu>;
505 def ADD     : ArithOverflowR<0x00, 0x20, "add", 1>;
506 def SUB     : ArithOverflowR<0x00, 0x22, "sub">;
507 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt>;
508 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult>;
509 def AND     : LogicR<0x24, "and", and>;
510 def OR      : LogicR<0x25, "or",  or>;
511 def XOR     : LogicR<0x26, "xor", xor>;
512 def NOR     : LogicNOR<0x00, 0x27, "nor">;
513
514 /// Shift Instructions
515 def SLL     : LogicR_shift_rotate_imm<0x00, 0x00, "sll", shl>;
516 def SRL     : LogicR_shift_rotate_imm<0x02, 0x00, "srl", srl>;
517 def SRA     : LogicR_shift_rotate_imm<0x03, 0x00, "sra", sra>;
518 def SLLV    : LogicR_shift_rotate_reg<0x04, 0x00, "sllv", shl>;
519 def SRLV    : LogicR_shift_rotate_reg<0x06, 0x00, "srlv", srl>;
520 def SRAV    : LogicR_shift_rotate_reg<0x07, 0x00, "srav", sra>;
521
522 // Rotate Instructions
523 let Predicates = [IsMips32r2] in {
524     def ROTR    : LogicR_shift_rotate_imm<0x02, 0x01, "rotr", rotr>;
525     def ROTRV   : LogicR_shift_rotate_reg<0x06, 0x01, "rotrv", rotr>;
526 }
527
528 /// Load and Store Instructions
529 def LB      : LoadM<0x20, "lb",  sextloadi8>;
530 def LBu     : LoadM<0x24, "lbu", zextloadi8>;
531 def LH      : LoadM<0x21, "lh",  sextloadi16>;
532 def LHu     : LoadM<0x25, "lhu", zextloadi16>;
533 def LW      : LoadM<0x23, "lw",  load>;
534 def SB      : StoreM<0x28, "sb", truncstorei8>;
535 def SH      : StoreM<0x29, "sh", truncstorei16>;
536 def SW      : StoreM<0x2b, "sw", store>;
537
538 let hasSideEffects = 1 in
539 def SYNC : MipsInst<(outs), (ins i32imm:$stype), "sync $stype",
540                     [(MipsSync imm:$stype)], NoItinerary>
541 {
542   let opcode = 0;
543   let Inst{25-11} = 0;
544   let Inst{5-0} = 15;
545 }
546
547 /// Load-linked, Store-conditional
548 let mayLoad = 1, hasDelaySlot = 1 in
549   def LL    : FI<0x30, (outs CPURegs:$dst), (ins mem:$addr),
550               "ll\t$dst, $addr", [], IILoad>;
551 let mayStore = 1, Constraints = "$src = $dst" in
552   def SC    : FI<0x38, (outs CPURegs:$dst), (ins CPURegs:$src, mem:$addr),
553               "sc\t$src, $addr", [], IIStore>;
554
555 /// Jump and Branch Instructions
556 def J       : JumpFJ<0x02, "j">;
557 let isIndirectBranch = 1 in
558   def JR      : JumpFR<0x00, 0x08, "jr">;
559 def JAL     : JumpLink<0x03, "jal">;
560 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
561 def BEQ     : CBranch<0x04, "beq", seteq>;
562 def BNE     : CBranch<0x05, "bne", setne>;
563
564 let rt=1 in
565   def BGEZ  : CBranchZero<0x01, "bgez", setge>;
566
567 let rt=0 in {
568   def BGTZ  : CBranchZero<0x07, "bgtz", setgt>;
569   def BLEZ  : CBranchZero<0x07, "blez", setle>;
570   def BLTZ  : CBranchZero<0x01, "bltz", setlt>;
571 }
572
573 def BGEZAL  : BranchLink<"bgezal">;
574 def BLTZAL  : BranchLink<"bltzal">;
575
576 let isReturn=1, isTerminator=1, hasDelaySlot=1,
577     isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
578   def RET : FR <0x00, 0x02, (outs), (ins CPURegs:$target),
579                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
580
581 /// Multiply and Divide Instructions.
582 def MULT    : Mul<0x18, "mult", IIImul>;
583 def MULTu   : Mul<0x19, "multu", IIImul>;
584 def SDIV    : Div<MipsDivRem, 0x1a, "div", IIIdiv>;
585 def UDIV    : Div<MipsDivRemU, 0x1b, "divu", IIIdiv>;
586
587 let Defs = [HI] in
588   def MTHI  : MoveToLOHI<0x11, "mthi">;
589 let Defs = [LO] in
590   def MTLO  : MoveToLOHI<0x13, "mtlo">;
591
592 let Uses = [HI] in
593   def MFHI  : MoveFromLOHI<0x10, "mfhi">;
594 let Uses = [LO] in
595   def MFLO  : MoveFromLOHI<0x12, "mflo">;
596
597 /// Sign Ext In Register Instructions.
598 let Predicates = [HasSEInReg] in {
599   let shamt = 0x10, rs = 0 in
600     def SEB : SignExtInReg<0x21, "seb", i8>;
601
602   let shamt = 0x18, rs = 0 in
603     def SEH : SignExtInReg<0x20, "seh", i16>;
604 }
605
606 /// Count Leading
607 def CLZ : CountLeading<0b100000, "clz",
608                        [(set CPURegs:$dst, (ctlz CPURegs:$src))]>;
609 def CLO : CountLeading<0b100001, "clo",
610                        [(set CPURegs:$dst, (ctlz (not CPURegs:$src)))]>;
611
612 /// Byte Swap
613 let Predicates = [HasSwap] in {
614   let shamt = 0x3, rs = 0 in
615     def WSBW : ByteSwap<0x20, "wsbw">;
616 }
617
618 /// Conditional Move
619 def MIPS_CMOV_ZERO  : PatLeaf<(i32 0)>;
620 def MIPS_CMOV_NZERO : PatLeaf<(i32 1)>;
621
622 // Conditional moves:
623 // These instructions are expanded in
624 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
625 // conditional move instructions.
626 // flag:int, data:int
627 let usesCustomInserter = 1, shamt = 0, Constraints = "$F = $dst" in
628   class CondMovIntInt<bits<6> funct, string instr_asm> :
629     FR<0, funct, (outs CPURegs:$dst),
630        (ins CPURegs:$T, CPURegs:$cond, CPURegs:$F),
631        !strconcat(instr_asm, "\t$dst, $T, $cond"), [], NoItinerary>;
632
633 def MOVZ_I : CondMovIntInt<0x0a, "movz">;
634 def MOVN_I : CondMovIntInt<0x0b, "movn">;
635
636 /// No operation
637 let addr=0 in
638   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
639
640 // FrameIndexes are legalized when they are operands from load/store
641 // instructions. The same not happens for stack address copies, so an
642 // add op with mem ComplexPattern is used and the stack address copy
643 // can be matched. It's similar to Sparc LEA_ADDRi
644 def LEA_ADDiu : EffectiveAddress<"addiu\t$dst, $addr">;
645
646 // DynAlloc node points to dynamically allocated stack space.
647 // $sp is added to the list of implicitly used registers to prevent dead code
648 // elimination from removing instructions that modify $sp.
649 let Uses = [SP] in
650 def DynAlloc : EffectiveAddress<"addiu\t$dst, $addr">;
651
652 // MADD*/MSUB*
653 def MADD  : MArithR<0, "madd", MipsMAdd, 1>;
654 def MADDU : MArithR<1, "maddu", MipsMAddu, 1>;
655 def MSUB  : MArithR<4, "msub", MipsMSub>;
656 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
657
658 // MUL is a assembly macro in the current used ISAs. In recent ISA's
659 // it is a real instruction.
660 def MUL   : ArithR<0x1c, 0x02, "mul", mul, IIImul, 1>, Requires<[IsMips32]>;
661
662 def RDHWR : ReadHardware;
663
664 //===----------------------------------------------------------------------===//
665 //  Arbitrary patterns that map to one or more instructions
666 //===----------------------------------------------------------------------===//
667
668 // Small immediates
669 def : Pat<(i32 immSExt16:$in),
670           (ADDiu ZERO, imm:$in)>;
671 def : Pat<(i32 immZExt16:$in),
672           (ORi ZERO, imm:$in)>;
673
674 // Arbitrary immediates
675 def : Pat<(i32 imm:$imm),
676           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
677
678 // Carry patterns
679 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
680           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
681 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
682           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
683 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
684           (ADDiu CPURegs:$src, imm:$imm)>;
685
686 // Call
687 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
688           (JAL tglobaladdr:$dst)>;
689 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
690           (JAL texternalsym:$dst)>;
691 //def : Pat<(MipsJmpLink CPURegs:$dst),
692 //          (JALR CPURegs:$dst)>;
693
694 // hi/lo relocs
695 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
696 def : Pat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
697 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
698           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
699 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
700           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
701
702 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
703 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
704           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
705
706 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
707 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
708           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
709
710 // gp_rel relocs
711 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
712           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
713 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
714           (ADDiu CPURegs:$gp, tconstpool:$in)>;
715
716 // tlsgd
717 def : Pat<(add CPURegs:$gp, (MipsTlsGd tglobaltlsaddr:$in)),
718           (ADDiu CPURegs:$gp, tglobaltlsaddr:$in)>;
719
720 // tprel hi/lo
721 def : Pat<(MipsTprelHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
722 def : Pat<(add CPURegs:$hi, (MipsTprelLo tglobaltlsaddr:$lo)),
723           (ADDiu CPURegs:$hi, tglobaltlsaddr:$lo)>;
724
725 // wrapper_pic
726 class WrapperPICPat<SDNode node>:
727       Pat<(MipsWrapperPIC node:$in),
728           (ADDiu GP, node:$in)>;
729
730 def : WrapperPICPat<tglobaladdr>;
731 def : WrapperPICPat<tconstpool>;
732 def : WrapperPICPat<texternalsym>;
733 def : WrapperPICPat<tblockaddress>;
734 def : WrapperPICPat<tjumptable>;
735
736 // Mips does not have "not", so we expand our way
737 def : Pat<(not CPURegs:$in),
738           (NOR CPURegs:$in, ZERO)>;
739
740 // extended load and stores
741 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
742 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
743 def : Pat<(extloadi16 addr:$src), (LHu addr:$src)>;
744
745 // peepholes
746 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
747
748 // brcond patterns
749 def : Pat<(brcond (setne CPURegs:$lhs, 0), bb:$dst),
750           (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
751 def : Pat<(brcond (seteq CPURegs:$lhs, 0), bb:$dst),
752           (BEQ CPURegs:$lhs, ZERO, bb:$dst)>;
753
754 def : Pat<(brcond (setge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
755           (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
756 def : Pat<(brcond (setuge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
757           (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
758 def : Pat<(brcond (setge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
759           (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
760 def : Pat<(brcond (setuge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
761           (BEQ (SLTiu CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
762
763 def : Pat<(brcond (setle CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
764           (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
765 def : Pat<(brcond (setule CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
766           (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
767
768 def : Pat<(brcond CPURegs:$cond, bb:$dst),
769           (BNE CPURegs:$cond, ZERO, bb:$dst)>;
770
771 // select patterns
772 multiclass MovzPats<RegisterClass RC, Instruction MOVZInst> {
773   def : Pat<(select (setge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
774             (MOVZInst RC:$T, (SLT CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
775   def : Pat<(select (setuge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
776             (MOVZInst RC:$T, (SLTu CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
777   def : Pat<(select (setge CPURegs:$lhs, immSExt16:$rhs), RC:$T, RC:$F),
778             (MOVZInst RC:$T, (SLTi CPURegs:$lhs, immSExt16:$rhs), RC:$F)>;
779   def : Pat<(select (setuge CPURegs:$lh, immSExt16:$rh), RC:$T, RC:$F),
780             (MOVZInst RC:$T, (SLTiu CPURegs:$lh, immSExt16:$rh), RC:$F)>;
781   def : Pat<(select (setle CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
782             (MOVZInst RC:$T, (SLT CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
783   def : Pat<(select (setule CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
784             (MOVZInst RC:$T, (SLTu CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
785   def : Pat<(select (seteq CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
786             (MOVZInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
787   def : Pat<(select (seteq CPURegs:$lhs, 0), RC:$T, RC:$F),
788             (MOVZInst RC:$T, CPURegs:$lhs, RC:$F)>;
789 }
790
791 multiclass MovnPats<RegisterClass RC, Instruction MOVNInst> {
792   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
793             (MOVNInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
794   def : Pat<(select CPURegs:$cond, RC:$T, RC:$F),
795             (MOVNInst RC:$T, CPURegs:$cond, RC:$F)>;
796   def : Pat<(select (setne CPURegs:$lhs, 0), RC:$T, RC:$F),
797             (MOVNInst RC:$T, CPURegs:$lhs, RC:$F)>;
798 }
799
800 defm : MovzPats<CPURegs, MOVZ_I>;
801 defm : MovnPats<CPURegs, MOVN_I>;
802
803 // setcc patterns
804 def : Pat<(seteq CPURegs:$lhs, CPURegs:$rhs),
805           (SLTu (XOR CPURegs:$lhs, CPURegs:$rhs), 1)>;
806 def : Pat<(setne CPURegs:$lhs, CPURegs:$rhs),
807           (SLTu ZERO, (XOR CPURegs:$lhs, CPURegs:$rhs))>;
808
809 def : Pat<(setle CPURegs:$lhs, CPURegs:$rhs),
810           (XORi (SLT CPURegs:$rhs, CPURegs:$lhs), 1)>;
811 def : Pat<(setule CPURegs:$lhs, CPURegs:$rhs),
812           (XORi (SLTu CPURegs:$rhs, CPURegs:$lhs), 1)>;
813
814 def : Pat<(setgt CPURegs:$lhs, CPURegs:$rhs),
815           (SLT CPURegs:$rhs, CPURegs:$lhs)>;
816 def : Pat<(setugt CPURegs:$lhs, CPURegs:$rhs),
817           (SLTu CPURegs:$rhs, CPURegs:$lhs)>;
818
819 def : Pat<(setge CPURegs:$lhs, CPURegs:$rhs),
820           (XORi (SLT CPURegs:$lhs, CPURegs:$rhs), 1)>;
821 def : Pat<(setuge CPURegs:$lhs, CPURegs:$rhs),
822           (XORi (SLTu CPURegs:$lhs, CPURegs:$rhs), 1)>;
823
824 def : Pat<(setge CPURegs:$lhs, immSExt16:$rhs),
825           (XORi (SLTi CPURegs:$lhs, immSExt16:$rhs), 1)>;
826 def : Pat<(setuge CPURegs:$lhs, immSExt16:$rhs),
827           (XORi (SLTiu CPURegs:$lhs, immSExt16:$rhs), 1)>;
828
829 // select MipsDynAlloc
830 def : Pat<(MipsDynAlloc addr:$f), (DynAlloc addr:$f)>;
831
832 //===----------------------------------------------------------------------===//
833 // Floating Point Support
834 //===----------------------------------------------------------------------===//
835
836 include "MipsInstrFPU.td"
837