Skip to content

Commit 960a0f7

Browse files
committed
Remove unnecessary VPEVLBasedIVPHIRecipe.
1 parent 54ae7b4 commit 960a0f7

35 files changed

+747
-1116
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

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

24262426
Value *TC = getTripCount();
2427-
Type *Ty = TC->getType();
24282427
IRBuilder<> Builder(InsertBlock->getTerminator());
24292428

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

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

@@ -4451,7 +4451,6 @@ static bool willGenerateVectors(VPlan &Plan, ElementCount VF,
44514451
case VPDef::VPVectorPointerSC:
44524452
case VPDef::VPVectorEndPointerSC:
44534453
case VPDef::VPExpandSCEVSC:
4454-
case VPDef::VPEVLBasedIVPHISC:
44554454
case VPDef::VPPredInstPHISC:
44564455
case VPDef::VPBranchOnMaskSC:
44574456
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:
@@ -2886,49 +2885,6 @@ class VPActiveLaneMaskPHIRecipe : public VPHeaderPHIRecipe {
28862885
#endif
28872886
};
28882887

2889-
/// A recipe for generating the phi node for the current index of elements,
2890-
/// adjusted in accordance with EVL value. It starts at the start value of the
2891-
/// canonical induction and gets incremented by EVL in each iteration of the
2892-
/// vector loop.
2893-
class VPEVLBasedIVPHIRecipe : public VPHeaderPHIRecipe {
2894-
public:
2895-
VPEVLBasedIVPHIRecipe(VPValue *StartIV, DebugLoc DL)
2896-
: VPHeaderPHIRecipe(VPDef::VPEVLBasedIVPHISC, nullptr, StartIV, DL) {}
2897-
2898-
~VPEVLBasedIVPHIRecipe() override = default;
2899-
2900-
VPEVLBasedIVPHIRecipe *clone() override {
2901-
llvm_unreachable("cloning not implemented yet");
2902-
}
2903-
2904-
VP_CLASSOF_IMPL(VPDef::VPEVLBasedIVPHISC)
2905-
2906-
void execute(VPTransformState &State) override {
2907-
llvm_unreachable("cannot execute this recipe, should be replaced by a "
2908-
"scalar phi recipe");
2909-
}
2910-
2911-
/// Return the cost of this VPEVLBasedIVPHIRecipe.
2912-
InstructionCost computeCost(ElementCount VF,
2913-
VPCostContext &Ctx) const override {
2914-
// For now, match the behavior of the legacy cost model.
2915-
return 0;
2916-
}
2917-
2918-
/// Returns true if the recipe only uses the first lane of operand \p Op.
2919-
bool onlyFirstLaneUsed(const VPValue *Op) const override {
2920-
assert(is_contained(operands(), Op) &&
2921-
"Op must be an operand of the recipe");
2922-
return true;
2923-
}
2924-
2925-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
2926-
/// Print the recipe.
2927-
void print(raw_ostream &O, const Twine &Indent,
2928-
VPSlotTracker &SlotTracker) const override;
2929-
#endif
2930-
};
2931-
29322888
/// A Recipe for widening the canonical induction variable of the vector loop.
29332889
class VPWidenCanonicalIVRecipe : public VPSingleDefRecipe,
29342890
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
@@ -3695,14 +3695,3 @@ void VPActiveLaneMaskPHIRecipe::print(raw_ostream &O, const Twine &Indent,
36953695
printOperands(O, SlotTracker);
36963696
}
36973697
#endif
3698-
3699-
#if !defined(NDEBUG) || defined(LLVM_ENABLE_DUMP)
3700-
void VPEVLBasedIVPHIRecipe::print(raw_ostream &O, const Twine &Indent,
3701-
VPSlotTracker &SlotTracker) const {
3702-
O << Indent << "EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI ";
3703-
3704-
printAsOperand(O, SlotTracker);
3705-
O << " = phi ";
3706-
printOperands(O, SlotTracker);
3707-
}
3708-
#endif

llvm/lib/Transforms/Vectorize/VPlanTransforms.cpp

Lines changed: 22 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -1823,10 +1823,9 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
18231823
}
18241824
}
18251825

