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