Skip to content

Commit c41ef17

Browse files
committed
[VPlan] Add getSingleUser helper (NFC).
Add helper to make it easier to retrieve the single user of a VPUser.
1 parent 810d993 commit c41ef17

File tree

3 files changed

+31
-23
lines changed

3 files changed

+31
-23
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 12 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4226,18 +4226,16 @@ VectorizationFactor LoopVectorizationPlanner::selectVectorizationFactor() {
42264226
// Selects are only modelled in the legacy cost model for safe
42274227
// divisors.
42284228
case Instruction::Select: {
4229-
VPValue *VPV = VPI->getVPSingleValue();
4230-
if (VPV->getNumUsers() == 1) {
4231-
if (auto *WR = dyn_cast<VPWidenRecipe>(*VPV->user_begin())) {
4232-
switch (WR->getOpcode()) {
4233-
case Instruction::UDiv:
4234-
case Instruction::SDiv:
4235-
case Instruction::URem:
4236-
case Instruction::SRem:
4237-
continue;
4238-
default:
4239-
break;
4240-
}
4229+
if (auto *WR =
4230+
dyn_cast_or_null<VPWidenRecipe>(VPI->getSingleUser())) {
4231+
switch (WR->getOpcode()) {
4232+
case Instruction::UDiv:
4233+
case Instruction::SDiv:
4234+
case Instruction::URem:
4235+
case Instruction::SRem:
4236+
continue;
4237+
default:
4238+
break;
42414239
}
42424240
}
42434241
C += VPI->cost(VF, CostCtx);
@@ -6976,11 +6974,10 @@ static bool planContainsAdditionalSimplifications(VPlan &Plan,
69766974
// the more accurate VPlan-based cost model.
69776975
for (VPRecipeBase &R : *Plan.getVectorPreheader()) {
69786976
auto *VPI = dyn_cast<VPInstruction>(&R);
6979-
if (!VPI || VPI->getOpcode() != Instruction::Select ||
6980-
VPI->getNumUsers() != 1)
6977+
if (!VPI || VPI->getOpcode() != Instruction::Select)
69816978
continue;
69826979

6983-
if (auto *WR = dyn_cast<VPWidenRecipe>(*VPI->user_begin())) {
6980+
if (auto *WR = dyn_cast_or_null<VPWidenRecipe>(VPI->getSingleUser())) {
69846981
switch (WR->getOpcode()) {
69856982
case Instruction::UDiv:
69866983
case Instruction::SDiv:

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -580,10 +580,13 @@ void VPlanTransforms::removeDeadRecipes(VPlan &Plan) {
580580

581581
// Check if R is a dead VPPhi <-> update cycle and remove it.
582582
auto *PhiR = dyn_cast<VPPhi>(&R);
583-
if (!PhiR || PhiR->getNumOperands() != 2 || PhiR->getNumUsers() != 1)
583+
if (!PhiR || PhiR->getNumOperands() != 2)
584+
continue;
585+
VPUser *PhiUser = PhiR->getSingleUser();
586+
if (!PhiUser)
584587
continue;
585588
VPValue *Incoming = PhiR->getOperand(1);
586-
if (*PhiR->user_begin() != Incoming->getDefiningRecipe() ||
589+
if (PhiUser != Incoming->getDefiningRecipe() ||
587590
Incoming->getNumUsers() != 1)
588591
continue;
589592
PhiR->replaceAllUsesWith(PhiR->getOperand(0));
@@ -1307,7 +1310,7 @@ static void simplifyRecipe(VPSingleDefRecipe *Def, VPTypeAnalysis &TypeInfo) {
13071310
isa<VPPhi>(X)) {
13081311
auto *Phi = cast<VPPhi>(X);
13091312
if (Phi->getOperand(1) != Def && match(Phi->getOperand(0), m_ZeroInt()) &&
1310-
Phi->getNumUsers() == 1 && (*Phi->user_begin() == Def)) {
1313+
Phi->getSingleUser() == Def) {
13111314
Phi->setOperand(0, Y);
13121315
Def->replaceAllUsesWith(Phi);
13131316
return;
@@ -1592,10 +1595,11 @@ static bool optimizeVectorInductionWidthForTCAndVFUF(VPlan &Plan,
15921595

15931596
// Currently only handle cases where the single user is a header-mask
15941597
// comparison with the backedge-taken-count.
1595-
if (!match(*WideIV->user_begin(),
1596-
m_ICmp(m_Specific(WideIV),
1597-
m_Broadcast(
1598-
m_Specific(Plan.getOrCreateBackedgeTakenCount())))))
1598+
VPUser *SingleUser = WideIV->getSingleUser();
1599+
if (!SingleUser ||
1600+
!match(SingleUser, m_ICmp(m_Specific(WideIV),
1601+
m_Broadcast(m_Specific(
1602+
Plan.getOrCreateBackedgeTakenCount())))))
15991603
continue;
16001604

16011605
// Update IV operands and comparison bound to use new narrower type.
@@ -1607,7 +1611,7 @@ static bool optimizeVectorInductionWidthForTCAndVFUF(VPlan &Plan,
16071611
auto *NewBTC = new VPWidenCastRecipe(
16081612
Instruction::Trunc, Plan.getOrCreateBackedgeTakenCount(), NewIVTy);
16091613
Plan.getVectorPreheader()->appendRecipe(NewBTC);
1610-
auto *Cmp = cast<VPInstruction>(*WideIV->user_begin());
1614+
auto *Cmp = cast<VPInstruction>(WideIV->getSingleUser());
16111615
Cmp->setOperand(1, NewBTC);
16121616

16131617
MadeChange = true;

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -150,6 +150,13 @@ class LLVM_ABI_FOR_TEST VPValue {
150150

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

153+
/// Return the single user of this value, or nullptr if there is not exactly
154+
/// one user.
155+
VPUser *getSingleUser() { return hasOneUse() ? *user_begin() : nullptr; }
156+
const VPUser *getSingleUser() const {
157+
return hasOneUse() ? *user_begin() : nullptr;
158+
}
159+
153160
void replaceAllUsesWith(VPValue *New);
154161

155162
/// Go through the uses list for this VPValue and make each use point to \p

0 commit comments

Comments
 (0)