1//===-- CSKYInstrInfo16Instr.td - CSKY 16-bit Instruction --*- tablegen -*-===//
2//
3// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6//
7//===----------------------------------------------------------------------===//
8//
9// This file describes the CSKY 16-bit instructions in TableGen format.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14// CSKY specific DAG Nodes.
15//===----------------------------------------------------------------------===//
16
17// Target-dependent nodes.
18def CSKY_NIE : SDNode<"CSKYISD::NIE", SDTNone,
19    [SDNPHasChain, SDNPOptInGlue]>;
20def CSKY_NIR : SDNode<"CSKYISD::NIR", SDTNone,
21    [SDNPHasChain, SDNPOptInGlue]>;
22
23//===----------------------------------------------------------------------===//
24// Operand and SDNode transformation definitions.
25//===----------------------------------------------------------------------===//
26
27def br_symbol_16bit : Operand<iPTR> {
28  let EncoderMethod =
29    "getBranchSymbolOpValue<CSKY::fixup_csky_pcrel_imm10_scale2>";
30  let ParserMatchClass = CSKYSymbol;
31  let DecoderMethod = "decodeSImmOperand<10, 1>";
32  let PrintMethod = "printCSKYSymbolOperand";
33  let OperandType = "OPERAND_PCREL";
34}
35
36def SPOperand : AsmOperandClass {
37  let Name = "SPOperand";
38  let RenderMethod = "addRegOperands";
39  let DiagnosticType = !strconcat("Invalid", Name);
40}
41
42def SPOp : RegisterOperand<GPR> {
43 let ParserMatchClass = SPOperand;
44}
45
46def constpool_symbol_16bit : Operand<iPTR> {
47  let ParserMatchClass = Constpool;
48  let EncoderMethod =
49    "getConstpoolSymbolOpValue<CSKY::fixup_csky_pcrel_uimm7_scale4>";
50  let DecoderMethod = "decodeLRW16Imm8";
51  let PrintMethod = "printConstpool";
52  let OperandType = "OPERAND_PCREL";
53}
54
55//===----------------------------------------------------------------------===//
56// Instruction Formats
57//===----------------------------------------------------------------------===//
58
59include "CSKYInstrFormats16Instr.td"
60
61//===----------------------------------------------------------------------===//
62// Instruction definitions.
63//===----------------------------------------------------------------------===//
64
65//===----------------------------------------------------------------------===//
66// Basic ALU instructions.
67//===----------------------------------------------------------------------===//
68
69let isCommutable = 1, isAdd = 1 in
70  def ADDU16 : R16_XYZ<0, "addu16", add>;
71let Pattern = [(set mGPR:$rz, (sub mGPR:$rx, mGPR:$ry))] in
72  def SUBU16 : R16_XYZ<1, "subu16", sub>;
73
74let isCommutable = 1, isAdd = 1 in
75  def ADDC16 : R16_XZ_BINOP_C<0b1000, 0b01, "addc16">;
76def SUBC16 : R16_XZ_BINOP_C<0b1000, 0b11, "subc16">;
77
78let isCommutable = 1 in {
79  let isAdd = 1 in
80  def ADDU16XZ : R16_XZ_BINOP<0b1000, 0b00, "addu16", BinOpFrag<(add node:$LHS, node:$RHS)>>;
81  def AND16 : R16_XZ_BINOP<0b1010, 0b00, "and16", BinOpFrag<(and node:$LHS, node:$RHS)>>;
82  def OR16 : R16_XZ_BINOP<0b1011, 0b00, "or16", BinOpFrag<(or node:$LHS, node:$RHS)>>;
83  def XOR16 : R16_XZ_BINOP<0b1011, 0b01, "xor16", BinOpFrag<(xor node:$LHS, node:$RHS)>>;
84  def NOR16 : R16_XZ_BINOP<0b1011, 0b10, "nor16", BinOpFrag<(not (or node:$LHS, node:$RHS))>>;
85  let isCodeGenOnly = 1 in
86  def NOT16 : R16_XZ_UNOP<0b1011, 0b10, "not16">;
87  def MULT16 :  R16_XZ_BINOP<0b1111, 0b00, "mult16", BinOpFrag<(mul node:$LHS, node:$RHS)>>;
88}
89def SUBU16XZ : R16_XZ_BINOP<0b1000, 0b10, "subu16", BinOpFrag<(sub node:$LHS, node:$RHS)>>;
90def ANDN16 : R16_XZ_BINOP<0b1010, 0b01, "andn16", BinOpFrag<(and node:$LHS, (not node:$RHS))>>;
91def LSL16 : R16_XZ_BINOP<0b1100, 0b00, "lsl16", BinOpFrag<(shl node:$LHS, node:$RHS)>>;
92def LSR16 : R16_XZ_BINOP<0b1100, 0b01, "lsr16", BinOpFrag<(srl node:$LHS, node:$RHS)>>;
93def ASR16 : R16_XZ_BINOP<0b1100, 0b10, "asr16", BinOpFrag<(sra node:$LHS, node:$RHS)>>;
94def ROTL16 : R16_XZ_BINOP<0b1100, 0b11, "rotl16", BinOpFrag<(rotl node:$LHS, (and node:$RHS, 0x1f))>>;
95
96def MULSH16 : R16_XZ_BINOP_NOPat<0b1111, 0b01, "mulsh16">;
97
98def ZEXTB16 : R16_XZ_UNOP<0b1101, 0b00, "zextb16">;
99def ZEXTH16 : R16_XZ_UNOP<0b1101, 0b01, "zexth16">;
100def SEXTB16 : R16_XZ_UNOP<0b1101, 0b10, "sextb16">;
101def SEXTH16 : R16_XZ_UNOP<0b1101, 0b11, "sexth16">;
102
103let Constraints = "$rZ = $rz", isReMaterializable = 1, isAsCheapAsAMove = 1 in {
104  let isAdd = 1, Pattern = [(set mGPR:$rz, (add mGPR:$rZ, oimm8:$imm8))] in
105  def ADDI16 : I16_Z_8<0b100, (ins mGPR:$rZ, oimm8:$imm8), "addi16\t$rz, $imm8">;
106  let Pattern = [(set mGPR:$rz, (sub mGPR:$rZ, oimm8:$imm8))] in
107  def SUBI16 : I16_Z_8<0b101, (ins mGPR:$rZ, oimm8:$imm8), "subi16\t$rz, $imm8">;
108}
109
110let isAdd = 1 in
111def ADDI16ZSP : I16_Z_8<0b011, (ins SPOp:$sp, uimm8_2:$imm8),
112                        "addi16\t$rz, $sp, $imm8">;
113
114let isAdd = 1 in
115def ADDI16SPSP : I16_SP_IMM7<0b000,"addi16">;
116def SUBI16SPSP : I16_SP_IMM7<0b001,"subi16">;
117
118let isReMaterializable = 1, isAsCheapAsAMove = 1 in {
119  def LSLI16 : I16_XZ_IMM5<0, "lsli16", shl>;
120  def LSRI16 : I16_XZ_IMM5<1, "lsri16", srl>;
121  def ASRI16 : I16_XZ_IMM5<2, "asri16", sra>;
122}
123
124let isAdd = 1 in
125def ADDI16XZ : I16_XZ_IMM3<0b10, "addi16", add>;
126def SUBI16XZ : I16_XZ_IMM3<0b11, "subi16", sub>;
127
128let Size = 4 in
129def NEG16 : CSKYPseudo<(outs mGPR:$rd), (ins mGPR:$rx), "neg16 $rd, $rx", []>;
130
131let Size = 4 in
132def RSUBI16 : CSKYPseudo<(outs mGPR:$rd),
133  (ins mGPR:$rx, uimm8:$imm8), "rsubi16 $rd, $rx, $imm8", []>;
134
135//===----------------------------------------------------------------------===//
136// Load & Store instructions.
137//===----------------------------------------------------------------------===//
138
139def LD16B : I16_XZ_LDST<AddrMode16B, 0b000, "ld16.b",
140  (outs mGPR:$rz), (ins mGPR:$rx, uimm5:$imm)>;
141def LD16H : I16_XZ_LDST<AddrMode16H, 0b001, "ld16.h",
142  (outs mGPR:$rz), (ins mGPR:$rx, uimm5_1:$imm)>;
143def LD16W : I16_XZ_LDST<AddrMode16W, 0b010, "ld16.w",
144  (outs mGPR:$rz), (ins mGPR:$rx, uimm5_2:$imm)>;
145def ST16B : I16_XZ_LDST<AddrMode16B, 0b100, "st16.b",
146  (outs), (ins mGPR:$rz, mGPR:$rx, uimm5:$imm)>;
147def ST16H : I16_XZ_LDST<AddrMode16H, 0b101, "st16.h",
148  (outs), (ins mGPR:$rz, mGPR:$rx, uimm5_1:$imm)>;
149def ST16W : I16_XZ_LDST<AddrMode16W, 0b110, "st16.w",
150  (outs), (ins mGPR:$rz, mGPR:$rx, uimm5_2:$imm)>;
151
152def LD16WSP : I16_ZSP_LDST<AddrMode16W, 0b011, "ld16.w",
153  (outs mGPR:$rz), (ins SPOp:$sp, uimm8_2:$addr)>;
154def ST16WSP : I16_ZSP_LDST<AddrMode16W, 0b111, "st16.w",
155  (outs), (ins mGPR:$rz, SPOp:$sp, uimm8_2:$addr)>;
156
157//===----------------------------------------------------------------------===//
158// Compare instructions.
159//===----------------------------------------------------------------------===//
160
161def CMPHS16 : R16_XY_CMP<0, "cmphs16">;
162def CMPLT16 : R16_XY_CMP<1, "cmplt16">;
163let isCommutable = 1 in
164def CMPNE16 : R16_XY_CMP<2, "cmpne16">;
165
166
167def CMPHSI16 : I16_X_CMP<0, "cmphsi16", oimm5>;
168def CMPLTI16 : I16_X_CMP<1, "cmplti16", oimm5>;
169def CMPLEI16 : CSKYPseudo<(outs CARRY:$ca), (ins mGPR:$rx, uimm5:$imm5),
170    "cmplei16\t$rx, $imm5", []>;
171def CMPNEI16 : I16_X_CMP<2, "cmpnei16", uimm5>;
172
173//===----------------------------------------------------------------------===//
174// Data move instructions.
175//===----------------------------------------------------------------------===//
176
177
178def MOVI16 : I16_Z_8<0b110, (ins uimm8:$imm8), "movi16\t$rz, $imm8"> {
179  let isReMaterializable = 1;
180  let isAsCheapAsAMove = 1;
181  let isMoveImm = 1;
182  let Pattern = [(set mGPR:$rz, uimm8:$imm8)];
183}
184
185def MOV16 : CSKY16Inst<AddrModeNone, (outs sGPR:$rz), (ins sGPR:$rx),
186                       "mov16\t$rz, $rx", []> {
187  bits<4> rz;
188  bits<4> rx;
189  let Inst{15,14} = 0b01;
190  let Inst{13 - 10} = 0b1011;
191  let Inst{9 - 6} = rz;
192  let Inst{5 - 2} = rx;
193  let Inst{1,0} = 0b11;
194}
195
196// MVC16 is not in "cskyv2 instructions reference manul"
197def MVCV16 : CSKY16Inst<AddrModeNone,
198  (outs sGPR:$rz), (ins CARRY:$ca), "mvcv16\t$rz", []> {
199  bits<4> rz;
200  let Inst{15,14} = 0b01;
201  let Inst{13 - 10} = 0b1001;
202  let Inst{9 - 6} = rz;
203  let Inst{5 - 2} = 0;
204  let Inst{1,0} = 0b11;
205}
206
207
208//===----------------------------------------------------------------------===//
209// Branch and call instructions.
210//===----------------------------------------------------------------------===//
211
212let isBranch = 1, isTerminator = 1 in {
213  let isBarrier = 1, isPredicable = 1 in
214    def BR16 : J16<1, "br16", (ins br_symbol_16bit:$offset)>;
215
216  def BT16 : J16_B<2, "bt16">;
217  def BF16 : J16_B<3, "bf16">;
218}
219
220def JMP16 : R16_X_J<0b11100000, 0b00, "jmp16"> {
221  let isBranch = 1;
222  let isTerminator = 1;
223  let isBarrier = 1;
224  let isIndirectBranch = 1;
225  let Pattern = [(brind sGPR:$rx)];
226}
227
228def JSR16 : R16_X_J<0b11101111, 0b01, "jsr16"> {
229  let isCall = 1;
230  let Defs = [ R15 ];
231}
232
233def RTS16 : CSKY16Inst<AddrModeNone, (outs), (ins), "rts16", [(CSKY_RET)]> {
234  let isTerminator = 1;
235  let isReturn = 1;
236  let isBarrier = 1;
237  let Inst = 0b0111100000111100;
238  let Uses = [R15];
239  let isCodeGenOnly = 1;
240}
241
242def JMPIX16 :  CSKY16Inst<AddrModeNone, (outs),
243  (ins mGPR:$rx, uimm2_jmpix:$indeximm2), "jmpix16\t$rx, $indeximm2", []> {
244  bits<3> rx;
245  bits<2> indeximm2;
246  let Inst{15,14} = 0b00;
247  let Inst{13 - 11} = 0b111;
248  let Inst{10 - 8} = rx;
249  let Inst{7 - 2} = 0b111000;
250  let Inst{1,0} = indeximm2;
251  let Predicates = [HasJAVA];
252  let Uses = [R30];
253}
254
255//===----------------------------------------------------------------------===//
256// Symbol address instructions.
257//===----------------------------------------------------------------------===//
258
259def LRW16 : CSKY16Inst<AddrModeNone, (outs mGPR:$rz),
260  (ins constpool_symbol_16bit:$label), "lrw16\t$rz, $label", []> {
261  bits<3> rz;
262  bits<8> label;
263  let Inst{15 - 13} = 0b000;
264  let Inst{12} = label{7};
265  let Inst{11,10} = 0b00;
266  let Inst{9,8} = label{6,5};
267  let Inst{7 - 5} = rz;
268  let Inst{4 - 0} = label{4-0};
269  let mayLoad = 1;
270  let mayStore = 0;
271}
272
273def LRW16_Gen : CSKY16Inst<AddrModeNone, (outs mGPR:$rz),
274  (ins bare_symbol:$src, constpool_symbol_16bit:$label),
275  "lrw16\t$rz, $label", []> {
276  bits<3> rz;
277  bits<8> label;
278  let Inst{15 - 13} = 0b000;
279  let Inst{12} = label{7};
280  let Inst{11,10} = 0b00;
281  let Inst{9,8} = label{6,5};
282  let Inst{7 - 5} = rz;
283  let Inst{4 - 0} = label{4-0};
284  let mayLoad = 1;
285  let mayStore = 0;
286  let isCodeGenOnly = 1;
287}
288
289
290//===----------------------------------------------------------------------===//
291// Other operation instructions.
292//===----------------------------------------------------------------------===//
293
294def REVB16 :  R16_XZ_UNOP<0b1110, 0b10, "revb16">;
295def REVH16 :  R16_XZ_UNOP<0b1110, 0b11, "revh16">;
296
297let isCodeGenOnly = 1 in
298def SETC16 : CSKY16Inst<AddrModeNone,
299  (outs CARRY:$ca), (ins), "setc16", []> {
300  let Inst{15, 14} = 0b01;
301  let Inst{13 - 10} = 0b1001;
302  let Inst{9 - 6} = 0;
303  let Inst{5 - 2} = 0;
304  let Inst{1, 0} = 0;
305  let isCompare = 1;
306}
307
308let isCodeGenOnly = 1 in
309def CLRC16 : CSKY16Inst<AddrModeNone,
310  (outs CARRY:$ca), (ins), "clrc16", []> {
311  let Inst{15, 14} = 0b01;
312  let Inst{13 - 10} = 0b1001;
313  let Inst{9 - 6} = 0;
314  let Inst{5 - 2} = 0;
315  let Inst{1, 0} = 2;
316  let isCompare = 1;
317}
318
319let Constraints = "$rZ = $rz" in {
320  def BCLRI16 : I16_Z_5<0b100, (outs mGPR:$rz), (ins mGPR:$rZ, uimm5:$imm5),
321                        "bclri16">;
322  def BSETI16 : I16_Z_5<0b101, (outs mGPR:$rz), (ins mGPR:$rZ, uimm5:$imm5),
323                        "bseti16">;
324}
325
326let Predicates = [HasBTST16] in
327  def BTSTI16 : I16_Z_5<0b110, (outs CARRY:$ca), (ins mGPR:$rz, uimm5:$imm5),
328                        "btsti16">;
329
330def TST16 : CSKY16Inst<AddrModeNone, (outs CARRY:$ca), (ins sGPR:$rx, sGPR:$ry),
331                        "tst16\t$rx, $ry", []> {
332  bits<4> ry;
333  bits<4> rx;
334  let Inst{15,14} = 0b01;
335  let Inst{13 - 10} = 0b1010;
336  let Inst{9 - 6} = ry;
337  let Inst{5 - 2} = rx;
338  let Inst{1,0} = 0b10;
339  let isCompare = 1;
340}
341
342def TSTNBZ16 : CSKY16Inst<AddrModeNone, (outs CARRY:$ca), (ins sGPR:$rx),
343                          "tstnbz16\t$rx", []> {
344  bits<4> rx;
345  let Inst{15,14} = 0b01;
346  let Inst{13 - 10} = 0b1010;
347  let Inst{9 - 6} = 0b0000;
348  let Inst{5 - 2} = rx;
349  let Inst{1,0} = 0b11;
350  let isCompare = 1;
351}
352
353//===----------------------------------------------------------------------===//
354// Special instructions.
355//===----------------------------------------------------------------------===//
356
357def BKPT : CSKY16Inst<AddrModeNone, (outs), (ins), "bkpt", []> {
358  let Inst = 0;
359}
360
361let mayStore = 1 in {
362def BPUSHH : I16_BPushPop<0b00010100111, 0, (outs), (ins mGPR:$rz), "bpush.h $rz">;
363def BPUSHW : I16_BPushPop<0b00010100111, 0b10, (outs), (ins mGPR:$rz), "bpush.w $rz">;
364}
365
366let mayLoad = 1 in {
367def BPOPH : I16_BPushPop<0b00010100101, 0, (outs mGPR:$rz), (ins),  "bpop.h $rz">;
368def BPOPW : I16_BPushPop<0b00010100101, 0b10, (outs mGPR:$rz), (ins), "bpop.w $rz">;
369}
370
371def NIE : CSKY16Inst<AddrModeNone, (outs), (ins), "nie", [(CSKY_NIE)]> {
372  let Inst = 0b0001010001100000;
373}
374
375let isBarrier = 1, isReturn = 1, isTerminator = 1 in
376def NIR : CSKY16Inst<AddrModeNone, (outs), (ins), "nir", [(CSKY_NIR)]> {
377  let Inst = 0b0001010001100001;
378}
379
380def IPUSH16 : CSKY16Inst<AddrModeNone, (outs), (ins), "ipush16", []> {
381  let Inst{15- 5} = 0b00010100011;
382  let Inst{4-0} = 0b00010;
383  let Predicates = [iHasE1];
384  let Defs  = [R14];
385  let Uses  = [R14, R0, R1, R2, R3, R12, R13];
386  let mayStore = 1;
387}
388
389def IPOP16 : CSKY16Inst<AddrModeNone, (outs), (ins), "ipop16", []> {
390  let Inst{15- 5} = 0b00010100011;
391  let Inst{4-0} = 0b00011;
392  let Predicates = [iHasE1];
393  let Defs  = [R14, R0, R1, R2, R3, R12, R13];
394  let Uses  = [R14];
395  let mayLoad = 1;
396}
397
398def PUSH16 : CSKY16Inst<AddrModeNone, (outs),
399  (ins reglist:$regs, variable_ops), "push16 $regs", []> {
400  bits<5> regs;
401
402  let Inst{15- 5} = 0b00010100110;
403  let Inst{4-0} = regs;
404  let Predicates = [iHasE1];
405  let Defs  = [R14];
406  let Uses  = [R14];
407  let mayStore = 1;
408}
409
410def POP16 : CSKY16Inst<AddrModeNone, (outs),
411  (ins reglist:$regs, variable_ops), "pop16 $regs", []> {
412  bits<5> regs;
413
414  let Inst{15- 5} = 0b00010100100;
415  let Inst{4-0} = regs;
416  let Predicates = [iHasE1];
417  let Defs  = [R14];
418  let Uses  = [R14];
419  let mayLoad = 1;
420}
421
422//===----------------------------------------------------------------------===//
423// CSKYPseudo
424//===----------------------------------------------------------------------===//
425
426let usesCustomInserter = 1 in  {
427  def ISEL16 : CSKYPseudo<(outs sGPR:$dst),
428    (ins CARRY:$cond, sGPR:$src1, sGPR:$src2),
429    "!isel16\t$dst, $src1, src2",
430    [(set sGPR:$dst, (select CARRY:$cond, sGPR:$src1, sGPR:$src2))]>;
431}
432
433class JBranchPseudo<dag out, dag ins, string opstr> :
434  CSKYPseudo<out, ins, opstr, []> {
435  let isBranch = 1;
436  let isTerminator = 1;
437  let isIndirectBranch = 1;
438  let mayLoad = 1;
439  let Size = 2;
440}
441
442let isBarrier = 1 in
443def JBR16 : JBranchPseudo<(outs),
444  (ins br_symbol_16bit:$src1), "jbr16\t$src1">;
445def JBT16 : JBranchPseudo<(outs),
446  (ins CARRY:$ca, br_symbol_16bit:$src1), "jbt16\t$src1">;
447def JBF16 : JBranchPseudo<(outs),
448  (ins CARRY:$ca, br_symbol_16bit:$src1), "jbf16\t$src1">;
449
450let mayLoad = 1, Size = 2, isCodeGenOnly = 0 in
451def PseudoLRW16 : CSKYPseudo<(outs mGPR:$rz),
452  (ins bare_symbol:$src), "lrw16 $rz, $src", []>;
453