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