This patch implements jalx instruction for Mips architecture.This instruction execute...
[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 //===----------------------------------------------------------------------===//
16 // Mips profiles and nodes
17 //===----------------------------------------------------------------------===//
18
19 def SDT_MipsJmpLink      : SDTypeProfile<0, 1, [SDTCisVT<0, iPTR>]>;
20 def SDT_MipsCMov         : SDTypeProfile<1, 4, [SDTCisSameAs<0, 1>,
21                                                 SDTCisSameAs<1, 2>,
22                                                 SDTCisSameAs<3, 4>,
23                                                 SDTCisInt<4>]>;
24 def SDT_MipsCallSeqStart : SDCallSeqStart<[SDTCisVT<0, i32>]>;
25 def SDT_MipsCallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i32>, SDTCisVT<1, i32>]>;
26 def SDT_MFLOHI : SDTypeProfile<1, 1, [SDTCisInt<0>, SDTCisVT<1, untyped>]>;
27 def SDT_MTLOHI : SDTypeProfile<1, 2, [SDTCisVT<0, untyped>,
28                                       SDTCisInt<1>, SDTCisSameAs<1, 2>]>;
29 def SDT_MipsMultDiv : SDTypeProfile<1, 2, [SDTCisVT<0, untyped>, SDTCisInt<1>,
30                                     SDTCisSameAs<1, 2>]>;
31 def SDT_MipsMAddMSub : SDTypeProfile<1, 3,
32                                      [SDTCisVT<0, untyped>, SDTCisSameAs<0, 3>,
33                                       SDTCisVT<1, i32>, SDTCisSameAs<1, 2>]>;
34 def SDT_MipsDivRem16 : SDTypeProfile<0, 2, [SDTCisInt<0>, SDTCisSameAs<0, 1>]>;
35
36 def SDT_MipsThreadPointer : SDTypeProfile<1, 0, [SDTCisPtrTy<0>]>;
37
38 def SDT_Sync             : SDTypeProfile<0, 1, [SDTCisVT<0, i32>]>;
39
40 def SDT_Ext : SDTypeProfile<1, 3, [SDTCisInt<0>, SDTCisSameAs<0, 1>,
41                                    SDTCisVT<2, i32>, SDTCisSameAs<2, 3>]>;
42 def SDT_Ins : SDTypeProfile<1, 4, [SDTCisInt<0>, SDTCisSameAs<0, 1>,
43                                    SDTCisVT<2, i32>, SDTCisSameAs<2, 3>,
44                                    SDTCisSameAs<0, 4>]>;
45
46 def SDTMipsLoadLR  : SDTypeProfile<1, 2,
47                                    [SDTCisInt<0>, SDTCisPtrTy<1>,
48                                     SDTCisSameAs<0, 2>]>;
49
50 // Call
51 def MipsJmpLink : SDNode<"MipsISD::JmpLink",SDT_MipsJmpLink,
52                          [SDNPHasChain, SDNPOutGlue, SDNPOptInGlue,
53                           SDNPVariadic]>;
54
55 // Tail call
56 def MipsTailCall : SDNode<"MipsISD::TailCall", SDT_MipsJmpLink,
57                           [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
58
59 // Hi and Lo nodes are used to handle global addresses. Used on
60 // MipsISelLowering to lower stuff like GlobalAddress, ExternalSymbol
61 // static model. (nothing to do with Mips Registers Hi and Lo)
62 def MipsHi    : SDNode<"MipsISD::Hi", SDTIntUnaryOp>;
63 def MipsLo    : SDNode<"MipsISD::Lo", SDTIntUnaryOp>;
64 def MipsGPRel : SDNode<"MipsISD::GPRel", SDTIntUnaryOp>;
65
66 // TlsGd node is used to handle General Dynamic TLS
67 def MipsTlsGd : SDNode<"MipsISD::TlsGd", SDTIntUnaryOp>;
68
69 // TprelHi and TprelLo nodes are used to handle Local Exec TLS
70 def MipsTprelHi    : SDNode<"MipsISD::TprelHi", SDTIntUnaryOp>;
71 def MipsTprelLo    : SDNode<"MipsISD::TprelLo", SDTIntUnaryOp>;
72
73 // Thread pointer
74 def MipsThreadPointer: SDNode<"MipsISD::ThreadPointer", SDT_MipsThreadPointer>;
75
76 // Return
77 def MipsRet : SDNode<"MipsISD::Ret", SDTNone,
78                      [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
79
80 // These are target-independent nodes, but have target-specific formats.
81 def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_MipsCallSeqStart,
82                            [SDNPHasChain, SDNPSideEffect, SDNPOutGlue]>;
83 def callseq_end   : SDNode<"ISD::CALLSEQ_END", SDT_MipsCallSeqEnd,
84                            [SDNPHasChain, SDNPSideEffect,
85                             SDNPOptInGlue, SDNPOutGlue]>;
86
87 // Nodes used to extract LO/HI registers.
88 def MipsMFHI : SDNode<"MipsISD::MFHI", SDT_MFLOHI>;
89 def MipsMFLO : SDNode<"MipsISD::MFLO", SDT_MFLOHI>;
90
91 // Node used to insert 32-bit integers to LOHI register pair.
92 def MipsMTLOHI : SDNode<"MipsISD::MTLOHI", SDT_MTLOHI>;
93
94 // Mult nodes.
95 def MipsMult  : SDNode<"MipsISD::Mult", SDT_MipsMultDiv>;
96 def MipsMultu : SDNode<"MipsISD::Multu", SDT_MipsMultDiv>;
97
98 // MAdd*/MSub* nodes
99 def MipsMAdd  : SDNode<"MipsISD::MAdd", SDT_MipsMAddMSub>;
100 def MipsMAddu : SDNode<"MipsISD::MAddu", SDT_MipsMAddMSub>;
101 def MipsMSub  : SDNode<"MipsISD::MSub", SDT_MipsMAddMSub>;
102 def MipsMSubu : SDNode<"MipsISD::MSubu", SDT_MipsMAddMSub>;
103
104 // DivRem(u) nodes
105 def MipsDivRem    : SDNode<"MipsISD::DivRem", SDT_MipsMultDiv>;
106 def MipsDivRemU   : SDNode<"MipsISD::DivRemU", SDT_MipsMultDiv>;
107 def MipsDivRem16  : SDNode<"MipsISD::DivRem16", SDT_MipsDivRem16,
108                            [SDNPOutGlue]>;
109 def MipsDivRemU16 : SDNode<"MipsISD::DivRemU16", SDT_MipsDivRem16,
110                            [SDNPOutGlue]>;
111
112 // Target constant nodes that are not part of any isel patterns and remain
113 // unchanged can cause instructions with illegal operands to be emitted.
114 // Wrapper node patterns give the instruction selector a chance to replace
115 // target constant nodes that would otherwise remain unchanged with ADDiu
116 // nodes. Without these wrapper node patterns, the following conditional move
117 // instruction is emitted when function cmov2 in test/CodeGen/Mips/cmov.ll is
118 // compiled:
119 //  movn  %got(d)($gp), %got(c)($gp), $4
120 // This instruction is illegal since movn can take only register operands.
121
122 def MipsWrapper    : SDNode<"MipsISD::Wrapper", SDTIntBinOp>;
123
124 def MipsSync : SDNode<"MipsISD::Sync", SDT_Sync, [SDNPHasChain,SDNPSideEffect]>;
125
126 def MipsExt :  SDNode<"MipsISD::Ext", SDT_Ext>;
127 def MipsIns :  SDNode<"MipsISD::Ins", SDT_Ins>;
128
129 def MipsLWL : SDNode<"MipsISD::LWL", SDTMipsLoadLR,
130                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
131 def MipsLWR : SDNode<"MipsISD::LWR", SDTMipsLoadLR,
132                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
133 def MipsSWL : SDNode<"MipsISD::SWL", SDTStore,
134                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
135 def MipsSWR : SDNode<"MipsISD::SWR", SDTStore,
136                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
137 def MipsLDL : SDNode<"MipsISD::LDL", SDTMipsLoadLR,
138                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
139 def MipsLDR : SDNode<"MipsISD::LDR", SDTMipsLoadLR,
140                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
141 def MipsSDL : SDNode<"MipsISD::SDL", SDTStore,
142                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
143 def MipsSDR : SDNode<"MipsISD::SDR", SDTStore,
144                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
145
146 //===----------------------------------------------------------------------===//
147 // Mips Instruction Predicate Definitions.
148 //===----------------------------------------------------------------------===//
149 def HasSEInReg  :     Predicate<"Subtarget.hasSEInReg()">,
150                       AssemblerPredicate<"FeatureSEInReg">;
151 def HasBitCount :     Predicate<"Subtarget.hasBitCount()">,
152                       AssemblerPredicate<"FeatureBitCount">;
153 def HasSwap     :     Predicate<"Subtarget.hasSwap()">,
154                       AssemblerPredicate<"FeatureSwap">;
155 def HasCondMov  :     Predicate<"Subtarget.hasCondMov()">,
156                       AssemblerPredicate<"FeatureCondMov">;
157 def HasFPIdx    :     Predicate<"Subtarget.hasFPIdx()">,
158                       AssemblerPredicate<"FeatureFPIdx">;
159 def HasMips32    :    Predicate<"Subtarget.hasMips32()">,
160                       AssemblerPredicate<"FeatureMips32">;
161 def HasMips32r2  :    Predicate<"Subtarget.hasMips32r2()">,
162                       AssemblerPredicate<"FeatureMips32r2">;
163 def HasMips64    :    Predicate<"Subtarget.hasMips64()">,
164                       AssemblerPredicate<"FeatureMips64">;
165 def NotMips64    :    Predicate<"!Subtarget.hasMips64()">,
166                       AssemblerPredicate<"!FeatureMips64">;
167 def HasMips64r2  :    Predicate<"Subtarget.hasMips64r2()">,
168                       AssemblerPredicate<"FeatureMips64r2">;
169 def IsN64       :     Predicate<"Subtarget.isABI_N64()">,
170                       AssemblerPredicate<"FeatureN64">;
171 def InMips16Mode :    Predicate<"Subtarget.inMips16Mode()">,
172                       AssemblerPredicate<"FeatureMips16">;
173 def RelocStatic :     Predicate<"TM.getRelocationModel() == Reloc::Static">,
174                       AssemblerPredicate<"FeatureMips32">;
175 def RelocPIC    :     Predicate<"TM.getRelocationModel() == Reloc::PIC_">,
176                       AssemblerPredicate<"FeatureMips32">;
177 def NoNaNsFPMath :    Predicate<"TM.Options.NoNaNsFPMath">,
178                       AssemblerPredicate<"FeatureMips32">;
179 def HasStdEnc :       Predicate<"Subtarget.hasStandardEncoding()">,
180                       AssemblerPredicate<"!FeatureMips16">;
181 def NotDSP :          Predicate<"!Subtarget.hasDSP()">;
182 def InMicroMips    :  Predicate<"Subtarget.inMicroMipsMode()">,
183                       AssemblerPredicate<"FeatureMicroMips">;
184 def NotInMicroMips :  Predicate<"!Subtarget.inMicroMipsMode()">,
185                       AssemblerPredicate<"!FeatureMicroMips">;
186 def IsLE           :  Predicate<"Subtarget.isLittle()">;
187 def IsBE           :  Predicate<"!Subtarget.isLittle()">;
188 def IsNotNaCl    :    Predicate<"!Subtarget.isTargetNaCl()">;
189
190 class MipsPat<dag pattern, dag result> : Pat<pattern, result> {
191   let Predicates = [HasStdEnc];
192 }
193
194 class IsCommutable {
195   bit isCommutable = 1;
196 }
197
198 class IsBranch {
199   bit isBranch = 1;
200 }
201
202 class IsReturn {
203   bit isReturn = 1;
204 }
205
206 class IsCall {
207   bit isCall = 1;
208 }
209
210 class IsTailCall {
211   bit isCall = 1;
212   bit isTerminator = 1;
213   bit isReturn = 1;
214   bit isBarrier = 1;
215   bit hasExtraSrcRegAllocReq = 1;
216   bit isCodeGenOnly = 1;
217 }
218
219 class IsAsCheapAsAMove {
220   bit isAsCheapAsAMove = 1;
221 }
222
223 class NeverHasSideEffects {
224   bit neverHasSideEffects = 1;
225 }
226
227 //===----------------------------------------------------------------------===//
228 // Instruction format superclass
229 //===----------------------------------------------------------------------===//
230
231 include "MipsInstrFormats.td"
232
233 //===----------------------------------------------------------------------===//
234 // Mips Operand, Complex Patterns and Transformations Definitions.
235 //===----------------------------------------------------------------------===//
236
237 // Instruction operand types
238 def jmptarget   : Operand<OtherVT> {
239   let EncoderMethod = "getJumpTargetOpValue";
240 }
241 def brtarget    : Operand<OtherVT> {
242   let EncoderMethod = "getBranchTargetOpValue";
243   let OperandType = "OPERAND_PCREL";
244   let DecoderMethod = "DecodeBranchTarget";
245 }
246 def calltarget  : Operand<iPTR> {
247   let EncoderMethod = "getJumpTargetOpValue";
248 }
249
250 def simm16      : Operand<i32> {
251   let DecoderMethod= "DecodeSimm16";
252 }
253
254 def simm20      : Operand<i32> {
255 }
256
257 def uimm20      : Operand<i32> {
258 }
259
260 def uimm10      : Operand<i32> {
261 }
262
263 def simm16_64   : Operand<i64> {
264   let DecoderMethod = "DecodeSimm16";
265 }
266
267 // Unsigned Operand
268 def uimm5       : Operand<i32> {
269   let PrintMethod = "printUnsignedImm";
270 }
271
272 def uimm6 : Operand<i32> {
273   let PrintMethod = "printUnsignedImm";
274 }
275
276 def uimm16      : Operand<i32> {
277   let PrintMethod = "printUnsignedImm";
278 }
279
280 def pcrel16      : Operand<i32> {
281 }
282
283 def MipsMemAsmOperand : AsmOperandClass {
284   let Name = "Mem";
285   let ParserMethod = "parseMemOperand";
286 }
287
288 def MipsInvertedImmoperand : AsmOperandClass {
289   let Name = "InvNum";
290   let RenderMethod = "addImmOperands";
291   let ParserMethod = "parseInvNum";
292 }
293
294 def PtrRegAsmOperand : AsmOperandClass {
295   let Name = "PtrReg";
296   let ParserMethod = "parsePtrReg";
297 }
298
299
300 def InvertedImOperand : Operand<i32> {
301   let ParserMatchClass = MipsInvertedImmoperand;
302 }
303
304 class mem_generic : Operand<iPTR> {
305   let PrintMethod = "printMemOperand";
306   let MIOperandInfo = (ops ptr_rc, simm16);
307   let EncoderMethod = "getMemEncoding";
308   let ParserMatchClass = MipsMemAsmOperand;
309   let OperandType = "OPERAND_MEMORY";
310 }
311
312 // Address operand
313 def mem : mem_generic;
314
315 // MSA specific address operand
316 def mem_msa : mem_generic {
317   let EncoderMethod = "getMSAMemEncoding";
318 }
319
320 def mem_ea : Operand<iPTR> {
321   let PrintMethod = "printMemOperandEA";
322   let MIOperandInfo = (ops ptr_rc, simm16);
323   let EncoderMethod = "getMemEncoding";
324   let OperandType = "OPERAND_MEMORY";
325 }
326
327 def PtrRC : Operand<iPTR> {
328   let MIOperandInfo = (ops ptr_rc);
329   let DecoderMethod = "DecodePtrRegisterClass";
330   let ParserMatchClass = PtrRegAsmOperand;
331 }
332
333 // size operand of ext instruction
334 def size_ext : Operand<i32> {
335   let EncoderMethod = "getSizeExtEncoding";
336   let DecoderMethod = "DecodeExtSize";
337 }
338
339 // size operand of ins instruction
340 def size_ins : Operand<i32> {
341   let EncoderMethod = "getSizeInsEncoding";
342   let DecoderMethod = "DecodeInsSize";
343 }
344
345 // Transformation Function - get the lower 16 bits.
346 def LO16 : SDNodeXForm<imm, [{
347   return getImm(N, N->getZExtValue() & 0xFFFF);
348 }]>;
349
350 // Transformation Function - get the higher 16 bits.
351 def HI16 : SDNodeXForm<imm, [{
352   return getImm(N, (N->getZExtValue() >> 16) & 0xFFFF);
353 }]>;
354
355 // Plus 1.
356 def Plus1 : SDNodeXForm<imm, [{ return getImm(N, N->getSExtValue() + 1); }]>;
357
358 // Node immediate fits as 16-bit sign extended on target immediate.
359 // e.g. addi, andi
360 def immSExt8  : PatLeaf<(imm), [{ return isInt<8>(N->getSExtValue()); }]>;
361
362 // Node immediate fits as 16-bit sign extended on target immediate.
363 // e.g. addi, andi
364 def immSExt16  : PatLeaf<(imm), [{ return isInt<16>(N->getSExtValue()); }]>;
365
366 // Node immediate fits as 15-bit sign extended on target immediate.
367 // e.g. addi, andi
368 def immSExt15  : PatLeaf<(imm), [{ return isInt<15>(N->getSExtValue()); }]>;
369
370 // Node immediate fits as 16-bit zero extended on target immediate.
371 // The LO16 param means that only the lower 16 bits of the node
372 // immediate are caught.
373 // e.g. addiu, sltiu
374 def immZExt16  : PatLeaf<(imm), [{
375   if (N->getValueType(0) == MVT::i32)
376     return (uint32_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
377   else
378     return (uint64_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
379 }], LO16>;
380
381 // Immediate can be loaded with LUi (32-bit int with lower 16-bit cleared).
382 def immLow16Zero : PatLeaf<(imm), [{
383   int64_t Val = N->getSExtValue();
384   return isInt<32>(Val) && !(Val & 0xffff);
385 }]>;
386
387 // shamt field must fit in 5 bits.
388 def immZExt5 : ImmLeaf<i32, [{return Imm == (Imm & 0x1f);}]>;
389
390 // True if (N + 1) fits in 16-bit field.
391 def immSExt16Plus1 : PatLeaf<(imm), [{
392   return isInt<17>(N->getSExtValue()) && isInt<16>(N->getSExtValue() + 1);
393 }]>;
394
395 // Mips Address Mode! SDNode frameindex could possibily be a match
396 // since load and store instructions from stack used it.
397 def addr :
398   ComplexPattern<iPTR, 2, "selectIntAddr", [frameindex]>;
399
400 def addrRegImm :
401   ComplexPattern<iPTR, 2, "selectAddrRegImm", [frameindex]>;
402
403 def addrRegReg :
404   ComplexPattern<iPTR, 2, "selectAddrRegReg", [frameindex]>;
405
406 def addrDefault :
407   ComplexPattern<iPTR, 2, "selectAddrDefault", [frameindex]>;
408
409 //===----------------------------------------------------------------------===//
410 // Instructions specific format
411 //===----------------------------------------------------------------------===//
412
413 // Arithmetic and logical instructions with 3 register operands.
414 class ArithLogicR<string opstr, RegisterOperand RO, bit isComm = 0,
415                   InstrItinClass Itin = NoItinerary,
416                   SDPatternOperator OpNode = null_frag>:
417   InstSE<(outs RO:$rd), (ins RO:$rs, RO:$rt),
418          !strconcat(opstr, "\t$rd, $rs, $rt"),
419          [(set RO:$rd, (OpNode RO:$rs, RO:$rt))], Itin, FrmR, opstr> {
420   let isCommutable = isComm;
421   let isReMaterializable = 1;
422 }
423
424 // Arithmetic and logical instructions with 2 register operands.
425 class ArithLogicI<string opstr, Operand Od, RegisterOperand RO,
426                   InstrItinClass Itin = NoItinerary,
427                   SDPatternOperator imm_type = null_frag,
428                   SDPatternOperator OpNode = null_frag> :
429   InstSE<(outs RO:$rt), (ins RO:$rs, Od:$imm16),
430          !strconcat(opstr, "\t$rt, $rs, $imm16"),
431          [(set RO:$rt, (OpNode RO:$rs, imm_type:$imm16))],
432          Itin, FrmI, opstr> {
433   let isReMaterializable = 1;
434   let TwoOperandAliasConstraint = "$rs = $rt";
435 }
436
437 // Arithmetic Multiply ADD/SUB
438 class MArithR<string opstr, InstrItinClass itin, bit isComm = 0> :
439   InstSE<(outs), (ins GPR32Opnd:$rs, GPR32Opnd:$rt),
440          !strconcat(opstr, "\t$rs, $rt"), [], itin, FrmR, opstr> {
441   let Defs = [HI0, LO0];
442   let Uses = [HI0, LO0];
443   let isCommutable = isComm;
444 }
445
446 //  Logical
447 class LogicNOR<string opstr, RegisterOperand RO>:
448   InstSE<(outs RO:$rd), (ins RO:$rs, RO:$rt),
449          !strconcat(opstr, "\t$rd, $rs, $rt"),
450          [(set RO:$rd, (not (or RO:$rs, RO:$rt)))], II_NOR, FrmR, opstr> {
451   let isCommutable = 1;
452 }
453
454 // Shifts
455 class shift_rotate_imm<string opstr, Operand ImmOpnd,
456                        RegisterOperand RO, InstrItinClass itin,
457                        SDPatternOperator OpNode = null_frag,
458                        SDPatternOperator PF = null_frag> :
459   InstSE<(outs RO:$rd), (ins RO:$rt, ImmOpnd:$shamt),
460          !strconcat(opstr, "\t$rd, $rt, $shamt"),
461          [(set RO:$rd, (OpNode RO:$rt, PF:$shamt))], itin, FrmR, opstr>;
462
463 class shift_rotate_reg<string opstr, RegisterOperand RO, InstrItinClass itin,
464                        SDPatternOperator OpNode = null_frag>:
465   InstSE<(outs RO:$rd), (ins RO:$rt, GPR32Opnd:$rs),
466          !strconcat(opstr, "\t$rd, $rt, $rs"),
467          [(set RO:$rd, (OpNode RO:$rt, GPR32Opnd:$rs))], itin, FrmR,
468          opstr>;
469
470 // Load Upper Imediate
471 class LoadUpper<string opstr, RegisterOperand RO, Operand Imm>:
472   InstSE<(outs RO:$rt), (ins Imm:$imm16), !strconcat(opstr, "\t$rt, $imm16"),
473          [], II_LUI, FrmI, opstr>, IsAsCheapAsAMove {
474   let neverHasSideEffects = 1;
475   let isReMaterializable = 1;
476 }
477
478 // Memory Load/Store
479 class Load<string opstr, DAGOperand RO, SDPatternOperator OpNode = null_frag,
480            InstrItinClass Itin = NoItinerary, ComplexPattern Addr = addr> :
481   InstSE<(outs RO:$rt), (ins mem:$addr), !strconcat(opstr, "\t$rt, $addr"),
482          [(set RO:$rt, (OpNode Addr:$addr))], Itin, FrmI, opstr> {
483   let DecoderMethod = "DecodeMem";
484   let canFoldAsLoad = 1;
485   let mayLoad = 1;
486 }
487
488 class Store<string opstr, DAGOperand RO, SDPatternOperator OpNode = null_frag,
489             InstrItinClass Itin = NoItinerary, ComplexPattern Addr = addr> :
490   InstSE<(outs), (ins RO:$rt, mem:$addr), !strconcat(opstr, "\t$rt, $addr"),
491          [(OpNode RO:$rt, Addr:$addr)], Itin, FrmI, opstr> {
492   let DecoderMethod = "DecodeMem";
493   let mayStore = 1;
494 }
495
496 // Load/Store Left/Right
497 let canFoldAsLoad = 1 in
498 class LoadLeftRight<string opstr, SDNode OpNode, RegisterOperand RO,
499                     InstrItinClass Itin> :
500   InstSE<(outs RO:$rt), (ins mem:$addr, RO:$src),
501          !strconcat(opstr, "\t$rt, $addr"),
502          [(set RO:$rt, (OpNode addr:$addr, RO:$src))], Itin, FrmI> {
503   let DecoderMethod = "DecodeMem";
504   string Constraints = "$src = $rt";
505 }
506
507 class StoreLeftRight<string opstr, SDNode OpNode, RegisterOperand RO,
508                      InstrItinClass Itin> :
509   InstSE<(outs), (ins RO:$rt, mem:$addr), !strconcat(opstr, "\t$rt, $addr"),
510          [(OpNode RO:$rt, addr:$addr)], Itin, FrmI> {
511   let DecoderMethod = "DecodeMem";
512 }
513
514 // Conditional Branch
515 class CBranch<string opstr, DAGOperand opnd, PatFrag cond_op,
516               RegisterOperand RO> :
517   InstSE<(outs), (ins RO:$rs, RO:$rt, opnd:$offset),
518          !strconcat(opstr, "\t$rs, $rt, $offset"),
519          [(brcond (i32 (cond_op RO:$rs, RO:$rt)), bb:$offset)], IIBranch,
520          FrmI, opstr> {
521   let isBranch = 1;
522   let isTerminator = 1;
523   let hasDelaySlot = 1;
524   let Defs = [AT];
525 }
526
527 class CBranchZero<string opstr, DAGOperand opnd, PatFrag cond_op,
528                   RegisterOperand RO> :
529   InstSE<(outs), (ins RO:$rs, opnd:$offset),
530          !strconcat(opstr, "\t$rs, $offset"),
531          [(brcond (i32 (cond_op RO:$rs, 0)), bb:$offset)], IIBranch,
532          FrmI, opstr> {
533   let isBranch = 1;
534   let isTerminator = 1;
535   let hasDelaySlot = 1;
536   let Defs = [AT];
537 }
538
539 // SetCC
540 class SetCC_R<string opstr, PatFrag cond_op, RegisterOperand RO> :
541   InstSE<(outs GPR32Opnd:$rd), (ins RO:$rs, RO:$rt),
542          !strconcat(opstr, "\t$rd, $rs, $rt"),
543          [(set GPR32Opnd:$rd, (cond_op RO:$rs, RO:$rt))],
544          II_SLT_SLTU, FrmR, opstr>;
545
546 class SetCC_I<string opstr, PatFrag cond_op, Operand Od, PatLeaf imm_type,
547               RegisterOperand RO>:
548   InstSE<(outs GPR32Opnd:$rt), (ins RO:$rs, Od:$imm16),
549          !strconcat(opstr, "\t$rt, $rs, $imm16"),
550          [(set GPR32Opnd:$rt, (cond_op RO:$rs, imm_type:$imm16))],
551          II_SLTI_SLTIU, FrmI, opstr>;
552
553 // Jump
554 class JumpFJ<DAGOperand opnd, string opstr, SDPatternOperator operator,
555              SDPatternOperator targetoperator, string bopstr> :
556   InstSE<(outs), (ins opnd:$target), !strconcat(opstr, "\t$target"),
557          [(operator targetoperator:$target)], IIBranch, FrmJ, bopstr> {
558   let isTerminator=1;
559   let isBarrier=1;
560   let hasDelaySlot = 1;
561   let DecoderMethod = "DecodeJumpTarget";
562   let Defs = [AT];
563 }
564
565 // Unconditional branch
566 class UncondBranch<Instruction BEQInst> :
567   PseudoSE<(outs), (ins brtarget:$offset), [(br bb:$offset)], IIBranch>,
568   PseudoInstExpansion<(BEQInst ZERO, ZERO, brtarget:$offset)> {
569   let isBranch = 1;
570   let isTerminator = 1;
571   let isBarrier = 1;
572   let hasDelaySlot = 1;
573   let Predicates = [RelocPIC, HasStdEnc];
574   let Defs = [AT];
575 }
576
577 // Base class for indirect branch and return instruction classes.
578 let isTerminator=1, isBarrier=1, hasDelaySlot = 1 in
579 class JumpFR<string opstr, RegisterOperand RO,
580              SDPatternOperator operator = null_frag>:
581   InstSE<(outs), (ins RO:$rs), "jr\t$rs", [(operator RO:$rs)], IIBranch,
582          FrmR, opstr>;
583
584 // Indirect branch
585 class IndirectBranch<string opstr, RegisterOperand RO> :
586       JumpFR<opstr, RO, brind> {
587   let isBranch = 1;
588   let isIndirectBranch = 1;
589 }
590
591 // Return instruction
592 class RetBase<string opstr, RegisterOperand RO>: JumpFR<opstr, RO> {
593   let isReturn = 1;
594   let isCodeGenOnly = 1;
595   let hasCtrlDep = 1;
596   let hasExtraSrcRegAllocReq = 1;
597 }
598
599 // Jump and Link (Call)
600 let isCall=1, hasDelaySlot=1, Defs = [RA] in {
601   class JumpLink<string opstr, DAGOperand opnd> :
602     InstSE<(outs), (ins opnd:$target), !strconcat(opstr, "\t$target"),
603            [(MipsJmpLink imm:$target)], IIBranch, FrmJ, opstr> {
604     let DecoderMethod = "DecodeJumpTarget";
605   }
606
607   class JumpLinkRegPseudo<RegisterOperand RO, Instruction JALRInst,
608                           Register RetReg, RegisterOperand ResRO = RO>:
609     PseudoSE<(outs), (ins RO:$rs), [(MipsJmpLink RO:$rs)], IIBranch>,
610     PseudoInstExpansion<(JALRInst RetReg, ResRO:$rs)>;
611
612   class JumpLinkReg<string opstr, RegisterOperand RO>:
613     InstSE<(outs RO:$rd), (ins RO:$rs), !strconcat(opstr, "\t$rd, $rs"),
614            [], IIBranch, FrmR, opstr>;
615
616   class BGEZAL_FT<string opstr, DAGOperand opnd, RegisterOperand RO> :
617     InstSE<(outs), (ins RO:$rs, opnd:$offset),
618            !strconcat(opstr, "\t$rs, $offset"), [], IIBranch, FrmI, opstr>;
619
620 }
621
622 let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, hasDelaySlot = 1,
623     hasExtraSrcRegAllocReq = 1, Defs = [AT] in {
624   class TailCall<Instruction JumpInst> :
625     PseudoSE<(outs), (ins calltarget:$target), [], IIBranch>,
626     PseudoInstExpansion<(JumpInst jmptarget:$target)>;
627
628   class TailCallReg<RegisterOperand RO, Instruction JRInst,
629                     RegisterOperand ResRO = RO> :
630     PseudoSE<(outs), (ins RO:$rs), [(MipsTailCall RO:$rs)], IIBranch>,
631     PseudoInstExpansion<(JRInst ResRO:$rs)>;
632 }
633
634 class BAL_BR_Pseudo<Instruction RealInst> :
635   PseudoSE<(outs), (ins brtarget:$offset), [], IIBranch>,
636   PseudoInstExpansion<(RealInst ZERO, brtarget:$offset)> {
637   let isBranch = 1;
638   let isTerminator = 1;
639   let isBarrier = 1;
640   let hasDelaySlot = 1;
641   let Defs = [RA];
642 }
643
644 // Syscall
645 class SYS_FT<string opstr> :
646   InstSE<(outs), (ins uimm20:$code_),
647          !strconcat(opstr, "\t$code_"), [], NoItinerary, FrmI, opstr>;
648 // Break
649 class BRK_FT<string opstr> :
650   InstSE<(outs), (ins uimm10:$code_1, uimm10:$code_2),
651          !strconcat(opstr, "\t$code_1, $code_2"), [], NoItinerary,
652          FrmOther, opstr>;
653
654 // (D)Eret
655 class ER_FT<string opstr> :
656   InstSE<(outs), (ins),
657          opstr, [], NoItinerary, FrmOther, opstr>;
658
659 // Interrupts
660 class DEI_FT<string opstr, RegisterOperand RO> :
661   InstSE<(outs RO:$rt), (ins),
662          !strconcat(opstr, "\t$rt"), [], NoItinerary, FrmOther, opstr>;
663
664 // Wait
665 class WAIT_FT<string opstr> :
666   InstSE<(outs), (ins), opstr, [], NoItinerary, FrmOther, opstr>;
667
668 // Sync
669 let hasSideEffects = 1 in
670 class SYNC_FT<string opstr> :
671   InstSE<(outs), (ins i32imm:$stype), "sync $stype", [(MipsSync imm:$stype)],
672          NoItinerary, FrmOther, opstr>;
673
674 let hasSideEffects = 1 in
675 class TEQ_FT<string opstr, RegisterOperand RO> :
676   InstSE<(outs), (ins RO:$rs, RO:$rt, uimm16:$code_),
677          !strconcat(opstr, "\t$rs, $rt, $code_"), [], NoItinerary,
678          FrmI, opstr>;
679
680 class TEQI_FT<string opstr, RegisterOperand RO> :
681   InstSE<(outs), (ins RO:$rs, uimm16:$imm16),
682          !strconcat(opstr, "\t$rs, $imm16"), [], NoItinerary, FrmOther, opstr>;
683 // Mul, Div
684 class Mult<string opstr, InstrItinClass itin, RegisterOperand RO,
685            list<Register> DefRegs> :
686   InstSE<(outs), (ins RO:$rs, RO:$rt), !strconcat(opstr, "\t$rs, $rt"), [],
687          itin, FrmR, opstr> {
688   let isCommutable = 1;
689   let Defs = DefRegs;
690   let neverHasSideEffects = 1;
691 }
692
693 // Pseudo multiply/divide instruction with explicit accumulator register
694 // operands.
695 class MultDivPseudo<Instruction RealInst, RegisterClass R0, RegisterOperand R1,
696                     SDPatternOperator OpNode, InstrItinClass Itin,
697                     bit IsComm = 1, bit HasSideEffects = 0,
698                     bit UsesCustomInserter = 0> :
699   PseudoSE<(outs R0:$ac), (ins R1:$rs, R1:$rt),
700            [(set R0:$ac, (OpNode R1:$rs, R1:$rt))], Itin>,
701   PseudoInstExpansion<(RealInst R1:$rs, R1:$rt)> {
702   let isCommutable = IsComm;
703   let hasSideEffects = HasSideEffects;
704   let usesCustomInserter = UsesCustomInserter;
705 }
706
707 // Pseudo multiply add/sub instruction with explicit accumulator register
708 // operands.
709 class MAddSubPseudo<Instruction RealInst, SDPatternOperator OpNode,
710                     InstrItinClass itin>
711   : PseudoSE<(outs ACC64:$ac),
712              (ins GPR32Opnd:$rs, GPR32Opnd:$rt, ACC64:$acin),
713              [(set ACC64:$ac,
714               (OpNode GPR32Opnd:$rs, GPR32Opnd:$rt, ACC64:$acin))],
715              itin>,
716     PseudoInstExpansion<(RealInst GPR32Opnd:$rs, GPR32Opnd:$rt)> {
717   string Constraints = "$acin = $ac";
718 }
719
720 class Div<string opstr, InstrItinClass itin, RegisterOperand RO,
721           list<Register> DefRegs> :
722   InstSE<(outs), (ins RO:$rs, RO:$rt), !strconcat(opstr, "\t$$zero, $rs, $rt"),
723          [], itin, FrmR, opstr> {
724   let Defs = DefRegs;
725 }
726
727 // Move from Hi/Lo
728 class PseudoMFLOHI<RegisterClass DstRC, RegisterClass SrcRC, SDNode OpNode>
729   : PseudoSE<(outs DstRC:$rd), (ins SrcRC:$hilo),
730              [(set DstRC:$rd, (OpNode SrcRC:$hilo))], II_MFHI_MFLO>;
731
732 class MoveFromLOHI<string opstr, RegisterOperand RO, Register UseReg>:
733   InstSE<(outs RO:$rd), (ins), !strconcat(opstr, "\t$rd"), [], II_MFHI_MFLO,
734          FrmR, opstr> {
735   let Uses = [UseReg];
736   let neverHasSideEffects = 1;
737 }
738
739 class PseudoMTLOHI<RegisterClass DstRC, RegisterClass SrcRC>
740   : PseudoSE<(outs DstRC:$lohi), (ins SrcRC:$lo, SrcRC:$hi),
741              [(set DstRC:$lohi, (MipsMTLOHI SrcRC:$lo, SrcRC:$hi))],
742              II_MTHI_MTLO>;
743
744 class MoveToLOHI<string opstr, RegisterOperand RO, list<Register> DefRegs>:
745   InstSE<(outs), (ins RO:$rs), !strconcat(opstr, "\t$rs"), [], II_MTHI_MTLO,
746   FrmR, opstr> {
747   let Defs = DefRegs;
748   let neverHasSideEffects = 1;
749 }
750
751 class EffectiveAddress<string opstr, RegisterOperand RO> :
752   InstSE<(outs RO:$rt), (ins mem_ea:$addr), !strconcat(opstr, "\t$rt, $addr"),
753          [(set RO:$rt, addr:$addr)], NoItinerary, FrmI,
754          !strconcat(opstr, "_lea")> {
755   let isCodeGenOnly = 1;
756   let DecoderMethod = "DecodeMem";
757 }
758
759 // Count Leading Ones/Zeros in Word
760 class CountLeading0<string opstr, RegisterOperand RO>:
761   InstSE<(outs RO:$rd), (ins RO:$rs), !strconcat(opstr, "\t$rd, $rs"),
762          [(set RO:$rd, (ctlz RO:$rs))], II_CLZ, FrmR, opstr>,
763   Requires<[HasBitCount, HasStdEnc]>;
764
765 class CountLeading1<string opstr, RegisterOperand RO>:
766   InstSE<(outs RO:$rd), (ins RO:$rs), !strconcat(opstr, "\t$rd, $rs"),
767          [(set RO:$rd, (ctlz (not RO:$rs)))], II_CLO, FrmR, opstr>,
768   Requires<[HasBitCount, HasStdEnc]>;
769
770
771 // Sign Extend in Register.
772 class SignExtInReg<string opstr, ValueType vt, RegisterOperand RO,
773                    InstrItinClass itin> :
774   InstSE<(outs RO:$rd), (ins RO:$rt), !strconcat(opstr, "\t$rd, $rt"),
775          [(set RO:$rd, (sext_inreg RO:$rt, vt))], itin, FrmR, opstr> {
776   let Predicates = [HasSEInReg, HasStdEnc];
777 }
778
779 // Subword Swap
780 class SubwordSwap<string opstr, RegisterOperand RO>:
781   InstSE<(outs RO:$rd), (ins RO:$rt), !strconcat(opstr, "\t$rd, $rt"), [],
782          NoItinerary, FrmR, opstr> {
783   let Predicates = [HasSwap, HasStdEnc];
784   let neverHasSideEffects = 1;
785 }
786
787 // Read Hardware
788 class ReadHardware<RegisterOperand CPURegOperand, RegisterOperand RO> :
789   InstSE<(outs CPURegOperand:$rt), (ins RO:$rd), "rdhwr\t$rt, $rd", [],
790          II_RDHWR, FrmR>;
791
792 // Ext and Ins
793 class ExtBase<string opstr, RegisterOperand RO, Operand PosOpnd,
794               SDPatternOperator Op = null_frag>:
795   InstSE<(outs RO:$rt), (ins RO:$rs, PosOpnd:$pos, size_ext:$size),
796          !strconcat(opstr, " $rt, $rs, $pos, $size"),
797          [(set RO:$rt, (Op RO:$rs, imm:$pos, imm:$size))], NoItinerary,
798          FrmR, opstr> {
799   let Predicates = [HasMips32r2, HasStdEnc];
800 }
801
802 class InsBase<string opstr, RegisterOperand RO, Operand PosOpnd,
803               SDPatternOperator Op = null_frag>:
804   InstSE<(outs RO:$rt), (ins RO:$rs, PosOpnd:$pos, size_ins:$size, RO:$src),
805          !strconcat(opstr, " $rt, $rs, $pos, $size"),
806          [(set RO:$rt, (Op RO:$rs, imm:$pos, imm:$size, RO:$src))],
807          NoItinerary, FrmR, opstr> {
808   let Predicates = [HasMips32r2, HasStdEnc];
809   let Constraints = "$src = $rt";
810 }
811
812 // Atomic instructions with 2 source operands (ATOMIC_SWAP & ATOMIC_LOAD_*).
813 class Atomic2Ops<PatFrag Op, RegisterClass DRC> :
814   PseudoSE<(outs DRC:$dst), (ins PtrRC:$ptr, DRC:$incr),
815            [(set DRC:$dst, (Op iPTR:$ptr, DRC:$incr))]>;
816
817 // Atomic Compare & Swap.
818 class AtomicCmpSwap<PatFrag Op, RegisterClass DRC> :
819   PseudoSE<(outs DRC:$dst), (ins PtrRC:$ptr, DRC:$cmp, DRC:$swap),
820            [(set DRC:$dst, (Op iPTR:$ptr, DRC:$cmp, DRC:$swap))]>;
821
822 class LLBase<string opstr, RegisterOperand RO> :
823   InstSE<(outs RO:$rt), (ins mem:$addr), !strconcat(opstr, "\t$rt, $addr"),
824          [], NoItinerary, FrmI> {
825   let DecoderMethod = "DecodeMem";
826   let mayLoad = 1;
827 }
828
829 class SCBase<string opstr, RegisterOperand RO> :
830   InstSE<(outs RO:$dst), (ins RO:$rt, mem:$addr),
831          !strconcat(opstr, "\t$rt, $addr"), [], NoItinerary, FrmI> {
832   let DecoderMethod = "DecodeMem";
833   let mayStore = 1;
834   let Constraints = "$rt = $dst";
835 }
836
837 class MFC3OP<string asmstr, RegisterOperand RO> :
838   InstSE<(outs RO:$rt, RO:$rd, uimm16:$sel), (ins),
839          !strconcat(asmstr, "\t$rt, $rd, $sel"), [], NoItinerary, FrmFR>;
840
841 class TrapBase<Instruction RealInst>
842   : PseudoSE<(outs), (ins), [(trap)], NoItinerary>,
843     PseudoInstExpansion<(RealInst 0, 0)> {
844   let isBarrier = 1;
845   let isTerminator = 1;
846   let isCodeGenOnly = 1;
847 }
848
849 //===----------------------------------------------------------------------===//
850 // Pseudo instructions
851 //===----------------------------------------------------------------------===//
852
853 // Return RA.
854 let isReturn=1, isTerminator=1, hasDelaySlot=1, isBarrier=1, hasCtrlDep=1 in
855 def RetRA : PseudoSE<(outs), (ins), [(MipsRet)]>;
856
857 let Defs = [SP], Uses = [SP], hasSideEffects = 1 in {
858 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins i32imm:$amt),
859                                   [(callseq_start timm:$amt)]>;
860 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
861                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
862 }
863
864 let usesCustomInserter = 1 in {
865   def ATOMIC_LOAD_ADD_I8   : Atomic2Ops<atomic_load_add_8, GPR32>;
866   def ATOMIC_LOAD_ADD_I16  : Atomic2Ops<atomic_load_add_16, GPR32>;
867   def ATOMIC_LOAD_ADD_I32  : Atomic2Ops<atomic_load_add_32, GPR32>;
868   def ATOMIC_LOAD_SUB_I8   : Atomic2Ops<atomic_load_sub_8, GPR32>;
869   def ATOMIC_LOAD_SUB_I16  : Atomic2Ops<atomic_load_sub_16, GPR32>;
870   def ATOMIC_LOAD_SUB_I32  : Atomic2Ops<atomic_load_sub_32, GPR32>;
871   def ATOMIC_LOAD_AND_I8   : Atomic2Ops<atomic_load_and_8, GPR32>;
872   def ATOMIC_LOAD_AND_I16  : Atomic2Ops<atomic_load_and_16, GPR32>;
873   def ATOMIC_LOAD_AND_I32  : Atomic2Ops<atomic_load_and_32, GPR32>;
874   def ATOMIC_LOAD_OR_I8    : Atomic2Ops<atomic_load_or_8, GPR32>;
875   def ATOMIC_LOAD_OR_I16   : Atomic2Ops<atomic_load_or_16, GPR32>;
876   def ATOMIC_LOAD_OR_I32   : Atomic2Ops<atomic_load_or_32, GPR32>;
877   def ATOMIC_LOAD_XOR_I8   : Atomic2Ops<atomic_load_xor_8, GPR32>;
878   def ATOMIC_LOAD_XOR_I16  : Atomic2Ops<atomic_load_xor_16, GPR32>;
879   def ATOMIC_LOAD_XOR_I32  : Atomic2Ops<atomic_load_xor_32, GPR32>;
880   def ATOMIC_LOAD_NAND_I8  : Atomic2Ops<atomic_load_nand_8, GPR32>;
881   def ATOMIC_LOAD_NAND_I16 : Atomic2Ops<atomic_load_nand_16, GPR32>;
882   def ATOMIC_LOAD_NAND_I32 : Atomic2Ops<atomic_load_nand_32, GPR32>;
883
884   def ATOMIC_SWAP_I8       : Atomic2Ops<atomic_swap_8, GPR32>;
885   def ATOMIC_SWAP_I16      : Atomic2Ops<atomic_swap_16, GPR32>;
886   def ATOMIC_SWAP_I32      : Atomic2Ops<atomic_swap_32, GPR32>;
887
888   def ATOMIC_CMP_SWAP_I8   : AtomicCmpSwap<atomic_cmp_swap_8, GPR32>;
889   def ATOMIC_CMP_SWAP_I16  : AtomicCmpSwap<atomic_cmp_swap_16, GPR32>;
890   def ATOMIC_CMP_SWAP_I32  : AtomicCmpSwap<atomic_cmp_swap_32, GPR32>;
891 }
892
893 /// Pseudo instructions for loading and storing accumulator registers.
894 let isPseudo = 1, isCodeGenOnly = 1 in {
895   def LOAD_ACC64  : Load<"", ACC64>;
896   def STORE_ACC64 : Store<"", ACC64>;
897 }
898
899 //===----------------------------------------------------------------------===//
900 // Instruction definition
901 //===----------------------------------------------------------------------===//
902 //===----------------------------------------------------------------------===//
903 // MipsI Instructions
904 //===----------------------------------------------------------------------===//
905
906 /// Arithmetic Instructions (ALU Immediate)
907 def ADDiu : MMRel, ArithLogicI<"addiu", simm16, GPR32Opnd, II_ADDIU, immSExt16,
908                                add>,
909             ADDI_FM<0x9>, IsAsCheapAsAMove;
910 def ADDi  : MMRel, ArithLogicI<"addi", simm16, GPR32Opnd>, ADDI_FM<0x8>;
911 def SLTi  : MMRel, SetCC_I<"slti", setlt, simm16, immSExt16, GPR32Opnd>,
912             SLTI_FM<0xa>;
913 def SLTiu : MMRel, SetCC_I<"sltiu", setult, simm16, immSExt16, GPR32Opnd>,
914             SLTI_FM<0xb>;
915 def ANDi  : MMRel, ArithLogicI<"andi", uimm16, GPR32Opnd, II_ANDI, immZExt16,
916                                and>,
917             ADDI_FM<0xc>;
918 def ORi   : MMRel, ArithLogicI<"ori", uimm16, GPR32Opnd, II_ORI, immZExt16,
919                                or>,
920             ADDI_FM<0xd>;
921 def XORi  : MMRel, ArithLogicI<"xori", uimm16, GPR32Opnd, II_XORI, immZExt16,
922                                xor>,
923             ADDI_FM<0xe>;
924 def LUi   : MMRel, LoadUpper<"lui", GPR32Opnd, uimm16>, LUI_FM;
925
926 /// Arithmetic Instructions (3-Operand, R-Type)
927 def ADDu  : MMRel, ArithLogicR<"addu", GPR32Opnd, 1, II_ADDU, add>,
928             ADD_FM<0, 0x21>;
929 def SUBu  : MMRel, ArithLogicR<"subu", GPR32Opnd, 0, II_SUBU, sub>,
930             ADD_FM<0, 0x23>;
931 let Defs = [HI0, LO0] in
932 def MUL   : MMRel, ArithLogicR<"mul", GPR32Opnd, 1, II_MUL, mul>,
933             ADD_FM<0x1c, 2>;
934 def ADD   : MMRel, ArithLogicR<"add", GPR32Opnd>, ADD_FM<0, 0x20>;
935 def SUB   : MMRel, ArithLogicR<"sub", GPR32Opnd>, ADD_FM<0, 0x22>;
936 def SLT   : MMRel, SetCC_R<"slt", setlt, GPR32Opnd>, ADD_FM<0, 0x2a>;
937 def SLTu  : MMRel, SetCC_R<"sltu", setult, GPR32Opnd>, ADD_FM<0, 0x2b>;
938 def AND   : MMRel, ArithLogicR<"and", GPR32Opnd, 1, II_AND, and>,
939             ADD_FM<0, 0x24>;
940 def OR    : MMRel, ArithLogicR<"or", GPR32Opnd, 1, II_OR, or>,
941             ADD_FM<0, 0x25>;
942 def XOR   : MMRel, ArithLogicR<"xor", GPR32Opnd, 1, II_XOR, xor>,
943             ADD_FM<0, 0x26>;
944 def NOR   : MMRel, LogicNOR<"nor", GPR32Opnd>, ADD_FM<0, 0x27>;
945
946 /// Shift Instructions
947 def SLL  : MMRel, shift_rotate_imm<"sll", uimm5, GPR32Opnd, II_SLL, shl,
948                                    immZExt5>, SRA_FM<0, 0>;
949 def SRL  : MMRel, shift_rotate_imm<"srl", uimm5, GPR32Opnd, II_SRL, srl,
950                                    immZExt5>, SRA_FM<2, 0>;
951 def SRA  : MMRel, shift_rotate_imm<"sra", uimm5, GPR32Opnd, II_SRA, sra,
952                                    immZExt5>, SRA_FM<3, 0>;
953 def SLLV : MMRel, shift_rotate_reg<"sllv", GPR32Opnd, II_SLLV, shl>,
954            SRLV_FM<4, 0>;
955 def SRLV : MMRel, shift_rotate_reg<"srlv", GPR32Opnd, II_SRLV, srl>,
956            SRLV_FM<6, 0>;
957 def SRAV : MMRel, shift_rotate_reg<"srav", GPR32Opnd, II_SRAV, sra>,
958            SRLV_FM<7, 0>;
959
960 // Rotate Instructions
961 let Predicates = [HasMips32r2, HasStdEnc] in {
962   def ROTR  : MMRel, shift_rotate_imm<"rotr", uimm5, GPR32Opnd, II_ROTR, rotr,
963                                       immZExt5>, SRA_FM<2, 1>;
964   def ROTRV : MMRel, shift_rotate_reg<"rotrv", GPR32Opnd, II_ROTRV, rotr>,
965               SRLV_FM<6, 1>;
966 }
967
968 /// Load and Store Instructions
969 ///  aligned
970 def LB  : Load<"lb", GPR32Opnd, sextloadi8, II_LB>, MMRel, LW_FM<0x20>;
971 def LBu : Load<"lbu", GPR32Opnd, zextloadi8, II_LBU, addrDefault>, MMRel,
972           LW_FM<0x24>;
973 def LH  : Load<"lh", GPR32Opnd, sextloadi16, II_LH, addrDefault>, MMRel,
974           LW_FM<0x21>;
975 def LHu : Load<"lhu", GPR32Opnd, zextloadi16, II_LHU>, MMRel, LW_FM<0x25>;
976 def LW  : Load<"lw", GPR32Opnd, load, II_LW, addrDefault>, MMRel,
977           LW_FM<0x23>;
978 def SB  : Store<"sb", GPR32Opnd, truncstorei8, II_SB>, MMRel, LW_FM<0x28>;
979 def SH  : Store<"sh", GPR32Opnd, truncstorei16, II_SH>, MMRel, LW_FM<0x29>;
980 def SW  : Store<"sw", GPR32Opnd, store, II_SW>, MMRel, LW_FM<0x2b>;
981
982 /// load/store left/right
983 let Predicates = [NotInMicroMips] in {
984 def LWL : LoadLeftRight<"lwl", MipsLWL, GPR32Opnd, II_LWL>, LW_FM<0x22>;
985 def LWR : LoadLeftRight<"lwr", MipsLWR, GPR32Opnd, II_LWR>, LW_FM<0x26>;
986 def SWL : StoreLeftRight<"swl", MipsSWL, GPR32Opnd, II_SWL>, LW_FM<0x2a>;
987 def SWR : StoreLeftRight<"swr", MipsSWR, GPR32Opnd, II_SWR>, LW_FM<0x2e>;
988 }
989
990 def SYNC : MMRel, SYNC_FT<"sync">, SYNC_FM;
991 def TEQ : MMRel, TEQ_FT<"teq", GPR32Opnd>, TEQ_FM<0x34>;
992 def TGE : MMRel, TEQ_FT<"tge", GPR32Opnd>, TEQ_FM<0x30>;
993 def TGEU : MMRel, TEQ_FT<"tgeu", GPR32Opnd>, TEQ_FM<0x31>;
994 def TLT : MMRel, TEQ_FT<"tlt", GPR32Opnd>, TEQ_FM<0x32>;
995 def TLTU : MMRel, TEQ_FT<"tltu", GPR32Opnd>, TEQ_FM<0x33>;
996 def TNE : MMRel, TEQ_FT<"tne", GPR32Opnd>, TEQ_FM<0x36>;
997
998 def TEQI : MMRel, TEQI_FT<"teqi", GPR32Opnd>, TEQI_FM<0xc>;
999 def TGEI : MMRel, TEQI_FT<"tgei", GPR32Opnd>, TEQI_FM<0x8>;
1000 def TGEIU : MMRel, TEQI_FT<"tgeiu", GPR32Opnd>, TEQI_FM<0x9>;
1001 def TLTI : MMRel, TEQI_FT<"tlti", GPR32Opnd>, TEQI_FM<0xa>;
1002 def TTLTIU : MMRel, TEQI_FT<"tltiu", GPR32Opnd>, TEQI_FM<0xb>;
1003 def TNEI : MMRel, TEQI_FT<"tnei", GPR32Opnd>, TEQI_FM<0xe>;
1004
1005 def BREAK : MMRel, BRK_FT<"break">, BRK_FM<0xd>;
1006 def SYSCALL : MMRel, SYS_FT<"syscall">, SYS_FM<0xc>;
1007 def TRAP : TrapBase<BREAK>;
1008
1009 def ERET : MMRel, ER_FT<"eret">, ER_FM<0x18>;
1010 def DERET : MMRel, ER_FT<"deret">, ER_FM<0x1f>;
1011
1012 def EI : MMRel, DEI_FT<"ei", GPR32Opnd>, EI_FM<1>;
1013 def DI : MMRel, DEI_FT<"di", GPR32Opnd>, EI_FM<0>;
1014
1015 def WAIT : MMRel, WAIT_FT<"wait">, WAIT_FM;
1016
1017 /// Load-linked, Store-conditional
1018 let Predicates = [NotInMicroMips] in {
1019 def LL : LLBase<"ll", GPR32Opnd>, LW_FM<0x30>;
1020 def SC : SCBase<"sc", GPR32Opnd>, LW_FM<0x38>;
1021 }
1022
1023 /// Jump and Branch Instructions
1024 def J       : MMRel, JumpFJ<jmptarget, "j", br, bb, "j">, FJ<2>,
1025               Requires<[RelocStatic, HasStdEnc]>, IsBranch;
1026 def JR      : MMRel, IndirectBranch<"jr", GPR32Opnd>, MTLO_FM<8>;
1027 def BEQ     : MMRel, CBranch<"beq", brtarget, seteq, GPR32Opnd>, BEQ_FM<4>;
1028 def BNE     : MMRel, CBranch<"bne", brtarget, setne, GPR32Opnd>, BEQ_FM<5>;
1029 def BGEZ    : MMRel, CBranchZero<"bgez", brtarget, setge, GPR32Opnd>,
1030               BGEZ_FM<1, 1>;
1031 def BGTZ    : MMRel, CBranchZero<"bgtz", brtarget, setgt, GPR32Opnd>,
1032               BGEZ_FM<7, 0>;
1033 def BLEZ    : MMRel, CBranchZero<"blez", brtarget, setle, GPR32Opnd>,
1034               BGEZ_FM<6, 0>;
1035 def BLTZ    : MMRel, CBranchZero<"bltz", brtarget, setlt, GPR32Opnd>,
1036               BGEZ_FM<1, 0>;
1037 def B       : UncondBranch<BEQ>;
1038
1039 def JAL  : MMRel, JumpLink<"jal", calltarget>, FJ<3>;
1040 def JALR : MMRel, JumpLinkReg<"jalr", GPR32Opnd>, JALR_FM;
1041 def JALX  : MMRel, JumpLink<"jalx", calltarget>, FJ<0x1D>;
1042 def JALRPseudo : JumpLinkRegPseudo<GPR32Opnd, JALR, RA>;
1043 def BGEZAL : MMRel, BGEZAL_FT<"bgezal", brtarget, GPR32Opnd>, BGEZAL_FM<0x11>;
1044 def BLTZAL : MMRel, BGEZAL_FT<"bltzal", brtarget, GPR32Opnd>, BGEZAL_FM<0x10>;
1045 def BAL_BR : BAL_BR_Pseudo<BGEZAL>;
1046 def TAILCALL : TailCall<J>;
1047 def TAILCALL_R : TailCallReg<GPR32Opnd, JR>;
1048
1049 def RET : MMRel, RetBase<"ret", GPR32Opnd>, MTLO_FM<8>;
1050
1051 // Exception handling related node and instructions.
1052 // The conversion sequence is:
1053 // ISD::EH_RETURN -> MipsISD::EH_RETURN ->
1054 // MIPSeh_return -> (stack change + indirect branch)
1055 //
1056 // MIPSeh_return takes the place of regular return instruction
1057 // but takes two arguments (V1, V0) which are used for storing
1058 // the offset and return address respectively.
1059 def SDT_MipsEHRET : SDTypeProfile<0, 2, [SDTCisInt<0>, SDTCisPtrTy<1>]>;
1060
1061 def MIPSehret : SDNode<"MipsISD::EH_RETURN", SDT_MipsEHRET,
1062                       [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
1063
1064 let Uses = [V0, V1], isTerminator = 1, isReturn = 1, isBarrier = 1 in {
1065   def MIPSeh_return32 : MipsPseudo<(outs), (ins GPR32:$spoff, GPR32:$dst),
1066                                 [(MIPSehret GPR32:$spoff, GPR32:$dst)]>;
1067   def MIPSeh_return64 : MipsPseudo<(outs), (ins GPR64:$spoff,
1068                                                 GPR64:$dst),
1069                                 [(MIPSehret GPR64:$spoff, GPR64:$dst)]>;
1070 }
1071
1072 /// Multiply and Divide Instructions.
1073 def MULT  : MMRel, Mult<"mult", II_MULT, GPR32Opnd, [HI0, LO0]>,
1074             MULT_FM<0, 0x18>;
1075 def MULTu : MMRel, Mult<"multu", II_MULTU, GPR32Opnd, [HI0, LO0]>,
1076             MULT_FM<0, 0x19>;
1077 def SDIV  : MMRel, Div<"div", II_DIV, GPR32Opnd, [HI0, LO0]>,
1078             MULT_FM<0, 0x1a>;
1079 def UDIV  : MMRel, Div<"divu", II_DIVU, GPR32Opnd, [HI0, LO0]>,
1080             MULT_FM<0, 0x1b>;
1081
1082 def MTHI : MMRel, MoveToLOHI<"mthi", GPR32Opnd, [HI0]>, MTLO_FM<0x11>;
1083 def MTLO : MMRel, MoveToLOHI<"mtlo", GPR32Opnd, [LO0]>, MTLO_FM<0x13>;
1084 def MFHI : MMRel, MoveFromLOHI<"mfhi", GPR32Opnd, AC0>, MFLO_FM<0x10>;
1085 def MFLO : MMRel, MoveFromLOHI<"mflo", GPR32Opnd, AC0>, MFLO_FM<0x12>;
1086
1087 /// Sign Ext In Register Instructions.
1088 def SEB : MMRel, SignExtInReg<"seb", i8, GPR32Opnd, II_SEB>, SEB_FM<0x10, 0x20>;
1089 def SEH : MMRel, SignExtInReg<"seh", i16, GPR32Opnd, II_SEH>, SEB_FM<0x18, 0x20>;
1090
1091 /// Count Leading
1092 def CLZ : MMRel, CountLeading0<"clz", GPR32Opnd>, CLO_FM<0x20>;
1093 def CLO : MMRel, CountLeading1<"clo", GPR32Opnd>, CLO_FM<0x21>;
1094
1095 /// Word Swap Bytes Within Halfwords
1096 def WSBH : MMRel, SubwordSwap<"wsbh", GPR32Opnd>, SEB_FM<2, 0x20>;
1097
1098 /// No operation.
1099 def NOP : PseudoSE<(outs), (ins), []>, PseudoInstExpansion<(SLL ZERO, ZERO, 0)>;
1100
1101 // FrameIndexes are legalized when they are operands from load/store
1102 // instructions. The same not happens for stack address copies, so an
1103 // add op with mem ComplexPattern is used and the stack address copy
1104 // can be matched. It's similar to Sparc LEA_ADDRi
1105 def LEA_ADDiu : MMRel, EffectiveAddress<"addiu", GPR32Opnd>, LW_FM<9>;
1106
1107 // MADD*/MSUB*
1108 def MADD  : MMRel, MArithR<"madd", II_MADD, 1>, MULT_FM<0x1c, 0>;
1109 def MADDU : MMRel, MArithR<"maddu", II_MADDU, 1>, MULT_FM<0x1c, 1>;
1110 def MSUB  : MMRel, MArithR<"msub", II_MSUB>, MULT_FM<0x1c, 4>;
1111 def MSUBU : MMRel, MArithR<"msubu", II_MSUBU>, MULT_FM<0x1c, 5>;
1112
1113 let Predicates = [HasStdEnc, NotDSP] in {
1114 def PseudoMULT  : MultDivPseudo<MULT, ACC64, GPR32Opnd, MipsMult, II_MULT>;
1115 def PseudoMULTu : MultDivPseudo<MULTu, ACC64, GPR32Opnd, MipsMultu, II_MULTU>;
1116 def PseudoMFHI : PseudoMFLOHI<GPR32, ACC64, MipsMFHI>;
1117 def PseudoMFLO : PseudoMFLOHI<GPR32, ACC64, MipsMFLO>;
1118 def PseudoMTLOHI : PseudoMTLOHI<ACC64, GPR32>;
1119 def PseudoMADD  : MAddSubPseudo<MADD, MipsMAdd, II_MADD>;
1120 def PseudoMADDU : MAddSubPseudo<MADDU, MipsMAddu, II_MADDU>;
1121 def PseudoMSUB  : MAddSubPseudo<MSUB, MipsMSub, II_MSUB>;
1122 def PseudoMSUBU : MAddSubPseudo<MSUBU, MipsMSubu, II_MSUBU>;
1123 }
1124
1125 def PseudoSDIV : MultDivPseudo<SDIV, ACC64, GPR32Opnd, MipsDivRem, II_DIV,
1126                                0, 1, 1>;
1127 def PseudoUDIV : MultDivPseudo<UDIV, ACC64, GPR32Opnd, MipsDivRemU, II_DIVU,
1128                                0, 1, 1>;
1129
1130 def RDHWR : ReadHardware<GPR32Opnd, HWRegsOpnd>, RDHWR_FM;
1131
1132 def EXT : MMRel, ExtBase<"ext", GPR32Opnd, uimm5, MipsExt>, EXT_FM<0>;
1133 def INS : MMRel, InsBase<"ins", GPR32Opnd, uimm5, MipsIns>, EXT_FM<4>;
1134
1135 /// Move Control Registers From/To CPU Registers
1136 def MFC0 : MFC3OP<"mfc0", GPR32Opnd>, MFC3OP_FM<0x10, 0>;
1137 def MTC0 : MFC3OP<"mtc0", GPR32Opnd>, MFC3OP_FM<0x10, 4>;
1138 def MFC2 : MFC3OP<"mfc2", GPR32Opnd>, MFC3OP_FM<0x12, 0>;
1139 def MTC2 : MFC3OP<"mtc2", GPR32Opnd>, MFC3OP_FM<0x12, 4>;
1140
1141 //===----------------------------------------------------------------------===//
1142 // Instruction aliases
1143 //===----------------------------------------------------------------------===//
1144 def : InstAlias<"move $dst, $src",
1145                 (ADDu GPR32Opnd:$dst, GPR32Opnd:$src,ZERO), 1>,
1146       Requires<[NotMips64]>;
1147 def : InstAlias<"bal $offset", (BGEZAL ZERO, brtarget:$offset), 0>;
1148 def : InstAlias<"addu $rs, $rt, $imm",
1149                 (ADDiu GPR32Opnd:$rs, GPR32Opnd:$rt, simm16:$imm), 0>;
1150 def : InstAlias<"add $rs, $rt, $imm",
1151                 (ADDi GPR32Opnd:$rs, GPR32Opnd:$rt, simm16:$imm), 0>;
1152 def : InstAlias<"and $rs, $rt, $imm",
1153                 (ANDi GPR32Opnd:$rs, GPR32Opnd:$rt, simm16:$imm), 0>;
1154 def : InstAlias<"j $rs", (JR GPR32Opnd:$rs), 0>;
1155 def : InstAlias<"jalr $rs", (JALR RA, GPR32Opnd:$rs), 0>;
1156 def : InstAlias<"jal $rs", (JALR RA, GPR32Opnd:$rs), 0>;
1157 def : InstAlias<"jal $rd,$rs", (JALR GPR32Opnd:$rd, GPR32Opnd:$rs), 0>;
1158 def : InstAlias<"not $rt, $rs",
1159                 (NOR GPR32Opnd:$rt, GPR32Opnd:$rs, ZERO), 0>;
1160 def : InstAlias<"neg $rt, $rs",
1161                 (SUB GPR32Opnd:$rt, ZERO, GPR32Opnd:$rs), 1>;
1162 def : InstAlias<"negu $rt, $rs",
1163                 (SUBu GPR32Opnd:$rt, ZERO, GPR32Opnd:$rs), 1>;
1164 def : InstAlias<"slt $rs, $rt, $imm",
1165                 (SLTi GPR32Opnd:$rs, GPR32Opnd:$rt, simm16:$imm), 0>;
1166 def : InstAlias<"xor $rs, $rt, $imm",
1167                 (XORi GPR32Opnd:$rs, GPR32Opnd:$rt, uimm16:$imm), 0>;
1168 def : InstAlias<"or $rs, $rt, $imm",
1169                 (ORi GPR32Opnd:$rs, GPR32Opnd:$rt, uimm16:$imm), 0>;
1170 def : InstAlias<"nop", (SLL ZERO, ZERO, 0), 1>;
1171 def : InstAlias<"mfc0 $rt, $rd", (MFC0 GPR32Opnd:$rt, GPR32Opnd:$rd, 0), 0>;
1172 def : InstAlias<"mtc0 $rt, $rd", (MTC0 GPR32Opnd:$rt, GPR32Opnd:$rd, 0), 0>;
1173 def : InstAlias<"mfc2 $rt, $rd", (MFC2 GPR32Opnd:$rt, GPR32Opnd:$rd, 0), 0>;
1174 def : InstAlias<"mtc2 $rt, $rd", (MTC2 GPR32Opnd:$rt, GPR32Opnd:$rd, 0), 0>;
1175 def : InstAlias<"b $offset", (BEQ ZERO, ZERO, brtarget:$offset), 0>;
1176 def : InstAlias<"bnez $rs,$offset",
1177                 (BNE GPR32Opnd:$rs, ZERO, brtarget:$offset), 0>;
1178 def : InstAlias<"beqz $rs,$offset",
1179                 (BEQ GPR32Opnd:$rs, ZERO, brtarget:$offset), 0>;
1180 def : InstAlias<"syscall", (SYSCALL 0), 1>;
1181
1182 def : InstAlias<"break $imm", (BREAK uimm10:$imm, 0), 1>;
1183 def : InstAlias<"break", (BREAK 0, 0), 1>;
1184 def : InstAlias<"ei", (EI ZERO), 1>;
1185 def : InstAlias<"di", (DI ZERO), 1>;
1186
1187 def  : InstAlias<"teq $rs, $rt", (TEQ GPR32Opnd:$rs, GPR32Opnd:$rt, 0), 1>;
1188 def  : InstAlias<"tge $rs, $rt", (TGE GPR32Opnd:$rs, GPR32Opnd:$rt, 0), 1>;
1189 def  : InstAlias<"tgeu $rs, $rt", (TGEU GPR32Opnd:$rs, GPR32Opnd:$rt, 0), 1>;
1190 def  : InstAlias<"tlt $rs, $rt", (TLT GPR32Opnd:$rs, GPR32Opnd:$rt, 0), 1>;
1191 def  : InstAlias<"tltu $rs, $rt", (TLTU GPR32Opnd:$rs, GPR32Opnd:$rt, 0), 1>;
1192 def  : InstAlias<"tne $rs, $rt", (TNE GPR32Opnd:$rs, GPR32Opnd:$rt, 0), 1>;
1193 def : InstAlias<"sub, $rd, $rs, $imm",
1194                 (ADDi GPR32Opnd:$rd, GPR32Opnd:$rs, InvertedImOperand:$imm)>;
1195 def : InstAlias<"subu, $rd, $rs, $imm",
1196                 (ADDiu GPR32Opnd:$rd, GPR32Opnd:$rs, InvertedImOperand:$imm)>;
1197
1198 //===----------------------------------------------------------------------===//
1199 // Assembler Pseudo Instructions
1200 //===----------------------------------------------------------------------===//
1201
1202 class LoadImm32< string instr_asm, Operand Od, RegisterOperand RO> :
1203   MipsAsmPseudoInst<(outs RO:$rt), (ins Od:$imm32),
1204                      !strconcat(instr_asm, "\t$rt, $imm32")> ;
1205 def LoadImm32Reg : LoadImm32<"li", uimm5, GPR32Opnd>;
1206
1207 class LoadAddress<string instr_asm, Operand MemOpnd, RegisterOperand RO> :
1208   MipsAsmPseudoInst<(outs RO:$rt), (ins MemOpnd:$addr),
1209                      !strconcat(instr_asm, "\t$rt, $addr")> ;
1210 def LoadAddr32Reg : LoadAddress<"la", mem, GPR32Opnd>;
1211
1212 class LoadAddressImm<string instr_asm, Operand Od, RegisterOperand RO> :
1213   MipsAsmPseudoInst<(outs RO:$rt), (ins Od:$imm32),
1214                      !strconcat(instr_asm, "\t$rt, $imm32")> ;
1215 def LoadAddr32Imm : LoadAddressImm<"la", uimm5, GPR32Opnd>;
1216
1217 //===----------------------------------------------------------------------===//
1218 //  Arbitrary patterns that map to one or more instructions
1219 //===----------------------------------------------------------------------===//
1220
1221 // Load/store pattern templates.
1222 class LoadRegImmPat<Instruction LoadInst, ValueType ValTy, PatFrag Node> :
1223   MipsPat<(ValTy (Node addrRegImm:$a)), (LoadInst addrRegImm:$a)>;
1224
1225 class StoreRegImmPat<Instruction StoreInst, ValueType ValTy> :
1226   MipsPat<(store ValTy:$v, addrRegImm:$a), (StoreInst ValTy:$v, addrRegImm:$a)>;
1227
1228 // Small immediates
1229 def : MipsPat<(i32 immSExt16:$in),
1230               (ADDiu ZERO, imm:$in)>;
1231 def : MipsPat<(i32 immZExt16:$in),
1232               (ORi ZERO, imm:$in)>;
1233 def : MipsPat<(i32 immLow16Zero:$in),
1234               (LUi (HI16 imm:$in))>;
1235
1236 // Arbitrary immediates
1237 def : MipsPat<(i32 imm:$imm),
1238           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
1239
1240 // Carry MipsPatterns
1241 def : MipsPat<(subc GPR32:$lhs, GPR32:$rhs),
1242               (SUBu GPR32:$lhs, GPR32:$rhs)>;
1243 let Predicates = [HasStdEnc, NotDSP] in {
1244   def : MipsPat<(addc GPR32:$lhs, GPR32:$rhs),
1245                 (ADDu GPR32:$lhs, GPR32:$rhs)>;
1246   def : MipsPat<(addc  GPR32:$src, immSExt16:$imm),
1247                 (ADDiu GPR32:$src, imm:$imm)>;
1248 }
1249
1250 // Call
1251 def : MipsPat<(MipsJmpLink (i32 tglobaladdr:$dst)),
1252               (JAL tglobaladdr:$dst)>;
1253 def : MipsPat<(MipsJmpLink (i32 texternalsym:$dst)),
1254               (JAL texternalsym:$dst)>;
1255 //def : MipsPat<(MipsJmpLink GPR32:$dst),
1256 //              (JALR GPR32:$dst)>;
1257
1258 // Tail call
1259 def : MipsPat<(MipsTailCall (iPTR tglobaladdr:$dst)),
1260               (TAILCALL tglobaladdr:$dst)>;
1261 def : MipsPat<(MipsTailCall (iPTR texternalsym:$dst)),
1262               (TAILCALL texternalsym:$dst)>;
1263 // hi/lo relocs
1264 def : MipsPat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
1265 def : MipsPat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
1266 def : MipsPat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
1267 def : MipsPat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
1268 def : MipsPat<(MipsHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
1269 def : MipsPat<(MipsHi texternalsym:$in), (LUi texternalsym:$in)>;
1270
1271 def : MipsPat<(MipsLo tglobaladdr:$in), (ADDiu ZERO, tglobaladdr:$in)>;
1272 def : MipsPat<(MipsLo tblockaddress:$in), (ADDiu ZERO, tblockaddress:$in)>;
1273 def : MipsPat<(MipsLo tjumptable:$in), (ADDiu ZERO, tjumptable:$in)>;
1274 def : MipsPat<(MipsLo tconstpool:$in), (ADDiu ZERO, tconstpool:$in)>;
1275 def : MipsPat<(MipsLo tglobaltlsaddr:$in), (ADDiu ZERO, tglobaltlsaddr:$in)>;
1276 def : MipsPat<(MipsLo texternalsym:$in), (ADDiu ZERO, texternalsym:$in)>;
1277
1278 def : MipsPat<(add GPR32:$hi, (MipsLo tglobaladdr:$lo)),
1279               (ADDiu GPR32:$hi, tglobaladdr:$lo)>;
1280 def : MipsPat<(add GPR32:$hi, (MipsLo tblockaddress:$lo)),
1281               (ADDiu GPR32:$hi, tblockaddress:$lo)>;
1282 def : MipsPat<(add GPR32:$hi, (MipsLo tjumptable:$lo)),
1283               (ADDiu GPR32:$hi, tjumptable:$lo)>;
1284 def : MipsPat<(add GPR32:$hi, (MipsLo tconstpool:$lo)),
1285               (ADDiu GPR32:$hi, tconstpool:$lo)>;
1286 def : MipsPat<(add GPR32:$hi, (MipsLo tglobaltlsaddr:$lo)),
1287               (ADDiu GPR32:$hi, tglobaltlsaddr:$lo)>;
1288
1289 // gp_rel relocs
1290 def : MipsPat<(add GPR32:$gp, (MipsGPRel tglobaladdr:$in)),
1291               (ADDiu GPR32:$gp, tglobaladdr:$in)>;
1292 def : MipsPat<(add GPR32:$gp, (MipsGPRel tconstpool:$in)),
1293               (ADDiu GPR32:$gp, tconstpool:$in)>;
1294
1295 // wrapper_pic
1296 class WrapperPat<SDNode node, Instruction ADDiuOp, RegisterClass RC>:
1297       MipsPat<(MipsWrapper RC:$gp, node:$in),
1298               (ADDiuOp RC:$gp, node:$in)>;
1299
1300 def : WrapperPat<tglobaladdr, ADDiu, GPR32>;
1301 def : WrapperPat<tconstpool, ADDiu, GPR32>;
1302 def : WrapperPat<texternalsym, ADDiu, GPR32>;
1303 def : WrapperPat<tblockaddress, ADDiu, GPR32>;
1304 def : WrapperPat<tjumptable, ADDiu, GPR32>;
1305 def : WrapperPat<tglobaltlsaddr, ADDiu, GPR32>;
1306
1307 // Mips does not have "not", so we expand our way
1308 def : MipsPat<(not GPR32:$in),
1309               (NOR GPR32Opnd:$in, ZERO)>;
1310
1311 // extended loads
1312 let Predicates = [HasStdEnc] in {
1313   def : MipsPat<(i32 (extloadi1  addr:$src)), (LBu addr:$src)>;
1314   def : MipsPat<(i32 (extloadi8  addr:$src)), (LBu addr:$src)>;
1315   def : MipsPat<(i32 (extloadi16 addr:$src)), (LHu addr:$src)>;
1316 }
1317
1318 // peepholes
1319 let Predicates = [HasStdEnc] in
1320 def : MipsPat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
1321
1322 // brcond patterns
1323 multiclass BrcondPats<RegisterClass RC, Instruction BEQOp, Instruction BNEOp,
1324                       Instruction SLTOp, Instruction SLTuOp, Instruction SLTiOp,
1325                       Instruction SLTiuOp, Register ZEROReg> {
1326 def : MipsPat<(brcond (i32 (setne RC:$lhs, 0)), bb:$dst),
1327               (BNEOp RC:$lhs, ZEROReg, bb:$dst)>;
1328 def : MipsPat<(brcond (i32 (seteq RC:$lhs, 0)), bb:$dst),
1329               (BEQOp RC:$lhs, ZEROReg, bb:$dst)>;
1330
1331 def : MipsPat<(brcond (i32 (setge RC:$lhs, RC:$rhs)), bb:$dst),
1332               (BEQ (SLTOp RC:$lhs, RC:$rhs), ZERO, bb:$dst)>;
1333 def : MipsPat<(brcond (i32 (setuge RC:$lhs, RC:$rhs)), bb:$dst),
1334               (BEQ (SLTuOp RC:$lhs, RC:$rhs), ZERO, bb:$dst)>;
1335 def : MipsPat<(brcond (i32 (setge RC:$lhs, immSExt16:$rhs)), bb:$dst),
1336               (BEQ (SLTiOp RC:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
1337 def : MipsPat<(brcond (i32 (setuge RC:$lhs, immSExt16:$rhs)), bb:$dst),
1338               (BEQ (SLTiuOp RC:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
1339 def : MipsPat<(brcond (i32 (setgt RC:$lhs, immSExt16Plus1:$rhs)), bb:$dst),
1340               (BEQ (SLTiOp RC:$lhs, (Plus1 imm:$rhs)), ZERO, bb:$dst)>;
1341 def : MipsPat<(brcond (i32 (setugt RC:$lhs, immSExt16Plus1:$rhs)), bb:$dst),
1342               (BEQ (SLTiuOp RC:$lhs, (Plus1 imm:$rhs)), ZERO, bb:$dst)>;
1343
1344 def : MipsPat<(brcond (i32 (setle RC:$lhs, RC:$rhs)), bb:$dst),
1345               (BEQ (SLTOp RC:$rhs, RC:$lhs), ZERO, bb:$dst)>;
1346 def : MipsPat<(brcond (i32 (setule RC:$lhs, RC:$rhs)), bb:$dst),
1347               (BEQ (SLTuOp RC:$rhs, RC:$lhs), ZERO, bb:$dst)>;
1348
1349 def : MipsPat<(brcond RC:$cond, bb:$dst),
1350               (BNEOp RC:$cond, ZEROReg, bb:$dst)>;
1351 }
1352
1353 defm : BrcondPats<GPR32, BEQ, BNE, SLT, SLTu, SLTi, SLTiu, ZERO>;
1354
1355 def : MipsPat<(brcond (i32 (setlt i32:$lhs, 1)), bb:$dst),
1356               (BLEZ i32:$lhs, bb:$dst)>;
1357 def : MipsPat<(brcond (i32 (setgt i32:$lhs, -1)), bb:$dst),
1358               (BGEZ i32:$lhs, bb:$dst)>;
1359
1360 // setcc patterns
1361 multiclass SeteqPats<RegisterClass RC, Instruction SLTiuOp, Instruction XOROp,
1362                      Instruction SLTuOp, Register ZEROReg> {
1363   def : MipsPat<(seteq RC:$lhs, 0),
1364                 (SLTiuOp RC:$lhs, 1)>;
1365   def : MipsPat<(setne RC:$lhs, 0),
1366                 (SLTuOp ZEROReg, RC:$lhs)>;
1367   def : MipsPat<(seteq RC:$lhs, RC:$rhs),
1368                 (SLTiuOp (XOROp RC:$lhs, RC:$rhs), 1)>;
1369   def : MipsPat<(setne RC:$lhs, RC:$rhs),
1370                 (SLTuOp ZEROReg, (XOROp RC:$lhs, RC:$rhs))>;
1371 }
1372
1373 multiclass SetlePats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1374   def : MipsPat<(setle RC:$lhs, RC:$rhs),
1375                 (XORi (SLTOp RC:$rhs, RC:$lhs), 1)>;
1376   def : MipsPat<(setule RC:$lhs, RC:$rhs),
1377                 (XORi (SLTuOp RC:$rhs, RC:$lhs), 1)>;
1378 }
1379
1380 multiclass SetgtPats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1381   def : MipsPat<(setgt RC:$lhs, RC:$rhs),
1382                 (SLTOp RC:$rhs, RC:$lhs)>;
1383   def : MipsPat<(setugt RC:$lhs, RC:$rhs),
1384                 (SLTuOp RC:$rhs, RC:$lhs)>;
1385 }
1386
1387 multiclass SetgePats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1388   def : MipsPat<(setge RC:$lhs, RC:$rhs),
1389                 (XORi (SLTOp RC:$lhs, RC:$rhs), 1)>;
1390   def : MipsPat<(setuge RC:$lhs, RC:$rhs),
1391                 (XORi (SLTuOp RC:$lhs, RC:$rhs), 1)>;
1392 }
1393
1394 multiclass SetgeImmPats<RegisterClass RC, Instruction SLTiOp,
1395                         Instruction SLTiuOp> {
1396   def : MipsPat<(setge RC:$lhs, immSExt16:$rhs),
1397                 (XORi (SLTiOp RC:$lhs, immSExt16:$rhs), 1)>;
1398   def : MipsPat<(setuge RC:$lhs, immSExt16:$rhs),
1399                 (XORi (SLTiuOp RC:$lhs, immSExt16:$rhs), 1)>;
1400 }
1401
1402 defm : SeteqPats<GPR32, SLTiu, XOR, SLTu, ZERO>;
1403 defm : SetlePats<GPR32, SLT, SLTu>;
1404 defm : SetgtPats<GPR32, SLT, SLTu>;
1405 defm : SetgePats<GPR32, SLT, SLTu>;
1406 defm : SetgeImmPats<GPR32, SLTi, SLTiu>;
1407
1408 // bswap pattern
1409 def : MipsPat<(bswap GPR32:$rt), (ROTR (WSBH GPR32:$rt), 16)>;
1410
1411 // Load halfword/word patterns.
1412 let AddedComplexity = 40 in {
1413   let Predicates = [HasStdEnc] in {
1414     def : LoadRegImmPat<LBu, i32, zextloadi8>;
1415     def : LoadRegImmPat<LH, i32, sextloadi16>;
1416     def : LoadRegImmPat<LW, i32, load>;
1417   }
1418 }
1419
1420 //===----------------------------------------------------------------------===//
1421 // Floating Point Support
1422 //===----------------------------------------------------------------------===//
1423
1424 include "MipsInstrFPU.td"
1425 include "Mips64InstrInfo.td"
1426 include "MipsCondMov.td"
1427
1428 //
1429 // Mips16
1430
1431 include "Mips16InstrFormats.td"
1432 include "Mips16InstrInfo.td"
1433
1434 // DSP
1435 include "MipsDSPInstrFormats.td"
1436 include "MipsDSPInstrInfo.td"
1437
1438 // MSA
1439 include "MipsMSAInstrFormats.td"
1440 include "MipsMSAInstrInfo.td"
1441
1442 // Micromips
1443 include "MicroMipsInstrFormats.td"
1444 include "MicroMipsInstrInfo.td"
1445 include "MicroMipsInstrFPU.td"