From e80da0f8409aaf70d18b12c5d5d884f2c9cbddcc Mon Sep 17 00:00:00 2001 From: Craig Topper Date: Tue, 25 Apr 2023 18:46:54 -0700 Subject: [PATCH] [RISCV] Disable combineBinOpToReduce if the reduction AVL might be 0. If the reduction AVL is 0, operand 0 of the reduction will be returned rather than the scalar input. To make the fold legal, we would need to fold the new scalar value with whatever operand 0 is which may require a new scalar operation before the reduction. Block the combine if we can't prove AVL is non-zero. Reviewed By: fakepaper56 Differential Revision: https://reviews.llvm.org/D149174 --- llvm/lib/Target/RISCV/RISCVISelLowering.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp index 6d2b442..22bca31 100644 --- a/llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -9170,6 +9170,11 @@ static SDValue combineBinOpToReduce(SDNode *N, SelectionDAG &DAG, 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); -- 2.7.4