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