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