Skip to content

Commit 84ae923

Browse files
committed
[VectorCombine] Relax vector type constraint on bitop(bitcast, bitcast)
1 parent c79de90 commit 84ae923

File tree

2 files changed

+17
-19
lines changed

2 files changed

+17
-19
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -870,38 +870,37 @@ bool VectorCombine::foldBitOpOfCastops(Instruction &I) {
870870
if (LHSSrc->getType() != RHSSrc->getType())
871871
return false;
872872

873-
// Only handle vector types with integer elements
874-
auto *SrcVecTy = dyn_cast<FixedVectorType>(LHSSrc->getType());
875-
auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType());
876-
if (!SrcVecTy || !DstVecTy)
873+
auto *SrcTy = LHSSrc->getType();
874+
auto *DstTy = I.getType();
875+
// Only handle vector types with integer elements if the cast is not bitcast
876+
if (CastOpcode != Instruction::BitCast &&
877+
(!isa<FixedVectorType>(SrcTy) || !isa<FixedVectorType>(DstTy)))
877878
return false;
878879

879-
if (!SrcVecTy->getScalarType()->isIntegerTy() ||
880-
!DstVecTy->getScalarType()->isIntegerTy())
880+
if (!SrcTy->getScalarType()->isIntegerTy() ||
881+
!DstTy->getScalarType()->isIntegerTy())
881882
return false;
882883

883884
// Cost Check :
884885
// OldCost = bitlogic + 2*casts
885886
// NewCost = bitlogic + cast
886887

887888
// Calculate specific costs for each cast with instruction context
888-
InstructionCost LHSCastCost =
889-
TTI.getCastInstrCost(CastOpcode, DstVecTy, SrcVecTy,
890-
TTI::CastContextHint::None, CostKind, LHSCast);
891-
InstructionCost RHSCastCost =
892-
TTI.getCastInstrCost(CastOpcode, DstVecTy, SrcVecTy,
893-
TTI::CastContextHint::None, CostKind, RHSCast);
889+
InstructionCost LHSCastCost = TTI.getCastInstrCost(
890+
CastOpcode, DstTy, SrcTy, TTI::CastContextHint::None, CostKind, LHSCast);
891+
InstructionCost RHSCastCost = TTI.getCastInstrCost(
892+
CastOpcode, DstTy, SrcTy, TTI::CastContextHint::None, CostKind, RHSCast);
894893

895894
InstructionCost OldCost =
896-
TTI.getArithmeticInstrCost(BinOp->getOpcode(), DstVecTy, CostKind) +
895+
TTI.getArithmeticInstrCost(BinOp->getOpcode(), DstTy, CostKind) +
897896
LHSCastCost + RHSCastCost;
898897

899898
// For new cost, we can't provide an instruction (it doesn't exist yet)
900899
InstructionCost GenericCastCost = TTI.getCastInstrCost(
901-
CastOpcode, DstVecTy, SrcVecTy, TTI::CastContextHint::None, CostKind);
900+
CastOpcode, DstTy, SrcTy, TTI::CastContextHint::None, CostKind);
902901

903902
InstructionCost NewCost =
904-
TTI.getArithmeticInstrCost(BinOp->getOpcode(), SrcVecTy, CostKind) +
903+
TTI.getArithmeticInstrCost(BinOp->getOpcode(), SrcTy, CostKind) +
905904
GenericCastCost;
906905

907906
// Account for multi-use casts using specific costs

llvm/test/Transforms/VectorCombine/X86/bitop-of-castops.ll

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,10 +510,9 @@ define <16 x i1> @xor_bitcast_i16_to_v16i1_constant(i16 %a) {
510510

511511
define <16 x i1> @xor_bitcast_i16_to_v16i1(i16 %a, i16 %b) {
512512
; CHECK-LABEL: @xor_bitcast_i16_to_v16i1(
513-
; CHECK-NEXT: [[BC1:%.*]] = bitcast i16 [[A:%.*]] to <16 x i1>
514-
; CHECK-NEXT: [[BC2:%.*]] = bitcast i16 [[B:%.*]] to <16 x i1>
515-
; CHECK-NEXT: [[OR:%.*]] = xor <16 x i1> [[BC1]], [[BC2]]
516-
; CHECK-NEXT: ret <16 x i1> [[OR]]
513+
; CHECK-NEXT: [[B:%.*]] = xor i16 [[A:%.*]], [[B1:%.*]]
514+
; CHECK-NEXT: [[BC2:%.*]] = bitcast i16 [[B]] to <16 x i1>
515+
; CHECK-NEXT: ret <16 x i1> [[BC2]]
517516
;
518517
%bc1 = bitcast i16 %a to <16 x i1>
519518
%bc2 = bitcast i16 %b to <16 x i1>

0 commit comments

Comments
 (0)