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