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 @@ -9170,6 +9170,11 @@ if (!ScalarV.hasOneUse()) return SDValue(); + // If the AVL is zero, operand 0 will be returned. So it's not safe to fold. + // FIXME: We might be able to improve this if operand 0 is undef. + if (!isNonZeroAVL(Reduce.getOperand(5))) + return SDValue(); + SDValue NewStart = N->getOperand(1 - ReduceIdx); SDLoc DL(N);