This patch addresses gp relative fixups/relocations for jump tables.
[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 MipsWrapperPIC    : SDNode<"MipsISD::WrapperPIC",  SDTIntUnaryOp>;
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 def HasBitCount : Predicate<"Subtarget.hasBitCount()">;
126 def HasSwap     : Predicate<"Subtarget.hasSwap()">;
127 def HasCondMov  : Predicate<"Subtarget.hasCondMov()">;
128 def HasMips32    : Predicate<"Subtarget.hasMips32()">;
129 def HasMips32r2  : Predicate<"Subtarget.hasMips32r2()">;
130 def HasMips64    : Predicate<"Subtarget.hasMips64()">;
131 def NotMips64    : Predicate<"!Subtarget.hasMips64()">;
132 def HasMips64r2  : Predicate<"Subtarget.hasMips64r2()">;
133 def IsN64       : Predicate<"Subtarget.isABI_N64()">;
134 def NotN64      : Predicate<"!Subtarget.isABI_N64()">;
135
136 //===----------------------------------------------------------------------===//
137 // Mips Operand, Complex Patterns and Transformations Definitions.
138 //===----------------------------------------------------------------------===//
139
140 // Instruction operand types
141 def jmptarget   : Operand<OtherVT> {
142   let EncoderMethod = "getJumpTargetOpValue";
143 }
144 def brtarget    : Operand<OtherVT> {
145   let EncoderMethod = "getBranchTargetOpValue";
146   let OperandType = "OPERAND_PCREL";
147 }
148 def calltarget  : Operand<i32>;
149 def calltarget64: Operand<i64>;
150 def simm16      : Operand<i32>;
151 def simm16_64   : Operand<i64>;
152 def shamt       : Operand<i32>;
153
154 // Unsigned Operand
155 def uimm16      : Operand<i32> {
156   let PrintMethod = "printUnsignedImm";
157 }
158
159 // Address operand
160 def mem : Operand<i32> {
161   let PrintMethod = "printMemOperand";
162   let MIOperandInfo = (ops CPURegs, simm16);
163   let EncoderMethod = "getMemEncoding";
164 }
165
166 def mem64 : Operand<i64> {
167   let PrintMethod = "printMemOperand";
168   let MIOperandInfo = (ops CPU64Regs, simm16_64);
169 }
170
171 def mem_ea : Operand<i32> {
172   let PrintMethod = "printMemOperandEA";
173   let MIOperandInfo = (ops CPURegs, simm16);
174   let EncoderMethod = "getMemEncoding";
175 }
176
177 def mem_ea_64 : Operand<i64> {
178   let PrintMethod = "printMemOperandEA";
179   let MIOperandInfo = (ops CPU64Regs, simm16_64);
180   let EncoderMethod = "getMemEncoding";
181 }
182
183 // size operand of ext instruction
184 def size_ext : Operand<i32> {
185   let EncoderMethod = "getSizeExtEncoding";
186 }
187
188 // size operand of ins instruction
189 def size_ins : Operand<i32> {
190   let EncoderMethod = "getSizeInsEncoding";
191 }
192
193 // Transformation Function - get the lower 16 bits.
194 def LO16 : SDNodeXForm<imm, [{
195   return getI32Imm((unsigned)N->getZExtValue() & 0xFFFF);
196 }]>;
197
198 // Transformation Function - get the higher 16 bits.
199 def HI16 : SDNodeXForm<imm, [{
200   return getI32Imm((unsigned)N->getZExtValue() >> 16);
201 }]>;
202
203 // Node immediate fits as 16-bit sign extended on target immediate.
204 // e.g. addi, andi
205 def immSExt16  : PatLeaf<(imm), [{ return isInt<16>(N->getSExtValue()); }]>;
206
207 // Node immediate fits as 16-bit zero extended on target immediate.
208 // The LO16 param means that only the lower 16 bits of the node
209 // immediate are caught.
210 // e.g. addiu, sltiu
211 def immZExt16  : PatLeaf<(imm), [{
212   if (N->getValueType(0) == MVT::i32)
213     return (uint32_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
214   else
215     return (uint64_t)N->getZExtValue() == (unsigned short)N->getZExtValue();
216 }], LO16>;
217
218 // shamt field must fit in 5 bits.
219 def immZExt5 : ImmLeaf<i32, [{return Imm == (Imm & 0x1f);}]>;
220
221 // Mips Address Mode! SDNode frameindex could possibily be a match
222 // since load and store instructions from stack used it.
223 def addr : ComplexPattern<iPTR, 2, "SelectAddr", [frameindex], []>;
224
225 //===----------------------------------------------------------------------===//
226 // Pattern fragment for load/store
227 //===----------------------------------------------------------------------===//
228 class UnalignedLoad<PatFrag Node> : PatFrag<(ops node:$ptr), (Node node:$ptr), [{
229   LoadSDNode *LD = cast<LoadSDNode>(N);
230   return LD->getMemoryVT().getSizeInBits()/8 > LD->getAlignment();
231 }]>;
232
233 class AlignedLoad<PatFrag Node> : PatFrag<(ops node:$ptr), (Node node:$ptr), [{
234   LoadSDNode *LD = cast<LoadSDNode>(N);
235   return LD->getMemoryVT().getSizeInBits()/8 <= LD->getAlignment();
236 }]>;
237
238 class UnalignedStore<PatFrag Node> : PatFrag<(ops node:$val, node:$ptr),
239                                              (Node node:$val, node:$ptr), [{
240   StoreSDNode *SD = cast<StoreSDNode>(N);
241   return SD->getMemoryVT().getSizeInBits()/8 > SD->getAlignment();
242 }]>;
243
244 class AlignedStore<PatFrag Node> : PatFrag<(ops node:$val, node:$ptr),
245                                            (Node node:$val, node:$ptr), [{
246   StoreSDNode *SD = cast<StoreSDNode>(N);
247   return SD->getMemoryVT().getSizeInBits()/8 <= SD->getAlignment();
248 }]>;
249
250 // Load/Store PatFrags.
251 def sextloadi16_a   : AlignedLoad<sextloadi16>;
252 def zextloadi16_a   : AlignedLoad<zextloadi16>;
253 def extloadi16_a    : AlignedLoad<extloadi16>;
254 def load_a          : AlignedLoad<load>;
255 def sextloadi32_a   : AlignedLoad<sextloadi32>;
256 def zextloadi32_a   : AlignedLoad<zextloadi32>;
257 def extloadi32_a    : AlignedLoad<extloadi32>;
258 def truncstorei16_a : AlignedStore<truncstorei16>;
259 def store_a         : AlignedStore<store>;
260 def truncstorei32_a : AlignedStore<truncstorei32>;
261 def sextloadi16_u   : UnalignedLoad<sextloadi16>;
262 def zextloadi16_u   : UnalignedLoad<zextloadi16>;
263 def extloadi16_u    : UnalignedLoad<extloadi16>;
264 def load_u          : UnalignedLoad<load>;
265 def sextloadi32_u   : UnalignedLoad<sextloadi32>;
266 def zextloadi32_u   : UnalignedLoad<zextloadi32>;
267 def extloadi32_u    : UnalignedLoad<extloadi32>;
268 def truncstorei16_u : UnalignedStore<truncstorei16>;
269 def store_u         : UnalignedStore<store>;
270 def truncstorei32_u : UnalignedStore<truncstorei32>;
271
272 //===----------------------------------------------------------------------===//
273 // Instructions specific format
274 //===----------------------------------------------------------------------===//
275
276 // Arithmetic and logical instructions with 3 register operands.
277 class ArithLogicR<bits<6> op, bits<6> func, string instr_asm, SDNode OpNode,
278                   InstrItinClass itin, RegisterClass RC, bit isComm = 0>:
279   FR<op, func, (outs RC:$rd), (ins RC:$rs, RC:$rt),
280      !strconcat(instr_asm, "\t$rd, $rs, $rt"),
281      [(set RC:$rd, (OpNode RC:$rs, RC:$rt))], itin> {
282   let shamt = 0;
283   let isCommutable = isComm;
284 }
285
286 class ArithOverflowR<bits<6> op, bits<6> func, string instr_asm,
287                     InstrItinClass itin, RegisterClass RC, bit isComm = 0>:
288   FR<op, func, (outs RC:$rd), (ins RC:$rs, RC:$rt),
289      !strconcat(instr_asm, "\t$rd, $rs, $rt"), [], itin> {
290   let shamt = 0;
291   let isCommutable = isComm;
292 }
293
294 // Arithmetic and logical instructions with 2 register operands.
295 class ArithLogicI<bits<6> op, string instr_asm, SDNode OpNode,
296                   Operand Od, PatLeaf imm_type, RegisterClass RC> :
297   FI<op, (outs RC:$rt), (ins RC:$rs, Od:$imm16),
298      !strconcat(instr_asm, "\t$rt, $rs, $imm16"),
299      [(set RC:$rt, (OpNode RC:$rs, imm_type:$imm16))], IIAlu>;
300
301 class ArithOverflowI<bits<6> op, string instr_asm, SDNode OpNode,
302                      Operand Od, PatLeaf imm_type, RegisterClass RC> :
303   FI<op, (outs RC:$rt), (ins RC:$rs, Od:$imm16),
304      !strconcat(instr_asm, "\t$rt, $rs, $imm16"), [], IIAlu>;
305
306 // Arithmetic Multiply ADD/SUB
307 let rd = 0, shamt = 0, Defs = [HI, LO], Uses = [HI, LO] in
308 class MArithR<bits<6> func, string instr_asm, SDNode op, bit isComm = 0> :
309   FR<0x1c, func, (outs), (ins CPURegs:$rs, CPURegs:$rt),
310      !strconcat(instr_asm, "\t$rs, $rt"),
311      [(op CPURegs:$rs, CPURegs:$rt, LO, HI)], IIImul> {
312   let rd = 0;
313   let shamt = 0;
314   let isCommutable = isComm;
315 }
316
317 //  Logical
318 class LogicNOR<bits<6> op, bits<6> func, string instr_asm, RegisterClass RC>:
319   FR<op, func, (outs RC:$rd), (ins RC:$rs, RC:$rt),
320      !strconcat(instr_asm, "\t$rd, $rs, $rt"),
321      [(set RC:$rd, (not (or RC:$rs, RC:$rt)))], IIAlu> {
322   let shamt = 0;
323   let isCommutable = 1;
324 }
325
326 // Shifts
327 class shift_rotate_imm<bits<6> func, bits<5> isRotate, string instr_asm,
328                        SDNode OpNode, PatFrag PF, Operand ImmOpnd,
329                        RegisterClass RC>:
330   FR<0x00, func, (outs RC:$rd), (ins RC:$rt, ImmOpnd:$shamt),
331      !strconcat(instr_asm, "\t$rd, $rt, $shamt"),
332      [(set RC:$rd, (OpNode RC:$rt, PF:$shamt))], IIAlu> {
333   let rs = isRotate;
334 }
335
336 // 32-bit shift instructions.
337 class shift_rotate_imm32<bits<6> func, bits<5> isRotate, string instr_asm,
338                          SDNode OpNode>:
339   shift_rotate_imm<func, isRotate, instr_asm, OpNode, immZExt5, shamt, CPURegs>;
340
341 class shift_rotate_reg<bits<6> func, bits<5> isRotate, string instr_asm,
342                        SDNode OpNode, RegisterClass RC>:
343   FR<0x00, func, (outs RC:$rd), (ins CPURegs:$rs, RC:$rt),
344      !strconcat(instr_asm, "\t$rd, $rt, $rs"),
345      [(set RC:$rd, (OpNode RC:$rt, CPURegs:$rs))], IIAlu> {
346   let shamt = isRotate;
347 }
348
349 // Load Upper Imediate
350 class LoadUpper<bits<6> op, string instr_asm, RegisterClass RC, Operand Imm>:
351   FI<op, (outs RC:$rt), (ins Imm:$imm16),
352      !strconcat(instr_asm, "\t$rt, $imm16"), [], IIAlu> {
353   let rs = 0;
354 }
355
356 class FMem<bits<6> op, dag outs, dag ins, string asmstr, list<dag> pattern,
357           InstrItinClass itin>: FFI<op, outs, ins, asmstr, pattern> {
358   bits<21> addr;
359   let Inst{25-21} = addr{20-16};
360   let Inst{15-0}  = addr{15-0};
361 }
362
363 // Memory Load/Store
364 let canFoldAsLoad = 1 in
365 class LoadM<bits<6> op, string instr_asm, PatFrag OpNode, RegisterClass RC,
366             Operand MemOpnd, bit Pseudo>:
367   FMem<op, (outs RC:$rt), (ins MemOpnd:$addr),
368      !strconcat(instr_asm, "\t$rt, $addr"),
369      [(set RC:$rt, (OpNode addr:$addr))], IILoad> {
370   let isPseudo = Pseudo;
371 }
372
373 class StoreM<bits<6> op, string instr_asm, PatFrag OpNode, RegisterClass RC,
374              Operand MemOpnd, bit Pseudo>:
375   FMem<op, (outs), (ins RC:$rt, MemOpnd:$addr),
376      !strconcat(instr_asm, "\t$rt, $addr"),
377      [(OpNode RC:$rt, addr:$addr)], IIStore> {
378   let isPseudo = Pseudo;
379 }
380
381 // 32-bit load.
382 multiclass LoadM32<bits<6> op, string instr_asm, PatFrag OpNode,
383                    bit Pseudo = 0> {
384   def #NAME# : LoadM<op, instr_asm, OpNode, CPURegs, mem, Pseudo>,
385                Requires<[NotN64]>;
386   def _P8    : LoadM<op, instr_asm, OpNode, CPURegs, mem64, Pseudo>,
387                Requires<[IsN64]>;
388
389
390 // 64-bit load.
391 multiclass LoadM64<bits<6> op, string instr_asm, PatFrag OpNode,
392                    bit Pseudo = 0> {
393   def #NAME# : LoadM<op, instr_asm, OpNode, CPU64Regs, mem, Pseudo>,
394                Requires<[NotN64]>;
395   def _P8    : LoadM<op, instr_asm, OpNode, CPU64Regs, mem64, Pseudo>,
396                Requires<[IsN64]>;
397
398
399 // 32-bit store.
400 multiclass StoreM32<bits<6> op, string instr_asm, PatFrag OpNode,
401                     bit Pseudo = 0> {
402   def #NAME# : StoreM<op, instr_asm, OpNode, CPURegs, mem, Pseudo>,
403                Requires<[NotN64]>;
404   def _P8    : StoreM<op, instr_asm, OpNode, CPURegs, mem64, Pseudo>,
405                Requires<[IsN64]>;
406 }
407
408 // 64-bit store.
409 multiclass StoreM64<bits<6> op, string instr_asm, PatFrag OpNode,
410                     bit Pseudo = 0> {
411   def #NAME# : StoreM<op, instr_asm, OpNode, CPU64Regs, mem, Pseudo>,
412                Requires<[NotN64]>;
413   def _P8    : StoreM<op, instr_asm, OpNode, CPU64Regs, mem64, Pseudo>,
414                Requires<[IsN64]>;
415 }
416
417 // Conditional Branch
418 class CBranch<bits<6> op, string instr_asm, PatFrag cond_op, RegisterClass RC>:
419   CBranchBase<op, (outs), (ins RC:$rs, RC:$rt, brtarget:$imm16),
420               !strconcat(instr_asm, "\t$rs, $rt, $imm16"),
421               [(brcond (i32 (cond_op RC:$rs, RC:$rt)), bb:$imm16)], IIBranch> {
422   let isBranch = 1;
423   let isTerminator = 1;
424   let hasDelaySlot = 1;
425 }
426
427 class CBranchZero<bits<6> op, bits<5> _rt, string instr_asm, PatFrag cond_op,
428                   RegisterClass RC>:
429   CBranchBase<op, (outs), (ins RC:$rs, brtarget:$imm16),
430               !strconcat(instr_asm, "\t$rs, $imm16"),
431               [(brcond (i32 (cond_op RC:$rs, 0)), bb:$imm16)], IIBranch> {
432   let rt = _rt;
433   let isBranch = 1;
434   let isTerminator = 1;
435   let hasDelaySlot = 1;
436 }
437
438 // SetCC
439 class SetCC_R<bits<6> op, bits<6> func, string instr_asm, PatFrag cond_op,
440               RegisterClass RC>:
441   FR<op, func, (outs CPURegs:$rd), (ins RC:$rs, RC:$rt),
442      !strconcat(instr_asm, "\t$rd, $rs, $rt"),
443      [(set CPURegs:$rd, (cond_op RC:$rs, RC:$rt))],
444      IIAlu> {
445   let shamt = 0;
446 }
447
448 class SetCC_I<bits<6> op, string instr_asm, PatFrag cond_op, Operand Od,
449               PatLeaf imm_type, RegisterClass RC>:
450   FI<op, (outs CPURegs:$rt), (ins RC:$rs, Od:$imm16),
451      !strconcat(instr_asm, "\t$rt, $rs, $imm16"),
452      [(set CPURegs:$rt, (cond_op RC:$rs, imm_type:$imm16))],
453      IIAlu>;
454
455 // Unconditional branch
456 let isBranch=1, isTerminator=1, isBarrier=1, hasDelaySlot = 1 in
457 class JumpFJ<bits<6> op, string instr_asm>:
458   FJ<op, (outs), (ins jmptarget:$target),
459      !strconcat(instr_asm, "\t$target"), [(br bb:$target)], IIBranch>;
460
461 let isBranch=1, isTerminator=1, isBarrier=1, rd=0, hasDelaySlot = 1,
462     isIndirectBranch = 1 in
463 class JumpFR<bits<6> op, bits<6> func, string instr_asm, RegisterClass RC>:
464   FR<op, func, (outs), (ins RC:$rs),
465      !strconcat(instr_asm, "\t$rs"), [(brind RC:$rs)], IIBranch> {
466   let rt = 0;
467   let rd = 0;
468   let shamt = 0;
469 }
470
471 // Jump and Link (Call)
472 let isCall=1, hasDelaySlot=1,
473   // All calls clobber the non-callee saved registers...
474   Defs = [AT, V0, V1, A0, A1, A2, A3, T0, T1, T2, T3, T4, T5, T6, T7, T8, T9,
475           K0, K1, D0, D1, D2, D3, D4, D5, D6, D7, D8, D9], Uses = [GP] in {
476   class JumpLink<bits<6> op, string instr_asm>:
477     FJ<op, (outs), (ins calltarget:$target, variable_ops),
478        !strconcat(instr_asm, "\t$target"), [(MipsJmpLink imm:$target)],
479        IIBranch>;
480
481   class JumpLinkReg<bits<6> op, bits<6> func, string instr_asm>:
482     FR<op, func, (outs), (ins CPURegs:$rs, variable_ops),
483        !strconcat(instr_asm, "\t$rs"), [(MipsJmpLink CPURegs:$rs)], IIBranch> {
484     let rt = 0;
485     let rd = 31;
486     let shamt = 0;
487   }
488
489   class BranchLink<string instr_asm>:
490     FI<0x1, (outs), (ins CPURegs:$rs, brtarget:$imm16, variable_ops),
491        !strconcat(instr_asm, "\t$rs, $imm16"), [], IIBranch>;
492 }
493
494 // Mul, Div
495 class Mult<bits<6> func, string instr_asm, InstrItinClass itin,
496            RegisterClass RC, list<Register> DefRegs>:
497   FR<0x00, func, (outs), (ins RC:$rs, RC:$rt),
498      !strconcat(instr_asm, "\t$rs, $rt"), [], itin> {
499   let rd = 0;
500   let shamt = 0;
501   let isCommutable = 1;
502   let Defs = DefRegs;
503 }
504
505 class Mult32<bits<6> func, string instr_asm, InstrItinClass itin>:
506   Mult<func, instr_asm, itin, CPURegs, [HI, LO]>;
507
508 class Div<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin,
509           RegisterClass RC, list<Register> DefRegs>:
510   FR<0x00, func, (outs), (ins RC:$rs, RC:$rt),
511      !strconcat(instr_asm, "\t$$zero, $rs, $rt"),
512      [(op RC:$rs, RC:$rt)], itin> {
513   let rd = 0;
514   let shamt = 0;
515   let Defs = DefRegs;
516 }
517
518 class Div32<SDNode op, bits<6> func, string instr_asm, InstrItinClass itin>:
519   Div<op, func, instr_asm, itin, CPURegs, [HI, LO]>;
520
521 // Move from Hi/Lo
522 class MoveFromLOHI<bits<6> func, string instr_asm, RegisterClass RC,
523                    list<Register> UseRegs>:
524   FR<0x00, func, (outs RC:$rd), (ins),
525      !strconcat(instr_asm, "\t$rd"), [], IIHiLo> {
526   let rs = 0;
527   let rt = 0;
528   let shamt = 0;
529   let Uses = UseRegs;
530 }
531
532 class MoveToLOHI<bits<6> func, string instr_asm, RegisterClass RC,
533                  list<Register> DefRegs>:
534   FR<0x00, func, (outs), (ins RC:$rs),
535      !strconcat(instr_asm, "\t$rs"), [], IIHiLo> {
536   let rt = 0;
537   let rd = 0;
538   let shamt = 0;
539   let Defs = DefRegs;
540 }
541
542 class EffectiveAddress<string instr_asm, RegisterClass RC, Operand Mem> :
543   FMem<0x09, (outs RC:$rt), (ins Mem:$addr),
544      instr_asm, [(set RC:$rt, addr:$addr)], IIAlu>;
545
546 // Count Leading Ones/Zeros in Word
547 class CountLeading0<bits<6> func, string instr_asm, RegisterClass RC>:
548   FR<0x1c, func, (outs RC:$rd), (ins RC:$rs),
549      !strconcat(instr_asm, "\t$rd, $rs"),
550      [(set RC:$rd, (ctlz RC:$rs))], IIAlu>,
551      Requires<[HasBitCount]> {
552   let shamt = 0;
553   let rt = rd;
554 }
555
556 class CountLeading1<bits<6> func, string instr_asm, RegisterClass RC>:
557   FR<0x1c, func, (outs RC:$rd), (ins RC:$rs),
558      !strconcat(instr_asm, "\t$rd, $rs"),
559      [(set RC:$rd, (ctlz (not RC:$rs)))], IIAlu>,
560      Requires<[HasBitCount]> {
561   let shamt = 0;
562   let rt = rd;
563 }
564
565 // Sign Extend in Register.
566 class SignExtInReg<bits<5> sa, string instr_asm, ValueType vt>:
567   FR<0x1f, 0x20, (outs CPURegs:$rd), (ins CPURegs:$rt),
568      !strconcat(instr_asm, "\t$rd, $rt"),
569      [(set CPURegs:$rd, (sext_inreg CPURegs:$rt, vt))], NoItinerary> {
570   let rs = 0;
571   let shamt = sa;
572   let Predicates = [HasSEInReg];
573 }
574
575 // Byte Swap
576 class ByteSwap<bits<6> func, bits<5> sa, string instr_asm>:
577   FR<0x1f, func, (outs CPURegs:$rd), (ins CPURegs:$rt),
578      !strconcat(instr_asm, "\t$rd, $rt"),
579      [(set CPURegs:$rd, (bswap CPURegs:$rt))], NoItinerary> {
580   let rs = 0;
581   let shamt = sa;
582   let Predicates = [HasSwap];
583 }
584
585 // Read Hardware
586 class ReadHardware: FR<0x1f, 0x3b, (outs CPURegs:$rt), (ins HWRegs:$rd),
587     "rdhwr\t$rt, $rd", [], IIAlu> {
588   let rs = 0;
589   let shamt = 0;
590 }
591
592 // Ext and Ins
593 class ExtIns<bits<6> _funct, string instr_asm, dag outs, dag ins,
594              list<dag> pattern, InstrItinClass itin>:
595   FR<0x1f, _funct, outs, ins, !strconcat(instr_asm, " $rt, $rs, $pos, $sz"),
596      pattern, itin>, Requires<[HasMips32r2]> {
597   bits<5> pos;
598   bits<5> sz;
599   let rd = sz;
600   let shamt = pos;
601 }
602
603 // Atomic instructions with 2 source operands (ATOMIC_SWAP & ATOMIC_LOAD_*).
604 class Atomic2Ops<PatFrag Op, string Opstr, RegisterClass DRC,
605                  RegisterClass PRC> :
606   MipsPseudo<(outs DRC:$dst), (ins PRC:$ptr, DRC:$incr),
607              !strconcat("atomic_", Opstr, "\t$dst, $ptr, $incr"),
608              [(set DRC:$dst, (Op PRC:$ptr, DRC:$incr))]>;
609
610 multiclass Atomic2Ops32<PatFrag Op, string Opstr> {
611   def #NAME# : Atomic2Ops<Op, Opstr, CPURegs, CPURegs>, Requires<[NotN64]>;
612   def _P8    : Atomic2Ops<Op, Opstr, CPURegs, CPU64Regs>, Requires<[IsN64]>;
613 }
614
615 // Atomic Compare & Swap.
616 class AtomicCmpSwap<PatFrag Op, string Width, RegisterClass DRC,
617                     RegisterClass PRC> :
618   MipsPseudo<(outs DRC:$dst), (ins PRC:$ptr, DRC:$cmp, DRC:$swap),
619              !strconcat("atomic_cmp_swap_", Width, "\t$dst, $ptr, $cmp, $swap"),
620              [(set DRC:$dst, (Op PRC:$ptr, DRC:$cmp, DRC:$swap))]>;
621
622 multiclass AtomicCmpSwap32<PatFrag Op, string Width>  {
623   def #NAME# : AtomicCmpSwap<Op, Width, CPURegs, CPURegs>, Requires<[NotN64]>;
624   def _P8    : AtomicCmpSwap<Op, Width, CPURegs, CPU64Regs>, Requires<[IsN64]>;
625 }
626
627 class LLBase<bits<6> Opc, string opstring, RegisterClass RC, Operand Mem> :
628   FMem<Opc, (outs RC:$rt), (ins Mem:$addr),
629        !strconcat(opstring, "\t$rt, $addr"), [], IILoad> {
630   let mayLoad = 1;
631 }
632
633 class SCBase<bits<6> Opc, string opstring, RegisterClass RC, Operand Mem> :
634   FMem<Opc, (outs RC:$dst), (ins RC:$rt, Mem:$addr),
635        !strconcat(opstring, "\t$rt, $addr"), [], IIStore> {
636   let mayStore = 1;
637   let Constraints = "$rt = $dst";
638 }
639
640 //===----------------------------------------------------------------------===//
641 // Pseudo instructions
642 //===----------------------------------------------------------------------===//
643
644 // As stack alignment is always done with addiu, we need a 16-bit immediate
645 let Defs = [SP], Uses = [SP] in {
646 def ADJCALLSTACKDOWN : MipsPseudo<(outs), (ins uimm16:$amt),
647                                   "!ADJCALLSTACKDOWN $amt",
648                                   [(callseq_start timm:$amt)]>;
649 def ADJCALLSTACKUP   : MipsPseudo<(outs), (ins uimm16:$amt1, uimm16:$amt2),
650                                   "!ADJCALLSTACKUP $amt1",
651                                   [(callseq_end timm:$amt1, timm:$amt2)]>;
652 }
653
654 // Some assembly macros need to avoid pseudoinstructions and assembler
655 // automatic reodering, we should reorder ourselves.
656 def MACRO     : MipsPseudo<(outs), (ins), ".set\tmacro",     []>;
657 def REORDER   : MipsPseudo<(outs), (ins), ".set\treorder",   []>;
658 def NOMACRO   : MipsPseudo<(outs), (ins), ".set\tnomacro",   []>;
659 def NOREORDER : MipsPseudo<(outs), (ins), ".set\tnoreorder", []>;
660
661 // These macros are inserted to prevent GAS from complaining
662 // when using the AT register.
663 def NOAT      : MipsPseudo<(outs), (ins), ".set\tnoat", []>;
664 def ATMACRO   : MipsPseudo<(outs), (ins), ".set\tat", []>;
665
666 // When handling PIC code the assembler needs .cpload and .cprestore
667 // directives. If the real instructions corresponding these directives
668 // are used, we have the same behavior, but get also a bunch of warnings
669 // from the assembler.
670 def CPLOAD : MipsPseudo<(outs), (ins CPURegs:$picreg), ".cpload\t$picreg", []>;
671 def CPRESTORE : MipsPseudo<(outs), (ins i32imm:$loc), ".cprestore\t$loc", []>;
672
673 let usesCustomInserter = 1 in {
674   defm ATOMIC_LOAD_ADD_I8   : Atomic2Ops32<atomic_load_add_8, "load_add_8">;
675   defm ATOMIC_LOAD_ADD_I16  : Atomic2Ops32<atomic_load_add_16, "load_add_16">;
676   defm ATOMIC_LOAD_ADD_I32  : Atomic2Ops32<atomic_load_add_32, "load_add_32">;
677   defm ATOMIC_LOAD_SUB_I8   : Atomic2Ops32<atomic_load_sub_8, "load_sub_8">;
678   defm ATOMIC_LOAD_SUB_I16  : Atomic2Ops32<atomic_load_sub_16, "load_sub_16">;
679   defm ATOMIC_LOAD_SUB_I32  : Atomic2Ops32<atomic_load_sub_32, "load_sub_32">;
680   defm ATOMIC_LOAD_AND_I8   : Atomic2Ops32<atomic_load_and_8, "load_and_8">;
681   defm ATOMIC_LOAD_AND_I16  : Atomic2Ops32<atomic_load_and_16, "load_and_16">;
682   defm ATOMIC_LOAD_AND_I32  : Atomic2Ops32<atomic_load_and_32, "load_and_32">;
683   defm ATOMIC_LOAD_OR_I8    : Atomic2Ops32<atomic_load_or_8, "load_or_8">;
684   defm ATOMIC_LOAD_OR_I16   : Atomic2Ops32<atomic_load_or_16, "load_or_16">;
685   defm ATOMIC_LOAD_OR_I32   : Atomic2Ops32<atomic_load_or_32, "load_or_32">;
686   defm ATOMIC_LOAD_XOR_I8   : Atomic2Ops32<atomic_load_xor_8, "load_xor_8">;
687   defm ATOMIC_LOAD_XOR_I16  : Atomic2Ops32<atomic_load_xor_16, "load_xor_16">;
688   defm ATOMIC_LOAD_XOR_I32  : Atomic2Ops32<atomic_load_xor_32, "load_xor_32">;
689   defm ATOMIC_LOAD_NAND_I8  : Atomic2Ops32<atomic_load_nand_8, "load_nand_8">;
690   defm ATOMIC_LOAD_NAND_I16 : Atomic2Ops32<atomic_load_nand_16, "load_nand_16">;
691   defm ATOMIC_LOAD_NAND_I32 : Atomic2Ops32<atomic_load_nand_32, "load_nand_32">;
692
693   defm ATOMIC_SWAP_I8       : Atomic2Ops32<atomic_swap_8, "swap_8">;
694   defm ATOMIC_SWAP_I16      : Atomic2Ops32<atomic_swap_16, "swap_16">;
695   defm ATOMIC_SWAP_I32      : Atomic2Ops32<atomic_swap_32, "swap_32">;
696
697   defm ATOMIC_CMP_SWAP_I8   : AtomicCmpSwap32<atomic_cmp_swap_8, "8">;
698   defm ATOMIC_CMP_SWAP_I16  : AtomicCmpSwap32<atomic_cmp_swap_16, "16">;
699   defm ATOMIC_CMP_SWAP_I32  : AtomicCmpSwap32<atomic_cmp_swap_32, "32">;
700 }
701
702 //===----------------------------------------------------------------------===//
703 // Instruction definition
704 //===----------------------------------------------------------------------===//
705
706 //===----------------------------------------------------------------------===//
707 // MipsI Instructions
708 //===----------------------------------------------------------------------===//
709
710 /// Arithmetic Instructions (ALU Immediate)
711 def ADDiu   : ArithLogicI<0x09, "addiu", add, simm16, immSExt16, CPURegs>;
712 def ADDi    : ArithOverflowI<0x08, "addi", add, simm16, immSExt16, CPURegs>;
713 def SLTi    : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16, CPURegs>;
714 def SLTiu   : SetCC_I<0x0b, "sltiu", setult, simm16, immSExt16, CPURegs>;
715 def ANDi    : ArithLogicI<0x0c, "andi", and, uimm16, immZExt16, CPURegs>;
716 def ORi     : ArithLogicI<0x0d, "ori", or, uimm16, immZExt16, CPURegs>;
717 def XORi    : ArithLogicI<0x0e, "xori", xor, uimm16, immZExt16, CPURegs>;
718 def LUi     : LoadUpper<0x0f, "lui", CPURegs, uimm16>;
719
720 /// Arithmetic Instructions (3-Operand, R-Type)
721 def ADDu    : ArithLogicR<0x00, 0x21, "addu", add, IIAlu, CPURegs, 1>;
722 def SUBu    : ArithLogicR<0x00, 0x23, "subu", sub, IIAlu, CPURegs>;
723 def ADD     : ArithOverflowR<0x00, 0x20, "add", IIAlu, CPURegs, 1>;
724 def SUB     : ArithOverflowR<0x00, 0x22, "sub", IIAlu, CPURegs>;
725 def SLT     : SetCC_R<0x00, 0x2a, "slt", setlt, CPURegs>;
726 def SLTu    : SetCC_R<0x00, 0x2b, "sltu", setult, CPURegs>;
727 def AND     : ArithLogicR<0x00, 0x24, "and", and, IIAlu, CPURegs, 1>;
728 def OR      : ArithLogicR<0x00, 0x25, "or",  or, IIAlu, CPURegs, 1>;
729 def XOR     : ArithLogicR<0x00, 0x26, "xor", xor, IIAlu, CPURegs, 1>;
730 def NOR     : LogicNOR<0x00, 0x27, "nor", CPURegs>;
731
732 /// Shift Instructions
733 def SLL     : shift_rotate_imm32<0x00, 0x00, "sll", shl>;
734 def SRL     : shift_rotate_imm32<0x02, 0x00, "srl", srl>;
735 def SRA     : shift_rotate_imm32<0x03, 0x00, "sra", sra>;
736 def SLLV    : shift_rotate_reg<0x04, 0x00, "sllv", shl, CPURegs>;
737 def SRLV    : shift_rotate_reg<0x06, 0x00, "srlv", srl, CPURegs>;
738 def SRAV    : shift_rotate_reg<0x07, 0x00, "srav", sra, CPURegs>;
739
740 // Rotate Instructions
741 let Predicates = [HasMips32r2] in {
742     def ROTR    : shift_rotate_imm32<0x02, 0x01, "rotr", rotr>;
743     def ROTRV   : shift_rotate_reg<0x06, 0x01, "rotrv", rotr, CPURegs>;
744 }
745
746 /// Load and Store Instructions
747 ///  aligned
748 defm LB      : LoadM32<0x20, "lb",  sextloadi8>;
749 defm LBu     : LoadM32<0x24, "lbu", zextloadi8>;
750 defm LH      : LoadM32<0x21, "lh",  sextloadi16_a>;
751 defm LHu     : LoadM32<0x25, "lhu", zextloadi16_a>;
752 defm LW      : LoadM32<0x23, "lw",  load_a>;
753 defm SB      : StoreM32<0x28, "sb", truncstorei8>;
754 defm SH      : StoreM32<0x29, "sh", truncstorei16_a>;
755 defm SW      : StoreM32<0x2b, "sw", store_a>;
756
757 ///  unaligned
758 defm ULH     : LoadM32<0x21, "ulh",  sextloadi16_u, 1>;
759 defm ULHu    : LoadM32<0x25, "ulhu", zextloadi16_u, 1>;
760 defm ULW     : LoadM32<0x23, "ulw",  load_u, 1>;
761 defm USH     : StoreM32<0x29, "ush", truncstorei16_u, 1>;
762 defm USW     : StoreM32<0x2b, "usw", store_u, 1>;
763
764 let hasSideEffects = 1 in
765 def SYNC : MipsInst<(outs), (ins i32imm:$stype), "sync $stype",
766                     [(MipsSync imm:$stype)], NoItinerary, FrmOther>
767 {
768   bits<5> stype;
769   let Opcode = 0;
770   let Inst{25-11} = 0;
771   let Inst{10-6} = stype;
772   let Inst{5-0} = 15;
773 }
774
775 /// Load-linked, Store-conditional
776 def LL    : LLBase<0x30, "ll", CPURegs, mem>, Requires<[NotN64]>;
777 def LL_P8 : LLBase<0x30, "ll", CPURegs, mem64>, Requires<[IsN64]>;
778 def SC    : SCBase<0x38, "sc", CPURegs, mem>, Requires<[NotN64]>;
779 def SC_P8 : SCBase<0x38, "sc", CPURegs, mem64>, Requires<[IsN64]>;
780
781 /// Jump and Branch Instructions
782 def J       : JumpFJ<0x02, "j">;
783 def JR      : JumpFR<0x00, 0x08, "jr", CPURegs>;
784 def JAL     : JumpLink<0x03, "jal">;
785 def JALR    : JumpLinkReg<0x00, 0x09, "jalr">;
786 def BEQ     : CBranch<0x04, "beq", seteq, CPURegs>;
787 def BNE     : CBranch<0x05, "bne", setne, CPURegs>;
788 def BGEZ    : CBranchZero<0x01, 1, "bgez", setge, CPURegs>;
789 def BGTZ    : CBranchZero<0x07, 0, "bgtz", setgt, CPURegs>;
790 def BLEZ    : CBranchZero<0x06, 0, "blez", setle, CPURegs>;
791 def BLTZ    : CBranchZero<0x01, 0, "bltz", setlt, CPURegs>;
792
793 let rt=0x11 in
794   def BGEZAL  : BranchLink<"bgezal">;
795 let rt=0x10 in
796   def BLTZAL  : BranchLink<"bltzal">;
797
798 let isReturn=1, isTerminator=1, hasDelaySlot=1,
799     isBarrier=1, hasCtrlDep=1, rd=0, rt=0, shamt=0 in
800   def RET : FR <0x00, 0x08, (outs), (ins CPURegs:$target),
801                 "jr\t$target", [(MipsRet CPURegs:$target)], IIBranch>;
802
803 /// Multiply and Divide Instructions.
804 def MULT    : Mult32<0x18, "mult", IIImul>;
805 def MULTu   : Mult32<0x19, "multu", IIImul>;
806 def SDIV    : Div32<MipsDivRem, 0x1a, "div", IIIdiv>;
807 def UDIV    : Div32<MipsDivRemU, 0x1b, "divu", IIIdiv>;
808
809 def MTHI : MoveToLOHI<0x11, "mthi", CPURegs, [HI]>;
810 def MTLO : MoveToLOHI<0x13, "mtlo", CPURegs, [LO]>;
811 def MFHI : MoveFromLOHI<0x10, "mfhi", CPURegs, [HI]>;
812 def MFLO : MoveFromLOHI<0x12, "mflo", CPURegs, [LO]>;
813
814 /// Sign Ext In Register Instructions.
815 def SEB : SignExtInReg<0x10, "seb", i8>;
816 def SEH : SignExtInReg<0x18, "seh", i16>;
817
818 /// Count Leading
819 def CLZ : CountLeading0<0x20, "clz", CPURegs>;
820 def CLO : CountLeading1<0x21, "clo", CPURegs>;
821
822 /// Byte Swap
823 def WSBW : ByteSwap<0x20, 0x2, "wsbw">;
824
825 /// No operation
826 let addr=0 in
827   def NOP   : FJ<0, (outs), (ins), "nop", [], IIAlu>;
828
829 // FrameIndexes are legalized when they are operands from load/store
830 // instructions. The same not happens for stack address copies, so an
831 // add op with mem ComplexPattern is used and the stack address copy
832 // can be matched. It's similar to Sparc LEA_ADDRi
833 def LEA_ADDiu : EffectiveAddress<"addiu\t$rt, $addr", CPURegs, mem_ea>;
834
835 // DynAlloc node points to dynamically allocated stack space.
836 // $sp is added to the list of implicitly used registers to prevent dead code
837 // elimination from removing instructions that modify $sp.
838 let Uses = [SP] in
839 def DynAlloc : EffectiveAddress<"addiu\t$rt, $addr", CPURegs, mem_ea>;
840
841 // MADD*/MSUB*
842 def MADD  : MArithR<0, "madd", MipsMAdd, 1>;
843 def MADDU : MArithR<1, "maddu", MipsMAddu, 1>;
844 def MSUB  : MArithR<4, "msub", MipsMSub>;
845 def MSUBU : MArithR<5, "msubu", MipsMSubu>;
846
847 // MUL is a assembly macro in the current used ISAs. In recent ISA's
848 // it is a real instruction.
849 def MUL   : ArithLogicR<0x1c, 0x02, "mul", mul, IIImul, CPURegs, 1>,
850             Requires<[HasMips32]>;
851
852 def RDHWR : ReadHardware;
853
854 def EXT : ExtIns<0, "ext", (outs CPURegs:$rt),
855                  (ins CPURegs:$rs, uimm16:$pos, size_ext:$sz),
856                  [(set CPURegs:$rt,
857                    (MipsExt CPURegs:$rs, immZExt5:$pos, immZExt5:$sz))],
858                  NoItinerary>;
859
860 let Constraints = "$src = $rt" in
861 def INS : ExtIns<4, "ins", (outs CPURegs:$rt),
862                  (ins CPURegs:$rs, uimm16:$pos, size_ins:$sz, CPURegs:$src),
863                  [(set CPURegs:$rt,
864                    (MipsIns CPURegs:$rs, immZExt5:$pos, immZExt5:$sz,
865                     CPURegs:$src))],
866                  NoItinerary>;
867
868 //===----------------------------------------------------------------------===//
869 //  Arbitrary patterns that map to one or more instructions
870 //===----------------------------------------------------------------------===//
871
872 // Small immediates
873 def : Pat<(i32 immSExt16:$in),
874           (ADDiu ZERO, imm:$in)>;
875 def : Pat<(i32 immZExt16:$in),
876           (ORi ZERO, imm:$in)>;
877
878 // Arbitrary immediates
879 def : Pat<(i32 imm:$imm),
880           (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
881
882 // Carry patterns
883 def : Pat<(subc CPURegs:$lhs, CPURegs:$rhs),
884           (SUBu CPURegs:$lhs, CPURegs:$rhs)>;
885 def : Pat<(addc CPURegs:$lhs, CPURegs:$rhs),
886           (ADDu CPURegs:$lhs, CPURegs:$rhs)>;
887 def : Pat<(addc  CPURegs:$src, immSExt16:$imm),
888           (ADDiu CPURegs:$src, imm:$imm)>;
889
890 // Call
891 def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
892           (JAL tglobaladdr:$dst)>;
893 def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
894           (JAL texternalsym:$dst)>;
895 //def : Pat<(MipsJmpLink CPURegs:$dst),
896 //          (JALR CPURegs:$dst)>;
897
898 // hi/lo relocs
899 def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
900 def : Pat<(MipsHi tblockaddress:$in), (LUi tblockaddress:$in)>;
901 def : Pat<(MipsHi tjumptable:$in), (LUi tjumptable:$in)>;
902 def : Pat<(MipsHi tconstpool:$in), (LUi tconstpool:$in)>;
903
904 def : Pat<(MipsLo tglobaladdr:$in), (ADDiu ZERO, tglobaladdr:$in)>;
905 def : Pat<(MipsLo tblockaddress:$in), (ADDiu ZERO, tblockaddress:$in)>;
906 def : Pat<(MipsLo tjumptable:$in), (ADDiu ZERO, tjumptable:$in)>;
907 def : Pat<(MipsLo tconstpool:$in), (ADDiu ZERO, tconstpool:$in)>;
908
909 def : Pat<(add CPURegs:$hi, (MipsLo tglobaladdr:$lo)),
910           (ADDiu CPURegs:$hi, tglobaladdr:$lo)>;
911 def : Pat<(add CPURegs:$hi, (MipsLo tblockaddress:$lo)),
912           (ADDiu CPURegs:$hi, tblockaddress:$lo)>;
913 def : Pat<(add CPURegs:$hi, (MipsLo tjumptable:$lo)),
914           (ADDiu CPURegs:$hi, tjumptable:$lo)>;
915 def : Pat<(add CPURegs:$hi, (MipsLo tconstpool:$lo)),
916           (ADDiu CPURegs:$hi, tconstpool:$lo)>;
917
918 // gp_rel relocs
919 def : Pat<(add CPURegs:$gp, (MipsGPRel tglobaladdr:$in)),
920           (ADDiu CPURegs:$gp, tglobaladdr:$in)>;
921 def : Pat<(add CPURegs:$gp, (MipsGPRel tconstpool:$in)),
922           (ADDiu CPURegs:$gp, tconstpool:$in)>;
923
924 // tlsgd
925 def : Pat<(add CPURegs:$gp, (MipsTlsGd tglobaltlsaddr:$in)),
926           (ADDiu CPURegs:$gp, tglobaltlsaddr:$in)>;
927
928 // tprel hi/lo
929 def : Pat<(MipsTprelHi tglobaltlsaddr:$in), (LUi tglobaltlsaddr:$in)>;
930 def : Pat<(MipsTprelLo tglobaltlsaddr:$in), (ADDiu ZERO, tglobaltlsaddr:$in)>;
931 def : Pat<(add CPURegs:$hi, (MipsTprelLo tglobaltlsaddr:$lo)),
932           (ADDiu CPURegs:$hi, tglobaltlsaddr:$lo)>;
933
934 // wrapper_pic
935 class WrapperPICPat<SDNode node>:
936       Pat<(MipsWrapperPIC node:$in),
937           (ADDiu GP, node:$in)>;
938
939 def : WrapperPICPat<tglobaladdr>;
940 def : WrapperPICPat<tconstpool>;
941 def : WrapperPICPat<texternalsym>;
942 def : WrapperPICPat<tblockaddress>;
943 def : WrapperPICPat<tjumptable>;
944
945 // Mips does not have "not", so we expand our way
946 def : Pat<(not CPURegs:$in),
947           (NOR CPURegs:$in, ZERO)>;
948
949 // extended load and stores
950 def : Pat<(extloadi1  addr:$src), (LBu addr:$src)>;
951 def : Pat<(extloadi8  addr:$src), (LBu addr:$src)>;
952 def : Pat<(extloadi16_a addr:$src), (LHu addr:$src)>;
953 def : Pat<(extloadi16_u addr:$src), (ULHu addr:$src)>;
954
955 // peepholes
956 def : Pat<(store (i32 0), addr:$dst), (SW ZERO, addr:$dst)>;
957
958 // brcond patterns
959 multiclass BrcondPats<RegisterClass RC, Instruction BEQOp, Instruction BNEOp,
960                       Instruction SLTOp, Instruction SLTuOp, Instruction SLTiOp,
961                       Instruction SLTiuOp, Register ZEROReg> {
962 def : Pat<(brcond (i32 (setne RC:$lhs, 0)), bb:$dst),
963           (BNEOp RC:$lhs, ZEROReg, bb:$dst)>;
964 def : Pat<(brcond (i32 (seteq RC:$lhs, 0)), bb:$dst),
965           (BEQOp RC:$lhs, ZEROReg, bb:$dst)>;
966
967 def : Pat<(brcond (i32 (setge RC:$lhs, RC:$rhs)), bb:$dst),
968           (BEQ (SLTOp RC:$lhs, RC:$rhs), ZERO, bb:$dst)>;
969 def : Pat<(brcond (i32 (setuge RC:$lhs, RC:$rhs)), bb:$dst),
970           (BEQ (SLTuOp RC:$lhs, RC:$rhs), ZERO, bb:$dst)>;
971 def : Pat<(brcond (i32 (setge RC:$lhs, immSExt16:$rhs)), bb:$dst),
972           (BEQ (SLTiOp RC:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
973 def : Pat<(brcond (i32 (setuge RC:$lhs, immSExt16:$rhs)), bb:$dst),
974           (BEQ (SLTiuOp RC:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
975
976 def : Pat<(brcond (i32 (setle RC:$lhs, RC:$rhs)), bb:$dst),
977           (BEQ (SLTOp RC:$rhs, RC:$lhs), ZERO, bb:$dst)>;
978 def : Pat<(brcond (i32 (setule RC:$lhs, RC:$rhs)), bb:$dst),
979           (BEQ (SLTuOp RC:$rhs, RC:$lhs), ZERO, bb:$dst)>;
980
981 def : Pat<(brcond RC:$cond, bb:$dst),
982           (BNEOp RC:$cond, ZEROReg, bb:$dst)>;
983 }
984
985 defm : BrcondPats<CPURegs, BEQ, BNE, SLT, SLTu, SLTi, SLTiu, ZERO>;
986
987 // setcc patterns
988 multiclass SeteqPats<RegisterClass RC, Instruction SLTiuOp, Instruction XOROp,
989                      Instruction SLTuOp, Register ZEROReg> {
990   def : Pat<(seteq RC:$lhs, RC:$rhs),
991             (SLTiuOp (XOROp RC:$lhs, RC:$rhs), 1)>;
992   def : Pat<(setne RC:$lhs, RC:$rhs),
993             (SLTuOp ZEROReg, (XOROp RC:$lhs, RC:$rhs))>;
994 }
995
996 multiclass SetlePats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
997   def : Pat<(setle RC:$lhs, RC:$rhs),
998             (XORi (SLTOp RC:$rhs, RC:$lhs), 1)>;
999   def : Pat<(setule RC:$lhs, RC:$rhs),
1000             (XORi (SLTuOp RC:$rhs, RC:$lhs), 1)>;
1001 }
1002
1003 multiclass SetgtPats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1004   def : Pat<(setgt RC:$lhs, RC:$rhs),
1005             (SLTOp RC:$rhs, RC:$lhs)>;
1006   def : Pat<(setugt RC:$lhs, RC:$rhs),
1007             (SLTuOp RC:$rhs, RC:$lhs)>;
1008 }
1009
1010 multiclass SetgePats<RegisterClass RC, Instruction SLTOp, Instruction SLTuOp> {
1011   def : Pat<(setge RC:$lhs, RC:$rhs),
1012             (XORi (SLTOp RC:$lhs, RC:$rhs), 1)>;
1013   def : Pat<(setuge RC:$lhs, RC:$rhs),
1014             (XORi (SLTuOp RC:$lhs, RC:$rhs), 1)>;
1015 }
1016
1017 multiclass SetgeImmPats<RegisterClass RC, Instruction SLTiOp,
1018                         Instruction SLTiuOp> {
1019   def : Pat<(setge RC:$lhs, immSExt16:$rhs),
1020             (XORi (SLTiOp RC:$lhs, immSExt16:$rhs), 1)>;
1021   def : Pat<(setuge RC:$lhs, immSExt16:$rhs),
1022             (XORi (SLTiuOp RC:$lhs, immSExt16:$rhs), 1)>;
1023 }
1024
1025 defm : SeteqPats<CPURegs, SLTiu, XOR, SLTu, ZERO>;
1026 defm : SetlePats<CPURegs, SLT, SLTu>;
1027 defm : SetgtPats<CPURegs, SLT, SLTu>;
1028 defm : SetgePats<CPURegs, SLT, SLTu>;
1029 defm : SetgeImmPats<CPURegs, SLTi, SLTiu>;
1030
1031 // select MipsDynAlloc
1032 def : Pat<(MipsDynAlloc addr:$f), (DynAlloc addr:$f)>;
1033
1034 //===----------------------------------------------------------------------===//
1035 // Floating Point Support
1036 //===----------------------------------------------------------------------===//
1037
1038 include "MipsInstrFPU.td"
1039 include "Mips64InstrInfo.td"
1040 include "MipsCondMov.td"
1041