[llvm] [LoopIdiom] Fix miscompile for big-endian sub-byte CRC (PR #213040)

Sean Clarke via llvm-commits llvm-commits at lists.llvm.org
Fri Jul 31 07:25:45 PDT 2026


================
@@ -1816,15 +1816,13 @@ void LoopIdiomRecognize::optimizeCRCLoopUsingTableLookup(
     };
     auto HiIdx = [LoByte, CRCBW](IRBuilderBase &Builder, Value *Op,
                                  const Twine &Name) {
-      Type *OpTy = Op->getType();
-
-      // When the bitwidth of the CRC mismatches the Op's bitwidth, we need to
-      // use the CRC's bitwidth as the reference for shifting right.
-      return LoByte(Builder,
-                    CRCBW > 8 ? Builder.CreateLShr(
-                                    Op, ConstantInt::get(OpTy, CRCBW - 8), Name)
-                              : Op,
-                    Name + ".lo.byte");
+      // Shift the top bits of Op to the bottom byte by using the CRC bitwidth
+      // as a reference.
+      if (CRCBW != 8) {
+        Op = CRCBW > 8 ? Builder.CreateLShr(Op, CRCBW - 8, Name)
----------------
xarkenz wrote:

Yes, it truncates Op down to `i8`, which avoids a potential crash when zexting to the index type (see #161509). This logic to `lshr` then use `LoByte` was already in place, so I don't think there's any need to change it here.

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


More information about the llvm-commits mailing list