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