Skip to content

Commit 91a40f9

Browse files
committed
Remove unnecessary VPEVLBasedIVPHIRecipe.
1 parent ef2e776 commit 91a40f9

35 files changed

+754
-1123
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

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

24272427
Value *TC = getTripCount();
2428-
Type *Ty = TC->getType();
24292428
IRBuilder<> Builder(InsertBlock->getTerminator());
24302429

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

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

@@ -4477,7 +4477,6 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
44774477
case VPDef::VPVectorPointerSC:
44784478
case VPDef::VPVectorEndPointerSC:
44794479
case VPDef::VPExpandSCEVSC:
4480-
case VPDef::VPEVLBasedIVPHISC:
44814480
case VPDef::VPPredInstPHISC:
44824481
case VPDef::VPBranchOnMaskSC:
44834482
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:
@@ -2919,49 +2918,6 @@ class VPActiveLaneMaskPHIRecipe : public VPHeaderPHIRecipe {
29192918
#endif
29202919
};
29212920

2922-
/// A recipe for generating the phi node for the current index of elements,
2923-
/// adjusted in accordance with EVL value. It starts at the start value of the
2924-
/// canonical induction and gets incremented by EVL in each iteration of the
2925-
/// vector loop.
2926-
class VPEVLBasedIVPHIRecipe : public VPHeaderPHIRecipe {
2927-
public:
2928-
VPEVLBasedIVPHIRecipe(VPValue *StartIV, DebugLoc DL)
2929-
: VPHeaderPHIRecipe(VPDef::VPEVLBasedIVPHISC, nullptr, StartIV, DL) {}
2930-
2931-
~VPEVLBasedIVPHIRecipe() override = default;
2932-
2933-
VPEVLBasedIVPHIRecipe *clone() override {
2934-
llvm_unreachable("cloning not implemented yet");
2935-
}
2936-
2937-
VP_CLASSOF_IMPL(VPDef::VPEVLBasedIVPHISC)
2938-
2939-
void execute(VPTransformState &State) override {
2940-
llvm_unreachable("cannot execute this recipe, should be replaced by a "
2941-
"scalar phi recipe");
2942-
}
2943-
2944-
/// Return the cost of this VPEVLBasedIVPHIRecipe.
2945-
InstructionCost computeCost(ElementCount VF,
2946-
VPCostContext &Ctx) const override {
2947-
// For now, match the behavior of the legacy cost model.
2948-
return 0;
2949-
}
2950-
2951-
/// Returns true if the recipe only uses the first lane of operand \p Op.
2952-
bool onlyFirstLaneUsed(const VPValue *Op) const override {
2953-
assert(is_contained(operands(), Op) &&
2954-
"Op must be an operand of the recipe");
2955-
return true;
2956-
}
2957-
2958-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2959-
/// Print the recipe.
2960-
void print(raw_ostream &O, const Twine &Indent,
2961-
VPSlotTracker &SlotTracker) const override;
2962-
#endif
2963-
};
2964-
29652921
/// A Recipe for widening the canonical induction variable of the vector loop.
29662922
class VPWidenCanonicalIVRecipe : public VPSingleDefRecipe,
29672923
public VPUnrollPartAccessor<1> {

llvm/lib/Transforms/Vectorize/VPlanAnalysis.cpp

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

llvm/lib/Transforms/Vectorize/VPlanRecipes.cpp

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3749,14 +3749,3 @@ void VPActiveLaneMaskPHIRecipe::print(raw_ostream &O, const Twine &Indent,
37493749
printOperands(O, SlotTracker);
37503750
}
37513751
#endif
3752-
3753-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3754-
void VPEVLBasedIVPHIRecipe::print(raw_ostream &O, const Twine &Indent,
3755-
VPSlotTracker &SlotTracker) const {
3756-
O << Indent << "EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI ";
3757-
3758-
printAsOperand(O, SlotTracker);
3759-
O << " = phi ";
3760-
printOperands(O, SlotTracker);
3761-
}
3762-
#endif

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -2021,10 +2021,9 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
20212021
}
20222022
}
20232023

