[llvm] [X86] Fold nested VGF2P8AFFINEQB instructions (PR #195210)

via llvm-commits llvm-commits at lists.llvm.org
Thu May 7 22:12:02 PDT 2026


================
@@ -62136,6 +62155,83 @@ static SDValue combineKSHIFT(SDNode *N, SelectionDAG &DAG,
   return SDValue();
 }
 
+// Fold: GF2P8AFFINEQB(GF2P8AFFINEQB(X, YSub), YSup)
+//    => GF2P8AFFINEQB(X, YFolded)
+// Permuting the sub-matrix by the super-matrix at a byte, rather than bit,
+// granularity produces a matrix that performs both permutations at once.
+static SDValue combineNestedGF2P8AFFINEQB(SDNode *N, const SDLoc &DL,
+                                          SelectionDAG &DAG, EVT VT) {
+  using namespace SDPatternMatch;
+
+  unsigned VecWidth = VT.getSizeInBits();
+  unsigned NumElts = VT.getVectorNumElements();
+  unsigned EltWidth = VT.getScalarSizeInBits();
+
+  SDValue X, YSub, YSup;
+  APInt ImmSub, ImmSup, ConstUndef;
+  SmallVector<APInt> YSubEltBits, YSupEltBits;
+
+  if (!(sd_match(N, m_TernaryOp(X86ISD::GF2P8AFFINEQB,
+                                m_TernaryOp(X86ISD::GF2P8AFFINEQB, m_Value(X),
+                                            m_Value(YSub), m_ConstInt(ImmSub)),
+                                m_Value(YSup), m_ConstInt(ImmSup))) &&
+        getTargetConstantBitsFromNode(YSub, EltWidth, ConstUndef, YSubEltBits,
+                                      /*AllowWholeUndefs=*/false) &&
+        getTargetConstantBitsFromNode(YSup, EltWidth, ConstUndef, YSupEltBits,
+                                      /*AllowWholeUndefs=*/false)))
+    return SDValue();
+
+  APInt SubM(VecWidth, 0);
+  APInt SupM(VecWidth, 0);
+  for (unsigned i = 0; i < NumElts; ++i) {
+    SubM.insertBits(YSubEltBits[i], i * EltWidth);
+    SupM.insertBits(YSupEltBits[i], i * EltWidth);
----------------
WalterKruger wrote:

I tried to use `VecWidth` as the `EltSizeInBits` argument but it failed to fold the double nested "reverse_reverse_reverse" case (well passing the rest).

When I added an assert between it and a regular width `getTargetConstantBitsFromNode`, it failed. Although it does recognize it when using an element size of 64 bits, just not 128 or greater.

@RKSimon 

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


More information about the llvm-commits mailing list