Skip to content

Commit 8c7674b

Browse files
committed
Revert "[VectorCombine] Allow shuffling between vectors the same type but different element sizes (llvm#121216)"
This reverts commit 8c1dbac.
1 parent e470dca commit 8c7674b

File tree

4 files changed

+18
-438
lines changed

4 files changed

+18
-438
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -3147,73 +3147,42 @@ bool VectorCombine::foldInsExtVectorToShuffle(Instruction &I) {
31473147
m_ConstantInt(InsIdx))))
31483148
return false;
31493149

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)
31553152
return false;
31563153

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)
31603156
return false;
31613157

31623158
// Insertion into poison is a cheaper single operand shuffle.
31633159
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)) {
31703162
SK = TargetTransformInfo::SK_PermuteSingleSrc;
3171-
if (!IsExtIdxInBounds && NeedExpOrNarrow)
3172-
Mask[InsIdx] = 0;
3173-
else
3174-
Mask[InsIdx] = ExtIdx;
3163+
Mask[InsIdx] = ExtIdx;
31753164
std::swap(DstVec, SrcVec);
31763165
} else {
31773166
SK = TargetTransformInfo::SK_PermuteTwoSrc;
31783167
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;
31833169
}
31843170

31853171
// Cost
31863172
auto *Ins = cast<InsertElementInst>(&I);
31873173
auto *Ext = cast<ExtractElementInst>(I.getOperand(1));
31883174
InstructionCost InsCost =
3189-
TTI.getVectorInstrCost(*Ins, DstVecTy, CostKind, InsIdx);
3175+
TTI.getVectorInstrCost(*Ins, VecTy, CostKind, InsIdx);
31903176
InstructionCost ExtCost =
3191-
TTI.getVectorInstrCost(*Ext, DstVecTy, CostKind, ExtIdx);
3177+
TTI.getVectorInstrCost(*Ext, VecTy, CostKind, ExtIdx);
31923178
InstructionCost OldCost = ExtCost + InsCost;
31933179

3180+
// Ignore 'free' identity insertion shuffle.
3181+
// TODO: getShuffleCost should return TCC_Free for Identity shuffles.
31943182
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});
32173186
if (!Ext->hasOneUse())
32183187
NewCost += ExtCost;
32193188

@@ -3224,16 +3193,9 @@ bool VectorCombine::foldInsExtVectorToShuffle(Instruction &I) {
32243193
if (OldCost < NewCost)
32253194
return false;
32263195

3227-
if (NeedExpOrNarrow) {
3228-
if (!NeedDstSrcSwap)
3229-
SrcVec = Builder.CreateShuffleVector(SrcVec, ExtToVecMask);
3230-
else
3231-
DstVec = Builder.CreateShuffleVector(DstVec, ExtToVecMask);
3232-
}
3233-
32343196
// Canonicalize undef param to RHS to help further folds.
32353197
if (isa<UndefValue>(DstVec) && !isa<UndefValue>(SrcVec)) {
3236-
ShuffleVectorInst::commuteShuffleMask(Mask, NumDstElts);
3198+
ShuffleVectorInst::commuteShuffleMask(Mask, NumElts);
32373199
std::swap(DstVec, SrcVec);
32383200
}
32393201

llvm/test/Transforms/VectorCombine/X86/extract-insert-poison.ll

Lines changed: 0 additions & 196 deletions
This file was deleted.

0 commit comments

Comments
 (0)