For Mips 16, add the optimization where the 16 bit form of addiu sp can be used
[oota-llvm.git] / lib / Target / Mips / Mips16InstrInfo.td
1 //===- Mips16InstrInfo.td - Target Description for Mips16  -*- 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 describes Mips16 instructions.
11 //
12 //===----------------------------------------------------------------------===//
13 //
14 //
15 // Mips Address
16 //
17 def addr16 :
18   ComplexPattern<iPTR, 3, "SelectAddr16", [frameindex], [SDNPWantParent]>;
19
20 //
21 // Address operand
22 def mem16 : Operand<i32> {
23   let PrintMethod = "printMemOperand";
24   let MIOperandInfo = (ops CPU16Regs, simm16, CPU16Regs);
25   let EncoderMethod = "getMemEncoding";
26 }
27
28 def mem16_ea : Operand<i32> {
29   let PrintMethod = "printMemOperandEA";
30   let MIOperandInfo = (ops CPU16Regs, simm16);
31   let EncoderMethod = "getMemEncoding";
32 }
33
34 //
35 //
36 // I8 instruction format
37 //
38
39 class FI816_ins_base<bits<3> _func, string asmstr,
40                      string asmstr2, InstrItinClass itin>:
41   FI816<_func, (outs), (ins simm16:$imm), !strconcat(asmstr, asmstr2),
42         [], itin>;
43
44
45 class FI816_SP_ins<bits<3> _func, string asmstr,
46                    InstrItinClass itin>:
47   FI816_ins_base<_func, asmstr, "\t$$sp, $imm # 16 bit inst", itin>;
48
49 //
50 // RI instruction format
51 //
52
53
54 class F2RI16_ins<bits<5> _op, string asmstr,
55                      InstrItinClass itin>:
56   FRI16<_op, (outs CPU16Regs:$rx), (ins CPU16Regs:$rx_, simm16:$imm),
57         !strconcat(asmstr, "\t$rx, $imm\t# 16 bit inst"), [], itin> {
58   let Constraints = "$rx_ = $rx";
59 }
60
61 //
62 // Compare a register and immediate and place result in CC
63 // Implicit use of T8
64 //
65 // EXT-CCRR Instruction format
66 //
67 class FEXT_CCRXI16_ins<bits<5> _op, string asmstr,
68                        InstrItinClass itin>:
69   FEXT_RI16<_op, (outs CPU16Regs:$cc), (ins CPU16Regs:$rx, simm16:$imm),
70             !strconcat(asmstr, "\t$rx, $imm\n\tmove\t$cc, $$t8"), [], itin> {
71   let isCodeGenOnly=1;
72 }
73
74 // JAL and JALX instruction format
75 //
76 class FJAL16_ins<bits<1> _X, string asmstr,
77                  InstrItinClass itin>:
78   FJAL16<_X, (outs), (ins simm20:$imm),
79          !strconcat(asmstr, "\t$imm\n\tnop"),[],
80          itin>  {
81   let isCodeGenOnly=1;
82 }
83 //
84 // EXT-I instruction format
85 //
86 class FEXT_I16_ins<bits<5> eop, string asmstr, InstrItinClass itin> :
87   FEXT_I16<eop, (outs), (ins brtarget:$imm16),
88            !strconcat(asmstr, "\t$imm16"),[], itin>;
89
90 //
91 // EXT-I8 instruction format
92 //
93
94 class FEXT_I816_ins_base<bits<3> _func, string asmstr,
95                          string asmstr2, InstrItinClass itin>:
96   FEXT_I816<_func, (outs), (ins simm16:$imm), !strconcat(asmstr, asmstr2),
97             [], itin>;
98
99 class FEXT_I816_ins<bits<3> _func, string asmstr,
100                     InstrItinClass itin>:
101   FEXT_I816_ins_base<_func, asmstr, "\t$imm", itin>;
102
103 class FEXT_I816_SP_ins<bits<3> _func, string asmstr,
104                        InstrItinClass itin>:
105       FEXT_I816_ins_base<_func, asmstr, "\t$$sp, $imm", itin>;
106
107 //
108 // Assembler formats in alphabetical order.
109 // Natural and pseudos are mixed together.
110 //
111 // Compare two registers and place result in CC
112 // Implicit use of T8
113 //
114 // CC-RR Instruction format
115 //
116 class FCCRR16_ins<bits<5> f, string asmstr, InstrItinClass itin> :
117   FRR16<f, (outs CPU16Regs:$cc), (ins CPU16Regs:$rx, CPU16Regs:$ry),
118         !strconcat(asmstr, "\t$rx, $ry\n\tmove\t$cc, $$t8"), [], itin> {
119   let isCodeGenOnly=1;
120 }
121
122 //
123 // EXT-RI instruction format
124 //
125
126 class FEXT_RI16_ins_base<bits<5> _op, string asmstr, string asmstr2,
127                          InstrItinClass itin>:
128   FEXT_RI16<_op, (outs CPU16Regs:$rx), (ins simm16:$imm),
129                   !strconcat(asmstr, asmstr2), [], itin>;
130
131 class FEXT_RI16_ins<bits<5> _op, string asmstr,
132                     InstrItinClass itin>:
133   FEXT_RI16_ins_base<_op, asmstr, "\t$rx, $imm", itin>;
134
135 class FEXT_RI16_PC_ins<bits<5> _op, string asmstr, InstrItinClass itin>:
136   FEXT_RI16_ins_base<_op, asmstr, "\t$rx, $$pc, $imm", itin>;
137
138 class FEXT_RI16_B_ins<bits<5> _op, string asmstr,
139                       InstrItinClass itin>:
140   FEXT_RI16<_op, (outs), (ins  CPU16Regs:$rx, brtarget:$imm),
141             !strconcat(asmstr, "\t$rx, $imm"), [], itin>;
142
143 class FEXT_2RI16_ins<bits<5> _op, string asmstr,
144                      InstrItinClass itin>:
145   FEXT_RI16<_op, (outs CPU16Regs:$rx), (ins CPU16Regs:$rx_, simm16:$imm),
146             !strconcat(asmstr, "\t$rx, $imm"), [], itin> {
147   let Constraints = "$rx_ = $rx";
148 }
149
150
151 // this has an explicit sp argument that we ignore to work around a problem
152 // in the compiler
153 class FEXT_RI16_SP_explicit_ins<bits<5> _op, string asmstr,
154                                 InstrItinClass itin>:
155   FEXT_RI16<_op, (outs CPU16Regs:$rx), (ins CPUSPReg:$ry, simm16:$imm),
156             !strconcat(asmstr, "\t$rx, $imm ( $ry ); "), [], itin>;
157
158 //
159 // EXT-RRI instruction format
160 //
161
162 class FEXT_RRI16_mem_ins<bits<5> op, string asmstr, Operand MemOpnd,
163                          InstrItinClass itin>:
164   FEXT_RRI16<op, (outs CPU16Regs:$ry), (ins  MemOpnd:$addr),
165              !strconcat(asmstr, "\t$ry, $addr"), [], itin>;
166
167 class FEXT_RRI16_mem2_ins<bits<5> op, string asmstr, Operand MemOpnd,
168                           InstrItinClass itin>:
169   FEXT_RRI16<op, (outs ), (ins  CPU16Regs:$ry, MemOpnd:$addr),
170              !strconcat(asmstr, "\t$ry, $addr"), [], itin>;
171
172 //
173 //
174 // EXT-RRI-A instruction format
175 //
176
177 class FEXT_RRI_A16_mem_ins<bits<1> op, string asmstr, Operand MemOpnd,
178                            InstrItinClass itin>:
179   FEXT_RRI_A16<op, (outs CPU16Regs:$ry), (ins  MemOpnd:$addr),
180                !strconcat(asmstr, "\t$ry, $addr"), [], itin>;
181
182 //
183 // EXT-SHIFT instruction format
184 //
185 class FEXT_SHIFT16_ins<bits<2> _f, string asmstr, InstrItinClass itin>:
186   FEXT_SHIFT16<_f, (outs CPU16Regs:$rx), (ins CPU16Regs:$ry, shamt:$sa),
187                !strconcat(asmstr, "\t$rx, $ry, $sa"), [], itin>;
188
189 //
190 // EXT-T8I8
191 //
192 class FEXT_T8I816_ins<bits<3> _func, string asmstr, string asmstr2,
193                       InstrItinClass itin>:
194   FEXT_I816<_func, (outs),
195             (ins CPU16Regs:$rx, CPU16Regs:$ry, brtarget:$imm),
196             !strconcat(asmstr2, !strconcat("\t$rx, $ry\n\t",
197             !strconcat(asmstr, "\t$imm"))),[], itin> {
198   let isCodeGenOnly=1;
199 }
200
201 //
202 // EXT-T8I8I
203 //
204 class FEXT_T8I8I16_ins<bits<3> _func, string asmstr, string asmstr2,
205                        InstrItinClass itin>:
206   FEXT_I816<_func, (outs),
207             (ins CPU16Regs:$rx, simm16:$imm, brtarget:$targ),
208             !strconcat(asmstr2, !strconcat("\t$rx, $imm\n\t",
209             !strconcat(asmstr, "\t$targ"))), [], itin> {
210   let isCodeGenOnly=1;
211 }
212 //
213
214
215 //
216 // I8_MOVR32 instruction format (used only by the MOVR32 instructio
217 //
218 class FI8_MOVR3216_ins<string asmstr, InstrItinClass itin>:
219        FI8_MOVR3216<(outs CPU16Regs:$rz), (ins CPURegs:$r32),
220        !strconcat(asmstr,  "\t$rz, $r32"), [], itin>;
221
222 //
223 // I8_MOV32R instruction format (used only by MOV32R instruction)
224 //
225
226 class FI8_MOV32R16_ins<string asmstr, InstrItinClass itin>:
227   FI8_MOV32R16<(outs CPURegs:$r32), (ins CPU16Regs:$rz),
228                !strconcat(asmstr,  "\t$r32, $rz"), [], itin>;
229
230 //
231 // This are pseudo formats for multiply
232 // This first one can be changed to non pseudo now.
233 //
234 // MULT
235 //
236 class FMULT16_ins<string asmstr, InstrItinClass itin> :
237   MipsPseudo16<(outs), (ins CPU16Regs:$rx, CPU16Regs:$ry),
238                !strconcat(asmstr, "\t$rx, $ry"), []>;
239
240 //
241 // MULT-LO
242 //
243 class FMULT16_LO_ins<string asmstr, InstrItinClass itin> :
244   MipsPseudo16<(outs CPU16Regs:$rz), (ins CPU16Regs:$rx, CPU16Regs:$ry),
245                !strconcat(asmstr, "\t$rx, $ry\n\tmflo\t$rz"), []> {
246   let isCodeGenOnly=1;
247 }
248
249 //
250 // RR-type instruction format
251 //
252
253 class FRR16_ins<bits<5> f, string asmstr, InstrItinClass itin> :
254   FRR16<f, (outs CPU16Regs:$rx), (ins CPU16Regs:$ry),
255         !strconcat(asmstr, "\t$rx, $ry"), [], itin> {
256 }
257
258 class FRRTR16_ins<bits<5> f, string asmstr, InstrItinClass itin> :
259   FRR16<f, (outs CPU16Regs:$rz), (ins CPU16Regs:$rx, CPU16Regs:$ry),
260         !strconcat(asmstr, "\t$rx, $ry\n\tmove\t$rz, $$t8"), [], itin> ;
261
262 //
263 // maybe refactor but need a $zero as a dummy first parameter
264 //
265 class FRR16_div_ins<bits<5> f, string asmstr, InstrItinClass itin> :
266   FRR16<f, (outs ), (ins CPU16Regs:$rx, CPU16Regs:$ry),
267         !strconcat(asmstr, "\t$$zero, $rx, $ry"), [], itin> ;
268
269 class FUnaryRR16_ins<bits<5> f, string asmstr, InstrItinClass itin> :
270   FRR16<f, (outs CPU16Regs:$rx), (ins CPU16Regs:$ry),
271         !strconcat(asmstr, "\t$rx, $ry"), [], itin> ;
272
273
274 class FRR16_M_ins<bits<5> f, string asmstr,
275                   InstrItinClass itin> :
276   FRR16<f, (outs CPU16Regs:$rx), (ins),
277         !strconcat(asmstr, "\t$rx"), [], itin>;
278
279 class FRxRxRy16_ins<bits<5> f, string asmstr,
280                     InstrItinClass itin> :
281   FRR16<f, (outs CPU16Regs:$rz), (ins CPU16Regs:$rx, CPU16Regs:$ry),
282             !strconcat(asmstr, "\t$rz, $ry"),
283             [], itin> {
284   let Constraints = "$rx = $rz";
285 }
286
287 let rx=0 in
288 class FRR16_JALRC_RA_only_ins<bits<1> nd_, bits<1> l_,
289                               string asmstr, InstrItinClass itin>:
290   FRR16_JALRC<nd_, l_, 1, (outs), (ins), !strconcat(asmstr, "\t $$ra"),
291               [], itin> ;
292
293
294 class FRR16_JALRC_ins<bits<1> nd, bits<1> l, bits<1> ra,
295                       string asmstr, InstrItinClass itin>:
296   FRR16_JALRC<nd, l, ra, (outs), (ins CPU16Regs:$rx),
297               !strconcat(asmstr, "\t $rx"), [], itin> ;
298
299 //
300 // RRR-type instruction format
301 //
302
303 class FRRR16_ins<bits<2> _f, string asmstr,  InstrItinClass itin> :
304   FRRR16<_f, (outs CPU16Regs:$rz), (ins CPU16Regs:$rx, CPU16Regs:$ry),
305          !strconcat(asmstr, "\t$rz, $rx, $ry"), [], itin>;
306
307 //
308 // These Sel patterns support the generation of conditional move
309 // pseudo instructions.
310 //
311 // The nomenclature uses the components making up the pseudo and may
312 // be a bit counter intuitive when compared with the end result we seek.
313 // For example using a bqez in the example directly below results in the
314 // conditional move being done if the tested register is not zero.
315 // I considered in easier to check by keeping the pseudo consistent with
316 // it's components but it could have been done differently.
317 //
318 // The simplest case is when can test and operand directly and do the
319 // conditional move based on a simple mips16 conditional
320 //  branch instruction.
321 // for example:
322 // if $op == beqz or bnez:
323 //
324 // $op1 $rt, .+4
325 // move $rd, $rs
326 //
327 // if $op == beqz, then if $rt != 0, then the conditional assignment
328 // $rd = $rs is done.
329
330 // if $op == bnez, then if $rt == 0, then the conditional assignment
331 // $rd = $rs is done.
332 //
333 // So this pseudo class only has one operand, i.e. op
334 //
335 class Sel<bits<5> f1, string op, InstrItinClass itin>:
336   MipsInst16_32<(outs CPU16Regs:$rd_), (ins CPU16Regs:$rd, CPU16Regs:$rs,
337                 CPU16Regs:$rt),
338                 !strconcat(op, "\t$rt, .+4\n\t\n\tmove $rd, $rs"), [], itin,
339                 Pseudo16> {
340   let isCodeGenOnly=1;
341   let Constraints = "$rd = $rd_";
342 }
343
344 //
345 // The next two instruction classes allow for an operand which tests
346 // two operands and returns a value in register T8 and
347 //then does a conditional branch based on the value of T8
348 //
349
350 // op2 can be cmpi or slti/sltiu
351 // op1 can bteqz or btnez
352 // the operands for op2 are a register and a signed constant
353 //
354 // $op2 $t, $imm  ;test register t and branch conditionally
355 // $op1 .+4       ;op1 is a conditional branch
356 // move $rd, $rs
357 //
358 //
359 class SeliT<bits<5> f1, string op1, bits<5> f2, string op2,
360                  InstrItinClass itin>:
361   MipsInst16_32<(outs CPU16Regs:$rd_), (ins CPU16Regs:$rd, CPU16Regs:$rs,
362                                         CPU16Regs:$rl, simm16:$imm),
363                  !strconcat(op2,
364                  !strconcat("\t$rl, $imm\n\t",
365                  !strconcat(op1, "\t.+4\n\tmove $rd, $rs"))), [], itin,
366                  Pseudo16> {
367   let isCodeGenOnly=1;
368   let Constraints = "$rd = $rd_";
369 }
370
371 //
372 // op2 can be cmp or slt/sltu
373 // op1 can be bteqz or btnez
374 // the operands for op2 are two registers
375 // op1 is a conditional branch
376 //
377 //
378 // $op2 $rl, $rr  ;test registers rl,rr
379 // $op1 .+4       ;op2 is a conditional branch
380 // move $rd, $rs
381 //
382 //
383 class SelT<bits<5> f1, string op1, bits<5> f2, string op2,
384            InstrItinClass itin>:
385   MipsInst16_32<(outs CPU16Regs:$rd_), (ins CPU16Regs:$rd, CPU16Regs:$rs,
386                 CPU16Regs:$rl, CPU16Regs:$rr),
387                 !strconcat(op2,
388                 !strconcat("\t$rl, $rr\n\t",
389                 !strconcat(op1, "\t.+4\n\tmove $rd, $rs"))), [], itin,
390                 Pseudo16> {
391   let isCodeGenOnly=1;
392   let Constraints = "$rd = $rd_";
393 }
394
395 //
396 // 32 bit constant
397 //
398 def imm32: Operand<i32>;
399
400 def Constant32:
401   MipsPseudo16<(outs), (ins imm32:$imm), "\t.word $imm", []>;
402
403 def LwConstant32:
404   MipsPseudo16<(outs), (ins CPU16Regs:$rx, imm32:$imm),
405     "lw\t$rx, 1f\n\tb\t2f\n\t.align\t2\n1: \t.word\t$imm\n2:", []>;
406
407
408 //
409 // Some general instruction class info
410 //
411 //
412
413 class ArithLogic16Defs<bit isCom=0> {
414   bits<5> shamt = 0;
415   bit isCommutable = isCom;
416   bit isReMaterializable = 1;
417   bit neverHasSideEffects = 1;
418 }
419
420 class branch16 {
421   bit isBranch = 1;
422   bit isTerminator = 1;
423   bit isBarrier = 1;
424 }
425
426 class cbranch16 {
427   bit isBranch = 1;
428   bit isTerminator = 1;
429 }
430
431 class MayLoad {
432   bit mayLoad = 1;
433 }
434
435 class MayStore {
436   bit mayStore = 1;
437 }
438 //
439
440 // Format: ADDIU rx, immediate MIPS16e
441 // Purpose: Add Immediate Unsigned Word (2-Operand, Extended)
442 // To add a constant to a 32-bit integer.
443 //
444 def AddiuRxImmX16: FEXT_RI16_ins<0b01001, "addiu", IIAlu>;
445
446 def AddiuRxRxImm16: F2RI16_ins<0b01001, "addiu", IIAlu>,
447   ArithLogic16Defs<0> {
448   let AddedComplexity = 5;
449 }
450 def AddiuRxRxImmX16: FEXT_2RI16_ins<0b01001, "addiu", IIAlu>,
451   ArithLogic16Defs<0>;
452
453 def AddiuRxRyOffMemX16:
454   FEXT_RRI_A16_mem_ins<0, "addiu", mem16_ea, IIAlu>;
455
456 //
457
458 // Format: ADDIU rx, pc, immediate MIPS16e
459 // Purpose: Add Immediate Unsigned Word (3-Operand, PC-Relative, Extended)
460 // To add a constant to the program counter.
461 //
462 def AddiuRxPcImmX16: FEXT_RI16_PC_ins<0b00001, "addiu", IIAlu>;
463
464 //
465 // Format: ADDIU sp, immediate MIPS16e
466 // Purpose: Add Immediate Unsigned Word (2-Operand, SP-Relative, Extended)
467 // To add a constant to the stack pointer.
468 //
469 def AddiuSpImm16
470   : FI816_SP_ins<0b011, "addiu", IIAlu> {
471   let Defs = [SP];
472   let Uses = [SP];
473   let AddedComplexity = 5;
474 }
475
476 def AddiuSpImmX16
477   : FEXT_I816_SP_ins<0b011, "addiu", IIAlu> {
478   let Defs = [SP];
479   let Uses = [SP];
480 }
481
482 //
483 // Format: ADDU rz, rx, ry MIPS16e
484 // Purpose: Add Unsigned Word (3-Operand)
485 // To add 32-bit integers.
486 //
487
488 def AdduRxRyRz16: FRRR16_ins<01, "addu", IIAlu>, ArithLogic16Defs<1>;
489
490 //
491 // Format: AND rx, ry MIPS16e
492 // Purpose: AND
493 // To do a bitwise logical AND.
494
495 def AndRxRxRy16: FRxRxRy16_ins<0b01100, "and", IIAlu>, ArithLogic16Defs<1>;
496
497
498 //
499 // Format: BEQZ rx, offset MIPS16e
500 // Purpose: Branch on Equal to Zero (Extended)
501 // To test a GPR then do a PC-relative conditional branch.
502 //
503 def BeqzRxImmX16: FEXT_RI16_B_ins<0b00100, "beqz", IIAlu>, cbranch16;
504
505 // Format: B offset MIPS16e
506 // Purpose: Unconditional Branch
507 // To do an unconditional PC-relative branch.
508 //
509 def BimmX16: FEXT_I16_ins<0b00010, "b", IIAlu>, branch16;
510
511 //
512 // Format: BNEZ rx, offset MIPS16e
513 // Purpose: Branch on Not Equal to Zero (Extended)
514 // To test a GPR then do a PC-relative conditional branch.
515 //
516 def BnezRxImmX16: FEXT_RI16_B_ins<0b00101, "bnez", IIAlu>, cbranch16;
517
518 //
519 // Format: BTEQZ offset MIPS16e
520 // Purpose: Branch on T Equal to Zero (Extended)
521 // To test special register T then do a PC-relative conditional branch.
522 //
523 def BteqzX16: FEXT_I816_ins<0b000, "bteqz", IIAlu>, cbranch16;
524
525 def BteqzT8CmpX16: FEXT_T8I816_ins<0b000, "bteqz", "cmp", IIAlu>, cbranch16;
526
527 def BteqzT8CmpiX16: FEXT_T8I8I16_ins<0b000, "bteqz", "cmpi", IIAlu>,
528   cbranch16;
529
530 def BteqzT8SltX16: FEXT_T8I816_ins<0b000, "bteqz", "slt", IIAlu>, cbranch16;
531
532 def BteqzT8SltuX16: FEXT_T8I816_ins<0b000, "bteqz", "sltu", IIAlu>, cbranch16;
533
534 def BteqzT8SltiX16: FEXT_T8I8I16_ins<0b000, "bteqz", "slti", IIAlu>, cbranch16;
535
536 def BteqzT8SltiuX16: FEXT_T8I8I16_ins<0b000, "bteqz", "sltiu", IIAlu>,
537   cbranch16;
538
539 //
540 // Format: BTNEZ offset MIPS16e
541 // Purpose: Branch on T Not Equal to Zero (Extended)
542 // To test special register T then do a PC-relative conditional branch.
543 //
544 def BtnezX16: FEXT_I816_ins<0b001, "btnez", IIAlu> ,cbranch16;
545
546 def BtnezT8CmpX16: FEXT_T8I816_ins<0b000, "btnez", "cmp", IIAlu>, cbranch16;
547
548 def BtnezT8CmpiX16: FEXT_T8I8I16_ins<0b000, "btnez", "cmpi", IIAlu>, cbranch16;
549
550 def BtnezT8SltX16: FEXT_T8I816_ins<0b000, "btnez", "slt", IIAlu>, cbranch16;
551
552 def BtnezT8SltuX16: FEXT_T8I816_ins<0b000, "btnez", "sltu", IIAlu>, cbranch16;
553
554 def BtnezT8SltiX16: FEXT_T8I8I16_ins<0b000, "btnez", "slti", IIAlu>, cbranch16;
555
556 def BtnezT8SltiuX16: FEXT_T8I8I16_ins<0b000, "btnez", "sltiu", IIAlu>,
557   cbranch16;
558
559 //
560 // Format: DIV rx, ry MIPS16e
561 // Purpose: Divide Word
562 // To divide 32-bit signed integers.
563 //
564 def DivRxRy16: FRR16_div_ins<0b11010, "div", IIAlu> {
565   let Defs = [HI, LO];
566 }
567
568 //
569 // Format: DIVU rx, ry MIPS16e
570 // Purpose: Divide Unsigned Word
571 // To divide 32-bit unsigned integers.
572 //
573 def DivuRxRy16: FRR16_div_ins<0b11011, "divu", IIAlu> {
574   let Defs = [HI, LO];
575 }
576 //
577 // Format: JAL target MIPS16e
578 // Purpose: Jump and Link
579 // To execute a procedure call within the current 256 MB-aligned
580 // region and preserve the current ISA.
581 //
582
583 def Jal16 : FJAL16_ins<0b0, "jal", IIAlu> {
584   let isBranch = 1;
585   let hasDelaySlot = 0;  // not true, but we add the nop for now
586   let isTerminator=1;
587   let isBarrier=1;
588 }
589
590 //
591 // Format: JR ra MIPS16e
592 // Purpose: Jump Register Through Register ra
593 // To execute a branch to the instruction address in the return
594 // address register.
595 //
596
597 def JrRa16: FRR16_JALRC_RA_only_ins<0, 0, "jr", IIAlu> {
598   let isBranch = 1;
599   let isIndirectBranch = 1;
600   let hasDelaySlot = 1;
601   let isTerminator=1;
602   let isBarrier=1;
603 }
604
605 def JrcRa16: FRR16_JALRC_RA_only_ins<0, 0, "jrc", IIAlu> {
606   let isBranch = 1;
607   let isIndirectBranch = 1;
608   let isTerminator=1;
609   let isBarrier=1;
610 }
611
612 def JrcRx16: FRR16_JALRC_ins<1, 1, 0, "jrc", IIAlu> {
613   let isBranch = 1;
614   let isIndirectBranch = 1;
615   let isTerminator=1;
616   let isBarrier=1;
617 }
618 //
619 // Format: LB ry, offset(rx) MIPS16e
620 // Purpose: Load Byte (Extended)
621 // To load a byte from memory as a signed value.
622 //
623 def LbRxRyOffMemX16: FEXT_RRI16_mem_ins<0b10011, "lb", mem16, IILoad>, MayLoad;
624
625 //
626 // Format: LBU ry, offset(rx) MIPS16e
627 // Purpose: Load Byte Unsigned (Extended)
628 // To load a byte from memory as a unsigned value.
629 //
630 def LbuRxRyOffMemX16:
631   FEXT_RRI16_mem_ins<0b10100, "lbu", mem16, IILoad>, MayLoad;
632
633 //
634 // Format: LH ry, offset(rx) MIPS16e
635 // Purpose: Load Halfword signed (Extended)
636 // To load a halfword from memory as a signed value.
637 //
638 def LhRxRyOffMemX16: FEXT_RRI16_mem_ins<0b10100, "lh", mem16, IILoad>, MayLoad;
639
640 //
641 // Format: LHU ry, offset(rx) MIPS16e
642 // Purpose: Load Halfword unsigned (Extended)
643 // To load a halfword from memory as an unsigned value.
644 //
645 def LhuRxRyOffMemX16:
646   FEXT_RRI16_mem_ins<0b10100, "lhu", mem16, IILoad>, MayLoad;
647
648 //
649 // Format: LI rx, immediate MIPS16e
650 // Purpose: Load Immediate (Extended)
651 // To load a constant into a GPR.
652 //
653 def LiRxImmX16: FEXT_RI16_ins<0b01101, "li", IIAlu>;
654
655 //
656 // Format: LW ry, offset(rx) MIPS16e
657 // Purpose: Load Word (Extended)
658 // To load a word from memory as a signed value.
659 //
660 def LwRxRyOffMemX16: FEXT_RRI16_mem_ins<0b10011, "lw", mem16, IILoad>, MayLoad;
661
662 // Format: LW rx, offset(sp) MIPS16e
663 // Purpose: Load Word (SP-Relative, Extended)
664 // To load an SP-relative word from memory as a signed value.
665 //
666 def LwRxSpImmX16: FEXT_RI16_SP_explicit_ins<0b10110, "lw", IILoad>, MayLoad{
667   let Uses = [SP];
668 }
669
670 //
671 // Format: MOVE r32, rz MIPS16e
672 // Purpose: Move
673 // To move the contents of a GPR to a GPR.
674 //
675 def Move32R16: FI8_MOV32R16_ins<"move", IIAlu>;
676
677 //
678 // Format: MOVE ry, r32 MIPS16e
679 //Purpose: Move
680 // To move the contents of a GPR to a GPR.
681 //
682 def MoveR3216: FI8_MOVR3216_ins<"move", IIAlu>;
683
684 //
685 // Format: MFHI rx MIPS16e
686 // Purpose: Move From HI Register
687 // To copy the special purpose HI register to a GPR.
688 //
689 def Mfhi16: FRR16_M_ins<0b10000, "mfhi", IIAlu> {
690   let Uses = [HI];
691   let neverHasSideEffects = 1;
692 }
693
694 //
695 // Format: MFLO rx MIPS16e
696 // Purpose: Move From LO Register
697 // To copy the special purpose LO register to a GPR.
698 //
699 def Mflo16: FRR16_M_ins<0b10010, "mflo", IIAlu> {
700   let Uses = [LO];
701   let neverHasSideEffects = 1;
702 }
703
704 //
705 // Pseudo Instruction for mult
706 //
707 def MultRxRy16:  FMULT16_ins<"mult",  IIAlu> {
708   let isCommutable = 1;
709   let neverHasSideEffects = 1;
710   let Defs = [HI, LO];
711 }
712
713 def MultuRxRy16: FMULT16_ins<"multu", IIAlu> {
714   let isCommutable = 1;
715   let neverHasSideEffects = 1;
716   let Defs = [HI, LO];
717 }
718
719 //
720 // Format: MULT rx, ry MIPS16e
721 // Purpose: Multiply Word
722 // To multiply 32-bit signed integers.
723 //
724 def MultRxRyRz16: FMULT16_LO_ins<"mult", IIAlu> {
725   let isCommutable = 1;
726   let neverHasSideEffects = 1;
727   let Defs = [HI, LO];
728 }
729
730 //
731 // Format: MULTU rx, ry MIPS16e
732 // Purpose: Multiply Unsigned Word
733 // To multiply 32-bit unsigned integers.
734 //
735 def MultuRxRyRz16: FMULT16_LO_ins<"multu", IIAlu> {
736   let isCommutable = 1;
737   let neverHasSideEffects = 1;
738   let Defs = [HI, LO];
739 }
740
741 //
742 // Format: NEG rx, ry MIPS16e
743 // Purpose: Negate
744 // To negate an integer value.
745 //
746 def NegRxRy16: FUnaryRR16_ins<0b11101, "neg", IIAlu>;
747
748 //
749 // Format: NOT rx, ry MIPS16e
750 // Purpose: Not
751 // To complement an integer value
752 //
753 def NotRxRy16: FUnaryRR16_ins<0b01111, "not", IIAlu>;
754
755 //
756 // Format: OR rx, ry MIPS16e
757 // Purpose: Or
758 // To do a bitwise logical OR.
759 //
760 def OrRxRxRy16: FRxRxRy16_ins<0b01101, "or", IIAlu>, ArithLogic16Defs<1>;
761
762 //
763 // Format: RESTORE {ra,}{s0/s1/s0-1,}{framesize}
764 // (All args are optional) MIPS16e
765 // Purpose: Restore Registers and Deallocate Stack Frame
766 // To deallocate a stack frame before exit from a subroutine,
767 // restoring return address and static registers, and adjusting
768 // stack
769 //
770
771 // fixed form for restoring RA and the frame
772 // for direct object emitter, encoding needs to be adjusted for the
773 // frame size
774 //
775 let ra=1, s=0,s0=1,s1=1 in
776 def RestoreRaF16:
777   FI8_SVRS16<0b1, (outs), (ins uimm16:$frame_size),
778              "restore\t$$ra,  $$s0, $$s1, $frame_size", [], IILoad >, MayLoad {
779   let isCodeGenOnly = 1;
780   let Defs = [S0, S1, RA, SP];
781   let Uses = [SP];
782 }
783
784 // Use Restore to increment SP since SP is not a Mip 16 register, this
785 // is an easy way to do that which does not require a register.
786 //
787 let ra=0, s=0,s0=0,s1=0 in
788 def RestoreIncSpF16:
789   FI8_SVRS16<0b1, (outs), (ins uimm16:$frame_size),
790              "restore\t$frame_size", [], IILoad >, MayLoad {
791   let isCodeGenOnly = 1;
792   let Defs = [SP];
793   let Uses = [SP];
794 }
795
796 //
797 // Format: SAVE {ra,}{s0/s1/s0-1,}{framesize} (All arguments are optional)
798 // MIPS16e
799 // Purpose: Save Registers and Set Up Stack Frame
800 // To set up a stack frame on entry to a subroutine,
801 // saving return address and static registers, and adjusting stack
802 //
803 let ra=1, s=1,s0=1,s1=1 in
804 def SaveRaF16:
805   FI8_SVRS16<0b1, (outs), (ins uimm16:$frame_size),
806              "save\t$$ra, $$s0, $$s1, $frame_size", [], IIStore >, MayStore {
807   let isCodeGenOnly = 1;
808   let Uses = [RA, SP, S0, S1];
809   let Defs = [SP];
810 }
811
812 //
813 // Use Save to decrement the SP by a constant since SP is not
814 // a Mips16 register.
815 //
816 let ra=0, s=0,s0=0,s1=0 in
817 def SaveDecSpF16:
818   FI8_SVRS16<0b1, (outs), (ins uimm16:$frame_size),
819              "save\t$frame_size", [], IIStore >, MayStore {
820   let isCodeGenOnly = 1;
821   let Uses = [SP];
822   let Defs = [SP];
823 }
824 //
825 // Format: SB ry, offset(rx) MIPS16e
826 // Purpose: Store Byte (Extended)
827 // To store a byte to memory.
828 //
829 def SbRxRyOffMemX16:
830   FEXT_RRI16_mem2_ins<0b11000, "sb", mem16, IIStore>, MayStore;
831
832 //
833 // The Sel(T) instructions are pseudos
834 // T means that they use T8 implicitly.
835 //
836 //
837 // Format: SelBeqZ rd, rs, rt
838 // Purpose: if rt==0, do nothing
839 //          else rs = rt
840 //
841 def SelBeqZ: Sel<0b00100, "beqz", IIAlu>;
842
843 //
844 // Format:  SelTBteqZCmp rd, rs, rl, rr
845 // Purpose: b = Cmp rl, rr.
846 //          If b==0 then do nothing.
847 //          if b!=0 then rd = rs
848 //
849 def SelTBteqZCmp: SelT<0b000, "bteqz", 0b01010, "cmp", IIAlu>;
850
851 //
852 // Format:  SelTBteqZCmpi rd, rs, rl, rr
853 // Purpose: b = Cmpi rl, imm.
854 //          If b==0 then do nothing.
855 //          if b!=0 then rd = rs
856 //
857 def SelTBteqZCmpi: SeliT<0b000, "bteqz", 0b01110, "cmpi", IIAlu>;
858
859 //
860 // Format:  SelTBteqZSlt rd, rs, rl, rr
861 // Purpose: b = Slt rl, rr.
862 //          If b==0 then do nothing.
863 //          if b!=0 then rd = rs
864 //
865 def SelTBteqZSlt: SelT<0b000, "bteqz", 0b00010, "slt", IIAlu>;
866
867 //
868 // Format:  SelTBteqZSlti rd, rs, rl, rr
869 // Purpose: b = Slti rl, imm.
870 //          If b==0 then do nothing.
871 //          if b!=0 then rd = rs
872 //
873 def SelTBteqZSlti: SeliT<0b000, "bteqz", 0b01010, "slti", IIAlu>;
874
875 //
876 // Format:  SelTBteqZSltu rd, rs, rl, rr
877 // Purpose: b = Sltu rl, rr.
878 //          If b==0 then do nothing.
879 //          if b!=0 then rd = rs
880 //
881 def SelTBteqZSltu: SelT<0b000, "bteqz", 0b00011, "sltu", IIAlu>;
882
883 //
884 // Format:  SelTBteqZSltiu rd, rs, rl, rr
885 // Purpose: b = Sltiu rl, imm.
886 //          If b==0 then do nothing.
887 //          if b!=0 then rd = rs
888 //
889 def SelTBteqZSltiu: SeliT<0b000, "bteqz", 0b01011, "sltiu", IIAlu>;
890
891 //
892 // Format: SelBnez rd, rs, rt
893 // Purpose: if rt!=0, do nothing
894 //          else rs = rt
895 //
896 def SelBneZ: Sel<0b00101, "bnez", IIAlu>;
897
898 //
899 // Format:  SelTBtneZCmp rd, rs, rl, rr
900 // Purpose: b = Cmp rl, rr.
901 //          If b!=0 then do nothing.
902 //          if b0=0 then rd = rs
903 //
904 def SelTBtneZCmp: SelT<0b001, "btnez", 0b01010, "cmp", IIAlu>;
905
906 //
907 // Format:  SelTBtnezCmpi rd, rs, rl, rr
908 // Purpose: b = Cmpi rl, imm.
909 //          If b!=0 then do nothing.
910 //          if b==0 then rd = rs
911 //
912 def SelTBtneZCmpi: SeliT<0b000, "btnez", 0b01110, "cmpi", IIAlu>;
913
914 //
915 // Format:  SelTBtneZSlt rd, rs, rl, rr
916 // Purpose: b = Slt rl, rr.
917 //          If b!=0 then do nothing.
918 //          if b==0 then rd = rs
919 //
920 def SelTBtneZSlt: SelT<0b001, "btnez", 0b00010, "slt", IIAlu>;
921
922 //
923 // Format:  SelTBtneZSlti rd, rs, rl, rr
924 // Purpose: b = Slti rl, imm.
925 //          If b!=0 then do nothing.
926 //          if b==0 then rd = rs
927 //
928 def SelTBtneZSlti: SeliT<0b001, "btnez", 0b01010, "slti", IIAlu>;
929
930 //
931 // Format:  SelTBtneZSltu rd, rs, rl, rr
932 // Purpose: b = Sltu rl, rr.
933 //          If b!=0 then do nothing.
934 //          if b==0 then rd = rs
935 //
936 def SelTBtneZSltu: SelT<0b001, "btnez", 0b00011, "sltu", IIAlu>;
937
938 //
939 // Format:  SelTBtneZSltiu rd, rs, rl, rr
940 // Purpose: b = Slti rl, imm.
941 //          If b!=0 then do nothing.
942 //          if b==0 then rd = rs
943 //
944 def SelTBtneZSltiu: SeliT<0b001, "btnez", 0b01011, "sltiu", IIAlu>;
945 //
946 //
947 // Format: SH ry, offset(rx) MIPS16e
948 // Purpose: Store Halfword (Extended)
949 // To store a halfword to memory.
950 //
951 def ShRxRyOffMemX16:
952   FEXT_RRI16_mem2_ins<0b11001, "sh", mem16, IIStore>, MayStore;
953
954 //
955 // Format: SLL rx, ry, sa MIPS16e
956 // Purpose: Shift Word Left Logical (Extended)
957 // To execute a left-shift of a word by a fixed number of bits—0 to 31 bits.
958 //
959 def SllX16: FEXT_SHIFT16_ins<0b00, "sll", IIAlu>;
960
961 //
962 // Format: SLLV ry, rx MIPS16e
963 // Purpose: Shift Word Left Logical Variable
964 // To execute a left-shift of a word by a variable number of bits.
965 //
966 def SllvRxRy16 : FRxRxRy16_ins<0b00100, "sllv", IIAlu>;
967
968 //
969 // Format: SLTI rx, immediate MIPS16e
970 // Purpose: Set on Less Than Immediate (Extended)
971 // To record the result of a less-than comparison with a constant.
972 //
973 def SltiCCRxImmX16: FEXT_CCRXI16_ins<0b01010, "slti", IIAlu>;
974
975 //
976 // Format: SLTIU rx, immediate MIPS16e
977 // Purpose: Set on Less Than Immediate Unsigned (Extended)
978 // To record the result of a less-than comparison with a constant.
979 //
980 def SltiuCCRxImmX16: FEXT_CCRXI16_ins<0b01011, "sltiu", IIAlu>;
981
982 //
983 // Format: SLT rx, ry MIPS16e
984 // Purpose: Set on Less Than
985 // To record the result of a less-than comparison.
986 //
987 def SltRxRy16: FRR16_ins<0b00010, "slt", IIAlu>;
988
989 def SltCCRxRy16: FCCRR16_ins<0b00010, "slt", IIAlu>;
990
991 // Format: SLTU rx, ry MIPS16e
992 // Purpose: Set on Less Than Unsigned
993 // To record the result of an unsigned less-than comparison.
994 //
995 def SltuRxRyRz16: FRRTR16_ins<0b00011, "sltu", IIAlu> {
996   let isCodeGenOnly=1;
997 }
998
999
1000 def SltuCCRxRy16: FCCRR16_ins<0b00011, "sltu", IIAlu>;
1001 //
1002 // Format: SRAV ry, rx MIPS16e
1003 // Purpose: Shift Word Right Arithmetic Variable
1004 // To execute an arithmetic right-shift of a word by a variable
1005 // number of bits.
1006 //
1007 def SravRxRy16: FRxRxRy16_ins<0b00111, "srav", IIAlu>;
1008
1009
1010 //
1011 // Format: SRA rx, ry, sa MIPS16e
1012 // Purpose: Shift Word Right Arithmetic (Extended)
1013 // To execute an arithmetic right-shift of a word by a fixed
1014 // number of bits—1 to 8 bits.
1015 //
1016 def SraX16: FEXT_SHIFT16_ins<0b11, "sra", IIAlu>;
1017
1018
1019 //
1020 // Format: SRLV ry, rx MIPS16e
1021 // Purpose: Shift Word Right Logical Variable
1022 // To execute a logical right-shift of a word by a variable
1023 // number of bits.
1024 //
1025 def SrlvRxRy16: FRxRxRy16_ins<0b00110, "srlv", IIAlu>;
1026
1027
1028 //
1029 // Format: SRL rx, ry, sa MIPS16e
1030 // Purpose: Shift Word Right Logical (Extended)
1031 // To execute a logical right-shift of a word by a fixed
1032 // number of bits—1 to 31 bits.
1033 //
1034 def SrlX16: FEXT_SHIFT16_ins<0b10, "srl", IIAlu>;
1035
1036 //
1037 // Format: SUBU rz, rx, ry MIPS16e
1038 // Purpose: Subtract Unsigned Word
1039 // To subtract 32-bit integers
1040 //
1041 def SubuRxRyRz16: FRRR16_ins<0b11, "subu", IIAlu>, ArithLogic16Defs<0>;
1042
1043 //
1044 // Format: SW ry, offset(rx) MIPS16e
1045 // Purpose: Store Word (Extended)
1046 // To store a word to memory.
1047 //
1048 def SwRxRyOffMemX16:
1049   FEXT_RRI16_mem2_ins<0b11011, "sw", mem16, IIStore>, MayStore;
1050
1051 //
1052 // Format: SW rx, offset(sp) MIPS16e
1053 // Purpose: Store Word rx (SP-Relative)
1054 // To store an SP-relative word to memory.
1055 //
1056 def SwRxSpImmX16: FEXT_RI16_SP_explicit_ins<0b11010, "sw", IIStore>, MayStore;
1057
1058 //
1059 //
1060 // Format: XOR rx, ry MIPS16e
1061 // Purpose: Xor
1062 // To do a bitwise logical XOR.
1063 //
1064 def XorRxRxRy16: FRxRxRy16_ins<0b01110, "xor", IIAlu>, ArithLogic16Defs<1>;
1065
1066 class Mips16Pat<dag pattern, dag result> : Pat<pattern, result> {
1067   let Predicates = [InMips16Mode];
1068 }
1069
1070 // Unary Arith/Logic
1071 //
1072 class ArithLogicU_pat<PatFrag OpNode, Instruction I> :
1073   Mips16Pat<(OpNode CPU16Regs:$r),
1074             (I CPU16Regs:$r)>;
1075
1076 def: ArithLogicU_pat<not, NotRxRy16>;
1077 def: ArithLogicU_pat<ineg, NegRxRy16>;
1078
1079 class ArithLogic16_pat<SDNode OpNode, Instruction I> :
1080   Mips16Pat<(OpNode CPU16Regs:$l, CPU16Regs:$r),
1081             (I CPU16Regs:$l, CPU16Regs:$r)>;
1082
1083 def: ArithLogic16_pat<add, AdduRxRyRz16>;
1084 def: ArithLogic16_pat<and, AndRxRxRy16>;
1085 def: ArithLogic16_pat<mul, MultRxRyRz16>;
1086 def: ArithLogic16_pat<or, OrRxRxRy16>;
1087 def: ArithLogic16_pat<sub, SubuRxRyRz16>;
1088 def: ArithLogic16_pat<xor, XorRxRxRy16>;
1089
1090 // Arithmetic and logical instructions with 2 register operands.
1091
1092 class ArithLogicI16_pat<SDNode OpNode, PatFrag imm_type, Instruction I> :
1093   Mips16Pat<(OpNode CPU16Regs:$in, imm_type:$imm),
1094             (I CPU16Regs:$in, imm_type:$imm)>;
1095
1096 def: ArithLogicI16_pat<add, immSExt8, AddiuRxRxImm16>;
1097 def: ArithLogicI16_pat<add, immSExt16, AddiuRxRxImmX16>;
1098 def: ArithLogicI16_pat<shl, immZExt5, SllX16>;
1099 def: ArithLogicI16_pat<srl, immZExt5, SrlX16>;
1100 def: ArithLogicI16_pat<sra, immZExt5, SraX16>;
1101
1102 class shift_rotate_reg16_pat<SDNode OpNode, Instruction I> :
1103   Mips16Pat<(OpNode CPU16Regs:$r, CPU16Regs:$ra),
1104             (I CPU16Regs:$r, CPU16Regs:$ra)>;
1105
1106 def: shift_rotate_reg16_pat<shl, SllvRxRy16>;
1107 def: shift_rotate_reg16_pat<sra, SravRxRy16>;
1108 def: shift_rotate_reg16_pat<srl, SrlvRxRy16>;
1109
1110 class LoadM16_pat<PatFrag OpNode, Instruction I> :
1111   Mips16Pat<(OpNode addr16:$addr), (I addr16:$addr)>;
1112
1113 def: LoadM16_pat<sextloadi8, LbRxRyOffMemX16>;
1114 def: LoadM16_pat<zextloadi8, LbuRxRyOffMemX16>;
1115 def: LoadM16_pat<sextloadi16, LhRxRyOffMemX16>;
1116 def: LoadM16_pat<zextloadi16, LhuRxRyOffMemX16>;
1117 def: LoadM16_pat<load, LwRxRyOffMemX16>;
1118
1119 class StoreM16_pat<PatFrag OpNode, Instruction I> :
1120   Mips16Pat<(OpNode CPU16Regs:$r, addr16:$addr),
1121             (I CPU16Regs:$r, addr16:$addr)>;
1122
1123 def: StoreM16_pat<truncstorei8, SbRxRyOffMemX16>;
1124 def: StoreM16_pat<truncstorei16, ShRxRyOffMemX16>;
1125 def: StoreM16_pat<store, SwRxRyOffMemX16>;
1126
1127 // Unconditional branch
1128 class UncondBranch16_pat<SDNode OpNode, Instruction I>:
1129   Mips16Pat<(OpNode bb:$imm16), (I bb:$imm16)> {
1130     let Predicates = [InMips16Mode];
1131   }
1132
1133 def : Mips16Pat<(MipsJmpLink (i32 tglobaladdr:$dst)),
1134                 (Jal16 tglobaladdr:$dst)>;
1135
1136 def : Mips16Pat<(MipsJmpLink (i32 texternalsym:$dst)),
1137                 (Jal16 texternalsym:$dst)>;
1138
1139 // Indirect branch
1140 def: Mips16Pat<
1141   (brind CPU16Regs:$rs),
1142   (JrcRx16 CPU16Regs:$rs)>;
1143
1144 // Jump and Link (Call)
1145 let isCall=1, hasDelaySlot=0 in
1146 def JumpLinkReg16:
1147   FRR16_JALRC<0, 0, 0, (outs), (ins CPU16Regs:$rs),
1148               "jalrc \t$rs", [(MipsJmpLink CPU16Regs:$rs)], IIBranch>;
1149
1150 // Mips16 pseudos
1151 let isReturn=1, isTerminator=1, hasDelaySlot=1, isBarrier=1, hasCtrlDep=1,
1152   hasExtraSrcRegAllocReq = 1 in
1153 def RetRA16 : MipsPseudo16<(outs), (ins), "", [(MipsRet)]>;
1154
1155
1156 // setcc patterns
1157
1158 class SetCC_R16<PatFrag cond_op, Instruction I>:
1159   Mips16Pat<(cond_op CPU16Regs:$rx, CPU16Regs:$ry),
1160             (I CPU16Regs:$rx, CPU16Regs:$ry)>;
1161
1162 class SetCC_I16<PatFrag cond_op, PatLeaf imm_type, Instruction I>:
1163   Mips16Pat<(cond_op CPU16Regs:$rx, imm_type:$imm16),
1164             (I CPU16Regs:$rx, imm_type:$imm16)>;
1165
1166
1167 def: Mips16Pat<(i32  addr16:$addr),
1168                (AddiuRxRyOffMemX16  addr16:$addr)>;
1169
1170
1171 // Large (>16 bit) immediate loads
1172 def : Mips16Pat<(i32 imm:$imm),
1173                 (OrRxRxRy16 (SllX16 (LiRxImmX16 (HI16 imm:$imm)), 16),
1174                 (LiRxImmX16 (LO16 imm:$imm)))>;
1175
1176 // Carry MipsPatterns
1177 def : Mips16Pat<(subc CPU16Regs:$lhs, CPU16Regs:$rhs),
1178                 (SubuRxRyRz16 CPU16Regs:$lhs, CPU16Regs:$rhs)>;
1179 def : Mips16Pat<(addc CPU16Regs:$lhs, CPU16Regs:$rhs),
1180                 (AdduRxRyRz16 CPU16Regs:$lhs, CPU16Regs:$rhs)>;
1181 def : Mips16Pat<(addc  CPU16Regs:$src, immSExt16:$imm),
1182                 (AddiuRxRxImmX16 CPU16Regs:$src, imm:$imm)>;
1183
1184 //
1185 // Some branch conditional patterns are not generated by llvm at this time.
1186 // Some are for seemingly arbitrary reasons not used: i.e. with signed number
1187 // comparison they are used and for unsigned a different pattern is used.
1188 // I am pushing upstream from the full mips16 port and it seemed that I needed
1189 // these earlier and the mips32 port has these but now I cannot create test
1190 // cases that use these patterns. While I sort this all out I will leave these
1191 // extra patterns commented out and if I can be sure they are really not used,
1192 // I will delete the code. I don't want to check the code in uncommented without
1193 // a valid test case. In some cases, the compiler is generating patterns with
1194 // setcc instead and earlier I had implemented setcc first so may have masked
1195 // the problem. The setcc variants are suboptimal for mips16 so I may wantto
1196 // figure out how to enable the brcond patterns or else possibly new
1197 // combinations of of brcond and setcc.
1198 //
1199 //
1200 // bcond-seteq
1201 //
1202 def: Mips16Pat
1203   <(brcond (i32 (seteq CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1204    (BteqzT8CmpX16 CPU16Regs:$rx, CPU16Regs:$ry,  bb:$imm16)
1205   >;
1206
1207
1208 def: Mips16Pat
1209   <(brcond (i32 (seteq CPU16Regs:$rx, immZExt16:$imm)), bb:$targ16),
1210    (BteqzT8CmpiX16 CPU16Regs:$rx, immSExt16:$imm,  bb:$targ16)
1211   >;
1212
1213 def: Mips16Pat
1214   <(brcond (i32 (seteq CPU16Regs:$rx, 0)), bb:$targ16),
1215    (BeqzRxImmX16 CPU16Regs:$rx, bb:$targ16)
1216   >;
1217
1218 //
1219 // bcond-setgt (do we need to have this pair of setlt, setgt??)
1220 //
1221 def: Mips16Pat
1222   <(brcond (i32 (setgt CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1223    (BtnezT8SltX16 CPU16Regs:$ry, CPU16Regs:$rx,  bb:$imm16)
1224   >;
1225
1226 //
1227 // bcond-setge
1228 //
1229 def: Mips16Pat
1230   <(brcond (i32 (setge CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1231    (BteqzT8SltX16 CPU16Regs:$rx, CPU16Regs:$ry,  bb:$imm16)
1232   >;
1233
1234 //
1235 // never called because compiler transforms a >= k to a > (k-1)
1236 def: Mips16Pat
1237   <(brcond (i32 (setge CPU16Regs:$rx, immSExt16:$imm)), bb:$imm16),
1238    (BteqzT8SltiX16 CPU16Regs:$rx, immSExt16:$imm,  bb:$imm16)
1239   >;
1240
1241 //
1242 // bcond-setlt
1243 //
1244 def: Mips16Pat
1245   <(brcond (i32 (setlt CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1246    (BtnezT8SltX16 CPU16Regs:$rx, CPU16Regs:$ry,  bb:$imm16)
1247   >;
1248
1249 def: Mips16Pat
1250   <(brcond (i32 (setlt CPU16Regs:$rx, immSExt16:$imm)), bb:$imm16),
1251    (BtnezT8SltiX16 CPU16Regs:$rx, immSExt16:$imm,  bb:$imm16)
1252   >;
1253
1254 //
1255 // bcond-setle
1256 //
1257 def: Mips16Pat
1258   <(brcond (i32 (setle CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1259    (BteqzT8SltX16 CPU16Regs:$ry, CPU16Regs:$rx,  bb:$imm16)
1260   >;
1261
1262 //
1263 // bcond-setne
1264 //
1265 def: Mips16Pat
1266   <(brcond (i32 (setne CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1267    (BtnezT8CmpX16 CPU16Regs:$rx, CPU16Regs:$ry,  bb:$imm16)
1268   >;
1269
1270 def: Mips16Pat
1271   <(brcond (i32 (setne CPU16Regs:$rx, immZExt16:$imm)), bb:$targ16),
1272    (BtnezT8CmpiX16 CPU16Regs:$rx, immSExt16:$imm,  bb:$targ16)
1273   >;
1274
1275 def: Mips16Pat
1276   <(brcond (i32 (setne CPU16Regs:$rx, 0)), bb:$targ16),
1277    (BnezRxImmX16 CPU16Regs:$rx, bb:$targ16)
1278   >;
1279
1280 //
1281 // This needs to be there but I forget which code will generate it
1282 //
1283 def: Mips16Pat
1284   <(brcond CPU16Regs:$rx, bb:$targ16),
1285    (BnezRxImmX16 CPU16Regs:$rx, bb:$targ16)
1286   >;
1287
1288 //
1289
1290 //
1291 // bcond-setugt
1292 //
1293 //def: Mips16Pat
1294 //  <(brcond (i32 (setugt CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1295 //   (BtnezT8SltuX16 CPU16Regs:$ry, CPU16Regs:$rx,  bb:$imm16)
1296 //  >;
1297
1298 //
1299 // bcond-setuge
1300 //
1301 //def: Mips16Pat
1302 //  <(brcond (i32 (setuge CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1303 //   (BteqzT8SltuX16 CPU16Regs:$rx, CPU16Regs:$ry,  bb:$imm16)
1304 //  >;
1305
1306
1307 //
1308 // bcond-setult
1309 //
1310 //def: Mips16Pat
1311 //  <(brcond (i32 (setult CPU16Regs:$rx, CPU16Regs:$ry)), bb:$imm16),
1312 //   (BtnezT8SltuX16 CPU16Regs:$rx, CPU16Regs:$ry,  bb:$imm16)
1313 //  >;
1314
1315 def: UncondBranch16_pat<br, BimmX16>;
1316
1317 // Small immediates
1318 def: Mips16Pat<(i32 immSExt16:$in),
1319                (AddiuRxRxImmX16 (Move32R16 ZERO), immSExt16:$in)>;
1320
1321 def: Mips16Pat<(i32 immZExt16:$in), (LiRxImmX16 immZExt16:$in)>;
1322
1323 //
1324 // MipsDivRem
1325 //
1326 def: Mips16Pat
1327   <(MipsDivRem CPU16Regs:$rx, CPU16Regs:$ry),
1328    (DivRxRy16 CPU16Regs:$rx, CPU16Regs:$ry)>;
1329
1330 //
1331 // MipsDivRemU
1332 //
1333 def: Mips16Pat
1334   <(MipsDivRemU CPU16Regs:$rx, CPU16Regs:$ry),
1335    (DivuRxRy16 CPU16Regs:$rx, CPU16Regs:$ry)>;
1336
1337 //  signed a,b
1338 //  x = (a>=b)?x:y
1339 //
1340 //  if !(a < b) x = y
1341 //
1342 def : Mips16Pat<(select (i32 (setge CPU16Regs:$a, CPU16Regs:$b)),
1343                  CPU16Regs:$x, CPU16Regs:$y),
1344                 (SelTBteqZSlt CPU16Regs:$x, CPU16Regs:$y,
1345                  CPU16Regs:$a, CPU16Regs:$b)>;
1346
1347 //  signed a,b
1348 //  x = (a>b)?x:y
1349 //
1350 //  if  (b < a) x = y
1351 //
1352 def : Mips16Pat<(select (i32 (setgt CPU16Regs:$a, CPU16Regs:$b)),
1353                  CPU16Regs:$x, CPU16Regs:$y),
1354                 (SelTBtneZSlt CPU16Regs:$x, CPU16Regs:$y,
1355                  CPU16Regs:$b, CPU16Regs:$a)>;
1356
1357 // unsigned a,b
1358 // x = (a>=b)?x:y
1359 //
1360 // if !(a < b) x = y;
1361 //
1362 def : Mips16Pat<
1363   (select (i32 (setuge CPU16Regs:$a, CPU16Regs:$b)),
1364    CPU16Regs:$x, CPU16Regs:$y),
1365   (SelTBteqZSltu CPU16Regs:$x, CPU16Regs:$y,
1366    CPU16Regs:$a, CPU16Regs:$b)>;
1367
1368 //  unsigned a,b
1369 //  x = (a>b)?x:y
1370 //
1371 //  if (b < a) x = y
1372 //
1373 def : Mips16Pat<(select (i32 (setugt CPU16Regs:$a, CPU16Regs:$b)),
1374                  CPU16Regs:$x, CPU16Regs:$y),
1375                 (SelTBtneZSltu CPU16Regs:$x, CPU16Regs:$y,
1376                  CPU16Regs:$b, CPU16Regs:$a)>;
1377
1378 // signed
1379 // x = (a >= k)?x:y
1380 // due to an llvm optimization, i don't think that this will ever
1381 // be used. This is transformed into x = (a > k-1)?x:y
1382 //
1383 //
1384
1385 //def : Mips16Pat<
1386 //  (select (i32 (setge CPU16Regs:$lhs, immSExt16:$rhs)),
1387 //   CPU16Regs:$T, CPU16Regs:$F),
1388 //  (SelTBteqZSlti CPU16Regs:$T, CPU16Regs:$F,
1389 //   CPU16Regs:$lhs, immSExt16:$rhs)>;
1390
1391 //def : Mips16Pat<
1392 //  (select (i32 (setuge CPU16Regs:$lhs, immSExt16:$rhs)),
1393 //   CPU16Regs:$T, CPU16Regs:$F),
1394 //  (SelTBteqZSltiu CPU16Regs:$T, CPU16Regs:$F,
1395 //   CPU16Regs:$lhs, immSExt16:$rhs)>;
1396
1397 // signed
1398 // x = (a < k)?x:y
1399 //
1400 // if !(a < k) x = y;
1401 //
1402 def : Mips16Pat<
1403   (select (i32 (setlt CPU16Regs:$a, immSExt16:$b)),
1404    CPU16Regs:$x, CPU16Regs:$y),
1405   (SelTBtneZSlti CPU16Regs:$x, CPU16Regs:$y,
1406    CPU16Regs:$a, immSExt16:$b)>;
1407
1408
1409 //
1410 //
1411 // signed
1412 // x = (a <= b)? x : y
1413 //
1414 // if  (b < a) x = y
1415 //
1416 def : Mips16Pat<(select (i32 (setle CPU16Regs:$a, CPU16Regs:$b)),
1417                  CPU16Regs:$x, CPU16Regs:$y),
1418                 (SelTBteqZSlt CPU16Regs:$x, CPU16Regs:$y,
1419                  CPU16Regs:$b, CPU16Regs:$a)>;
1420
1421 //
1422 // unnsigned
1423 // x = (a <= b)? x : y
1424 //
1425 // if  (b < a) x = y
1426 //
1427 def : Mips16Pat<(select (i32 (setule CPU16Regs:$a, CPU16Regs:$b)),
1428                  CPU16Regs:$x, CPU16Regs:$y),
1429                 (SelTBteqZSltu CPU16Regs:$x, CPU16Regs:$y,
1430                  CPU16Regs:$b, CPU16Regs:$a)>;
1431
1432 //
1433 // signed/unsigned
1434 // x = (a == b)? x : y
1435 //
1436 // if (a != b) x = y
1437 //
1438 def : Mips16Pat<(select (i32 (seteq CPU16Regs:$a, CPU16Regs:$b)),
1439                  CPU16Regs:$x, CPU16Regs:$y),
1440                 (SelTBteqZCmp CPU16Regs:$x, CPU16Regs:$y,
1441                  CPU16Regs:$b, CPU16Regs:$a)>;
1442
1443 //
1444 // signed/unsigned
1445 // x = (a == 0)? x : y
1446 //
1447 // if (a != 0) x = y
1448 //
1449 def : Mips16Pat<(select (i32 (seteq CPU16Regs:$a, 0)),
1450                  CPU16Regs:$x, CPU16Regs:$y),
1451                 (SelBeqZ CPU16Regs:$x, CPU16Regs:$y,
1452                  CPU16Regs:$a)>;
1453
1454
1455 //
1456 // signed/unsigned
1457 // x = (a == k)? x : y
1458 //
1459 // if (a != k) x = y
1460 //
1461 def : Mips16Pat<(select (i32 (seteq CPU16Regs:$a, immZExt16:$k)),
1462                  CPU16Regs:$x, CPU16Regs:$y),
1463                 (SelTBteqZCmpi CPU16Regs:$x, CPU16Regs:$y,
1464                  CPU16Regs:$a, immZExt16:$k)>;
1465
1466
1467 //
1468 // signed/unsigned
1469 // x = (a != b)? x : y
1470 //
1471 // if (a == b) x = y
1472 //
1473 //
1474 def : Mips16Pat<(select (i32 (setne CPU16Regs:$a, CPU16Regs:$b)),
1475                  CPU16Regs:$x, CPU16Regs:$y),
1476                 (SelTBtneZCmp CPU16Regs:$x, CPU16Regs:$y,
1477                  CPU16Regs:$b, CPU16Regs:$a)>;
1478
1479 //
1480 // signed/unsigned
1481 // x = (a != 0)? x : y
1482 //
1483 // if (a == 0) x = y
1484 //
1485 def : Mips16Pat<(select (i32 (setne CPU16Regs:$a, 0)),
1486                  CPU16Regs:$x, CPU16Regs:$y),
1487                 (SelBneZ CPU16Regs:$x, CPU16Regs:$y,
1488                  CPU16Regs:$a)>;
1489
1490 // signed/unsigned
1491 // x = (a)? x : y
1492 //
1493 // if (!a) x = y
1494 //
1495 def : Mips16Pat<(select  CPU16Regs:$a,
1496                  CPU16Regs:$x, CPU16Regs:$y),
1497       (SelBneZ CPU16Regs:$x, CPU16Regs:$y,
1498        CPU16Regs:$a)>;
1499
1500
1501 //
1502 // signed/unsigned
1503 // x = (a != k)? x : y
1504 //
1505 // if (a == k) x = y
1506 //
1507 def : Mips16Pat<(select (i32 (setne CPU16Regs:$a, immZExt16:$k)),
1508                  CPU16Regs:$x, CPU16Regs:$y),
1509                 (SelTBtneZCmpi CPU16Regs:$x, CPU16Regs:$y,
1510                  CPU16Regs:$a, immZExt16:$k)>;
1511
1512 //
1513 // When writing C code to test setxx these patterns,
1514 // some will be transformed into
1515 // other things. So we test using C code but using -O3 and -O0
1516 //
1517 // seteq
1518 //
1519 def : Mips16Pat
1520   <(seteq CPU16Regs:$lhs,CPU16Regs:$rhs),
1521    (SltiuCCRxImmX16 (XorRxRxRy16 CPU16Regs:$lhs, CPU16Regs:$rhs), 1)>;
1522
1523 def : Mips16Pat
1524   <(seteq CPU16Regs:$lhs, 0),
1525    (SltiuCCRxImmX16 CPU16Regs:$lhs, 1)>;
1526
1527
1528 //
1529 // setge
1530 //
1531
1532 def: Mips16Pat
1533   <(setge CPU16Regs:$lhs, CPU16Regs:$rhs),
1534    (XorRxRxRy16 (SltCCRxRy16 CPU16Regs:$lhs, CPU16Regs:$rhs),
1535    (LiRxImmX16 1))>;
1536
1537 //
1538 // For constants, llvm transforms this to:
1539 // x > (k -1) and then reverses the operands to use setlt. So this pattern
1540 // is not used now by the compiler. (Presumably checking that k-1 does not
1541 // overflow). The compiler never uses this at a the current time, due to
1542 // other optimizations.
1543 //
1544 //def: Mips16Pat
1545 //  <(setge CPU16Regs:$lhs, immSExt16:$rhs),
1546 //   (XorRxRxRy16 (SltiCCRxImmX16 CPU16Regs:$lhs, immSExt16:$rhs),
1547 //   (LiRxImmX16 1))>;
1548
1549 // This catches the x >= -32768 case by transforming it to  x > -32769
1550 //
1551 def: Mips16Pat
1552   <(setgt CPU16Regs:$lhs, -32769),
1553    (XorRxRxRy16 (SltiCCRxImmX16 CPU16Regs:$lhs, -32768),
1554    (LiRxImmX16 1))>;
1555
1556 //
1557 // setgt
1558 //
1559 //
1560
1561 def: Mips16Pat
1562   <(setgt CPU16Regs:$lhs, CPU16Regs:$rhs),
1563    (SltCCRxRy16 CPU16Regs:$rhs, CPU16Regs:$lhs)>;
1564
1565 //
1566 // setle
1567 //
1568 def: Mips16Pat
1569   <(setle CPU16Regs:$lhs, CPU16Regs:$rhs),
1570    (XorRxRxRy16 (SltCCRxRy16 CPU16Regs:$rhs, CPU16Regs:$lhs), (LiRxImmX16 1))>;
1571
1572 //
1573 // setlt
1574 //
1575 def: SetCC_R16<setlt, SltCCRxRy16>;
1576
1577 def: SetCC_I16<setlt, immSExt16, SltiCCRxImmX16>;
1578
1579 //
1580 // setne
1581 //
1582 def : Mips16Pat
1583   <(setne CPU16Regs:$lhs,CPU16Regs:$rhs),
1584    (SltuCCRxRy16 (LiRxImmX16 0),
1585    (XorRxRxRy16 CPU16Regs:$lhs, CPU16Regs:$rhs))>;
1586
1587
1588 //
1589 // setuge
1590 //
1591 def: Mips16Pat
1592   <(setuge CPU16Regs:$lhs, CPU16Regs:$rhs),
1593    (XorRxRxRy16 (SltuCCRxRy16 CPU16Regs:$lhs, CPU16Regs:$rhs),
1594    (LiRxImmX16 1))>;
1595
1596 // this pattern will never be used because the compiler will transform
1597 // x >= k to x > (k - 1) and then use SLT
1598 //
1599 //def: Mips16Pat
1600 //  <(setuge CPU16Regs:$lhs, immZExt16:$rhs),
1601 //   (XorRxRxRy16 (SltiuCCRxImmX16 CPU16Regs:$lhs, immZExt16:$rhs),
1602 //   (LiRxImmX16 1))>;
1603
1604 //
1605 // setugt
1606 //
1607 def: Mips16Pat
1608   <(setugt CPU16Regs:$lhs, CPU16Regs:$rhs),
1609    (SltuCCRxRy16 CPU16Regs:$rhs, CPU16Regs:$lhs)>;
1610
1611 //
1612 // setule
1613 //
1614 def: Mips16Pat
1615   <(setule CPU16Regs:$lhs, CPU16Regs:$rhs),
1616    (XorRxRxRy16 (SltuCCRxRy16 CPU16Regs:$rhs, CPU16Regs:$lhs), (LiRxImmX16 1))>;
1617
1618 //
1619 // setult
1620 //
1621 def: SetCC_R16<setult, SltuCCRxRy16>;
1622
1623 def: SetCC_I16<setult, immSExt16, SltiuCCRxImmX16>;
1624
1625 def: Mips16Pat<(add CPU16Regs:$hi, (MipsLo tglobaladdr:$lo)),
1626                (AddiuRxRxImmX16 CPU16Regs:$hi, tglobaladdr:$lo)>;
1627
1628 // hi/lo relocs
1629
1630 def : Mips16Pat<(MipsHi tglobaladdr:$in), 
1631                 (SllX16 (LiRxImmX16 tglobaladdr:$in), 16)>;
1632 def : Mips16Pat<(MipsHi tjumptable:$in),
1633                 (SllX16 (LiRxImmX16 tjumptable:$in), 16)>;
1634 def : Mips16Pat<(MipsHi tglobaltlsaddr:$in),
1635                 (SllX16 (LiRxImmX16 tglobaltlsaddr:$in), 16)>;
1636
1637 // wrapper_pic
1638 class Wrapper16Pat<SDNode node, Instruction ADDiuOp, RegisterClass RC>:
1639   Mips16Pat<(MipsWrapper RC:$gp, node:$in),
1640             (ADDiuOp RC:$gp, node:$in)>;
1641
1642
1643 def : Wrapper16Pat<tglobaladdr, AddiuRxRxImmX16, CPU16Regs>;
1644 def : Wrapper16Pat<tglobaltlsaddr, AddiuRxRxImmX16, CPU16Regs>;
1645
1646 def : Mips16Pat<(i32 (extloadi8   addr16:$src)),
1647                 (LbuRxRyOffMemX16  addr16:$src)>;
1648 def : Mips16Pat<(i32 (extloadi16  addr16:$src)),
1649                 (LhuRxRyOffMemX16  addr16:$src)>;