Make changes necessary for supporting floating point load and store instructions
[oota-llvm.git] / lib / Target / Mips / MipsInstrInfo.td
1 //===- MipsInstrInfo.td - Target Description for Mips Target -*- 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 // This file contains the Mips implementation of the TargetInstrInfo class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 //===----------------------------------------------------------------------===//
15 // Instruction format superclass
16 //===----------------------------------------------------------------------===//
17
18 include "MipsInstrFormats.td"
19
20 //===----------------------------------------------------------------------===//
21 // Mips profiles and nodes
22 //===----------------------------------------------------------------------===//
23
24 def SDT_MipsRet          : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
25 def SDT_MipsJmpLink      : SDTypeProfile<0, 1, [SDTCisVT<0, iPTR>]>;
26 def SDT_MipsCMov         : SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
27                                                 SDTCisSameAs<1, 2>,
28                                                 SDTCisSameAs<3, 4>,
29                                                 SDTCisInt<4>]>;
30 def SDT_MipsCallSeqStart : SDCallSeqStart<[SDTCisVT<0, i32>]>;
31 def SDT_MipsCallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
32 def SDT_MipsMAddMSub     : SDTypeProfile<0, 4,
33                                          [SDTCisVT<0, i32>, SDTCisSameAs<0, 1>,
34                                           SDTCisSameAs<1, 2>,
35                                           SDTCisSameAs<2, 3>]>;
36 def SDT_MipsDivRem       : SDTypeProfile<0, 2,
37                                          [SDTCisInt<0>,
38                                           SDTCisSameAs<0, 1>]>;
39
40 def SDT_MipsThreadPointer : SDTypeProfile<1, 0, [SDTCisPtrTy<0>]>;
41
42 def SDT_MipsDynAlloc    : SDTypeProfile<1, 1, [SDTCisVT<0, i32>,
43                                                SDTCisVT<1, iPTR>]>;
44 def SDT_Sync             : SDTypeProfile<0, 1, [SDTCisVT<0, i32>]>;
45
46 def SDT_Ext : SDTypeProfile<1, 3, [SDTCisInt<0>, SDTCisSameAs<0, 1>,
47                                    SDTCisVT<2, i32>, SDTCisSameAs<2, 3>]>;
48 def SDT_Ins : SDTypeProfile<1, 4, [SDTCisInt<0>, SDTCisSameAs<0, 1>,
49                                    SDTCisVT<2, i32>, SDTCisSameAs<2, 3>,
50                                    SDTCisSameAs<0, 4>]>;
51
52 // Call
53 def MipsJmpLink : SDNode<"MipsISD::JmpLink",SDT_MipsJmpLink,
54                          [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,
55                           SDNPVariadic]>;
56
57 // Hi and Lo nodes are used to handle global addresses. Used on
58 // MipsISelLowering to lower stuff like GlobalAddress, ExternalSymbol
59 // static model. (nothing to do with Mips Registers Hi and Lo)
60 def MipsHi    : SDNode<"MipsISD::Hi", SDTIntUnaryOp>;
61 def MipsLo    : SDNode<"MipsISD::Lo", SDTIntUnaryOp>;
62 def MipsGPRel : SDNode<"MipsISD::GPRel", SDTIntUnaryOp>;
63
64 // TlsGd node is used to handle General Dynamic TLS
65 def MipsTlsGd : SDNode<"MipsISD::TlsGd", SDTIntUnaryOp>;
66
67 // TprelHi and TprelLo nodes are used to handle Local Exec TLS
68 def MipsTprelHi    : SDNode<"MipsISD::TprelHi", SDTIntUnaryOp>;
69 def MipsTprelLo    : SDNode<"MipsISD::TprelLo", SDTIntUnaryOp>;
70
71 // Thread pointer
72 def MipsThreadPointer: SDNode<"MipsISD::ThreadPointer", SDT_MipsThreadPointer>;
73
74 // Return
75 def MipsRet : SDNode<"MipsISD::Ret", SDT_MipsRet, [SDNPHasChain,
76                      SDNPOptInGlue]>;
77
78 // These are target-independent nodes, but have target-specific formats.
79 def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_MipsCallSeqStart,
80                            [SDNPHasChain, SDNPOutGlue]>;
81 def callseq_end   : SDNode<"ISD::CALLSEQ_END", SDT_MipsCallSeqEnd,
82                            [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
83
84 // MAdd*/MSub* nodes
85 def MipsMAdd      : SDNode<"MipsISD::MAdd", SDT_MipsMAddMSub,
86                            [SDNPOptInGlue, SDNPOutGlue]>;
87 def MipsMAddu     : SDNode<"MipsISD::MAddu", SDT_MipsMAddMSub,
88                            [SDNPOptInGlue, SDNPOutGlue]>;
89 def MipsMSub      : SDNode<"MipsISD::MSub", SDT_MipsMAddMSub,
90                            [SDNPOptInGlue, SDNPOutGlue]>;
91 def MipsMSubu     : SDNode<"MipsISD::MSubu", SDT_MipsMAddMSub,
92                            [SDNPOptInGlue, SDNPOutGlue]>;
93
94 // DivRem(u) nodes
95 def MipsDivRem    : SDNode<"MipsISD::DivRem", SDT_MipsDivRem,
96                            [SDNPOutGlue]>;
97 def MipsDivRemU   : SDNode<"MipsISD::DivRemU", SDT_MipsDivRem,
98                            [SDNPOutGlue]>;
99
100 // Target constant nodes that are not part of any isel patterns and remain
101 // unchanged can cause instructions with illegal operands to be emitted.
102 // Wrapper node patterns give the instruction selector a chance to replace
103 // target constant nodes that would otherwise remain unchanged with ADDiu
104 // nodes. Without these wrapper node patterns, the following conditional move
105 // instrucion is emitted when function cmov2 in test/CodeGen/Mips/cmov.ll is
106 // compiled: 
107 //  movn  %got(d)($gp), %got(c)($gp), $4
108 // This instruction is illegal since movn can take only register operands.
109
110 def MipsWrapperPIC    : SDNode<"MipsISD::WrapperPIC",  SDTIntUnaryOp>;
111
112 // Pointer to dynamically allocated stack area.
113 def MipsDynAlloc  : SDNode<"MipsISD::DynAlloc", SDT_MipsDynAlloc,
114                            [SDNPHasChain, SDNPInGlue]>;
115
116 def MipsSync : SDNode<"MipsISD::Sync", SDT_Sync, [SDNPHasChain]>;
117
118 def MipsExt :  SDNode<"MipsISD::Ext", SDT_Ext>;
119 def MipsIns :  SDNode<"MipsISD::Ins", SDT_Ins>;
120
121 //===----------------------------------------------------------------------===//
122 // Mips Instruction Predicate Definitions.
123 //===----------------------------------------------------------------------===//
124 def HasSEInReg  : Predicate<"Subtarget.hasSEInReg()">;
125 def HasBitCount : Predicate<"Subtarget.hasBitCount()">;
126 def HasSwap     : Predicate<"Subtarget.hasSwap()">;
127 def HasCondMov  : Predicate<"Subtarget.hasCondMov()">;
128 def HasMips32    : Predicate<"Subtarget.hasMips32()">;
129 def HasMips32r2  : Predicate<"Subtarget.hasMips32r2()">;
130 def HasMips64    : Predicate<"Subtarget.hasMips64()">;
131 def NotMips64    : Predicate<"!Subtarget.hasMips64()">;
132 def HasMips64r2  : Predicate<"Subtarget.hasMips64r2()">;
133 def IsN64       : Predicate<"Subtarget.isABI_N64()">;
134 def NotN64      : Predicate<"!Subtarget.isABI_N64()">;
135
136 //===----------------------------------------------------------------------===//
137 // Mips Operand, Complex Patterns and Transformations Definitions.
138 //===----------------------------------------------------------------------===//
139
140 // Instruction operand types
141 def brtarget    : Operand<OtherVT>;
142 def calltarget  : Operand<i32>;
143 def simm16      : Operand<i32>;
144 def simm16_64   : Operand<i64>;
145 def shamt       : Operand<i32>;
146
147 // Unsigned Operand
148 def uimm16      : Operand<i32> {
149   let PrintMethod = "printUnsignedImm";
150 }
151
152 // Address operand
153 def mem : Operand<i32> {
154   let PrintMethod = "printMemOperand";
155   let MIOperandInfo = (ops CPURegs, simm16);
156 }
157
158 def mem64 : Operand<i64> {
159   let PrintMethod = "printMemOperand";
160   let MIOperandInfo = (ops CPU64Regs, simm16_64);
161 }
162
163 def mem_ea : Operand<i32> {
164   let PrintMethod = "printMemOperandEA";
165   let MIOperandInfo = (ops CPURegs, simm16);
166 }
167
168 // Transformation Function - get the lower 16 bits.
169 def LO16 : SDNodeXForm<imm, [{
170   return getI32Imm((unsigned)N->getZExtValue() & 0xFFFF);
171 }]>;
172
173 // Transformation Function - get the higher 16 bits.
174 def HI16 : SDNodeXForm<imm, [{
175   return getI32Imm((unsigned)N->getZExtValue() >> 16);
176 }]>;
177
178 // Node immediate fits as 16-bit sign extended on target immediate.
179 // e.g. addi, andi
180 def immSExt16  : PatLeaf<(imm), [{ return isInt<16>(N->getSExtValue()); }]>;
181
182 // Node immediate fits as 16-bit zero extended on target immediate.
183 // The LO16 param means that only the lower 16 bits of the node
184 // immediate are caught.
185 // e.g. addiu, sltiu
186 def immZExt16  : PatLeaf<(imm), [{
187   if (N->getValueType(0) == MVT::i32)
188     return (uint32_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
189   else
190     return (uint64_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
191 }], LO16>;
192
193 // shamt field must fit in 5 bits.
194 def immZExt5 : PatLeaf<(imm), [{
195   return N->getZExtValue() == ((N->getZExtValue()) & 0x1f) ;
196 }]>;
197
198 // Mips Address Mode! SDNode frameindex could possibily be a match
199 // since load and store instructions from stack used it.
200 def addr : ComplexPattern<iPTR, 2, "SelectAddr", [frameindex], []>;
201
202 //===----------------------------------------------------------------------===//
203 // Pattern fragment for load/store
204 //===----------------------------------------------------------------------===//
205 class UnalignedLoad<PatFrag Node> : PatFrag<(ops node:$ptr), (Node node:$ptr), [{
206   LoadSDNode *LD = cast<LoadSDNode>(N);
207   return LD->getMemoryVT().getSizeInBits()/8 > LD->getAlignment();
208 }]>;
209
210 class AlignedLoad<PatFrag Node> : PatFrag<(ops node:$ptr), (Node node:$ptr), [{
211   LoadSDNode *LD = cast<LoadSDNode>(N);
212   return LD->getMemoryVT().getSizeInBits()/8 <= LD->getAlignment();
213 }]>;
214
215 class UnalignedStore<PatFrag Node> : PatFrag<(ops node:$val, node:$ptr),
216                                              (Node node:$val, node:$ptr), [{
217   StoreSDNode *SD = cast<StoreSDNode>(N);
218   return SD->getMemoryVT().getSizeInBits()/8 > SD->getAlignment();
219 }]>;
220
221 class AlignedStore<PatFrag Node> : PatFrag<(ops node:$val, node:$ptr),
222                                            (Node node:$val, node:$ptr), [{
223   StoreSDNode *SD = cast<StoreSDNode>(N);
224   return SD->getMemoryVT().getSizeInBits()/8 <= SD->getAlignment();
225 }]>;
226
227 // Load/Store PatFrags.
228 def sextloadi16_a   : AlignedLoad<sextloadi16>;
229 def zextloadi16_a   : AlignedLoad<zextloadi16>;
230 def extloadi16_a    : AlignedLoad<extloadi16>;
231 def load_a          : AlignedLoad<load>;
232 def sextloadi32_a   : AlignedLoad<sextloadi32>;
233 def zextloadi32_a   : AlignedLoad<zextloadi32>;
234 def extloadi32_a    : AlignedLoad<extloadi32>;
235 def truncstorei16_a : AlignedStore<truncstorei16>;
236 def store_a         : AlignedStore<store>;
237 def truncstorei32_a : AlignedStore<truncstorei32>;
238 def sextloadi16_u   : UnalignedLoad<sextloadi16>;
239 def zextloadi16_u   : UnalignedLoad<zextloadi16>;
240 def extloadi16_u    : UnalignedLoad<extloadi16>;
241 def load_u          : UnalignedLoad<load>;
242 def sextloadi32_u   : UnalignedLoad<sextloadi32>;
243 def zextloadi32_u   : UnalignedLoad<zextloadi32>;
244 def extloadi32_u    : UnalignedLoad<extloadi32>;
245 def truncstorei16_u : UnalignedStore<truncstorei16>;
246 def store_u         : UnalignedStore<store>;
247 def truncstorei32_u : UnalignedStore<truncstorei32>;
248
249 //===----------------------------------------------------------------------===//
250 // Instructions specific format
251 //===----------------------------------------------------------------------===//
252
253 // Arithmetic 3 register operands
254 class ArithR<bits<6> op, bits<6> func, string instr_asm, SDNode OpNode,
255              InstrItinClass itin, bit isComm = 0>:
256   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
257      !strconcat(instr_asm, "\t$dst, $b, $c"),
258      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], itin> {
259   let isCommutable = isComm;
260 }
261
262 class ArithOverflowR<bits<6> op, bits<6> func, string instr_asm,
263                      bit isComm = 0>:
264   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
265      !strconcat(instr_asm, "\t$dst, $b, $c"), [], IIAlu> {
266   let isCommutable = isComm;
267 }
268
269 // Arithmetic 2 register operands
270 class ArithI<bits<6> op, string instr_asm, SDNode OpNode,
271              Operand Od, PatLeaf imm_type> :
272   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
273      !strconcat(instr_asm, "\t$dst, $b, $c"),
274      [(set CPURegs:$dst, (OpNode CPURegs:$b, imm_type:$c))], IIAlu>;
275
276 class ArithOverflowI<bits<6> op, string instr_asm, SDNode OpNode,
277              Operand Od, PatLeaf imm_type> :
278   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
279      !strconcat(instr_asm, "\t$dst, $b, $c"), [], IIAlu>;
280
281 // Arithmetic Multiply ADD/SUB
282 let rd = 0, shamt = 0, Defs = [HI, LO], Uses = [HI, LO] in
283 class MArithR<bits<6> func, string instr_asm, SDNode op, bit isComm = 0> :
284   FR<0x1c, func, (outs), (ins CPURegs:$rs, CPURegs:$rt),
285      !strconcat(instr_asm, "\t$rs, $rt"),
286      [(op CPURegs:$rs, CPURegs:$rt, LO, HI)], IIImul> {
287   let isCommutable = isComm;
288 }
289
290 //  Logical
291 let isCommutable = 1 in
292 class LogicR<bits<6> func, string instr_asm, SDNode OpNode>:
293   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
294      !strconcat(instr_asm, "\t$dst, $b, $c"),
295      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], IIAlu>;
296
297 class LogicI<bits<6> op, string instr_asm, SDNode OpNode>:
298   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, uimm16:$c),
299      !strconcat(instr_asm, "\t$dst, $b, $c"),
300      [(set CPURegs:$dst, (OpNode CPURegs:$b, immZExt16:$c))], IIAlu>;
301
302 let isCommutable = 1 in
303 class LogicNOR<bits<6> op, bits<6> func, string instr_asm>:
304   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
305      !strconcat(instr_asm, "\t$dst, $b, $c"),
306      [(set CPURegs:$dst, (not (or CPURegs:$b, CPURegs:$c)))], IIAlu>;
307
308 // Shifts
309 class LogicR_shift_rotate_imm<bits<6> func, bits<5> _rs, string instr_asm,
310                               SDNode OpNode>:
311   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$b, shamt:$c),
312      !strconcat(instr_asm, "\t$dst, $b, $c"),
313      [(set CPURegs:$dst, (OpNode CPURegs:$b, (i32 immZExt5:$c)))], IIAlu> {
314   let rs = _rs;
315 }
316
317 class LogicR_shift_rotate_reg<bits<6> func, bits<5> _shamt, string instr_asm,
318                               SDNode OpNode>:
319   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$c, CPURegs:$b),
320      !strconcat(instr_asm, "\t$dst, $b, $c"),
321      [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))], IIAlu> {
322   let shamt = _shamt;
323 }
324
325 // Load Upper Imediate
326 class LoadUpper<bits<6> op, string instr_asm>:
327   FI< op,
328       (outs CPURegs:$dst),
329       (ins uimm16:$imm),
330       !strconcat(instr_asm, "\t$dst, $imm"),
331       [], IIAlu>;
332
333 // Memory Load/Store
334 let canFoldAsLoad = 1 in
335 class LoadM<bits<6> op, string instr_asm, PatFrag OpNode, RegisterClass RC,
336             Operand MemOpnd, bit Pseudo>:
337   FI<op, (outs RC:$dst), (ins MemOpnd:$addr),
338      !strconcat(instr_asm, "\t$dst, $addr"),
339      [(set RC:$dst, (OpNode addr:$addr))], IILoad> {
340   let isPseudo = Pseudo;
341 }
342
343 class StoreM<bits<6> op, string instr_asm, PatFrag OpNode, RegisterClass RC,
344              Operand MemOpnd, bit Pseudo>:
345   FI<op, (outs), (ins RC:$dst, MemOpnd:$addr),
346      !strconcat(instr_asm, "\t$dst, $addr"),
347      [(OpNode RC:$dst, addr:$addr)], IIStore> {
348   let isPseudo = Pseudo;
349 }
350
351 // 32-bit load.
352 multiclass LoadM32<bits<6> op, string instr_asm, PatFrag OpNode,
353                    bit Pseudo = 0> {
354   def #NAME# : LoadM<op, instr_asm, OpNode, CPURegs, mem, Pseudo>,
355                Requires<[NotN64]>;
356   def _P8    : LoadM<op, instr_asm, OpNode, CPURegs, mem64, Pseudo>,
357                Requires<[IsN64]>;
358
359
360 // 64-bit load.
361 multiclass LoadM64<bits<6> op, string instr_asm, PatFrag OpNode,
362                    bit Pseudo = 0> {
363   def #NAME# : LoadM<op, instr_asm, OpNode, CPU64Regs, mem, Pseudo>,
364                Requires<[NotN64]>;
365   def _P8    : LoadM<op, instr_asm, OpNode, CPU64Regs, mem64, Pseudo>,
366                Requires<[IsN64]>;
367
368
369 // 32-bit store.
370 multiclass StoreM32<bits<6> op, string instr_asm, PatFrag OpNode,
371                     bit Pseudo = 0> {
372   def #NAME# : StoreM<op, instr_asm, OpNode, CPURegs, mem, Pseudo>,
373                Requires<[NotN64]>;
374   def _P8    : StoreM<op, instr_asm, OpNode, CPURegs, mem64, Pseudo>,
375                Requires<[IsN64]>;
376 }
377
378 // 64-bit store.
379 multiclass StoreM64<bits<6> op, string instr_asm, PatFrag OpNode,
380                     bit Pseudo = 0> {
381   def #NAME# : StoreM<op, instr_asm, OpNode, CPU64Regs, mem, Pseudo>,
382                Requires<[NotN64]>;
383   def _P8    : StoreM<op, instr_asm, OpNode, CPU64Regs, mem64, Pseudo>,
384                Requires<[IsN64]>;
385 }
386
387 // Conditional Branch
388 let isBranch = 1, isTerminator=1, hasDelaySlot = 1 in {
389 class CBranch<bits<6> op, string instr_asm, PatFrag cond_op>:
390   FI<op, (outs), (ins CPURegs:$a, CPURegs:$b, brtarget:$offset),
391      !strconcat(instr_asm, "\t$a, $b, $offset"),
392      [(brcond (i32 (cond_op CPURegs:$a, CPURegs:$b)), bb:$offset)],
393      IIBranch>;
394
395 class CBranchZero<bits<6> op, string instr_asm, PatFrag cond_op>:
396   FI<op, (outs), (ins CPURegs:$src, brtarget:$offset),
397      !strconcat(instr_asm, "\t$src, $offset"),
398      [(brcond (i32 (cond_op CPURegs:$src, 0)), bb:$offset)],
399      IIBranch>;
400 }
401
402 // SetCC
403 class SetCC_R<bits<6> op, bits<6> func, string instr_asm,
404       PatFrag cond_op>:
405   FR<op, func, (outs CPURegs:$dst), (ins CPURegs:$b, CPURegs:$c),
406      !strconcat(instr_asm, "\t$dst, $b, $c"),
407      [(set CPURegs:$dst, (cond_op CPURegs:$b, CPURegs:$c))],
408      IIAlu>;
409
410 class SetCC_I<bits<6> op, string instr_asm, PatFrag cond_op,
411       Operand Od, PatLeaf imm_type>:
412   FI<op, (outs CPURegs:$dst), (ins CPURegs:$b, Od:$c),
413      !strconcat(instr_asm, "\t$dst, $b, $c"),
414      [(set CPURegs:$dst, (cond_op CPURegs:$b, imm_type:$c))],
415      IIAlu>;
416
417 // Unconditional branch
418 let isBranch=1, isTerminator=1, isBarrier=1, hasDelaySlot = 1 in
419 class JumpFJ<bits<6> op, string instr_asm>:
420   FJ<op, (outs), (ins brtarget:$target),
421      !strconcat(instr_asm, "\t$target"), [(br bb:$target)], IIBranch>;
422
423 let isBranch=1, isTerminator=1, isBarrier=1, rd=0, hasDelaySlot = 1 in
424 class JumpFR<bits<6> op, bits<6> func, string instr_asm>:
425   FR<op, func, (outs), (ins CPURegs:$target),
426      !strconcat(instr_asm, "\t$target"), [(brind CPURegs:$target)], IIBranch>;
427
428 // Jump and Link (Call)
429 let isCall=1, hasDelaySlot=1,
430   // All calls clobber the non-callee saved registers...
431   Defs = [AT, V0, V1, A0, A1, A2, A3, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9,
432           K0, K1, D0, D1, D2, D3, D4, D5, D6, D7, D8, D9], Uses = [GP] in {
433   class JumpLink<bits<6> op, string instr_asm>:
434     FJ<op, (outs), (ins calltarget:$target, variable_ops),
435        !strconcat(instr_asm, "\t$target"), [(MipsJmpLink imm:$target)],
436        IIBranch>;
437
438   let rd=31 in
439   class JumpLinkReg<bits<6> op, bits<6> func, string instr_asm>:
440     FR<op, func, (outs), (ins CPURegs:$rs, variable_ops),
441        !strconcat(instr_asm, "\t$rs"), [(MipsJmpLink CPURegs:$rs)], IIBranch>;
442
443   class BranchLink<string instr_asm>:
444     FI<0x1, (outs), (ins CPURegs:$rs, brtarget:$target, variable_ops),
445        !strconcat(instr_asm, "\t$rs, $target"), [], IIBranch>;
446 }
447
448 // Mul, Div
449 let Defs = [HI, LO] in {
450   let isCommutable = 1 in
451   class Mul<bits<6> func, string instr_asm, InstrItinClass itin>:
452     FR<0x00, func, (outs), (ins CPURegs:$a, CPURegs:$b),
453        !strconcat(instr_asm, "\t$a, $b"), [], itin>;
454
455   class Div<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin>:
456             FR<0x00, func, (outs), (ins CPURegs:$a, CPURegs:$b),
457             !strconcat(instr_asm, "\t$$zero, $a, $b"),
458             [(op CPURegs:$a, CPURegs:$b)], itin>;
459 }
460
461 // Move from Hi/Lo
462 let shamt = 0 in {
463 let rs = 0, rt = 0 in
464 class MoveFromLOHI<bits<6> func, string instr_asm>:
465   FR<0x00, func, (outs CPURegs:$dst), (ins),
466      !strconcat(instr_asm, "\t$dst"), [], IIHiLo>;
467
468 let rt = 0, rd = 0 in
469 class MoveToLOHI<bits<6> func, string instr_asm>:
470   FR<0x00, func, (outs), (ins CPURegs:$src),
471      !strconcat(instr_asm, "\t$src"), [], IIHiLo>;
472 }
473
474 class EffectiveAddress<string instr_asm> :
475   FI<0x09, (outs CPURegs:$dst), (ins mem_ea:$addr),
476      instr_asm, [(set CPURegs:$dst, addr:$addr)], IIAlu>;
477
478 // Count Leading Ones/Zeros in Word
479 class CountLeading<bits<6> func, string instr_asm, list<dag> pattern>:
480   FR<0x1c, func, (outs CPURegs:$dst), (ins CPURegs:$src),
481      !strconcat(instr_asm, "\t$dst, $src"), pattern, IIAlu>,
482      Requires<[HasBitCount]> {
483   let shamt = 0;
484   let rt = rd;
485 }
486
487 // Sign Extend in Register.
488 class SignExtInReg<bits<6> func, string instr_asm, ValueType vt>:
489   FR<0x3f, func, (outs CPURegs:$dst), (ins CPURegs:$src),
490      !strconcat(instr_asm, "\t$dst, $src"),
491      [(set CPURegs:$dst, (sext_inreg CPURegs:$src, vt))], NoItinerary>;
492
493 // Byte Swap
494 class ByteSwap<bits<6> func, string instr_asm>:
495   FR<0x1f, func, (outs CPURegs:$dst), (ins CPURegs:$src),
496      !strconcat(instr_asm, "\t$dst, $src"),
497      [(set CPURegs:$dst, (bswap CPURegs:$src))], NoItinerary>;
498
499 // Conditional Move
500 class CondMov<bits<6> func, string instr_asm, PatLeaf MovCode>:
501   FR<0x00, func, (outs CPURegs:$dst), (ins CPURegs:$F, CPURegs:$T,
502      CPURegs:$cond), !strconcat(instr_asm, "\t$dst, $T, $cond"),
503      [], NoItinerary>;
504
505 // Read Hardware
506 class ReadHardware: FR<0x1f, 0x3b, (outs CPURegs:$dst), (ins HWRegs:$src),
507     "rdhwr\t$dst, $src", [], IIAlu> {
508   let rs = 0;
509   let shamt = 0;
510 }
511
512 // Ext and Ins
513 class ExtIns<bits<6> _funct, string instr_asm, dag outs, dag ins,
514              list<dag> pattern, InstrItinClass itin>:
515   FR<0x1f, _funct, outs, ins, !strconcat(instr_asm, " $rt, $rs, $pos, $sz"),
516      pattern, itin>, Requires<[HasMips32r2]> {
517   bits<5> pos;
518   bits<5> sz;
519   let rd = sz;
520   let shamt = pos;
521 }
522
523 // Atomic instructions with 2 source operands (ATOMIC_SWAP & ATOMIC_LOAD_*).
524 class Atomic2Ops<PatFrag Op, string Opstr> :
525   MipsPseudo<(outs CPURegs:$dst), (ins CPURegs:$ptr, CPURegs:$incr),
526              !strconcat("atomic_", Opstr, "\t$dst, $ptr, $incr"),
527              [(set CPURegs:$dst,
528               (Op CPURegs:$ptr, CPURegs:$incr))]>;
529
530 // Atomic Compare & Swap.
531 class AtomicCmpSwap<PatFrag Op, string Width> :
532   MipsPseudo<(outs CPURegs:$dst), 
533              (ins CPURegs:$ptr, CPURegs:$cmp, CPURegs:$swap),
534              !strconcat("atomic_cmp_swap_", Width, 
535                         "\t$dst, $ptr, $cmp, $swap"),
536              [(set CPURegs:$dst,
537               (Op CPURegs:$ptr, CPURegs:$cmp, CPURegs:$swap))]>;
538
539 //===----------------------------------------------------------------------===//
540 // Pseudo instructions
541 //===----------------------------------------------------------------------===//
542
543 // As stack alignment is always done with addiu, we need a 16-bit immediate
544 let Defs = [SP], Uses = [SP] in {
545 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins uimm16:$amt),
546                                   "!ADJCALLSTACKDOWN $amt",
547                                   [(callseq_start timm:$amt)]>;
548 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins uimm16:$amt1, uimm16:$amt2),
549                                   "!ADJCALLSTACKUP $amt1",
550                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
551 }
552
553 // Some assembly macros need to avoid pseudoinstructions and assembler
554 // automatic reodering, we should reorder ourselves.
555 def MACRO     : MipsPseudo<(outs), (ins), ".set\tmacro",     []>;
556 def REORDER   : MipsPseudo<(outs), (ins), ".set\treorder",   []>;
557 def NOMACRO   : MipsPseudo<(outs), (ins), ".set\tnomacro",   []>;
558 def NOREORDER : MipsPseudo<(outs), (ins), ".set\tnoreorder", []>;
559
560 // These macros are inserted to prevent GAS from complaining
561 // when using the AT register.
562 def NOAT      : MipsPseudo<(outs), (ins), ".set\tnoat", []>;
563 def ATMACRO   : MipsPseudo<(outs), (ins), ".set\tat", []>;
564
565 // When handling PIC code the assembler needs .cpload and .cprestore
566 // directives. If the real instructions corresponding these directives
567 // are used, we have the same behavior, but get also a bunch of warnings
568 // from the assembler.
569 def CPLOAD : MipsPseudo<(outs), (ins CPURegs:$picreg), ".cpload\t$picreg", []>;
570 def CPRESTORE : MipsPseudo<(outs), (ins i32imm:$loc), ".cprestore\t$loc", []>;
571
572 let usesCustomInserter = 1 in {
573   def ATOMIC_LOAD_ADD_I8   : Atomic2Ops<atomic_load_add_8, "load_add_8">;
574   def ATOMIC_LOAD_ADD_I16  : Atomic2Ops<atomic_load_add_16, "load_add_16">;
575   def ATOMIC_LOAD_ADD_I32  : Atomic2Ops<atomic_load_add_32, "load_add_32">;
576   def ATOMIC_LOAD_SUB_I8   : Atomic2Ops<atomic_load_sub_8, "load_sub_8">;
577   def ATOMIC_LOAD_SUB_I16  : Atomic2Ops<atomic_load_sub_16, "load_sub_16">;
578   def ATOMIC_LOAD_SUB_I32  : Atomic2Ops<atomic_load_sub_32, "load_sub_32">;
579   def ATOMIC_LOAD_AND_I8   : Atomic2Ops<atomic_load_and_8, "load_and_8">;
580   def ATOMIC_LOAD_AND_I16  : Atomic2Ops<atomic_load_and_16, "load_and_16">;
581   def ATOMIC_LOAD_AND_I32  : Atomic2Ops<atomic_load_and_32, "load_and_32">;
582   def ATOMIC_LOAD_OR_I8    : Atomic2Ops<atomic_load_or_8, "load_or_8">;
583   def ATOMIC_LOAD_OR_I16   : Atomic2Ops<atomic_load_or_16, "load_or_16">;
584   def ATOMIC_LOAD_OR_I32   : Atomic2Ops<atomic_load_or_32, "load_or_32">;
585   def ATOMIC_LOAD_XOR_I8   : Atomic2Ops<atomic_load_xor_8, "load_xor_8">;
586   def ATOMIC_LOAD_XOR_I16  : Atomic2Ops<atomic_load_xor_16, "load_xor_16">;
587   def ATOMIC_LOAD_XOR_I32  : Atomic2Ops<atomic_load_xor_32, "load_xor_32">;
588   def ATOMIC_LOAD_NAND_I8  : Atomic2Ops<atomic_load_nand_8, "load_nand_8">;
589   def ATOMIC_LOAD_NAND_I16 : Atomic2Ops<atomic_load_nand_16, "load_nand_16">;
590   def ATOMIC_LOAD_NAND_I32 : Atomic2Ops<atomic_load_nand_32, "load_nand_32">;
591
592   def ATOMIC_SWAP_I8       : Atomic2Ops<atomic_swap_8, "swap_8">;
593   def ATOMIC_SWAP_I16      : Atomic2Ops<atomic_swap_16, "swap_16">;
594   def ATOMIC_SWAP_I32      : Atomic2Ops<atomic_swap_32, "swap_32">;
595
596   def ATOMIC_CMP_SWAP_I8   : AtomicCmpSwap<atomic_cmp_swap_8, "8">;
597   def ATOMIC_CMP_SWAP_I16  : AtomicCmpSwap<atomic_cmp_swap_16, "16">;
598   def ATOMIC_CMP_SWAP_I32  : AtomicCmpSwap<atomic_cmp_swap_32, "32">;
599 }
600
601 //===----------------------------------------------------------------------===//
602 // Instruction definition
603 //===----------------------------------------------------------------------===//
604
605 //===----------------------------------------------------------------------===//
606 // MipsI Instructions
607 //===----------------------------------------------------------------------===//
608
609 /// Arithmetic Instructions (ALU Immediate)
610 def ADDiu   : ArithI<0x09, "addiu", add, simm16, immSExt16>;
611 def ADDi    : ArithOverflowI<0x08, "addi",  add, simm16, immSExt16>;
612 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
613 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16>;
614 def ANDi    : LogicI<0x0c, "andi", and>;
615 def ORi     : LogicI<0x0d, "ori",  or>;
616 def XORi    : LogicI<0x0e, "xori",  xor>;
617 def LUi     : LoadUpper<0x0f, "lui">;
618
619 /// Arithmetic Instructions (3-Operand, R-Type)
620 def ADDu    : ArithR<0x00, 0x21, "addu", add, IIAlu, 1>;
621 def SUBu    : ArithR<0x00, 0x23, "subu", sub, IIAlu>;
622 def ADD     : ArithOverflowR<0x00, 0x20, "add", 1>;
623 def SUB     : ArithOverflowR<0x00, 0x22, "sub">;
624 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt>;
625 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult>;
626 def AND     : LogicR<0x24, "and", and>;
627 def OR      : LogicR<0x25, "or",  or>;
628 def XOR     : LogicR<0x26, "xor", xor>;
629 def NOR     : LogicNOR<0x00, 0x27, "nor">;
630
631 /// Shift Instructions
632 def SLL     : LogicR_shift_rotate_imm<0x00, 0x00, "sll", shl>;
633 def SRL     : LogicR_shift_rotate_imm<0x02, 0x00, "srl", srl>;
634 def SRA     : LogicR_shift_rotate_imm<0x03, 0x00, "sra", sra>;
635 def SLLV    : LogicR_shift_rotate_reg<0x04, 0x00, "sllv", shl>;
636 def SRLV    : LogicR_shift_rotate_reg<0x06, 0x00, "srlv", srl>;
637 def SRAV    : LogicR_shift_rotate_reg<0x07, 0x00, "srav", sra>;
638
639 // Rotate Instructions
640 let Predicates = [HasMips32r2] in {
641     def ROTR    : LogicR_shift_rotate_imm<0x02, 0x01, "rotr", rotr>;
642     def ROTRV   : LogicR_shift_rotate_reg<0x06, 0x01, "rotrv", rotr>;
643 }
644
645 /// Load and Store Instructions
646 ///  aligned
647 defm LB      : LoadM32<0x20, "lb",  sextloadi8>;
648 defm LBu     : LoadM32<0x24, "lbu", zextloadi8>;
649 defm LH      : LoadM32<0x21, "lh",  sextloadi16_a>;
650 defm LHu     : LoadM32<0x25, "lhu", zextloadi16_a>;
651 defm LW      : LoadM32<0x23, "lw",  load_a>;
652 defm SB      : StoreM32<0x28, "sb", truncstorei8>;
653 defm SH      : StoreM32<0x29, "sh", truncstorei16_a>;
654 defm SW      : StoreM32<0x2b, "sw", store_a>;
655
656 ///  unaligned
657 defm ULH     : LoadM32<0x21, "ulh",  sextloadi16_u, 1>;
658 defm ULHu    : LoadM32<0x25, "ulhu", zextloadi16_u, 1>;
659 defm ULW     : LoadM32<0x23, "ulw",  load_u, 1>;
660 defm USH     : StoreM32<0x29, "ush", truncstorei16_u, 1>;
661 defm USW     : StoreM32<0x2b, "usw", store_u, 1>;
662
663 let hasSideEffects = 1 in
664 def SYNC : MipsInst<(outs), (ins i32imm:$stype), "sync $stype",
665                     [(MipsSync imm:$stype)], NoItinerary>
666 {
667   let opcode = 0;
668   let Inst{25-11} = 0;
669   let Inst{5-0} = 15;
670 }
671
672 /// Load-linked, Store-conditional
673 let mayLoad = 1 in
674   def LL    : FI<0x30, (outs CPURegs:$dst), (ins mem:$addr),
675               "ll\t$dst, $addr", [], IILoad>;
676 let mayStore = 1, Constraints = "$src = $dst" in
677   def SC    : FI<0x38, (outs CPURegs:$dst), (ins CPURegs:$src, mem:$addr),
678               "sc\t$src, $addr", [], IIStore>;
679
680 /// Jump and Branch Instructions
681 def J       : JumpFJ<0x02, "j">;
682 let isIndirectBranch = 1 in
683   def JR      : JumpFR<0x00, 0x08, "jr">;
684 def JAL     : JumpLink<0x03, "jal">;
685 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
686 def BEQ     : CBranch<0x04, "beq", seteq>;
687 def BNE     : CBranch<0x05, "bne", setne>;
688
689 let rt=1 in
690   def BGEZ  : CBranchZero<0x01, "bgez", setge>;
691
692 let rt=0 in {
693   def BGTZ  : CBranchZero<0x07, "bgtz", setgt>;
694   def BLEZ  : CBranchZero<0x07, "blez", setle>;
695   def BLTZ  : CBranchZero<0x01, "bltz", setlt>;
696 }
697
698 def BGEZAL  : BranchLink<"bgezal">;
699 def BLTZAL  : BranchLink<"bltzal">;
700
701 let isReturn=1, isTerminator=1, hasDelaySlot=1,
702     isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
703   def RET : FR <0x00, 0x02, (outs), (ins CPURegs:$target),
704                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
705
706 /// Multiply and Divide Instructions.
707 def MULT    : Mul<0x18, "mult", IIImul>;
708 def MULTu   : Mul<0x19, "multu", IIImul>;
709 def SDIV    : Div<MipsDivRem, 0x1a, "div", IIIdiv>;
710 def UDIV    : Div<MipsDivRemU, 0x1b, "divu", IIIdiv>;
711
712 let Defs = [HI] in
713   def MTHI  : MoveToLOHI<0x11, "mthi">;
714 let Defs = [LO] in
715   def MTLO  : MoveToLOHI<0x13, "mtlo">;
716
717 let Uses = [HI] in
718   def MFHI  : MoveFromLOHI<0x10, "mfhi">;
719 let Uses = [LO] in
720   def MFLO  : MoveFromLOHI<0x12, "mflo">;
721
722 /// Sign Ext In Register Instructions.
723 let Predicates = [HasSEInReg] in {
724   let shamt = 0x10, rs = 0 in
725     def SEB : SignExtInReg<0x21, "seb", i8>;
726
727   let shamt = 0x18, rs = 0 in
728     def SEH : SignExtInReg<0x20, "seh", i16>;
729 }
730
731 /// Count Leading
732 def CLZ : CountLeading<0b100000, "clz",
733                        [(set CPURegs:$dst, (ctlz CPURegs:$src))]>;
734 def CLO : CountLeading<0b100001, "clo",
735                        [(set CPURegs:$dst, (ctlz (not CPURegs:$src)))]>;
736
737 /// Byte Swap
738 let Predicates = [HasSwap] in {
739   let shamt = 0x3, rs = 0 in
740     def WSBW : ByteSwap<0x20, "wsbw">;
741 }
742
743 /// Conditional Move
744 def MIPS_CMOV_ZERO  : PatLeaf<(i32 0)>;
745 def MIPS_CMOV_NZERO : PatLeaf<(i32 1)>;
746
747 // Conditional moves:
748 // These instructions are expanded in
749 // MipsISelLowering::EmitInstrWithCustomInserter if target does not have
750 // conditional move instructions.
751 // flag:int, data:int
752 let usesCustomInserter = 1, shamt = 0, Constraints = "$F = $dst" in
753   class CondMovIntInt<bits<6> funct, string instr_asm> :
754     FR<0, funct, (outs CPURegs:$dst),
755        (ins CPURegs:$T, CPURegs:$cond, CPURegs:$F),
756        !strconcat(instr_asm, "\t$dst, $T, $cond"), [], NoItinerary>;
757
758 def MOVZ_I : CondMovIntInt<0x0a, "movz">;
759 def MOVN_I : CondMovIntInt<0x0b, "movn">;
760
761 /// No operation
762 let addr=0 in
763   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
764
765 // FrameIndexes are legalized when they are operands from load/store
766 // instructions. The same not happens for stack address copies, so an
767 // add op with mem ComplexPattern is used and the stack address copy
768 // can be matched. It's similar to Sparc LEA_ADDRi
769 def LEA_ADDiu : EffectiveAddress<"addiu\t$dst, $addr">;
770
771 // DynAlloc node points to dynamically allocated stack space.
772 // $sp is added to the list of implicitly used registers to prevent dead code
773 // elimination from removing instructions that modify $sp.
774 let Uses = [SP] in
775 def DynAlloc : EffectiveAddress<"addiu\t$dst, $addr">;
776
777 // MADD*/MSUB*
778 def MADD  : MArithR<0, "madd", MipsMAdd, 1>;
779 def MADDU : MArithR<1, "maddu", MipsMAddu, 1>;
780 def MSUB  : MArithR<4, "msub", MipsMSub>;
781 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
782
783 // MUL is a assembly macro in the current used ISAs. In recent ISA's
784 // it is a real instruction.
785 def MUL   : ArithR<0x1c, 0x02, "mul", mul, IIImul, 1>, Requires<[HasMips32]>;
786
787 def RDHWR : ReadHardware;
788
789 def EXT : ExtIns<0, "ext", (outs CPURegs:$rt),
790                  (ins CPURegs:$rs, uimm16:$pos, uimm16:$sz),
791                  [(set CPURegs:$rt,
792                    (MipsExt CPURegs:$rs, immZExt5:$pos, immZExt5:$sz))],
793                  NoItinerary>;
794
795 let Constraints = "$src = $rt" in
796 def INS : ExtIns<4, "ins", (outs CPURegs:$rt),
797                  (ins CPURegs:$rs, uimm16:$pos, uimm16:$sz, CPURegs:$src),
798                  [(set CPURegs:$rt,
799                    (MipsIns CPURegs:$rs, immZExt5:$pos, immZExt5:$sz,
800                     CPURegs:$src))],
801                  NoItinerary>;
802
803 //===----------------------------------------------------------------------===//
804 //  Arbitrary patterns that map to one or more instructions
805 //===----------------------------------------------------------------------===//
806
807 // Small immediates
808 def : Pat<(i32 immSExt16:$in),
809           (ADDiu ZERO, imm:$in)>;
810 def : Pat<(i32 immZExt16:$in),
811           (ORi ZERO, imm:$in)>;
812
813 // Arbitrary immediates
814 def : Pat<(i32 imm:$imm),
815           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
816
817 // Carry patterns
818 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
819           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
820 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
821           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
822 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
823           (ADDiu CPURegs:$src, imm:$imm)>;
824
825 // Call
826 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
827           (JAL tglobaladdr:$dst)>;
828 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
829           (JAL texternalsym:$dst)>;
830 //def : Pat<(MipsJmpLink CPURegs:$dst),
831 //          (JALR CPURegs:$dst)>;
832
833 // hi/lo relocs
834 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
835 def : Pat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
836 def : Pat<(MipsLo tglobaladdr:$in), (ADDiu ZERO, tglobaladdr:$in)>;
837 def : Pat<(MipsLo tblockaddress:$in), (ADDiu ZERO, tblockaddress:$in)>;
838 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
839           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
840 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
841           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
842
843 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
844 def : Pat<(MipsLo tjumptable:$in), (ADDiu ZERO, tjumptable:$in)>;
845 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
846           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
847
848 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
849 def : Pat<(MipsLo tconstpool:$in), (ADDiu ZERO, tconstpool:$in)>;
850 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
851           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
852
853 // gp_rel relocs
854 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
855           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
856 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
857           (ADDiu CPURegs:$gp, tconstpool:$in)>;
858
859 // tlsgd
860 def : Pat<(add CPURegs:$gp, (MipsTlsGd tglobaltlsaddr:$in)),
861           (ADDiu CPURegs:$gp, tglobaltlsaddr:$in)>;
862
863 // tprel hi/lo
864 def : Pat<(MipsTprelHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
865 def : Pat<(MipsTprelLo tglobaltlsaddr:$in), (ADDiu ZERO, tglobaltlsaddr:$in)>;
866 def : Pat<(add CPURegs:$hi, (MipsTprelLo tglobaltlsaddr:$lo)),
867           (ADDiu CPURegs:$hi, tglobaltlsaddr:$lo)>;
868
869 // wrapper_pic
870 class WrapperPICPat<SDNode node>:
871       Pat<(MipsWrapperPIC node:$in),
872           (ADDiu GP, node:$in)>;
873
874 def : WrapperPICPat<tglobaladdr>;
875 def : WrapperPICPat<tconstpool>;
876 def : WrapperPICPat<texternalsym>;
877 def : WrapperPICPat<tblockaddress>;
878 def : WrapperPICPat<tjumptable>;
879
880 // Mips does not have "not", so we expand our way
881 def : Pat<(not CPURegs:$in),
882           (NOR CPURegs:$in, ZERO)>;
883
884 // extended load and stores
885 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
886 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
887 def : Pat<(extloadi16_a addr:$src), (LHu addr:$src)>;
888 def : Pat<(extloadi16_u addr:$src), (ULHu addr:$src)>;
889
890 // peepholes
891 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
892
893 // brcond patterns
894 def : Pat<(brcond (i32 (setne CPURegs:$lhs, 0)), bb:$dst),
895           (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
896 def : Pat<(brcond (i32 (seteq CPURegs:$lhs, 0)), bb:$dst),
897           (BEQ CPURegs:$lhs, ZERO, bb:$dst)>;
898
899 def : Pat<(brcond (i32 (setge CPURegs:$lhs, CPURegs:$rhs)), bb:$dst),
900           (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
901 def : Pat<(brcond (i32 (setuge CPURegs:$lhs, CPURegs:$rhs)), bb:$dst),
902           (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
903 def : Pat<(brcond (i32 (setge CPURegs:$lhs, immSExt16:$rhs)), bb:$dst),
904           (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
905 def : Pat<(brcond (i32 (setuge CPURegs:$lhs, immSExt16:$rhs)), bb:$dst),
906           (BEQ (SLTiu CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
907
908 def : Pat<(brcond (i32 (setle CPURegs:$lhs, CPURegs:$rhs)), bb:$dst),
909           (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
910 def : Pat<(brcond (i32 (setule CPURegs:$lhs, CPURegs:$rhs)), bb:$dst),
911           (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
912
913 def : Pat<(brcond CPURegs:$cond, bb:$dst),
914           (BNE CPURegs:$cond, ZERO, bb:$dst)>;
915
916 // select patterns
917 multiclass MovzPats<RegisterClass RC, Instruction MOVZInst> {
918   def : Pat<(select (i32 (setge CPURegs:$lhs, CPURegs:$rhs)), RC:$T, RC:$F),
919             (MOVZInst RC:$T, (SLT CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
920   def : Pat<(select (i32 (setuge CPURegs:$lhs, CPURegs:$rhs)), RC:$T, RC:$F),
921             (MOVZInst RC:$T, (SLTu CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
922   def : Pat<(select (i32 (setge CPURegs:$lhs, immSExt16:$rhs)), RC:$T, RC:$F),
923             (MOVZInst RC:$T, (SLTi CPURegs:$lhs, immSExt16:$rhs), RC:$F)>;
924   def : Pat<(select (i32 (setuge CPURegs:$lh, immSExt16:$rh)), RC:$T, RC:$F),
925             (MOVZInst RC:$T, (SLTiu CPURegs:$lh, immSExt16:$rh), RC:$F)>;
926   def : Pat<(select (i32 (setle CPURegs:$lhs, CPURegs:$rhs)), RC:$T, RC:$F),
927             (MOVZInst RC:$T, (SLT CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
928   def : Pat<(select (i32 (setule CPURegs:$lhs, CPURegs:$rhs)), RC:$T, RC:$F),
929             (MOVZInst RC:$T, (SLTu CPURegs:$rhs, CPURegs:$lhs), RC:$F)>;
930   def : Pat<(select (i32 (seteq CPURegs:$lhs, CPURegs:$rhs)), RC:$T, RC:$F),
931             (MOVZInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
932   def : Pat<(select (i32 (seteq CPURegs:$lhs, 0)), RC:$T, RC:$F),
933             (MOVZInst RC:$T, CPURegs:$lhs, RC:$F)>;
934 }
935
936 multiclass MovnPats<RegisterClass RC, Instruction MOVNInst> {
937   def : Pat<(select (i32 (setne CPURegs:$lhs, CPURegs:$rhs)), RC:$T, RC:$F),
938             (MOVNInst RC:$T, (XOR CPURegs:$lhs, CPURegs:$rhs), RC:$F)>;
939   def : Pat<(select CPURegs:$cond, RC:$T, RC:$F),
940             (MOVNInst RC:$T, CPURegs:$cond, RC:$F)>;
941   def : Pat<(select (i32 (setne CPURegs:$lhs, 0)), RC:$T, RC:$F),
942             (MOVNInst RC:$T, CPURegs:$lhs, RC:$F)>;
943 }
944
945 defm : MovzPats<CPURegs, MOVZ_I>;
946 defm : MovnPats<CPURegs, MOVN_I>;
947
948 // setcc patterns
949 def : Pat<(seteq CPURegs:$lhs, CPURegs:$rhs),
950           (SLTu (XOR CPURegs:$lhs, CPURegs:$rhs), 1)>;
951 def : Pat<(setne CPURegs:$lhs, CPURegs:$rhs),
952           (SLTu ZERO, (XOR CPURegs:$lhs, CPURegs:$rhs))>;
953
954 def : Pat<(setle CPURegs:$lhs, CPURegs:$rhs),
955           (XORi (SLT CPURegs:$rhs, CPURegs:$lhs), 1)>;
956 def : Pat<(setule CPURegs:$lhs, CPURegs:$rhs),
957           (XORi (SLTu CPURegs:$rhs, CPURegs:$lhs), 1)>;
958
959 def : Pat<(setgt CPURegs:$lhs, CPURegs:$rhs),
960           (SLT CPURegs:$rhs, CPURegs:$lhs)>;
961 def : Pat<(setugt CPURegs:$lhs, CPURegs:$rhs),
962           (SLTu CPURegs:$rhs, CPURegs:$lhs)>;
963
964 def : Pat<(setge CPURegs:$lhs, CPURegs:$rhs),
965           (XORi (SLT CPURegs:$lhs, CPURegs:$rhs), 1)>;
966 def : Pat<(setuge CPURegs:$lhs, CPURegs:$rhs),
967           (XORi (SLTu CPURegs:$lhs, CPURegs:$rhs), 1)>;
968
969 def : Pat<(setge CPURegs:$lhs, immSExt16:$rhs),
970           (XORi (SLTi CPURegs:$lhs, immSExt16:$rhs), 1)>;
971 def : Pat<(setuge CPURegs:$lhs, immSExt16:$rhs),
972           (XORi (SLTiu CPURegs:$lhs, immSExt16:$rhs), 1)>;
973
974 // select MipsDynAlloc
975 def : Pat<(MipsDynAlloc addr:$f), (DynAlloc addr:$f)>;
976
977 //===----------------------------------------------------------------------===//
978 // Floating Point Support
979 //===----------------------------------------------------------------------===//
980
981 include "MipsInstrFPU.td"
982 include "Mips64InstrInfo.td"
983