[llvm] [VectorCombine] Combine extract/insert from vector (PR #115213)
via llvm-commits
llvm-commits at lists.llvm.org
Fri Nov 8 10:25:05 PST 2024
================
@@ -2678,6 +2679,49 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
return true;
}
+/// insert (DstVec, (extract SrcVec, ExtIdx), InsIdx) -->
+/// shuffle (DstVec, SrcVec, Mask)
+bool VectorCombine::foldInsExtVectorToShuffle(Instruction &I) {
+ Value *DstVec, *SrcVec;
+ uint64_t ExtIdx, InsIdx;
+ if (!match(&I, m_InsertElt(m_Value(DstVec),
+ m_OneUse(m_ExtractElt(m_Value(SrcVec),
+ m_ConstantInt(ExtIdx))),
+ m_ConstantInt(InsIdx))))
+ return false;
+
+ auto *VecTy = dyn_cast<FixedVectorType>(I.getType());
+ if (!VecTy || SrcVec->getType() != VecTy)
+ return false;
+
+ unsigned NumElts = VecTy->getNumElements();
+ if (ExtIdx >= NumElts)
+ return false;
+
+ SmallVector<int> Mask(NumElts, 0);
+ std::iota(Mask.begin(), Mask.end(), 0);
+ Mask[InsIdx] = ExtIdx + NumElts;
+ // Cost
+ ExtractElementInst *Ext;
+ Ext = isa<ExtractElementInst>(I.getOperand(0))
+ ? cast<ExtractElementInst>(I.getOperand(0))
+ : cast<ExtractElementInst>(I.getOperand(1));
+
+ TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
+ InstructionCost OldCost =
+ TTI.getVectorInstrCost(*Ext, VecTy, CostKind, ExtIdx);
+ InstructionCost NewCost =
+ TTI.getShuffleCost(TargetTransformInfo::SK_Select, VecTy, Mask);
----------------
ParkHanbum wrote:
I'm not familiar with vectorcombine so I made a mistake, thanks for letting me know and I'll try harder.
https://github.com/llvm/llvm-project/pull/115213
More information about the llvm-commits
mailing list