[llvm] InstSimplify: lookthru casts, binops in folding shuffles (PR #92668)
Matt Arsenault via llvm-commits
llvm-commits at lists.llvm.org
Sat May 18 12:29:15 PDT 2024
================
@@ -5315,55 +5315,104 @@ Value *llvm::simplifyCastInst(unsigned CastOpc, Value *Op, Type *Ty,
return ::simplifyCastInst(CastOpc, Op, Ty, Q, RecursionLimit);
}
+using ReplacementTy = std::optional<std::pair<Value *, Value *>>;
+
/// For the given destination element of a shuffle, peek through shuffles to
/// match a root vector source operand that contains that element in the same
/// vector lane (ie, the same mask index), so we can eliminate the shuffle(s).
-static Value *foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1,
- int MaskVal, Value *RootVec,
- unsigned MaxRecurse) {
+static std::pair<Value *, ReplacementTy>
+foldIdentityShuffles(int DestElt, Value *Op0, Value *Op1, int MaskVal,
+ Value *RootVec, unsigned MaxRecurse) {
if (!MaxRecurse--)
- return nullptr;
+ return {nullptr, std::nullopt};
// Bail out if any mask value is undefined. That kind of shuffle may be
// simplified further based on demanded bits or other folds.
if (MaskVal == -1)
- return nullptr;
+ return {nullptr, std::nullopt};
// The mask value chooses which source operand we need to look at next.
- int InVecNumElts = cast<FixedVectorType>(Op0->getType())->getNumElements();
+ unsigned InVecNumElts =
+ cast<FixedVectorType>(Op0->getType())->getNumElements();
int RootElt = MaskVal;
Value *SourceOp = Op0;
- if (MaskVal >= InVecNumElts) {
+ if (MaskVal > -1 && static_cast<unsigned>(MaskVal) >= InVecNumElts) {
RootElt = MaskVal - InVecNumElts;
SourceOp = Op1;
}
+ // The next RootVec preseves an existing RootVec for casts and binops, so that
+ // the final RootVec is the last cast or binop in the chain.
+ Value *NextRootVec = RootVec ? RootVec : SourceOp;
+
+ // Look through a cast instruction that preserves number of elements in the
+ // vector. Set the RootVec to the cast, the SourceOp to the operand and
+ // recurse. If, in a later stack frame, an appropriate ShuffleVector is
+ // matched, the example will reduce.
+ if (auto *SourceCast = dyn_cast<CastInst>(SourceOp))
----------------
arsenm wrote:
Braces
https://github.com/llvm/llvm-project/pull/92668
More information about the llvm-commits
mailing list