blob: 1f5d152261e1de2fd105e95912a422e8a3f10377 [file] [log] [blame]
Dan Gohmanf17a25c2007-07-18 16:29:46 +00001//===- MipsInstrInfo.td - Mips Register defs --------------------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file was developed by Bruno Cardoso Lopes and is distributed under the
6// University of Illinois Open Source License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9
10//===----------------------------------------------------------------------===//
11// Instruction format superclass
12//===----------------------------------------------------------------------===//
13
14include "MipsInstrFormats.td"
15
16//===----------------------------------------------------------------------===//
17// Mips profiles and nodes
18//===----------------------------------------------------------------------===//
19
20// Call
21def SDT_MipsJmpLink : SDTypeProfile<0, 1, [SDTCisVT<0, iPTR>]>;
22def MipsJmpLink : SDNode<"MipsISD::JmpLink",SDT_MipsJmpLink, [SDNPHasChain,
23 SDNPOutFlag]>;
24
25// Hi and Lo nodes are created to let easy manipulation of 16-bit when
26// handling 32-bit immediates. They are used on MipsISelLowering to
27// lower stuff like GlobalAddress, ExternalSymbol, ...
28// This two nodes have nothing to do with Mips Registers Hi and Lo.
29def MipsHi : SDNode<"MipsISD::Hi", SDTIntUnaryOp>;
30def MipsLo : SDNode<"MipsISD::Lo", SDTIntUnaryOp>;
31
32// Return
33def SDT_MipsRet : SDTypeProfile<0, 1, [SDTCisInt<0>]>;
34def MipsRet : SDNode<"MipsISD::Ret", SDT_MipsRet, [SDNPHasChain,
35 SDNPOptInFlag]>;
36
37// These are target-independent nodes, but have target-specific formats.
38def SDT_MipsCallSeq : SDTypeProfile<0, 1, [SDTCisVT<0, i32>]>;
39def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_MipsCallSeq,
40 [SDNPHasChain, SDNPOutFlag]>;
41def callseq_end : SDNode<"ISD::CALLSEQ_END", SDT_MipsCallSeq,
42 [SDNPHasChain, SDNPOutFlag]>;
43
44// Instruction operand types
45def brtarget : Operand<OtherVT>;
46def calltarget : Operand<i32>;
47def uimm16 : Operand<i32>;
48def simm16 : Operand<i32>;
49def shamt : Operand<i32>;
50
51// Address operand
52def mem : Operand<i32> {
53 let PrintMethod = "printMemOperand";
54 let MIOperandInfo = (ops simm16, CPURegs);
55}
56
57//===----------------------------------------------------------------------===//
58// Mips Patterns and Transformations
59//===----------------------------------------------------------------------===//
60
61// Transformation Function - get the lower 16 bits.
62def LO16 : SDNodeXForm<imm, [{
63 return getI32Imm((unsigned)N->getValue() & 0xFFFF);
64}]>;
65
66// Transformation Function - get the higher 16 bits.
67def HI16 : SDNodeXForm<imm, [{
68 return getI32Imm((unsigned)N->getValue() >> 16);
69}]>;
70
71// Node immediate fits as 16-bit sign extended on target immediate.
72// e.g. addi, andi
73def immSExt16 : PatLeaf<(imm), [{
74 if (N->getValueType(0) == MVT::i32)
75 return (int32_t)N->getValue() == (short)N->getValue();
76 else
77 return (int64_t)N->getValue() == (short)N->getValue();
78}]>;
79
80// Node immediate fits as 16-bit zero extended on target immediate.
81// The LO16 param means that only the lower 16 bits of the node
82// immediate are caught.
83// e.g. addiu, sltiu
84def immZExt16 : PatLeaf<(imm), [{
85 if (N->getValueType(0) == MVT::i32)
86 return (uint32_t)N->getValue() == (unsigned short)N->getValue();
87 else
88 return (uint64_t)N->getValue() == (unsigned short)N->getValue();
89}], LO16>;
90
91// shamt field must fit in 5 bits.
92def immZExt5 : PatLeaf<(imm), [{
93 return N->getValue() == ((N->getValue()) & 0x1f) ;
94}]>;
95
96// Mips Address Mode! SDNode frameindex could possibily be a match
97// since load and store instructions from stack used it.
98def addr : ComplexPattern<i32, 2, "SelectAddr", [frameindex], []>;
99
100//===----------------------------------------------------------------------===//
101// Instructions specific format
102//===----------------------------------------------------------------------===//
103
104// Arithmetic 3 register operands
105let isCommutable = 1 in
106class ArithR< bits<6> op, bits<6> func, string instr_asm, SDNode OpNode>:
107 FR< op,
108 func,
109 (ops CPURegs:$dst, CPURegs:$b, CPURegs:$c),
110 !strconcat(instr_asm, " $dst, $b, $c"),
111 [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))] >;
112
113let isCommutable = 1 in
114class ArithOverflowR< bits<6> op, bits<6> func, string instr_asm>:
115 FR< op,
116 func,
117 (ops CPURegs:$dst, CPURegs:$b, CPURegs:$c),
118 !strconcat(instr_asm, " $dst, $b, $c"),
119 []>;
120
121// Arithmetic 2 register operands
122let isCommutable = 1 in
123class ArithI<bits<6> op, string instr_asm, SDNode OpNode,
124 Operand Od, PatLeaf imm_type> :
125 FI< op,
126 (ops CPURegs:$dst, CPURegs:$b, Od:$c),
127 !strconcat(instr_asm, " $dst, $b, $c"),
128 [(set CPURegs:$dst, (OpNode CPURegs:$b, imm_type:$c))] >;
129
130// Arithmetic Multiply ADD/SUB
131let rd=0 in
132class MArithR<bits<6> func, string instr_asm> :
133 FR< 0x1c,
134 func,
135 (ops CPURegs:$rs, CPURegs:$rt),
136 !strconcat(instr_asm, " $rs, $rt"),
137 []>;
138
139// Logical
140class LogicR<bits<6> func, string instr_asm, SDNode OpNode>:
141 FR< 0x00,
142 func,
143 (ops CPURegs:$dst, CPURegs:$b, CPURegs:$c),
144 !strconcat(instr_asm, " $dst, $b, $c"),
145 [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))] >;
146
147class LogicI<bits<6> op, string instr_asm, SDNode OpNode>:
148 FI< op,
149 (ops CPURegs:$dst, CPURegs:$b, uimm16:$c),
150 !strconcat(instr_asm, " $dst, $b, $c"),
151 [(set CPURegs:$dst, (OpNode CPURegs:$b, immSExt16:$c))]>;
152
153class LogicNOR<bits<6> op, bits<6> func, string instr_asm>:
154 FR< op,
155 func,
156 (ops CPURegs:$dst, CPURegs:$b, CPURegs:$c),
157 !strconcat(instr_asm, " $dst, $b, $c"),
158 [(set CPURegs:$dst, (not (or CPURegs:$b, CPURegs:$c)))] >;
159
160// Shifts
161let rt = 0 in
162class LogicR_shift_imm<bits<6> func, string instr_asm, SDNode OpNode>:
163 FR< 0x00,
164 func,
165 (ops CPURegs:$dst, CPURegs:$b, shamt:$c),
166 !strconcat(instr_asm, " $dst, $b, $c"),
167 [(set CPURegs:$dst, (OpNode CPURegs:$b, immZExt5:$c))] >;
168
169class LogicR_shift_reg<bits<6> func, string instr_asm, SDNode OpNode>:
170 FR< 0x00,
171 func,
172 (ops CPURegs:$dst, CPURegs:$b, CPURegs:$c),
173 !strconcat(instr_asm, " $dst, $b, $c"),
174 [(set CPURegs:$dst, (OpNode CPURegs:$b, CPURegs:$c))] >;
175
176// Load Upper Imediate
177class LoadUpper<bits<6> op, string instr_asm>:
178 FI< op,
179 (ops CPURegs:$dst, uimm16:$imm),
180 !strconcat(instr_asm, " $dst, $imm"),
181 []>;
182
183// Memory Load/Store
184let isLoad = 1 in
185class LoadM<bits<6> op, string instr_asm, PatFrag OpNode>:
186 FI< op,
187 (ops CPURegs:$dst, mem:$addr),
188 !strconcat(instr_asm, " $dst, $addr"),
189 [(set CPURegs:$dst, (OpNode addr:$addr))]>;
190
191let isStore = 1 in
192class StoreM<bits<6> op, string instr_asm, PatFrag OpNode>:
193 FI< op,
194 (ops CPURegs:$dst, mem:$addr),
195 !strconcat(instr_asm, " $dst, $addr"),
196 [(OpNode CPURegs:$dst, addr:$addr)]>;
197
198// Conditional Branch
199let isBranch = 1, noResults=1, isTerminator=1 in
200class CBranch<bits<6> op, string instr_asm, PatFrag cond_op>:
201 FI< op,
202 (ops CPURegs:$a, CPURegs:$b, brtarget:$offset),
203 !strconcat(instr_asm, " $a, $b, $offset"),
204 [(brcond (cond_op CPURegs:$a, CPURegs:$b), bb:$offset)]>;
205
206class SetCC_R<bits<6> op, bits<6> func, string instr_asm,
207 PatFrag cond_op>:
208 FR< op,
209 func,
210 (ops CPURegs:$dst, CPURegs:$b, CPURegs:$c),
211 !strconcat(instr_asm, " $dst, $b, $c"),
212 [(set CPURegs:$dst, (cond_op CPURegs:$b, CPURegs:$c))]>;
213
214class SetCC_I<bits<6> op, string instr_asm, PatFrag cond_op,
215 Operand Od, PatLeaf imm_type>:
216 FI< op,
217 (ops CPURegs:$dst, CPURegs:$b, Od:$c),
218 !strconcat(instr_asm, " $dst, $b, $c"),
219 [(set CPURegs:$dst, (cond_op CPURegs:$b, imm_type:$c))]>;
220
221// Unconditional branch
222let hasCtrlDep=1, noResults=1, isTerminator=1 in
223class JumpFJ<bits<6> op, string instr_asm>:
224 FJ< op,
225 (ops brtarget:$target),
226 !strconcat(instr_asm, " $target"),
227 [(br bb:$target)]>;
228
229let hasCtrlDep=1, noResults=1, isTerminator=1, rd=0 in
230class JumpFR<bits<6> op, bits<6> func, string instr_asm>:
231 FR< op,
232 func,
233 (ops CPURegs:$target),
234 !strconcat(instr_asm, " $target"),
235 []>;
236
237// Jump and Link (Call)
238let isCall=1 in
239class JumpLink<bits<6> op, string instr_asm>:
240 FJ< op,
241 (ops calltarget:$target),
242 !strconcat(instr_asm, " $target"),
243 [(MipsJmpLink imm:$target)]>;
244
245let isCall=1 in
246class JumpLinkReg<bits<6> op, bits<6> func, string instr_asm>:
247 FR< op,
248 func,
249 (ops CPURegs:$rd, CPURegs:$rs),
250 !strconcat(instr_asm, " $rs, $rd"),
251 []>;
252
253// Mul, Div
254class MulDiv<bits<6> func, string instr_asm>:
255 FR< 0x00,
256 func,
257 (ops CPURegs:$a, CPURegs:$b),
258 !strconcat(instr_asm, " $a, $b"),
259 []>;
260
261// Move from Hi/Lo
262class MoveFromTo<bits<6> func, string instr_asm>:
263 FR< 0x00,
264 func,
265 (ops CPURegs:$dst),
266 !strconcat(instr_asm, " $dst"),
267 []>;
268
269// Count Leading Ones/Zeros in Word
270class CountLeading<bits<6> func, string instr_asm>:
271 FR< 0x1c,
272 func,
273 (ops CPURegs:$dst, CPURegs:$src),
274 !strconcat(instr_asm, " $dst, $src"),
275 []>;
276
277
278//===----------------------------------------------------------------------===//
279// Pseudo instructions
280//===----------------------------------------------------------------------===//
281
282class Pseudo<dag ops, string asmstr, list<dag> pattern>:
283 MipsInst<ops, asmstr, pattern>;
284
285// As stack alignment is always done with addiu, we need a 16-bit immediate
286def ADJCALLSTACKDOWN : Pseudo<(ops uimm16:$amt),
287 "!ADJCALLSTACKDOWN $amt",
288 [(callseq_start imm:$amt)]>, Imp<[SP],[SP]>;
289def ADJCALLSTACKUP : Pseudo<(ops uimm16:$amt),
290 "!ADJCALLSTACKUP $amt",
291 [(callseq_end imm:$amt)]>, Imp<[SP],[SP]>;
292
293def IMPLICIT_DEF_CPURegs : Pseudo<(ops CPURegs:$dst),
294 "!IMPLICIT_DEF $dst",
295 [(set CPURegs:$dst, (undef))]>;
296
297//===----------------------------------------------------------------------===//
298// Instruction definition
299//===----------------------------------------------------------------------===//
300
301//===----------------------------------------------------------------------===//
302// Mips32 I
303//===----------------------------------------------------------------------===//
304
305// Arithmetic
306def ADDiu : ArithI<0x09, "addiu", add, uimm16, immSExt16>;
307def ADDi : ArithI<0x08, "addi", add, simm16, immZExt16>;
308def MUL : ArithR<0x1c, 0x02, "mul", mul>;
309def ADDu : ArithR<0x00, 0x21, "addu", add>;
310def SUBu : ArithR<0x00, 0x23, "subu", sub>;
311def ADD : ArithOverflowR<0x00, 0x20, "add">;
312def SUB : ArithOverflowR<0x00, 0x22, "sub">;
313def MADD : MArithR<0x00, "madd">;
314def MADDU : MArithR<0x01, "maddu">;
315def MSUB : MArithR<0x04, "msub">;
316def MSUBU : MArithR<0x05, "msubu">;
317
318// Logical
319def AND : LogicR<0x24, "and", and>;
320def OR : LogicR<0x25, "or", or>;
321def XOR : LogicR<0x26, "xor", xor>;
322def ANDi : LogicI<0x0c, "andi", and>;
323def ORi : LogicI<0x0d, "ori", or>;
324def XORi : LogicI<0x0e, "xori", xor>;
325def NOR : LogicNOR<0x00, 0x27, "nor">;
326
327// Shifts
328def SLL : LogicR_shift_imm<0x00, "sll", shl>;
329def SRL : LogicR_shift_imm<0x02, "srl", srl>;
330def SRA : LogicR_shift_imm<0x03, "sra", sra>;
331def SLLV : LogicR_shift_reg<0x04, "sllv", shl>;
332def SRLV : LogicR_shift_reg<0x06, "srlv", srl>;
333def SRAV : LogicR_shift_reg<0x07, "srav", sra>;
334
335// Load Upper Immediate
336def LUi : LoadUpper<0x0f, "lui">;
337
338// Load/Store
339def LB : LoadM<0x20, "lb", sextloadi8>;
340def LBu : LoadM<0x24, "lbu", zextloadi8>;
341def LH : LoadM<0x21, "lh", sextloadi16>;
342def LHu : LoadM<0x25, "lhu", zextloadi16>;
343def LW : LoadM<0x23, "lw", load>;
344def SB : StoreM<0x28, "sb", truncstorei8>;
345def SH : StoreM<0x29, "sh", truncstorei16>;
346def SW : StoreM<0x2b, "sw", store>;
347
348// Conditional Branch
349def BEQ : CBranch<0x04, "beq", seteq>;
350def BNE : CBranch<0x05, "bne", setne>;
351def SLT : SetCC_R<0x00, 0x2a, "slt", setlt>;
352def SLTu : SetCC_R<0x00, 0x2b, "sltu", setult>;
353def SLTi : SetCC_I<0x0a, "slti", setlt, simm16, immSExt16>;
354def SLTiu : SetCC_I<0x0b, "sltiu", setult, uimm16, immZExt16>;
355
356// Unconditional jump
357def J : JumpFJ<0x02, "j">;
358def JR : JumpFR<0x00, 0x08, "jr">;
359
360// Jump and Link (Call)
361def JAL : JumpLink<0x03, "jal">;
362def JALR : JumpLinkReg<0x00, 0x09, "jalr">;
363
364// MulDiv and Move From Hi/Lo operations, have
365// their correpondent SDNodes created on ISelDAG.
366// Special Mul, Div operations
367def MULT : MulDiv<0x18, "mult">;
368def MULTu : MulDiv<0x19, "multu">;
369def DIV : MulDiv<0x1a, "div">;
370def DIVu : MulDiv<0x1b, "divu">;
371
372// Move From Hi/Lo
373def MFHI : MoveFromTo<0x10, "mfhi">;
374def MFLO : MoveFromTo<0x12, "mflo">;
375def MTHI : MoveFromTo<0x11, "mthi">;
376def MTLO : MoveFromTo<0x13, "mtlo">;
377
378// Count Leading
379def CLO : CountLeading<0x21, "clo">;
380def CLZ : CountLeading<0x20, "clz">;
381
382// No operation
383let addr=0 in
384def NOOP : FJ<0, (ops), "nop", []>;
385
386// Ret instruction - as mips does not have "ret" a
387// jr $ra must be generated.
388let isReturn=1, isTerminator=1, hasDelaySlot=1, noResults=1,
389 isBarrier=1, hasCtrlDep=1, rs=0, rt=0, shamt=0 in
390{
391 def RET : FR <0x00, 0x02, (ops CPURegs:$target),
392 "jr $target", [(MipsRet CPURegs:$target)]>;
393}
394
395//===----------------------------------------------------------------------===//
396// Arbitrary patterns that map to one or more instructions
397//===----------------------------------------------------------------------===//
398
399// Small immediates
400def : Pat<(i32 immSExt16:$in),
401 (ADDiu ZERO, imm:$in)>;
402def : Pat<(i32 immZExt16:$in),
403 (ORi ZERO, imm:$in)>;
404
405// Arbitrary immediates
406def : Pat<(i32 imm:$imm),
407 (ORi (LUi (HI16 imm:$imm)), (LO16 imm:$imm))>;
408
409// Call
410def : Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
411 (JAL tglobaladdr:$dst)>;
412def : Pat<(MipsJmpLink (i32 texternalsym:$dst)),
413 (JAL texternalsym:$dst)>;
414
415// GlobalAddress, Constant Pool, ExternalSymbol, and JumpTable
416def : Pat<(MipsHi tglobaladdr:$in), (LUi tglobaladdr:$in)>;
417def : Pat<(MipsLo tglobaladdr:$in), (ADDiu ZERO, tglobaladdr:$in)>;
418
419// When extracting the address from GlobalAddress we
420// need something of the form "addiu $reg, %lo(addr)"
421def : Pat<(add CPURegs:$a, (MipsLo tglobaladdr:$in)),
422 (ADDiu CPURegs:$a, tglobaladdr:$in)>;
423
424// Mips does not have not, so we increase the operation
425def : Pat<(not CPURegs:$in),
426 (NOR CPURegs:$in, CPURegs:$in)>;
427
428// extended load and stores
429def : Pat<(i32 (extloadi8 addr:$src)), (LBu addr:$src)>;
430def : Pat<(i32 (extloadi16 addr:$src)), (LHu addr:$src)>;
431def : Pat<(truncstorei1 CPURegs:$src, addr:$addr),
432 (SB CPURegs:$src, addr:$src)>;
433
434def : Pat<(brcond (setne CPURegs:$lhs, (add ZERO, 0)), bb:$dst),
435 (BNE CPURegs:$lhs, ZERO, bb:$dst)>;
436
437
438// Conditional branch patterns.
439// cond branches patterns, 2 register operands signed.
440def : Pat<(brcond (setlt CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
441 (BNE (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
442def : Pat<(brcond (setle CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
443 (BEQ (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
444def : Pat<(brcond (setgt CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
445 (BNE (SLT CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
446def : Pat<(brcond (setge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
447 (BEQ (SLT CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
448
449// cond branches patterns, 2 register operands unsigned.
450def : Pat<(brcond (setult CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
451 (BNE (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
452def : Pat<(brcond (setule CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
453 (BEQ (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
454def : Pat<(brcond (setugt CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
455 (BNE (SLTu CPURegs:$rhs, CPURegs:$lhs), ZERO, bb:$dst)>;
456def : Pat<(brcond (setuge CPURegs:$lhs, CPURegs:$rhs), bb:$dst),
457 (BEQ (SLTu CPURegs:$lhs, CPURegs:$rhs), ZERO, bb:$dst)>;
458
459// cond branches patterns, reg/imm operands signed.
460def : Pat<(brcond (setult CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
461 (BNE (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
462def : Pat<(brcond (setuge CPURegs:$lhs, immSExt16:$rhs), bb:$dst),
463 (BEQ (SLTi CPURegs:$lhs, immSExt16:$rhs), ZERO, bb:$dst)>;
464
465// cond branches patterns, reg/imm operands unsigned.
466def : Pat<(brcond (setult CPURegs:$lhs, immZExt16:$rhs), bb:$dst),
467 (BNE (SLTiu CPURegs:$lhs, immZExt16:$rhs), ZERO, bb:$dst)>;
468def : Pat<(brcond (setuge CPURegs:$lhs, immZExt16:$rhs), bb:$dst),
469 (BEQ (SLTiu CPURegs:$lhs, immZExt16:$rhs), ZERO, bb:$dst)>;