Fix lines that have incorrect indentation or exceed 80 columns. There is no change...
[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),
373                            ".cprestore\t$loc\n", []>;
374
375 //===---------------------------------------------------------------------===//
376 // Instruction definition
377 //===---------------------------------------------------------------------===//
378
379 //===---------------------------------------------------------------------===//
380 // MipsI Instructions
381 //===---------------------------------------------------------------------===//
382
383 /// Arithmetic Instructions (ALU Immediate)
384 def ADDiu   : ArithI<0x09, "addiu", add, simm16, immSExt16>;
385 def ADDi    : ArithOverflowI<0x08, "addi",  add, simm16, immSExt16>;
386 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
387 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16>;
388 def ANDi    : LogicI<0x0c, "andi", and>;
389 def ORi     : LogicI<0x0d, "ori",  or>;
390 def XORi    : LogicI<0x0e, "xori",  xor>;
391 def LUi     : LoadUpper<0x0f, "lui">;
392
393 /// Arithmetic Instructions (3-Operand, R-Type)
394 def ADDu    : ArithR<0x00, 0x21, "addu", add, IIAlu>;
395 def SUBu    : ArithR<0x00, 0x23, "subu", sub, IIAlu>;
396 def ADD     : ArithOverflowR<0x00, 0x20, "add">;
397 def SUB     : ArithOverflowR<0x00, 0x22, "sub">;
398 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt>;
399 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult>;
400 def AND     : LogicR<0x24, "and", and>;
401 def OR      : LogicR<0x25, "or",  or>;
402 def XOR     : LogicR<0x26, "xor", xor>;
403 def NOR     : LogicNOR<0x00, 0x27, "nor">;
404
405 /// Shift Instructions
406 def SLL     : LogicR_shift_rotate_imm<0x00, 0x00, "sll", shl>;
407 def SRL     : LogicR_shift_rotate_imm<0x02, 0x00, "srl", srl>;
408 def SRA     : LogicR_shift_rotate_imm<0x03, 0x00, "sra", sra>;
409 def SLLV    : LogicR_shift_rotate_reg<0x04, 0x00, "sllv", shl>;
410 def SRLV    : LogicR_shift_rotate_reg<0x06, 0x00, "srlv", srl>;
411 def SRAV    : LogicR_shift_rotate_reg<0x07, 0x00, "srav", sra>;
412
413 // Rotate Instructions
414 let Predicates = [IsMips32r2] in {
415     def ROTR    : LogicR_shift_rotate_imm<0x02, 0x01, "rotr", rotr>;
416     def ROTRV   : LogicR_shift_rotate_reg<0x06, 0x01, "rotrv", rotr>;
417 }
418
419 /// Load and Store Instructions
420 def LB      : LoadM<0x20, "lb",  sextloadi8>;
421 def LBu     : LoadM<0x24, "lbu", zextloadi8>;
422 def LH      : LoadM<0x21, "lh",  sextloadi16>;
423 def LHu     : LoadM<0x25, "lhu", zextloadi16>;
424 def LW      : LoadM<0x23, "lw",  load>;
425 def SB      : StoreM<0x28, "sb", truncstorei8>;
426 def SH      : StoreM<0x29, "sh", truncstorei16>;
427 def SW      : StoreM<0x2b, "sw", store>;
428
429 /// Jump and Branch Instructions
430 def J       : JumpFJ<0x02, "j">;
431 def JR      : JumpFR<0x00, 0x08, "jr">;
432 def JAL     : JumpLink<0x03, "jal">;
433 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
434 def BEQ     : CBranch<0x04, "beq", seteq>;
435 def BNE     : CBranch<0x05, "bne", setne>;
436
437 let rt=1 in
438   def BGEZ  : CBranchZero<0x01, "bgez", setge>;
439
440 let rt=0 in {
441   def BGTZ  : CBranchZero<0x07, "bgtz", setgt>;
442   def BLEZ  : CBranchZero<0x07, "blez", setle>;
443   def BLTZ  : CBranchZero<0x01, "bltz", setlt>;
444 }
445
446 def BGEZAL  : BranchLink<"bgezal">;
447 def BLTZAL  : BranchLink<"bltzal">;
448
449 let isReturn=1, isTerminator=1, hasDelaySlot=1,
450     isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
451   def RET : FR <0x00, 0x02, (outs), (ins CPURegs:$target),
452                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
453
454 /// Multiply and Divide Instructions.
455 def MULT    : Mul<0x18, "mult", IIImul>;
456 def MULTu   : Mul<0x19, "multu", IIImul>;
457 def SDIV    : Div<MipsDivRem, 0x1a, "div", IIIdiv>;
458 def UDIV    : Div<MipsDivRemU, 0x1b, "divu", IIIdiv>;
459
460 let Defs = [HI] in
461   def MTHI  : MoveToLOHI<0x11, "mthi">;
462 let Defs = [LO] in
463   def MTLO  : MoveToLOHI<0x13, "mtlo">;
464
465 let Uses = [HI] in
466   def MFHI  : MoveFromLOHI<0x10, "mfhi">;
467 let Uses = [LO] in
468   def MFLO  : MoveFromLOHI<0x12, "mflo">;
469
470 /// Sign Ext In Register Instructions.
471 let Predicates = [HasSEInReg] in {
472   let shamt = 0x10, rs = 0 in
473     def SEB : SignExtInReg<0x21, "seb", i8>;
474
475   let shamt = 0x18, rs = 0 in
476     def SEH : SignExtInReg<0x20, "seh", i16>;
477 }
478
479 /// Count Leading
480 def CLZ : CountLeading<0b100000, "clz",
481                        [(set CPURegs:$dst, (ctlz CPURegs:$src))]>;
482 def CLO : CountLeading<0b100001, "clo",
483                        [(set CPURegs:$dst, (ctlz (not CPURegs:$src)))]>;
484
485 /// Byte Swap
486 let Predicates = [HasSwap] in {
487   let shamt = 0x3, rs = 0 in
488     def WSBW : ByteSwap<0x20, "wsbw">;
489 }
490
491 /// Conditional Move
492 def MIPS_CMOV_ZERO  : PatLeaf<(i32 0)>;
493 def MIPS_CMOV_NZERO : PatLeaf<(i32 1)>;
494
495 // Conditional moves:
496 // These instructions are expanded in
497 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
498 // conditional move instructions.
499 // flag:int, data:int
500 let usesCustomInserter = 1, shamt = 0, Constraints = "$F = $dst" in
501   class CondMovIntInt<bits<6> funct, string instr_asm> :
502     FR<0, funct, (outs CPURegs:$dst),
503        (ins CPURegs:$T, CPURegs:$cond, CPURegs:$F),
504        !strconcat(instr_asm, "\t$dst, $T, $cond"), [], NoItinerary>;
505
506 def MOVZ_I : CondMovIntInt<0x0a, "movz">;
507 def MOVN_I : CondMovIntInt<0x0b, "movn">;
508
509 /// No operation
510 let addr=0 in
511   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
512
513 // FrameIndexes are legalized when they are operands from load/store
514 // instructions. The same not happens for stack address copies, so an
515 // add op with mem ComplexPattern is used and the stack address copy
516 // can be matched. It's similar to Sparc LEA_ADDRi
517 def LEA_ADDiu : EffectiveAddress<"addiu\t$dst, ${addr:stackloc}">;
518
519 // MADD*/MSUB*
520 def MADD  : MArithR<0, "madd", MipsMAdd>;
521 def MADDU : MArithR<1, "maddu", MipsMAddu>;
522 def MSUB  : MArithR<4, "msub", MipsMSub>;
523 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
524
525 // MUL is a assembly macro in the current used ISAs. In recent ISA's
526 // it is a real instruction.
527 def MUL   : ArithR<0x1c, 0x02, "mul", mul, IIImul>, Requires<[IsMips32]>;
528
529 //===---------------------------------------------------------------------===//
530 //  Arbitrary patterns that map to one or more instructions
531 //===---------------------------------------------------------------------===//
532
533 // Small immediates
534 def : Pat<(i32 immSExt16:$in),
535           (ADDiu ZERO, imm:$in)>;
536 def : Pat<(i32 immZExt16:$in),
537           (ORi ZERO, imm:$in)>;
538
539 // Arbitrary immediates
540 def : Pat<(i32 imm:$imm),
541           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
542
543 // Carry patterns
544 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
545           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
546 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
547           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
548 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
549           (ADDiu CPURegs:$src, imm:$imm)>;
550
551 // Call
552 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
553           (JAL tglobaladdr:$dst)>;
554 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
555           (JAL texternalsym:$dst)>;
556 //def : Pat<(MipsJmpLink CPURegs:$dst),
557 //          (JALR CPURegs:$dst)>;
558
559 // hi/lo relocs
560 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
561 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
562           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
563 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
564           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
565
566 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
567 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
568           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
569
570 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
571 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
572           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
573
574 // gp_rel relocs
575 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
576           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
577 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
578           (ADDiu CPURegs:$gp, tconstpool:$in)>;
579
580 // Mips does not have "not", so we expand our way
581 def : Pat<(not CPURegs:$in),
582           (NOR CPURegs:$in, ZERO)>;
583
584 // extended load and stores
585 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
586 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
587 def : Pat<(extloadi16 addr:$src), (LHu addr:$src)>;
588
589 // peepholes
590 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
591
592 // brcond patterns
593 def : Pat<(brcond (setne CPURegs:$lhs, 0), bb:$dst),
594           (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
595 def : Pat<(brcond (seteq CPURegs:$lhs, 0), bb:$dst),
596           (BEQ CPURegs:$lhs, ZERO, bb:$dst)>;
597
598 def : Pat<(brcond (setge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
599           (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
600 def : Pat<(brcond (setuge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
601           (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
602 def : Pat<(brcond (setge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
603           (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
604 def : Pat<(brcond (setuge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
605           (BEQ (SLTiu CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
606
607 def : Pat<(brcond (setle CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
608           (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
609 def : Pat<(brcond (setule CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
610           (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
611
612 def : Pat<(brcond CPURegs:$cond, bb:$dst),
613           (BNE CPURegs:$cond, ZERO, bb:$dst)>;
614
615 // select patterns
616 multiclass MovzPats<RegisterClass RC, Instruction MOVZInst> {
617   def : Pat<(select (setge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
618             (MOVZInst RC:$T, (SLT CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
619   def : Pat<(select (setuge CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
620             (MOVZInst RC:$T, (SLTu CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
621   def : Pat<(select (setge CPURegs:$lhs, immSExt16:$rhs), RC:$T, RC:$F),
622             (MOVZInst RC:$T, (SLTi CPURegs:$lhs, immSExt16:$rhs), RC:$F)>;
623   def : Pat<(select (setuge CPURegs:$lh, immSExt16:$rh), RC:$T, RC:$F),
624             (MOVZInst RC:$T, (SLTiu CPURegs:$lh, immSExt16:$rh), RC:$F)>;
625   def : Pat<(select (setle CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
626             (MOVZInst RC:$T, (SLT CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
627   def : Pat<(select (setule CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
628             (MOVZInst RC:$T, (SLTu CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
629   def : Pat<(select (seteq CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
630             (MOVZInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
631   def : Pat<(select (seteq CPURegs:$lhs, 0), RC:$T, RC:$F),
632             (MOVZInst RC:$T, CPURegs:$lhs, RC:$F)>;
633 }
634
635 multiclass MovnPats<RegisterClass RC, Instruction MOVNInst> {
636   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs), RC:$T, RC:$F),
637             (MOVNInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
638   def : Pat<(select CPURegs:$cond, RC:$T, RC:$F),
639             (MOVNInst RC:$T, CPURegs:$cond, RC:$F)>;
640   def : Pat<(select (setne CPURegs:$lhs, 0), RC:$T, RC:$F),
641             (MOVNInst RC:$T, CPURegs:$lhs, RC:$F)>;
642 }
643
644 defm : MovzPats<CPURegs, MOVZ_I>;
645 defm : MovnPats<CPURegs, MOVN_I>;
646
647 // select patterns with got access
648 let AddedComplexity = 10 in
649   def : Pat<(select (setne CPURegs:$lhs, CPURegs:$rhs),
650                     (i32 tglobaladdr:$T), CPURegs:$F),
651             (MOVN_I CPURegs:$F, (ADDiu GP, tglobaladdr:$T),
652                     (XOR CPURegs:$lhs, CPURegs:$rhs))>;
653
654 // setcc patterns
655 def : Pat<(seteq CPURegs:$lhs, CPURegs:$rhs),
656           (SLTu (XOR CPURegs:$lhs, CPURegs:$rhs), 1)>;
657 def : Pat<(setne CPURegs:$lhs, CPURegs:$rhs),
658           (SLTu ZERO, (XOR CPURegs:$lhs, CPURegs:$rhs))>;
659
660 def : Pat<(setle CPURegs:$lhs, CPURegs:$rhs),
661           (XORi (SLT CPURegs:$rhs, CPURegs:$lhs), 1)>;
662 def : Pat<(setule CPURegs:$lhs, CPURegs:$rhs),
663           (XORi (SLTu CPURegs:$rhs, CPURegs:$lhs), 1)>;
664
665 def : Pat<(setgt CPURegs:$lhs, CPURegs:$rhs),
666           (SLT CPURegs:$rhs, CPURegs:$lhs)>;
667 def : Pat<(setugt CPURegs:$lhs, CPURegs:$rhs),
668           (SLTu CPURegs:$rhs, CPURegs:$lhs)>;
669
670 def : Pat<(setge CPURegs:$lhs, CPURegs:$rhs),
671           (XORi (SLT CPURegs:$lhs, CPURegs:$rhs), 1)>;
672 def : Pat<(setuge CPURegs:$lhs, CPURegs:$rhs),
673           (XORi (SLTu CPURegs:$lhs, CPURegs:$rhs), 1)>;
674
675 def : Pat<(setge CPURegs:$lhs, immSExt16:$rhs),
676           (XORi (SLTi CPURegs:$lhs, immSExt16:$rhs), 1)>;
677 def : Pat<(setuge CPURegs:$lhs, immSExt16:$rhs),
678           (XORi (SLTiu CPURegs:$lhs, immSExt16:$rhs), 1)>;
679
680 //===---------------------------------------------------------------------===//
681 // Floating Point Support
682 //===---------------------------------------------------------------------===//
683
684 include "MipsInstrFPU.td"