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