Skip to content
Merged
Show file tree
Hide file tree
Changes from 5 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
60 changes: 60 additions & 0 deletions llvm/lib/Transforms/Vectorize/VectorCombine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,7 @@ class VectorCombine {
Instruction &I);
bool foldExtractExtract(Instruction &I);
bool foldInsExtFNeg(Instruction &I);
bool foldInsExtBinop(Instruction &I);
bool foldInsExtVectorToShuffle(Instruction &I);
bool foldBitcastShuffle(Instruction &I);
bool scalarizeBinopOrCmp(Instruction &I);
Expand Down Expand Up @@ -738,6 +739,64 @@ bool VectorCombine::foldInsExtFNeg(Instruction &I) {
return true;
}

/// Try to fold insert(binop(x,y),binop(a,b),idx)
/// --> binop(insert(x,a,idx),insert(x,a,idx))
bool VectorCombine::foldInsExtBinop(Instruction &I) {
BinaryOperator *VecBinOp, *SclBinOp;
uint64_t Index;
if (!match(&I,
m_InsertElt(m_OneUse(m_BinOp(VecBinOp)),
m_OneUse(m_BinOp(SclBinOp)), m_ConstantInt(Index))))
return false;

// TODO: Add support for addlike etc.
Instruction::BinaryOps BinOpcode = VecBinOp->getOpcode();
if (BinOpcode != SclBinOp->getOpcode())
return false;

auto *ResultTy = dyn_cast<FixedVectorType>(I.getType());
if (!ResultTy)
return false;

// TODO: Attempt to detect m_ExtractElt for scalar operands and convert to
// shuffle?

InstructionCost OldCost = TTI.getInstructionCost(&I, CostKind) +
TTI.getInstructionCost(VecBinOp, CostKind) +
TTI.getInstructionCost(SclBinOp, CostKind);
InstructionCost NewCost =
TTI.getArithmeticInstrCost(BinOpcode, ResultTy, CostKind) +
TTI.getVectorInstrCost(Instruction::InsertElement, ResultTy, CostKind,
Index, VecBinOp->getOperand(0),
SclBinOp->getOperand(0)) +
TTI.getVectorInstrCost(Instruction::InsertElement, ResultTy, CostKind,
Index, VecBinOp->getOperand(1),
SclBinOp->getOperand(1));

LLVM_DEBUG(dbgs() << "Found an insertion of two binops: " << I
<< "\n OldCost: " << OldCost << " vs NewCost: " << NewCost
<< "\n");
if (NewCost > OldCost)
return false;

Value *NewIns0 = Builder.CreateInsertElement(VecBinOp->getOperand(0),
SclBinOp->getOperand(0), Index);
Value *NewIns1 = Builder.CreateInsertElement(VecBinOp->getOperand(1),
SclBinOp->getOperand(1), Index);
Value *NewBO = Builder.CreateBinOp(BinOpcode, NewIns0, NewIns1);

// Intersect flags from the old binops.
if (auto *NewInst = dyn_cast<Instruction>(NewBO)) {
NewInst->copyIRFlags(VecBinOp);
NewInst->andIRFlags(SclBinOp);
}

Worklist.pushValue(NewIns0);
Worklist.pushValue(NewIns1);
replaceValue(I, *NewBO);
return true;
}

/// If this is a bitcast of a shuffle, try to bitcast the source vector to the
/// destination type followed by shuffle. This can enable further transforms by
/// moving bitcasts or shuffles together.
Expand Down Expand Up @@ -3206,6 +3265,7 @@ bool VectorCombine::run() {
switch (Opcode) {
case Instruction::InsertElement:
MadeChange |= foldInsExtFNeg(I);
MadeChange |= foldInsExtBinop(I);
MadeChange |= foldInsExtVectorToShuffle(I);
break;
case Instruction::ShuffleVector:
Expand Down
Loading