Skip to content

Commit 916bae2

Browse files
committed
[VectorCombine] foldShuffleOfBinops - refactor to make it easier to match icmp/fcmp patterns
NFC refactor to make it easier to also use the fold for icmp/fcmp patterns in a future patch - match the Shuffle with general Instruction operands and avoid explicit use of the BinaryOperator matches as much as possible for the general costing / fold.
1 parent 6b493ba commit 916bae2

File tree

1 file changed

+16
-11
lines changed

1 file changed

+16
-11
lines changed

llvm/lib/Transforms/Vectorize/VectorCombine.cpp

Lines changed: 16 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1628,11 +1628,16 @@ bool VectorCombine::foldPermuteOfBinops(Instruction &I) {
16281628
}
16291629

16301630
/// Try to convert "shuffle (binop), (binop)" into "binop (shuffle), (shuffle)".
1631+
/// TODO: Handle "shuffle (cmp), (cmp)" into "cmp (shuffle), (shuffle)".
16311632
bool VectorCombine::foldShuffleOfBinops(Instruction &I) {
1632-
BinaryOperator *B0, *B1;
16331633
ArrayRef<int> OldMask;
1634-
if (!match(&I, m_Shuffle(m_OneUse(m_BinOp(B0)), m_OneUse(m_BinOp(B1)),
1635-
m_Mask(OldMask))))
1634+
Instruction *LHS, *RHS;
1635+
if (!match(&I, m_Shuffle(m_OneUse(m_Instruction(LHS)),
1636+
m_OneUse(m_Instruction(RHS)), m_Mask(OldMask))))
1637+
return false;
1638+
1639+
BinaryOperator *B0, *B1;
1640+
if (!match(LHS, m_BinOp(B0)) || !match(RHS, m_BinOp(B1)))
16361641
return false;
16371642

16381643
// Don't introduce poison into div/rem.
@@ -1645,15 +1650,15 @@ bool VectorCombine::foldShuffleOfBinops(Instruction &I) {
16451650
return false;
16461651

16471652
auto *ShuffleDstTy = dyn_cast<FixedVectorType>(I.getType());
1648-
auto *BinOpTy = dyn_cast<FixedVectorType>(B0->getType());
1653+
auto *BinOpTy = dyn_cast<FixedVectorType>(LHS->getType());
16491654
if (!ShuffleDstTy || !BinOpTy)
16501655
return false;
16511656

16521657
unsigned NumSrcElts = BinOpTy->getNumElements();
16531658

16541659
// If we have something like "add X, Y" and "add Z, X", swap ops to match.
1655-
Value *X = B0->getOperand(0), *Y = B0->getOperand(1);
1656-
Value *Z = B1->getOperand(0), *W = B1->getOperand(1);
1660+
Value *X = LHS->getOperand(0), *Y = LHS->getOperand(1);
1661+
Value *Z = RHS->getOperand(0), *W = RHS->getOperand(1);
16571662
if (BinaryOperator::isCommutative(Opcode) && X != Z && Y != W &&
16581663
(X == W || Y == Z))
16591664
std::swap(X, Y);
@@ -1681,10 +1686,10 @@ bool VectorCombine::foldShuffleOfBinops(Instruction &I) {
16811686

16821687
// Try to replace a binop with a shuffle if the shuffle is not costly.
16831688
InstructionCost OldCost =
1684-
TTI.getArithmeticInstrCost(B0->getOpcode(), BinOpTy, CostKind) +
1685-
TTI.getArithmeticInstrCost(B1->getOpcode(), BinOpTy, CostKind) +
1689+
TTI.getInstructionCost(LHS, CostKind) +
1690+
TTI.getInstructionCost(RHS, CostKind) +
16861691
TTI.getShuffleCost(TargetTransformInfo::SK_PermuteTwoSrc, BinOpTy,
1687-
OldMask, CostKind, 0, nullptr, {B0, B1}, &I);
1692+
OldMask, CostKind, 0, nullptr, {LHS, RHS}, &I);
16881693

16891694
InstructionCost NewCost =
16901695
TTI.getShuffleCost(SK0, BinOpTy, NewMask0, CostKind, 0, nullptr, {X, Z}) +
@@ -1703,8 +1708,8 @@ bool VectorCombine::foldShuffleOfBinops(Instruction &I) {
17031708

17041709
// Intersect flags from the old binops.
17051710
if (auto *NewInst = dyn_cast<Instruction>(NewBO)) {
1706-
NewInst->copyIRFlags(B0);
1707-
NewInst->andIRFlags(B1);
1711+
NewInst->copyIRFlags(LHS);
1712+
NewInst->andIRFlags(RHS);
17081713
}
17091714

17101715
Worklist.pushValue(Shuf0);

0 commit comments

Comments
 (0)