Skip to content

Commit 9672f6d

Browse files
committed
reduce repeated code
1 parent 83ed351 commit 9672f6d

File tree

1 file changed

+30
-31
lines changed

1 file changed

+30
-31
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 30 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -929,6 +929,29 @@ getInterchangeableInstruction(Instruction *I) {
929929
return PII;
930930
}
931931

932+
/// \returns the Op and operands which \p I convert to.
933+
static std::pair<Value *, SmallVector<Value *>>
934+
getInterchangeableInstruction(Instruction *I, Instruction *MainOp,
935+
Instruction *AltOp) {
936+
SmallVector<InterchangeableInstruction> IIList =
937+
getInterchangeableInstruction(I);
938+
auto Iter = find_if(IIList, [&](const InterchangeableInstruction &II) {
939+
return II.Opcode == MainOp->getOpcode();
940+
});
941+
Value *SelectedOp;
942+
if (Iter == IIList.end()) {
943+
Iter = find_if(IIList, [&](const InterchangeableInstruction &II) {
944+
return II.Opcode == AltOp->getOpcode();
945+
});
946+
assert(Iter != IIList.end() &&
947+
"Cannot find an interchangeable instruction.");
948+
SelectedOp = AltOp;
949+
} else {
950+
SelectedOp = MainOp;
951+
}
952+
return std::make_pair(SelectedOp, Iter->Ops);
953+
}
954+
932955
/// \returns true if \p Opcode is allowed as part of the main/alternate
933956
/// instruction for SLP vectorization.
934957
///
@@ -2484,22 +2507,8 @@ class BoUpSLP {
24842507
OpsVec[OpIdx].resize(NumLanes);
24852508
for (auto [I, V] : enumerate(VL)) {
24862509
assert(isa<Instruction>(V) && "Expected instruction");
2487-
SmallVector<InterchangeableInstruction> IIList =
2488-
getInterchangeableInstruction(cast<Instruction>(V));
2489-
Value *SelectedOp;
2490-
auto Iter = find_if(IIList, [&](const InterchangeableInstruction &II) {
2491-
return II.Opcode == S.MainOp->getOpcode();
2492-
});
2493-
if (Iter == IIList.end()) {
2494-
Iter = find_if(IIList, [&](const InterchangeableInstruction &II) {
2495-
return II.Opcode == S.AltOp->getOpcode();
2496-
});
2497-
SelectedOp = S.AltOp;
2498-
} else {
2499-
SelectedOp = S.MainOp;
2500-
}
2501-
assert(Iter != IIList.end() &&
2502-
"Cannot find an interchangeable instruction.");
2510+
auto [SelectedOp, Ops] = getInterchangeableInstruction(
2511+
cast<Instruction>(V), S.MainOp, S.AltOp);
25032512
// Our tree has just 3 nodes: the root and two operands.
25042513
// It is therefore trivial to get the APO. We only need to check the
25052514
// opcode of V and whether the operand at OpIdx is the LHS or RHS
@@ -2513,7 +2522,7 @@ class BoUpSLP {
25132522
bool IsInverseOperation = !isCommutative(cast<Instruction>(SelectedOp));
25142523
for (unsigned OpIdx : seq<unsigned>(NumOperands)) {
25152524
bool APO = (OpIdx == 0) ? false : IsInverseOperation;
2516-
OpsVec[OpIdx][I] = {Iter->Ops[OpIdx], APO, false};
2525+
OpsVec[OpIdx][I] = {Ops[OpIdx], APO, false};
25172526
}
25182527
}
25192528
}
@@ -3417,20 +3426,10 @@ class BoUpSLP {
34173426
for (unsigned OpIdx : seq<unsigned>(NumOperands))
34183427
Operands[OpIdx].resize(NumLanes);
34193428
for (auto [I, V] : enumerate(Scalars)) {
3420-
SmallVector<InterchangeableInstruction> IIList =
3421-
getInterchangeableInstruction(cast<Instruction>(V));
3422-
auto Iter = find_if(IIList, [&](const InterchangeableInstruction &II) {
3423-
return II.Opcode == MainOp->getOpcode();
3424-
});
3425-
if (Iter == IIList.end())
3426-
Iter = find_if(IIList, [&](const InterchangeableInstruction &II) {
3427-
return II.Opcode == AltOp->getOpcode();
3428-
});
3429-
assert(Iter != IIList.end() &&
3430-
"Cannot find an interchangeable instruction.");
3431-
assert(Iter->Ops.size() == NumOperands &&
3432-
"Expected same number of operands");
3433-
for (auto [J, Op] : enumerate(Iter->Ops))
3429+
auto [SelectedOp, Ops] =
3430+
getInterchangeableInstruction(cast<Instruction>(V), MainOp, AltOp);
3431+
assert(Ops.size() == NumOperands && "Expected same number of operands");
3432+
for (auto [J, Op] : enumerate(Ops))
34343433
Operands[J][I] = Op;
34353434
}
34363435
}

0 commit comments

Comments
 (0)