diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -501,6 +501,10 @@ if (Subtarget.is64Bit()) setOperationAction(ISD::INTRINSIC_WO_CHAIN, MVT::i32, Custom); + if (Subtarget.hasStdExtZicbop()) { + setOperationAction(ISD::PREFETCH, MVT::Other, Legal); + } + if (Subtarget.hasStdExtA()) { setMaxAtomicSizeInBitsSupported(Subtarget.getXLen()); setMinCmpXchgSizeInBits(32); diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoZicbo.td b/llvm/lib/Target/RISCV/RISCVInstrInfoZicbo.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfoZicbo.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoZicbo.td @@ -42,7 +42,7 @@ let rd = 0b00000; } -let hasSideEffects = 0, mayLoad = 1, mayStore = 0 in +let hasSideEffects = 0, mayLoad = 1, mayStore = 1 in class Prefetch_ri optype, string opcodestr> : RVInstS<0b110, OPC_OP_IMM, (outs), (ins GPR:$rs1, simm12_lsb00000:$imm12), opcodestr, "${imm12}(${rs1})"> { @@ -69,3 +69,17 @@ def PREFETCH_R : Prefetch_ri<0b00001, "prefetch.r">, Sched<[]>; def PREFETCH_W : Prefetch_ri<0b00011, "prefetch.w">, Sched<[]>; } // Predicates = [HasStdExtZicbop] + +//===----------------------------------------------------------------------===// +// Patterns +//===----------------------------------------------------------------------===// + +let Predicates = [HasStdExtZicbop] in { + // FIXME: Match address with offset + def : Pat<(prefetch GPR:$rs1, imm, imm, (XLenVT 0)), + (PREFETCH_I GPR:$rs1, 0)>; + def : Pat<(prefetch GPR:$rs1, (XLenVT 0), imm, (XLenVT 1)), + (PREFETCH_R GPR:$rs1, 0)>; + def : Pat<(prefetch GPR:$rs1, (XLenVT 1), imm, (XLenVT 1)), + (PREFETCH_W GPR:$rs1, 0)>; +} diff --git a/llvm/test/CodeGen/RISCV/prefetch.ll b/llvm/test/CodeGen/RISCV/prefetch.ll --- a/llvm/test/CodeGen/RISCV/prefetch.ll +++ b/llvm/test/CodeGen/RISCV/prefetch.ll @@ -3,17 +3,75 @@ ; RUN: | FileCheck -check-prefix=RV32I %s ; RUN: llc -mtriple=riscv64 -verify-machineinstrs < %s \ ; RUN: | FileCheck -check-prefix=RV64I %s +; RUN: llc -mtriple=riscv32 -mattr=+zicbop -verify-machineinstrs < %s \ +; RUN: | FileCheck -check-prefix=RV32ZICBOP %s +; RUN: llc -mtriple=riscv64 -mattr=zicbop -verify-machineinstrs < %s \ +; RUN: | FileCheck -check-prefix=RV64ZICBOP %s declare void @llvm.prefetch(ptr, i32, i32, i32) -define void @test_prefetch(ptr %a) nounwind { -; RV32I-LABEL: test_prefetch: +define void @test_prefetch_read(ptr %a) nounwind { +; RV32I-LABEL: test_prefetch_read: ; RV32I: # %bb.0: ; RV32I-NEXT: ret ; -; RV64I-LABEL: test_prefetch: +; RV64I-LABEL: test_prefetch_read: ; RV64I: # %bb.0: ; RV64I-NEXT: ret +; +; RV32ZICBOP-LABEL: test_prefetch_read: +; RV32ZICBOP: # %bb.0: +; RV32ZICBOP-NEXT: prefetch.r 0(a0) +; RV32ZICBOP-NEXT: ret +; +; RV64ZICBOP-LABEL: test_prefetch_read: +; RV64ZICBOP: # %bb.0: +; RV64ZICBOP-NEXT: prefetch.r 0(a0) +; RV64ZICBOP-NEXT: ret call void @llvm.prefetch(ptr %a, i32 0, i32 2, i32 1) ret void } + +define void @test_prefetch_write(ptr %a) nounwind { +; RV32I-LABEL: test_prefetch_write: +; RV32I: # %bb.0: +; RV32I-NEXT: ret +; +; RV64I-LABEL: test_prefetch_write: +; RV64I: # %bb.0: +; RV64I-NEXT: ret +; +; RV32ZICBOP-LABEL: test_prefetch_write: +; RV32ZICBOP: # %bb.0: +; RV32ZICBOP-NEXT: prefetch.w 0(a0) +; RV32ZICBOP-NEXT: ret +; +; RV64ZICBOP-LABEL: test_prefetch_write: +; RV64ZICBOP: # %bb.0: +; RV64ZICBOP-NEXT: prefetch.w 0(a0) +; RV64ZICBOP-NEXT: ret + call void @llvm.prefetch(ptr %a, i32 1, i32 2, i32 1) + ret void +} + +define void @test_prefetch_instruction(ptr %a) nounwind { +; RV32I-LABEL: test_prefetch_instruction: +; RV32I: # %bb.0: +; RV32I-NEXT: ret +; +; RV64I-LABEL: test_prefetch_instruction: +; RV64I: # %bb.0: +; RV64I-NEXT: ret +; +; RV32ZICBOP-LABEL: test_prefetch_instruction: +; RV32ZICBOP: # %bb.0: +; RV32ZICBOP-NEXT: prefetch.i 0(a0) +; RV32ZICBOP-NEXT: ret +; +; RV64ZICBOP-LABEL: test_prefetch_instruction: +; RV64ZICBOP: # %bb.0: +; RV64ZICBOP-NEXT: prefetch.i 0(a0) +; RV64ZICBOP-NEXT: ret + call void @llvm.prefetch(ptr %a, i32 0, i32 2, i32 0) + ret void +}