Index: llvm/lib/Target/RISCV/RISCVISelLowering.cpp =================================================================== --- llvm/lib/Target/RISCV/RISCVISelLowering.cpp +++ llvm/lib/Target/RISCV/RISCVISelLowering.cpp @@ -12260,12 +12260,19 @@ SDValue TrueVal, SDValue FalseVal, bool Swapped) { bool Commutative = true; + bool AllOnesIsIdentity = false; switch (TrueVal.getOpcode()) { default: return SDValue(); + case ISD::SHL: + case ISD::SRA: + case ISD::SRL: case ISD::SUB: Commutative = false; break; + case ISD::AND: + AllOnesIsIdentity = true; + break; case ISD::ADD: case ISD::OR: case ISD::XOR: @@ -12285,12 +12292,15 @@ EVT VT = N->getValueType(0); SDLoc DL(N); - SDValue Zero = DAG.getConstant(0, DL, VT); SDValue OtherOp = TrueVal.getOperand(1 - OpToFold); + EVT OtherOpVT = OtherOp->getValueType(0); + SDValue IdentityOperand = OtherOpVT.isFloatingPoint() ? + DAG.getConstantFP(0.0, DL, OtherOpVT) : + AllOnesIsIdentity ? DAG.getAllOnesConstant(DL, OtherOpVT) : DAG.getConstant(0, DL, OtherOpVT); if (Swapped) - std::swap(OtherOp, Zero); - SDValue NewSel = DAG.getSelect(DL, VT, N->getOperand(0), OtherOp, Zero); + std::swap(OtherOp, IdentityOperand); + SDValue NewSel = DAG.getSelect(DL, OtherOpVT, N->getOperand(0), OtherOp, IdentityOperand); return DAG.getNode(TrueVal.getOpcode(), DL, VT, FalseVal, NewSel); }