Skip to content

Commit 357bbaa

Browse files
committed
[VPlan] Add VPRecipeBase::toVPUser helper (NFC).
This adds a helper to convert a VPRecipeBase pointer to a VPUser, for recipes that inherit from VPUser. Once VPRecipeBase directly inherits from VPUser this helper can be removed.
1 parent f5fe7ab commit 357bbaa

File tree

3 files changed

+47
-1
lines changed

3 files changed

+47
-1
lines changed

llvm/lib/Transforms/Vectorize/VPlan.cpp

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,30 @@ void VPRecipeBase::dump() const {
7777
dbgs() << "\n";
7878
}
7979

80+
VPUser *VPRecipeBase::toVPUser() {
81+
if (auto *U = dyn_cast<VPInstruction>(this))
82+
return U;
83+
if (auto *U = dyn_cast<VPWidenRecipe>(this))
84+
return U;
85+
if (auto *U = dyn_cast<VPWidenCallRecipe>(this))
86+
return U;
87+
if (auto *U = dyn_cast<VPWidenSelectRecipe>(this))
88+
return U;
89+
if (auto *U = dyn_cast<VPWidenGEPRecipe>(this))
90+
return U;
91+
if (auto *U = dyn_cast<VPBlendRecipe>(this))
92+
return U;
93+
if (auto *U = dyn_cast<VPInterleaveRecipe>(this))
94+
return U;
95+
if (auto *U = dyn_cast<VPReplicateRecipe>(this))
96+
return U;
97+
if (auto *U = dyn_cast<VPBranchOnMaskRecipe>(this))
98+
return U;
99+
if (auto *U = dyn_cast<VPWidenMemoryInstructionRecipe>(this))
100+
return U;
101+
return nullptr;
102+
}
103+
80104
// Get the top-most entry block of \p Start. This is the entry block of the
81105
// containing VPlan. This function is templated to support both const and non-const blocks
82106
template <typename T> static T *getPlanEntry(T *Start) {

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -676,10 +676,15 @@ class VPRecipeBase : public ilist_node_with_parent<VPRecipeBase, VPBasicBlock> {
676676
///
677677
/// \returns an iterator pointing to the element after the erased one
678678
iplist<VPRecipeBase>::iterator eraseFromParent();
679+
680+
/// Returns a pointer to a VPUser, if the recipe inherits from VPUser or
681+
/// nullptr otherwise.
682+
VPUser *toVPUser();
679683
};
680684

681685
inline bool VPUser::classof(const VPRecipeBase *Recipe) {
682-
return Recipe->getVPRecipeID() == VPRecipeBase::VPWidenSC ||
686+
return Recipe->getVPRecipeID() == VPRecipeBase::VPInstructionSC ||
687+
Recipe->getVPRecipeID() == VPRecipeBase::VPWidenSC ||
683688
Recipe->getVPRecipeID() == VPRecipeBase::VPWidenCallSC ||
684689
Recipe->getVPRecipeID() == VPRecipeBase::VPWidenSelectSC ||
685690
Recipe->getVPRecipeID() == VPRecipeBase::VPWidenGEPSC ||

llvm/unittests/Transforms/Vectorize/VPlanTest.cpp

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -339,6 +339,16 @@ compound=true
339339
}
340340
}
341341

342+
TEST(VPRecipeTest, CastVPInstructionToVPUser) {
343+
VPValue Op1;
344+
VPValue Op2;
345+
VPInstruction Recipe(Instruction::Add, {&Op1, &Op2});
346+
EXPECT_TRUE(isa<VPUser>(&Recipe));
347+
VPRecipeBase *BaseR = &Recipe;
348+
EXPECT_TRUE(isa<VPUser>(BaseR));
349+
EXPECT_EQ(&Recipe, BaseR->toVPUser());
350+
}
351+
342352
TEST(VPRecipeTest, CastVPWidenRecipeToVPUser) {
343353
LLVMContext C;
344354

@@ -354,6 +364,7 @@ TEST(VPRecipeTest, CastVPWidenRecipeToVPUser) {
354364
EXPECT_TRUE(isa<VPUser>(&WidenR));
355365
VPRecipeBase *WidenRBase = &WidenR;
356366
EXPECT_TRUE(isa<VPUser>(WidenRBase));
367+
EXPECT_EQ(&WidenR, WidenRBase->toVPUser());
357368
delete AI;
358369
}
359370

@@ -372,6 +383,7 @@ TEST(VPRecipeTest, CastVPWidenCallRecipeToVPUser) {
372383
EXPECT_TRUE(isa<VPUser>(&Recipe));
373384
VPRecipeBase *BaseR = &Recipe;
374385
EXPECT_TRUE(isa<VPUser>(BaseR));
386+
EXPECT_EQ(&Recipe, BaseR->toVPUser());
375387
delete Call;
376388
}
377389

@@ -394,6 +406,7 @@ TEST(VPRecipeTest, CastVPWidenSelectRecipeToVPUser) {
394406
EXPECT_TRUE(isa<VPUser>(&WidenSelectR));
395407
VPRecipeBase *BaseR = &WidenSelectR;
396408
EXPECT_TRUE(isa<VPUser>(BaseR));
409+
EXPECT_EQ(&WidenSelectR, BaseR->toVPUser());
397410
delete SelectI;
398411
}
399412

@@ -413,6 +426,7 @@ TEST(VPRecipeTest, CastVPWidenGEPRecipeToVPUser) {
413426
EXPECT_TRUE(isa<VPUser>(&Recipe));
414427
VPRecipeBase *BaseR = &Recipe;
415428
EXPECT_TRUE(isa<VPUser>(BaseR));
429+
EXPECT_EQ(&Recipe, BaseR->toVPUser());
416430
delete GEP;
417431
}
418432

@@ -442,6 +456,7 @@ TEST(VPRecipeTest, CastVPInterleaveRecipeToVPUser) {
442456
EXPECT_TRUE(isa<VPUser>(&Recipe));
443457
VPRecipeBase *BaseR = &Recipe;
444458
EXPECT_TRUE(isa<VPUser>(BaseR));
459+
EXPECT_EQ(&Recipe, BaseR->toVPUser());
445460
}
446461

447462
TEST(VPRecipeTest, CastVPReplicateRecipeToVPUser) {
@@ -468,6 +483,7 @@ TEST(VPRecipeTest, CastVPBranchOnMaskRecipeToVPUser) {
468483
EXPECT_TRUE(isa<VPUser>(&Recipe));
469484
VPRecipeBase *BaseR = &Recipe;
470485
EXPECT_TRUE(isa<VPUser>(BaseR));
486+
EXPECT_EQ(&Recipe, BaseR->toVPUser());
471487
}
472488

473489
TEST(VPRecipeTest, CastVPWidenMemoryInstructionRecipeToVPUser) {
@@ -483,6 +499,7 @@ TEST(VPRecipeTest, CastVPWidenMemoryInstructionRecipeToVPUser) {
483499
EXPECT_TRUE(isa<VPUser>(&Recipe));
484500
VPRecipeBase *BaseR = &Recipe;
485501
EXPECT_TRUE(isa<VPUser>(BaseR));
502+
EXPECT_EQ(&Recipe, BaseR->toVPUser());
486503
delete Load;
487504
}
488505

0 commit comments

Comments
 (0)