@@ -1013,19 +1013,22 @@ bool VectorCombine::foldBitOpOfCastConstant(Instruction &I) {
10131013
10141014 Value *LHSSrc = LHSCast->getOperand (0 );
10151015
1016- // Only handle vector types with integer elements
1017- auto *SrcVecTy = dyn_cast<FixedVectorType>(LHSSrc->getType ());
1018- auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType ());
1019- if (!SrcVecTy || !DstVecTy)
1016+ auto *SrcTy = LHSSrc->getType ();
1017+ auto *DstTy = I.getType ();
1018+ // Bitcasts can handle scalar/vector mixes, such as i16 -> <16 x i1>.
1019+ // Other casts only handle vector types with integer elements.
1020+ if (CastOpcode != Instruction::BitCast &&
1021+ (!isa<FixedVectorType>(SrcTy) || !isa<FixedVectorType>(DstTy)))
10201022 return false ;
10211023
1022- if (!SrcVecTy->getScalarType ()->isIntegerTy () ||
1023- !DstVecTy->getScalarType ()->isIntegerTy ())
1024+ // Only integer scalar/vector values are legal for bitwise logic operations.
1025+ if (!SrcTy->getScalarType ()->isIntegerTy () ||
1026+ !DstTy->getScalarType ()->isIntegerTy ())
10241027 return false ;
10251028
10261029 // Find the constant InvC, such that castop(InvC) equals to C.
10271030 PreservedCastFlags RHSFlags;
1028- Constant *InvC = getLosslessInvCast (C, SrcVecTy , CastOpcode, *DL, RHSFlags);
1031+ Constant *InvC = getLosslessInvCast (C, SrcTy , CastOpcode, *DL, RHSFlags);
10291032 if (!InvC)
10301033 return false ;
10311034
@@ -1034,20 +1037,18 @@ bool VectorCombine::foldBitOpOfCastConstant(Instruction &I) {
10341037 // NewCost = bitlogic + cast
10351038
10361039 // Calculate specific costs for each cast with instruction context
1037- InstructionCost LHSCastCost =
1038- TTI.getCastInstrCost (CastOpcode, DstVecTy, SrcVecTy,
1039- TTI::CastContextHint::None, CostKind, LHSCast);
1040+ InstructionCost LHSCastCost = TTI.getCastInstrCost (
1041+ CastOpcode, DstTy, SrcTy, TTI::CastContextHint::None, CostKind, LHSCast);
10401042
10411043 InstructionCost OldCost =
1042- TTI.getArithmeticInstrCost (I.getOpcode (), DstVecTy, CostKind) +
1043- LHSCastCost;
1044+ TTI.getArithmeticInstrCost (I.getOpcode (), DstTy, CostKind) + LHSCastCost;
10441045
10451046 // For new cost, we can't provide an instruction (it doesn't exist yet)
10461047 InstructionCost GenericCastCost = TTI.getCastInstrCost (
1047- CastOpcode, DstVecTy, SrcVecTy , TTI::CastContextHint::None, CostKind);
1048+ CastOpcode, DstTy, SrcTy , TTI::CastContextHint::None, CostKind);
10481049
10491050 InstructionCost NewCost =
1050- TTI.getArithmeticInstrCost (I.getOpcode (), SrcVecTy , CostKind) +
1051+ TTI.getArithmeticInstrCost (I.getOpcode (), SrcTy , CostKind) +
10511052 GenericCastCost;
10521053
10531054 // Account for multi-use casts using specific costs
0 commit comments