Reverse unnecessary changes made in r129606 and r129608. There is no change in functi...
[oota-llvm.git] / lib / Target / Mips / MipsInstrInfo.td
1 //===- MipsInstrInfo.td - Mips Register defs ---------------*- 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 //===----------------------------------------------------------------------===//
11 // Instruction format superclass
12 //===----------------------------------------------------------------------===//
13
14 include "MipsInstrFormats.td"
15
16 //===----------------------------------------------------------------------===//
17 // Mips profiles and nodes
18 //===----------------------------------------------------------------------===//
19
20 def SDT_MipsRet          : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
21 def SDT_MipsJmpLink      : SDTypeProfile<0, 1, [SDTCisVT<0, iPTR>]>;
22 def SDT_MipsCMov         : SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
23                                                 SDTCisSameAs<1, 2>,
24                                                 SDTCisSameAs<3, 4>,
25                                                 SDTCisInt<4>]>;
26 def SDT_MipsCallSeqStart : SDCallSeqStart<[SDTCisVT<0, i32>]>;
27 def SDT_MipsCallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
28 def SDT_MipsMAddMSub     : SDTypeProfile<0, 4,
29                                          [SDTCisVT<0, i32>, SDTCisSameAs<0, 1>,
30                                           SDTCisSameAs<1, 2>,
31                                           SDTCisSameAs<2, 3>]>;
32 def SDT_MipsDivRem       : SDTypeProfile<0, 2,
33                                          [SDTCisVT<0, i32>,
34                                           SDTCisSameAs<0, 1>]>;
35
36 // Call
37 def MipsJmpLink : SDNode<"MipsISD::JmpLink",SDT_MipsJmpLink,
38                          [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,
39                           SDNPVariadic]>;
40
41 // Hi and Lo nodes are used to handle global addresses. Used on
42 // MipsISelLowering to lower stuff like GlobalAddress, ExternalSymbol
43 // static model. (nothing to do with Mips Registers Hi and Lo)
44 def MipsHi    : SDNode<"MipsISD::Hi", SDTIntUnaryOp>;
45 def MipsLo    : SDNode<"MipsISD::Lo", SDTIntUnaryOp>;
46 def MipsGPRel : SDNode<"MipsISD::GPRel", SDTIntUnaryOp>;
47
48 // Return
49 def MipsRet : SDNode<"MipsISD::Ret", SDT_MipsRet, [SDNPHasChain,
50                      SDNPOptInGlue]>;
51
52 // These are target-independent nodes, but have target-specific formats.
53 def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_MipsCallSeqStart,
54                            [SDNPHasChain, SDNPOutGlue]>;
55 def callseq_end   : SDNode<"ISD::CALLSEQ_END", SDT_MipsCallSeqEnd,
56                            [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
57
58 // MAdd*/MSub* nodes
59 def MipsMAdd      : SDNode<"MipsISD::MAdd", SDT_MipsMAddMSub,
60                            [SDNPOptInGlue, SDNPOutGlue]>;
61 def MipsMAddu     : SDNode<"MipsISD::MAddu", SDT_MipsMAddMSub,
62                            [SDNPOptInGlue, SDNPOutGlue]>;
63 def MipsMSub      : SDNode<"MipsISD::MSub", SDT_MipsMAddMSub,
64                            [SDNPOptInGlue, SDNPOutGlue]>;
65 def MipsMSubu     : SDNode<"MipsISD::MSubu", SDT_MipsMAddMSub,
66                            [SDNPOptInGlue, SDNPOutGlue]>;
67
68 // DivRem(u) nodes
69 def MipsDivRem    : SDNode<"MipsISD::DivRem", SDT_MipsDivRem,
70                            [SDNPOutGlue]>;
71 def MipsDivRemU   : SDNode<"MipsISD::DivRemU", SDT_MipsDivRem,
72                            [SDNPOutGlue]>;
73
74 //===----------------------------------------------------------------------===//
75 // Mips Instruction Predicate Definitions.
76 //===----------------------------------------------------------------------===//
77 def HasSEInReg  : Predicate<"Subtarget.hasSEInReg()">;
78 def HasBitCount : Predicate<"Subtarget.hasBitCount()">;
79 def HasSwap     : Predicate<"Subtarget.hasSwap()">;
80 def HasCondMov  : Predicate<"Subtarget.hasCondMov()">;
81 def IsMips32    : Predicate<"Subtarget.isMips32()">;
82 def IsMips32r2  : Predicate<"Subtarget.isMips32r2()">;
83
84 //===----------------------------------------------------------------------===//
85 // Mips Operand, Complex Patterns and Transformations Definitions.
86 //===----------------------------------------------------------------------===//
87
88 // Instruction operand types
89 def brtarget    : Operand<OtherVT>;
90 def calltarget  : Operand<i32>;
91 def simm16      : Operand<i32>;
92 def shamt       : Operand<i32>;
93
94 // Unsigned Operand
95 def uimm16      : Operand<i32> {
96   let PrintMethod = "printUnsignedImm";
97 }
98
99 // Address operand
100 def mem : Operand<i32> {
101   let PrintMethod = "printMemOperand";
102   let MIOperandInfo = (ops simm16, CPURegs);
103 }
104
105 // Transformation Function - get the lower 16 bits.
106 def LO16 : SDNodeXForm<imm, [{
107   return getI32Imm((unsigned)N->getZExtValue() & 0xFFFF);
108 }]>;
109
110 // Transformation Function - get the higher 16 bits.
111 def HI16 : SDNodeXForm<imm, [{
112   return getI32Imm((unsigned)N->getZExtValue() >> 16);
113 }]>;
114
115 // Node immediate fits as 16-bit sign extended on target immediate.
116 // e.g. addi, andi
117 def immSExt16  : PatLeaf<(imm), [{ return isInt<16>(N->getSExtValue()); }]>;
118
119 // Node immediate fits as 16-bit zero extended on target immediate.
120 // The LO16 param means that only the lower 16 bits of the node
121 // immediate are caught.
122 // e.g. addiu, sltiu
123 def immZExt16  : PatLeaf<(imm), [{
124   if (N->getValueType(0) == MVT::i32)
125     return (uint32_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
126   else
127     return (uint64_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
128 }], LO16>;
129
130 // shamt field must fit in 5 bits.
131 def immZExt5 : PatLeaf<(imm), [{
132   return N->getZExtValue() == ((N->getZExtValue()) & 0x1f) ;
133 }]>;
134
135 // Mips Address Mode! SDNode frameindex could possibily be a match
136 // since load and store instructions from stack used it.
137 def addr : ComplexPattern<iPTR, 2, "SelectAddr", [frameindex], []>;
138
139 //===----------------------------------------------------------------------===//
140 // Instructions specific format
141 //===----------------------------------------------------------------------===//
142
143 // Arithmetic 3 register operands
144 let isCommutable = 1 in
145 class ArithR<bits<6> op, bits<6> func, string instr_asm, SDNode OpNode,
146              InstrItinClass itin>:
147   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
148      !strconcat(instr_asm, "\t$dst, $b, $c"),
149      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], itin>;
150
151 let isCommutable = 1 in
152 class ArithOverflowR<bits<6> op, bits<6> func, string instr_asm>:
153   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
154      !strconcat(instr_asm, "\t$dst, $b, $c"), [], IIAlu>;
155
156 // Arithmetic 2 register operands
157 class ArithI<bits<6> op, string instr_asm, SDNode OpNode,
158              Operand Od, PatLeaf imm_type> :
159   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
160      !strconcat(instr_asm, "\t$dst, $b, $c"),
161      [(set CPURegs:$dst, (OpNode CPURegs:$b, imm_type:$c))], IIAlu>;
162
163 class ArithOverflowI<bits<6> op, string instr_asm, SDNode OpNode,
164              Operand Od, PatLeaf imm_type> :
165   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
166      !strconcat(instr_asm, "\t$dst, $b, $c"), [], IIAlu>;
167
168 // Arithmetic Multiply ADD/SUB
169 let rd = 0, shamt = 0, Defs = [HI, LO], Uses = [HI, LO] in
170 class MArithR<bits<6> func, string instr_asm, SDNode op> :
171   FR<0x1c, func, (outs), (ins CPURegs:$rs, CPURegs:$rt),
172      !strconcat(instr_asm, "\t$rs, $rt"),
173      [(op CPURegs:$rs, CPURegs:$rt, LO, HI)], IIImul>;
174
175 //  Logical
176 class LogicR<bits<6> func, string instr_asm, SDNode OpNode>:
177   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
178      !strconcat(instr_asm, "\t$dst, $b, $c"),
179      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], IIAlu>;
180
181 class LogicI<bits<6> op, string instr_asm, SDNode OpNode>:
182   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, uimm16:$c),
183      !strconcat(instr_asm, "\t$dst, $b, $c"),
184      [(set CPURegs:$dst, (OpNode CPURegs:$b, immZExt16:$c))], IIAlu>;
185
186 class LogicNOR<bits<6> op, bits<6> func, string instr_asm>:
187   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
188      !strconcat(instr_asm, "\t$dst, $b, $c"),
189      [(set CPURegs:$dst, (not (or CPURegs:$b, CPURegs:$c)))], IIAlu>;
190
191 // Shifts
192 class LogicR_shift_rotate_imm<bits<6> func, bits<5> _rs, string instr_asm,
193                               SDNode OpNode>:
194   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$b, shamt:$c),
195      !strconcat(instr_asm, "\t$dst, $b, $c"),
196      [(set CPURegs:$dst, (OpNode CPURegs:$b, immZExt5:$c))], IIAlu> {
197   let rs = _rs;
198 }
199
200 class LogicR_shift_rotate_reg<bits<6> func, bits<5> _shamt, string instr_asm,
201                               SDNode OpNode>:
202   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$c, CPURegs:$b),
203      !strconcat(instr_asm, "\t$dst, $b, $c"),
204      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], IIAlu> {
205   let shamt = _shamt;
206 }
207
208 // Load Upper Imediate
209 class LoadUpper<bits<6> op, string instr_asm>:
210   FI< op,
211       (outs CPURegs:$dst),
212       (ins uimm16:$imm),
213       !strconcat(instr_asm, "\t$dst, $imm"),
214       [], IIAlu>;
215
216 // Memory Load/Store
217 let canFoldAsLoad = 1, hasDelaySlot = 1 in
218 class LoadM<bits<6> op, string instr_asm, PatFrag OpNode>:
219   FI<op, (outs CPURegs:$dst), (ins mem:$addr),
220      !strconcat(instr_asm, "\t$dst, $addr"),
221      [(set CPURegs:$dst, (OpNode addr:$addr))], IILoad>;
222
223 class StoreM<bits<6> op, string instr_asm, PatFrag OpNode>:
224   FI<op, (outs), (ins CPURegs:$dst, mem:$addr),
225      !strconcat(instr_asm, "\t$dst, $addr"),
226      [(OpNode CPURegs:$dst, addr:$addr)], IIStore>;
227
228 // Conditional Branch
229 let isBranch = 1, isTerminator=1, hasDelaySlot = 1 in {
230 class CBranch<bits<6> op, string instr_asm, PatFrag cond_op>:
231   FI<op, (outs), (ins CPURegs:$a, CPURegs:$b, brtarget:$offset),
232      !strconcat(instr_asm, "\t$a, $b, $offset"),
233      [(brcond (cond_op CPURegs:$a, CPURegs:$b), bb:$offset)],
234      IIBranch>;
235
236 class CBranchZero<bits<6> op, string instr_asm, PatFrag cond_op>:
237   FI<op, (outs), (ins CPURegs:$src, brtarget:$offset),
238      !strconcat(instr_asm, "\t$src, $offset"),
239      [(brcond (cond_op CPURegs:$src, 0), bb:$offset)],
240      IIBranch>;
241 }
242
243 // SetCC
244 class SetCC_R<bits<6> op, bits<6> func, string instr_asm,
245       PatFrag cond_op>:
246   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
247      !strconcat(instr_asm, "\t$dst, $b, $c"),
248      [(set CPURegs:$dst, (cond_op CPURegs:$b, CPURegs:$c))],
249      IIAlu>;
250
251 class SetCC_I<bits<6> op, string instr_asm, PatFrag cond_op,
252       Operand Od, PatLeaf imm_type>:
253   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
254      !strconcat(instr_asm, "\t$dst, $b, $c"),
255      [(set CPURegs:$dst, (cond_op CPURegs:$b, imm_type:$c))],
256      IIAlu>;
257
258 // Unconditional branch
259 let isBranch=1, isTerminator=1, isBarrier=1, hasDelaySlot = 1 in
260 class JumpFJ<bits<6> op, string instr_asm>:
261   FJ<op, (outs), (ins brtarget:$target),
262      !strconcat(instr_asm, "\t$target"), [(br bb:$target)], IIBranch>;
263
264 let isBranch=1, isTerminator=1, isBarrier=1, rd=0, hasDelaySlot = 1 in
265 class JumpFR<bits<6> op, bits<6> func, string instr_asm>:
266   FR<op, func, (outs), (ins CPURegs:$target),
267      !strconcat(instr_asm, "\t$target"), [(brind CPURegs:$target)], IIBranch>;
268
269 // Jump and Link (Call)
270 let isCall=1, hasDelaySlot=1,
271   // All calls clobber the non-callee saved registers...
272   Defs = [AT, V0, V1, A0, A1, A2, A3, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9,
273           K0, K1, D0, D1, D2, D3, D4, D5, D6, D7, D8, D9], Uses = [GP] in {
274   class JumpLink<bits<6> op, string instr_asm>:
275     FJ<op, (outs), (ins calltarget:$target, variable_ops),
276        !strconcat(instr_asm, "\t$target"), [(MipsJmpLink imm:$target)],
277        IIBranch>;
278
279   let rd=31 in
280   class JumpLinkReg<bits<6> op, bits<6> func, string instr_asm>:
281     FR<op, func, (outs), (ins CPURegs:$rs, variable_ops),
282        !strconcat(instr_asm, "\t$rs"), [(MipsJmpLink CPURegs:$rs)], IIBranch>;
283
284   class BranchLink<string instr_asm>:
285     FI<0x1, (outs), (ins CPURegs:$rs, brtarget:$target, variable_ops),
286        !strconcat(instr_asm, "\t$rs, $target"), [], IIBranch>;
287 }
288
289 // Mul, Div
290 let Defs = [HI, LO] in {
291   class Mul<bits<6> func, string instr_asm, InstrItinClass itin>:
292     FR<0x00, func, (outs), (ins CPURegs:$a, CPURegs:$b),
293        !strconcat(instr_asm, "\t$a, $b"), [], itin>;
294
295   class Div<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin>:
296             FR<0x00, func, (outs), (ins CPURegs:$a, CPURegs:$b),
297             !strconcat(instr_asm, "\t$$zero, $a, $b"),
298             [(op CPURegs:$a, CPURegs:$b)], itin>;
299 }
300
301 // Move from Hi/Lo
302 class MoveFromLOHI<bits<6> func, string instr_asm>:
303   FR<0x00, func, (outs CPURegs:$dst), (ins),
304      !strconcat(instr_asm, "\t$dst"), [], IIHiLo>;
305
306 class MoveToLOHI<bits<6> func, string instr_asm>:
307   FR<0x00, func, (outs), (ins CPURegs:$src),
308      !strconcat(instr_asm, "\t$src"), [], IIHiLo>;
309
310 class EffectiveAddress<string instr_asm> :
311   FI<0x09, (outs CPURegs:$dst), (ins mem:$addr),
312      instr_asm, [(set CPURegs:$dst, addr:$addr)], IIAlu>;
313
314 // Count Leading Ones/Zeros in Word
315 class CountLeading<bits<6> func, string instr_asm, list<dag> pattern>:
316   FR<0x1c, func, (outs CPURegs:$dst), (ins CPURegs:$src),
317      !strconcat(instr_asm, "\t$dst, $src"), pattern, IIAlu>,
318      Requires<[HasBitCount]> {
319   let shamt = 0;
320   let rt = rd;
321 }
322
323 // Sign Extend in Register.
324 class SignExtInReg<bits<6> func, string instr_asm, ValueType vt>:
325   FR<0x3f, func, (outs CPURegs:$dst), (ins CPURegs:$src),
326      !strconcat(instr_asm, "\t$dst, $src"),
327      [(set CPURegs:$dst, (sext_inreg CPURegs:$src, vt))], NoItinerary>;
328
329 // Byte Swap
330 class ByteSwap<bits<6> func, string instr_asm>:
331   FR<0x1f, func, (outs CPURegs:$dst), (ins CPURegs:$src),
332      !strconcat(instr_asm, "\t$dst, $src"),
333      [(set CPURegs:$dst, (bswap CPURegs:$src))], NoItinerary>;
334
335 // Conditional Move
336 class CondMov<bits<6> func, string instr_asm, PatLeaf MovCode>:
337   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$F, CPURegs:$T,
338      CPURegs:$cond), !strconcat(instr_asm, "\t$dst, $T, $cond"),
339      [], NoItinerary>;
340
341 //===----------------------------------------------------------------------===//
342 // Pseudo instructions
343 //===----------------------------------------------------------------------===//
344
345 // As stack alignment is always done with addiu, we need a 16-bit immediate
346 let Defs = [SP], Uses = [SP] in {
347 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins uimm16:$amt),
348                                   "!ADJCALLSTACKDOWN $amt",
349                                   [(callseq_start timm:$amt)]>;
350 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins uimm16:$amt1, uimm16:$amt2),
351                                   "!ADJCALLSTACKUP $amt1",
352                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
353 }
354
355 // Some assembly macros need to avoid pseudoinstructions and assembler
356 // automatic reodering, we should reorder ourselves.
357 def MACRO     : MipsPseudo<(outs), (ins), ".set\tmacro",     []>;
358 def REORDER   : MipsPseudo<(outs), (ins), ".set\treorder",   []>;
359 def NOMACRO   : MipsPseudo<(outs), (ins), ".set\tnomacro",   []>;
360 def NOREORDER : MipsPseudo<(outs), (ins), ".set\tnoreorder", []>;
361
362 // These macros are inserted to prevent GAS from complaining
363 // when using the AT register.
364 def NOAT      : MipsPseudo<(outs), (ins), ".set\tnoat", []>;
365 def ATMACRO   : MipsPseudo<(outs), (ins), ".set\tat", []>;
366
367 // When handling PIC code the assembler needs .cpload and .cprestore
368 // directives. If the real instructions corresponding these directives
369 // are used, we have the same behavior, but get also a bunch of warnings
370 // from the assembler.
371 def CPLOAD : MipsPseudo<(outs), (ins CPURegs:$picreg), ".cpload\t$picreg", []>;
372 def CPRESTORE : MipsPseudo<(outs), (ins uimm16:$loc), ".cprestore\t$loc\n", []>;
373
374 //===----------------------------------------------------------------------===//
375 // Instruction definition
376 //===----------------------------------------------------------------------===//
377
378 //===----------------------------------------------------------------------===//
379 // MipsI Instructions
380 //===----------------------------------------------------------------------===//
381
382 /// Arithmetic Instructions (ALU Immediate)
383 def ADDiu   : ArithI<0x09, "addiu", add, simm16, immSExt16>;
384 def ADDi    : ArithOverflowI<0x08, "addi",  add, simm16, immSExt16>;
385 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
386 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16>;
387 def ANDi    : LogicI<0x0c, "andi", and>;
388 def ORi     : LogicI<0x0d, "ori",  or>;
389 def XORi    : LogicI<0x0e, "xori",  xor>;
390 def LUi     : LoadUpper<0x0f, "lui">;
391
392 /// Arithmetic Instructions (3-Operand, R-Type)
393 def ADDu    : ArithR<0x00, 0x21, "addu", add, IIAlu>;
394 def SUBu    : ArithR<0x00, 0x23, "subu", sub, IIAlu>;
395 def ADD     : ArithOverflowR<0x00, 0x20, "add">;
396 def SUB     : ArithOverflowR<0x00, 0x22, "sub">;
397 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt>;
398 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult>;
399 def AND     : LogicR<0x24, "and", and>;
400 def OR      : LogicR<0x25, "or",  or>;
401 def XOR     : LogicR<0x26, "xor", xor>;
402 def NOR     : LogicNOR<0x00, 0x27, "nor">;
403
404 /// Shift Instructions
405 def SLL     : LogicR_shift_rotate_imm<0x00, 0x00, "sll", shl>;
406 def SRL     : LogicR_shift_rotate_imm<0x02, 0x00, "srl", srl>;
407 def SRA     : LogicR_shift_rotate_imm<0x03, 0x00, "sra", sra>;
408 def SLLV    : LogicR_shift_rotate_reg<0x04, 0x00, "sllv", shl>;
409 def SRLV    : LogicR_shift_rotate_reg<0x06, 0x00, "srlv", srl>;
410 def SRAV    : LogicR_shift_rotate_reg<0x07, 0x00, "srav", sra>;
411
412 // Rotate Instructions
413 let Predicates = [IsMips32r2] in {
414     def ROTR    : LogicR_shift_rotate_imm<0x02, 0x01, "rotr", rotr>;
415     def ROTRV   : LogicR_shift_rotate_reg<0x06, 0x01, "rotrv", rotr>;
416 }
417
418 /// Load and Store Instructions
419 def LB      : LoadM<0x20, "lb",  sextloadi8>;
420 def LBu     : LoadM<0x24, "lbu", zextloadi8>;
421 def LH      : LoadM<0x21, "lh",  sextloadi16>;
422 def LHu     : LoadM<0x25, "lhu", zextloadi16>;
423 def LW      : LoadM<0x23, "lw",  load>;
424 def SB      : StoreM<0x28, "sb", truncstorei8>;
425 def SH      : StoreM<0x29, "sh", truncstorei16>;
426 def SW      : StoreM<0x2b, "sw", store>;
427
428 /// Jump and Branch Instructions
429 def J       : JumpFJ<0x02, "j">;
430 def JR      : JumpFR<0x00, 0x08, "jr">;
431 def JAL     : JumpLink<0x03, "jal">;
432 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
433 def BEQ     : CBranch<0x04, "beq", seteq>;
434 def BNE     : CBranch<0x05, "bne", setne>;
435
436 let rt=1 in
437   def BGEZ  : CBranchZero<0x01, "bgez", setge>;
438
439 let rt=0 in {
440   def BGTZ  : CBranchZero<0x07, "bgtz", setgt>;
441   def BLEZ  : CBranchZero<0x07, "blez", setle>;
442   def BLTZ  : CBranchZero<0x01, "bltz", setlt>;
443 }
444
445 def BGEZAL  : BranchLink<"bgezal">;
446 def BLTZAL  : BranchLink<"bltzal">;
447
448 let isReturn=1, isTerminator=1, hasDelaySlot=1,
449     isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
450   def RET : FR <0x00, 0x02, (outs), (ins CPURegs:$target),
451                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
452
453 /// Multiply and Divide Instructions.
454 def MULT    : Mul<0x18, "mult", IIImul>;
455 def MULTu   : Mul<0x19, "multu", IIImul>;
456 def SDIV    : Div<MipsDivRem, 0x1a, "div", IIIdiv>;
457 def UDIV    : Div<MipsDivRemU, 0x1b, "divu", IIIdiv>;
458
459 let Defs = [HI] in
460   def MTHI  : MoveToLOHI<0x11, "mthi">;
461 let Defs = [LO] in
462   def MTLO  : MoveToLOHI<0x13, "mtlo">;
463
464 let Uses = [HI] in
465   def MFHI  : MoveFromLOHI<0x10, "mfhi">;
466 let Uses = [LO] in
467   def MFLO  : MoveFromLOHI<0x12, "mflo">;
468
469 /// Sign Ext In Register Instructions.
470 let Predicates = [HasSEInReg] in {
471   let shamt = 0x10, rs = 0 in
472     def SEB : SignExtInReg<0x21, "seb", i8>;
473
474   let shamt = 0x18, rs = 0 in
475     def SEH : SignExtInReg<0x20, "seh", i16>;
476 }
477
478 /// Count Leading
479 def CLZ : CountLeading<0b100000, "clz",
480                        [(set CPURegs:$dst, (ctlz CPURegs:$src))]>;
481 def CLO : CountLeading<0b100001, "clo",
482                        [(set CPURegs:$dst, (ctlz (not CPURegs:$src)))]>;
483
484 /// Byte Swap
485 let Predicates = [HasSwap] in {
486   let shamt = 0x3, rs = 0 in
487     def WSBW : ByteSwap<0x20, "wsbw">;
488 }
489
490 /// Conditional Move
491 def MIPS_CMOV_ZERO  : PatLeaf<(i32 0)>;
492 def MIPS_CMOV_NZERO : PatLeaf<(i32 1)>;
493
494 // Conditional moves:
495 // These instructions are expanded in
496 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
497 // conditional move instructions.
498 // flag:int, data:int
499 let usesCustomInserter = 1, shamt = 0, Constraints = "$F = $dst" in
500   class CondMovIntInt<bits<6> funct, string instr_asm> :
501     FR<0, funct, (outs CPURegs:$dst),
502        (ins CPURegs:$T, CPURegs:$cond, CPURegs:$F),
503        !strconcat(instr_asm, "\t$dst, $T, $cond"), [], NoItinerary>;
504
505 def MOVZ_I : CondMovIntInt<0x0a, "movz">;
506 def MOVN_I : CondMovIntInt<0x0b, "movn">;
507
508 /// No operation
509 let addr=0 in
510   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
511
512 // FrameIndexes are legalized when they are operands from load/store
513 // instructions. The same not happens for stack address copies, so an
514 // add op with mem ComplexPattern is used and the stack address copy
515 // can be matched. It's similar to Sparc LEA_ADDRi
516 def LEA_ADDiu : EffectiveAddress<"addiu\t$dst, ${addr:stackloc}">;
517
518 // MADD*/MSUB*
519 def MADD  : MArithR<0, "madd", MipsMAdd>;
520 def MADDU : MArithR<1, "maddu", MipsMAddu>;
521 def MSUB  : MArithR<4, "msub", MipsMSub>;
522 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
523
524 // MUL is a assembly macro in the current used ISAs. In recent ISA's
525 // it is a real instruction.
526 def MUL   : ArithR<0x1c, 0x02, "mul", mul, IIImul>, Requires<[IsMips32]>;
527
528 //===----------------------------------------------------------------------===//
529 //  Arbitrary patterns that map to one or more instructions
530 //===----------------------------------------------------------------------===//
531
532 // Small immediates
533 def : Pat<(i32 immSExt16:$in),
534           (ADDiu ZERO, imm:$in)>;
535 def : Pat<(i32 immZExt16:$in),
536           (ORi ZERO, imm:$in)>;
537
538 // Arbitrary immediates
539 def : Pat<(i32 imm:$imm),
540           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
541
542 // Carry patterns
543 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
544           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
545 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
546           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
547 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
548           (ADDiu CPURegs:$src, imm:$imm)>;
549
550 // Call
551 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
552           (JAL tglobaladdr:$dst)>;
553 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
554           (JAL texternalsym:$dst)>;
555 //def : Pat<(MipsJmpLink CPURegs:$dst),
556 //          (JALR CPURegs:$dst)>;
557
558 // hi/lo relocs
559 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
560 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
561           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
562 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
563           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
564
565 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
566 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
567           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
568
569 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
570 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
571           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
572
573 // gp_rel relocs
574 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
575           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
576 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
577           (ADDiu CPURegs:$gp, tconstpool:$in)>;
578
579 // Mips does not have "not", so we expand our way
580 def : Pat<(not CPURegs:$in),
581           (NOR CPURegs:$in, ZERO)>;
582
583 // extended load and stores
584 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
585 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
586 def : Pat<(extloadi16 addr:$src), (LHu addr:$src)>;
587
588 // peepholes
589 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
590
591 // brcond patterns
592 def : Pat<(brcond (setne CPURegs:$lhs, 0), bb:$dst),
593           (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
594 def : Pat<(brcond (seteq CPURegs:$lhs, 0), bb:$dst),
595           (BEQ CPURegs:$lhs, ZERO, bb:$dst)>;
596
597 def : Pat<(brcond (setge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
598           (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
599 def : Pat<(brcond (setuge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
600           (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
601 def : Pat<(brcond (setge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
602           (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
603 def : Pat<(brcond (setuge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
604           (BEQ (SLTiu CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
605
606 def : Pat<(brcond (setle CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
607           (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
608 def : Pat<(brcond (setule CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
609           (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
610
611 def : Pat<(brcond CPURegs:$cond, bb:$dst),
612           (BNE CPURegs:$cond, ZERO, bb:$dst)>;
613
614 // select patterns
615 multiclass MovzPats<RegisterClass RC, Instruction MOVZInst> {
616   def : Pat<(select (setge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
617             (MOVZInst RC:$T, (SLT CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
618   def : Pat<(select (setuge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
619             (MOVZInst RC:$T, (SLTu CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
620   def : Pat<(select (setge CPURegs:$lhs, immSExt16:$rhs), RC:$T, RC:$F),
621             (MOVZInst RC:$T, (SLTi CPURegs:$lhs, immSExt16:$rhs), RC:$F)>;
622   def : Pat<(select (setuge CPURegs:$lh, immSExt16:$rh), RC:$T, RC:$F),
623             (MOVZInst RC:$T, (SLTiu CPURegs:$lh, immSExt16:$rh), RC:$F)>;
624   def : Pat<(select (setle CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
625             (MOVZInst RC:$T, (SLT CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
626   def : Pat<(select (setule CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
627             (MOVZInst RC:$T, (SLTu CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
628   def : Pat<(select (seteq CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
629             (MOVZInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
630   def : Pat<(select (seteq CPURegs:$lhs, 0), RC:$T, RC:$F),
631             (MOVZInst RC:$T, CPURegs:$lhs, RC:$F)>;
632 }
633
634 multiclass MovnPats<RegisterClass RC, Instruction MOVNInst> {
635   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
636             (MOVNInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
637   def : Pat<(select CPURegs:$cond, RC:$T, RC:$F),
638             (MOVNInst RC:$T, CPURegs:$cond, RC:$F)>;
639   def : Pat<(select (setne CPURegs:$lhs, 0), RC:$T, RC:$F),
640             (MOVNInst RC:$T, CPURegs:$lhs, RC:$F)>;
641 }
642
643 defm : MovzPats<CPURegs, MOVZ_I>;
644 defm : MovnPats<CPURegs, MOVN_I>;
645
646 // select patterns with got access
647 let AddedComplexity = 10 in
648   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs),
649                     (i32 tglobaladdr:$T), CPURegs:$F),
650             (MOVN_I CPURegs:$F, (ADDiu GP, tglobaladdr:$T),
651                     (XOR CPURegs:$lhs, CPURegs:$rhs))>;
652
653 // setcc patterns
654 def : Pat<(seteq CPURegs:$lhs, CPURegs:$rhs),
655           (SLTu (XOR CPURegs:$lhs, CPURegs:$rhs), 1)>;
656 def : Pat<(setne CPURegs:$lhs, CPURegs:$rhs),
657           (SLTu ZERO, (XOR CPURegs:$lhs, CPURegs:$rhs))>;
658
659 def : Pat<(setle CPURegs:$lhs, CPURegs:$rhs),
660           (XORi (SLT CPURegs:$rhs, CPURegs:$lhs), 1)>;
661 def : Pat<(setule CPURegs:$lhs, CPURegs:$rhs),
662           (XORi (SLTu CPURegs:$rhs, CPURegs:$lhs), 1)>;
663
664 def : Pat<(setgt CPURegs:$lhs, CPURegs:$rhs),
665           (SLT CPURegs:$rhs, CPURegs:$lhs)>;
666 def : Pat<(setugt CPURegs:$lhs, CPURegs:$rhs),
667           (SLTu CPURegs:$rhs, CPURegs:$lhs)>;
668
669 def : Pat<(setge CPURegs:$lhs, CPURegs:$rhs),
670           (XORi (SLT CPURegs:$lhs, CPURegs:$rhs), 1)>;
671 def : Pat<(setuge CPURegs:$lhs, CPURegs:$rhs),
672           (XORi (SLTu CPURegs:$lhs, CPURegs:$rhs), 1)>;
673
674 def : Pat<(setge CPURegs:$lhs, immSExt16:$rhs),
675           (XORi (SLTi CPURegs:$lhs, immSExt16:$rhs), 1)>;
676 def : Pat<(setuge CPURegs:$lhs, immSExt16:$rhs),
677           (XORi (SLTiu CPURegs:$lhs, immSExt16:$rhs), 1)>;
678
679 //===----------------------------------------------------------------------===//
680 // Floating Point Support
681 //===----------------------------------------------------------------------===//
682
683 include "MipsInstrFPU.td"
684