diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp index 16e3a94839c6..be7b32da8e9f 100644 --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -2319,8 +2319,15 @@ static MachineBasicBlock *addVSetVL(MachineInstr &MI, MachineBasicBlock *BB, // FIXME: This is conservatively correct, but we might want to detect that // the input is undefined. bool TailAgnostic = true; - if (MI.isRegTiedToUseOperand(0) && !WritesElement0) + unsigned UseOpIdx; + if (MI.isRegTiedToUseOperand(0, &UseOpIdx) && !WritesElement0) { TailAgnostic = false; + // If the tied operand is an IMPLICIT_DEF we can keep TailAgnostic. + const MachineOperand &UseMO = MI.getOperand(UseOpIdx); + MachineInstr *UseMI = MRI.getVRegDef(UseMO.getReg()); + if (UseMI && UseMI->isImplicitDef()) + TailAgnostic = true; + } // For simplicity we reuse the vtype representation here. MIB.addImm(RISCVVType::encodeVTYPE(VLMul, ElementWidth, diff --git a/llvm/test/CodeGen/RISCV/rvv/vcompress-rv32.ll b/llvm/test/CodeGen/RISCV/rvv/vcompress-rv32.ll index c8a0488e9056..2a26a1f8fd9a 100644 --- a/llvm/test/CodeGen/RISCV/rvv/vcompress-rv32.ll +++ b/llvm/test/CodeGen/RISCV/rvv/vcompress-rv32.ll @@ -738,3 +738,21 @@ entry: ret %a } + +; Test with undef for the dest operand. This should use tail agnostic policy. +define @intrinsic_vcompress_um_nxv1i8_nxv1i8( %0, %1, i32 %2) nounwind { +; CHECK-LABEL: intrinsic_vcompress_um_nxv1i8_nxv1i8: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetvli a0, a0, e8,mf8,ta,mu +; CHECK-NEXT: vcompress.vm v25, v16, v0 +; CHECK-NEXT: vmv1r.v v16, v25 +; CHECK-NEXT: jalr zero, 0(ra) +entry: + %a = call @llvm.riscv.vcompress.nxv1i8( + undef, + %0, + %1, + i32 %2) + + ret %a +} diff --git a/llvm/test/CodeGen/RISCV/rvv/vcompress-rv64.ll b/llvm/test/CodeGen/RISCV/rvv/vcompress-rv64.ll index dd1b48983344..d4ca98bcdec5 100644 --- a/llvm/test/CodeGen/RISCV/rvv/vcompress-rv64.ll +++ b/llvm/test/CodeGen/RISCV/rvv/vcompress-rv64.ll @@ -828,3 +828,20 @@ entry: ret %a } + +; Test with undef for the dest operand. This should use tail agnostic policy. +define @intrinsic_vcompress_um_nxv1i8_nxv1i8( %0, %1, i64 %2) nounwind { +; CHECK-LABEL: intrinsic_vcompress_um_nxv1i8_nxv1i8: +; CHECK: # %bb.0: # %entry +; CHECK-NEXT: vsetvli a0, a0, e8,mf8,ta,mu +; CHECK-NEXT: vcompress.vm v25, v16, v0 +; CHECK-NEXT: vmv1r.v v16, v25 +; CHECK-NEXT: jalr zero, 0(ra) +entry: + %a = call @llvm.riscv.vcompress.nxv1i8( + undef, + %0, + %1, + i64 %2) + ret %a +}