Skip to content

Commit 75ec467

Browse files
committed
[SLP] NFC. Use InstructionsState::getOpcode only when necessary.
Use isa, isa_and_present and dyn_cast_if_present instead of getOpcode.
1 parent 5624823 commit 75ec467

File tree

1 file changed

+44
-45
lines changed

1 file changed

+44
-45
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 44 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -821,7 +821,8 @@ class InstructionsState {
821821

822822
/// The main/alternate opcodes for the list of instructions.
823823
unsigned getOpcode() const {
824-
return MainOp ? MainOp->getOpcode() : 0;
824+
assert(MainOp && "InstructionsState is invalid.");
825+
return MainOp->getOpcode();
825826
}
826827

827828
unsigned getAltOpcode() const {
@@ -1845,7 +1846,7 @@ class BoUpSLP {
18451846
InstructionsState S = getSameOpcode(Ops, TLI);
18461847
// Note: Only consider instructions with <= 2 operands to avoid
18471848
// complexity explosion.
1848-
if (S.getOpcode() &&
1849+
if (S.getMainOp() &&
18491850
(S.getMainOp()->getNumOperands() <= 2 || !MainAltOps.empty() ||
18501851
!S.isAltShuffle()) &&
18511852
all_of(Ops, [&S](Value *V) {
@@ -2696,7 +2697,7 @@ class BoUpSLP {
26962697
OperandData &AltOp = getData(OpIdx, Lane);
26972698
InstructionsState OpS =
26982699
getSameOpcode({MainAltOps[OpIdx].front(), AltOp.V}, TLI);
2699-
if (OpS.getOpcode() && OpS.isAltShuffle())
2700+
if (OpS.getMainOp() && OpS.isAltShuffle())
27002701
MainAltOps[OpIdx].push_back(AltOp.V);
27012702
}
27022703
}
@@ -3592,8 +3593,8 @@ class BoUpSLP {
35923593
// Gathered loads still gathered? Do not create entry, use the original one.
35933594
if (GatheredLoadsEntriesFirst.has_value() &&
35943595
EntryState == TreeEntry::NeedToGather &&
3595-
S.getOpcode() == Instruction::Load && UserTreeIdx.EdgeIdx == UINT_MAX &&
3596-
!UserTreeIdx.UserTE)
3596+
isa_and_present<LoadInst>(S.getMainOp()) &&
3597+
UserTreeIdx.EdgeIdx == UINT_MAX && !UserTreeIdx.UserTE)
35973598
return nullptr;
35983599
VectorizableTree.push_back(std::make_unique<TreeEntry>(VectorizableTree));
35993600
TreeEntry *Last = VectorizableTree.back().get();
@@ -8067,7 +8068,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
80678068
}
80688069

80698070
// Check if this is a duplicate of another entry.
8070-
if (S.getOpcode()) {
8071+
if (S.getMainOp()) {
80718072
if (TreeEntry *E = getTreeEntry(S.getMainOp())) {
80728073
LLVM_DEBUG(dbgs() << "SLP: \tChecking bundle: " << *S.getMainOp()
80738074
<< ".\n");
@@ -8133,8 +8134,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
81338134
all_of(VL, [&S](const Value *I) {
81348135
return match(I,
81358136
m_OneUse(m_ZExtOrSExt(m_OneUse(m_Load(m_Value()))))) &&
8136-
cast<Instruction>(I)->getOpcode() ==
8137-
S.getMainOp()->getOpcode();
8137+
cast<Instruction>(I)->getOpcode() == S.getOpcode();
81388138
})))) {
81398139
LLVM_DEBUG(dbgs() << "SLP: Gathering due to max recursion depth.\n");
81408140
if (TryToFindDuplicates(S))
@@ -8144,15 +8144,14 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
81448144
}
81458145

81468146
// Don't handle scalable vectors
8147-
if (S.getOpcode() == Instruction::ExtractElement &&
8148-
isa<ScalableVectorType>(
8149-
cast<ExtractElementInst>(S.getMainOp())->getVectorOperandType())) {
8150-
LLVM_DEBUG(dbgs() << "SLP: Gathering due to scalable vector type.\n");
8151-
if (TryToFindDuplicates(S))
8152-
newTreeEntry(VL, std::nullopt /*not vectorized*/, S, UserTreeIdx,
8153-
ReuseShuffleIndices);
8154-
return;
8155-
}
8147+
if (auto *EE = dyn_cast_if_present<ExtractElementInst>(S.getMainOp()))
8148+
if (isa<ScalableVectorType>(EE->getVectorOperandType())) {
8149+
LLVM_DEBUG(dbgs() << "SLP: Gathering due to scalable vector type.\n");
8150+
if (TryToFindDuplicates(S))
8151+
newTreeEntry(VL, std::nullopt /*not vectorized*/, S, UserTreeIdx,
8152+
ReuseShuffleIndices);
8153+
return;
8154+
}
81568155

81578156
// Don't handle vectors.
81588157
if (!SLPReVec && getValueType(VL.front())->isVectorTy()) {
@@ -8168,7 +8167,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
81688167
// vectorize.
81698168
auto &&NotProfitableForVectorization = [&S, this,
81708169
Depth](ArrayRef<Value *> VL) {
8171-
if (!S.getOpcode() || !S.isAltShuffle() || VL.size() > 2)
8170+
if (!S.getMainOp() || !S.isAltShuffle() || VL.size() > 2)
81728171
return false;
81738172
if (VectorizableTree.size() < MinTreeSize)
81748173
return false;
@@ -8223,7 +8222,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
82238222
bool IsScatterVectorizeUserTE =
82248223
UserTreeIdx.UserTE &&
82258224
UserTreeIdx.UserTE->State == TreeEntry::ScatterVectorize;
8226-
bool AreAllSameBlock = S.getOpcode() && allSameBlock(VL);
8225+
bool AreAllSameBlock = S.getMainOp() && allSameBlock(VL);
82278226
bool AreScatterAllGEPSameBlock =
82288227
(IsScatterVectorizeUserTE && VL.front()->getType()->isPointerTy() &&
82298228
VL.size() > 2 &&
@@ -8240,7 +8239,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
82408239
sortPtrAccesses(VL, UserTreeIdx.UserTE->getMainOp()->getType(), *DL, *SE,
82418240
SortedIndices));
82428241
bool AreAllSameInsts = AreAllSameBlock || AreScatterAllGEPSameBlock;
8243-
if (!AreAllSameInsts || (!S.getOpcode() && allConstant(VL)) || isSplat(VL) ||
8242+
if (!AreAllSameInsts || (!S.getMainOp() && allConstant(VL)) || isSplat(VL) ||
82448243
(isa_and_present<InsertElementInst, ExtractValueInst, ExtractElementInst>(
82458244
S.getMainOp()) &&
82468245
!all_of(VL, isVectorLikeInstWithConstOps)) ||
@@ -8253,7 +8252,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
82538252
}
82548253

82558254
// Don't vectorize ephemeral values.
8256-
if (S.getOpcode() && !EphValues.empty()) {
8255+
if (S.getMainOp() && !EphValues.empty()) {
82578256
for (Value *V : VL) {
82588257
if (EphValues.count(V)) {
82598258
LLVM_DEBUG(dbgs() << "SLP: The instruction (" << *V
@@ -8359,15 +8358,15 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
83598358
newTreeEntry(VL, std::nullopt /*not vectorized*/, S, UserTreeIdx,
83608359
ReuseShuffleIndices);
83618360
NonScheduledFirst.insert(VL.front());
8362-
if (S.getOpcode() == Instruction::Load &&
8361+
if (isa<LoadInst>(S.getMainOp()) &&
83638362
BS.ScheduleRegionSize < BS.ScheduleRegionSizeLimit)
83648363
registerNonVectorizableLoads(VL);
83658364
return;
83668365
}
83678366
LLVM_DEBUG(dbgs() << "SLP: We are able to schedule this bundle.\n");
83688367

8369-
unsigned ShuffleOrOp = S.isAltShuffle() ?
8370-
(unsigned) Instruction::ShuffleVector : S.getOpcode();
8368+
unsigned ShuffleOrOp =
8369+
S.isAltShuffle() ? (unsigned)Instruction::ShuffleVector : S.getOpcode();
83718370
auto CreateOperandNodes = [&](TreeEntry *TE, const auto &Operands) {
83728371
// Postpone PHI nodes creation
83738372
SmallVector<unsigned> PHIOps;
@@ -8376,7 +8375,7 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
83768375
if (Op.empty())
83778376
continue;
83788377
InstructionsState S = getSameOpcode(Op, *TLI);
8379-
if (S.getOpcode() != Instruction::PHI || S.isAltShuffle())
8378+
if (!isa_and_present<PHINode>(S.getMainOp()) || S.isAltShuffle())
83808379
buildTree_rec(Op, Depth + 1, {TE, I});
83818380
else
83828381
PHIOps.push_back(I);
@@ -9730,10 +9729,10 @@ void BoUpSLP::transformNodes() {
97309729
if (IsSplat)
97319730
continue;
97329731
InstructionsState S = getSameOpcode(Slice, *TLI);
9733-
if (!S.getOpcode() || S.isAltShuffle() || !allSameBlock(Slice) ||
9734-
(S.getOpcode() == Instruction::Load &&
9732+
if (!S.getMainOp() || S.isAltShuffle() || !allSameBlock(Slice) ||
9733+
(isa<LoadInst>(S.getMainOp()) &&
97359734
areKnownNonVectorizableLoads(Slice)) ||
9736-
(S.getOpcode() != Instruction::Load && !has_single_bit(VF)))
9735+
(!isa<LoadInst>(S.getMainOp()) && !has_single_bit(VF)))
97379736
continue;
97389737
if (VF == 2) {
97399738
// Try to vectorize reduced values or if all users are vectorized.
@@ -9748,7 +9747,7 @@ void BoUpSLP::transformNodes() {
97489747
UserIgnoreList);
97499748
}))
97509749
continue;
9751-
if (S.getOpcode() == Instruction::Load) {
9750+
if (isa<LoadInst>(S.getMainOp())) {
97529751
OrdersType Order;
97539752
SmallVector<Value *> PointerOps;
97549753
LoadsState Res =
@@ -9765,7 +9764,7 @@ void BoUpSLP::transformNodes() {
97659764
}
97669765
continue;
97679766
}
9768-
} else if (S.getOpcode() == Instruction::ExtractElement ||
9767+
} else if (isa<ExtractElementInst>(S.getMainOp()) ||
97699768
(TTI->getInstructionCost(S.getMainOp(), CostKind) <
97709769
TTI::TCC_Expensive &&
97719770
!CheckOperandsProfitability(
@@ -11047,7 +11046,8 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
1104711046
if (const TreeEntry *OpTE = getTreeEntry(V))
1104811047
return getCastContextHint(*OpTE);
1104911048
InstructionsState SrcState = getSameOpcode(E->getOperand(0), *TLI);
11050-
if (SrcState.getOpcode() == Instruction::Load && !SrcState.isAltShuffle())
11049+
if (isa_and_present<LoadInst>(SrcState.getMainOp()) &&
11050+
!SrcState.isAltShuffle())
1105111051
return TTI::CastContextHint::GatherScatter;
1105211052
return TTI::CastContextHint::None;
1105311053
};
@@ -14394,12 +14394,12 @@ BoUpSLP::TreeEntry *BoUpSLP::getMatchedVectorizedOperand(const TreeEntry *E,
1439414394
ArrayRef<Value *> VL = E->getOperand(NodeIdx);
1439514395
InstructionsState S = getSameOpcode(VL, *TLI);
1439614396
// Special processing for GEPs bundle, which may include non-gep values.
14397-
if (!S.getOpcode() && VL.front()->getType()->isPointerTy()) {
14397+
if (!S.getMainOp() && VL.front()->getType()->isPointerTy()) {
1439814398
const auto *It = find_if(VL, IsaPred<GetElementPtrInst>);
1439914399
if (It != VL.end())
1440014400
S = getSameOpcode(*It, *TLI);
1440114401
}
14402-
if (!S.getOpcode())
14402+
if (!S.getMainOp())
1440314403
return nullptr;
1440414404
auto CheckSameVE = [&](const TreeEntry *VE) {
1440514405
return VE->isSame(VL) &&
@@ -18376,8 +18376,7 @@ SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
1837618376
hasFullVectorsOrPowerOf2(*TTI, ValOps.front()->getType(),
1837718377
ValOps.size()) ||
1837818378
(VectorizeNonPowerOf2 && has_single_bit(ValOps.size() + 1));
18379-
if ((!IsAllowedSize && S.getOpcode() &&
18380-
S.getOpcode() != Instruction::Load &&
18379+
if ((!IsAllowedSize && S.getMainOp() && !isa<LoadInst>(S.getMainOp()) &&
1838118380
(!S.getMainOp()->isSafeToRemove() ||
1838218381
any_of(ValOps.getArrayRef(),
1838318382
[&](Value *V) {
@@ -18387,8 +18386,8 @@ SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
1838718386
return !Stores.contains(U);
1838818387
}));
1838918388
}))) ||
18390-
(ValOps.size() > Chain.size() / 2 && !S.getOpcode())) {
18391-
Size = (!IsAllowedSize && S.getOpcode()) ? 1 : 2;
18389+
(ValOps.size() > Chain.size() / 2 && !S.getMainOp())) {
18390+
Size = (!IsAllowedSize && S.getMainOp()) ? 1 : 2;
1839218391
return false;
1839318392
}
1839418393
}
@@ -18411,7 +18410,7 @@ SLPVectorizerPass::vectorizeStoreChain(ArrayRef<Value *> Chain, BoUpSLP &R,
1841118410
R.computeMinimumValueSizes();
1841218411

1841318412
Size = R.getCanonicalGraphSize();
18414-
if (S.getOpcode() == Instruction::Load)
18413+
if (isa_and_present<LoadInst>(S.getMainOp()))
1841518414
Size = 2; // cut off masked gather small trees
1841618415
InstructionCost Cost = R.getTreeCost();
1841718416

@@ -18912,7 +18911,7 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
1891218911
// Check that all of the parts are instructions of the same type,
1891318912
// we permit an alternate opcode via InstructionsState.
1891418913
InstructionsState S = getSameOpcode(VL, *TLI);
18915-
if (!S.getOpcode())
18914+
if (!S.getMainOp())
1891618915
return false;
1891718916

1891818917
Instruction *I0 = S.getMainOp();
@@ -19724,16 +19723,16 @@ class HorizontalReduction {
1972419723
// Also check if the instruction was folded to constant/other value.
1972519724
auto *Inst = dyn_cast<Instruction>(RdxVal);
1972619725
if ((Inst && isVectorLikeInstWithConstOps(Inst) &&
19727-
(!S.getOpcode() || !S.isOpcodeOrAlt(Inst))) ||
19728-
(S.getOpcode() && !Inst))
19726+
(!S.getMainOp() || !S.isOpcodeOrAlt(Inst))) ||
19727+
(S.getMainOp() && !Inst))
1972919728
continue;
1973019729
Candidates.push_back(RdxVal);
1973119730
TrackedToOrig.try_emplace(RdxVal, OrigReducedVals[Cnt]);
1973219731
}
1973319732
bool ShuffledExtracts = false;
1973419733
// Try to handle shuffled extractelements.
19735-
if (S.getOpcode() == Instruction::ExtractElement && !S.isAltShuffle() &&
19736-
I + 1 < E) {
19734+
if (isa_and_present<ExtractElementInst>(S.getMainOp()) &&
19735+
!S.isAltShuffle() && I + 1 < E) {
1973719736
SmallVector<Value *> CommonCandidates(Candidates);
1973819737
for (Value *RV : ReducedVals[I + 1]) {
1973919738
Value *RdxVal = TrackedVals.at(RV);
@@ -21128,7 +21127,7 @@ static bool compareCmp(Value *V, Value *V2, TargetLibraryInfo &TLI,
2112821127
return NodeI1->getDFSNumIn() < NodeI2->getDFSNumIn();
2112921128
}
2113021129
InstructionsState S = getSameOpcode({I1, I2}, TLI);
21131-
if (S.getOpcode() && (IsCompatibility || !S.isAltShuffle()))
21130+
if (S.getMainOp() && (IsCompatibility || !S.isAltShuffle()))
2113221131
continue;
2113321132
if (IsCompatibility)
2113421133
return false;
@@ -21283,7 +21282,7 @@ bool SLPVectorizerPass::vectorizeChainsInBlock(BasicBlock *BB, BoUpSLP &R) {
2128321282
if (NodeI1 != NodeI2)
2128421283
return NodeI1->getDFSNumIn() < NodeI2->getDFSNumIn();
2128521284
InstructionsState S = getSameOpcode({I1, I2}, *TLI);
21286-
if (S.getOpcode() && !S.isAltShuffle())
21285+
if (S.getMainOp() && !S.isAltShuffle())
2128721286
continue;
2128821287
return I1->getOpcode() < I2->getOpcode();
2128921288
}

0 commit comments

Comments
 (0)