From ff53a4a23467dde54233e1b7fe51e8049dfb6dda Mon Sep 17 00:00:00 2001 From: Simon Pilgrim Date: Fri, 16 Feb 2018 16:22:14 +0000 Subject: [SelectionDAG] Enable SimplifyDemandedVectorElts support for simplifying shuffle masks Based off the DemandedElts mask the and UNDEF elements returned from the SimplifyDemandedVectorElts calls to the shuffle operands, we can attempt to simplify the shuffle mask. I had to be very conservative here as accepting post-legalized shuffle masks could cause problems for targets that legalize UNDEF mask elements back to inrange values (PowerPC), similarly combining to identity shuffle masks could cause too much UNDEF information to disappear for later combines. llvm-svn: 325354 --- llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp | 25 ++++++++++++++++++++++++ 1 file changed, 25 insertions(+) (limited to 'llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp') diff --git a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp index 8eef9b58c57..d82f90e7311 100644 --- a/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp +++ b/llvm/lib/CodeGen/SelectionDAG/TargetLowering.cpp @@ -1458,6 +1458,31 @@ bool TargetLowering::SimplifyDemandedVectorElts( ZeroRHS, TLO, Depth + 1)) return true; + // Simplify mask using undef elements from LHS/RHS. + bool Updated = false; + bool IdentityLHS = true, IdentityRHS = true; + SmallVector NewMask(ShuffleMask.begin(), ShuffleMask.end()); + for (int i = 0; i != NumElts; ++i) { + int &M = NewMask[i]; + if (M < 0) + continue; + if (!DemandedElts[i] || (M < (int)NumElts && UndefLHS[M]) || + (M >= (int)NumElts && UndefRHS[M - NumElts])) { + Updated = true; + M = -1; + } + IdentityLHS &= (M < 0) || (M == i); + IdentityRHS &= (M < 0) || ((M - NumElts) == i); + } + + // Update legal shuffle masks based on demanded elements if it won't reduce + // to Identity which can cause premature removal of the shuffle mask. + if (Updated && !IdentityLHS && !IdentityRHS && !TLO.LegalOps && + isShuffleMaskLegal(NewMask, VT)) + return TLO.CombineTo(Op, + TLO.DAG.getVectorShuffle(VT, DL, Op.getOperand(0), + Op.getOperand(1), NewMask)); + // Propagate undef/zero elements from LHS/RHS. for (unsigned i = 0; i != NumElts; ++i) { int M = ShuffleMask[i]; -- cgit v1.2.3