[llvm] [VectorCombine] Fold bitcast(bitreverse.v8i8(bitcast(IntTy))) into bswap+bitreverse (PR #209037)

Deepak Shirke via llvm-commits llvm-commits at lists.llvm.org
Thu Jul 16 08:44:28 PDT 2026


================
@@ -6114,49 +6114,88 @@ bool VectorCombine::foldBitcastOfVPLoad(Instruction &I) {
 
 /// 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)))
+///   bswap(bitreverse(x)) <--> bitcast(bitreverse(bitcast(x)))
+///   bitreverse(bswap(x)) <--> bitcast(bitreverse(bitcast(x)))
+/// The direction of the fold is cost-model driven.
 bool VectorCombine::foldBitOrderReverseAndSwap(Instruction &I) {
   Value *X;
+
+  if (match(&I, m_BitCast(m_BitReverse(m_BitCast(m_Value(X)))))) {
+    Type *Ty = X->getType();
+    Type *VecTy = cast<BitCastInst>(&I)->getOperand(0)->getType();
+    // Only handle integer scalar types with even byte count (bswap requirement)
+    // and fixed-length byte vectors.
+    if (Ty->isIntegerTy() && Ty == I.getType() && isa<FixedVectorType>(VecTy) &&
+        cast<FixedVectorType>(VecTy)->getElementType()->isIntegerTy(8) &&
+        Ty->getIntegerBitWidth() % 16 == 0) {
+      auto *OuterBitCast = cast<BitCastInst>(&I);
----------------
deepakshirkem wrote:

Done ::)

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


More information about the llvm-commits mailing list