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

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Mon Jul 13 02:35:26 PDT 2026


================
@@ -6118,6 +6118,45 @@ bool VectorCombine::foldBitcastOfVPLoad(Instruction &I) {
 ///   bitreverse(bswap(x)) --> bitcast(bitreverse(bitcast(x)))
 bool VectorCombine::foldBitOrderReverseAndSwap(Instruction &I) {
   Value *X;
+
+  if (match(&I, m_BitCast(m_Intrinsic<Intrinsic::bitreverse>(
+                    m_BitCast(m_Value(X)))))) {
+    Type *Ty = X->getType();
+    Type *VecTy = cast<BitCastInst>(&I)->getOperand(0)->getType();
+    if (Ty->isIntegerTy() && Ty == I.getType() && isa<FixedVectorType>(VecTy) &&
+        cast<FixedVectorType>(VecTy)->getElementType()->isIntegerTy(8) &&
+        Ty->getIntegerBitWidth() % 16 == 0) {
+      auto *InnerCall = cast<Instruction>(cast<BitCastInst>(&I)->getOperand(0));
+      // OldCost = bitcast to vec + bitreverse.v8i8 + bitcast to int
+      InstructionCost OldCost =
+          TTI.getCastInstrCost(Instruction::BitCast, VecTy, Ty,
+                               TTI::CastContextHint::None, CostKind) +
+          TTI.getInstructionCost(InnerCall, CostKind) +
+          TTI.getCastInstrCost(Instruction::BitCast, Ty, VecTy,
+                               TTI::CastContextHint::None, CostKind);
+      // NewCost = bswap + bitreverse on integer type.
+      // If bitreverse.v8i8 has other uses we cannot remove it,
----------------
RKSimon wrote:

not just v8i8?

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


More information about the llvm-commits mailing list