diff --git a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp --- a/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp +++ b/llvm/lib/Target/RISCV/RISCVInsertVSETVLI.cpp @@ -633,6 +633,8 @@ void RISCVInsertVSETVLI::emitVSETVLIs(MachineBasicBlock &MBB) { VSETVLIInfo CurInfo; + // Only be set if current VSETVLIInfo is from an explicit VSET(I)VLI. + MachineInstr* PrevVSETVLIMI = nullptr; for (MachineInstr &MI : MBB) { // If this is an explicit VSETVLI or VSETIVLI, update our state. @@ -645,6 +647,7 @@ MI.getOperand(3).setIsDead(false); MI.getOperand(4).setIsDead(false); CurInfo = getInfoForVSETVLI(MI); + PrevVSETVLIMI = &MI; continue; } @@ -678,10 +681,28 @@ // If this instruction isn't compatible with the previous VL/VTYPE // we need to insert a VSETVLI. if (needVSETVLI(NewInfo, CurInfo)) { - insertVSETVLI(MBB, MI, NewInfo, CurInfo); + // If the previous VL/VTYPE is set by VSETVLI and do not use, Merge it + // with current VL/VTYPE. + bool NeedInsertVSETVLI = true; + if (PrevVSETVLIMI) { + bool HasSameAVL = + CurInfo.hasSameAVL(NewInfo) || + (NewInfo.hasAVLReg() && NewInfo.getAVLReg().isVirtual() && + NewInfo.getAVLReg() == PrevVSETVLIMI->getOperand(0).getReg()); + // If these two VSETVLI have the same AVL and the same VLMAX, + // we could merge these two VSETVLI. + if (HasSameAVL && + CurInfo.getSEWLMULRatio() == NewInfo.getSEWLMULRatio()) { + PrevVSETVLIMI->getOperand(2).setImm(NewInfo.encodeVTYPE()); + NeedInsertVSETVLI = false; + } + } + if (NeedInsertVSETVLI) + insertVSETVLI(MBB, MI, NewInfo, CurInfo); CurInfo = NewInfo; } } + PrevVSETVLIMI = nullptr; } // If this is something updates VL/VTYPE that we don't know about, set @@ -689,6 +710,7 @@ if (MI.isCall() || MI.isInlineAsm() || MI.modifiesRegister(RISCV::VL) || MI.modifiesRegister(RISCV::VTYPE)) { CurInfo = VSETVLIInfo::getUnknown(); + PrevVSETVLIMI = nullptr; } } } diff --git a/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll new file mode 100644 --- /dev/null +++ b/llvm/test/CodeGen/RISCV/rvv/vsetvli-insert.ll @@ -0,0 +1,46 @@ +; NOTE: Assertions have been autogenerated by utils/update_llc_test_checks.py +; RUN: llc -mtriple=riscv64 -mattr=+m,+f,+d,+a,+c,+experimental-v \ +; RUN: -verify-machineinstrs -O2 < %s | FileCheck %s + +declare i64 @llvm.riscv.vsetvli(i64, i64, i64) +declare @llvm.riscv.vfadd.nxv1f64.nxv1f64( + , + , + i64) +declare @llvm.riscv.vle.mask.nxv1i64( + , + *, + , + i64) + +define @test1(i64 %avl, %a, %b) nounwind { +; CHECK-LABEL: test1: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetvli a0, a0, e64, m1, ta, mu +; CHECK-NEXT: vfadd.vv v8, v8, v9 +; CHECK-NEXT: ret +entry: + %0 = tail call i64 @llvm.riscv.vsetvli(i64 %avl, i64 2, i64 7) + %1 = tail call @llvm.riscv.vfadd.nxv1f64.nxv1f64( + %a, + %b, + i64 %0) + ret %1 +} + +define @test2(i64 %avl, %a, * %b, %c) nounwind { +; CHECK-LABEL: test2: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetvli a0, a0, e64, m1, tu, mu +; CHECK-NEXT: vle64.v v8, (a1), v0.t +; CHECK-NEXT: ret +entry: + %0 = tail call i64 @llvm.riscv.vsetvli(i64 %avl, i64 3, i64 0) + %1 = call @llvm.riscv.vle.mask.nxv1i64( + %a, + * %b, + %c, + i64 %0) + + ret %1 +}