@@ -3147,73 +3147,42 @@ bool VectorCombine::foldInsExtVectorToShuffle(Instruction &I) {
3147
3147
m_ConstantInt (InsIdx))))
3148
3148
return false ;
3149
3149
3150
- auto *DstVecTy = dyn_cast<FixedVectorType>(I.getType ());
3151
- auto *SrcVecTy = dyn_cast<FixedVectorType>(SrcVec->getType ());
3152
- // We can try combining vectors with different element sizes.
3153
- if (!DstVecTy || !SrcVecTy ||
3154
- SrcVecTy->getElementType () != DstVecTy->getElementType ())
3150
+ auto *VecTy = dyn_cast<FixedVectorType>(I.getType ());
3151
+ if (!VecTy || SrcVec->getType () != VecTy)
3155
3152
return false ;
3156
3153
3157
- unsigned NumDstElts = DstVecTy->getNumElements ();
3158
- unsigned NumSrcElts = SrcVecTy->getNumElements ();
3159
- if (InsIdx >= NumDstElts || ExtIdx >= NumSrcElts || NumDstElts == 1 )
3154
+ unsigned NumElts = VecTy->getNumElements ();
3155
+ if (ExtIdx >= NumElts || InsIdx >= NumElts)
3160
3156
return false ;
3161
3157
3162
3158
// Insertion into poison is a cheaper single operand shuffle.
3163
3159
TargetTransformInfo::ShuffleKind SK;
3164
- SmallVector<int > Mask (NumDstElts, PoisonMaskElem);
3165
-
3166
- bool NeedExpOrNarrow = NumSrcElts != NumDstElts;
3167
- bool IsExtIdxInBounds = ExtIdx < NumDstElts;
3168
- bool NeedDstSrcSwap = isa<PoisonValue>(DstVec) && !isa<UndefValue>(SrcVec);
3169
- if (NeedDstSrcSwap) {
3160
+ SmallVector<int > Mask (NumElts, PoisonMaskElem);
3161
+ if (isa<PoisonValue>(DstVec) && !isa<UndefValue>(SrcVec)) {
3170
3162
SK = TargetTransformInfo::SK_PermuteSingleSrc;
3171
- if (!IsExtIdxInBounds && NeedExpOrNarrow)
3172
- Mask[InsIdx] = 0 ;
3173
- else
3174
- Mask[InsIdx] = ExtIdx;
3163
+ Mask[InsIdx] = ExtIdx;
3175
3164
std::swap (DstVec, SrcVec);
3176
3165
} else {
3177
3166
SK = TargetTransformInfo::SK_PermuteTwoSrc;
3178
3167
std::iota (Mask.begin (), Mask.end (), 0 );
3179
- if (!IsExtIdxInBounds && NeedExpOrNarrow)
3180
- Mask[InsIdx] = NumDstElts;
3181
- else
3182
- Mask[InsIdx] = ExtIdx + NumDstElts;
3168
+ Mask[InsIdx] = ExtIdx + NumElts;
3183
3169
}
3184
3170
3185
3171
// Cost
3186
3172
auto *Ins = cast<InsertElementInst>(&I);
3187
3173
auto *Ext = cast<ExtractElementInst>(I.getOperand (1 ));
3188
3174
InstructionCost InsCost =
3189
- TTI.getVectorInstrCost (*Ins, DstVecTy , CostKind, InsIdx);
3175
+ TTI.getVectorInstrCost (*Ins, VecTy , CostKind, InsIdx);
3190
3176
InstructionCost ExtCost =
3191
- TTI.getVectorInstrCost (*Ext, DstVecTy , CostKind, ExtIdx);
3177
+ TTI.getVectorInstrCost (*Ext, VecTy , CostKind, ExtIdx);
3192
3178
InstructionCost OldCost = ExtCost + InsCost;
3193
3179
3180
+ // Ignore 'free' identity insertion shuffle.
3181
+ // TODO: getShuffleCost should return TCC_Free for Identity shuffles.
3194
3182
InstructionCost NewCost = 0 ;
3195
- SmallVector<int > ExtToVecMask;
3196
- if (!NeedExpOrNarrow) {
3197
- // Ignore 'free' identity insertion shuffle.
3198
- // TODO: getShuffleCost should return TCC_Free for Identity shuffles.
3199
- if (!ShuffleVectorInst::isIdentityMask (Mask, NumSrcElts))
3200
- NewCost += TTI.getShuffleCost (SK, DstVecTy, Mask, CostKind, 0 , nullptr ,
3201
- {DstVec, SrcVec});
3202
- } else {
3203
- // When creating length-changing-vector, always create with a Mask whose
3204
- // first element has an ExtIdx, so that the first element of the vector
3205
- // being created is always the target to be extracted.
3206
- ExtToVecMask.assign (NumDstElts, PoisonMaskElem);
3207
- if (IsExtIdxInBounds)
3208
- ExtToVecMask[ExtIdx] = ExtIdx;
3209
- else
3210
- ExtToVecMask[0 ] = ExtIdx;
3211
- // Add cost for expanding or narrowing
3212
- NewCost = TTI.getShuffleCost (TargetTransformInfo::SK_PermuteSingleSrc,
3213
- DstVecTy, ExtToVecMask, CostKind);
3214
- NewCost += TTI.getShuffleCost (SK, DstVecTy, Mask, CostKind);
3215
- }
3216
-
3183
+ if (!ShuffleVectorInst::isIdentityMask (Mask, NumElts))
3184
+ NewCost += TTI.getShuffleCost (SK, VecTy, Mask, CostKind, 0 , nullptr ,
3185
+ {DstVec, SrcVec});
3217
3186
if (!Ext->hasOneUse ())
3218
3187
NewCost += ExtCost;
3219
3188
@@ -3224,16 +3193,9 @@ bool VectorCombine::foldInsExtVectorToShuffle(Instruction &I) {
3224
3193
if (OldCost < NewCost)
3225
3194
return false ;
3226
3195
3227
- if (NeedExpOrNarrow) {
3228
- if (!NeedDstSrcSwap)
3229
- SrcVec = Builder.CreateShuffleVector (SrcVec, ExtToVecMask);
3230
- else
3231
- DstVec = Builder.CreateShuffleVector (DstVec, ExtToVecMask);
3232
- }
3233
-
3234
3196
// Canonicalize undef param to RHS to help further folds.
3235
3197
if (isa<UndefValue>(DstVec) && !isa<UndefValue>(SrcVec)) {
3236
- ShuffleVectorInst::commuteShuffleMask (Mask, NumDstElts );
3198
+ ShuffleVectorInst::commuteShuffleMask (Mask, NumElts );
3237
3199
std::swap (DstVec, SrcVec);
3238
3200
}
3239
3201
0 commit comments