1826-
/// Add a VPEVLBasedIVPHIRecipe and related recipes to \p Plan and
1827-
/// replaces all uses except the canonical IV increment of
1828-
/// VPCanonicalIVPHIRecipe with a VPEVLBasedIVPHIRecipe. VPCanonicalIVPHIRecipe
1829-
/// is used only for loop iterations counting after this transformation.
1826+
/// Add a VPInstruction::ExplicitVectorLength and related recipes to \p Plan and
1827+
/// adjust the increment of the canonical induction variable to an explicit
1828+
/// vector length.
18301829
///
18311830
/// The function uses the following definitions:
18321831
/// %StartV is the canonical induction start value.
@@ -1837,29 +1836,25 @@ static void transformRecipestoEVLRecipes(VPlan &Plan, VPValue &EVL) {
18371836
/// ...
18381837
///
18391838
/// vector.body:
1840-
/// ...
1841-
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
1842-
/// [ %NextEVLIV, %vector.body ]
1843-
/// %AVL = sub original TC, %EVLPhi
1839+
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT
1840+
/// %AVL = sub original TC, %IVPhi
18441841
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %AVL
18451842
/// ...
1846-
/// %NextEVLIV = add IVSize (cast i32 %VPEVVL to IVSize), %EVLPhi
1843+
/// %IV.NEXT = add %IVPhi IVSize (cast i32 %VPEVL to IVSize)
18471844
/// ...
18481845
///
18491846
/// If MaxSafeElements is provided, the function adds the following recipes:
18501847
/// vector.ph:
18511848
/// ...
18521849
///
18531850
/// vector.body:
1854-
/// ...
1855-
/// %EVLPhi = EXPLICIT-VECTOR-LENGTH-BASED-IV-PHI [ %StartV, %vector.ph ],
1856-
/// [ %NextEVLIV, %vector.body ]
1851+
/// %IVPhi = CANONICAL-INDUCTION ir<0>, %IV.NEXT
18571852
/// %AVL = sub original TC, %EVLPhi
18581853
/// %cmp = cmp ult %AVL, MaxSafeElements
18591854
/// %SAFE_AVL = select %cmp, %AVL, MaxSafeElements
18601855
/// %VPEVL = EXPLICIT-VECTOR-LENGTH %SAFE_AVL
18611856
/// ...
1862-
/// %NextEVLIV = add IVSize (cast i32 %VPEVL to IVSize), %EVLPhi
1857+
/// %IV.NEXT = add %IVPhi IVSize (cast i32 %VPEVL to IVSize)
18631858
/// ...
18641859
///
18651860
bool VPlanTransforms::tryAddExplicitVectorLength(
@@ -1875,16 +1870,11 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
18751870
return false;
18761871

18771872
auto *CanonicalIVPHI = Plan.getCanonicalIV();
1878-
VPValue *StartV = CanonicalIVPHI->getStartValue();
1879-
1880-
// Create the ExplicitVectorLengthPhi recipe in the main loop.
1881-
auto *EVLPhi = new VPEVLBasedIVPHIRecipe(StartV, DebugLoc());
1882-
EVLPhi->insertAfter(CanonicalIVPHI);
18831873
VPBuilder Builder(Header, Header->getFirstNonPhi());
18841874
// Compute original TC - IV as the AVL (application vector length).
1885-
VPValue *AVL = Builder.createNaryOp(Instruction::Sub,
1886-
{&Plan.getVectorTripCount(), EVLPhi},
1887-
DebugLoc(), "avl");
1875+
VPValue *AVL = Builder.createNaryOp(
1876+
Instruction::Sub, {&Plan.getVectorTripCount(), CanonicalIVPHI},
1877+
DebugLoc(), "avl");
18881878
if (MaxSafeElements) {
18891879
// Support for MaxSafeDist for correct loop emission.
18901880
VPValue *AVLSafe = Plan.getOrAddLiveIn(
@@ -1897,29 +1887,20 @@ bool VPlanTransforms::tryAddExplicitVectorLength(
18971887

18981888
auto *CanonicalIVIncrement =
18991889
cast<VPInstruction>(CanonicalIVPHI->getBackedgeValue());
1900-
Builder.setInsertPoint(CanonicalIVIncrement);
19011890
VPSingleDefRecipe *OpVPEVL = VPEVL;
19021891
if (unsigned IVSize = CanonicalIVPHI->getScalarType()->getScalarSizeInBits();
19031892
IVSize != 32) {
19041893
OpVPEVL = Builder.createScalarCast(
19051894
IVSize < 32 ? Instruction::Trunc : Instruction::ZExt, OpVPEVL,
19061895
CanonicalIVPHI->getScalarType(), CanonicalIVIncrement->getDebugLoc());
19071896
}
1908-
auto *NextEVLIV = Builder.createOverflowingOp(
1909-
Instruction::Add, {OpVPEVL, EVLPhi},
1910-
{CanonicalIVIncrement->hasNoUnsignedWrap(),
1911-
CanonicalIVIncrement->hasNoSignedWrap()},
1912-
CanonicalIVIncrement->getDebugLoc(), "index.evl.next");
1913-
EVLPhi->addOperand(NextEVLIV);
19141897

19151898
transformRecipestoEVLRecipes(Plan, *VPEVL);
19161899

1917-
// Replace all uses of VPCanonicalIVPHIRecipe by
1918-
// VPEVLBasedIVPHIRecipe except for the canonical IV increment.
1919-
CanonicalIVPHI->replaceAllUsesWith(EVLPhi);
1920-
CanonicalIVIncrement->replaceAllUsesWith(NextEVLIV);
1900+
// Adjust the increment of the canonical induction variable to an explicit
1901+
// vector length.
19211902
CanonicalIVIncrement->setOperand(0, CanonicalIVPHI);
1922-
CanonicalIVPHI->setOperand(1, CanonicalIVIncrement);
1903+
CanonicalIVIncrement->setOperand(1, OpVPEVL);
19231904
// TODO: support unroll factor > 1.
19241905
Plan.setUF(1);
19251906
return true;
@@ -2103,17 +2084,14 @@ void VPlanTransforms::convertToConcreteRecipes(VPlan &Plan) {
21032084
for (VPBasicBlock *VPBB : VPBlockUtils::blocksOnly<VPBasicBlock>(
21042085
vp_depth_first_deep(Plan.getEntry()))) {
21052086
for (VPRecipeBase &R : make_early_inc_range(VPBB->phis())) {
2106-
if (!isa<VPCanonicalIVPHIRecipe, VPEVLBasedIVPHIRecipe>(&R))
2107-
continue;
2108-
auto *PhiR = cast<VPHeaderPHIRecipe>(&R);
2109-
StringRef Name =
2110-
isa<VPCanonicalIVPHIRecipe>(PhiR) ? "index" : "evl.based.iv";
2111-
auto *ScalarR = new VPInstruction(
2112-
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()},
2113-
PhiR->getDebugLoc(), Name);
2114-
ScalarR->insertBefore(PhiR);
2115-
PhiR->replaceAllUsesWith(ScalarR);
2116-
PhiR->eraseFromParent();
2087+
if (auto *PhiR = dyn_cast<VPCanonicalIVPHIRecipe>(&R)) {
2088+
auto *ScalarR = new VPInstruction(
2089+
Instruction::PHI, {PhiR->getStartValue(), PhiR->getBackedgeValue()},
2090+
PhiR->getDebugLoc(), "index");
2091+
ScalarR->insertBefore(PhiR);
2092+
PhiR->replaceAllUsesWith(ScalarR);
2093+
PhiR->eraseFromParent();
2094+
}
21172095
}
21182096
}
21192097
}

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)