[llvm] [VectorCombine] Fold chain of (scalar load)->ext->ext to load->ext. (PR #141109)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Fri May 23 01:53:32 PDT 2025
================
@@ -2777,6 +2778,55 @@ bool VectorCombine::foldShuffleToIdentity(Instruction &I) {
return true;
}
+bool VectorCombine::foldShuffleExtExtracts(Instruction &I) {
+ // Try to fold vector zero- and sign-extends split across multiple operations
+ // into a single extend, removing redundant inserts and shuffles.
+
+ // Check if we have an extended shuffle that selects the first vector, which
+ // itself is another extend fed by a load.
+ Instruction *L;
+ if (!match(
+ &I,
+ m_OneUse(m_Shuffle(
+ m_OneUse(m_ZExtOrSExt(m_OneUse(m_BitCast(m_OneUse(m_InsertElt(
+ m_Value(), m_OneUse(m_Instruction(L)), m_SpecificInt(0))))))),
+ m_Value()))) ||
+ !cast<ShuffleVectorInst>(&I)->isIdentityWithExtract() ||
+ !isa<LoadInst>(L))
+ return false;
+ auto *InnerExt = cast<Instruction>(I.getOperand(0));
+ auto *OuterExt = dyn_cast<Instruction>(*I.user_begin());
+ if (!isa<SExtInst, ZExtInst>(OuterExt))
+ return false;
+
+ // If the inner extend is a sign extend and the outer one isnt (i.e. a
+ // zero-extend), don't fold. If the first one is zero-extend, it doesn't
+ // matter if the second one is a sign- or zero-extend.
+ if (isa<SExtInst>(InnerExt) && !isa<SExtInst>(OuterExt))
+ return false;
+
+ // Don't try to convert the load if it has an odd size.
+ if (!DL->typeSizeEqualsStoreSize(L->getType()))
+ return false;
+ auto *DstTy = cast<FixedVectorType>(OuterExt->getType());
+ auto *SrcTy =
+ FixedVectorType::get(InnerExt->getOperand(0)->getType()->getScalarType(),
+ DstTy->getNumElements());
+ if (DL->getTypeStoreSize(SrcTy) != DL->getTypeStoreSize(L->getType()))
+ return false;
+
+ // Convert to a vector load feeding a single wide extend.
+ Builder.SetInsertPoint(*L->getInsertionPointAfterDef());
+ auto *NewLoad = cast<LoadInst>(
+ Builder.CreateLoad(SrcTy, L->getOperand(0), L->getName() + ".vec"));
+ auto *NewExt = isa<ZExtInst>(InnerExt) ? Builder.CreateZExt(NewLoad, DstTy)
----------------
arsenm wrote:
Loses nneg flag on the cast?
https://github.com/llvm/llvm-project/pull/141109
More information about the llvm-commits
mailing list