[llvm] [VectorCombine] Combine BinOp with extract/insert to vector BinOp (PR #115213)

Min-Yih Hsu via llvm-commits llvm-commits at lists.llvm.org
Wed Nov 6 16:59:44 PST 2024


================
@@ -2678,6 +2679,52 @@ bool VectorCombine::shrinkType(llvm::Instruction &I) {
   return true;
 }
 
+/// insert (DstVec, (extract (binop), ExtIdx), InsIdx) -->
+/// shuffl (DstVec, (binop), Mask)
+bool VectorCombine::foldInsExtOfBinOpShuffle(Instruction &I) {
+  Value *DstVec;
+  BinaryOperator *BO;
+  uint64_t ExtIdx, InsIdx;
+  if (!match(&I, m_InsertElt(
+                     m_Value(DstVec),
+                     m_OneUse(m_ExtractElt(m_BinOp(BO), m_ConstantInt(ExtIdx))),
+                     m_ConstantInt(InsIdx))))
+    return false;
+
+  if (!isSafeToSpeculativelyExecute(BO))
+    return false;
+
+  auto *VecTy = cast<FixedVectorType>(I.getType());
+  if (BO->getType() != VecTy)
+    return false;
+
+  unsigned NumElts = VecTy->getNumElements();
+  if (ExtIdx >= NumElts)
+    return false;
+
+  SmallVector<int> Mask(NumElts);
+  std::iota(Mask.begin(), Mask.end(), 0);
+  Mask[InsIdx] = ExtIdx + NumElts;
+  // Cost
+  ExtractElementInst *Ext;
+  if ((Ext = dyn_cast<ExtractElementInst>(I.getOperand(0))) == nullptr)
----------------
mshockwave wrote:

`if (!isa<ExtractElementInst>(I.getOperand(0)))` ?

https://github.com/llvm/llvm-project/pull/115213


More information about the llvm-commits mailing list