[llvm] [RISCV] Turn certain cases of masked.load into vp.load + vp.merge (PR #214350)
Craig Topper via llvm-commits
llvm-commits at lists.llvm.org
Thu Aug 6 10:52:16 PDT 2026
================
@@ -20657,6 +20658,104 @@ static SDValue performVP_STORECombine(SDNode *N, SelectionDAG &DAG,
VPStore->isCompressingStore());
}
+/// Given
+/// ```
+/// %b = splat %base
+/// %s = <0, 1, 2, 3, ...>
+/// %a = add nuw %b, %s
+/// %N = splat %n
+/// %m = icmp ult %a, %N
+/// %v = mask.load %p, %m, %passthru
+/// ```
+/// we can turn this use a vp.load + vp.merge instead to avoid
+/// emitting mask. The VL of these two vp operations would be
+/// `min(%n - min(%n, %base + %offset), numElements)`
+/// where %offset is the start value of `%s` and numElements is the
+/// fixed vector size.
+static SDValue performMaskedLoadToVPLoadCombine(SDNode *N, SelectionDAG &DAG) {
+ using namespace SDPatternMatch;
+ auto *MLoad = cast<MaskedLoadSDNode>(N);
+ EVT MaskVT = MLoad->getMask().getValueType();
+ assert(MaskVT.isVector());
+ if (!MaskVT.isFixedLengthVector())
+ return SDValue();
+ unsigned NumElements = MaskVT.getVectorNumElements();
+ SDLoc DL(N);
+
+ SDValue SetCCLHS, SetCCRHS;
+ ISD::CondCode CC;
+ if (!sd_match(MLoad->getMask(), m_SetCC(m_Value(SetCCLHS), m_Value(SetCCRHS),
+ m_CondCode(CC))) ||
+ !(ISD::isUnsignedIntSetCC(CC) || ISD::isSignedIntSetCC(CC)))
+ return SDValue();
+ // Canonicalize the comparison.
+ if (CC == ISD::SETUGT || CC == ISD::SETUGE || CC == ISD::SETGT ||
+ CC == ISD::SETGE) {
+ std::swap(SetCCLHS, SetCCRHS);
+ // Flip the 'L' and 'G' bits;
+ CC = ISD::CondCode(static_cast<unsigned>(CC) ^ 6);
+ }
+ bool IsSigned = ISD::isSignedIntSetCC(CC);
+
+ SDValue Boundary = DAG.getSplatValue(SetCCRHS, /*LegalizeType=*/true);
+ if (!Boundary)
+ return SDValue();
+
+ SDValue LHSOp0, LHSOp1;
+ if (!sd_match(SetCCLHS, m_AddLike(m_Value(LHSOp0), m_Value(LHSOp1))))
+ return SDValue();
+ auto Flags = SetCCLHS->getFlags();
+ if (!Flags.hasDisjoint() &&
----------------
topperc wrote:
Should we have m_NSWAddLike and m_NUWAddLike like IR PatternMatch.h?
https://github.com/llvm/llvm-project/pull/214350
More information about the llvm-commits
mailing list