2024-
/// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and
2025-
/// replaces all uses except the canonical IV increment of
2026-
/// VPCanonicalIVPHIRecipe with a VPEVLBasedIVPHIRecipe. VPCanonicalIVPHIRecipe
2027-
/// is used only for loop iterations counting after this transformation.
2024+
/// Add a VPInstruction::ExplicitVectorLength and related recipes to \p Plan and
2025+
/// adjust the increment of the canonical induction variable to an explicit
2026+
/// vector length.
20282027
///
20292028
/// The function uses the following definitions:
20302029
/// %StartV is the canonical induction start value.
@@ -2035,29 +2034,25 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
20352034
/// ...
20362035
///
20372036
/// vector.body:
2038-
/// ...
2039-
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
2040-
/// [ %NextEVLIV, %vector.body ]
2041-
/// %AVL = sub original TC, %EVLPhi
2037+
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT
2038+
/// %AVL = sub original TC, %IVPhi
20422039
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %AVL
20432040
/// ...
2044-
/// %NextEVLIV = add IVSize (cast i32 %VPEVVL to IVSize), %EVLPhi
2041+
/// %IV.NEXT = add %IVPhi IVSize (cast i32 %VPEVL to IVSize)
20452042
/// ...
20462043
///
20472044
/// If MaxSafeElements is provided, the function adds the following recipes:
20482045
/// vector.ph:
20492046
/// ...
20502047
///
20512048
/// vector.body:
2052-
/// ...
2053-
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
2054-
/// [ %NextEVLIV, %vector.body ]
2049+
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT
20552050
/// %AVL = sub original TC, %EVLPhi
20562051
/// %cmp = cmp ult %AVL, MaxSafeElements
20572052
/// %SAFE_AVL = select %cmp, %AVL, MaxSafeElements
20582053
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %SAFE_AVL
20592054
/// ...
2060-
/// %NextEVLIV = add IVSize (cast i32 %VPEVL to IVSize), %EVLPhi
2055+
/// %IV.NEXT = add %IVPhi IVSize (cast i32 %VPEVL to IVSize)
20612056
/// ...
20622057
///
20632058
bool VPlanTransforms::tryAddExplicitVectorLength(
@@ -2073,16 +2068,11 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
20732068
return false;
20742069

20752070
auto *CanonicalIVPHI = Plan.getCanonicalIV();
2076-
VPValue *StartV = CanonicalIVPHI->getStartValue();
2077-
2078-
// Create the ExplicitVectorLengthPhi recipe in the main loop.
2079-
auto *EVLPhi = new VPEVLBasedIVPHIRecipe(StartV, DebugLoc());
2080-
EVLPhi->insertAfter(CanonicalIVPHI);
20812071
VPBuilder Builder(Header, Header->getFirstNonPhi());
20822072
// Compute original TC - IV as the AVL (application vector length).
2083-
VPValue *AVL = Builder.createNaryOp(Instruction::Sub,
2084-
{&Plan.getVectorTripCount(), EVLPhi},
2085-
DebugLoc(), "avl");
2073+
VPValue *AVL = Builder.createNaryOp(
2074+
Instruction::Sub, {&Plan.getVectorTripCount(), CanonicalIVPHI},
2075+
DebugLoc(), "avl");
20862076
if (MaxSafeElements) {
20872077
// Support for MaxSafeDist for correct loop emission.
20882078
VPValue *AVLSafe = Plan.getOrAddLiveIn(
@@ -2095,29 +2085,20 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
20952085

20962086
auto *CanonicalIVIncrement =
20972087
cast<VPInstruction>(CanonicalIVPHI->getBackedgeValue());
2098-
Builder.setInsertPoint(CanonicalIVIncrement);
20992088
VPSingleDefRecipe *OpVPEVL = VPEVL;
21002089
if (unsigned IVSize = CanonicalIVPHI->getScalarType()->getScalarSizeInBits();
21012090
IVSize != 32) {
21022091
OpVPEVL = Builder.createScalarCast(
21032092
IVSize < 32 ? Instruction::Trunc : Instruction::ZExt, OpVPEVL,
21042093
CanonicalIVPHI->getScalarType(), CanonicalIVIncrement->getDebugLoc());
21052094
}
2106-
auto *NextEVLIV = Builder.createOverflowingOp(
2107-
Instruction::Add, {OpVPEVL, EVLPhi},
2108-
{CanonicalIVIncrement->hasNoUnsignedWrap(),
2109-
CanonicalIVIncrement->hasNoSignedWrap()},
2110-
CanonicalIVIncrement->getDebugLoc(), "index.evl.next");
2111-
EVLPhi->addOperand(NextEVLIV);
21122095

21132096
transformRecipestoEVLRecipes(Plan, *VPEVL);
21142097

2115-
// Replace all uses of VPCanonicalIVPHIRecipe by
2116-
// VPEVLBasedIVPHIRecipe except for the canonical IV increment.
2117-
CanonicalIVPHI->replaceAllUsesWith(EVLPhi);
2118-
CanonicalIVIncrement->replaceAllUsesWith(NextEVLIV);
2098+
// Adjust the increment of the canonical induction variable to an explicit
2099+
// vector length.
21192100
CanonicalIVIncrement->setOperand(0, CanonicalIVPHI);
2120-
CanonicalIVPHI->setOperand(1, CanonicalIVIncrement);
2101+
CanonicalIVIncrement->setOperand(1, OpVPEVL);
21212102
// TODO: support unroll factor > 1.
21222103
Plan.setUF(1);
21232104
return true;
@@ -2301,17 +2282,14 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
23012282
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
23022283
vp_depth_first_deep(Plan.getEntry()))) {
23032284
for (VPRecipeBase &R : make_early_inc_range(VPBB->phis())) {
2304-
if (!isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R))
2305-
continue;
2306-
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
2307-
StringRef Name =
2308-
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
2309-
auto *ScalarR = new VPInstruction(
2310-
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()},
2311-
PhiR->getDebugLoc(), Name);
2312-
ScalarR->insertBefore(PhiR);
2313-
PhiR->replaceAllUsesWith(ScalarR);
2314-
PhiR->eraseFromParent();
2285+
if (auto *PhiR = dyn_cast<VPCanonicalIVPHIRecipe>(&R)) {
2286+
auto *ScalarR = new VPInstruction(
2287+
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()},
2288+
PhiR->getDebugLoc(), "index");
2289+
ScalarR->insertBefore(PhiR);
2290+
PhiR->replaceAllUsesWith(ScalarR);
2291+
PhiR->eraseFromParent();
2292+
}
23152293
}
23162294
}
23172295
}

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)