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