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