[llvm] [WebAssembly] Fix missed optimization in 50142 (PR #144741)
Luke Lau via llvm-commits
llvm-commits at lists.llvm.org
Fri Jun 20 04:59:04 PDT 2025
================
@@ -3239,6 +3239,42 @@ static SDValue performBitcastCombine(SDNode *N,
return SDValue();
}
+static SDValue performAnyTrueCombine(SDNode *N, SelectionDAG &DAG) {
+ // any_true (setcc <X>, 0, eq)
+ // => not (all_true X)
+
+ SDLoc DL(N);
+ assert(N->getOpcode() == ISD::INTRINSIC_WO_CHAIN);
+ if (N->getConstantOperandVal(0) != Intrinsic::wasm_anytrue)
+ return SDValue();
+
+ SDValue SetCC = N->getOperand(1);
+ if (SetCC.getOpcode() != ISD::SETCC)
+ return SDValue();
+
+ SDValue LHS = SetCC->getOperand(0);
+ SDValue RHS = SetCC->getOperand(1);
+ ISD::CondCode Cond = cast<CondCodeSDNode>(SetCC->getOperand(2))->get();
+ EVT LT = LHS.getValueType();
+ unsigned NumElts = LT.getVectorNumElements();
+ if (NumElts != 2 && NumElts != 4 && NumElts != 8 && NumElts != 16)
+ return SDValue();
+
+ EVT Width = MVT::getIntegerVT(128 / NumElts);
+
+ if (!isNullOrNullSplat(RHS) || Cond != ISD::SETEQ)
+ return SDValue();
+
+ SDValue Ret = DAG.getZExtOrTrunc(
+ DAG.getNode(
+ ISD::INTRINSIC_WO_CHAIN, DL, MVT::i32,
+ {DAG.getConstant(Intrinsic::wasm_alltrue, DL, MVT::i32),
+ DAG.getSExtOrTrunc(LHS, DL, LT.changeVectorElementType(Width))}),
----------------
lukel97 wrote:
I don't think we can truncate the underlying vector, e.g. if we had `any_true (setcc (v4i64 x), 0, eq)` and we were to fold that `not (all_true (trunc (v4i64 x)))` then we would return the wrong result if any of the bits 32 and above were set.
I think we need to bail if `LT.getScalarSizeInBits() > Width`, can you also add a test for this case?
https://github.com/llvm/llvm-project/pull/144741
More information about the llvm-commits
mailing list