Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2487,21 +2487,31 @@ bool VectorCombine::foldShuffleOfCastops(Instruction &I) {
if (!match(&I, m_Shuffle(m_Value(V0), m_Value(V1), m_Mask(OldMask))))
return false;

// Check whether this is a binary shuffle.
bool IsBinaryShuffle = !isa<UndefValue>(V1);

auto *C0 = dyn_cast<CastInst>(V0);
auto *C1 = dyn_cast<CastInst>(V1);
if (!C0 || !C1)
if (!C0 || (IsBinaryShuffle && !C1))
return false;

Instruction::CastOps Opcode = C0->getOpcode();
if (C0->getSrcTy() != C1->getSrcTy())

// If this is allowed, foldShuffleOfCastops can get stuck in a loop
// with foldBitcastOfShuffle. Reject in favor of foldBitcastOfShuffle.
if (!IsBinaryShuffle && Opcode == Instruction::BitCast)
return false;

// Handle shuffle(zext_nneg(x), sext(y)) -> sext(shuffle(x,y)) folds.
if (Opcode != C1->getOpcode()) {
if (match(C0, m_SExtLike(m_Value())) && match(C1, m_SExtLike(m_Value())))
Opcode = Instruction::SExt;
else
if (IsBinaryShuffle) {
if (C0->getSrcTy() != C1->getSrcTy())
return false;
// Handle shuffle(zext_nneg(x), sext(y)) -> sext(shuffle(x,y)) folds.
if (Opcode != C1->getOpcode()) {
if (match(C0, m_SExtLike(m_Value())) && match(C1, m_SExtLike(m_Value())))
Opcode = Instruction::SExt;
else
return false;
}
}

auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType());
Expand Down Expand Up @@ -2544,38 +2554,52 @@ bool VectorCombine::foldShuffleOfCastops(Instruction &I) {
InstructionCost CostC0 =
TTI.getCastInstrCost(C0->getOpcode(), CastDstTy, CastSrcTy,
TTI::CastContextHint::None, CostKind);
InstructionCost CostC1 =
TTI.getCastInstrCost(C1->getOpcode(), CastDstTy, CastSrcTy,
TTI::CastContextHint::None, CostKind);
InstructionCost OldCost = CostC0 + CostC1;
OldCost +=
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, ShuffleDstTy,
CastDstTy, OldMask, CostKind, 0, nullptr, {}, &I);

InstructionCost NewCost =
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, NewShuffleDstTy,
CastSrcTy, NewMask, CostKind);
TargetTransformInfo::ShuffleKind ShuffleKind;
if (IsBinaryShuffle)
ShuffleKind = TargetTransformInfo::SK_PermuteTwoSrc;
else
ShuffleKind = TargetTransformInfo::SK_PermuteSingleSrc;

InstructionCost OldCost = CostC0;
OldCost += TTI.getShuffleCost(ShuffleKind, ShuffleDstTy, CastDstTy, OldMask,
CostKind, 0, nullptr, {}, &I);

InstructionCost NewCost = TTI.getShuffleCost(ShuffleKind, NewShuffleDstTy,
CastSrcTy, NewMask, CostKind);
NewCost += TTI.getCastInstrCost(Opcode, ShuffleDstTy, NewShuffleDstTy,
TTI::CastContextHint::None, CostKind);
if (!C0->hasOneUse())
NewCost += CostC0;
if (!C1->hasOneUse())
NewCost += CostC1;
if (IsBinaryShuffle) {
InstructionCost CostC1 =
TTI.getCastInstrCost(C1->getOpcode(), CastDstTy, CastSrcTy,
TTI::CastContextHint::None, CostKind);
OldCost += CostC1;
if (!C1->hasOneUse())
NewCost += CostC1;
}

LLVM_DEBUG(dbgs() << "Found a shuffle feeding two casts: " << I
<< "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
<< "\n");
if (NewCost > OldCost)
return false;

Value *Shuf = Builder.CreateShuffleVector(C0->getOperand(0),
C1->getOperand(0), NewMask);
Value *Shuf;
if (IsBinaryShuffle)
Shuf = Builder.CreateShuffleVector(C0->getOperand(0), C1->getOperand(0),
NewMask);
else
Shuf = Builder.CreateShuffleVector(C0->getOperand(0), NewMask);

Value *Cast = Builder.CreateCast(Opcode, Shuf, ShuffleDstTy);

// Intersect flags from the old casts.
if (auto *NewInst = dyn_cast<Instruction>(Cast)) {
NewInst->copyIRFlags(C0);
NewInst->andIRFlags(C1);
if (IsBinaryShuffle)
NewInst->andIRFlags(C1);
}

Worklist.pushValue(Shuf);
Expand Down
Loading