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