[llvm] [RISCV] Canonicalize mask-producing SETCC for easier conversion on the consuming instruction to be VL-predicate (PR #214877)

Craig Topper via llvm-commits llvm-commits at lists.llvm.org
Mon Aug 17 10:55:45 PDT 2026


================
@@ -18750,6 +18750,89 @@ static bool narrowIndex(SDValue &N, ISD::MemIndexType IndexType, SelectionDAG &D
   return true;
 }
 
+/// Given
+/// ```
+/// %b = splat %base
+/// %s = <%offset, %offset + 1, %offset + 2, %offset + 3, ...>
+/// %a = add nuw %b, %s
+/// %N = splat %n
+/// %p = setcc ult %a, %N
+/// ```
+/// in which the SETCC's condition code could be (U)LT or (U)GT.
+/// We can turn it into
+/// ```
+/// %s = <0, 1, 2, 3, ...>
+/// %m = sub %n, min(%n, %base + %offset)
+/// %M = splat %m
+/// %p = setcc ult %s, %M
+/// ```
+/// where the SETCC's condition code is also canonicalized into (U)LT;
+/// The idea behind this canonicalization is that if %p is used as a mask
+/// in a masked.load/store, we can easily turn it to use VL-predicate later
+/// by assigning `vl = min(%m, <number of vector elements>)`.
+static SDValue canonicalizeMaskForVLPredicate(EVT MaskVT, SDValue LHS,
+                                              SDValue RHS, ISD::CondCode CC,
+                                              const SDLoc &DL,
+                                              SelectionDAG &DAG) {
+  using namespace SDPatternMatch;
+  if (!MaskVT.isFixedLengthVector() ||
+      !(CC == ISD::SETUGT || CC == ISD::SETGT || CC == ISD::SETULT ||
+        CC == ISD::SETLT) ||
+      !LHS.getValueType().isInteger())
+    return SDValue();
+
+  // Canonicalize the comparison.
+  if (CC == ISD::SETUGT || CC == ISD::SETGT) {
+    std::swap(LHS, RHS);
+    // Flip the 'L' and 'G' bits.
+    CC = ISD::CondCode(static_cast<unsigned>(CC) ^ 6);
----------------
topperc wrote:

Use getSetCCSwappedOperands?

https://github.com/llvm/llvm-project/pull/214877


More information about the llvm-commits mailing list