[llvm] [VectorCombine] fold nested bitreverse and bswap (PR #202236)

Kenny Lau via llvm-commits llvm-commits at lists.llvm.org
Fri Jun 12 13:02:11 PDT 2026


================
@@ -5946,6 +5908,74 @@ bool VectorCombine::foldBitcastOfVPLoad(Instruction &I) {
   return true;
 }
 
+/// Fold the following cases into a single byte-level bit-reverse operation
+/// and accepts bswap and bitreverse intrinsics:
+///   bswap(bitreverse(x)) --> bitcast(bitreverse(bitcast(x)))
+///   bitreverse(bswap(x)) --> bitcast(bitreverse(bitcast(x)))
+bool VectorCombine::foldBitOrderReverseAndSwap(Instruction &I) {
+  auto *II = dyn_cast<IntrinsicInst>(&I);
+  if (!II)
+    return false;
+
+  Intrinsic::ID IntrID = II->getIntrinsicID();
+  if (IntrID != Intrinsic::bitreverse && IntrID != Intrinsic::bswap)
+    return false;
+
+  // No fold on scalable vectors
----------------
lauk20 wrote:

> why not?

@RKSimon Seems like I misunderstood that scalable vectors could not be folded. I've updated the PR to handle scalable vectors and address the changes.

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


More information about the llvm-commits mailing list