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