[mips] Make the branch nodes used in jump instructions a template parameter.
[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_MipsMAddMSub     : SDTypeProfile<0, 4,
27                                          [SDTCisVT<0, i32>, SDTCisSameAs<0, 1>,
28                                           SDTCisSameAs<1, 2>,
29                                           SDTCisSameAs<2, 3>]>;
30 def SDT_MipsDivRem       : SDTypeProfile<0, 2,
31                                          [SDTCisInt<0>,
32                                           SDTCisSameAs<0, 1>]>;
33
34 def SDT_MipsThreadPointer : SDTypeProfile<1, 0, [SDTCisPtrTy<0>]>;
35
36 def SDT_MipsDynAlloc    : SDTypeProfile<1, 1, [SDTCisVT<0, iPTR>,
37                                                SDTCisSameAs<0, 1>]>;
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, [SDNPHasChain, SDNPOptInGlue]>;
78
79 // These are target-independent nodes, but have target-specific formats.
80 def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_MipsCallSeqStart,
81                            [SDNPHasChain, SDNPSideEffect, SDNPOutGlue]>;
82 def callseq_end   : SDNode<"ISD::CALLSEQ_END", SDT_MipsCallSeqEnd,
83                            [SDNPHasChain, SDNPSideEffect,
84                             SDNPOptInGlue, SDNPOutGlue]>;
85
86 // MAdd*/MSub* nodes
87 def MipsMAdd      : SDNode<"MipsISD::MAdd", SDT_MipsMAddMSub,
88                            [SDNPOptInGlue, SDNPOutGlue]>;
89 def MipsMAddu     : SDNode<"MipsISD::MAddu", SDT_MipsMAddMSub,
90                            [SDNPOptInGlue, SDNPOutGlue]>;
91 def MipsMSub      : SDNode<"MipsISD::MSub", SDT_MipsMAddMSub,
92                            [SDNPOptInGlue, SDNPOutGlue]>;
93 def MipsMSubu     : SDNode<"MipsISD::MSubu", SDT_MipsMAddMSub,
94                            [SDNPOptInGlue, SDNPOutGlue]>;
95
96 // DivRem(u) nodes
97 def MipsDivRem    : SDNode<"MipsISD::DivRem", SDT_MipsDivRem,
98                            [SDNPOutGlue]>;
99 def MipsDivRemU   : SDNode<"MipsISD::DivRemU", SDT_MipsDivRem,
100                            [SDNPOutGlue]>;
101
102 // Target constant nodes that are not part of any isel patterns and remain
103 // unchanged can cause instructions with illegal operands to be emitted.
104 // Wrapper node patterns give the instruction selector a chance to replace
105 // target constant nodes that would otherwise remain unchanged with ADDiu
106 // nodes. Without these wrapper node patterns, the following conditional move
107 // instrucion is emitted when function cmov2 in test/CodeGen/Mips/cmov.ll is
108 // compiled:
109 //  movn  %got(d)($gp), %got(c)($gp), $4
110 // This instruction is illegal since movn can take only register operands.
111
112 def MipsWrapper    : SDNode<"MipsISD::Wrapper", SDTIntBinOp>;
113
114 // Pointer to dynamically allocated stack area.
115 def MipsDynAlloc  : SDNode<"MipsISD::DynAlloc", SDT_MipsDynAlloc,
116                            [SDNPHasChain, SDNPInGlue]>;
117
118 def MipsSync : SDNode<"MipsISD::Sync", SDT_Sync, [SDNPHasChain,SDNPSideEffect]>;
119
120 def MipsExt :  SDNode<"MipsISD::Ext", SDT_Ext>;
121 def MipsIns :  SDNode<"MipsISD::Ins", SDT_Ins>;
122
123 def MipsLWL : SDNode<"MipsISD::LWL", SDTMipsLoadLR,
124                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
125 def MipsLWR : SDNode<"MipsISD::LWR", SDTMipsLoadLR,
126                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
127 def MipsSWL : SDNode<"MipsISD::SWL", SDTStore,
128                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
129 def MipsSWR : SDNode<"MipsISD::SWR", SDTStore,
130                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
131 def MipsLDL : SDNode<"MipsISD::LDL", SDTMipsLoadLR,
132                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
133 def MipsLDR : SDNode<"MipsISD::LDR", SDTMipsLoadLR,
134                      [SDNPHasChain, SDNPMayLoad, SDNPMemOperand]>;
135 def MipsSDL : SDNode<"MipsISD::SDL", SDTStore,
136                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
137 def MipsSDR : SDNode<"MipsISD::SDR", SDTStore,
138                      [SDNPHasChain, SDNPMayStore, SDNPMemOperand]>;
139
140 //===----------------------------------------------------------------------===//
141 // Mips Instruction Predicate Definitions.
142 //===----------------------------------------------------------------------===//
143 def HasSEInReg  :     Predicate<"Subtarget.hasSEInReg()">,
144                       AssemblerPredicate<"FeatureSEInReg">;
145 def HasBitCount :     Predicate<"Subtarget.hasBitCount()">,
146                       AssemblerPredicate<"FeatureBitCount">;
147 def HasSwap     :     Predicate<"Subtarget.hasSwap()">,
148                       AssemblerPredicate<"FeatureSwap">;
149 def HasCondMov  :     Predicate<"Subtarget.hasCondMov()">,
150                       AssemblerPredicate<"FeatureCondMov">;
151 def HasMips32    :    Predicate<"Subtarget.hasMips32()">,
152                       AssemblerPredicate<"FeatureMips32">;
153 def HasMips32r2  :    Predicate<"Subtarget.hasMips32r2()">,
154                       AssemblerPredicate<"FeatureMips32r2">;
155 def HasMips64    :    Predicate<"Subtarget.hasMips64()">,
156                       AssemblerPredicate<"FeatureMips64">;
157 def HasMips32r2Or64 : Predicate<"Subtarget.hasMips32r2Or64()">,
158                       AssemblerPredicate<"FeatureMips32r2,FeatureMips64">;
159 def NotMips64    :    Predicate<"!Subtarget.hasMips64()">,
160                       AssemblerPredicate<"!FeatureMips64">;
161 def HasMips64r2  :    Predicate<"Subtarget.hasMips64r2()">,
162                       AssemblerPredicate<"FeatureMips64r2">;
163 def IsN64       :     Predicate<"Subtarget.isABI_N64()">,
164                       AssemblerPredicate<"FeatureN64">;
165 def NotN64      :     Predicate<"!Subtarget.isABI_N64()">,
166                       AssemblerPredicate<"!FeatureN64">;
167 def InMips16Mode :    Predicate<"Subtarget.inMips16Mode()">,
168                       AssemblerPredicate<"FeatureMips16">;
169 def RelocStatic :     Predicate<"TM.getRelocationModel() == Reloc::Static">,
170                       AssemblerPredicate<"FeatureMips32">;
171 def RelocPIC    :     Predicate<"TM.getRelocationModel() == Reloc::PIC_">,
172                       AssemblerPredicate<"FeatureMips32">;
173 def NoNaNsFPMath :    Predicate<"TM.Options.NoNaNsFPMath">,
174                       AssemblerPredicate<"FeatureMips32">;
175 def HasStandardEncoding : Predicate<"Subtarget.hasStandardEncoding()">,
176                           AssemblerPredicate<"!FeatureMips16">;
177
178 class MipsPat<dag pattern, dag result> : Pat<pattern, result> {
179   let Predicates = [HasStandardEncoding];
180 }
181
182 class IsBranch {
183   bit isBranch = 1;
184 }
185
186 class IsReturn {
187   bit isReturn = 1;
188 }
189
190 class IsCall {
191   bit isCall = 1;
192 }
193
194 //===----------------------------------------------------------------------===//
195 // Instruction format superclass
196 //===----------------------------------------------------------------------===//
197
198 include "MipsInstrFormats.td"
199
200 //===----------------------------------------------------------------------===//
201 // Mips Operand, Complex Patterns and Transformations Definitions.
202 //===----------------------------------------------------------------------===//
203
204 // Instruction operand types
205 def jmptarget   : Operand<OtherVT> {
206   let EncoderMethod = "getJumpTargetOpValue";
207 }
208 def brtarget    : Operand<OtherVT> {
209   let EncoderMethod = "getBranchTargetOpValue";
210   let OperandType = "OPERAND_PCREL";
211   let DecoderMethod = "DecodeBranchTarget";
212 }
213 def calltarget  : Operand<iPTR> {
214   let EncoderMethod = "getJumpTargetOpValue";
215 }
216 def calltarget64: Operand<i64>;
217 def simm16      : Operand<i32> {
218   let DecoderMethod= "DecodeSimm16";
219 }
220 def simm16_64   : Operand<i64>;
221 def shamt       : Operand<i32>;
222
223 // Unsigned Operand
224 def uimm16      : Operand<i32> {
225   let PrintMethod = "printUnsignedImm";
226 }
227
228 def MipsMemAsmOperand : AsmOperandClass {
229   let Name = "Mem";
230   let ParserMethod = "parseMemOperand";
231 }
232
233 // Address operand
234 def mem : Operand<i32> {
235   let PrintMethod = "printMemOperand";
236   let MIOperandInfo = (ops CPURegs, simm16);
237   let EncoderMethod = "getMemEncoding";
238   let ParserMatchClass = MipsMemAsmOperand;
239 }
240
241 def mem64 : Operand<i64> {
242   let PrintMethod = "printMemOperand";
243   let MIOperandInfo = (ops CPU64Regs, simm16_64);
244   let EncoderMethod = "getMemEncoding";
245   let ParserMatchClass = MipsMemAsmOperand;
246 }
247
248 def mem_ea : Operand<i32> {
249   let PrintMethod = "printMemOperandEA";
250   let MIOperandInfo = (ops CPURegs, simm16);
251   let EncoderMethod = "getMemEncoding";
252 }
253
254 def mem_ea_64 : Operand<i64> {
255   let PrintMethod = "printMemOperandEA";
256   let MIOperandInfo = (ops CPU64Regs, simm16_64);
257   let EncoderMethod = "getMemEncoding";
258 }
259
260 // size operand of ext instruction
261 def size_ext : Operand<i32> {
262   let EncoderMethod = "getSizeExtEncoding";
263   let DecoderMethod = "DecodeExtSize";
264 }
265
266 // size operand of ins instruction
267 def size_ins : Operand<i32> {
268   let EncoderMethod = "getSizeInsEncoding";
269   let DecoderMethod = "DecodeInsSize";
270 }
271
272 // Transformation Function - get the lower 16 bits.
273 def LO16 : SDNodeXForm<imm, [{
274   return getImm(N, N->getZExtValue() & 0xFFFF);
275 }]>;
276
277 // Transformation Function - get the higher 16 bits.
278 def HI16 : SDNodeXForm<imm, [{
279   return getImm(N, (N->getZExtValue() >> 16) & 0xFFFF);
280 }]>;
281
282 // Node immediate fits as 16-bit sign extended on target immediate.
283 // e.g. addi, andi
284 def immSExt16  : PatLeaf<(imm), [{ return isInt<16>(N->getSExtValue()); }]>;
285
286 // Node immediate fits as 16-bit zero extended on target immediate.
287 // The LO16 param means that only the lower 16 bits of the node
288 // immediate are caught.
289 // e.g. addiu, sltiu
290 def immZExt16  : PatLeaf<(imm), [{
291   if (N->getValueType(0) == MVT::i32)
292     return (uint32_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
293   else
294     return (uint64_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
295 }], LO16>;
296
297 // Immediate can be loaded with LUi (32-bit int with lower 16-bit cleared).
298 def immLow16Zero : PatLeaf<(imm), [{
299   int64_t Val = N->getSExtValue();
300   return isInt<32>(Val) && !(Val & 0xffff);
301 }]>;
302
303 // shamt field must fit in 5 bits.
304 def immZExt5 : ImmLeaf<i32, [{return Imm == (Imm & 0x1f);}]>;
305
306 // Mips Address Mode! SDNode frameindex could possibily be a match
307 // since load and store instructions from stack used it.
308 def addr :
309   ComplexPattern<iPTR, 2, "SelectAddr", [frameindex], [SDNPWantParent]>;
310
311 //===----------------------------------------------------------------------===//
312 // Instructions specific format
313 //===----------------------------------------------------------------------===//
314
315 /// Move Control Registers From/To CPU Registers
316 def MFC0_3OP  : MFC3OP<0x10, 0, (outs CPURegs:$rt),
317                        (ins CPURegs:$rd, uimm16:$sel),"mfc0\t$rt, $rd, $sel">;
318 def : InstAlias<"mfc0 $rt, $rd", (MFC0_3OP CPURegs:$rt, CPURegs:$rd, 0)>;
319
320 def MTC0_3OP  : MFC3OP<0x10, 4, (outs CPURegs:$rd, uimm16:$sel),
321                        (ins CPURegs:$rt),"mtc0\t$rt, $rd, $sel">;
322 def : InstAlias<"mtc0 $rt, $rd", (MTC0_3OP CPURegs:$rd, 0, CPURegs:$rt)>;
323
324 def MFC2_3OP  : MFC3OP<0x12, 0, (outs CPURegs:$rt),
325                        (ins CPURegs:$rd, uimm16:$sel),"mfc2\t$rt, $rd, $sel">;
326 def : InstAlias<"mfc2 $rt, $rd", (MFC2_3OP CPURegs:$rt, CPURegs:$rd, 0)>;
327
328 def MTC2_3OP  : MFC3OP<0x12, 4, (outs CPURegs:$rd, uimm16:$sel),
329                        (ins CPURegs:$rt),"mtc2\t$rt, $rd, $sel">;
330 def : InstAlias<"mtc2 $rt, $rd", (MTC2_3OP CPURegs:$rd, 0, CPURegs:$rt)>;
331
332 // Arithmetic and logical instructions with 3 register operands.
333 class ArithLogicR<bits<6> op, bits<6> func, string instr_asm, SDNode OpNode,
334                   InstrItinClass itin, RegisterClass RC, bit isComm = 0>:
335   FR<op, func, (outs RC:$rd), (ins RC:$rs, RC:$rt),
336      !strconcat(instr_asm, "\t$rd, $rs, $rt"),
337      [(set RC:$rd, (OpNode RC:$rs, RC:$rt))], itin> {
338   let shamt = 0;
339   let isCommutable = isComm;
340   let isReMaterializable = 1;
341 }
342
343 class ArithOverflowR<bits<6> op, bits<6> func, string instr_asm,
344                     InstrItinClass itin, RegisterClass RC, bit isComm = 0>:
345   FR<op, func, (outs RC:$rd), (ins RC:$rs, RC:$rt),
346      !strconcat(instr_asm, "\t$rd, $rs, $rt"), [], itin> {
347   let shamt = 0;
348   let isCommutable = isComm;
349 }
350
351 // Arithmetic and logical instructions with 2 register operands.
352 class ArithLogicI<bits<6> op, string instr_asm, SDNode OpNode,
353                   Operand Od, PatLeaf imm_type, RegisterClass RC> :
354   FI<op, (outs RC:$rt), (ins RC:$rs, Od:$imm16),
355      !strconcat(instr_asm, "\t$rt, $rs, $imm16"),
356      [(set RC:$rt, (OpNode RC:$rs, imm_type:$imm16))], IIAlu> {
357   let isReMaterializable = 1;
358 }
359
360 class ArithOverflowI<bits<6> op, string instr_asm, SDNode OpNode,
361                      Operand Od, PatLeaf imm_type, RegisterClass RC> :
362   FI<op, (outs RC:$rt), (ins RC:$rs, Od:$imm16),
363      !strconcat(instr_asm, "\t$rt, $rs, $imm16"), [], IIAlu>;
364
365 // Arithmetic Multiply ADD/SUB
366 let rd = 0, shamt = 0, Defs = [HI, LO], Uses = [HI, LO] in
367 class MArithR<bits<6> func, string instr_asm, SDNode op, bit isComm = 0> :
368   FR<0x1c, func, (outs), (ins CPURegs:$rs, CPURegs:$rt),
369      !strconcat(instr_asm, "\t$rs, $rt"),
370      [(op CPURegs:$rs, CPURegs:$rt, LO, HI)], IIImul> {
371   let rd = 0;
372   let shamt = 0;
373   let isCommutable = isComm;
374 }
375
376 //  Logical
377 class LogicNOR<bits<6> op, bits<6> func, string instr_asm, RegisterClass RC>:
378   FR<op, func, (outs RC:$rd), (ins RC:$rs, RC:$rt),
379      !strconcat(instr_asm, "\t$rd, $rs, $rt"),
380      [(set RC:$rd, (not (or RC:$rs, RC:$rt)))], IIAlu> {
381   let shamt = 0;
382   let isCommutable = 1;
383 }
384
385 // Shifts
386 class shift_rotate_imm<bits<6> func, bits<5> isRotate, string instr_asm,
387                        SDNode OpNode, PatFrag PF, Operand ImmOpnd,
388                        RegisterClass RC>:
389   FR<0x00, func, (outs RC:$rd), (ins RC:$rt, ImmOpnd:$shamt),
390      !strconcat(instr_asm, "\t$rd, $rt, $shamt"),
391      [(set RC:$rd, (OpNode RC:$rt, PF:$shamt))], IIAlu> {
392   let rs = isRotate;
393 }
394
395 // 32-bit shift instructions.
396 class shift_rotate_imm32<bits<6> func, bits<5> isRotate, string instr_asm,
397                          SDNode OpNode>:
398   shift_rotate_imm<func, isRotate, instr_asm, OpNode, immZExt5, shamt, CPURegs>;
399
400 class shift_rotate_reg<bits<6> func, bits<5> isRotate, string instr_asm,
401                        SDNode OpNode, RegisterClass RC>:
402   FR<0x00, func, (outs RC:$rd), (ins CPURegs:$rs, RC:$rt),
403      !strconcat(instr_asm, "\t$rd, $rt, $rs"),
404      [(set RC:$rd, (OpNode RC:$rt, CPURegs:$rs))], IIAlu> {
405   let shamt = isRotate;
406 }
407
408 // Load Upper Imediate
409 class LoadUpper<bits<6> op, string instr_asm, RegisterClass RC, Operand Imm>:
410   FI<op, (outs RC:$rt), (ins Imm:$imm16),
411      !strconcat(instr_asm, "\t$rt, $imm16"), [], IIAlu> {
412   let rs = 0;
413   let neverHasSideEffects = 1;
414   let isReMaterializable = 1;
415 }
416
417 class FMem<bits<6> op, dag outs, dag ins, string asmstr, list<dag> pattern,
418           InstrItinClass itin>: FFI<op, outs, ins, asmstr, pattern> {
419   bits<21> addr;
420   let Inst{25-21} = addr{20-16};
421   let Inst{15-0}  = addr{15-0};
422   let DecoderMethod = "DecodeMem";
423 }
424
425 // Memory Load/Store
426 let canFoldAsLoad = 1 in
427 class LoadM<bits<6> op, string instr_asm, PatFrag OpNode, RegisterClass RC,
428             Operand MemOpnd, bit Pseudo>:
429   FMem<op, (outs RC:$rt), (ins MemOpnd:$addr),
430      !strconcat(instr_asm, "\t$rt, $addr"),
431      [(set RC:$rt, (OpNode addr:$addr))], IILoad> {
432   let isPseudo = Pseudo;
433 }
434
435 class StoreM<bits<6> op, string instr_asm, PatFrag OpNode, RegisterClass RC,
436              Operand MemOpnd, bit Pseudo>:
437   FMem<op, (outs), (ins RC:$rt, MemOpnd:$addr),
438      !strconcat(instr_asm, "\t$rt, $addr"),
439      [(OpNode RC:$rt, addr:$addr)], IIStore> {
440   let isPseudo = Pseudo;
441 }
442
443 // 32-bit load.
444 multiclass LoadM32<bits<6> op, string instr_asm, PatFrag OpNode,
445                    bit Pseudo = 0> {
446   def #NAME# : LoadM<op, instr_asm, OpNode, CPURegs, mem, Pseudo>,
447                Requires<[NotN64, HasStandardEncoding]>;
448   def _P8    : LoadM<op, instr_asm, OpNode, CPURegs, mem64, Pseudo>,
449                Requires<[IsN64, HasStandardEncoding]> {
450     let DecoderNamespace = "Mips64";
451     let isCodeGenOnly = 1;
452   }
453 }
454
455 // 64-bit load.
456 multiclass LoadM64<bits<6> op, string instr_asm, PatFrag OpNode,
457                    bit Pseudo = 0> {
458   def #NAME# : LoadM<op, instr_asm, OpNode, CPU64Regs, mem, Pseudo>,
459                Requires<[NotN64, HasStandardEncoding]>;
460   def _P8    : LoadM<op, instr_asm, OpNode, CPU64Regs, mem64, Pseudo>,
461                Requires<[IsN64, HasStandardEncoding]> {
462     let DecoderNamespace = "Mips64";
463     let isCodeGenOnly = 1;
464   }
465 }
466
467 // 32-bit store.
468 multiclass StoreM32<bits<6> op, string instr_asm, PatFrag OpNode,
469                     bit Pseudo = 0> {
470   def #NAME# : StoreM<op, instr_asm, OpNode, CPURegs, mem, Pseudo>,
471                Requires<[NotN64, HasStandardEncoding]>;
472   def _P8    : StoreM<op, instr_asm, OpNode, CPURegs, mem64, Pseudo>,
473                Requires<[IsN64, HasStandardEncoding]> {
474     let DecoderNamespace = "Mips64";
475     let isCodeGenOnly = 1;
476   }
477 }
478
479 // 64-bit store.
480 multiclass StoreM64<bits<6> op, string instr_asm, PatFrag OpNode,
481                     bit Pseudo = 0> {
482   def #NAME# : StoreM<op, instr_asm, OpNode, CPU64Regs, mem, Pseudo>,
483                Requires<[NotN64, HasStandardEncoding]>;
484   def _P8    : StoreM<op, instr_asm, OpNode, CPU64Regs, mem64, Pseudo>,
485                Requires<[IsN64, HasStandardEncoding]> {
486     let DecoderNamespace = "Mips64";
487     let isCodeGenOnly = 1;
488   }
489 }
490
491 // Load/Store Left/Right
492 let canFoldAsLoad = 1 in
493 class LoadLeftRight<bits<6> op, string instr_asm, SDNode OpNode,
494                     RegisterClass RC, Operand MemOpnd> :
495   FMem<op, (outs RC:$rt), (ins MemOpnd:$addr, RC:$src),
496        !strconcat(instr_asm, "\t$rt, $addr"),
497        [(set RC:$rt, (OpNode addr:$addr, RC:$src))], IILoad> {
498   string Constraints = "$src = $rt";
499 }
500
501 class StoreLeftRight<bits<6> op, string instr_asm, SDNode OpNode,
502                      RegisterClass RC, Operand MemOpnd>:
503   FMem<op, (outs), (ins RC:$rt, MemOpnd:$addr),
504        !strconcat(instr_asm, "\t$rt, $addr"), [(OpNode RC:$rt, addr:$addr)],
505        IIStore>;
506
507 // 32-bit load left/right.
508 multiclass LoadLeftRightM32<bits<6> op, string instr_asm, SDNode OpNode> {
509   def #NAME# : LoadLeftRight<op, instr_asm, OpNode, CPURegs, mem>,
510                Requires<[NotN64, HasStandardEncoding]>;
511   def _P8    : LoadLeftRight<op, instr_asm, OpNode, CPURegs, mem64>,
512                Requires<[IsN64, HasStandardEncoding]> {
513     let DecoderNamespace = "Mips64";
514     let isCodeGenOnly = 1;
515   }
516 }
517
518 // 64-bit load left/right.
519 multiclass LoadLeftRightM64<bits<6> op, string instr_asm, SDNode OpNode> {
520   def #NAME# : LoadLeftRight<op, instr_asm, OpNode, CPU64Regs, mem>,
521                Requires<[NotN64, HasStandardEncoding]>;
522   def _P8    : LoadLeftRight<op, instr_asm, OpNode, CPU64Regs, mem64>,
523                Requires<[IsN64, HasStandardEncoding]> {
524     let DecoderNamespace = "Mips64";
525     let isCodeGenOnly = 1;
526   }
527 }
528
529 // 32-bit store left/right.
530 multiclass StoreLeftRightM32<bits<6> op, string instr_asm, SDNode OpNode> {
531   def #NAME# : StoreLeftRight<op, instr_asm, OpNode, CPURegs, mem>,
532                Requires<[NotN64, HasStandardEncoding]>;
533   def _P8    : StoreLeftRight<op, instr_asm, OpNode, CPURegs, mem64>,
534                Requires<[IsN64, HasStandardEncoding]> {
535     let DecoderNamespace = "Mips64";
536     let isCodeGenOnly = 1;
537   }
538 }
539
540 // 64-bit store left/right.
541 multiclass StoreLeftRightM64<bits<6> op, string instr_asm, SDNode OpNode> {
542   def #NAME# : StoreLeftRight<op, instr_asm, OpNode, CPU64Regs, mem>,
543                Requires<[NotN64, HasStandardEncoding]>;
544   def _P8    : StoreLeftRight<op, instr_asm, OpNode, CPU64Regs, mem64>,
545                Requires<[IsN64, HasStandardEncoding]> {
546     let DecoderNamespace = "Mips64";
547     let isCodeGenOnly = 1;
548   }
549 }
550
551 // Conditional Branch
552 class CBranch<bits<6> op, string instr_asm, PatFrag cond_op, RegisterClass RC>:
553   BranchBase<op, (outs), (ins RC:$rs, RC:$rt, brtarget:$imm16),
554              !strconcat(instr_asm, "\t$rs, $rt, $imm16"),
555              [(brcond (i32 (cond_op RC:$rs, RC:$rt)), bb:$imm16)], IIBranch> {
556   let isBranch = 1;
557   let isTerminator = 1;
558   let hasDelaySlot = 1;
559   let Defs = [AT];
560 }
561
562 class CBranchZero<bits<6> op, bits<5> _rt, string instr_asm, PatFrag cond_op,
563                   RegisterClass RC>:
564   BranchBase<op, (outs), (ins RC:$rs, brtarget:$imm16),
565              !strconcat(instr_asm, "\t$rs, $imm16"),
566              [(brcond (i32 (cond_op RC:$rs, 0)), bb:$imm16)], IIBranch> {
567   let rt = _rt;
568   let isBranch = 1;
569   let isTerminator = 1;
570   let hasDelaySlot = 1;
571   let Defs = [AT];
572 }
573
574 // SetCC
575 class SetCC_R<bits<6> op, bits<6> func, string instr_asm, PatFrag cond_op,
576               RegisterClass RC>:
577   FR<op, func, (outs CPURegs:$rd), (ins RC:$rs, RC:$rt),
578      !strconcat(instr_asm, "\t$rd, $rs, $rt"),
579      [(set CPURegs:$rd, (cond_op RC:$rs, RC:$rt))],
580      IIAlu> {
581   let shamt = 0;
582 }
583
584 class SetCC_I<bits<6> op, string instr_asm, PatFrag cond_op, Operand Od,
585               PatLeaf imm_type, RegisterClass RC>:
586   FI<op, (outs CPURegs:$rt), (ins RC:$rs, Od:$imm16),
587      !strconcat(instr_asm, "\t$rt, $rs, $imm16"),
588      [(set CPURegs:$rt, (cond_op RC:$rs, imm_type:$imm16))],
589      IIAlu>;
590
591 // Jump
592 class JumpFJ<bits<6> op, string instr_asm, SDPatternOperator operator>:
593   FJ<op, (outs), (ins jmptarget:$target), !strconcat(instr_asm, "\t$target"),
594      [(operator bb:$target)], IIBranch> {
595   let isTerminator=1;
596   let isBarrier=1;
597   let hasDelaySlot = 1;
598   let DecoderMethod = "DecodeJumpTarget";
599   let Defs = [AT];
600 }
601
602 // Unconditional branch
603 class UncondBranch<bits<6> op, string instr_asm>:
604   BranchBase<op, (outs), (ins brtarget:$imm16),
605              !strconcat(instr_asm, "\t$imm16"), [(br bb:$imm16)], IIBranch> {
606   let rs = 0;
607   let rt = 0;
608   let isBranch = 1;
609   let isTerminator = 1;
610   let isBarrier = 1;
611   let hasDelaySlot = 1;
612   let Predicates = [RelocPIC, HasStandardEncoding];
613   let Defs = [AT];
614 }
615
616 // Base class for indirect branch and return instruction classes.
617 let isTerminator=1, isBarrier=1, hasDelaySlot = 1 in
618 class JumpFR<RegisterClass RC, SDPatternOperator operator = null_frag>:
619   FR<0, 0x8, (outs), (ins RC:$rs), "jr\t$rs", [(operator RC:$rs)], IIBranch> {
620   let rt = 0;
621   let rd = 0;
622   let shamt = 0;
623 }
624
625 // Indirect branch
626 class IndirectBranch<RegisterClass RC>: JumpFR<RC, brind> {
627   let isBranch = 1;
628   let isIndirectBranch = 1;
629 }
630
631 // Return instruction
632 class RetBase<RegisterClass RC>: JumpFR<RC> {
633   let isReturn = 1;
634   let isCodeGenOnly = 1;
635   let hasCtrlDep = 1;
636   let hasExtraSrcRegAllocReq = 1;
637 }
638
639 // Jump and Link (Call)
640 let isCall=1, hasDelaySlot=1, Defs = [RA] in {
641   class JumpLink<bits<6> op, string instr_asm>:
642     FJ<op, (outs), (ins calltarget:$target),
643        !strconcat(instr_asm, "\t$target"), [(MipsJmpLink imm:$target)],
644        IIBranch> {
645        let DecoderMethod = "DecodeJumpTarget";
646        }
647
648   class JumpLinkReg<bits<6> op, bits<6> func, string instr_asm,
649                     RegisterClass RC>:
650     FR<op, func, (outs), (ins RC:$rs),
651        !strconcat(instr_asm, "\t$rs"), [(MipsJmpLink RC:$rs)], IIBranch> {
652     let rt = 0;
653     let rd = 31;
654     let shamt = 0;
655   }
656
657   class BranchLink<string instr_asm, bits<5> _rt, RegisterClass RC>:
658     FI<0x1, (outs), (ins RC:$rs, brtarget:$imm16),
659        !strconcat(instr_asm, "\t$rs, $imm16"), [], IIBranch> {
660     let rt = _rt;
661   }
662 }
663
664 // Mul, Div
665 class Mult<bits<6> func, string instr_asm, InstrItinClass itin,
666            RegisterClass RC, list<Register> DefRegs>:
667   FR<0x00, func, (outs), (ins RC:$rs, RC:$rt),
668      !strconcat(instr_asm, "\t$rs, $rt"), [], itin> {
669   let rd = 0;
670   let shamt = 0;
671   let isCommutable = 1;
672   let Defs = DefRegs;
673   let neverHasSideEffects = 1;
674 }
675
676 class Mult32<bits<6> func, string instr_asm, InstrItinClass itin>:
677   Mult<func, instr_asm, itin, CPURegs, [HI, LO]>;
678
679 class Div<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin,
680           RegisterClass RC, list<Register> DefRegs>:
681   FR<0x00, func, (outs), (ins RC:$rs, RC:$rt),
682      !strconcat(instr_asm, "\t$$zero, $rs, $rt"),
683      [(op RC:$rs, RC:$rt)], itin> {
684   let rd = 0;
685   let shamt = 0;
686   let Defs = DefRegs;
687 }
688
689 class Div32<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin>:
690   Div<op, func, instr_asm, itin, CPURegs, [HI, LO]>;
691
692 // Move from Hi/Lo
693 class MoveFromLOHI<bits<6> func, string instr_asm, RegisterClass RC,
694                    list<Register> UseRegs>:
695   FR<0x00, func, (outs RC:$rd), (ins),
696      !strconcat(instr_asm, "\t$rd"), [], IIHiLo> {
697   let rs = 0;
698   let rt = 0;
699   let shamt = 0;
700   let Uses = UseRegs;
701   let neverHasSideEffects = 1;
702 }
703
704 class MoveToLOHI<bits<6> func, string instr_asm, RegisterClass RC,
705                  list<Register> DefRegs>:
706   FR<0x00, func, (outs), (ins RC:$rs),
707      !strconcat(instr_asm, "\t$rs"), [], IIHiLo> {
708   let rt = 0;
709   let rd = 0;
710   let shamt = 0;
711   let Defs = DefRegs;
712   let neverHasSideEffects = 1;
713 }
714
715 class EffectiveAddress<bits<6> opc, string instr_asm, RegisterClass RC, Operand Mem> :
716   FMem<opc, (outs RC:$rt), (ins Mem:$addr),
717      instr_asm, [(set RC:$rt, addr:$addr)], IIAlu> {
718  let isCodeGenOnly = 1;
719 }
720
721 // Count Leading Ones/Zeros in Word
722 class CountLeading0<bits<6> func, string instr_asm, RegisterClass RC>:
723   FR<0x1c, func, (outs RC:$rd), (ins RC:$rs),
724      !strconcat(instr_asm, "\t$rd, $rs"),
725      [(set RC:$rd, (ctlz RC:$rs))], IIAlu>,
726      Requires<[HasBitCount, HasStandardEncoding]> {
727   let shamt = 0;
728   let rt = rd;
729 }
730
731 class CountLeading1<bits<6> func, string instr_asm, RegisterClass RC>:
732   FR<0x1c, func, (outs RC:$rd), (ins RC:$rs),
733      !strconcat(instr_asm, "\t$rd, $rs"),
734      [(set RC:$rd, (ctlz (not RC:$rs)))], IIAlu>,
735      Requires<[HasBitCount, HasStandardEncoding]> {
736   let shamt = 0;
737   let rt = rd;
738 }
739
740 // Sign Extend in Register.
741 class SignExtInReg<bits<5> sa, string instr_asm, ValueType vt,
742                    RegisterClass RC>:
743   FR<0x1f, 0x20, (outs RC:$rd), (ins RC:$rt),
744      !strconcat(instr_asm, "\t$rd, $rt"),
745      [(set RC:$rd, (sext_inreg RC:$rt, vt))], NoItinerary> {
746   let rs = 0;
747   let shamt = sa;
748   let Predicates = [HasSEInReg, HasStandardEncoding];
749 }
750
751 // Subword Swap
752 class SubwordSwap<bits<6> func, bits<5> sa, string instr_asm, RegisterClass RC>:
753   FR<0x1f, func, (outs RC:$rd), (ins RC:$rt),
754      !strconcat(instr_asm, "\t$rd, $rt"), [], NoItinerary> {
755   let rs = 0;
756   let shamt = sa;
757   let Predicates = [HasSwap, HasStandardEncoding];
758   let neverHasSideEffects = 1;
759 }
760
761 // Read Hardware
762 class ReadHardware<RegisterClass CPURegClass, RegisterClass HWRegClass>
763   : FR<0x1f, 0x3b, (outs CPURegClass:$rt), (ins HWRegClass:$rd),
764        "rdhwr\t$rt, $rd", [], IIAlu> {
765   let rs = 0;
766   let shamt = 0;
767 }
768
769 // Ext and Ins
770 class ExtBase<bits<6> _funct, string instr_asm, RegisterClass RC>:
771   FR<0x1f, _funct, (outs RC:$rt), (ins RC:$rs, uimm16:$pos, size_ext:$sz),
772      !strconcat(instr_asm, " $rt, $rs, $pos, $sz"),
773      [(set RC:$rt, (MipsExt RC:$rs, imm:$pos, imm:$sz))], NoItinerary> {
774   bits<5> pos;
775   bits<5> sz;
776   let rd = sz;
777   let shamt = pos;
778   let Predicates = [HasMips32r2, HasStandardEncoding];
779 }
780
781 class InsBase<bits<6> _funct, string instr_asm, RegisterClass RC>:
782   FR<0x1f, _funct, (outs RC:$rt),
783      (ins RC:$rs, uimm16:$pos, size_ins:$sz, RC:$src),
784      !strconcat(instr_asm, " $rt, $rs, $pos, $sz"),
785      [(set RC:$rt, (MipsIns RC:$rs, imm:$pos, imm:$sz, RC:$src))],
786      NoItinerary> {
787   bits<5> pos;
788   bits<5> sz;
789   let rd = sz;
790   let shamt = pos;
791   let Predicates = [HasMips32r2, HasStandardEncoding];
792   let Constraints = "$src = $rt";
793 }
794
795 // Atomic instructions with 2 source operands (ATOMIC_SWAP & ATOMIC_LOAD_*).
796 class Atomic2Ops<PatFrag Op, string Opstr, RegisterClass DRC,
797                  RegisterClass PRC> :
798   PseudoSE<(outs DRC:$dst), (ins PRC:$ptr, DRC:$incr),
799            !strconcat("atomic_", Opstr, "\t$dst, $ptr, $incr"),
800            [(set DRC:$dst, (Op PRC:$ptr, DRC:$incr))]>;
801
802 multiclass Atomic2Ops32<PatFrag Op, string Opstr> {
803   def #NAME# : Atomic2Ops<Op, Opstr, CPURegs, CPURegs>,
804                           Requires<[NotN64, HasStandardEncoding]>;
805   def _P8    : Atomic2Ops<Op, Opstr, CPURegs, CPU64Regs>,
806                           Requires<[IsN64, HasStandardEncoding]> {
807     let DecoderNamespace = "Mips64";
808   }
809 }
810
811 // Atomic Compare & Swap.
812 class AtomicCmpSwap<PatFrag Op, string Width, RegisterClass DRC,
813                     RegisterClass PRC> :
814   PseudoSE<(outs DRC:$dst), (ins PRC:$ptr, DRC:$cmp, DRC:$swap),
815            !strconcat("atomic_cmp_swap_", Width, "\t$dst, $ptr, $cmp, $swap"),
816            [(set DRC:$dst, (Op PRC:$ptr, DRC:$cmp, DRC:$swap))]>;
817
818 multiclass AtomicCmpSwap32<PatFrag Op, string Width>  {
819   def #NAME# : AtomicCmpSwap<Op, Width, CPURegs, CPURegs>,
820                              Requires<[NotN64, HasStandardEncoding]>;
821   def _P8    : AtomicCmpSwap<Op, Width, CPURegs, CPU64Regs>,
822                              Requires<[IsN64, HasStandardEncoding]> {
823     let DecoderNamespace = "Mips64";
824   }
825 }
826
827 class LLBase<bits<6> Opc, string opstring, RegisterClass RC, Operand Mem> :
828   FMem<Opc, (outs RC:$rt), (ins Mem:$addr),
829        !strconcat(opstring, "\t$rt, $addr"), [], IILoad> {
830   let mayLoad = 1;
831 }
832
833 class SCBase<bits<6> Opc, string opstring, RegisterClass RC, Operand Mem> :
834   FMem<Opc, (outs RC:$dst), (ins RC:$rt, Mem:$addr),
835        !strconcat(opstring, "\t$rt, $addr"), [], IIStore> {
836   let mayStore = 1;
837   let Constraints = "$rt = $dst";
838 }
839
840 //===----------------------------------------------------------------------===//
841 // Pseudo instructions
842 //===----------------------------------------------------------------------===//
843
844 // Return RA.
845 let isReturn=1, isTerminator=1, hasDelaySlot=1, isBarrier=1, hasCtrlDep=1 in
846 def RetRA : PseudoSE<(outs), (ins), "", [(MipsRet)]>;
847
848 let Defs = [SP], Uses = [SP], hasSideEffects = 1 in {
849 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins i32imm:$amt),
850                                   "!ADJCALLSTACKDOWN $amt",
851                                   [(callseq_start timm:$amt)]>;
852 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
853                                   "!ADJCALLSTACKUP $amt1",
854                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
855 }
856
857 // When handling PIC code the assembler needs .cpload and .cprestore
858 // directives. If the real instructions corresponding these directives
859 // are used, we have the same behavior, but get also a bunch of warnings
860 // from the assembler.
861 let neverHasSideEffects = 1 in
862 def CPRESTORE : PseudoSE<(outs), (ins i32imm:$loc, CPURegs:$gp),
863                          ".cprestore\t$loc", []>;
864
865 let usesCustomInserter = 1 in {
866   defm ATOMIC_LOAD_ADD_I8   : Atomic2Ops32<atomic_load_add_8, "load_add_8">;
867   defm ATOMIC_LOAD_ADD_I16  : Atomic2Ops32<atomic_load_add_16, "load_add_16">;
868   defm ATOMIC_LOAD_ADD_I32  : Atomic2Ops32<atomic_load_add_32, "load_add_32">;
869   defm ATOMIC_LOAD_SUB_I8   : Atomic2Ops32<atomic_load_sub_8, "load_sub_8">;
870   defm ATOMIC_LOAD_SUB_I16  : Atomic2Ops32<atomic_load_sub_16, "load_sub_16">;
871   defm ATOMIC_LOAD_SUB_I32  : Atomic2Ops32<atomic_load_sub_32, "load_sub_32">;
872   defm ATOMIC_LOAD_AND_I8   : Atomic2Ops32<atomic_load_and_8, "load_and_8">;
873   defm ATOMIC_LOAD_AND_I16  : Atomic2Ops32<atomic_load_and_16, "load_and_16">;
874   defm ATOMIC_LOAD_AND_I32  : Atomic2Ops32<atomic_load_and_32, "load_and_32">;
875   defm ATOMIC_LOAD_OR_I8    : Atomic2Ops32<atomic_load_or_8, "load_or_8">;
876   defm ATOMIC_LOAD_OR_I16   : Atomic2Ops32<atomic_load_or_16, "load_or_16">;
877   defm ATOMIC_LOAD_OR_I32   : Atomic2Ops32<atomic_load_or_32, "load_or_32">;
878   defm ATOMIC_LOAD_XOR_I8   : Atomic2Ops32<atomic_load_xor_8, "load_xor_8">;
879   defm ATOMIC_LOAD_XOR_I16  : Atomic2Ops32<atomic_load_xor_16, "load_xor_16">;
880   defm ATOMIC_LOAD_XOR_I32  : Atomic2Ops32<atomic_load_xor_32, "load_xor_32">;
881   defm ATOMIC_LOAD_NAND_I8  : Atomic2Ops32<atomic_load_nand_8, "load_nand_8">;
882   defm ATOMIC_LOAD_NAND_I16 : Atomic2Ops32<atomic_load_nand_16, "load_nand_16">;
883   defm ATOMIC_LOAD_NAND_I32 : Atomic2Ops32<atomic_load_nand_32, "load_nand_32">;
884
885   defm ATOMIC_SWAP_I8       : Atomic2Ops32<atomic_swap_8, "swap_8">;
886   defm ATOMIC_SWAP_I16      : Atomic2Ops32<atomic_swap_16, "swap_16">;
887   defm ATOMIC_SWAP_I32      : Atomic2Ops32<atomic_swap_32, "swap_32">;
888
889   defm ATOMIC_CMP_SWAP_I8   : AtomicCmpSwap32<atomic_cmp_swap_8, "8">;
890   defm ATOMIC_CMP_SWAP_I16  : AtomicCmpSwap32<atomic_cmp_swap_16, "16">;
891   defm ATOMIC_CMP_SWAP_I32  : AtomicCmpSwap32<atomic_cmp_swap_32, "32">;
892 }
893
894 //===----------------------------------------------------------------------===//
895 // Instruction definition
896 //===----------------------------------------------------------------------===//
897
898 class LoadImm32< string instr_asm, Operand Od, RegisterClass RC> :
899   MipsAsmPseudoInst<(outs RC:$rt), (ins Od:$imm32),
900                      !strconcat(instr_asm, "\t$rt, $imm32")> ;
901 def LoadImm32Reg : LoadImm32<"li", shamt,CPURegs>;
902
903 class LoadAddress<string instr_asm, Operand MemOpnd, RegisterClass RC> :
904   MipsAsmPseudoInst<(outs RC:$rt), (ins MemOpnd:$addr),
905                      !strconcat(instr_asm, "\t$rt, $addr")> ;
906 def LoadAddr32Reg : LoadAddress<"la", mem, CPURegs>;
907
908 class LoadAddressImm<string instr_asm, Operand Od, RegisterClass RC> :
909   MipsAsmPseudoInst<(outs RC:$rt), (ins Od:$imm32),
910                      !strconcat(instr_asm, "\t$rt, $imm32")> ;
911 def LoadAddr32Imm : LoadAddressImm<"la", shamt,CPURegs>;
912
913 //===----------------------------------------------------------------------===//
914 // MipsI Instructions
915 //===----------------------------------------------------------------------===//
916
917 /// Arithmetic Instructions (ALU Immediate)
918 def ADDiu   : ArithLogicI<0x09, "addiu", add, simm16, immSExt16, CPURegs>;
919 def ADDi    : ArithOverflowI<0x08, "addi", add, simm16, immSExt16, CPURegs>;
920 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16, CPURegs>;
921 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16, CPURegs>;
922 def ANDi    : ArithLogicI<0x0c, "andi", and, uimm16, immZExt16, CPURegs>;
923 def ORi     : ArithLogicI<0x0d, "ori", or, uimm16, immZExt16, CPURegs>;
924 def XORi    : ArithLogicI<0x0e, "xori", xor, uimm16, immZExt16, CPURegs>;
925 def LUi     : LoadUpper<0x0f, "lui", CPURegs, uimm16>;
926
927 /// Arithmetic Instructions (3-Operand, R-Type)
928 def ADDu    : ArithLogicR<0x00, 0x21, "addu", add, IIAlu, CPURegs, 1>;
929 def SUBu    : ArithLogicR<0x00, 0x23, "subu", sub, IIAlu, CPURegs>;
930 def ADD     : ArithOverflowR<0x00, 0x20, "add", IIAlu, CPURegs, 1>;
931 def SUB     : ArithOverflowR<0x00, 0x22, "sub", IIAlu, CPURegs>;
932 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt, CPURegs>;
933 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult, CPURegs>;
934 def AND     : ArithLogicR<0x00, 0x24, "and", and, IIAlu, CPURegs, 1>;
935 def OR      : ArithLogicR<0x00, 0x25, "or",  or, IIAlu, CPURegs, 1>;
936 def XOR     : ArithLogicR<0x00, 0x26, "xor", xor, IIAlu, CPURegs, 1>;
937 def NOR     : LogicNOR<0x00, 0x27, "nor", CPURegs>;
938
939 /// Shift Instructions
940 def SLL     : shift_rotate_imm32<0x00, 0x00, "sll", shl>;
941 def SRL     : shift_rotate_imm32<0x02, 0x00, "srl", srl>;
942 def SRA     : shift_rotate_imm32<0x03, 0x00, "sra", sra>;
943 def SLLV    : shift_rotate_reg<0x04, 0x00, "sllv", shl, CPURegs>;
944 def SRLV    : shift_rotate_reg<0x06, 0x00, "srlv", srl, CPURegs>;
945 def SRAV    : shift_rotate_reg<0x07, 0x00, "srav", sra, CPURegs>;
946
947 // Rotate Instructions
948 let Predicates = [HasMips32r2, HasStandardEncoding] in {
949     def ROTR    : shift_rotate_imm32<0x02, 0x01, "rotr", rotr>;
950     def ROTRV   : shift_rotate_reg<0x06, 0x01, "rotrv", rotr, CPURegs>;
951 }
952
953 /// Load and Store Instructions
954 ///  aligned
955 defm LB      : LoadM32<0x20, "lb",  sextloadi8>;
956 defm LBu     : LoadM32<0x24, "lbu", zextloadi8>;
957 defm LH      : LoadM32<0x21, "lh",  sextloadi16>;
958 defm LHu     : LoadM32<0x25, "lhu", zextloadi16>;
959 defm LW      : LoadM32<0x23, "lw",  load>;
960 defm SB      : StoreM32<0x28, "sb", truncstorei8>;
961 defm SH      : StoreM32<0x29, "sh", truncstorei16>;
962 defm SW      : StoreM32<0x2b, "sw", store>;
963
964 /// load/store left/right
965 defm LWL : LoadLeftRightM32<0x22, "lwl", MipsLWL>;
966 defm LWR : LoadLeftRightM32<0x26, "lwr", MipsLWR>;
967 defm SWL : StoreLeftRightM32<0x2a, "swl", MipsSWL>;
968 defm SWR : StoreLeftRightM32<0x2e, "swr", MipsSWR>;
969
970 let hasSideEffects = 1 in
971 def SYNC : InstSE<(outs), (ins i32imm:$stype), "sync $stype",
972                   [(MipsSync imm:$stype)], NoItinerary, FrmOther>
973 {
974   bits<5> stype;
975   let Opcode = 0;
976   let Inst{25-11} = 0;
977   let Inst{10-6} = stype;
978   let Inst{5-0} = 15;
979 }
980
981 /// Load-linked, Store-conditional
982 def LL    : LLBase<0x30, "ll", CPURegs, mem>,
983             Requires<[NotN64, HasStandardEncoding]>;
984 def LL_P8 : LLBase<0x30, "ll", CPURegs, mem64>,
985             Requires<[IsN64, HasStandardEncoding]> {
986   let DecoderNamespace = "Mips64";
987 }
988
989 def SC    : SCBase<0x38, "sc", CPURegs, mem>,
990             Requires<[NotN64, HasStandardEncoding]>;
991 def SC_P8 : SCBase<0x38, "sc", CPURegs, mem64>,
992             Requires<[IsN64, HasStandardEncoding]> {
993   let DecoderNamespace = "Mips64";
994 }
995
996 /// Jump and Branch Instructions
997 def J       : JumpFJ<0x02, "j", br>,
998               Requires<[RelocStatic, HasStandardEncoding]>, IsBranch;
999 def JR      : IndirectBranch<CPURegs>;
1000 def B       : UncondBranch<0x04, "b">;
1001 def BEQ     : CBranch<0x04, "beq", seteq, CPURegs>;
1002 def BNE     : CBranch<0x05, "bne", setne, CPURegs>;
1003 def BGEZ    : CBranchZero<0x01, 1, "bgez", setge, CPURegs>;
1004 def BGTZ    : CBranchZero<0x07, 0, "bgtz", setgt, CPURegs>;
1005 def BLEZ    : CBranchZero<0x06, 0, "blez", setle, CPURegs>;
1006 def BLTZ    : CBranchZero<0x01, 0, "bltz", setlt, CPURegs>;
1007
1008 let rt = 0, rs = 0, isBranch = 1, isTerminator = 1, isBarrier = 1,
1009     hasDelaySlot = 1, Defs = [RA] in
1010 def BAL_BR: FI<0x1, (outs), (ins brtarget:$imm16), "bal\t$imm16", [], IIBranch>;
1011
1012 def JAL  : JumpLink<0x03, "jal">;
1013 def JALR : JumpLinkReg<0x00, 0x09, "jalr", CPURegs>;
1014 def BGEZAL  : BranchLink<"bgezal", 0x11, CPURegs>;
1015 def BLTZAL  : BranchLink<"bltzal", 0x10, CPURegs>;
1016
1017 def RET : RetBase<CPURegs>;
1018
1019 /// Multiply and Divide Instructions.
1020 def MULT    : Mult32<0x18, "mult", IIImul>;
1021 def MULTu   : Mult32<0x19, "multu", IIImul>;
1022 def SDIV    : Div32<MipsDivRem, 0x1a, "div", IIIdiv>;
1023 def UDIV    : Div32<MipsDivRemU, 0x1b, "divu", IIIdiv>;
1024
1025 def MTHI : MoveToLOHI<0x11, "mthi", CPURegs, [HI]>;
1026 def MTLO : MoveToLOHI<0x13, "mtlo", CPURegs, [LO]>;
1027 def MFHI : MoveFromLOHI<0x10, "mfhi", CPURegs, [HI]>;
1028 def MFLO : MoveFromLOHI<0x12, "mflo", CPURegs, [LO]>;
1029
1030 /// Sign Ext In Register Instructions.
1031 def SEB : SignExtInReg<0x10, "seb", i8, CPURegs>;
1032 def SEH : SignExtInReg<0x18, "seh", i16, CPURegs>;
1033
1034 /// Count Leading
1035 def CLZ : CountLeading0<0x20, "clz", CPURegs>;
1036 def CLO : CountLeading1<0x21, "clo", CPURegs>;
1037
1038 /// Word Swap Bytes Within Halfwords
1039 def WSBH : SubwordSwap<0x20, 0x2, "wsbh", CPURegs>;
1040
1041 /// No operation
1042 let addr=0 in
1043   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
1044
1045 // FrameIndexes are legalized when they are operands from load/store
1046 // instructions. The same not happens for stack address copies, so an
1047 // add op with mem ComplexPattern is used and the stack address copy
1048 // can be matched. It's similar to Sparc LEA_ADDRi
1049 def LEA_ADDiu : EffectiveAddress<0x09,"addiu\t$rt, $addr", CPURegs, mem_ea>;
1050
1051 // DynAlloc node points to dynamically allocated stack space.
1052 // $sp is added to the list of implicitly used registers to prevent dead code
1053 // elimination from removing instructions that modify $sp.
1054 let Uses = [SP] in
1055 def DynAlloc : EffectiveAddress<0x09,"addiu\t$rt, $addr", CPURegs, mem_ea>;
1056
1057 // MADD*/MSUB*
1058 def MADD  : MArithR<0, "madd", MipsMAdd, 1>;
1059 def MADDU : MArithR<1, "maddu", MipsMAddu, 1>;
1060 def MSUB  : MArithR<4, "msub", MipsMSub>;
1061 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
1062
1063 // MUL is a assembly macro in the current used ISAs. In recent ISA's
1064 // it is a real instruction.
1065 def MUL   : ArithLogicR<0x1c, 0x02, "mul", mul, IIImul, CPURegs, 1>,
1066             Requires<[HasMips32, HasStandardEncoding]>;
1067
1068 def RDHWR : ReadHardware<CPURegs, HWRegs>;
1069
1070 def EXT : ExtBase<0, "ext", CPURegs>;
1071 def INS : InsBase<4, "ins", CPURegs>;
1072
1073 //===----------------------------------------------------------------------===//
1074 // Instruction aliases
1075 //===----------------------------------------------------------------------===//
1076 def : InstAlias<"move $dst,$src", (ADD CPURegs:$dst,CPURegs:$src,ZERO)>;
1077 def : InstAlias<"bal $offset", (BGEZAL RA,brtarget:$offset)>;
1078 def : InstAlias<"addu $rs,$rt,$imm",
1079                 (ADDiu CPURegs:$rs,CPURegs:$rt,simm16:$imm)>;
1080 def : InstAlias<"add $rs,$rt,$imm",
1081                 (ADDi CPURegs:$rs,CPURegs:$rt,simm16:$imm)>;
1082 def : InstAlias<"and $rs,$rt,$imm",
1083                 (ANDi CPURegs:$rs,CPURegs:$rt,simm16:$imm)>;
1084 def : InstAlias<"j $rs", (JR CPURegs:$rs)>;
1085 def : InstAlias<"not $rt,$rs", (NOR CPURegs:$rt,CPURegs:$rs,ZERO)>;
1086 def : InstAlias<"neg $rt,$rs", (SUB CPURegs:$rt,ZERO,CPURegs:$rs)>;
1087 def : InstAlias<"negu $rt,$rs", (SUBu CPURegs:$rt,ZERO,CPURegs:$rs)>;
1088 def : InstAlias<"slt $rs,$rt,$imm",
1089                 (SLTi CPURegs:$rs,CPURegs:$rt,simm16:$imm)>;
1090 def : InstAlias<"xor $rs,$rt,$imm",
1091                 (XORi CPURegs:$rs,CPURegs:$rt,simm16:$imm)>;
1092
1093 //===----------------------------------------------------------------------===//
1094 //  Arbitrary patterns that map to one or more instructions
1095 //===----------------------------------------------------------------------===//
1096
1097 // Small immediates
1098 def : MipsPat<(i32 immSExt16:$in),
1099               (ADDiu ZERO, imm:$in)>;
1100 def : MipsPat<(i32 immZExt16:$in),
1101               (ORi ZERO, imm:$in)>;
1102 def : MipsPat<(i32 immLow16Zero:$in),
1103               (LUi (HI16 imm:$in))>;
1104
1105 // Arbitrary immediates
1106 def : MipsPat<(i32 imm:$imm),
1107           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
1108
1109 // Carry MipsPatterns
1110 def : MipsPat<(subc CPURegs:$lhs, CPURegs:$rhs),
1111               (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
1112 def : MipsPat<(addc CPURegs:$lhs, CPURegs:$rhs),
1113               (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
1114 def : MipsPat<(addc  CPURegs:$src, immSExt16:$imm),
1115               (ADDiu CPURegs:$src, imm:$imm)>;
1116
1117 // Call
1118 def : MipsPat<(MipsJmpLink (i32 tglobaladdr:$dst)),
1119               (JAL tglobaladdr:$dst)>;
1120 def : MipsPat<(MipsJmpLink (i32 texternalsym:$dst)),
1121               (JAL texternalsym:$dst)>;
1122 //def : MipsPat<(MipsJmpLink CPURegs:$dst),
1123 //              (JALR CPURegs:$dst)>;
1124
1125 // hi/lo relocs
1126 def : MipsPat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
1127 def : MipsPat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
1128 def : MipsPat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
1129 def : MipsPat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
1130 def : MipsPat<(MipsHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
1131
1132 def : MipsPat<(MipsLo tglobaladdr:$in), (ADDiu ZERO, tglobaladdr:$in)>;
1133 def : MipsPat<(MipsLo tblockaddress:$in), (ADDiu ZERO, tblockaddress:$in)>;
1134 def : MipsPat<(MipsLo tjumptable:$in), (ADDiu ZERO, tjumptable:$in)>;
1135 def : MipsPat<(MipsLo tconstpool:$in), (ADDiu ZERO, tconstpool:$in)>;
1136 def : MipsPat<(MipsLo tglobaltlsaddr:$in), (ADDiu ZERO, tglobaltlsaddr:$in)>;
1137
1138 def : MipsPat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
1139               (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
1140 def : MipsPat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
1141               (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
1142 def : MipsPat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
1143               (ADDiu CPURegs:$hi, tjumptable:$lo)>;
1144 def : MipsPat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
1145               (ADDiu CPURegs:$hi, tconstpool:$lo)>;
1146 def : MipsPat<(add CPURegs:$hi, (MipsLo tglobaltlsaddr:$lo)),
1147               (ADDiu CPURegs:$hi, tglobaltlsaddr:$lo)>;
1148
1149 // gp_rel relocs
1150 def : MipsPat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
1151               (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
1152 def : MipsPat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
1153               (ADDiu CPURegs:$gp, tconstpool:$in)>;
1154
1155 // wrapper_pic
1156 class WrapperPat<SDNode node, Instruction ADDiuOp, RegisterClass RC>:
1157       MipsPat<(MipsWrapper RC:$gp, node:$in),
1158               (ADDiuOp RC:$gp, node:$in)>;
1159
1160 def : WrapperPat<tglobaladdr, ADDiu, CPURegs>;
1161 def : WrapperPat<tconstpool, ADDiu, CPURegs>;
1162 def : WrapperPat<texternalsym, ADDiu, CPURegs>;
1163 def : WrapperPat<tblockaddress, ADDiu, CPURegs>;
1164 def : WrapperPat<tjumptable, ADDiu, CPURegs>;
1165 def : WrapperPat<tglobaltlsaddr, ADDiu, CPURegs>;
1166
1167 // Mips does not have "not", so we expand our way
1168 def : MipsPat<(not CPURegs:$in),
1169               (NOR CPURegs:$in, ZERO)>;
1170
1171 // extended loads
1172 let Predicates = [NotN64, HasStandardEncoding] in {
1173   def : MipsPat<(i32 (extloadi1  addr:$src)), (LBu addr:$src)>;
1174   def : MipsPat<(i32 (extloadi8  addr:$src)), (LBu addr:$src)>;
1175   def : MipsPat<(i32 (extloadi16 addr:$src)), (LHu addr:$src)>;
1176 }
1177 let Predicates = [IsN64, HasStandardEncoding] in {
1178   def : MipsPat<(i32 (extloadi1  addr:$src)), (LBu_P8 addr:$src)>;
1179   def : MipsPat<(i32 (extloadi8  addr:$src)), (LBu_P8 addr:$src)>;
1180   def : MipsPat<(i32 (extloadi16 addr:$src)), (LHu_P8 addr:$src)>;
1181 }
1182
1183 // peepholes
1184 let Predicates = [NotN64, HasStandardEncoding] in {
1185   def : MipsPat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
1186 }
1187 let Predicates = [IsN64, HasStandardEncoding] in {
1188   def : MipsPat<(store (i32 0), addr:$dst), (SW_P8 ZERO, addr:$dst)>;
1189 }
1190
1191 // brcond patterns
1192 multiclass BrcondPats<RegisterClass RC, Instruction BEQOp, Instruction BNEOp,
1193                       Instruction SLTOp, Instruction SLTuOp, Instruction SLTiOp,
1194                       Instruction SLTiuOp, Register ZEROReg> {
1195 def : MipsPat<(brcond (i32 (setne RC:$lhs, 0)), bb:$dst),
1196               (BNEOp RC:$lhs, ZEROReg, bb:$dst)>;
1197 def : MipsPat<(brcond (i32 (seteq RC:$lhs, 0)), bb:$dst),
1198               (BEQOp RC:$lhs, ZEROReg, bb:$dst)>;
1199
1200 def : MipsPat<(brcond (i32 (setge RC:$lhs, RC:$rhs)), bb:$dst),
1201               (BEQ (SLTOp RC:$lhs, RC:$rhs), ZERO, bb:$dst)>;
1202 def : MipsPat<(brcond (i32 (setuge RC:$lhs, RC:$rhs)), bb:$dst),
1203               (BEQ (SLTuOp RC:$lhs, RC:$rhs), ZERO, bb:$dst)>;
1204 def : MipsPat<(brcond (i32 (setge RC:$lhs, immSExt16:$rhs)), bb:$dst),
1205               (BEQ (SLTiOp RC:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
1206 def : MipsPat<(brcond (i32 (setuge RC:$lhs, immSExt16:$rhs)), bb:$dst),
1207               (BEQ (SLTiuOp RC:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
1208
1209 def : MipsPat<(brcond (i32 (setle RC:$lhs, RC:$rhs)), bb:$dst),
1210               (BEQ (SLTOp RC:$rhs, RC:$lhs), ZERO, bb:$dst)>;
1211 def : MipsPat<(brcond (i32 (setule RC:$lhs, RC:$rhs)), bb:$dst),
1212               (BEQ (SLTuOp RC:$rhs, RC:$lhs), ZERO, bb:$dst)>;
1213
1214 def : MipsPat<(brcond RC:$cond, bb:$dst),
1215               (BNEOp RC:$cond, ZEROReg, bb:$dst)>;
1216 }
1217
1218 defm : BrcondPats<CPURegs, BEQ, BNE, SLT, SLTu, SLTi, SLTiu, ZERO>;
1219
1220 // setcc patterns
1221 multiclass SeteqPats<RegisterClass RC, Instruction SLTiuOp, Instruction XOROp,
1222                      Instruction SLTuOp, Register ZEROReg> {
1223   def : MipsPat<(seteq RC:$lhs, RC:$rhs),
1224                 (SLTiuOp (XOROp RC:$lhs, RC:$rhs), 1)>;
1225   def : MipsPat<(setne RC:$lhs, RC:$rhs),
1226                 (SLTuOp ZEROReg, (XOROp RC:$lhs, RC:$rhs))>;
1227 }
1228
1229 multiclass SetlePats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1230   def : MipsPat<(setle RC:$lhs, RC:$rhs),
1231                 (XORi (SLTOp RC:$rhs, RC:$lhs), 1)>;
1232   def : MipsPat<(setule RC:$lhs, RC:$rhs),
1233                 (XORi (SLTuOp RC:$rhs, RC:$lhs), 1)>;
1234 }
1235
1236 multiclass SetgtPats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1237   def : MipsPat<(setgt RC:$lhs, RC:$rhs),
1238                 (SLTOp RC:$rhs, RC:$lhs)>;
1239   def : MipsPat<(setugt RC:$lhs, RC:$rhs),
1240                 (SLTuOp RC:$rhs, RC:$lhs)>;
1241 }
1242
1243 multiclass SetgePats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1244   def : MipsPat<(setge RC:$lhs, RC:$rhs),
1245                 (XORi (SLTOp RC:$lhs, RC:$rhs), 1)>;
1246   def : MipsPat<(setuge RC:$lhs, RC:$rhs),
1247                 (XORi (SLTuOp RC:$lhs, RC:$rhs), 1)>;
1248 }
1249
1250 multiclass SetgeImmPats<RegisterClass RC, Instruction SLTiOp,
1251                         Instruction SLTiuOp> {
1252   def : MipsPat<(setge RC:$lhs, immSExt16:$rhs),
1253                 (XORi (SLTiOp RC:$lhs, immSExt16:$rhs), 1)>;
1254   def : MipsPat<(setuge RC:$lhs, immSExt16:$rhs),
1255                 (XORi (SLTiuOp RC:$lhs, immSExt16:$rhs), 1)>;
1256 }
1257
1258 defm : SeteqPats<CPURegs, SLTiu, XOR, SLTu, ZERO>;
1259 defm : SetlePats<CPURegs, SLT, SLTu>;
1260 defm : SetgtPats<CPURegs, SLT, SLTu>;
1261 defm : SetgePats<CPURegs, SLT, SLTu>;
1262 defm : SetgeImmPats<CPURegs, SLTi, SLTiu>;
1263
1264 // select MipsDynAlloc
1265 def : MipsPat<(MipsDynAlloc addr:$f), (DynAlloc addr:$f)>;
1266
1267 // bswap pattern
1268 def : MipsPat<(bswap CPURegs:$rt), (ROTR (WSBH CPURegs:$rt), 16)>;
1269
1270 //===----------------------------------------------------------------------===//
1271 // Floating Point Support
1272 //===----------------------------------------------------------------------===//
1273
1274 include "MipsInstrFPU.td"
1275 include "Mips64InstrInfo.td"
1276 include "MipsCondMov.td"
1277
1278 //
1279 // Mips16
1280
1281 include "Mips16InstrFormats.td"
1282 include "Mips16InstrInfo.td"
1283
1284 // DSP
1285 include "MipsDSPInstrFormats.td"
1286 include "MipsDSPInstrInfo.td"
1287