Skip to content

Commit ec360d6

Browse files
committed
[SLP][NFC]Add getValueType function and use instead of complex scalar type analysis
1 parent 6b4b8dc commit ec360d6

File tree

1 file changed

+21
-26
lines changed

1 file changed

+21
-26
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

+21-26
Original file line numberDiff line numberDiff line change
@@ -231,6 +231,20 @@ static bool isValidElementType(Type *Ty) {
231231
!Ty->isPPC_FP128Ty();
232232
}
233233

234+
/// Returns the type of the given value/instruction \p V. If it is store,
235+
/// returns the type of its value operand, for Cmp - the types of the compare
236+
/// operands and for insertelement - the type os the inserted operand.
237+
/// Otherwise, just the type of the value is returned.
238+
template <typename T> static Type *getValueType(T *V) {
239+
if (auto *SI = dyn_cast<StoreInst>(V))
240+
return SI->getValueOperand()->getType();
241+
if (auto *CI = dyn_cast<CmpInst>(V))
242+
return CI->getOperand(0)->getType();
243+
if (auto *IE = dyn_cast<InsertElementInst>(V))
244+
return IE->getOperand(1)->getType();
245+
return V->getType();
246+
}
247+
234248
/// \returns the number of elements for Ty.
235249
static unsigned getNumElements(Type *Ty) {
236250
assert(!isa<ScalableVectorType>(Ty) &&
@@ -6987,20 +7001,13 @@ void BoUpSLP::buildTree_rec(ArrayRef<Value *> VL, unsigned Depth,
69877001
}
69887002

69897003
// Don't handle vectors.
6990-
if (!SLPReVec && S.OpValue->getType()->isVectorTy() &&
7004+
if (!SLPReVec && getValueType(S.OpValue)->isVectorTy() &&
69917005
!isa<InsertElementInst>(S.OpValue)) {
69927006
LLVM_DEBUG(dbgs() << "SLP: Gathering due to vector type.\n");
69937007
newTreeEntry(VL, std::nullopt /*not vectorized*/, S, UserTreeIdx);
69947008
return;
69957009
}
69967010

6997-
if (StoreInst *SI = dyn_cast<StoreInst>(S.OpValue))
6998-
if (!SLPReVec && SI->getValueOperand()->getType()->isVectorTy()) {
6999-
LLVM_DEBUG(dbgs() << "SLP: Gathering due to store vector type.\n");
7000-
newTreeEntry(VL, std::nullopt /*not vectorized*/, S, UserTreeIdx);
7001-
return;
7002-
}
7003-
70047011
// If all of the operands are identical or constant we have a simple solution.
70057012
// If we deal with insert/extract instructions, they all must have constant
70067013
// indices, otherwise we should gather them, not try to vectorize.
@@ -9433,15 +9440,7 @@ BoUpSLP::getEntryCost(const TreeEntry *E, ArrayRef<Value *> VectorizedVals,
94339440
SmallPtrSetImpl<Value *> &CheckedExtracts) {
94349441
ArrayRef<Value *> VL = E->Scalars;
94359442

9436-
Type *ScalarTy = VL[0]->getType();
9437-
if (!E->isGather()) {
9438-
if (auto *SI = dyn_cast<StoreInst>(VL[0]))
9439-
ScalarTy = SI->getValueOperand()->getType();
9440-
else if (auto *CI = dyn_cast<CmpInst>(VL[0]))
9441-
ScalarTy = CI->getOperand(0)->getType();
9442-
else if (auto *IE = dyn_cast<InsertElementInst>(VL[0]))
9443-
ScalarTy = IE->getOperand(1)->getType();
9444-
}
9443+
Type *ScalarTy = getValueType(VL[0]);
94459444
if (!isValidElementType(ScalarTy))
94469445
return InstructionCost::getInvalid();
94479446
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
@@ -13132,11 +13131,9 @@ Value *BoUpSLP::vectorizeTree(TreeEntry *E, bool PostponedPHIs) {
1313213131
}
1313313132

1313413133
Value *V = E->Scalars.front();
13135-
Type *ScalarTy = V->getType();
13136-
if (auto *Store = dyn_cast<StoreInst>(V))
13137-
ScalarTy = Store->getValueOperand()->getType();
13138-
else if (auto *IE = dyn_cast<InsertElementInst>(V))
13139-
ScalarTy = IE->getOperand(1)->getType();
13134+
Type *ScalarTy = getValueType(V);
13135+
if (isa<CmpInst>(V))
13136+
ScalarTy = V->getType();
1314013137
auto It = MinBWs.find(E);
1314113138
if (It != MinBWs.end()) {
1314213139
auto *VecTy = dyn_cast<FixedVectorType>(ScalarTy);
@@ -16937,9 +16934,7 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
1693716934
bool Changed = false;
1693816935
bool CandidateFound = false;
1693916936
InstructionCost MinCost = SLPCostThreshold.getValue();
16940-
Type *ScalarTy = VL[0]->getType();
16941-
if (auto *IE = dyn_cast<InsertElementInst>(VL[0]))
16942-
ScalarTy = IE->getOperand(1)->getType();
16937+
Type *ScalarTy = getValueType(VL[0]);
1694316938

1694416939
unsigned NextInst = 0, MaxInst = VL.size();
1694516940
for (unsigned VF = MaxVF; NextInst + 1 < MaxInst && VF >= MinVF; VF /= 2) {
@@ -19057,7 +19052,7 @@ bool SLPVectorizerPass::vectorizeCmpInsts(iterator_range<ItT> CmpInsts,
1905719052

1905819053
SmallVector<Value *> Vals;
1905919054
for (Instruction *V : CmpInsts)
19060-
if (!R.isDeleted(V) && isValidElementType(V->getType()))
19055+
if (!R.isDeleted(V) && isValidElementType(getValueType(V)))
1906119056
Vals.push_back(V);
1906219057
if (Vals.size() <= 1)
1906319058
return Changed;

0 commit comments

Comments
 (0)