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