xref: /freebsd/contrib/llvm-project/llvm/lib/Target/RISCV/RISCVInstrInfo.td (revision 62cfcf62f627e5093fb37026a6d8c98e4d2ef04c)
1//===-- RISCVInstrInfo.td - Target Description for RISCV ---*- 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 RISC-V instructions in TableGen format.
10//
11//===----------------------------------------------------------------------===//
12
13//===----------------------------------------------------------------------===//
14// RISC-V specific DAG Nodes.
15//===----------------------------------------------------------------------===//
16
17// Target-independent type requirements, but with target-specific formats.
18def SDT_CallSeqStart : SDCallSeqStart<[SDTCisVT<0, i32>,
19                                       SDTCisVT<1, i32>]>;
20def SDT_CallSeqEnd   : SDCallSeqEnd<[SDTCisVT<0, i32>,
21                                     SDTCisVT<1, i32>]>;
22
23// Target-dependent type requirements.
24def SDT_RISCVCall     : SDTypeProfile<0, -1, [SDTCisVT<0, XLenVT>]>;
25def SDT_RISCVSelectCC : SDTypeProfile<1, 5, [SDTCisSameAs<1, 2>,
26                                             SDTCisSameAs<0, 4>,
27                                             SDTCisSameAs<4, 5>]>;
28
29// Target-independent nodes, but with target-specific formats.
30def callseq_start : SDNode<"ISD::CALLSEQ_START", SDT_CallSeqStart,
31                           [SDNPHasChain, SDNPOutGlue]>;
32def callseq_end   : SDNode<"ISD::CALLSEQ_END", SDT_CallSeqEnd,
33                           [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue]>;
34
35// Target-dependent nodes.
36def riscv_call      : SDNode<"RISCVISD::CALL", SDT_RISCVCall,
37                             [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
38                              SDNPVariadic]>;
39def riscv_ret_flag  : SDNode<"RISCVISD::RET_FLAG", SDTNone,
40                             [SDNPHasChain, SDNPOptInGlue, SDNPVariadic]>;
41def riscv_uret_flag : SDNode<"RISCVISD::URET_FLAG", SDTNone,
42                             [SDNPHasChain, SDNPOptInGlue]>;
43def riscv_sret_flag : SDNode<"RISCVISD::SRET_FLAG", SDTNone,
44                             [SDNPHasChain, SDNPOptInGlue]>;
45def riscv_mret_flag : SDNode<"RISCVISD::MRET_FLAG", SDTNone,
46                             [SDNPHasChain, SDNPOptInGlue]>;
47def riscv_selectcc  : SDNode<"RISCVISD::SELECT_CC", SDT_RISCVSelectCC,
48                             [SDNPInGlue]>;
49def riscv_tail      : SDNode<"RISCVISD::TAIL", SDT_RISCVCall,
50                             [SDNPHasChain, SDNPOptInGlue, SDNPOutGlue,
51                              SDNPVariadic]>;
52def riscv_sllw      : SDNode<"RISCVISD::SLLW", SDTIntShiftOp>;
53def riscv_sraw      : SDNode<"RISCVISD::SRAW", SDTIntShiftOp>;
54def riscv_srlw      : SDNode<"RISCVISD::SRLW", SDTIntShiftOp>;
55
56//===----------------------------------------------------------------------===//
57// Operand and SDNode transformation definitions.
58//===----------------------------------------------------------------------===//
59
60class ImmXLenAsmOperand<string prefix, string suffix = ""> : AsmOperandClass {
61  let Name = prefix # "ImmXLen" # suffix;
62  let RenderMethod = "addImmOperands";
63  let DiagnosticType = !strconcat("Invalid", Name);
64}
65
66class ImmAsmOperand<string prefix, int width, string suffix> : AsmOperandClass {
67  let Name = prefix # "Imm" # width # suffix;
68  let RenderMethod = "addImmOperands";
69  let DiagnosticType = !strconcat("Invalid", Name);
70}
71
72def ImmZeroAsmOperand : AsmOperandClass {
73  let Name = "ImmZero";
74  let RenderMethod = "addImmOperands";
75  let DiagnosticType = !strconcat("Invalid", Name);
76}
77
78class SImmAsmOperand<int width, string suffix = "">
79    : ImmAsmOperand<"S", width, suffix> {
80}
81
82class UImmAsmOperand<int width, string suffix = "">
83    : ImmAsmOperand<"U", width, suffix> {
84}
85
86def FenceArg : AsmOperandClass {
87  let Name = "FenceArg";
88  let RenderMethod = "addFenceArgOperands";
89  let DiagnosticType = "InvalidFenceArg";
90}
91
92def fencearg : Operand<XLenVT> {
93  let ParserMatchClass = FenceArg;
94  let PrintMethod = "printFenceArg";
95  let DecoderMethod = "decodeUImmOperand<4>";
96  let OperandType = "OPERAND_UIMM4";
97  let OperandNamespace = "RISCVOp";
98}
99
100def UImmLog2XLenAsmOperand : AsmOperandClass {
101  let Name = "UImmLog2XLen";
102  let RenderMethod = "addImmOperands";
103  let DiagnosticType = "InvalidUImmLog2XLen";
104}
105
106def uimmlog2xlen : Operand<XLenVT>, ImmLeaf<XLenVT, [{
107  if (Subtarget->is64Bit())
108    return isUInt<6>(Imm);
109  return isUInt<5>(Imm);
110}]> {
111  let ParserMatchClass = UImmLog2XLenAsmOperand;
112  // TODO: should ensure invalid shamt is rejected when decoding.
113  let DecoderMethod = "decodeUImmOperand<6>";
114  let MCOperandPredicate = [{
115    int64_t Imm;
116    if (!MCOp.evaluateAsConstantImm(Imm))
117      return false;
118    if (STI.getTargetTriple().isArch64Bit())
119      return  isUInt<6>(Imm);
120    return isUInt<5>(Imm);
121  }];
122  let OperandType = "OPERAND_UIMMLOG2XLEN";
123  let OperandNamespace = "RISCVOp";
124}
125
126def uimm5 : Operand<XLenVT>, ImmLeaf<XLenVT, [{return isUInt<5>(Imm);}]> {
127  let ParserMatchClass = UImmAsmOperand<5>;
128  let DecoderMethod = "decodeUImmOperand<5>";
129  let OperandType = "OPERAND_UIMM5";
130  let OperandNamespace = "RISCVOp";
131}
132
133def simm12 : Operand<XLenVT>, ImmLeaf<XLenVT, [{return isInt<12>(Imm);}]> {
134  let ParserMatchClass = SImmAsmOperand<12>;
135  let EncoderMethod = "getImmOpValue";
136  let DecoderMethod = "decodeSImmOperand<12>";
137  let MCOperandPredicate = [{
138    int64_t Imm;
139    if (MCOp.evaluateAsConstantImm(Imm))
140      return isInt<12>(Imm);
141    return MCOp.isBareSymbolRef();
142  }];
143  let OperandType = "OPERAND_SIMM12";
144  let OperandNamespace = "RISCVOp";
145}
146
147// A 13-bit signed immediate where the least significant bit is zero.
148def simm13_lsb0 : Operand<OtherVT> {
149  let ParserMatchClass = SImmAsmOperand<13, "Lsb0">;
150  let EncoderMethod = "getImmOpValueAsr1";
151  let DecoderMethod = "decodeSImmOperandAndLsl1<13>";
152  let MCOperandPredicate = [{
153    int64_t Imm;
154    if (MCOp.evaluateAsConstantImm(Imm))
155      return isShiftedInt<12, 1>(Imm);
156    return MCOp.isBareSymbolRef();
157  }];
158  let OperandType = "OPERAND_SIMM13_LSB0";
159  let OperandNamespace = "RISCVOp";
160}
161
162class UImm20Operand : Operand<XLenVT> {
163  let EncoderMethod = "getImmOpValue";
164  let DecoderMethod = "decodeUImmOperand<20>";
165  let MCOperandPredicate = [{
166    int64_t Imm;
167    if (MCOp.evaluateAsConstantImm(Imm))
168      return isUInt<20>(Imm);
169    return MCOp.isBareSymbolRef();
170  }];
171  let OperandType = "OPERAND_UIMM20";
172  let OperandNamespace = "RISCVOp";
173}
174
175def uimm20_lui : UImm20Operand {
176  let ParserMatchClass = UImmAsmOperand<20, "LUI">;
177}
178def uimm20_auipc : UImm20Operand {
179  let ParserMatchClass = UImmAsmOperand<20, "AUIPC">;
180}
181
182def Simm21Lsb0JALAsmOperand : SImmAsmOperand<21, "Lsb0JAL"> {
183  let ParserMethod = "parseJALOffset";
184}
185
186// A 21-bit signed immediate where the least significant bit is zero.
187def simm21_lsb0_jal : Operand<OtherVT> {
188  let ParserMatchClass = Simm21Lsb0JALAsmOperand;
189  let EncoderMethod = "getImmOpValueAsr1";
190  let DecoderMethod = "decodeSImmOperandAndLsl1<21>";
191  let MCOperandPredicate = [{
192    int64_t Imm;
193    if (MCOp.evaluateAsConstantImm(Imm))
194      return isShiftedInt<20, 1>(Imm);
195    return MCOp.isBareSymbolRef();
196  }];
197  let OperandType = "OPERAND_SIMM21_LSB0";
198  let OperandNamespace = "RISCVOp";
199}
200
201def BareSymbol : AsmOperandClass {
202  let Name = "BareSymbol";
203  let RenderMethod = "addImmOperands";
204  let DiagnosticType = "InvalidBareSymbol";
205  let ParserMethod = "parseBareSymbol";
206}
207
208// A bare symbol.
209def bare_symbol : Operand<XLenVT> {
210  let ParserMatchClass = BareSymbol;
211}
212
213def CallSymbol : AsmOperandClass {
214  let Name = "CallSymbol";
215  let RenderMethod = "addImmOperands";
216  let DiagnosticType = "InvalidCallSymbol";
217  let ParserMethod = "parseCallSymbol";
218}
219
220// A bare symbol used in call/tail only.
221def call_symbol : Operand<XLenVT> {
222  let ParserMatchClass = CallSymbol;
223}
224
225def TPRelAddSymbol : AsmOperandClass {
226  let Name = "TPRelAddSymbol";
227  let RenderMethod = "addImmOperands";
228  let DiagnosticType = "InvalidTPRelAddSymbol";
229  let ParserMethod = "parseOperandWithModifier";
230}
231
232// A bare symbol with the %tprel_add variant.
233def tprel_add_symbol : Operand<XLenVT> {
234  let ParserMatchClass = TPRelAddSymbol;
235}
236
237def CSRSystemRegister : AsmOperandClass {
238  let Name = "CSRSystemRegister";
239  let ParserMethod = "parseCSRSystemRegister";
240  let DiagnosticType = "InvalidCSRSystemRegister";
241}
242
243def csr_sysreg : Operand<XLenVT> {
244  let ParserMatchClass = CSRSystemRegister;
245  let PrintMethod = "printCSRSystemRegister";
246  let DecoderMethod = "decodeUImmOperand<12>";
247  let OperandType = "OPERAND_UIMM12";
248  let OperandNamespace = "RISCVOp";
249}
250
251// A parameterized register class alternative to i32imm/i64imm from Target.td.
252def ixlenimm : Operand<XLenVT>;
253
254def ixlenimm_li : Operand<XLenVT> {
255  let ParserMatchClass = ImmXLenAsmOperand<"", "LI">;
256}
257
258// Standalone (codegen-only) immleaf patterns.
259def simm32     : ImmLeaf<XLenVT, [{return isInt<32>(Imm);}]>;
260def simm32hi20 : ImmLeaf<XLenVT, [{return isShiftedInt<20, 12>(Imm);}]>;
261// A mask value that won't affect significant shift bits.
262def immbottomxlenset : ImmLeaf<XLenVT, [{
263  if (Subtarget->is64Bit())
264    return countTrailingOnes<uint64_t>(Imm) >= 6;
265  return countTrailingOnes<uint64_t>(Imm) >= 5;
266}]>;
267
268// Addressing modes.
269// Necessary because a frameindex can't be matched directly in a pattern.
270def AddrFI : ComplexPattern<iPTR, 1, "SelectAddrFI", [frameindex], []>;
271
272// Extract least significant 12 bits from an immediate value and sign extend
273// them.
274def LO12Sext : SDNodeXForm<imm, [{
275  return CurDAG->getTargetConstant(SignExtend64<12>(N->getZExtValue()),
276                                   SDLoc(N), N->getValueType(0));
277}]>;
278
279// Extract the most significant 20 bits from an immediate value. Add 1 if bit
280// 11 is 1, to compensate for the low 12 bits in the matching immediate addi
281// or ld/st being negative.
282def HI20 : SDNodeXForm<imm, [{
283  return CurDAG->getTargetConstant(((N->getZExtValue()+0x800) >> 12) & 0xfffff,
284                                   SDLoc(N), N->getValueType(0));
285}]>;
286
287//===----------------------------------------------------------------------===//
288// Instruction Formats
289//===----------------------------------------------------------------------===//
290
291include "RISCVInstrFormats.td"
292
293//===----------------------------------------------------------------------===//
294// Instruction Class Templates
295//===----------------------------------------------------------------------===//
296
297let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
298class BranchCC_rri<bits<3> funct3, string opcodestr>
299    : RVInstB<funct3, OPC_BRANCH, (outs),
300              (ins GPR:$rs1, GPR:$rs2, simm13_lsb0:$imm12),
301              opcodestr, "$rs1, $rs2, $imm12">,
302      Sched<[WriteJmp]> {
303  let isBranch = 1;
304  let isTerminator = 1;
305}
306
307let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in
308class Load_ri<bits<3> funct3, string opcodestr>
309    : RVInstI<funct3, OPC_LOAD, (outs GPR:$rd), (ins GPR:$rs1, simm12:$imm12),
310              opcodestr, "$rd, ${imm12}(${rs1})">;
311
312// Operands for stores are in the order srcreg, base, offset rather than
313// reflecting the order these fields are specified in the instruction
314// encoding.
315let hasSideEffects = 0, mayLoad = 0, mayStore = 1 in
316class Store_rri<bits<3> funct3, string opcodestr>
317    : RVInstS<funct3, OPC_STORE, (outs),
318              (ins GPR:$rs2, GPR:$rs1, simm12:$imm12),
319              opcodestr, "$rs2, ${imm12}(${rs1})">;
320
321let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
322class ALU_ri<bits<3> funct3, string opcodestr>
323    : RVInstI<funct3, OPC_OP_IMM, (outs GPR:$rd), (ins GPR:$rs1, simm12:$imm12),
324              opcodestr, "$rd, $rs1, $imm12">,
325      Sched<[WriteIALU, ReadIALU]>;
326
327let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
328class Shift_ri<bit arithshift, bits<3> funct3, string opcodestr>
329    : RVInstIShift<arithshift, funct3, OPC_OP_IMM, (outs GPR:$rd),
330                   (ins GPR:$rs1, uimmlog2xlen:$shamt), opcodestr,
331                   "$rd, $rs1, $shamt">,
332      Sched<[WriteShift, ReadShift]>;
333
334let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
335class ALU_rr<bits<7> funct7, bits<3> funct3, string opcodestr>
336    : RVInstR<funct7, funct3, OPC_OP, (outs GPR:$rd), (ins GPR:$rs1, GPR:$rs2),
337              opcodestr, "$rd, $rs1, $rs2">;
338
339let hasSideEffects = 1, mayLoad = 0, mayStore = 0 in
340class CSR_ir<bits<3> funct3, string opcodestr>
341    : RVInstI<funct3, OPC_SYSTEM, (outs GPR:$rd), (ins csr_sysreg:$imm12, GPR:$rs1),
342              opcodestr, "$rd, $imm12, $rs1">, Sched<[WriteCSR, ReadCSR]>;
343
344let hasSideEffects = 1, mayLoad = 0, mayStore = 0 in
345class CSR_ii<bits<3> funct3, string opcodestr>
346    : RVInstI<funct3, OPC_SYSTEM, (outs GPR:$rd),
347              (ins csr_sysreg:$imm12, uimm5:$rs1),
348              opcodestr, "$rd, $imm12, $rs1">, Sched<[WriteCSR]>;
349
350let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
351class ShiftW_ri<bit arithshift, bits<3> funct3, string opcodestr>
352    : RVInstIShiftW<arithshift, funct3, OPC_OP_IMM_32, (outs GPR:$rd),
353                    (ins GPR:$rs1, uimm5:$shamt), opcodestr,
354                    "$rd, $rs1, $shamt">,
355      Sched<[WriteShift32, ReadShift32]>;
356
357let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
358class ALUW_rr<bits<7> funct7, bits<3> funct3, string opcodestr>
359    : RVInstR<funct7, funct3, OPC_OP_32, (outs GPR:$rd),
360              (ins GPR:$rs1, GPR:$rs2), opcodestr, "$rd, $rs1, $rs2">;
361
362let hasSideEffects = 1, mayLoad = 0, mayStore = 0 in
363class Priv<string opcodestr, bits<7> funct7>
364    : RVInstR<funct7, 0b000, OPC_SYSTEM, (outs), (ins GPR:$rs1, GPR:$rs2),
365              opcodestr, "">;
366
367//===----------------------------------------------------------------------===//
368// Instructions
369//===----------------------------------------------------------------------===//
370
371let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in {
372let isReMaterializable = 1, isAsCheapAsAMove = 1 in
373def LUI : RVInstU<OPC_LUI, (outs GPR:$rd), (ins uimm20_lui:$imm20),
374                  "lui", "$rd, $imm20">, Sched<[WriteIALU]>;
375
376def AUIPC : RVInstU<OPC_AUIPC, (outs GPR:$rd), (ins uimm20_auipc:$imm20),
377                    "auipc", "$rd, $imm20">, Sched<[WriteIALU]>;
378
379let isCall = 1 in
380def JAL : RVInstJ<OPC_JAL, (outs GPR:$rd), (ins simm21_lsb0_jal:$imm20),
381                  "jal", "$rd, $imm20">, Sched<[WriteJal]>;
382
383let isCall = 1 in
384def JALR : RVInstI<0b000, OPC_JALR, (outs GPR:$rd),
385                   (ins GPR:$rs1, simm12:$imm12),
386                   "jalr", "$rd, ${imm12}(${rs1})">,
387           Sched<[WriteJalr, ReadJalr]>;
388} // hasSideEffects = 0, mayLoad = 0, mayStore = 0
389
390def BEQ  : BranchCC_rri<0b000, "beq">;
391def BNE  : BranchCC_rri<0b001, "bne">;
392def BLT  : BranchCC_rri<0b100, "blt">;
393def BGE  : BranchCC_rri<0b101, "bge">;
394def BLTU : BranchCC_rri<0b110, "bltu">;
395def BGEU : BranchCC_rri<0b111, "bgeu">;
396
397def LB  : Load_ri<0b000, "lb">, Sched<[WriteLDB, ReadMemBase]>;
398def LH  : Load_ri<0b001, "lh">, Sched<[WriteLDH, ReadMemBase]>;
399def LW  : Load_ri<0b010, "lw">, Sched<[WriteLDW, ReadMemBase]>;
400def LBU : Load_ri<0b100, "lbu">, Sched<[WriteLDB, ReadMemBase]>;
401def LHU : Load_ri<0b101, "lhu">, Sched<[WriteLDH, ReadMemBase]>;
402
403def SB : Store_rri<0b000, "sb">, Sched<[WriteSTB, ReadStoreData, ReadMemBase]>;
404def SH : Store_rri<0b001, "sh">, Sched<[WriteSTH, ReadStoreData, ReadMemBase]>;
405def SW : Store_rri<0b010, "sw">, Sched<[WriteSTW, ReadStoreData, ReadMemBase]>;
406
407// ADDI isn't always rematerializable, but isReMaterializable will be used as
408// a hint which is verified in isReallyTriviallyReMaterializable.
409let isReMaterializable = 1, isAsCheapAsAMove = 1 in
410def ADDI  : ALU_ri<0b000, "addi">;
411
412def SLTI  : ALU_ri<0b010, "slti">;
413def SLTIU : ALU_ri<0b011, "sltiu">;
414
415let isReMaterializable = 1, isAsCheapAsAMove = 1 in {
416def XORI  : ALU_ri<0b100, "xori">;
417def ORI   : ALU_ri<0b110, "ori">;
418}
419
420def ANDI  : ALU_ri<0b111, "andi">;
421
422def SLLI : Shift_ri<0, 0b001, "slli">;
423def SRLI : Shift_ri<0, 0b101, "srli">;
424def SRAI : Shift_ri<1, 0b101, "srai">;
425
426def ADD  : ALU_rr<0b0000000, 0b000, "add">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
427def SUB  : ALU_rr<0b0100000, 0b000, "sub">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
428def SLL  : ALU_rr<0b0000000, 0b001, "sll">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
429def SLT  : ALU_rr<0b0000000, 0b010, "slt">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
430def SLTU : ALU_rr<0b0000000, 0b011, "sltu">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
431def XOR  : ALU_rr<0b0000000, 0b100, "xor">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
432def SRL  : ALU_rr<0b0000000, 0b101, "srl">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
433def SRA  : ALU_rr<0b0100000, 0b101, "sra">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
434def OR   : ALU_rr<0b0000000, 0b110, "or">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
435def AND  : ALU_rr<0b0000000, 0b111, "and">, Sched<[WriteIALU, ReadIALU, ReadIALU]>;
436
437let hasSideEffects = 1, mayLoad = 0, mayStore = 0 in {
438def FENCE : RVInstI<0b000, OPC_MISC_MEM, (outs),
439                    (ins fencearg:$pred, fencearg:$succ),
440                    "fence", "$pred, $succ">, Sched<[]> {
441  bits<4> pred;
442  bits<4> succ;
443
444  let rs1 = 0;
445  let rd = 0;
446  let imm12 = {0b0000,pred,succ};
447}
448
449def FENCE_TSO : RVInstI<0b000, OPC_MISC_MEM, (outs), (ins), "fence.tso", "">, Sched<[]> {
450  let rs1 = 0;
451  let rd = 0;
452  let imm12 = {0b1000,0b0011,0b0011};
453}
454
455def FENCE_I : RVInstI<0b001, OPC_MISC_MEM, (outs), (ins), "fence.i", "">, Sched<[]> {
456  let rs1 = 0;
457  let rd = 0;
458  let imm12 = 0;
459}
460
461def ECALL : RVInstI<0b000, OPC_SYSTEM, (outs), (ins), "ecall", "">, Sched<[WriteJmp]> {
462  let rs1 = 0;
463  let rd = 0;
464  let imm12 = 0;
465}
466
467def EBREAK : RVInstI<0b000, OPC_SYSTEM, (outs), (ins), "ebreak", "">,
468             Sched<[]> {
469  let rs1 = 0;
470  let rd = 0;
471  let imm12 = 1;
472}
473
474// This is a de facto standard (as set by GNU binutils) 32-bit unimplemented
475// instruction (i.e., it should always trap, if your implementation has invalid
476// instruction traps).
477def UNIMP : RVInstI<0b001, OPC_SYSTEM, (outs), (ins), "unimp", "">,
478            Sched<[]> {
479  let rs1 = 0;
480  let rd = 0;
481  let imm12 = 0b110000000000;
482}
483} // hasSideEffects = 1, mayLoad = 0, mayStore = 0
484
485def CSRRW : CSR_ir<0b001, "csrrw">;
486def CSRRS : CSR_ir<0b010, "csrrs">;
487def CSRRC : CSR_ir<0b011, "csrrc">;
488
489def CSRRWI : CSR_ii<0b101, "csrrwi">;
490def CSRRSI : CSR_ii<0b110, "csrrsi">;
491def CSRRCI : CSR_ii<0b111, "csrrci">;
492
493/// RV64I instructions
494
495let Predicates = [IsRV64] in {
496def LWU   : Load_ri<0b110, "lwu">, Sched<[WriteLDWU, ReadMemBase]>;
497def LD    : Load_ri<0b011, "ld">, Sched<[WriteLDD, ReadMemBase]>;
498def SD    : Store_rri<0b011, "sd">, Sched<[WriteSTD, ReadStoreData, ReadMemBase]>;
499
500let hasSideEffects = 0, mayLoad = 0, mayStore = 0 in
501def ADDIW : RVInstI<0b000, OPC_OP_IMM_32, (outs GPR:$rd),
502                    (ins GPR:$rs1, simm12:$imm12),
503                    "addiw", "$rd, $rs1, $imm12">,
504            Sched<[WriteIALU32, ReadIALU32]>;
505
506def SLLIW : ShiftW_ri<0, 0b001, "slliw">;
507def SRLIW : ShiftW_ri<0, 0b101, "srliw">;
508def SRAIW : ShiftW_ri<1, 0b101, "sraiw">;
509
510def ADDW  : ALUW_rr<0b0000000, 0b000, "addw">,
511            Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
512def SUBW  : ALUW_rr<0b0100000, 0b000, "subw">,
513            Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
514def SLLW  : ALUW_rr<0b0000000, 0b001, "sllw">,
515            Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
516def SRLW  : ALUW_rr<0b0000000, 0b101, "srlw">,
517            Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
518def SRAW  : ALUW_rr<0b0100000, 0b101, "sraw">,
519            Sched<[WriteIALU32, ReadIALU32, ReadIALU32]>;
520} // Predicates = [IsRV64]
521
522//===----------------------------------------------------------------------===//
523// Privileged instructions
524//===----------------------------------------------------------------------===//
525
526let isBarrier = 1, isReturn = 1, isTerminator = 1 in {
527def URET : Priv<"uret", 0b0000000>, Sched<[]> {
528  let rd = 0;
529  let rs1 = 0;
530  let rs2 = 0b00010;
531}
532
533def SRET : Priv<"sret", 0b0001000>, Sched<[]> {
534  let rd = 0;
535  let rs1 = 0;
536  let rs2 = 0b00010;
537}
538
539def MRET : Priv<"mret", 0b0011000>, Sched<[]> {
540  let rd = 0;
541  let rs1 = 0;
542  let rs2 = 0b00010;
543}
544} // isBarrier = 1, isReturn = 1, isTerminator = 1
545
546def WFI : Priv<"wfi", 0b0001000>, Sched<[]> {
547  let rd = 0;
548  let rs1 = 0;
549  let rs2 = 0b00101;
550}
551
552let hasSideEffects = 1, mayLoad = 0, mayStore = 0 in
553def SFENCE_VMA : RVInstR<0b0001001, 0b000, OPC_SYSTEM, (outs),
554                         (ins GPR:$rs1, GPR:$rs2),
555                         "sfence.vma", "$rs1, $rs2">, Sched<[]> {
556  let rd = 0;
557}
558
559//===----------------------------------------------------------------------===//
560// Assembler Pseudo Instructions (User-Level ISA, Version 2.2, Chapter 20)
561//===----------------------------------------------------------------------===//
562
563def : InstAlias<"nop",           (ADDI      X0,      X0,       0)>;
564
565// Note that the size is 32 because up to 8 32-bit instructions are needed to
566// generate an arbitrary 64-bit immediate. However, the size does not really
567// matter since PseudoLI is currently only used in the AsmParser where it gets
568// expanded to real instructions immediately.
569let hasSideEffects = 0, mayLoad = 0, mayStore = 0, Size = 32,
570    isCodeGenOnly = 0, isAsmParserOnly = 1 in
571def PseudoLI : Pseudo<(outs GPR:$rd), (ins ixlenimm_li:$imm), [],
572                      "li", "$rd, $imm">;
573
574def PseudoLB  : PseudoLoad<"lb">;
575def PseudoLBU : PseudoLoad<"lbu">;
576def PseudoLH  : PseudoLoad<"lh">;
577def PseudoLHU : PseudoLoad<"lhu">;
578def PseudoLW  : PseudoLoad<"lw">;
579
580def PseudoSB  : PseudoStore<"sb">;
581def PseudoSH  : PseudoStore<"sh">;
582def PseudoSW  : PseudoStore<"sw">;
583
584let Predicates = [IsRV64] in {
585def PseudoLWU : PseudoLoad<"lwu">;
586def PseudoLD  : PseudoLoad<"ld">;
587def PseudoSD  : PseudoStore<"sd">;
588} // Predicates = [IsRV64]
589
590def : InstAlias<"mv $rd, $rs",   (ADDI GPR:$rd, GPR:$rs,       0)>;
591def : InstAlias<"not $rd, $rs",  (XORI GPR:$rd, GPR:$rs,      -1)>;
592def : InstAlias<"neg $rd, $rs",  (SUB  GPR:$rd,      X0, GPR:$rs)>;
593
594let Predicates = [IsRV64] in {
595def : InstAlias<"negw $rd, $rs",   (SUBW  GPR:$rd,      X0, GPR:$rs)>;
596def : InstAlias<"sext.w $rd, $rs", (ADDIW GPR:$rd, GPR:$rs,       0)>;
597} // Predicates = [IsRV64]
598
599def : InstAlias<"seqz $rd, $rs", (SLTIU GPR:$rd, GPR:$rs,       1)>;
600def : InstAlias<"snez $rd, $rs", (SLTU  GPR:$rd,      X0, GPR:$rs)>;
601def : InstAlias<"sltz $rd, $rs", (SLT   GPR:$rd, GPR:$rs,      X0)>;
602def : InstAlias<"sgtz $rd, $rs", (SLT   GPR:$rd,      X0, GPR:$rs)>;
603
604// sgt/sgtu are recognised by the GNU assembler but the canonical slt/sltu
605// form will always be printed. Therefore, set a zero weight.
606def : InstAlias<"sgt $rd, $rs, $rt", (SLT GPR:$rd, GPR:$rt, GPR:$rs), 0>;
607def : InstAlias<"sgtu $rd, $rs, $rt", (SLTU GPR:$rd, GPR:$rt, GPR:$rs), 0>;
608
609def : InstAlias<"beqz $rs, $offset",
610                (BEQ GPR:$rs,      X0, simm13_lsb0:$offset)>;
611def : InstAlias<"bnez $rs, $offset",
612                (BNE GPR:$rs,      X0, simm13_lsb0:$offset)>;
613def : InstAlias<"blez $rs, $offset",
614                (BGE      X0, GPR:$rs, simm13_lsb0:$offset)>;
615def : InstAlias<"bgez $rs, $offset",
616                (BGE GPR:$rs,      X0, simm13_lsb0:$offset)>;
617def : InstAlias<"bltz $rs, $offset",
618                (BLT GPR:$rs,      X0, simm13_lsb0:$offset)>;
619def : InstAlias<"bgtz $rs, $offset",
620                (BLT      X0, GPR:$rs, simm13_lsb0:$offset)>;
621
622// Always output the canonical mnemonic for the pseudo branch instructions.
623// The GNU tools emit the canonical mnemonic for the branch pseudo instructions
624// as well (e.g. "bgt" will be recognised by the assembler but never printed by
625// objdump). Match this behaviour by setting a zero weight.
626def : InstAlias<"bgt $rs, $rt, $offset",
627                (BLT  GPR:$rt, GPR:$rs, simm13_lsb0:$offset), 0>;
628def : InstAlias<"ble $rs, $rt, $offset",
629                (BGE  GPR:$rt, GPR:$rs, simm13_lsb0:$offset), 0>;
630def : InstAlias<"bgtu $rs, $rt, $offset",
631                (BLTU GPR:$rt, GPR:$rs, simm13_lsb0:$offset), 0>;
632def : InstAlias<"bleu $rs, $rt, $offset",
633                (BGEU GPR:$rt, GPR:$rs, simm13_lsb0:$offset), 0>;
634
635def : InstAlias<"j $offset",   (JAL X0, simm21_lsb0_jal:$offset)>;
636def : InstAlias<"jal $offset", (JAL X1, simm21_lsb0_jal:$offset)>;
637
638// Non-zero offset aliases of "jalr" are the lowest weight, followed by the
639// two-register form, then the one-register forms and finally "ret".
640def : InstAlias<"jr $rs",                (JALR      X0, GPR:$rs, 0), 3>;
641def : InstAlias<"jr ${offset}(${rs})",   (JALR      X0, GPR:$rs, simm12:$offset)>;
642def : InstAlias<"jalr $rs",              (JALR      X1, GPR:$rs, 0), 3>;
643def : InstAlias<"jalr ${offset}(${rs})", (JALR      X1, GPR:$rs, simm12:$offset)>;
644def : InstAlias<"jalr $rd, $rs",         (JALR GPR:$rd, GPR:$rs, 0), 2>;
645def : InstAlias<"ret",                   (JALR      X0,      X1, 0), 4>;
646
647// Non-canonical forms for jump targets also accepted by the assembler.
648def : InstAlias<"jr $rs, $offset",        (JALR      X0, GPR:$rs, simm12:$offset), 0>;
649def : InstAlias<"jalr $rs, $offset",      (JALR      X1, GPR:$rs, simm12:$offset), 0>;
650def : InstAlias<"jalr $rd, $rs, $offset", (JALR GPR:$rd, GPR:$rs, simm12:$offset), 0>;
651
652def : InstAlias<"fence", (FENCE 0xF, 0xF)>; // 0xF == iorw
653
654def : InstAlias<"rdinstret $rd", (CSRRS GPR:$rd, INSTRET.Encoding, X0)>;
655def : InstAlias<"rdcycle $rd",   (CSRRS GPR:$rd, CYCLE.Encoding, X0)>;
656def : InstAlias<"rdtime $rd",    (CSRRS GPR:$rd, TIME.Encoding, X0)>;
657
658let Predicates = [IsRV32] in {
659def : InstAlias<"rdinstreth $rd", (CSRRS GPR:$rd, INSTRETH.Encoding, X0)>;
660def : InstAlias<"rdcycleh $rd",   (CSRRS GPR:$rd, CYCLEH.Encoding, X0)>;
661def : InstAlias<"rdtimeh $rd",    (CSRRS GPR:$rd, TIMEH.Encoding, X0)>;
662} // Predicates = [IsRV32]
663
664def : InstAlias<"csrr $rd, $csr", (CSRRS GPR:$rd, csr_sysreg:$csr,      X0)>;
665def : InstAlias<"csrw $csr, $rs", (CSRRW      X0, csr_sysreg:$csr, GPR:$rs)>;
666def : InstAlias<"csrs $csr, $rs", (CSRRS      X0, csr_sysreg:$csr, GPR:$rs)>;
667def : InstAlias<"csrc $csr, $rs", (CSRRC      X0, csr_sysreg:$csr, GPR:$rs)>;
668
669def : InstAlias<"csrwi $csr, $imm", (CSRRWI X0, csr_sysreg:$csr, uimm5:$imm)>;
670def : InstAlias<"csrsi $csr, $imm", (CSRRSI X0, csr_sysreg:$csr, uimm5:$imm)>;
671def : InstAlias<"csrci $csr, $imm", (CSRRCI X0, csr_sysreg:$csr, uimm5:$imm)>;
672
673let EmitPriority = 0 in {
674def : InstAlias<"csrw $csr, $imm", (CSRRWI X0, csr_sysreg:$csr, uimm5:$imm)>;
675def : InstAlias<"csrs $csr, $imm", (CSRRSI X0, csr_sysreg:$csr, uimm5:$imm)>;
676def : InstAlias<"csrc $csr, $imm", (CSRRCI X0, csr_sysreg:$csr, uimm5:$imm)>;
677
678def : InstAlias<"csrrw $rd, $csr, $imm", (CSRRWI GPR:$rd, csr_sysreg:$csr, uimm5:$imm)>;
679def : InstAlias<"csrrs $rd, $csr, $imm", (CSRRSI GPR:$rd, csr_sysreg:$csr, uimm5:$imm)>;
680def : InstAlias<"csrrc $rd, $csr, $imm", (CSRRCI GPR:$rd, csr_sysreg:$csr, uimm5:$imm)>;
681}
682
683def : InstAlias<"sfence.vma",     (SFENCE_VMA      X0, X0)>;
684def : InstAlias<"sfence.vma $rs", (SFENCE_VMA GPR:$rs, X0)>;
685
686let EmitPriority = 0 in {
687def : InstAlias<"lb $rd, (${rs1})",
688                (LB  GPR:$rd, GPR:$rs1, 0)>;
689def : InstAlias<"lh $rd, (${rs1})",
690                (LH  GPR:$rd, GPR:$rs1, 0)>;
691def : InstAlias<"lw $rd, (${rs1})",
692                (LW  GPR:$rd, GPR:$rs1, 0)>;
693def : InstAlias<"lbu $rd, (${rs1})",
694                (LBU  GPR:$rd, GPR:$rs1, 0)>;
695def : InstAlias<"lhu $rd, (${rs1})",
696                (LHU  GPR:$rd, GPR:$rs1, 0)>;
697
698def : InstAlias<"sb $rs2, (${rs1})",
699                (SB  GPR:$rs2, GPR:$rs1, 0)>;
700def : InstAlias<"sh $rs2, (${rs1})",
701                (SH  GPR:$rs2, GPR:$rs1, 0)>;
702def : InstAlias<"sw $rs2, (${rs1})",
703                (SW  GPR:$rs2, GPR:$rs1, 0)>;
704
705def : InstAlias<"add $rd, $rs1, $imm12",
706                (ADDI  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
707def : InstAlias<"and $rd, $rs1, $imm12",
708                (ANDI  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
709def : InstAlias<"xor $rd, $rs1, $imm12",
710                (XORI  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
711def : InstAlias<"or $rd, $rs1, $imm12",
712                (ORI  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
713def : InstAlias<"sll $rd, $rs1, $shamt",
714                (SLLI  GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt)>;
715def : InstAlias<"srl $rd, $rs1, $shamt",
716                (SRLI  GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt)>;
717def : InstAlias<"sra $rd, $rs1, $shamt",
718                (SRAI  GPR:$rd, GPR:$rs1, uimmlog2xlen:$shamt)>;
719let Predicates = [IsRV64] in {
720def : InstAlias<"lwu $rd, (${rs1})",
721                (LWU  GPR:$rd, GPR:$rs1, 0)>;
722def : InstAlias<"ld $rd, (${rs1})",
723                (LD  GPR:$rd, GPR:$rs1, 0)>;
724def : InstAlias<"sd $rs2, (${rs1})",
725                (SD  GPR:$rs2, GPR:$rs1, 0)>;
726
727def : InstAlias<"addw $rd, $rs1, $imm12",
728                (ADDIW  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
729def : InstAlias<"sllw $rd, $rs1, $shamt",
730                (SLLIW  GPR:$rd, GPR:$rs1, uimm5:$shamt)>;
731def : InstAlias<"srlw $rd, $rs1, $shamt",
732                (SRLIW  GPR:$rd, GPR:$rs1, uimm5:$shamt)>;
733def : InstAlias<"sraw $rd, $rs1, $shamt",
734                (SRAIW  GPR:$rd, GPR:$rs1, uimm5:$shamt)>;
735} // Predicates = [IsRV64]
736def : InstAlias<"slt $rd, $rs1, $imm12",
737                (SLTI  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
738def : InstAlias<"sltu $rd, $rs1, $imm12",
739                (SLTIU  GPR:$rd, GPR:$rs1, simm12:$imm12)>;
740}
741
742def : MnemonicAlias<"move", "mv">;
743
744// The SCALL and SBREAK instructions wererenamed to ECALL and EBREAK in
745// version 2.1 of the user-level ISA. Like the GNU toolchain, we still accept
746// the old name for backwards compatibility.
747def : MnemonicAlias<"scall", "ecall">;
748def : MnemonicAlias<"sbreak", "ebreak">;
749
750//===----------------------------------------------------------------------===//
751// Pseudo-instructions and codegen patterns
752//
753// Naming convention: For 'generic' pattern classes, we use the naming
754// convention PatTy1Ty2. For pattern classes which offer a more complex
755// expension, prefix the class name, e.g. BccPat.
756//===----------------------------------------------------------------------===//
757
758/// Generic pattern classes
759
760class PatGprGpr<SDPatternOperator OpNode, RVInst Inst>
761    : Pat<(OpNode GPR:$rs1, GPR:$rs2), (Inst GPR:$rs1, GPR:$rs2)>;
762class PatGprSimm12<SDPatternOperator OpNode, RVInstI Inst>
763    : Pat<(OpNode GPR:$rs1, simm12:$imm12), (Inst GPR:$rs1, simm12:$imm12)>;
764class PatGprUimmLog2XLen<SDPatternOperator OpNode, RVInstIShift Inst>
765    : Pat<(OpNode GPR:$rs1, uimmlog2xlen:$shamt),
766          (Inst GPR:$rs1, uimmlog2xlen:$shamt)>;
767
768/// Predicates
769
770def IsOrAdd: PatFrag<(ops node:$A, node:$B), (or node:$A, node:$B), [{
771  return isOrEquivalentToAdd(N);
772}]>;
773def assertsexti32 : PatFrag<(ops node:$src), (assertsext node:$src), [{
774  return cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32;
775}]>;
776def sexti32 : PatFrags<(ops node:$src),
777                       [(sext_inreg node:$src, i32),
778                        (assertsexti32 node:$src)]>;
779def assertzexti32 : PatFrag<(ops node:$src), (assertzext node:$src), [{
780  return cast<VTSDNode>(N->getOperand(1))->getVT() == MVT::i32;
781}]>;
782def zexti32 : PatFrags<(ops node:$src),
783                       [(and node:$src, 0xffffffff),
784                        (assertzexti32 node:$src)]>;
785
786/// Immediates
787
788def : Pat<(simm12:$imm), (ADDI X0, simm12:$imm)>;
789def : Pat<(simm32hi20:$imm), (LUI (HI20 imm:$imm))>;
790def : Pat<(simm32:$imm), (ADDI (LUI (HI20 imm:$imm)), (LO12Sext imm:$imm))>,
791      Requires<[IsRV32]>;
792
793/// Simple arithmetic operations
794
795def : PatGprGpr<add, ADD>;
796def : PatGprSimm12<add, ADDI>;
797def : PatGprGpr<sub, SUB>;
798def : PatGprGpr<or, OR>;
799def : PatGprSimm12<or, ORI>;
800def : PatGprGpr<and, AND>;
801def : PatGprSimm12<and, ANDI>;
802def : PatGprGpr<xor, XOR>;
803def : PatGprSimm12<xor, XORI>;
804def : PatGprUimmLog2XLen<shl, SLLI>;
805def : PatGprUimmLog2XLen<srl, SRLI>;
806def : PatGprUimmLog2XLen<sra, SRAI>;
807
808// Match both a plain shift and one where the shift amount is masked (this is
809// typically introduced when the legalizer promotes the shift amount and
810// zero-extends it). For RISC-V, the mask is unnecessary as shifts in the base
811// ISA only read the least significant 5 bits (RV32I) or 6 bits (RV64I).
812class shiftop<SDPatternOperator operator>
813    : PatFrags<(ops node:$val, node:$count),
814               [(operator node:$val, node:$count),
815                (operator node:$val, (and node:$count, immbottomxlenset))]>;
816
817def : PatGprGpr<shiftop<shl>, SLL>;
818def : PatGprGpr<shiftop<srl>, SRL>;
819def : PatGprGpr<shiftop<sra>, SRA>;
820
821// This is a special case of the ADD instruction used to facilitate the use of a
822// fourth operand to emit a relocation on a symbol relating to this instruction.
823// The relocation does not affect any bits of the instruction itself but is used
824// as a hint to the linker.
825let hasSideEffects = 0, mayLoad = 0, mayStore = 0, isCodeGenOnly = 0 in
826def PseudoAddTPRel : Pseudo<(outs GPR:$rd),
827                            (ins GPR:$rs1, GPR:$rs2, tprel_add_symbol:$src), [],
828                            "add", "$rd, $rs1, $rs2, $src">;
829
830/// FrameIndex calculations
831
832def : Pat<(add (i32 AddrFI:$Rs), simm12:$imm12),
833          (ADDI (i32 AddrFI:$Rs), simm12:$imm12)>;
834def : Pat<(IsOrAdd (i32 AddrFI:$Rs), simm12:$imm12),
835          (ADDI (i32 AddrFI:$Rs), simm12:$imm12)>;
836
837/// Setcc
838
839def : PatGprGpr<setlt, SLT>;
840def : PatGprSimm12<setlt, SLTI>;
841def : PatGprGpr<setult, SLTU>;
842def : PatGprSimm12<setult, SLTIU>;
843
844// Define pattern expansions for setcc operations that aren't directly
845// handled by a RISC-V instruction.
846def : Pat<(seteq GPR:$rs1, 0), (SLTIU GPR:$rs1, 1)>;
847def : Pat<(seteq GPR:$rs1, GPR:$rs2), (SLTIU (XOR GPR:$rs1, GPR:$rs2), 1)>;
848def : Pat<(seteq GPR:$rs1, simm12:$imm12),
849          (SLTIU (XORI GPR:$rs1, simm12:$imm12), 1)>;
850def : Pat<(setne GPR:$rs1, 0), (SLTU X0, GPR:$rs1)>;
851def : Pat<(setne GPR:$rs1, GPR:$rs2), (SLTU X0, (XOR GPR:$rs1, GPR:$rs2))>;
852def : Pat<(setne GPR:$rs1, simm12:$imm12),
853          (SLTU X0, (XORI GPR:$rs1, simm12:$imm12))>;
854def : Pat<(setugt GPR:$rs1, GPR:$rs2), (SLTU GPR:$rs2, GPR:$rs1)>;
855def : Pat<(setuge GPR:$rs1, GPR:$rs2), (XORI (SLTU GPR:$rs1, GPR:$rs2), 1)>;
856def : Pat<(setule GPR:$rs1, GPR:$rs2), (XORI (SLTU GPR:$rs2, GPR:$rs1), 1)>;
857def : Pat<(setgt GPR:$rs1, GPR:$rs2), (SLT GPR:$rs2, GPR:$rs1)>;
858def : Pat<(setge GPR:$rs1, GPR:$rs2), (XORI (SLT GPR:$rs1, GPR:$rs2), 1)>;
859def : Pat<(setle GPR:$rs1, GPR:$rs2), (XORI (SLT GPR:$rs2, GPR:$rs1), 1)>;
860
861let usesCustomInserter = 1 in
862class SelectCC_rrirr<RegisterClass valty, RegisterClass cmpty>
863    : Pseudo<(outs valty:$dst),
864             (ins cmpty:$lhs, cmpty:$rhs, ixlenimm:$imm,
865              valty:$truev, valty:$falsev),
866             [(set valty:$dst, (riscv_selectcc cmpty:$lhs, cmpty:$rhs,
867              (XLenVT imm:$imm), valty:$truev, valty:$falsev))]>;
868
869def Select_GPR_Using_CC_GPR : SelectCC_rrirr<GPR, GPR>;
870
871/// Branches and jumps
872
873// Match `(brcond (CondOp ..), ..)` and lower to the appropriate RISC-V branch
874// instruction.
875class BccPat<PatFrag CondOp, RVInstB Inst>
876    : Pat<(brcond (XLenVT (CondOp GPR:$rs1, GPR:$rs2)), bb:$imm12),
877          (Inst GPR:$rs1, GPR:$rs2, simm13_lsb0:$imm12)>;
878
879def : BccPat<seteq, BEQ>;
880def : BccPat<setne, BNE>;
881def : BccPat<setlt, BLT>;
882def : BccPat<setge, BGE>;
883def : BccPat<setult, BLTU>;
884def : BccPat<setuge, BGEU>;
885
886class BccSwapPat<PatFrag CondOp, RVInst InstBcc>
887    : Pat<(brcond (XLenVT (CondOp GPR:$rs1, GPR:$rs2)), bb:$imm12),
888          (InstBcc GPR:$rs2, GPR:$rs1, bb:$imm12)>;
889
890// Condition codes that don't have matching RISC-V branch instructions, but
891// are trivially supported by swapping the two input operands
892def : BccSwapPat<setgt, BLT>;
893def : BccSwapPat<setle, BGE>;
894def : BccSwapPat<setugt, BLTU>;
895def : BccSwapPat<setule, BGEU>;
896
897// An extra pattern is needed for a brcond without a setcc (i.e. where the
898// condition was calculated elsewhere).
899def : Pat<(brcond GPR:$cond, bb:$imm12), (BNE GPR:$cond, X0, bb:$imm12)>;
900
901let isBarrier = 1, isBranch = 1, isTerminator = 1 in
902def PseudoBR : Pseudo<(outs), (ins simm21_lsb0_jal:$imm20), [(br bb:$imm20)]>,
903               PseudoInstExpansion<(JAL X0, simm21_lsb0_jal:$imm20)>;
904
905let isCall = 1, Defs=[X1] in
906let isBarrier = 1, isBranch = 1, isIndirectBranch = 1, isTerminator = 1 in
907def PseudoBRIND : Pseudo<(outs), (ins GPR:$rs1, simm12:$imm12), []>,
908                  PseudoInstExpansion<(JALR X0, GPR:$rs1, simm12:$imm12)>;
909
910def : Pat<(brind GPR:$rs1), (PseudoBRIND GPR:$rs1, 0)>;
911def : Pat<(brind (add GPR:$rs1, simm12:$imm12)),
912          (PseudoBRIND GPR:$rs1, simm12:$imm12)>;
913
914// PseudoCALLReg is a generic pseudo instruction for calls which will eventually
915// expand to auipc and jalr while encoding, with any given register used as the
916// destination.
917// Define AsmString to print "call" when compile with -S flag.
918// Define isCodeGenOnly = 0 to support parsing assembly "call" instruction.
919let isCall = 1, isBarrier = 1, isCodeGenOnly = 0, hasSideEffects = 0,
920    mayStore = 0, mayLoad = 0 in
921def PseudoCALLReg : Pseudo<(outs GPR:$rd), (ins call_symbol:$func), []> {
922  let AsmString = "call\t$rd, $func";
923}
924
925// PseudoCALL is a pseudo instruction which will eventually expand to auipc
926// and jalr while encoding. This is desirable, as an auipc+jalr pair with
927// R_RISCV_CALL and R_RISCV_RELAX relocations can be be relaxed by the linker
928// if the offset fits in a signed 21-bit immediate.
929// Define AsmString to print "call" when compile with -S flag.
930// Define isCodeGenOnly = 0 to support parsing assembly "call" instruction.
931let isCall = 1, Defs = [X1], isCodeGenOnly = 0 in
932def PseudoCALL : Pseudo<(outs), (ins call_symbol:$func), []> {
933  let AsmString = "call\t$func";
934}
935
936def : Pat<(riscv_call tglobaladdr:$func), (PseudoCALL tglobaladdr:$func)>;
937def : Pat<(riscv_call texternalsym:$func), (PseudoCALL texternalsym:$func)>;
938
939def : Pat<(riscv_uret_flag), (URET X0, X0)>;
940def : Pat<(riscv_sret_flag), (SRET X0, X0)>;
941def : Pat<(riscv_mret_flag), (MRET X0, X0)>;
942
943let isCall = 1, Defs = [X1] in
944def PseudoCALLIndirect : Pseudo<(outs), (ins GPR:$rs1),
945                                [(riscv_call GPR:$rs1)]>,
946                         PseudoInstExpansion<(JALR X1, GPR:$rs1, 0)>;
947
948let isBarrier = 1, isReturn = 1, isTerminator = 1 in
949def PseudoRET : Pseudo<(outs), (ins), [(riscv_ret_flag)]>,
950                PseudoInstExpansion<(JALR X0, X1, 0)>;
951
952// PseudoTAIL is a pseudo instruction similar to PseudoCALL and will eventually
953// expand to auipc and jalr while encoding.
954// Define AsmString to print "tail" when compile with -S flag.
955let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, Uses = [X2],
956    isCodeGenOnly = 0 in
957def PseudoTAIL : Pseudo<(outs), (ins call_symbol:$dst), []> {
958  let AsmString = "tail\t$dst";
959}
960
961let isCall = 1, isTerminator = 1, isReturn = 1, isBarrier = 1, Uses = [X2] in
962def PseudoTAILIndirect : Pseudo<(outs), (ins GPRTC:$rs1),
963                                [(riscv_tail GPRTC:$rs1)]>,
964                         PseudoInstExpansion<(JALR X0, GPR:$rs1, 0)>;
965
966def : Pat<(riscv_tail (iPTR tglobaladdr:$dst)),
967          (PseudoTAIL texternalsym:$dst)>;
968def : Pat<(riscv_tail (iPTR texternalsym:$dst)),
969          (PseudoTAIL texternalsym:$dst)>;
970
971let hasSideEffects = 0, mayLoad = 0, mayStore = 0, isCodeGenOnly = 0,
972    isAsmParserOnly = 1 in
973def PseudoLLA : Pseudo<(outs GPR:$dst), (ins bare_symbol:$src), [],
974                       "lla", "$dst, $src">;
975
976let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isCodeGenOnly = 0,
977    isAsmParserOnly = 1 in
978def PseudoLA : Pseudo<(outs GPR:$dst), (ins bare_symbol:$src), [],
979                      "la", "$dst, $src">;
980
981let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isCodeGenOnly = 0,
982    isAsmParserOnly = 1 in
983def PseudoLA_TLS_IE : Pseudo<(outs GPR:$dst), (ins bare_symbol:$src), [],
984                             "la.tls.ie", "$dst, $src">;
985
986let hasSideEffects = 0, mayLoad = 1, mayStore = 0, isCodeGenOnly = 0,
987    isAsmParserOnly = 1 in
988def PseudoLA_TLS_GD : Pseudo<(outs GPR:$dst), (ins bare_symbol:$src), [],
989                             "la.tls.gd", "$dst, $src">;
990
991/// Loads
992
993multiclass LdPat<PatFrag LoadOp, RVInst Inst> {
994  def : Pat<(LoadOp GPR:$rs1), (Inst GPR:$rs1, 0)>;
995  def : Pat<(LoadOp AddrFI:$rs1), (Inst AddrFI:$rs1, 0)>;
996  def : Pat<(LoadOp (add GPR:$rs1, simm12:$imm12)),
997            (Inst GPR:$rs1, simm12:$imm12)>;
998  def : Pat<(LoadOp (add AddrFI:$rs1, simm12:$imm12)),
999            (Inst AddrFI:$rs1, simm12:$imm12)>;
1000  def : Pat<(LoadOp (IsOrAdd AddrFI:$rs1, simm12:$imm12)),
1001            (Inst AddrFI:$rs1, simm12:$imm12)>;
1002}
1003
1004defm : LdPat<sextloadi8, LB>;
1005defm : LdPat<extloadi8, LB>;
1006defm : LdPat<sextloadi16, LH>;
1007defm : LdPat<extloadi16, LH>;
1008defm : LdPat<load, LW>, Requires<[IsRV32]>;
1009defm : LdPat<zextloadi8, LBU>;
1010defm : LdPat<zextloadi16, LHU>;
1011
1012/// Stores
1013
1014multiclass StPat<PatFrag StoreOp, RVInst Inst, RegisterClass StTy> {
1015  def : Pat<(StoreOp StTy:$rs2, GPR:$rs1), (Inst StTy:$rs2, GPR:$rs1, 0)>;
1016  def : Pat<(StoreOp StTy:$rs2, AddrFI:$rs1), (Inst StTy:$rs2, AddrFI:$rs1, 0)>;
1017  def : Pat<(StoreOp StTy:$rs2, (add GPR:$rs1, simm12:$imm12)),
1018            (Inst StTy:$rs2, GPR:$rs1, simm12:$imm12)>;
1019  def : Pat<(StoreOp StTy:$rs2, (add AddrFI:$rs1, simm12:$imm12)),
1020            (Inst StTy:$rs2, AddrFI:$rs1, simm12:$imm12)>;
1021  def : Pat<(StoreOp StTy:$rs2, (IsOrAdd AddrFI:$rs1, simm12:$imm12)),
1022            (Inst StTy:$rs2, AddrFI:$rs1, simm12:$imm12)>;
1023}
1024
1025defm : StPat<truncstorei8, SB, GPR>;
1026defm : StPat<truncstorei16, SH, GPR>;
1027defm : StPat<store, SW, GPR>, Requires<[IsRV32]>;
1028
1029/// Fences
1030
1031// Refer to Table A.6 in the version 2.3 draft of the RISC-V Instruction Set
1032// Manual: Volume I.
1033
1034// fence acquire -> fence r, rw
1035def : Pat<(atomic_fence (XLenVT 4), (timm)), (FENCE 0b10, 0b11)>;
1036// fence release -> fence rw, w
1037def : Pat<(atomic_fence (XLenVT 5), (timm)), (FENCE 0b11, 0b1)>;
1038// fence acq_rel -> fence.tso
1039def : Pat<(atomic_fence (XLenVT 6), (timm)), (FENCE_TSO)>;
1040// fence seq_cst -> fence rw, rw
1041def : Pat<(atomic_fence (XLenVT 7), (timm)), (FENCE 0b11, 0b11)>;
1042
1043// Lowering for atomic load and store is defined in RISCVInstrInfoA.td.
1044// Although these are lowered to fence+load/store instructions defined in the
1045// base RV32I/RV64I ISA, this lowering is only used when the A extension is
1046// present. This is necessary as it isn't valid to mix __atomic_* libcalls
1047// with inline atomic operations for the same object.
1048
1049/// Other pseudo-instructions
1050
1051// Pessimistically assume the stack pointer will be clobbered
1052let Defs = [X2], Uses = [X2] in {
1053def ADJCALLSTACKDOWN : Pseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
1054                              [(callseq_start timm:$amt1, timm:$amt2)]>;
1055def ADJCALLSTACKUP   : Pseudo<(outs), (ins i32imm:$amt1, i32imm:$amt2),
1056                              [(callseq_end timm:$amt1, timm:$amt2)]>;
1057} // Defs = [X2], Uses = [X2]
1058
1059/// RV64 patterns
1060
1061let Predicates = [IsRV64] in {
1062
1063/// sext and zext
1064
1065def : Pat<(sext_inreg GPR:$rs1, i32), (ADDIW GPR:$rs1, 0)>;
1066def : Pat<(and GPR:$rs1, 0xffffffff), (SRLI (SLLI GPR:$rs1, 32), 32)>;
1067
1068/// ALU operations
1069
1070def : Pat<(sext_inreg (add GPR:$rs1, GPR:$rs2), i32),
1071          (ADDW GPR:$rs1, GPR:$rs2)>;
1072def : Pat<(sext_inreg (add GPR:$rs1, simm12:$imm12), i32),
1073          (ADDIW GPR:$rs1, simm12:$imm12)>;
1074def : Pat<(sext_inreg (sub GPR:$rs1, GPR:$rs2), i32),
1075          (SUBW GPR:$rs1, GPR:$rs2)>;
1076def : Pat<(sext_inreg (shl GPR:$rs1, uimm5:$shamt), i32),
1077          (SLLIW GPR:$rs1, uimm5:$shamt)>;
1078// (srl (zexti32 ...), uimm5:$shamt) is matched with custom code due to the
1079// need to undo manipulation of the mask value performed by DAGCombine.
1080def : Pat<(sra (sext_inreg GPR:$rs1, i32), uimm5:$shamt),
1081          (SRAIW GPR:$rs1, uimm5:$shamt)>;
1082
1083def : PatGprGpr<riscv_sllw, SLLW>;
1084def : PatGprGpr<riscv_srlw, SRLW>;
1085def : PatGprGpr<riscv_sraw, SRAW>;
1086
1087/// Loads
1088
1089defm : LdPat<sextloadi32, LW>;
1090defm : LdPat<extloadi32, LW>;
1091defm : LdPat<zextloadi32, LWU>;
1092defm : LdPat<load, LD>;
1093
1094/// Stores
1095
1096defm : StPat<truncstorei32, SW, GPR>;
1097defm : StPat<store, SD, GPR>;
1098} // Predicates = [IsRV64]
1099
1100/// readcyclecounter
1101// On RV64, we can directly read the 64-bit "cycle" CSR.
1102let Predicates = [IsRV64] in
1103def : Pat<(readcyclecounter), (CSRRS CYCLE.Encoding, X0)>;
1104// On RV32, ReadCycleWide will be expanded to the suggested loop reading both
1105// halves of the 64-bit "cycle" CSR.
1106let Predicates = [IsRV32], usesCustomInserter = 1, hasSideEffects = 0,
1107mayLoad = 0, mayStore = 0, hasNoSchedulingInfo = 1 in
1108def ReadCycleWide : Pseudo<(outs GPR:$lo, GPR:$hi), (ins), [], "", "">;
1109
1110/// traps
1111
1112// We lower `trap` to `unimp`, as this causes a hard exception on nearly all
1113// systems.
1114def : Pat<(trap), (UNIMP)>;
1115
1116// We lower `debugtrap` to `ebreak`, as this will get the attention of the
1117// debugger if possible.
1118def : Pat<(debugtrap), (EBREAK)>;
1119
1120//===----------------------------------------------------------------------===//
1121// Standard extensions
1122//===----------------------------------------------------------------------===//
1123
1124include "RISCVInstrInfoM.td"
1125include "RISCVInstrInfoA.td"
1126include "RISCVInstrInfoF.td"
1127include "RISCVInstrInfoD.td"
1128include "RISCVInstrInfoC.td"
1129