Skip to content

Commit e9fdb96

Browse files
committed
[SLP][NFC]Make collectValuesToDemote member of BoUpSLP to avoid using
Expr container, NFC. Saves the memory and may improve compile time.
1 parent c38dbfd commit e9fdb96

File tree

1 file changed

+34
-40
lines changed

1 file changed

+34
-40
lines changed

llvm/lib/Transforms/Vectorize/SLPVectorizer.cpp

Lines changed: 34 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2283,6 +2283,14 @@ class BoUpSLP {
22832283
~BoUpSLP();
22842284

22852285
private:
2286+
/// Determine if a vectorized value \p V in can be demoted to
2287+
/// a smaller type with a truncation. We collect the values that will be
2288+
/// demoted in ToDemote and additional roots that require investigating in
2289+
/// Roots.
2290+
bool collectValuesToDemote(Value *V, SmallVectorImpl<Value *> &ToDemote,
2291+
SmallVectorImpl<Value *> &Roots,
2292+
DenseSet<Value *> &Visited) const;
2293+
22862294
/// Check if the operands on the edges \p Edges of the \p UserTE allows
22872295
/// reordering (i.e. the operands can be reordered because they have only one
22882296
/// user and reordarable).
@@ -9044,8 +9052,7 @@ InstructionCost BoUpSLP::getTreeCost(ArrayRef<Value *> VectorizedVals) {
90449052
// for the extract and the added cost of the sign extend if needed.
90459053
auto *VecTy = FixedVectorType::get(EU.Scalar->getType(), BundleWidth);
90469054
TTI::TargetCostKind CostKind = TTI::TCK_RecipThroughput;
9047-
auto *ScalarRoot = VectorizableTree[0]->Scalars[0];
9048-
auto It = MinBWs.find(ScalarRoot);
9055+
auto It = MinBWs.find(EU.Scalar);
90499056
if (It != MinBWs.end()) {
90509057
auto *MinTy = IntegerType::get(F->getContext(), It->second.first);
90519058
unsigned Extend =
@@ -13074,19 +13081,20 @@ unsigned BoUpSLP::getVectorElementSize(Value *V) {
1307413081
// Determine if a value V in a vectorizable expression Expr can be demoted to a
1307513082
// smaller type with a truncation. We collect the values that will be demoted
1307613083
// in ToDemote and additional roots that require investigating in Roots.
13077-
static bool collectValuesToDemote(Value *V, SmallPtrSetImpl<Value *> &Expr,
13078-
SmallVectorImpl<Value *> &ToDemote,
13079-
SmallVectorImpl<Value *> &Roots) {
13084+
bool BoUpSLP::collectValuesToDemote(Value *V,
13085+
SmallVectorImpl<Value *> &ToDemote,
13086+
SmallVectorImpl<Value *> &Roots,
13087+
DenseSet<Value *> &Visited) const {
1308013088
// We can always demote constants.
1308113089
if (isa<Constant>(V)) {
1308213090
ToDemote.push_back(V);
1308313091
return true;
1308413092
}
1308513093

13086-
// If the value is not an instruction in the expression with only one use, it
13087-
// cannot be demoted.
13094+
// If the value is not a vectorized instruction in the expression with only
13095+
// one use, it cannot be demoted.
1308813096
auto *I = dyn_cast<Instruction>(V);
13089-
if (!I || !I->hasOneUse() || !Expr.count(I))
13097+
if (!I || !I->hasOneUse() || !getTreeEntry(I) || !Visited.insert(I).second)
1309013098
return false;
1309113099

1309213100
switch (I->getOpcode()) {
@@ -13110,16 +13118,16 @@ static bool collectValuesToDemote(Value *V, SmallPtrSetImpl<Value *> &Expr,
1311013118
case Instruction::And:
1311113119
case Instruction::Or:
1311213120
case Instruction::Xor:
13113-
if (!collectValuesToDemote(I->getOperand(0), Expr, ToDemote, Roots) ||
13114-
!collectValuesToDemote(I->getOperand(1), Expr, ToDemote, Roots))
13121+
if (!collectValuesToDemote(I->getOperand(0), ToDemote, Roots, Visited) ||
13122+
!collectValuesToDemote(I->getOperand(1), ToDemote, Roots, Visited))
1311513123
return false;
1311613124
break;
1311713125

1311813126
// We can demote selects if we can demote their true and false values.
1311913127
case Instruction::Select: {
1312013128
SelectInst *SI = cast<SelectInst>(I);
13121-
if (!collectValuesToDemote(SI->getTrueValue(), Expr, ToDemote, Roots) ||
13122-
!collectValuesToDemote(SI->getFalseValue(), Expr, ToDemote, Roots))
13129+
if (!collectValuesToDemote(SI->getTrueValue(), ToDemote, Roots, Visited) ||
13130+
!collectValuesToDemote(SI->getFalseValue(), ToDemote, Roots, Visited))
1312313131
return false;
1312413132
break;
1312513133
}
@@ -13129,7 +13137,7 @@ static bool collectValuesToDemote(Value *V, SmallPtrSetImpl<Value *> &Expr,
1312913137
case Instruction::PHI: {
1313013138
PHINode *PN = cast<PHINode>(I);
1313113139
for (Value *IncValue : PN->incoming_values())
13132-
if (!collectValuesToDemote(IncValue, Expr, ToDemote, Roots))
13140+
if (!collectValuesToDemote(IncValue, ToDemote, Roots, Visited))
1313313141
return false;
1313413142
break;
1313513143
}
@@ -13156,36 +13164,20 @@ void BoUpSLP::computeMinimumValueSizes() {
1315613164
if (!TreeRootIT)
1315713165
return;
1315813166

13159-
// If the expression is not rooted by a store, these roots should have
13160-
// external uses.
13161-
// TOSO: investigate if this can be relaxed.
13162-
SmallPtrSet<Value *, 32> Expr(TreeRoot.begin(), TreeRoot.end());
13163-
for (auto &EU : ExternalUses)
13164-
if (!Expr.erase(EU.Scalar))
13165-
return;
13166-
if (!Expr.empty())
13167+
// Ensure the roots of the vectorizable tree don't form a cycle.
13168+
if (!VectorizableTree.front()->UserTreeIndices.empty())
1316713169
return;
1316813170

13169-
// Collect the scalar values of the vectorizable expression. We will use this
13170-
// context to determine which values can be demoted. If we see a truncation,
13171-
// we mark it as seeding another demotion.
13172-
for (auto &EntryPtr : VectorizableTree)
13173-
Expr.insert(EntryPtr->Scalars.begin(), EntryPtr->Scalars.end());
13174-
13175-
// Ensure the roots of the vectorizable tree don't form a cycle. They must
13176-
// have a single external user that is not in the vectorizable tree.
13177-
for (auto *Root : TreeRoot)
13178-
if (!Root->hasOneUse() || Expr.count(*Root->user_begin()))
13179-
return;
13180-
1318113171
// Conservatively determine if we can actually truncate the roots of the
1318213172
// expression. Collect the values that can be demoted in ToDemote and
1318313173
// additional roots that require investigating in Roots.
1318413174
SmallVector<Value *, 32> ToDemote;
1318513175
SmallVector<Value *, 4> Roots;
13186-
for (auto *Root : TreeRoot)
13187-
if (!collectValuesToDemote(Root, Expr, ToDemote, Roots))
13176+
for (auto *Root : TreeRoot) {
13177+
DenseSet<Value *> Visited;
13178+
if (!collectValuesToDemote(Root, ToDemote, Roots, Visited))
1318813179
return;
13180+
}
1318913181

1319013182
// The maximum bit width required to represent all the values that can be
1319113183
// demoted without loss of precision. It would be safe to truncate the roots
@@ -13215,9 +13207,9 @@ void BoUpSLP::computeMinimumValueSizes() {
1321513207
// maximum bit width required to store the scalar by using ValueTracking to
1321613208
// compute the number of high-order bits we can truncate.
1321713209
if (MaxBitWidth == DL->getTypeSizeInBits(TreeRoot[0]->getType()) &&
13218-
llvm::all_of(TreeRoot, [](Value *R) {
13219-
assert(R->hasOneUse() && "Root should have only one use!");
13220-
return isa<GetElementPtrInst>(R->user_back());
13210+
all_of(TreeRoot, [](Value *V) {
13211+
return all_of(V->users(),
13212+
[](User *U) { return isa<GetElementPtrInst>(U); });
1322113213
})) {
1322213214
MaxBitWidth = 8u;
1322313215

@@ -13266,8 +13258,10 @@ void BoUpSLP::computeMinimumValueSizes() {
1326613258
// If we can truncate the root, we must collect additional values that might
1326713259
// be demoted as a result. That is, those seeded by truncations we will
1326813260
// modify.
13269-
while (!Roots.empty())
13270-
collectValuesToDemote(Roots.pop_back_val(), Expr, ToDemote, Roots);
13261+
while (!Roots.empty()) {
13262+
DenseSet<Value *> Visited;
13263+
collectValuesToDemote(Roots.pop_back_val(), ToDemote, Roots, Visited);
13264+
}
1327113265

1327213266
// Finally, map the values we can demote to the maximum bit with we computed.
1327313267
DenseMap<const TreeEntry *, bool> Signendness;

0 commit comments

Comments
 (0)