[llvm] [X86] Implement CRC32 const folding (PR #219452)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 03:55:22 PDT 2026


================
@@ -2260,6 +2260,29 @@ X86TTIImpl::instCombineIntrinsic(InstCombiner &IC, IntrinsicInst &II) const {
     }
     break;
 
+  case Intrinsic::x86_sse42_crc32_32_8:
+  case Intrinsic::x86_sse42_crc32_32_16:
+  case Intrinsic::x86_sse42_crc32_32_32:
+  case Intrinsic::x86_sse42_crc32_64_64: {
+    auto *CrcArg = dyn_cast<ConstantInt>(II.getArgOperand(0));
+    auto *DataArg = dyn_cast<ConstantInt>(II.getArgOperand(1));
+    if (!CrcArg || !DataArg)
+      break;
+
+    // If both operands are constant, we can completely constant fold this.
+    uint64_t Crc = CrcArg->getZExtValue() & 0xffffffff;
+    uint64_t Data = DataArg->getZExtValue();
+    unsigned BitWidth = DataArg->getBitWidth();
+    Crc ^= Data;
+    // CRC32C polynomial (iSCSI polynomial, bit-reversed)
+    const uint32_t Poly = 0x82F63B78;
+    for (unsigned Bit = 0; Bit != BitWidth; ++Bit) {
+      Crc = (Crc >> 1) ^ ((Crc & 1) ? Poly : 0);
+    }
----------------
RKSimon wrote:

I'm saying I don't like that we have 2 non-identical implementations trying to do the same thing. 

If you're going to work on aarch64 as well, what can we pull out into APIntOps (or MathExtra/whatever) as a yak shave PR first to reduce scope/duplication in all of these?

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


More information about the llvm-commits mailing list