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

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 03:17:01 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:

It'd be better if we at least matched the same eval pattern as interp__builtin_ia32_crc32 etc

But I'm tempted to try and get this added as a APIntOps method - do other targets have similar CRC32 instructions?

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


More information about the llvm-commits mailing list