Add isIndirectBranch flag.
[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 //===----------------------------------------------------------------------===//
478 // Instruction definition
479 //===----------------------------------------------------------------------===//
480
481 //===----------------------------------------------------------------------===//
482 // MipsI Instructions
483 //===----------------------------------------------------------------------===//
484
485 /// Arithmetic Instructions (ALU Immediate)
486 def ADDiu   : ArithI<0x09, "addiu", add, simm16, immSExt16>;
487 def ADDi    : ArithOverflowI<0x08, "addi",  add, simm16, immSExt16>;
488 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
489 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16>;
490 def ANDi    : LogicI<0x0c, "andi", and>;
491 def ORi     : LogicI<0x0d, "ori",  or>;
492 def XORi    : LogicI<0x0e, "xori",  xor>;
493 def LUi     : LoadUpper<0x0f, "lui">;
494
495 /// Arithmetic Instructions (3-Operand, R-Type)
496 def ADDu    : ArithR<0x00, 0x21, "addu", add, IIAlu, 1>;
497 def SUBu    : ArithR<0x00, 0x23, "subu", sub, IIAlu>;
498 def ADD     : ArithOverflowR<0x00, 0x20, "add", 1>;
499 def SUB     : ArithOverflowR<0x00, 0x22, "sub">;
500 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt>;
501 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult>;
502 def AND     : LogicR<0x24, "and", and>;
503 def OR      : LogicR<0x25, "or",  or>;
504 def XOR     : LogicR<0x26, "xor", xor>;
505 def NOR     : LogicNOR<0x00, 0x27, "nor">;
506
507 /// Shift Instructions
508 def SLL     : LogicR_shift_rotate_imm<0x00, 0x00, "sll", shl>;
509 def SRL     : LogicR_shift_rotate_imm<0x02, 0x00, "srl", srl>;
510 def SRA     : LogicR_shift_rotate_imm<0x03, 0x00, "sra", sra>;
511 def SLLV    : LogicR_shift_rotate_reg<0x04, 0x00, "sllv", shl>;
512 def SRLV    : LogicR_shift_rotate_reg<0x06, 0x00, "srlv", srl>;
513 def SRAV    : LogicR_shift_rotate_reg<0x07, 0x00, "srav", sra>;
514
515 // Rotate Instructions
516 let Predicates = [IsMips32r2] in {
517     def ROTR    : LogicR_shift_rotate_imm<0x02, 0x01, "rotr", rotr>;
518     def ROTRV   : LogicR_shift_rotate_reg<0x06, 0x01, "rotrv", rotr>;
519 }
520
521 /// Load and Store Instructions
522 def LB      : LoadM<0x20, "lb",  sextloadi8>;
523 def LBu     : LoadM<0x24, "lbu", zextloadi8>;
524 def LH      : LoadM<0x21, "lh",  sextloadi16>;
525 def LHu     : LoadM<0x25, "lhu", zextloadi16>;
526 def LW      : LoadM<0x23, "lw",  load>;
527 def SB      : StoreM<0x28, "sb", truncstorei8>;
528 def SH      : StoreM<0x29, "sh", truncstorei16>;
529 def SW      : StoreM<0x2b, "sw", store>;
530
531 let hasSideEffects = 1 in
532 def SYNC : MipsInst<(outs), (ins i32imm:$stype), "sync $stype",
533                     [(MipsSync imm:$stype)], NoItinerary>
534 {
535   let opcode = 0;
536   let Inst{25-11} = 0;
537   let Inst{5-0} = 15;
538 }
539
540 /// Load-linked, Store-conditional
541 let mayLoad = 1, hasDelaySlot = 1 in
542   def LL    : FI<0x30, (outs CPURegs:$dst), (ins mem:$addr),
543               "ll\t$dst, $addr", [], IILoad>;
544 let mayStore = 1, Constraints = "$src = $dst" in
545   def SC    : FI<0x38, (outs CPURegs:$dst), (ins CPURegs:$src, mem:$addr),
546               "sc\t$src, $addr", [], IIStore>;
547
548 /// Jump and Branch Instructions
549 def J       : JumpFJ<0x02, "j">;
550 let isIndirectBranch = 1 in
551   def JR      : JumpFR<0x00, 0x08, "jr">;
552 def JAL     : JumpLink<0x03, "jal">;
553 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
554 def BEQ     : CBranch<0x04, "beq", seteq>;
555 def BNE     : CBranch<0x05, "bne", setne>;
556
557 let rt=1 in
558   def BGEZ  : CBranchZero<0x01, "bgez", setge>;
559
560 let rt=0 in {
561   def BGTZ  : CBranchZero<0x07, "bgtz", setgt>;
562   def BLEZ  : CBranchZero<0x07, "blez", setle>;
563   def BLTZ  : CBranchZero<0x01, "bltz", setlt>;
564 }
565
566 def BGEZAL  : BranchLink<"bgezal">;
567 def BLTZAL  : BranchLink<"bltzal">;
568
569 let isReturn=1, isTerminator=1, hasDelaySlot=1,
570     isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
571   def RET : FR <0x00, 0x02, (outs), (ins CPURegs:$target),
572                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
573
574 /// Multiply and Divide Instructions.
575 def MULT    : Mul<0x18, "mult", IIImul>;
576 def MULTu   : Mul<0x19, "multu", IIImul>;
577 def SDIV    : Div<MipsDivRem, 0x1a, "div", IIIdiv>;
578 def UDIV    : Div<MipsDivRemU, 0x1b, "divu", IIIdiv>;
579
580 let Defs = [HI] in
581   def MTHI  : MoveToLOHI<0x11, "mthi">;
582 let Defs = [LO] in
583   def MTLO  : MoveToLOHI<0x13, "mtlo">;
584
585 let Uses = [HI] in
586   def MFHI  : MoveFromLOHI<0x10, "mfhi">;
587 let Uses = [LO] in
588   def MFLO  : MoveFromLOHI<0x12, "mflo">;
589
590 /// Sign Ext In Register Instructions.
591 let Predicates = [HasSEInReg] in {
592   let shamt = 0x10, rs = 0 in
593     def SEB : SignExtInReg<0x21, "seb", i8>;
594
595   let shamt = 0x18, rs = 0 in
596     def SEH : SignExtInReg<0x20, "seh", i16>;
597 }
598
599 /// Count Leading
600 def CLZ : CountLeading<0b100000, "clz",
601                        [(set CPURegs:$dst, (ctlz CPURegs:$src))]>;
602 def CLO : CountLeading<0b100001, "clo",
603                        [(set CPURegs:$dst, (ctlz (not CPURegs:$src)))]>;
604
605 /// Byte Swap
606 let Predicates = [HasSwap] in {
607   let shamt = 0x3, rs = 0 in
608     def WSBW : ByteSwap<0x20, "wsbw">;
609 }
610
611 /// Conditional Move
612 def MIPS_CMOV_ZERO  : PatLeaf<(i32 0)>;
613 def MIPS_CMOV_NZERO : PatLeaf<(i32 1)>;
614
615 // Conditional moves:
616 // These instructions are expanded in
617 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
618 // conditional move instructions.
619 // flag:int, data:int
620 let usesCustomInserter = 1, shamt = 0, Constraints = "$F = $dst" in
621   class CondMovIntInt<bits<6> funct, string instr_asm> :
622     FR<0, funct, (outs CPURegs:$dst),
623        (ins CPURegs:$T, CPURegs:$cond, CPURegs:$F),
624        !strconcat(instr_asm, "\t$dst, $T, $cond"), [], NoItinerary>;
625
626 def MOVZ_I : CondMovIntInt<0x0a, "movz">;
627 def MOVN_I : CondMovIntInt<0x0b, "movn">;
628
629 /// No operation
630 let addr=0 in
631   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
632
633 // FrameIndexes are legalized when they are operands from load/store
634 // instructions. The same not happens for stack address copies, so an
635 // add op with mem ComplexPattern is used and the stack address copy
636 // can be matched. It's similar to Sparc LEA_ADDRi
637 def LEA_ADDiu : EffectiveAddress<"addiu\t$dst, $addr">;
638
639 // DynAlloc node points to dynamically allocated stack space.
640 // $sp is added to the list of implicitly used registers to prevent dead code
641 // elimination from removing instructions that modify $sp.
642 let Uses = [SP] in
643 def DynAlloc : EffectiveAddress<"addiu\t$dst, $addr">;
644
645 // MADD*/MSUB*
646 def MADD  : MArithR<0, "madd", MipsMAdd, 1>;
647 def MADDU : MArithR<1, "maddu", MipsMAddu, 1>;
648 def MSUB  : MArithR<4, "msub", MipsMSub>;
649 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
650
651 // MUL is a assembly macro in the current used ISAs. In recent ISA's
652 // it is a real instruction.
653 def MUL   : ArithR<0x1c, 0x02, "mul", mul, IIImul, 1>, Requires<[IsMips32]>;
654
655 def RDHWR : ReadHardware;
656
657 //===----------------------------------------------------------------------===//
658 //  Arbitrary patterns that map to one or more instructions
659 //===----------------------------------------------------------------------===//
660
661 // Small immediates
662 def : Pat<(i32 immSExt16:$in),
663           (ADDiu ZERO, imm:$in)>;
664 def : Pat<(i32 immZExt16:$in),
665           (ORi ZERO, imm:$in)>;
666
667 // Arbitrary immediates
668 def : Pat<(i32 imm:$imm),
669           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
670
671 // Carry patterns
672 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
673           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
674 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
675           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
676 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
677           (ADDiu CPURegs:$src, imm:$imm)>;
678
679 // Call
680 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
681           (JAL tglobaladdr:$dst)>;
682 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
683           (JAL texternalsym:$dst)>;
684 //def : Pat<(MipsJmpLink CPURegs:$dst),
685 //          (JALR CPURegs:$dst)>;
686
687 // hi/lo relocs
688 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
689 def : Pat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
690 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
691           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
692 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
693           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
694
695 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
696 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
697           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
698
699 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
700 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
701           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
702
703 // gp_rel relocs
704 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
705           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
706 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
707           (ADDiu CPURegs:$gp, tconstpool:$in)>;
708
709 // tlsgd
710 def : Pat<(add CPURegs:$gp, (MipsTlsGd tglobaltlsaddr:$in)),
711           (ADDiu CPURegs:$gp, tglobaltlsaddr:$in)>;
712
713 // tprel hi/lo
714 def : Pat<(MipsTprelHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
715 def : Pat<(add CPURegs:$hi, (MipsTprelLo tglobaltlsaddr:$lo)),
716           (ADDiu CPURegs:$hi, tglobaltlsaddr:$lo)>;
717
718 // wrapper_pic
719 class WrapperPICPat<SDNode node>:
720       Pat<(MipsWrapperPIC node:$in),
721           (ADDiu GP, node:$in)>;
722
723 def : WrapperPICPat<tglobaladdr>;
724 def : WrapperPICPat<tconstpool>;
725 def : WrapperPICPat<texternalsym>;
726 def : WrapperPICPat<tblockaddress>;
727 def : WrapperPICPat<tjumptable>;
728
729 // Mips does not have "not", so we expand our way
730 def : Pat<(not CPURegs:$in),
731           (NOR CPURegs:$in, ZERO)>;
732
733 // extended load and stores
734 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
735 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
736 def : Pat<(extloadi16 addr:$src), (LHu addr:$src)>;
737
738 // peepholes
739 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
740
741 // brcond patterns
742 def : Pat<(brcond (setne CPURegs:$lhs, 0), bb:$dst),
743           (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
744 def : Pat<(brcond (seteq CPURegs:$lhs, 0), bb:$dst),
745           (BEQ CPURegs:$lhs, ZERO, bb:$dst)>;
746
747 def : Pat<(brcond (setge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
748           (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
749 def : Pat<(brcond (setuge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
750           (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
751 def : Pat<(brcond (setge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
752           (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
753 def : Pat<(brcond (setuge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
754           (BEQ (SLTiu CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
755
756 def : Pat<(brcond (setle CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
757           (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
758 def : Pat<(brcond (setule CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
759           (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
760
761 def : Pat<(brcond CPURegs:$cond, bb:$dst),
762           (BNE CPURegs:$cond, ZERO, bb:$dst)>;
763
764 // select patterns
765 multiclass MovzPats<RegisterClass RC, Instruction MOVZInst> {
766   def : Pat<(select (setge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
767             (MOVZInst RC:$T, (SLT CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
768   def : Pat<(select (setuge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
769             (MOVZInst RC:$T, (SLTu CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
770   def : Pat<(select (setge CPURegs:$lhs, immSExt16:$rhs), RC:$T, RC:$F),
771             (MOVZInst RC:$T, (SLTi CPURegs:$lhs, immSExt16:$rhs), RC:$F)>;
772   def : Pat<(select (setuge CPURegs:$lh, immSExt16:$rh), RC:$T, RC:$F),
773             (MOVZInst RC:$T, (SLTiu CPURegs:$lh, immSExt16:$rh), RC:$F)>;
774   def : Pat<(select (setle CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
775             (MOVZInst RC:$T, (SLT CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
776   def : Pat<(select (setule CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
777             (MOVZInst RC:$T, (SLTu CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
778   def : Pat<(select (seteq CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
779             (MOVZInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
780   def : Pat<(select (seteq CPURegs:$lhs, 0), RC:$T, RC:$F),
781             (MOVZInst RC:$T, CPURegs:$lhs, RC:$F)>;
782 }
783
784 multiclass MovnPats<RegisterClass RC, Instruction MOVNInst> {
785   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
786             (MOVNInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
787   def : Pat<(select CPURegs:$cond, RC:$T, RC:$F),
788             (MOVNInst RC:$T, CPURegs:$cond, RC:$F)>;
789   def : Pat<(select (setne CPURegs:$lhs, 0), RC:$T, RC:$F),
790             (MOVNInst RC:$T, CPURegs:$lhs, RC:$F)>;
791 }
792
793 defm : MovzPats<CPURegs, MOVZ_I>;
794 defm : MovnPats<CPURegs, MOVN_I>;
795
796 // setcc patterns
797 def : Pat<(seteq CPURegs:$lhs, CPURegs:$rhs),
798           (SLTu (XOR CPURegs:$lhs, CPURegs:$rhs), 1)>;
799 def : Pat<(setne CPURegs:$lhs, CPURegs:$rhs),
800           (SLTu ZERO, (XOR CPURegs:$lhs, CPURegs:$rhs))>;
801
802 def : Pat<(setle CPURegs:$lhs, CPURegs:$rhs),
803           (XORi (SLT CPURegs:$rhs, CPURegs:$lhs), 1)>;
804 def : Pat<(setule CPURegs:$lhs, CPURegs:$rhs),
805           (XORi (SLTu CPURegs:$rhs, CPURegs:$lhs), 1)>;
806
807 def : Pat<(setgt CPURegs:$lhs, CPURegs:$rhs),
808           (SLT CPURegs:$rhs, CPURegs:$lhs)>;
809 def : Pat<(setugt CPURegs:$lhs, CPURegs:$rhs),
810           (SLTu CPURegs:$rhs, CPURegs:$lhs)>;
811
812 def : Pat<(setge CPURegs:$lhs, CPURegs:$rhs),
813           (XORi (SLT CPURegs:$lhs, CPURegs:$rhs), 1)>;
814 def : Pat<(setuge CPURegs:$lhs, CPURegs:$rhs),
815           (XORi (SLTu CPURegs:$lhs, CPURegs:$rhs), 1)>;
816
817 def : Pat<(setge CPURegs:$lhs, immSExt16:$rhs),
818           (XORi (SLTi CPURegs:$lhs, immSExt16:$rhs), 1)>;
819 def : Pat<(setuge CPURegs:$lhs, immSExt16:$rhs),
820           (XORi (SLTiu CPURegs:$lhs, immSExt16:$rhs), 1)>;
821
822 // select MipsDynAlloc
823 def : Pat<(MipsDynAlloc addr:$f), (DynAlloc addr:$f)>;
824
825 //===----------------------------------------------------------------------===//
826 // Floating Point Support
827 //===----------------------------------------------------------------------===//
828
829 include "MipsInstrFPU.td"
830