[llvm] [VectorCombine] Combine extract/insert from vector (PR #115213)

Simon Pilgrim via llvm-commits llvm-commits at lists.llvm.org
Fri Nov 8 02:59:31 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));
----------------
RKSimon wrote:

The match above has confirmed that I is an InsertElementInst - so this would be better as:
```
auto *Ins = cast<InsertElementInst>(&I): 
auto *Ext = cast<ExtractElementInst>(I.getOperand(1)) ;
```

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


More information about the llvm-commits mailing list