Skip to content

Commit b7b1833

Browse files
committed
[SLP][NFC]Improve perf of BoUpSLP::buildExternalUses function, NFC.
Perform required operations, only when the result is required + check if value was not inserted already into the list of the externally used scalars and early exit, if it was.
1 parent c0b77e0 commit b7b1833

File tree

1 file changed

+17
-3
lines changed

1 file changed

+17
-3
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5514,6 +5514,7 @@ void BoUpSLP::reorderBottomToTop(bool IgnoreReorder) {
55145514

55155515
void BoUpSLP::buildExternalUses(
55165516
const ExtraValueToDebugLocsMap &ExternallyUsedValues) {
5517+
DenseMap<Value *, unsigned> ScalarToExtUses;
55175518
// Collect the values that we need to extract from the tree.
55185519
for (auto &TEPtr : VectorizableTree) {
55195520
TreeEntry *Entry = TEPtr.get();
@@ -5527,13 +5528,18 @@ void BoUpSLP::buildExternalUses(
55275528
Value *Scalar = Entry->Scalars[Lane];
55285529
if (!isa<Instruction>(Scalar))
55295530
continue;
5530-
int FoundLane = Entry->findLaneForValue(Scalar);
5531+
// All uses must be replaced already? No need to do it again.
5532+
auto It = ScalarToExtUses.find(Scalar);
5533+
if (It != ScalarToExtUses.end() && !ExternalUses[It->second].User)
5534+
continue;
55315535

55325536
// Check if the scalar is externally used as an extra arg.
55335537
const auto *ExtI = ExternallyUsedValues.find(Scalar);
55345538
if (ExtI != ExternallyUsedValues.end()) {
5539+
int FoundLane = Entry->findLaneForValue(Scalar);
55355540
LLVM_DEBUG(dbgs() << "SLP: Need to extract: Extra arg from lane "
5536-
<< Lane << " from " << *Scalar << ".\n");
5541+
<< FoundLane << " from " << *Scalar << ".\n");
5542+
ScalarToExtUses.try_emplace(Scalar, ExternalUses.size());
55375543
ExternalUses.emplace_back(Scalar, nullptr, FoundLane);
55385544
}
55395545
for (User *U : Scalar->users()) {
@@ -5561,12 +5567,20 @@ void BoUpSLP::buildExternalUses(
55615567
continue;
55625568
}
55635569
U = nullptr;
5570+
if (It != ScalarToExtUses.end()) {
5571+
ExternalUses[It->second].User = nullptr;
5572+
break;
5573+
}
55645574
}
55655575

5576+
int FoundLane = Entry->findLaneForValue(Scalar);
55665577
LLVM_DEBUG(dbgs() << "SLP: Need to extract:" << *UserInst
5567-
<< " from lane " << Lane << " from " << *Scalar
5578+
<< " from lane " << FoundLane << " from " << *Scalar
55685579
<< ".\n");
5580+
It = ScalarToExtUses.try_emplace(Scalar, ExternalUses.size()).first;
55695581
ExternalUses.emplace_back(Scalar, U, FoundLane);
5582+
if (!U)
5583+
break;
55705584
}
55715585
}
55725586
}

0 commit comments

Comments
 (0)