[llvm] [X86] combineOr - undemand and freeze elements if the other operand's element is allones (PR #215538)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Thu Aug 13 01:11:43 PDT 2026


================
@@ -53857,27 +53857,25 @@ static SDValue combineOr(SDNode *N, SelectionDAG &DAG,
     if (SDValue Res = combineX86ShufflesRecursively(Op, DAG, Subtarget))
       return Res;
 
-    // If either operand is a constant mask, then only the elements that aren't
-    // allones are actually demanded by the other operand.
-    auto SimplifyUndemandedElts = [&](SDValue Op, SDValue OtherOp) {
-      APInt UndefElts;
-      SmallVector<APInt> EltBits;
-      int NumElts = VT.getVectorNumElements();
-      int EltSizeInBits = VT.getScalarSizeInBits();
-      if (!getTargetConstantBitsFromNode(Op, EltSizeInBits, UndefElts, EltBits))
-        return false;
-
+    // If second operand is a constant mask, then only the elements that aren't
+    // allones are actually demanded by the first operand.
+    APInt UndefElts;
+    SmallVector<APInt> EltBits;
+    int NumElts = VT.getVectorNumElements();
+    int EltSizeInBits = VT.getScalarSizeInBits();
+    if (getTargetConstantBitsFromNode(N1, EltSizeInBits, UndefElts, EltBits)) {
       APInt DemandedElts = APInt::getZero(NumElts);
       for (int I = 0; I != NumElts; ++I)
         if (!EltBits[I].isAllOnes())
           DemandedElts.setBit(I);
 
-      return TLI.SimplifyDemandedVectorElts(OtherOp, DemandedElts, DCI);
-    };
-    if (SimplifyUndemandedElts(N0, N1) || SimplifyUndemandedElts(N1, N0)) {
-      if (N->getOpcode() != ISD::DELETED_NODE)
-        DCI.AddToWorklist(N);
-      return SDValue(N, 0);
+      // We must freeze the result to prevent OR(poison,-1) -> poison.
+      if (!DemandedElts.isAllOnes() && !DAG.isGuaranteedNotToBePoison(N0) &&
----------------
RKSimon wrote:

What this is trying to do is prevent an infinite loop whereby the SimplifyDemandedVectorElts removes the existing FREEZE (because its safe to do so for the demanded elts), and then we add the freeze back again..... 

I'll push a tweaked version shortly but there's no way around it (we're trying to say we don't demand certain elements but then demand that they stay frozen......)

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


More information about the llvm-commits mailing list