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

Shreesh Adiga via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 22 03:33:28 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();
----------------
tantei3 wrote:

For `_mm_crc32_u64` intrinsic, Data is `uint64_t` and Crc parameter is also defined as `uint64_t` like `unsigned __int64 _mm_crc32_u64 (unsigned __int64 crc, unsigned __int64 v)` in the header file, therefore expression like `_mm_crc32_u64(-1, -1)` would end up getting sign extended, whereas the actual operation is done only on 32 bits, thus Crc value needs to be masked off. 

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


More information about the llvm-commits mailing list