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