Skip to content

Commit f61eaad

Browse files
committed
Intro hasOneUser(), change getSingleUser(), and reuse it.
1 parent b252f89 commit f61eaad

File tree

2 files changed

+15
-10
lines changed

2 files changed

+15
-10
lines changed

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2260,15 +2260,10 @@ InstructionCost VPWidenCastRecipe::computeCost(ElementCount VF,
22602260
TTI::CastContextHint CCH = TTI::CastContextHint::None;
22612261
// For Trunc/FPTrunc, get the context from the only user.
22622262
if (Opcode == Instruction::Trunc || Opcode == Instruction::FPTrunc) {
2263-
auto GetOnlyUser = [](const VPSingleDefRecipe *R) -> VPRecipeBase * {
2264-
if (R->getNumUsers() == 0 || R->hasMoreThanOneUniqueUser())
2265-
return nullptr;
2266-
return dyn_cast<VPRecipeBase>(*R->user_begin());
2267-
};
2268-
2269-
if (VPRecipeBase *Recipe = GetOnlyUser(this)) {
2263+
if (auto *Recipe = dyn_cast_or_null<VPRecipeBase>(getSingleUser())) {
22702264
if (match(Recipe, m_VPInstruction<VPInstruction::Reverse>(m_VPValue())))
2271-
Recipe = GetOnlyUser(cast<VPInstruction>(Recipe));
2265+
Recipe = dyn_cast_or_null<VPRecipeBase>(
2266+
cast<VPInstruction>(Recipe)->getSingleUser());
22722267
if (Recipe)
22732268
CCH = ComputeCCH(Recipe);
22742269
}

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -150,11 +150,21 @@ class LLVM_ABI_FOR_TEST VPValue {
150150

151151
bool hasOneUse() const { return getNumUsers() == 1; }
152152

153+
/// Returns true if the value has exactly one unique user, ignoring multiple
154+
/// uses by the same user.
155+
bool hasOneUser() const {
156+
if (getNumUsers() == 0)
157+
return false;
158+
if (hasOneUse())
159+
return true;
160+
return std::equal(std::next(user_begin()), user_end(), user_begin());
161+
}
162+
153163
/// Return the single user of this value, or nullptr if there is not exactly
154164
/// one user.
155-
VPUser *getSingleUser() { return hasOneUse() ? *user_begin() : nullptr; }
165+
VPUser *getSingleUser() { return hasOneUser() ? *user_begin() : nullptr; }
156166
const VPUser *getSingleUser() const {
157-
return hasOneUse() ? *user_begin() : nullptr;
167+
return hasOneUser() ? *user_begin() : nullptr;
158168
}
159169

160170
void replaceAllUsesWith(VPValue *New);

0 commit comments

Comments
 (0)