diff --git a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.h b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.h --- a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.h +++ b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.h @@ -57,7 +57,6 @@ bool selectSExti32(SDValue N, SDValue &Val); bool selectZExti32(SDValue N, SDValue &Val); - bool MatchSRLIW(SDNode *N) const; bool MatchSLLIUW(SDNode *N) const; bool selectVLOp(SDValue N, SDValue &VL); diff --git a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp --- a/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp +++ b/llvm/lib/Target/RISCV/RISCVISelDAGToDAG.cpp @@ -1146,27 +1146,6 @@ return false; } -// Match (srl (and val, mask), imm) where the result would be a -// zero-extended 32-bit integer. i.e. the mask is 0xffffffff or the result -// is equivalent to this (SimplifyDemandedBits may have removed lower bits -// from the mask that aren't necessary due to the right-shifting). -bool RISCVDAGToDAGISel::MatchSRLIW(SDNode *N) const { - assert(N->getOpcode() == ISD::SRL); - assert(N->getOperand(0).getOpcode() == ISD::AND); - assert(isa(N->getOperand(1))); - assert(isa(N->getOperand(0).getOperand(1))); - - // The IsRV64 predicate is checked after PatFrag predicates so we can get - // here even on RV32. - if (!Subtarget->is64Bit()) - return false; - - SDValue And = N->getOperand(0); - uint64_t ShAmt = N->getConstantOperandVal(1); - uint64_t Mask = And.getConstantOperandVal(1); - return (Mask | maskTrailingOnes(ShAmt)) == 0xffffffff; -} - // Check that it is a SLLIUW (Shift Logical Left Immediate Unsigned i32 // on RV64). // SLLIUW is the same as SLLI except for the fact that it clears the bits 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 @@ -4681,16 +4681,36 @@ // Clear all non-demanded bits initially. APInt ShrunkMask = Mask & DemandedBits; + // Try to make a smaller immediate by setting undemanded bits. + + APInt ExpandedMask = Mask | ~DemandedBits; + + auto IsLegalMask = [ShrunkMask, ExpandedMask](const APInt &Mask) -> bool { + return ShrunkMask.isSubsetOf(Mask) && Mask.isSubsetOf(ExpandedMask); + }; + auto UseMask = [Mask, Op, VT, &TLO](const APInt &NewMask) -> bool { + if (NewMask == Mask) + return true; + SDLoc DL(Op); + SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT); + SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC); + return TLO.CombineTo(Op, NewOp); + }; + // If the shrunk mask fits in sign extended 12 bits, let the target // independent code apply it. if (ShrunkMask.isSignedIntN(12)) return false; - // Try to make a smaller immediate by setting undemanded bits. + // Try to preserve (and X, 0xffffffff), the (zext_inreg X, i32) pattern. + if (VT == MVT::i64) { + APInt NewMask = APInt(64, 0xffffffff); + if (IsLegalMask(NewMask)) + return UseMask(NewMask); + } - // We need to be able to make a negative number through a combination of mask - // and undemanded bits. - APInt ExpandedMask = Mask | ~DemandedBits; + // For the remaining optimizations, we need to be able to make a negative + // number through a combination of mask and undemanded bits. if (!ExpandedMask.isNegative()) return false; @@ -4708,18 +4728,8 @@ return false; // Sanity check that our new mask is a subset of the demanded mask. - assert(NewMask.isSubsetOf(ExpandedMask)); - - // If we aren't changing the mask, just return true to keep it and prevent - // the caller from optimizing. - if (NewMask == Mask) - return true; - - // Replace the constant with the new mask. - SDLoc DL(Op); - SDValue NewC = TLO.DAG.getConstant(NewMask, DL, VT); - SDValue NewOp = TLO.DAG.getNode(ISD::AND, DL, VT, Op.getOperand(0), NewC); - return TLO.CombineTo(Op, NewOp); + assert(IsLegalMask(NewMask)); + return UseMask(NewMask); } void RISCVTargetLowering::computeKnownBitsForTargetNode(const SDValue Op, diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfo.td b/llvm/lib/Target/RISCV/RISCVInstrInfo.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfo.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfo.td @@ -862,11 +862,6 @@ }]>; def zexti32 : ComplexPattern; -def SRLIWPat : PatFrag<(ops node:$A, node:$B), - (srl (and node:$A, imm), node:$B), [{ - return MatchSRLIW(N); -}]>; - // Check that it is a SLLIUW (Shift Logical Left Immediate Unsigned i32 // on RV64). Also used to optimize the same sequence without SLLIUW. def SLLIUWPat : PatFrag<(ops node:$A, node:$B), @@ -1186,7 +1181,7 @@ (SUBW GPR:$rs1, GPR:$rs2)>; def : Pat<(sext_inreg (shl GPR:$rs1, uimm5:$shamt), i32), (SLLIW GPR:$rs1, uimm5:$shamt)>; -def : Pat<(i64 (SRLIWPat GPR:$rs1, uimm5:$shamt)), +def : Pat<(i64 (srl (and GPR:$rs1, 0xffffffff), uimm5:$shamt)), (SRLIW GPR:$rs1, uimm5:$shamt)>; def : Pat<(i64 (srl (shl GPR:$rs1, (i64 32)), uimm6gt32:$shamt)), (SRLIW GPR:$rs1, (ImmSub32 uimm6gt32:$shamt))>; diff --git a/llvm/lib/Target/RISCV/RISCVInstrInfoB.td b/llvm/lib/Target/RISCV/RISCVInstrInfoB.td --- a/llvm/lib/Target/RISCV/RISCVInstrInfoB.td +++ b/llvm/lib/Target/RISCV/RISCVInstrInfoB.td @@ -871,6 +871,6 @@ i32)), (PACKW GPR:$rs1, GPR:$rs2)>; def : Pat<(i64 (or (and (assertsexti32 GPR:$rs2), 0xFFFFFFFFFFFF0000), - (SRLIWPat GPR:$rs1, (i64 16)))), + (srl (and GPR:$rs1, 0xFFFFFFFF), (i64 16)))), (PACKUW GPR:$rs1, GPR:$rs2)>; } // Predicates = [HasStdExtZbp, IsRV64]