Skip to content

Commit 060d151

Browse files
committed
[SLP][NFCI]Check early for deleted instructions
Check as early as possible for the deleted instructions before trying to vectorize the code. May reduce number of attempts for the vectorization.
1 parent a24e8a7 commit 060d151

File tree

1 file changed

+31
-19
lines changed

1 file changed

+31
-19
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 31 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18212,13 +18212,22 @@ bool SLPVectorizerPass::tryToVectorizeList(ArrayRef<Value *> VL, BoUpSLP &R,
1821218212
if ((VF > MinVF && ActualVF <= VF / 2) || (VF == MinVF && ActualVF < 2))
1821318213
break;
1821418214

18215-
ArrayRef<Value *> Ops = VL.slice(I, ActualVF);
18216-
// Check that a previous iteration of this loop did not delete the Value.
18217-
if (llvm::any_of(Ops, [&R](Value *V) {
18218-
auto *I = dyn_cast<Instruction>(V);
18219-
return I && R.isDeleted(I);
18220-
}))
18221-
continue;
18215+
SmallVector<Value *> Ops(ActualVF, nullptr);
18216+
unsigned Idx = 0;
18217+
for (Value *V : VL.drop_front(I)) {
18218+
// Check that a previous iteration of this loop did not delete the
18219+
// Value.
18220+
if (auto *Inst = dyn_cast<Instruction>(V);
18221+
!Inst || !R.isDeleted(Inst)) {
18222+
Ops[Idx] = V;
18223+
++Idx;
18224+
if (Idx == ActualVF)
18225+
break;
18226+
}
18227+
}
18228+
// Not enough vectorizable instructions - exit.
18229+
if (Idx != ActualVF)
18230+
break;
1822218231

1822318232
LLVM_DEBUG(dbgs() << "SLP: Analyzing " << ActualVF << " operations "
1822418233
<< "\n");
@@ -18286,7 +18295,8 @@ bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) {
1828618295
// Vectorize in current basic block only.
1828718296
auto *Op0 = dyn_cast<Instruction>(I->getOperand(0));
1828818297
auto *Op1 = dyn_cast<Instruction>(I->getOperand(1));
18289-
if (!Op0 || !Op1 || Op0->getParent() != P || Op1->getParent() != P)
18298+
if (!Op0 || !Op1 || Op0->getParent() != P || Op1->getParent() != P ||
18299+
R.isDeleted(Op0) || R.isDeleted(Op1))
1829018300
return false;
1829118301

1829218302
// First collect all possible candidates
@@ -18299,18 +18309,18 @@ bool SLPVectorizerPass::tryToVectorize(Instruction *I, BoUpSLP &R) {
1829918309
if (A && B && B->hasOneUse()) {
1830018310
auto *B0 = dyn_cast<BinaryOperator>(B->getOperand(0));
1830118311
auto *B1 = dyn_cast<BinaryOperator>(B->getOperand(1));
18302-
if (B0 && B0->getParent() == P)
18312+
if (B0 && B0->getParent() == P && !R.isDeleted(B0))
1830318313
Candidates.emplace_back(A, B0);
18304-
if (B1 && B1->getParent() == P)
18314+
if (B1 && B1->getParent() == P && !R.isDeleted(B1))
1830518315
Candidates.emplace_back(A, B1);
1830618316
}
1830718317
// Try to skip A.
1830818318
if (B && A && A->hasOneUse()) {
1830918319
auto *A0 = dyn_cast<BinaryOperator>(A->getOperand(0));
1831018320
auto *A1 = dyn_cast<BinaryOperator>(A->getOperand(1));
18311-
if (A0 && A0->getParent() == P)
18321+
if (A0 && A0->getParent() == P && !R.isDeleted(A0))
1831218322
Candidates.emplace_back(A0, B);
18313-
if (A1 && A1->getParent() == P)
18323+
if (A1 && A1->getParent() == P && !R.isDeleted(A1))
1831418324
Candidates.emplace_back(A1, B);
1831518325
}
1831618326

@@ -19769,16 +19779,16 @@ static void findBuildAggregate_rec(Instruction *LastInsertInst,
1976919779
TargetTransformInfo *TTI,
1977019780
SmallVectorImpl<Value *> &BuildVectorOpds,
1977119781
SmallVectorImpl<Value *> &InsertElts,
19772-
unsigned OperandOffset) {
19782+
unsigned OperandOffset, const BoUpSLP &R) {
1977319783
do {
1977419784
Value *InsertedOperand = LastInsertInst->getOperand(1);
1977519785
std::optional<unsigned> OperandIndex =
1977619786
getElementIndex(LastInsertInst, OperandOffset);
19777-
if (!OperandIndex)
19787+
if (!OperandIndex || R.isDeleted(LastInsertInst))
1977819788
return;
1977919789
if (isa<InsertElementInst, InsertValueInst>(InsertedOperand)) {
1978019790
findBuildAggregate_rec(cast<Instruction>(InsertedOperand), TTI,
19781-
BuildVectorOpds, InsertElts, *OperandIndex);
19791+
BuildVectorOpds, InsertElts, *OperandIndex, R);
1978219792

1978319793
} else {
1978419794
BuildVectorOpds[*OperandIndex] = InsertedOperand;
@@ -19807,7 +19817,8 @@ static void findBuildAggregate_rec(Instruction *LastInsertInst,
1980719817
static bool findBuildAggregate(Instruction *LastInsertInst,
1980819818
TargetTransformInfo *TTI,
1980919819
SmallVectorImpl<Value *> &BuildVectorOpds,
19810-
SmallVectorImpl<Value *> &InsertElts) {
19820+
SmallVectorImpl<Value *> &InsertElts,
19821+
const BoUpSLP &R) {
1981119822

1981219823
assert((isa<InsertElementInst>(LastInsertInst) ||
1981319824
isa<InsertValueInst>(LastInsertInst)) &&
@@ -19822,7 +19833,8 @@ static bool findBuildAggregate(Instruction *LastInsertInst,
1982219833
BuildVectorOpds.resize(*AggregateSize);
1982319834
InsertElts.resize(*AggregateSize);
1982419835

19825-
findBuildAggregate_rec(LastInsertInst, TTI, BuildVectorOpds, InsertElts, 0);
19836+
findBuildAggregate_rec(LastInsertInst, TTI, BuildVectorOpds, InsertElts, 0,
19837+
R);
1982619838
llvm::erase(BuildVectorOpds, nullptr);
1982719839
llvm::erase(InsertElts, nullptr);
1982819840
if (BuildVectorOpds.size() >= 2)
@@ -20068,7 +20080,7 @@ bool SLPVectorizerPass::vectorizeInsertValueInst(InsertValueInst *IVI,
2006820080

2006920081
SmallVector<Value *, 16> BuildVectorOpds;
2007020082
SmallVector<Value *, 16> BuildVectorInsts;
20071-
if (!findBuildAggregate(IVI, TTI, BuildVectorOpds, BuildVectorInsts))
20083+
if (!findBuildAggregate(IVI, TTI, BuildVectorOpds, BuildVectorInsts, R))
2007220084
return false;
2007320085

2007420086
if (MaxVFOnly && BuildVectorOpds.size() == 2) {
@@ -20090,7 +20102,7 @@ bool SLPVectorizerPass::vectorizeInsertElementInst(InsertElementInst *IEI,
2009020102
SmallVector<Value *, 16> BuildVectorInsts;
2009120103
SmallVector<Value *, 16> BuildVectorOpds;
2009220104
SmallVector<int> Mask;
20093-
if (!findBuildAggregate(IEI, TTI, BuildVectorOpds, BuildVectorInsts) ||
20105+
if (!findBuildAggregate(IEI, TTI, BuildVectorOpds, BuildVectorInsts, R) ||
2009420106
(llvm::all_of(BuildVectorOpds, IsaPred<ExtractElementInst, UndefValue>) &&
2009520107
isFixedVectorShuffle(BuildVectorOpds, Mask)))
2009620108
return false;

0 commit comments

Comments
 (0)