Skip to content

Commit 8a1196c

Browse files
committed
Remove unnecessary VPEVLBasedIVPHIRecipe.
1 parent a80f972 commit 8a1196c

35 files changed

+682
-1051
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2426,7 +2426,6 @@ InnerLoopVectorizer::getOrCreateVectorTripCount(BasicBlock *InsertBlock) {
24262426
return VectorTripCount;
24272427

24282428
Value *TC = getTripCount();
2429-
Type *Ty = TC->getType();
24302429
IRBuilder<> Builder(InsertBlock->getTerminator());
24312430

24322431
// Use original trip count as the vector trip count if use predicated EVL
@@ -2438,6 +2437,7 @@ InnerLoopVectorizer::getOrCreateVectorTripCount(BasicBlock *InsertBlock) {
24382437
return VectorTripCount = TC;
24392438
}
24402439

2440+
Type *Ty = TC->getType();
24412441
// This is where we can make the step a runtime constant.
24422442
Value *Step = createStepForVF(Builder, Ty, VF, UF);
24432443

@@ -4453,7 +4453,6 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
44534453
case VPDef::VPVectorPointerSC:
44544454
case VPDef::VPVectorEndPointerSC:
44554455
case VPDef::VPExpandSCEVSC:
4456-
case VPDef::VPEVLBasedIVPHISC:
44574456
case VPDef::VPPredInstPHISC:
44584457
case VPDef::VPBranchOnMaskSC:
44594458
continue;

llvm/lib/Transforms/Vectorize/VPlan.h

Lines changed: 0 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -506,7 +506,6 @@ class VPSingleDefRecipe : public VPRecipeBase, public VPValue {
506506
static inline bool classof(const VPRecipeBase *R) {
507507
switch (R->getVPDefID()) {
508508
case VPRecipeBase::VPDerivedIVSC:
509-
case VPRecipeBase::VPEVLBasedIVPHISC:
510509
case VPRecipeBase::VPExpandSCEVSC:
511510
case VPRecipeBase::VPInstructionSC:
512511
case VPRecipeBase::VPReductionEVLSC:
@@ -2891,49 +2890,6 @@ class VPActiveLaneMaskPHIRecipe : public VPHeaderPHIRecipe {
28912890
#endif
28922891
};
28932892

2894-
/// A recipe for generating the phi node for the current index of elements,
2895-
/// adjusted in accordance with EVL value. It starts at the start value of the
2896-
/// canonical induction and gets incremented by EVL in each iteration of the
2897-
/// vector loop.
2898-
class VPEVLBasedIVPHIRecipe : public VPHeaderPHIRecipe {
2899-
public:
2900-
VPEVLBasedIVPHIRecipe(VPValue *StartIV, DebugLoc DL)
2901-
: VPHeaderPHIRecipe(VPDef::VPEVLBasedIVPHISC, nullptr, StartIV, DL) {}
2902-
2903-
~VPEVLBasedIVPHIRecipe() override = default;
2904-
2905-
VPEVLBasedIVPHIRecipe *clone() override {
2906-
llvm_unreachable("cloning not implemented yet");
2907-
}
2908-
2909-
VP_CLASSOF_IMPL(VPDef::VPEVLBasedIVPHISC)
2910-
2911-
void execute(VPTransformState &State) override {
2912-
llvm_unreachable(
2913-
"cannot execute this recipe, should be replaced by VPScalarPHIRecipe");
2914-
}
2915-
2916-
/// Return the cost of this VPEVLBasedIVPHIRecipe.
2917-
InstructionCost computeCost(ElementCount VF,
2918-
VPCostContext &Ctx) const override {
2919-
// For now, match the behavior of the legacy cost model.
2920-
return 0;
2921-
}
2922-
2923-
/// Returns true if the recipe only uses the first lane of operand \p Op.
2924-
bool onlyFirstLaneUsed(const VPValue *Op) const override {
2925-
assert(is_contained(operands(), Op) &&
2926-
"Op must be an operand of the recipe");
2927-
return true;
2928-
}
2929-
2930-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2931-
/// Print the recipe.
2932-
void print(raw_ostream &O, const Twine &Indent,
2933-
VPSlotTracker &SlotTracker) const override;
2934-
#endif
2935-
};
2936-
29372893
/// A Recipe for widening the canonical induction variable of the vector loop.
29382894
class VPWidenCanonicalIVRecipe : public VPSingleDefRecipe,
29392895
public VPUnrollPartAccessor<1> {

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -243,14 +243,13 @@ Type *VPTypeAnalysis::inferScalarType(const VPValue *V) {
243243
TypeSwitch<const VPRecipeBase *, Type *>(V->getDefiningRecipe())
244244
.Case<VPActiveLaneMaskPHIRecipe, VPCanonicalIVPHIRecipe,
245245
VPFirstOrderRecurrencePHIRecipe, VPReductionPHIRecipe,
246-
VPWidenPointerInductionRecipe, VPEVLBasedIVPHIRecipe>(
247-
[this](const auto *R) {
248-
// Handle header phi recipes, except VPWidenIntOrFpInduction
249-
// which needs special handling due it being possibly truncated.
250-
// TODO: consider inferring/caching type of siblings, e.g.,
251-
// backedge value, here and in cases below.
252-
return inferScalarType(R->getStartValue());
253-
})
246+
VPWidenPointerInductionRecipe>([this](const auto *R) {
247+
// Handle header phi recipes, except VPWidenIntOrFpInduction which
248+
// needs special handling due it being possibly truncated.
249+
// TODO: consider inferring/caching type of siblings, e.g., backedge
250+
// value, here and in cases below.
251+
return inferScalarType(R->getStartValue());
252+
})
254253
.Case<VPWidenIntOrFpInductionRecipe, VPDerivedIVRecipe>(
255254
[](const auto *R) { return R->getScalarType(); })
256255
.Case<VPReductionRecipe, VPPredInstPHIRecipe, VPWidenPHIRecipe,

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3700,14 +3700,3 @@ void VPActiveLaneMaskPHIRecipe::print(raw_ostream &O, const Twine &Indent,
37003700
printOperands(O, SlotTracker);
37013701
}
37023702
#endif
3703-
3704-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3705-
void VPEVLBasedIVPHIRecipe::print(raw_ostream &O, const Twine &Indent,
3706-
VPSlotTracker &SlotTracker) const {
3707-
O << Indent << "EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI ";
3708-
3709-
printAsOperand(O, SlotTracker);
3710-
O << " = phi ";
3711-
printOperands(O, SlotTracker);
3712-
}
3713-
#endif

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1812,10 +1812,9 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
18121812
}
18131813
}
18141814

1815-
/// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and
1816-
/// replaces all uses except the canonical IV increment of
1817-
/// VPCanonicalIVPHIRecipe with a VPEVLBasedIVPHIRecipe. VPCanonicalIVPHIRecipe
1818-
/// is used only for loop iterations counting after this transformation.
1815+
/// Add a VPInstruction::ExplicitVectorLength and related recipes to \p Plan and
1816+
/// adjust the increment of the canonical induction variable to an explicit
1817+
/// vector length.
18191818
///
18201819
/// The function uses the following definitions:
18211820
/// %StartV is the canonical induction start value.
@@ -1826,29 +1825,25 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
18261825
/// ...
18271826
///
18281827
/// vector.body:
1829-
/// ...
1830-
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
1831-
/// [ %NextEVLIV, %vector.body ]
1832-
/// %AVL = sub original TC, %EVLPhi
1828+
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT
1829+
/// %AVL = sub original TC, %IVPhi
18331830
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %AVL
18341831
/// ...
1835-
/// %NextEVLIV = add IVSize (cast i32 %VPEVVL to IVSize), %EVLPhi
1832+
/// %IV.NEXT = add %IVPhi IVSize (cast i32 %VPEVL to IVSize)
18361833
/// ...
18371834
///
18381835
/// If MaxSafeElements is provided, the function adds the following recipes:
18391836
/// vector.ph:
18401837
/// ...
18411838
///
18421839
/// vector.body:
1843-
/// ...
1844-
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
1845-
/// [ %NextEVLIV, %vector.body ]
1840+
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT
18461841
/// %AVL = sub original TC, %EVLPhi
18471842
/// %cmp = cmp ult %AVL, MaxSafeElements
18481843
/// %SAFE_AVL = select %cmp, %AVL, MaxSafeElements
18491844
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %SAFE_AVL
18501845
/// ...
1851-
/// %NextEVLIV = add IVSize (cast i32 %VPEVL to IVSize), %EVLPhi
1846+
/// %IV.NEXT = add %IVPhi IVSize (cast i32 %VPEVL to IVSize)
18521847
/// ...
18531848
///
18541849
bool VPlanTransforms::tryAddExplicitVectorLength(
@@ -1864,16 +1859,11 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
18641859
return false;
18651860

18661861
auto *CanonicalIVPHI = Plan.getCanonicalIV();
1867-
VPValue *StartV = CanonicalIVPHI->getStartValue();
1868-
1869-
// Create the ExplicitVectorLengthPhi recipe in the main loop.
1870-
auto *EVLPhi = new VPEVLBasedIVPHIRecipe(StartV, DebugLoc());
1871-
EVLPhi->insertAfter(CanonicalIVPHI);
18721862
VPBuilder Builder(Header, Header->getFirstNonPhi());
18731863
// Compute original TC - IV as the AVL (application vector length).
1874-
VPValue *AVL = Builder.createNaryOp(Instruction::Sub,
1875-
{&Plan.getVectorTripCount(), EVLPhi},
1876-
DebugLoc(), "avl");
1864+
VPValue *AVL = Builder.createNaryOp(
1865+
Instruction::Sub, {&Plan.getVectorTripCount(), CanonicalIVPHI},
1866+
DebugLoc(), "avl");
18771867
if (MaxSafeElements) {
18781868
// Support for MaxSafeDist for correct loop emission.
18791869
VPValue *AVLSafe = Plan.getOrAddLiveIn(
@@ -1886,29 +1876,20 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
18861876

18871877
auto *CanonicalIVIncrement =
18881878
cast<VPInstruction>(CanonicalIVPHI->getBackedgeValue());
1889-
Builder.setInsertPoint(CanonicalIVIncrement);
18901879
VPSingleDefRecipe *OpVPEVL = VPEVL;
18911880
if (unsigned IVSize = CanonicalIVPHI->getScalarType()->getScalarSizeInBits();
18921881
IVSize != 32) {
18931882
OpVPEVL = Builder.createScalarCast(
18941883
IVSize < 32 ? Instruction::Trunc : Instruction::ZExt, OpVPEVL,
18951884
CanonicalIVPHI->getScalarType(), CanonicalIVIncrement->getDebugLoc());
18961885
}
1897-
auto *NextEVLIV = Builder.createOverflowingOp(
1898-
Instruction::Add, {OpVPEVL, EVLPhi},
1899-
{CanonicalIVIncrement->hasNoUnsignedWrap(),
1900-
CanonicalIVIncrement->hasNoSignedWrap()},
1901-
CanonicalIVIncrement->getDebugLoc(), "index.evl.next");
1902-
EVLPhi->addOperand(NextEVLIV);
19031886

19041887
transformRecipestoEVLRecipes(Plan, *VPEVL);
19051888

1906-
// Replace all uses of VPCanonicalIVPHIRecipe by
1907-
// VPEVLBasedIVPHIRecipe except for the canonical IV increment.
1908-
CanonicalIVPHI->replaceAllUsesWith(EVLPhi);
1909-
CanonicalIVIncrement->replaceAllUsesWith(NextEVLIV);
1889+
// Adjust the increment of the canonical induction variable to an explicit
1890+
// vector length.
19101891
CanonicalIVIncrement->setOperand(0, CanonicalIVPHI);
1911-
CanonicalIVPHI->setOperand(1, CanonicalIVIncrement);
1892+
CanonicalIVIncrement->setOperand(1, OpVPEVL);
19121893
// TODO: support unroll factor > 1.
19131894
Plan.setUF(1);
19141895
return true;
@@ -2092,17 +2073,14 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
20922073
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
20932074
vp_depth_first_deep(Plan.getEntry()))) {
20942075
for (VPRecipeBase &R : make_early_inc_range(VPBB->phis())) {
2095-
if (!isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R))
2096-
continue;
2097-
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
2098-
StringRef Name =
2099-
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
2100-
auto *ScalarR = new VPInstruction(
2101-
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()},
2102-
PhiR->getDebugLoc(), Name);
2103-
ScalarR->insertBefore(PhiR);
2104-
PhiR->replaceAllUsesWith(ScalarR);
2105-
PhiR->eraseFromParent();
2076+
if (auto *PhiR = dyn_cast<VPCanonicalIVPHIRecipe>(&R)) {
2077+
auto *ScalarR = new VPInstruction(
2078+
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()},
2079+
PhiR->getDebugLoc(), "index");
2080+
ScalarR->insertBefore(PhiR);
2081+
PhiR->replaceAllUsesWith(ScalarR);
2082+
PhiR->eraseFromParent();
2083+
}
21062084
}
21072085
}
21082086
}

llvm/lib/Transforms/Vectorize/VPlanTransforms.h

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -143,11 +143,9 @@ struct VPlanTransforms {
143143
VPlan &Plan,
144144
const std::function<bool(BasicBlock *)> &BlockNeedsPredication);
145145

146-
/// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and
147-
/// replaces all uses except the canonical IV increment of
148-
/// VPCanonicalIVPHIRecipe with a VPEVLBasedIVPHIRecipe.
149-
/// VPCanonicalIVPHIRecipe is only used to control the loop after
150-
/// this transformation.
146+
/// Add a VPInstruction::ExplicitVectorLength and related recipes to \p Plan
147+
/// and adjust the increment of the canonical induction variable to an
148+
/// explicit vector length.
151149
/// \returns true if the transformation succeeds, or false if it doesn't.
152150
static bool
153151
tryAddExplicitVectorLength(VPlan &Plan,

llvm/lib/Transforms/Vectorize/VPlanValue.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,6 @@ class VPDef {
356356
// VPHeaderPHIRecipe need to be kept together.
357357
VPCanonicalIVPHISC,
358358
VPActiveLaneMaskPHISC,
359-
VPEVLBasedIVPHISC,
360359
VPFirstOrderRecurrencePHISC,
361360
VPWidenIntOrFpInductionSC,
362361
VPWidenPointerInductionSC,

llvm/lib/Transforms/Vectorize/VPlanVerifier.cpp

Lines changed: 0 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -156,16 +156,6 @@ bool VPlanVerifier::verifyEVLRecipe(const VPInstruction &EVL) const {
156156
errs() << "EVL is used as an operand in non-VPInstruction::Add\n";
157157
return false;
158158
}
159-
if (!all_of(I->users(), [](VPUser *U) {
160-
if (auto *VPI = dyn_cast<VPInstruction>(U))
161-
return VPI->getOpcode() == VPInstruction::BranchOnCount;
162-
return isa<VPEVLBasedIVPHIRecipe>(U);
163-
})) {
164-
errs()
165-
<< "Result of VPInstruction::Add with EVL operand is not used "
166-
"by VPEVLBasedIVPHIRecipe or VPInstruction::BranchOnCount\n";
167-
return false;
168-
}
169159
return true;
170160
})
171161
.Default([&](const VPUser *U) {

0 commit comments

Comments
 (0)