Lower memory barriers to sync instructions.
[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 //===----------------------------------------------------------------------===//
400 // Pseudo instructions
401 //===----------------------------------------------------------------------===//
402
403 // As stack alignment is always done with addiu, we need a 16-bit immediate
404 let Defs = [SP], Uses = [SP] in {
405 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins uimm16:$amt),
406                                   "!ADJCALLSTACKDOWN $amt",
407                                   [(callseq_start timm:$amt)]>;
408 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins uimm16:$amt1, uimm16:$amt2),
409                                   "!ADJCALLSTACKUP $amt1",
410                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
411 }
412
413 // Some assembly macros need to avoid pseudoinstructions and assembler
414 // automatic reodering, we should reorder ourselves.
415 def MACRO     : MipsPseudo<(outs), (ins), ".set\tmacro",     []>;
416 def REORDER   : MipsPseudo<(outs), (ins), ".set\treorder",   []>;
417 def NOMACRO   : MipsPseudo<(outs), (ins), ".set\tnomacro",   []>;
418 def NOREORDER : MipsPseudo<(outs), (ins), ".set\tnoreorder", []>;
419
420 // These macros are inserted to prevent GAS from complaining
421 // when using the AT register.
422 def NOAT      : MipsPseudo<(outs), (ins), ".set\tnoat", []>;
423 def ATMACRO   : MipsPseudo<(outs), (ins), ".set\tat", []>;
424
425 // When handling PIC code the assembler needs .cpload and .cprestore
426 // directives. If the real instructions corresponding these directives
427 // are used, we have the same behavior, but get also a bunch of warnings
428 // from the assembler.
429 def CPLOAD : MipsPseudo<(outs), (ins CPURegs:$picreg), ".cpload\t$picreg", []>;
430 def CPRESTORE : MipsPseudo<(outs), (ins i32imm:$loc), ".cprestore\t$loc", []>;
431
432 let usesCustomInserter = 1 in {
433   def ATOMIC_LOAD_ADD_I8 : MipsPseudo<
434     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
435     "atomic_load_add_8\t$dst, $ptr, $incr",
436     [(set CPURegs:$dst, (atomic_load_add_8 CPURegs:$ptr, CPURegs:$incr))]>;
437   def ATOMIC_LOAD_ADD_I16 : MipsPseudo<
438     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
439     "atomic_load_add_16\t$dst, $ptr, $incr",
440     [(set CPURegs:$dst, (atomic_load_add_16 CPURegs:$ptr, CPURegs:$incr))]>;
441   def ATOMIC_LOAD_ADD_I32 : MipsPseudo<
442     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
443     "atomic_load_add_32\t$dst, $ptr, $incr",
444     [(set CPURegs:$dst, (atomic_load_add_32 CPURegs:$ptr, CPURegs:$incr))]>;
445
446   def ATOMIC_LOAD_SUB_I8 : MipsPseudo<
447     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
448     "atomic_load_sub_8\t$dst, $ptr, $incr",
449     [(set CPURegs:$dst, (atomic_load_sub_8 CPURegs:$ptr, CPURegs:$incr))]>;
450   def ATOMIC_LOAD_SUB_I16 : MipsPseudo<
451     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
452     "atomic_load_sub_16\t$dst, $ptr, $incr",
453     [(set CPURegs:$dst, (atomic_load_sub_16 CPURegs:$ptr, CPURegs:$incr))]>;
454   def ATOMIC_LOAD_SUB_I32 : MipsPseudo<
455     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
456     "atomic_load_sub_32\t$dst, $ptr, $incr",
457     [(set CPURegs:$dst, (atomic_load_sub_32 CPURegs:$ptr, CPURegs:$incr))]>;
458
459   def ATOMIC_LOAD_AND_I8 : MipsPseudo<
460     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
461     "atomic_load_and_8\t$dst, $ptr, $incr",
462     [(set CPURegs:$dst, (atomic_load_and_8 CPURegs:$ptr, CPURegs:$incr))]>;
463   def ATOMIC_LOAD_AND_I16 : MipsPseudo<
464     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
465     "atomic_load_and_16\t$dst, $ptr, $incr",
466     [(set CPURegs:$dst, (atomic_load_and_16 CPURegs:$ptr, CPURegs:$incr))]>;
467   def ATOMIC_LOAD_AND_I32 : MipsPseudo<
468     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
469     "atomic_load_and_32\t$dst, $ptr, $incr",
470     [(set CPURegs:$dst, (atomic_load_and_32 CPURegs:$ptr, CPURegs:$incr))]>;
471
472   def ATOMIC_LOAD_OR_I8 : MipsPseudo<
473     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
474     "atomic_load_or_8\t$dst, $ptr, $incr",
475     [(set CPURegs:$dst, (atomic_load_or_8 CPURegs:$ptr, CPURegs:$incr))]>;
476   def ATOMIC_LOAD_OR_I16 : MipsPseudo<
477     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
478     "atomic_load_or_16\t$dst, $ptr, $incr",
479     [(set CPURegs:$dst, (atomic_load_or_16 CPURegs:$ptr, CPURegs:$incr))]>;
480   def ATOMIC_LOAD_OR_I32 : MipsPseudo<
481     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
482     "atomic_load_or_32\t$dst, $ptr, $incr",
483     [(set CPURegs:$dst, (atomic_load_or_32 CPURegs:$ptr, CPURegs:$incr))]>;
484
485   def ATOMIC_LOAD_XOR_I8 : MipsPseudo<
486     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
487     "atomic_load_xor_8\t$dst, $ptr, $incr",
488     [(set CPURegs:$dst, (atomic_load_xor_8 CPURegs:$ptr, CPURegs:$incr))]>;
489   def ATOMIC_LOAD_XOR_I16 : MipsPseudo<
490     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
491     "atomic_load_xor_16\t$dst, $ptr, $incr",
492     [(set CPURegs:$dst, (atomic_load_xor_16 CPURegs:$ptr, CPURegs:$incr))]>;
493   def ATOMIC_LOAD_XOR_I32 : MipsPseudo<
494     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
495     "atomic_load_xor_32\t$dst, $ptr, $incr",
496     [(set CPURegs:$dst, (atomic_load_xor_32 CPURegs:$ptr, CPURegs:$incr))]>;
497
498   def ATOMIC_LOAD_NAND_I8 : MipsPseudo<
499     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
500     "atomic_load_nand_8\t$dst, $ptr, $incr",
501     [(set CPURegs:$dst, (atomic_load_nand_8 CPURegs:$ptr, CPURegs:$incr))]>;
502   def ATOMIC_LOAD_NAND_I16 : MipsPseudo<
503     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
504     "atomic_load_nand_16\t$dst, $ptr, $incr",
505     [(set CPURegs:$dst, (atomic_load_nand_16 CPURegs:$ptr, CPURegs:$incr))]>;
506   def ATOMIC_LOAD_NAND_I32 : MipsPseudo<
507     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
508     "atomic_load_nand_32\t$dst, $ptr, $incr",
509     [(set CPURegs:$dst, (atomic_load_nand_32 CPURegs:$ptr, CPURegs:$incr))]>;
510
511   def ATOMIC_SWAP_I8 : MipsPseudo<
512     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$val),
513     "atomic_swap_8\t$dst, $ptr, $val",
514     [(set CPURegs:$dst, (atomic_swap_8 CPURegs:$ptr, CPURegs:$val))]>;
515   def ATOMIC_SWAP_I16 : MipsPseudo<
516     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$val),
517     "atomic_swap_16\t$dst, $ptr, $val",
518     [(set CPURegs:$dst, (atomic_swap_16 CPURegs:$ptr, CPURegs:$val))]>;
519   def ATOMIC_SWAP_I32 : MipsPseudo<
520     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$val),
521     "atomic_swap_32\t$dst, $ptr, $val",
522     [(set CPURegs:$dst, (atomic_swap_32 CPURegs:$ptr, CPURegs:$val))]>;
523
524   def ATOMIC_CMP_SWAP_I8 : MipsPseudo<
525     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$oldval, CPURegs:$newval),
526     "atomic_cmp_swap_8\t$dst, $ptr, $oldval, $newval",
527     [(set CPURegs:$dst,
528          (atomic_cmp_swap_8 CPURegs:$ptr, CPURegs:$oldval, CPURegs:$newval))]>;
529   def ATOMIC_CMP_SWAP_I16 : MipsPseudo<
530     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$oldval, CPURegs:$newval),
531     "atomic_cmp_swap_16\t$dst, $ptr, $oldval, $newval",
532     [(set CPURegs:$dst,
533          (atomic_cmp_swap_16 CPURegs:$ptr, CPURegs:$oldval, CPURegs:$newval))]>;
534   def ATOMIC_CMP_SWAP_I32 : MipsPseudo<
535     (outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$oldval, CPURegs:$newval),
536     "atomic_cmp_swap_32\t$dst, $ptr, $oldval, $newval",
537     [(set CPURegs:$dst,
538          (atomic_cmp_swap_32 CPURegs:$ptr, CPURegs:$oldval, CPURegs:$newval))]>;
539 }
540
541 //===----------------------------------------------------------------------===//
542 // Instruction definition
543 //===----------------------------------------------------------------------===//
544
545 //===----------------------------------------------------------------------===//
546 // MipsI Instructions
547 //===----------------------------------------------------------------------===//
548
549 /// Arithmetic Instructions (ALU Immediate)
550 def ADDiu   : ArithI<0x09, "addiu", add, simm16, immSExt16>;
551 def ADDi    : ArithOverflowI<0x08, "addi",  add, simm16, immSExt16>;
552 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
553 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16>;
554 def ANDi    : LogicI<0x0c, "andi", and>;
555 def ORi     : LogicI<0x0d, "ori",  or>;
556 def XORi    : LogicI<0x0e, "xori",  xor>;
557 def LUi     : LoadUpper<0x0f, "lui">;
558
559 /// Arithmetic Instructions (3-Operand, R-Type)
560 def ADDu    : ArithR<0x00, 0x21, "addu", add, IIAlu, 1>;
561 def SUBu    : ArithR<0x00, 0x23, "subu", sub, IIAlu>;
562 def ADD     : ArithOverflowR<0x00, 0x20, "add", 1>;
563 def SUB     : ArithOverflowR<0x00, 0x22, "sub">;
564 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt>;
565 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult>;
566 def AND     : LogicR<0x24, "and", and>;
567 def OR      : LogicR<0x25, "or",  or>;
568 def XOR     : LogicR<0x26, "xor", xor>;
569 def NOR     : LogicNOR<0x00, 0x27, "nor">;
570
571 /// Shift Instructions
572 def SLL     : LogicR_shift_rotate_imm<0x00, 0x00, "sll", shl>;
573 def SRL     : LogicR_shift_rotate_imm<0x02, 0x00, "srl", srl>;
574 def SRA     : LogicR_shift_rotate_imm<0x03, 0x00, "sra", sra>;
575 def SLLV    : LogicR_shift_rotate_reg<0x04, 0x00, "sllv", shl>;
576 def SRLV    : LogicR_shift_rotate_reg<0x06, 0x00, "srlv", srl>;
577 def SRAV    : LogicR_shift_rotate_reg<0x07, 0x00, "srav", sra>;
578
579 // Rotate Instructions
580 let Predicates = [IsMips32r2] in {
581     def ROTR    : LogicR_shift_rotate_imm<0x02, 0x01, "rotr", rotr>;
582     def ROTRV   : LogicR_shift_rotate_reg<0x06, 0x01, "rotrv", rotr>;
583 }
584
585 /// Load and Store Instructions
586 def LB      : LoadM<0x20, "lb",  sextloadi8>;
587 def LBu     : LoadM<0x24, "lbu", zextloadi8>;
588 def LH      : LoadM<0x21, "lh",  sextloadi16>;
589 def LHu     : LoadM<0x25, "lhu", zextloadi16>;
590 def LW      : LoadM<0x23, "lw",  load>;
591 def SB      : StoreM<0x28, "sb", truncstorei8>;
592 def SH      : StoreM<0x29, "sh", truncstorei16>;
593 def SW      : StoreM<0x2b, "sw", store>;
594
595 let hasSideEffects = 1 in
596 def SYNC : MipsInst<(outs), (ins i32imm:$stype), "sync $stype",
597                     [(MipsSync imm:$stype)], NoItinerary>
598 {
599   let opcode = 0;
600   let Inst{25-11} = 0;
601   let Inst{5-0} = 15;
602 }
603
604 /// Load-linked, Store-conditional
605 let mayLoad = 1, hasDelaySlot = 1 in
606   def LL    : FI<0x30, (outs CPURegs:$dst), (ins mem:$addr),
607               "ll\t$dst, $addr", [], IILoad>;
608 let mayStore = 1, Constraints = "$src = $dst" in
609   def SC    : FI<0x38, (outs CPURegs:$dst), (ins CPURegs:$src, mem:$addr),
610               "sc\t$src, $addr", [], IIStore>;
611
612 /// Jump and Branch Instructions
613 def J       : JumpFJ<0x02, "j">;
614 def JR      : JumpFR<0x00, 0x08, "jr">;
615 def JAL     : JumpLink<0x03, "jal">;
616 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
617 def BEQ     : CBranch<0x04, "beq", seteq>;
618 def BNE     : CBranch<0x05, "bne", setne>;
619
620 let rt=1 in
621   def BGEZ  : CBranchZero<0x01, "bgez", setge>;
622
623 let rt=0 in {
624   def BGTZ  : CBranchZero<0x07, "bgtz", setgt>;
625   def BLEZ  : CBranchZero<0x07, "blez", setle>;
626   def BLTZ  : CBranchZero<0x01, "bltz", setlt>;
627 }
628
629 def BGEZAL  : BranchLink<"bgezal">;
630 def BLTZAL  : BranchLink<"bltzal">;
631
632 let isReturn=1, isTerminator=1, hasDelaySlot=1,
633     isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
634   def RET : FR <0x00, 0x02, (outs), (ins CPURegs:$target),
635                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
636
637 /// Multiply and Divide Instructions.
638 def MULT    : Mul<0x18, "mult", IIImul>;
639 def MULTu   : Mul<0x19, "multu", IIImul>;
640 def SDIV    : Div<MipsDivRem, 0x1a, "div", IIIdiv>;
641 def UDIV    : Div<MipsDivRemU, 0x1b, "divu", IIIdiv>;
642
643 let Defs = [HI] in
644   def MTHI  : MoveToLOHI<0x11, "mthi">;
645 let Defs = [LO] in
646   def MTLO  : MoveToLOHI<0x13, "mtlo">;
647
648 let Uses = [HI] in
649   def MFHI  : MoveFromLOHI<0x10, "mfhi">;
650 let Uses = [LO] in
651   def MFLO  : MoveFromLOHI<0x12, "mflo">;
652
653 /// Sign Ext In Register Instructions.
654 let Predicates = [HasSEInReg] in {
655   let shamt = 0x10, rs = 0 in
656     def SEB : SignExtInReg<0x21, "seb", i8>;
657
658   let shamt = 0x18, rs = 0 in
659     def SEH : SignExtInReg<0x20, "seh", i16>;
660 }
661
662 /// Count Leading
663 def CLZ : CountLeading<0b100000, "clz",
664                        [(set CPURegs:$dst, (ctlz CPURegs:$src))]>;
665 def CLO : CountLeading<0b100001, "clo",
666                        [(set CPURegs:$dst, (ctlz (not CPURegs:$src)))]>;
667
668 /// Byte Swap
669 let Predicates = [HasSwap] in {
670   let shamt = 0x3, rs = 0 in
671     def WSBW : ByteSwap<0x20, "wsbw">;
672 }
673
674 /// Conditional Move
675 def MIPS_CMOV_ZERO  : PatLeaf<(i32 0)>;
676 def MIPS_CMOV_NZERO : PatLeaf<(i32 1)>;
677
678 // Conditional moves:
679 // These instructions are expanded in
680 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
681 // conditional move instructions.
682 // flag:int, data:int
683 let usesCustomInserter = 1, shamt = 0, Constraints = "$F = $dst" in
684   class CondMovIntInt<bits<6> funct, string instr_asm> :
685     FR<0, funct, (outs CPURegs:$dst),
686        (ins CPURegs:$T, CPURegs:$cond, CPURegs:$F),
687        !strconcat(instr_asm, "\t$dst, $T, $cond"), [], NoItinerary>;
688
689 def MOVZ_I : CondMovIntInt<0x0a, "movz">;
690 def MOVN_I : CondMovIntInt<0x0b, "movn">;
691
692 /// No operation
693 let addr=0 in
694   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
695
696 // FrameIndexes are legalized when they are operands from load/store
697 // instructions. The same not happens for stack address copies, so an
698 // add op with mem ComplexPattern is used and the stack address copy
699 // can be matched. It's similar to Sparc LEA_ADDRi
700 def LEA_ADDiu : EffectiveAddress<"addiu\t$dst, $addr">;
701
702 // DynAlloc node points to dynamically allocated stack space.
703 // $sp is added to the list of implicitly used registers to prevent dead code
704 // elimination from removing instructions that modify $sp.
705 let Uses = [SP] in
706 def DynAlloc : EffectiveAddress<"addiu\t$dst, $addr">;
707
708 // MADD*/MSUB*
709 def MADD  : MArithR<0, "madd", MipsMAdd, 1>;
710 def MADDU : MArithR<1, "maddu", MipsMAddu, 1>;
711 def MSUB  : MArithR<4, "msub", MipsMSub>;
712 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
713
714 // MUL is a assembly macro in the current used ISAs. In recent ISA's
715 // it is a real instruction.
716 def MUL   : ArithR<0x1c, 0x02, "mul", mul, IIImul, 1>, Requires<[IsMips32]>;
717
718 def RDHWR : ReadHardware;
719
720 //===----------------------------------------------------------------------===//
721 //  Arbitrary patterns that map to one or more instructions
722 //===----------------------------------------------------------------------===//
723
724 // Small immediates
725 def : Pat<(i32 immSExt16:$in),
726           (ADDiu ZERO, imm:$in)>;
727 def : Pat<(i32 immZExt16:$in),
728           (ORi ZERO, imm:$in)>;
729
730 // Arbitrary immediates
731 def : Pat<(i32 imm:$imm),
732           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
733
734 // Carry patterns
735 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
736           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
737 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
738           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
739 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
740           (ADDiu CPURegs:$src, imm:$imm)>;
741
742 // Call
743 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
744           (JAL tglobaladdr:$dst)>;
745 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
746           (JAL texternalsym:$dst)>;
747 //def : Pat<(MipsJmpLink CPURegs:$dst),
748 //          (JALR CPURegs:$dst)>;
749
750 // hi/lo relocs
751 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
752 def : Pat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
753 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
754           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
755 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
756           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
757
758 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
759 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
760           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
761
762 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
763 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
764           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
765
766 // gp_rel relocs
767 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
768           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
769 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
770           (ADDiu CPURegs:$gp, tconstpool:$in)>;
771
772 // tlsgd
773 def : Pat<(add CPURegs:$gp, (MipsTlsGd tglobaltlsaddr:$in)),
774           (ADDiu CPURegs:$gp, tglobaltlsaddr:$in)>;
775
776 // tprel hi/lo
777 def : Pat<(MipsTprelHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
778 def : Pat<(add CPURegs:$hi, (MipsTprelLo tglobaltlsaddr:$lo)),
779           (ADDiu CPURegs:$hi, tglobaltlsaddr:$lo)>;
780
781 // wrapper_pic
782 class WrapperPICPat<SDNode node>:
783       Pat<(MipsWrapperPIC node:$in),
784           (ADDiu GP, node:$in)>;
785
786 def : WrapperPICPat<tglobaladdr>;
787 def : WrapperPICPat<tconstpool>;
788 def : WrapperPICPat<texternalsym>;
789 def : WrapperPICPat<tblockaddress>;
790 def : WrapperPICPat<tjumptable>;
791
792 // Mips does not have "not", so we expand our way
793 def : Pat<(not CPURegs:$in),
794           (NOR CPURegs:$in, ZERO)>;
795
796 // extended load and stores
797 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
798 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
799 def : Pat<(extloadi16 addr:$src), (LHu addr:$src)>;
800
801 // peepholes
802 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
803
804 // brcond patterns
805 def : Pat<(brcond (setne CPURegs:$lhs, 0), bb:$dst),
806           (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
807 def : Pat<(brcond (seteq CPURegs:$lhs, 0), bb:$dst),
808           (BEQ CPURegs:$lhs, ZERO, bb:$dst)>;
809
810 def : Pat<(brcond (setge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
811           (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
812 def : Pat<(brcond (setuge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
813           (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
814 def : Pat<(brcond (setge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
815           (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
816 def : Pat<(brcond (setuge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
817           (BEQ (SLTiu CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
818
819 def : Pat<(brcond (setle CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
820           (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
821 def : Pat<(brcond (setule CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
822           (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
823
824 def : Pat<(brcond CPURegs:$cond, bb:$dst),
825           (BNE CPURegs:$cond, ZERO, bb:$dst)>;
826
827 // select patterns
828 multiclass MovzPats<RegisterClass RC, Instruction MOVZInst> {
829   def : Pat<(select (setge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
830             (MOVZInst RC:$T, (SLT CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
831   def : Pat<(select (setuge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
832             (MOVZInst RC:$T, (SLTu CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
833   def : Pat<(select (setge CPURegs:$lhs, immSExt16:$rhs), RC:$T, RC:$F),
834             (MOVZInst RC:$T, (SLTi CPURegs:$lhs, immSExt16:$rhs), RC:$F)>;
835   def : Pat<(select (setuge CPURegs:$lh, immSExt16:$rh), RC:$T, RC:$F),
836             (MOVZInst RC:$T, (SLTiu CPURegs:$lh, immSExt16:$rh), RC:$F)>;
837   def : Pat<(select (setle CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
838             (MOVZInst RC:$T, (SLT CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
839   def : Pat<(select (setule CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
840             (MOVZInst RC:$T, (SLTu CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
841   def : Pat<(select (seteq CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
842             (MOVZInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
843   def : Pat<(select (seteq CPURegs:$lhs, 0), RC:$T, RC:$F),
844             (MOVZInst RC:$T, CPURegs:$lhs, RC:$F)>;
845 }
846
847 multiclass MovnPats<RegisterClass RC, Instruction MOVNInst> {
848   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
849             (MOVNInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
850   def : Pat<(select CPURegs:$cond, RC:$T, RC:$F),
851             (MOVNInst RC:$T, CPURegs:$cond, RC:$F)>;
852   def : Pat<(select (setne CPURegs:$lhs, 0), RC:$T, RC:$F),
853             (MOVNInst RC:$T, CPURegs:$lhs, RC:$F)>;
854 }
855
856 defm : MovzPats<CPURegs, MOVZ_I>;
857 defm : MovnPats<CPURegs, MOVN_I>;
858
859 // setcc patterns
860 def : Pat<(seteq CPURegs:$lhs, CPURegs:$rhs),
861           (SLTu (XOR CPURegs:$lhs, CPURegs:$rhs), 1)>;
862 def : Pat<(setne CPURegs:$lhs, CPURegs:$rhs),
863           (SLTu ZERO, (XOR CPURegs:$lhs, CPURegs:$rhs))>;
864
865 def : Pat<(setle CPURegs:$lhs, CPURegs:$rhs),
866           (XORi (SLT CPURegs:$rhs, CPURegs:$lhs), 1)>;
867 def : Pat<(setule CPURegs:$lhs, CPURegs:$rhs),
868           (XORi (SLTu CPURegs:$rhs, CPURegs:$lhs), 1)>;
869
870 def : Pat<(setgt CPURegs:$lhs, CPURegs:$rhs),
871           (SLT CPURegs:$rhs, CPURegs:$lhs)>;
872 def : Pat<(setugt CPURegs:$lhs, CPURegs:$rhs),
873           (SLTu CPURegs:$rhs, CPURegs:$lhs)>;
874
875 def : Pat<(setge CPURegs:$lhs, CPURegs:$rhs),
876           (XORi (SLT CPURegs:$lhs, CPURegs:$rhs), 1)>;
877 def : Pat<(setuge CPURegs:$lhs, CPURegs:$rhs),
878           (XORi (SLTu CPURegs:$lhs, CPURegs:$rhs), 1)>;
879
880 def : Pat<(setge CPURegs:$lhs, immSExt16:$rhs),
881           (XORi (SLTi CPURegs:$lhs, immSExt16:$rhs), 1)>;
882 def : Pat<(setuge CPURegs:$lhs, immSExt16:$rhs),
883           (XORi (SLTiu CPURegs:$lhs, immSExt16:$rhs), 1)>;
884
885 // select MipsDynAlloc
886 def : Pat<(MipsDynAlloc addr:$f), (DynAlloc addr:$f)>;
887
888 //===----------------------------------------------------------------------===//
889 // Floating Point Support
890 //===----------------------------------------------------------------------===//
891
892 include "MipsInstrFPU.td"
893